Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Boost libraries return things "convertible to `bool`" rather than just returning `bool`s?

Tags:

c++

boost

Several times, while perusing the Boost library's documentation, I've run across return values that are marked "convertible to bool" (search that page for the phrase "convertible to bool", it's about a third of the way down). I once stumbled across an oblique reference to a paper explaining the reason for that, but have never been able to find said paper (and I can no longer find the page I saw the reference on either).

Can anyone explain why (and when) you should return something that's "convertible to bool" rather than simply returning a bool?

like image 822
Head Geek Avatar asked Nov 04 '08 19:11

Head Geek


2 Answers

“convertible to bool” simply means anything which can meaningfully be used in a boolean context (e.g. in an if condition). This makes sense in implicit conversions. Imagine an object which you want to use in a boolean context, e.g. std::fstream:

ifstream ifs("filename");
while (ifs >> token)
    cout "token " << token << " read." << endl;

Here, ifs is convertible to boolean. Well, actually, it isn't. Rather, it is convertible to something that, in turn, is convertible to bool. This is to prevent such statements:

int b = ifs;

The reasoning is that such a statement is most probably not intended and the compiler should therefore prevent it. By returning a “convertible to bool” rather than a bool, this is achieved because two user-defined implicit conversions can't be chained in one expression.

In this context, you might want to look up the safe bool idiom. Chris has already alluded to one possible implementation, using void* as a return type. Usually, the this pointer is then used to represent true. This is what gets used by the STL. However, this is unfortunately still flawed. Several alternatives have been proposed (neatly wrapped up in the article I've linked above) and as far as I know, have also been included into C++0x for consideration. I'm not aware of the current status of these proposals, though.

like image 86
Konrad Rudolph Avatar answered Oct 21 '22 04:10

Konrad Rudolph


bools are promotable to ints and can participate in arithmetic operations. This is often not the desired outcome, when a value should just be used for truth testing.

A convertible-to-bool is usually something like a void*, where the null pointer is false, and anything else is true, and yet can't be used for arithmetic operations.

like image 45
Chris Jester-Young Avatar answered Oct 21 '22 04:10

Chris Jester-Young