我可以为任何 C++ 编译器假设 (bool)true == (int)1 吗? [英] Can I assume (bool)true == (int)1 for any C++ compiler?

查看:21
本文介绍了我可以为任何 C++ 编译器假设 (bool)true == (int)1 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以为任何 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.

这篇关于我可以为任何 C++ 编译器假设 (bool)true == (int)1 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆