简体   繁体   中英

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

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 ?

“convertible to bool” simply means anything which can meaningfully be used in a boolean context (eg in an if condition). This makes sense in implicit conversions. Imagine an object which you want to use in a boolean context, eg 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.

bool s are promotable to int s 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM