Was boost::bool_testable<> relocated or removed?
Asked Answered
C

1

2

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.

Centrum answered 7/5, 2012 at 23:50 Comment(1)
According to my searching bool_testable was added, but then removed shortly after and never existed in a release (reference). In what version of boost did you see it?Cravens
D
6

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.

Delgadillo answered 27/3, 2013 at 0:7 Comment(3)
I guess that's that then. A great answer justifying its removal. Thanks!Centrum
Since it is almost 10 years after the events you described and C++ has evolved, do you think the idiom could be reintroduced in a safe & robust way today?Fayth
@Fayth There is no need to re-introduce any idiom as we have explicit operator bool() now. Or if you will, that is the "new" Safe-Bool-Idiom.Delgadillo

© 2022 - 2024 — McMap. All rights reserved.