单个语句中的多个增量运算符
Possible Duplicate:
Undefined Behavior and Sequence Points
Pleae explain the behaviour of following statements
int b=3;
cout<<b++*++b<<endl;
How will it be calculated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里的行为是未定义的。请参阅此问题
相关标准引用:
§5 /4.1 在上一个和下一个序列点之间,标量对象的存储值最多应通过表达式的求值修改一次。
最常见的序列点是语句的结尾。
标准中还值得注意的是:
§5.2.2/8 参数的求值顺序未指定。
The behavior here is undefined. See this question
Relevant standard quote:
§5/4.1 Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.
The most common sequence point is the end of a statement.
Also worth noting from the standard:
§5.2.2/8 The order of evaluation of arguments is unspecified.
标准说这是未定义的。只要遵循运算符优先级规则,编译器就可以以任何认为合适的顺序自由地计算语句。 UB 的结果是:
The standard says this is undefined. The compiler is free to evaluate the statements in any order is sees fit as long as it follows the operator precedence rules. This results in UB:
正如其他人所说,该行为将是不确定的。
输出取决于编译器的实现。
但根据标准,它应该是未定义的。
The behavior will be undefined as told by others.
The output depends upon the implementation of compiler.
But as per the standard it should be undefined.
由于这是一种未定义的行为,因此无法说出最终结果。结果取决于实施。
AS this is undefined behaviour, one can't tell the end result. The result depends on the implementation.
未定义的行为,由于运算符的优先级相同,编译器可以以任何顺序自由地计算此表达式。考虑
改用
Undefined behavior, Compiler is free to evaluate this expression in any order because of the same precedence of the operators. Consider using
instead