Was boost::bool_testable<> relocated or removed

2019-02-19 10:42发布

问题:

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 library (1.49 as of this post) doesn't seem to have it anymore.

Where did it go? Is there a better alternative available now, and I've just missed it?

I'm not able to use C++11 and therefore cannot use the language extensions that render safe bool unnecessary.

回答1:

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.