标准有误?

发布于 2024-10-13 01:39:57 字数 262 浏览 2 评论 0原文

§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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最后的乘客 2024-10-20 01:39:57

是的,请参阅此缺陷报告: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.

留一抹残留的笑 2024-10-20 01:39:57
i = 7, i++, i++; // i becomes 9

完全没问题。运算符 = 的优先级高于 因此该表达式等效于

(i = 7), i++, i++; 

is 完美定义的行为,因为 , 是一个序列点。

就目前

i = ++i + 1; //the behavior is unspecified

而言,该行为是未定义< /strong> 在 C++03 中,但 C++0x 中定义明确。如果您有 C++0x 草案,可以查看 1.9/15 部分

i = i++ + 1; // the behavior is undefined
i = 7, i++, i++; // i becomes 9

is perfectly fine. Operator = has higher precedence than ,so the expression is equivalent to

(i = 7), i++, i++; 

which is perfectly well defined behaviour because , is a sequence point.

As far as

i = ++i + 1; //the behavior is unspecified

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

i = i++ + 1; // the behavior is undefined
一梦浮鱼 2024-10-20 01:39:57

不,标准是对的。 逗号运算符保证前一个操作数的任何副作用在评估下一个操作数之前完成。

这些保证由 序列点 提供,其中逗号运算符(以及 & &||) 是。

请注意,您对第二个声明的措辞更改是正确的。它是未定义的,而不是未指定的。

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.

执笏见 2024-10-20 01:39:57

应该改为

i = 7, i++, i++; // 行为未定义

?标准是正确的,不应该改变,并且这种行为是明确定义的。

逗号 (,) 在计算中引入 序列点,以便执行顺序已定义。

That should be changed to

i = 7, i++, i++;  // the behavior is undefined

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文