The answer depends on which version of the C++ standard you are using (or your compiler is using).
C++ 2003 5.2.2 p8 said:
The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations take effect before the function is entered. The order of evaluation of the postfix expression and the argument expression list is unspecified.
This means there is not a sequence point between evaluating f(x)
and args
.
In C++ 2011 the whole concept of sequence points has been replaced (see N1944), and that wording is now just a note:
[ Note: The evaluations of the postfix expression and of the argument expressions are all unsequenced relative to one another. All side effects of argument expression evaluations are sequenced before the function is entered (see 1.9). — end note ]
and 1.9 p15 says
When calling a function (whether or not the function is inline), 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. [ Note: Value computations and side effects associated with different argument expressions are unsequenced. — end note ]
This says the expression f(x)
and the expression args
are sequenced before everything in the body of g
, but that they are unsequenced relative to each other, which is the same as the C++03 rules but worded differently.
C++14 has the same rules as C++11, but as noted in the comment below, the rules changed in C++17.
C++ 2017 8.2.2 [expr.call] p5 says:
The postfix-expression is sequenced before each expression in the expression-list and any default argument. The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.
This means for your example the following steps happen in order:
f
is evaluated.
x
is evaluated and the parameters of f
are initialized.
- The function call
f(x)
is evaluated.
f(x)->g
is evaluated.
args
and the other arguments to g
are evaluated and the parameters of g
are initialized (in an unspecified order).
- Finally, the function call
f(x)->g(args, ...)
is evaluated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…