为什么C++中bool和int8_t的普通类型是int32_t?

Why is the common type of bool and int8_t an int32_t in C++?

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

简答:积分促销

在数值运算中,小整数类型(包括boolcharunsigned charsigned charshortunsigned short、 etc) 如果所有可能的值都符合 int,则被提升为 int,否则它们被提升为 unsigned int.

在今天的大多数机器上,int32_tint 相同。在 bool + int8_tbool + int16_t 的情况下,两者都被提升为 int.