"warning: operation of ... may be undefined"用于三元操作 - 不是if/else块
"warning: operation of ... may be undefined" for ternary operation -- not if/else block
这是我的代码:
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
语句?
它们不等效。请注意,在三元运算符表达式中,您将结果分配给test
将if
条件更改为:
if(anotherInt > test)
test = test++; // undefined!
您可能也会在这里看到相同的警告。
你可能知道在这段代码中:otherInt>test ? test++ : 0; 计算机可能先运行 test++,也许运行 otherInt>test ? first.so 表达式中,如果使用变量,u 不应该在这个表达式中的其他地方更改它。您可以将 test++ 更改为 test+1。
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 重载操作程序时出错>>用于类中的字符串 memebr
- 对字符串进行位操作
- 我可以在 C++ 中的函数体之外进行操作吗?
- MPI突然停止了对多个核心的操作
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 对字符数组中的元素执行逐位操作
- 如何在directx/c++中进行平移/缩放操作
- 逐位操作的隐式类型转换
- 为什么一个向量上的多线程操作很慢
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- 为什么三元操作失败不咳嗽
- 不兼容的操作原则到三元op
- 三元操作的结果(类型)是什么
- "warning: operation of ... may be undefined"用于三元操作 - 不是if/else块
- 空白或不执行任何操作的三元运算符的第二个或第三个参数
- 使用流进行三元操作安全吗?
- 为什么在std::max实现中使用if-else而不是三元操作?
- 铸造指针和三元?:操作人员我重新发明轮子了吗