简体   繁体   English

单个语句中的多个复合赋值:是否为未定义行为?

[英]Multiple compound assignments in a single statement: is it Undefined Behavior or not?

I can't find a definitive answer for this: does the following code have undefined behavior? 我无法找到明确的答案:以下代码是否有未定义的行为?

int x = 2;
x+=x+=x+=2.5;

The behavior is undefined. 行为未定义。 Let's look at the slightly simpler expression: 让我们看一下稍微简单的表达式:

x += (x+=1)

In C++11, the value computation of the left x is unsequenced relative to the value computation of the expression (x+=1) . 在C ++ 11中,左x的值计算相对于表达式的值计算(x+=1)是未序列的。 This means that value computation of x is unsequenced relative to the assignment to x (due to x+=1 ), and therefore the behavior is undefined. 这意味着x值计算相对于x的赋值是无序的(由于x+=1 ),因此行为是未定义的。

The reason for this is that the value computation of the two sides of the += operator are unsequenced relative to each other (as the standard doesn't specify otherwise). 这样做的原因是+=运算符的两边的值计算相对于彼此是无序的(因为标准没有另外指定)。 And 1.9p15 states: 并且1.9p15州:

If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. 如果对标量对象的副作用相对于同一标量对象的另一个副作用或使用相同标量对象的值进行的值计算未被排序,则行为未定义。

In C++03 the behavior is undefined because x is modified twice without an intervening sequence point. 在C ++ 03中,行为未定义,因为x被修改两次而没有插入序列点。

For standard quotes see the other answers. 对于标准报价,请参阅其他答案。 It is likely to find one of two different behaviours in this case. 在这种情况下,它可能会找到两种不同的行为之一。

x += (x += 2);

May either be 可能是

x = 2 + 4 (= 6)

if the value of x on left side is evaluated before x+=2 or 如果在x+=2或之前评估左侧x的值

x = 4 + 4 (= 8)

if the value of x for the left operator is determined afterwards. 如果之后确定左操作符的x值。


-edit- -编辑-

I know I not gonna get many fans on SO if I say I don't like those "anything may happen" claims very much. 我知道如果我说我不喜欢那些“任何可能发生的事情”的话,我就不会得到很多粉丝。 It is true that any compiler can declare itself standard conformant regardless of how the statement we discuss here is handled with respect to the value of x. 确实,无论我们在这里讨论的语句如何处理x的值,任何编译器都可以声明自己的标准符合。 Nevertheless, I think it doesn't mean that operator += may result in a wrong result or that parantheses may be ignored. 尽管如此,我认为这并不意味着operator + =可能会导致错误的结果,或者可能会忽略parantheses。 Undefined behaviour is not the same as undefined behaviour in any other case. 未定义的行为与任何其他情况下的未定义行为不同。

It is bad to back on any expectation regarding undefined behaviour but in the above example i see good reasons for neglecting any possible outcome but 6 and 8. 回到对未定义行为的任​​何期望是不好的,但在上面的例子中,我看到了忽略任何可能结果但6和8的充分理由。

In addition I actually suspect x to be 8 after the evaluation of int x=2; x += (x += 2); 另外我实际上怀疑在评估int x=2; x += (x += 2);之后x为8 int x=2; x += (x += 2); int x=2; x += (x += 2); for most of the established compilers (clang, g++, vc, icpc...). 对于大多数已建立的编译器(clang,g ++,vc,icpc ......)。

It is to be said again that you shouldn't rely on such behaviour but that doesn't mean that it is completely unpredictable. 再说一遍,你不应该依赖这种行为,但这并不意味着它完全不可预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM