我可以为任何 C++ 编译器假设 (bool)true == (int)1 吗?

2021-12-31 00:00:00 boolean casting c++

我可以为任何 C++ 编译器假设 (bool)true == (int)1 吗?

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

推荐答案

是的.演员表是多余的.在你的表达中:

Yes. The casts are redundant. In your expression:

true == 1

积分提升适用,bool 值将提升为 int 并且此提升必须产生 1.

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

参考:4.7 [conv.integral]/4:如果源类型为bool...true 转换为1.

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

相关文章