11

I can't find a definitive answer for this: does the following code have undefined behavior?

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

2 回答 2

14

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). 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.

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:

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.

于 2013-06-18T10:58:45.030 回答
0

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 = 4 + 4 (= 8)

if the value of x for the left operator is determined afterwards.


-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. Nevertheless, I think it doesn't mean that operator += may result in a wrong result or that parantheses may be ignored. 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.

In addition I actually suspect x to be 8 after the evaluation of int x=2; x += (x += 2); for most of the established compilers (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.

于 2013-06-18T11:08:16.250 回答