tags:

views:

490

answers:

3

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?

A: 

Maybe for performance? In C/C++ you can do an if statement on numbers (0 is false, anything else is true). Converting to a strict bool is an extra operation, which in many cases wouldn't be required.

I haven't actually used boost, so that's just a guess, but it seems like a reasonable one to me.

Herms
+10  A: 

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.

Chris Jester-Young
+19  A: 

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

Konrad Rudolph
That's the article I hadn't been able to find. Thanks!
Head Geek
The current status is explicit conversion operators, which make the safe bool idiom superfluous.
Sebastian Redl
@Sebastian, does it? Will "if (f)" call the explicit operator bool() or will you need "if (bool(f))"? It's not like the standard defines the parameter for if as being a bool, merely convertible to bool.
jmucchiello
@jmucchiello: yes it does indeed. It’s the expressed intent of this amendment to address the current shortcomings in the context of condition expressions. For that purpose, the context of e.g. `if(cond)` will count as an explicit conversion, without the need to write it as such.
Konrad Rudolph