Consider the following code:
int main(){
int i = 0;
int a = ++i + ++i;
}
I can't find any information that says that the operands of +
are unsequenced. So according to the standard, the sequence of operands of binary +
are indeterminately sequenced.
Given any two evaluations A and B, if A is sequenced before B (or, equivalently, B is sequenced after A), then the execution of A shall precede the execution of B. If A is not sequenced before B and B is not sequenced before A, then A and B are unsequenced. [ Note: The execution of unsequenced evaluations can overlap. — end note ]
Evaluations A and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A, but it is unspecified which.[ Note: Indeterminately sequenced evaluations cannot overlap, but either could be executed first. — end note ]
The quote means that the evaluation of A can occur before B or the evaluation of B can occur before A. And the execution of unsequenced evaluations can overlap, whereas indeterminately sequenced evaluations cannot overlap, which are different.
We know the modification of i
always occurs before value computation of i
due to prefix ++
.
Then according to the rules:
Evaluation of an expression (or a subexpression) in general includes both value computations (including determining the identity of an object for glvalue evaluation and fetching a value previously assigned to an object for prvalue evaluation) and initiation of side effects
If a side effect on a memory location is unsequenced relative to either another side effect on the same memory location or a value computation using the value of any object in the same memory location, and they are not potentially concurrent, the behavior is undefined
So regardless of whether the evaluation of A is before B or the converse, there are no side effects related to corresponding value computation or side effect for ++i + ++i;
. Because indeterminately sequenced evaluations cannot overlap, one of the two evaluation must be completely executed before the other. The evaluation includes both value computation and side effect. Therefore, one increment to i
is evaluated before the other.
Unsequenced evaluations, however, follow different rules, so the confusion will be resolved if the evaluations of operands of binary +
are unsequenced rather than indeterminately sequenced. If I missed something in the standard in the analysis above, please correct me.
Update
I found the following sentence, which seems to suggest that the evaluations are unsequenced:
Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced.
However, I don't know how to understand the sentence correctly. I came up with two interpretations:
For an operator A, the evaluations of the operands of A are unsequenced with each other; for an expression B, the evaluations of the subexpressions of B are unsequenced with each other.
and
Take evaluations of operands of individual operators as A. Take evaluations of subexpressions of individual expressions as B. A is unsequenced with B.
Which interpretation is correct?