I'm using someone else's code that was written with an older compiler that mapped a special BOOL
type to an unsigned int
, but in my compiler it's mapped to a true bool
. In some places in his code he uses the bitwise shift operator <<
on the bool
type, which I had never seen before and my compiler surprised me when it didn't complain.
Is that valid C++? Does the bool
automatically get promoted to an int
or uint
?
I saw this related question, which provided some clarity on another issue, but it doesn't address the shift operators.
From Shift operators [expr.shift]
The operands shall be of integral or unscoped enumeration type and integral promotions are performed. The type of the result is that of the promoted left operand
bool
is an integral type so the code is well formed (bool
is promoted to int
and result is an int
).
From [conv.prom], we show what integers the booleans get promoted to:
A prvalue of type
bool
can be converted to a prvalue of typeint
, withfalse
becoming zero and true becomingone
Afterwards, the shift behaves normally. (Thanks, @chris)