Is there a safe bool idiom helper in boost? [close

2019-04-18 11:35发布

25% of programmers work time is spended by checking if the required code already exist.

I'm searching for a base class for implementing the safe bool idiom.

2条回答
forever°为你锁心
2楼-- · 2019-04-18 12:33

bool_testable<> in Boost.Operators looks promising.

The reference mentions that:

bool_testable provides the antithesis of operator bool, such that the expression if (!p) is valid, whilst also making operator bool safer by preventing accidental conversions to integer types. ... bool_testable<> prevents these accidental conversions by declaring a private conversion operator to signed char, and not defining the body.

查看更多
Anthone
3楼-- · 2019-04-18 12:33

The safest thing to do is to not have implicit conversion to bool.

Make that conversion explicit.

Then the name, if chosen well, can also help people understand what it's all about.

Cheers & hth.,

查看更多
登录 后发表回答