为什么是布尔在C ++中int32_t,并在中int8_t常见的类型? [英] Why is the common type of bool and int8_t an int32_t in C++?
问题描述
我很好奇一些内置的布尔
键入C ++的行为。据我了解, 的std :: common_type
决定使用隐式可兑换常见的类型。我期望与布尔
和其他类型的前pression将导致布尔转换为该类型。例如,我可以看到布尔+浮动
- > 浮动
和布尔+双
- > 双击
。然而,布尔+中int8_t
- > int32_t
和布尔+ int16_t
- > int32_t
。为什么会出现这种情况?
I'm curious about some of the behavior of the builtin bool
type in C++. As I understand it, std::common_type
determines the common type using implicit convertibility. I would expect that an expression with bool
and another type would cause bool to convert to that type. For instance, I can see that bool + float
-> float
and bool + double
-> double
. However, bool + int8_t
-> int32_t
and bool + int16_t
-> int32_t
. Why is this the case?
推荐答案
简短的回答:积分的推广
在数值运算,小整型(包括布尔
,字符
, unsigned char型
,符号字符
,短
,无符号短
等)被晋升为 INT
如果所有可能的值适合 INT
,否则会被提拔到 unsigned int类型
。
In numerical arithmetic, small integral types (including bool
, char
, unsigned char
, signed char
, short
, unsigned short
, etc) are promoted to int
if all the possible values fit in int
, otherwise they are promoted to unsigned int
.
在大多数机器的今天, int32_t
相同 INT
。在的情况下,布尔+中int8_t
或布尔+ int16_t
,两者都提升到 INT
。
On most machines today, int32_t
is the same as int
. In the case of bool + int8_t
or bool + int16_t
, both are promoted to int
.
这篇关于为什么是布尔在C ++中int32_t,并在中int8_t常见的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!