Was boost::bool_testable<> relocated or removed?

前端 未结 1 1410
情深已故
情深已故 2021-01-15 06:44

I\'m trying to leverage boost::bool_testable<> (from Boost.Operators) to implement the safe bool idiom for a class, but the most recent version of the lib

相关标签:
1条回答
  • 2021-01-15 07:12

    It's a late answer, but I'm only active on Stack Overflow for a short time and I just found your question. I am the maintainer of Boost.Operators and I removed bool_testable back in December 2003 before it could accidentially be released.

    Sam Partington proposed it a few weeks earlier and I added it to the CVS repository. It looked promising in the beginning, but soon problems showed up in certain scenarious.

    The main problem, IIRC, for a class T derived from boost::bool_testable<T> had to do with conversion detection. A class which is convertible to bool, but not to int, should yield boost::is_convertible<T,int>::value == false, but instead, it became ambiguous and you ended up with a compile failure.

    There were also other issues and solving one of them usually implied breaking another. One example involved types where the user wanted explicit conversion to bool and his own operator int().

    So, long story short, we never figured out how to make it robust enough. In case of doubt, the benefit was too small (safe ~5 lines of copy-paste code) compared with the potential problems, that I decided to play it safe and hence I removed it.

    After it was removed, the issue never came up again and people eventually started to either copy-paste the Safe-Bool-Idiom code to their classes, or (some time later) they started to use explicit operator bool() as it became available.

    That said, it's best if you just copy the lines manually. I know it's not an elegant solution and I don't like copy-paste either, but the alternatives were all worse than that.

    0 讨论(0)
提交回复
热议问题