cpp/language/eval order

Order of evaluation of any part of any expression, including order of evaluation of function arguments is unspecified (with some exceptions listed below). The compiler can evaluate operands and other subexpressions in any order, and may choose another order when the same expression is evaluated again.

There is no concept of left-to-right or right-to-left evaluation in C++. This is not to be confused with left-to-right and right-to-left associativity of operators: the expression is parsed as  due to left-to-right associativity of operator+, but  may be evaluated first, last, or between  or  at run time:

Evaluation of Expressions
Evaluation of each expression includes:
 * Value computations: calculation of the value that is returned by the expression. This may involve determination of the identity of the object (glvalue evaluation, e.g. if the expression returns a reference to some object) or reading the value previously assigned to an object (prvalue evaluation, e.g. if the expression returns a number, or some other value).
 * Initiation of side effects: access (read or write) to an object designated by a volatile glvalue, modification (writing) to an object, calling a library I/O function, or calling a function that does any of those operations.

Ordering
Sequenced before is an asymmetric, transitive, pair-wise relationship between evaluations within the same thread.


 * If A is sequenced before B (or, equivalently, B is sequenced after A), then evaluation of A will be complete before evaluation of B begins.
 * If A is not sequenced before B and B is sequenced before A, then evaluation of B will be complete before evaluation of A begins.
 * If A is not sequenced before B and B is not sequenced before A, then two possibilities exist:
 * Evaluations of A and B are unsequenced: they may be performed in any order and may overlap (within a single thread of execution, the compiler may interleave the CPU instructions that comprise A and B).
 * Evaluations of A and B are indeterminately sequenced: they may be performed in any order but may not overlap: either A will be complete before B, or B will be complete before A. The order may be the opposite the next time the same expression is evaluated.

Rules
@1@ Each value computation and side effect of a is sequenced before each value computation and side effect of the next full-expression.

@2@ The value computations (but not the side effects) of the operands to any are sequenced before the value computation of the result of the operator (but not its side effects).

@3@ When calling a function (whether or not the function is inline, and whether or not explicit function call syntax is used), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function.

@4@ The value computation of the built-in operators is sequenced before its side effect.

@5@ The side effect of the built-in operators is sequenced before its value computation (implicit rule due to definition as compound assignment).

@6@ Every value computation and side effect of the first (left) argument of the built-in AND operator  and the built-in logical OR operator  is sequenced before every value computation and side effect of the second (right) argument.

@7@ Every value computation and side effect associated with the first expression in the  is sequenced before every value computation and side effect associated with the second or third expression.

@8@ The side effect (modification of the left argument) of the built-in operator and of all built-in  assignment operators is sequenced after the value computation (but not the side effects) of both left and right arguments, and is sequenced before the value computation of the assignment expression (that is, before returning the reference to the modified object).

@9@ Every value computation and side effect of the first (left) argument of the built-in  is sequenced before every value computation and side effect of the second (right) argument.

@10@ In, every value computation and side effect of a given initializer clause is sequenced before every value computation and side effect associated with any initializer clause that follows it in the brace-enclosed comma-separated list of initializers.

@11@ A function call that is not sequenced before or sequenced after another expression evaluation outside of the function (possibly another function call) is indeterminately sequenced with respect to that evaluation (the program must behave the CPU instructions that constitute a function call were not interleaved with instructions constituting evaluations of other expressions, including other function calls, even if the function was inlined).

@12@ The call to the allocation function is  the evaluation of the constructor arguments in a.

@13@ When returning from a function, copy-initialization of the temporary that is the result of evaluating the function call is sequenced before the destruction of all temporaries at the end of the operand of the, which, in turn, is sequenced before the destruction of local variables of the block enclosing the return statement.

Undefined behavior
1) If a side effect on a memory location is unsequenced relative to another side effect on the same memory location,.

2) If a side effect on a memory location is unsequenced relative to a value computation using the value of any object in the same memory location,.

Pre-C++11 Definitions
Evaluation of an expression might produce side effects, which are: accessing an object designated by a volatile lvalue, modifying an object, calling a library I/O function, or calling a function that does any of those operations.

A sequence point is a point in the execution sequence where all side effects from the previous evaluations in the sequence are complete, and no side effects of the subsequent evaluations started.

Pre-C++11 Rules
1) There is a sequence point at the end of each (typically, at the semicolon).

2) When calling a function (whether or not the function is inline and whether or not function call syntax was used), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body.

3) When returning from a function, there is a sequence point after the copy-initialization of the result of the function call, and before the destruction of all temporary objects at the end of in the  (if any).

4) There is a sequence point after the copying of a returned value of a function and before the execution of any expressions outside the function.

5) Once the execution of a function begins, no expressions from the calling function are evaluated until execution of the called function has completed (functions cannot be interleaved). 6) In the evaluation of each of the following four expressions, using the built-in (non-overloaded) operators, there is a sequence point after the evaluation of the expression.

Pre-C++11 Undefined behavior
1) Between the previous and next sequence point, the value of any object in a memory location must be modified at most once by the evaluation of an expression, otherwise.

2) Between the previous and next sequence point, for any object in a memory location, its prior value that is modified by the evaluation of the expression must be accessed only to determine the value to be stored. If it is accessed in any other way,.