【问题标题】:Why is the common type of bool and int8_t an int32_t in C++?为什么 C++ 中 bool 和 int8_t 的常见类型是 int32_t?
【发布时间】:2015-09-06 23:43:26
【问题描述】:

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

【问题讨论】:

    标签: c++ c++11 boolean std


    【解决方案1】:

    简答:整体推广

    在数值算术中,如果所有可能的值都适合,小整数类型(包括boolcharunsigned charsigned charshortunsigned short 等)将提升为intint,否则他们被提升为unsigned int

    在当今的大多数机器上,int32_tint 相同。对于bool + int8_tbool + int16_t,两者都提升为int

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 2015-09-22
    • 2013-01-09
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多