Is there a safe bool idiom helper in boost? [closed]

…衆ロ難τιáo~ 提交于 2019-12-04 22:56:44

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.

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.,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!