In C++, does the following have undefined behaviour:
int i = 0;
(i+=10)+=10;
There was some debate about this in the comments to my answer
In C++11 the expression is well defined and will result in i == 20
.
From [expr.ass]/1
:
In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression.
This means that the assignment i+=1
is sequenced before the value computation of the left hand side of (i+=10)+=10
, which is in turn sequenced before the final assignment to i
.
In C++03 the expression has undefined behavior, because it causes i
to be modified twice with no intervening sequence point.