【发布时间】:2018-08-26 08:57:36
【问题描述】:
和cout语句的执行顺序有关系吗?
代码:
int main() {
int x=80;
int &y=x;
cout<<x<<" "<<(--y)<<endl;
return 0;
}
输出: 79 79
【问题讨论】:
标签: c++ reference cout operator-precedence decrement
和cout语句的执行顺序有关系吗?
代码:
int main() {
int x=80;
int &y=x;
cout<<x<<" "<<(--y)<<endl;
return 0;
}
输出: 79 79
【问题讨论】:
标签: c++ reference cout operator-precedence decrement
和cout的执行顺序有关系吗 声明?
这与order of evaluation 有关。在 c++17 之前,您的示例将表现出未定义的行为:
如果标量对象的副作用相对于某个值是无序的 使用相同标量对象的值进行计算,行为是 未定义。
从 c++17 开始,定义了行为,在这种情况下正确的输出是
80 79
【讨论】:
这是未定义的行为,意味着任何事情都可能发生。
欲了解更多信息,请参阅:Unexpected order of evaluation (compiler bug?)
【讨论】: