标准有误?

时间:2011-01-22 11:26:01

标签: c++ standards

§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

正确?

4 个答案:

答案 0 :(得分:7)

是的,请参阅此缺陷报告:http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#351

澄清:示例错误,但第一个语句的“修复”不正确。第一个语句在标准中正确评论。这只是第二个不准确的评论。

答案 1 :(得分:6)

i = 7, i++, i++; // i becomes 9

非常好。运算符=的优先级高于,,因此表达式等效于

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

这是完全明确定义的行为,因为,是一个序列点。

至于

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

关注的行为是undefined in C++03,但well defined in C++0x。如果你有C ++ 0x草案,你可以在1.9/15

部分查看
i = i++ + 1; // the behavior is undefined

答案 2 :(得分:4)

不,标准是对的。 comma operator保证在评估下一个操作数之前完成先前操作数的任何副作用。

这些保证由sequence points提供,逗号运算符(以及&&||)是。

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

答案 3 :(得分:2)

  

应该改为

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

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

逗号(,)在计算中引入sequence point,因此定义了执行顺序。