Petruza Petruza - 1 year ago 68
C++ Question

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

Can I assume

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download