【发布时间】:2016-02-14 06:51:19
【问题描述】:
这是我的代码:
int main() {
static int test = 0;
const int anotherInt = 1;
test = anotherInt > test ? test++ : 0;
if (anotherInt > test)
test++;
else
test = 0;
return 0;
}
这是我构建它时产生的警告:
../main.cpp:15:40: warning: operation on ‘test’ may be undefined [-Wsequence-point]
test= anotherInt>test ? test++ : 0;
^
为什么 C++ 给我一个关于三元运算的警告,而不是常规的if..else 语句?
【问题讨论】:
-
您的
if不等价。你的三元正在做test = test++; -
您将
test++更改为test=test++,这有很大的不同。 -
请注意,undefined 指的是表达式的输出,而不是任何变量的输出。不同的编译器会以不同的方式评估“text = text++”;因此,它会被标记。
标签: c++ ternary-operator