从标准来看,我试图理解哪种类型的表达最终会是:
bool myBool; [...] uint8_t(255) * (myBool);我保证myBool将被转换为uint8_t (aka unsigned char ),或者整个结果可能是int吗?
有用的链接: 布尔到int转换
From the standard, I was trying to understand which type the expression will end up to be:
bool myBool; [...] uint8_t(255) * (myBool);Am I guaranteed that myBool will be casted to uint8_t (a.k.a. unsigned char), or the whole result might be int?
Useful link: bool to int conversion
最满意答案
从您链接到的文档:
5个表达式
9许多期望算术或枚举类型操作数的二元运算符以相似的方式导致转换和产生结果类型。 目的是产生一个共同的类型,这也是结果的类型。 这种模式被称为通常的算术转换,其定义如下:
...
- 否则,积分促销( conv.prom )应在两个操作数上执行.1 )
和
4.5积分促销
1如果int可以表示源类型的所有值,则可以将类型为char,signed char,unsigned char,short int或unsigned short int的右值转换为int类型的右值; 否则,源rvalue可以转换为unsigned int类型的右值
...
4 bool类型的右值可以转换为int类型的右值,其中false值为零,true为1。
在你的情况下,在算术运算之前,LHS和RHS都将被提升为int ,结果将是int类型。
From the document you linked to:
5 Expressions
9 Many binary operators that expect operands of arithmetic or enumera- tion type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
...
--Otherwise, the integral promotions (conv.prom) shall be performed on both operands.1)
and
4.5 Integral promotions
1 An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int
...
4 An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.
In your case, both the LHS and RHS will be promoted to int before the arithmetic operation and the resultant will be of type int.
更多推荐
发布评论