为什么bool和int8_t的常见类型是C++中的int32_t?

use*_*829 10 c++ boolean std c++11

我很好奇boolC++中内置类型的一些行为.据我了解,std::common_type使用隐式可转换性确定常见类型.我希望带有bool和另一种类型的表达式会导致bool转换为该类型.例如,我可以看到bool + float- > floatbool + double- > double.但是,bool + int8_t- > int32_tbool + int16_t- > int32_t.为什么会这样?

Yu *_*Hao 10

简答:积分推广.

在数值运算,小积分类型(包括bool,char,unsigned char,signed char,short,unsigned short,等等)被提升到int如果所有可能的值适合int,否则将它们提升到unsigned int.

在今天的大多数机器上,int32_t都是一样的int.在bool + int8_t或的情况下bool + int16_t,两者都被提升为int.

  • @ user2333829 [为什么在C和C++中算术运算之前必须将short转换为int?](http://stackoverflow.com/q/2437186​​8/995714) (3认同)