(a=1)=2
was undefined prior to C++11, as the =
operator did not introduce a sequence point and therefore a
is modified twice without an intervening sequence point. The same applies to (a+=1)%=7
The text was:
Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.
It's worth mentioning that the description of the assignment operator is defective:
The result of the assignment operation is the value stored in the left operand after the assignment has taken place; the result is an lvalue.
If the result is an lvalue then the result cannot be the stored value (that would be an rvalue). Lvalues designate memory locations. This sentence seems to imply an ordering relation, but regardless of how we want to interpret it, it doesn't use the term "sequence point" and therefore the earlier text about sequence points applies.
If anything, that wording casts a bit of doubt on expressions like (a=1) + 2
. The C++11 revision of sequencing straightened out all these ambiguities.
int a = 2; (a = 1) = a;
then that is undefined (asclang-cl
has it: "unsequenced modification and access to 'a'").+=
and%=
operators will both modifya
- gives undefined behaviour.