What is the type of uint8_t multiplied by a boolean?

From standard , I was trying to figure out what type the expression would end in:

bool myBool;
[...]
uint8_t(255) * (myBool);

      

I'm sure it myBool

will be posted to uint8_t

(aka unsigned char

), or the whole result could be int

?

Useful link: bool to int conversion

+3


source to share


2 answers


From the doc you linked to:

5 expressions

9 Many binary operators expecting arithmetic or enumera-type operands invoke conversion calls and yield result type results in a similar manner. The goal is to get a generic type, which is also a result type. This pattern is called regular arithmetic conversion, which are defined as follows:

...

- Otherwise, integral actions (conv.prom) on both operands must be performed .1)

and



4.5 Integral promotions

1 rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to rvalue of type int if int can represent all values ​​of source type; otherwise the source rvalue can be converted to an rvalue of type unsigned int

...

4 rvalues ​​of type bool can be converted to r-value of type int, with false becomes null and true becomes single.

In your case, both LHS and RHS will be promoted int

to arithmetic, and the resulting will be of type int

.

+3


source


Both operands will advance to int

, and this will be the type of the result.

In general, integer operands are promoted to at least int

or, if necessary, a larger type, and arithmetic is not performed on smaller types. This is described in C ++ 11 4.5 (Integral Promotions).

For uint8_t

:

1 / Prvalue integer different from bool

, char16_t

, char32_t

or wchar_t

whose integer rank less than the rank transformation int

, it can be converted into prvalue type int

if int

can represent all values of the source type; otherwise, the original prvalue can be converted to a prvalue of type unsigned int

.



If it exists, then all 8-bit values ​​are uint8_t

represented int

(which must be at least 16 bits), so it int

is an advanced type.

For bool

:

6 / The pr value bool

can be converted to a prvalue of type int

, in this case it false

becomes equal to zero, and true

becomes one.

So it also goes up to int

, giving a general type of result int

.

+4


source







All Articles