Can I assume (bool)true == (int)1 for any C++ compiler?

Petruza picture Petruza · Apr 27, 2010 · Viewed 105.8k times · Source

Can I assume (bool)true == (int)1 for any C++ compiler ?

Answer

CB Bailey picture CB Bailey · Apr 27, 2010

Yes. The casts are redundant. In your expression:

true == 1

Integral promotion applies and the bool value will be promoted to an int and this promotion must yield 1.

Reference: 4.7 [conv.integral] / 4: If the source type is bool... true is converted to one.