标准有误?
§5/4 C++ 标准
i = 7, i++, i++; // i becomes 9
i = ++i + 1; //the behavior is unspecified
那应该改成对
i = 7, i++, i++; // the behavior is undefined
i = ++i + 1; //the behavior is undefined
吧?
§5/4 C++ standard
i = 7, i++, i++; // i becomes 9
i = ++i + 1; //the behavior is unspecified
That should be changed to
i = 7, i++, i++; // the behavior is undefined
i = ++i + 1; //the behavior is undefined
right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,请参阅此缺陷报告:http://www .open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351 。
澄清:该示例是错误的,但您的“修复”对于第一个语句是不正确的。标准中对第一个语句的注释是正确的。只有第二条评论不准确。
Yes, please see this defect report: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351 .
Clarification: the example is wrong but your 'fix' is incorrect for the first statement. The first statement is correctly commented in the standard. It is only the second comment that is inaccurate.
完全没问题。运算符
=
的优先级高于,
因此该表达式等效于is 完美定义的行为,因为
,
是一个序列点。就目前
而言,该行为是未定义< /strong> 在 C++03 中,但 C++0x 中定义明确。如果您有 C++0x 草案,可以查看
1.9/15
部分is perfectly fine. Operator
=
has higher precedence than,
so the expression is equivalent towhich is perfectly well defined behaviour because
,
is a sequence point.As far as
is concerned the behaviour is undefined in C++03 but well defined in C++0x. If you have the C++0x draft you can check out in section
1.9/15
不,标准是对的。 逗号运算符保证前一个操作数的任何副作用在评估下一个操作数之前完成。
这些保证由 序列点 提供,其中逗号运算符(以及
& &
和||
) 是。请注意,您对第二个声明的措辞更改是正确的。它是未定义的,而不是未指定的。
No, the standard is right. The comma operator guarantees that any side effects of previous operands are completed before evaluating the next.
These guarantees are provided by sequence points, which the comma operator (as well as
&&
and||
) are.Note that you are correct on the wording change for the second statement. It is undefined, not unspecified.
?标准是正确的,不应该改变,并且这种行为是明确定义的。
逗号 (
,
) 在计算中引入 序列点,以便执行顺序已定义。Why? The standard is correct, this shouldn’t be changed, and this behaviour is well-defined.
A comma (
,
) introduces a sequence point into the calculation so the order of execution is defined.