From the C++ 17 Standard (7.6 Integral promotions)
6 A prvalue of type bool can be converted to a prvalue of type int,
with false becoming zero and true becoming one.
and (7.14 Boolean conversions)
1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
to member type can be converted to a prvalue of type bool. A zero
value, null pointer value, or null member pointer value is converted
to false; any other value is converted to true. For
direct-initialization (11.6), a prvalue of type std::nullptr_t can be
converted to a prvalue of type bool; the resulting value is false.
and at last (8.7 Additive operators)
1 The additive operators + and - group left-to-right. The usual
arithmetic conversions are performed for operands of arithmetic or
enumeration type.
This expression statement
delayMessages += Delay ? 1 : -1;
can be rewritten like
delayMessages = delayMessages + ( Delay ? 1 : -1 );
The result of the expression with the conditional operator is -1
because the first sub-expression (Delay
) evaluates to false
.
So in fact you have
delayMessages = delayMessages + -1;
The variable delayMessage
declared like
bool delayMessages=0;
has the value false
according to the quotes from the section 7.14.
In the expression with binary plus operator + it is converted to the integer 0 according to the quotes (7.6 Integral promotions) and 8.7 Additive operators and you have
delayMessages = 0 + -1;
or
delayMessages = -1;
Again according to the quote 7.14 Boolean conversions the result value of the variable delayMessage
will be true
.
The operator << outputs a boolean value true as 1 in this statement
std::cout << "Hello world!"<<delayMessages;