Are C++ bools converted to ints during comparison?

Viewed 165

When using comparison operators in C++, are bools converted to ints?

The reason I ask is that the question of whether or not to always explicitly compare to true/false in if-statements came up. The two options being:

1) if (my_bool == true) doSomething();
2) if (my_bool) doSomething();

We were thinking you should generally avoid the explicit comparison (1) because of the following:

int myFunc(){return 4;}
if (myFunc() == true) doSomething();

Something like the code above would come up if you need to work with C interfaces that simply return nonzero to indicate "true". The myFunc() example would fail in C because myFunc returns 4, true is macro'd to 1, and 4 == 1 is not true.

Is this still the case in C++? Does the "equal to" operator convert the bool to an int rather than the other way around? References to the standard (C++11 is what I'm working with) are appreciated, but if it differs among versions of the language I'd be interested in knowing.

(I'd like to ask specifically about the pros/cons of explicit true/false comparison, but that seems like it could be subjective.)

2 Answers
Related