一元运算符的条件运算符的输出是什么

what is the output of conditional operator with unary operator

我有以下代码,其中的行为我不清楚。有人可以帮助条件运算符如何评估以下代码并将 ans 输出为 1

#include

int main() {

bool delayMessages=0;
bool Delay = false;
delayMessages += Delay ? 1 : -1;
std::cout << "Hello world!"<<delayMessages;

return 0;
} 

Ans: Hello world!1

请问 Soemone 能否帮助如何评估此代码“delayMessages += Delay ? 1 : -1;”

右边的表达式被计算为 if-statement。

if (Delay == true)
   return 1;
else
   return -1;

然后将结果用于 += 作业。

C++20 draft标准即

7.6.19 (6) (Assignment and compound assignment operators)

The behavior of an expression of the form E1 op= E2 is equivalent to E1 = E1 op E2 except that E1 is evaluated only once. [...]

Delay == false起,三元运算符的return值为-1。事实上,您在 boolean 而不是 int 上操作可以使它看起来像您得到了 +1

请注意,您会收到编译器警告 C4804:

warning C4804: '+=': unsafe use of type 'bool' in operation

这是未定义的行为吗?号

7.6.19 (6) (Assignment and compound assignment operators)

[...] For += and -=, E1 shall either have arithmetic type or be a pointer to a possibly cv-qualified completely-defined object type. In all other cases, E1 shall have arithmetic type.

7.3.8 (2) (Integral conversions)

If the destination type is bool, see 7.3.14.

这表示

7.3.14 (1) (Boolean conversions)

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.

所以 -1 被转换为 true 并且 true 打印为 1.

 Delay ? 1 : -1

计算为 -1 因为 Delay 为假。将 -1 转换为 bool 会产生 true,因为只有 0 会转换为 false。然后打印 true 打印 1。结果是 true 无论 Delaytrue 还是 false 并且在这两种情况下都打印 1

对布尔值使用 true / false,对整数使用整数类型。也许这就是您真正想要做的:

int delayMessages=0;
bool Delay = false;
delayMessages += Delay ? 1 : -1;
std::cout << "Hello world!"<<delayMessages;
delayMessages += Delay ? 1 : -1;

这基本上意味着如果 Delay 为真,则 delayMessages += 1(即 1),否则 delayMessages += -1(也为 1)。

来自 C++ 17 标准(7.6 积分提升)

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

和(7.14 布尔转换)

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.

最后(8.7 加法运算符)

1 The additive operators + and - group left-to-right. The usual arithmetic conversions are performed for operands of arithmetic or enumeration type.

这个表达式语句

delayMessages += Delay ? 1 : -1;

可以改写成

delayMessages = delayMessages + ( Delay ? 1 : -1 );

带有条件运算符的表达式的结果是 -1 因为第一个 sub-expression (Delay) 的计算结果是 false.

所以事实上你有

delayMessages = delayMessages + -1;

变量delayMessage声明为

bool delayMessages=0;
根据第 7.14 节的引述,

的值为 false

表达式中的二元加号 + 根据引号(7.6 整数提升)和 8.7 加法运算符转换为整数 0 得到

delayMessages = 0 + -1;

delayMessages = -1;

再次根据引用 7.14 布尔转换,变量 delayMessage 的结果值将是 true.

运算符<<在此语句中输出一个布尔值true为1

std::cout << "Hello world!"<<delayMessages;