3

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

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
  • 1
    In general, this makes no sense. Multiplying a number by `true` or `false` doesn't make sense. Just like multiplying the number 42 by orange. I suggest recoding to something that does make sense. Let the compiler optimize it. – Thomas Matthews Sep 18 '14 at 16:36
  • @ThomasMatthews I tend to disagree with your comment: the behaviour is well defined in the standard. – Antonio Sep 18 '14 at 16:53
  • So what is the semantic meaning of a number added together falsely? Remember that multiplication is repetitive additions. So you are adding *false* to a number. Again, `true` and `false` are Boolean or logic properties, not mathematical properties. They could be mapped to 4 and 5 or "frog" and "rover", and it still doesn't make sense. This is a *hack*. – Thomas Matthews Sep 18 '14 at 19:36
  • @ThomasMatthews I don't understand your statement: The standard defines to what true and false are mapped too... – Antonio Sep 19 '14 at 07:32

2 Answers2

4

Both operands will be promoted to int, and that will be the result type.

In general, integer operands are promoted to at least int, or a larger type if necessary, and arithmetic is not performed on smaller types. This is described by C++11 4.5 (Integral promotions).

For uint8_t:

1/ A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

If it exists, then all 8-bit uint8_t values are representable by int (which must be at least 16 bits), so int is the promoted type.

For bool:

6/ A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

So that is also promoted to int, giving an overall result type of int.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
3

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270