Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of evaluation of expressions in a function call

Tags:

c++

c++17

Given a function call func(E1, E2, E3) where E1 etc are arbitrary expressions, is it true that each expression is indeterminately sequenced with respect to each other expression, or are all the expressions unsequenced (i.e. the evaluations can overlap)?

I've looked at the cppreference page on this and it, in rule 15, uses the sentence

In a function call, value computations and side effects of the initialization of every parameter are indeterminately sequenced with respect to value computations and side effects of any other parameter.

which I don't think is quite the same as what I'm asking as the initialisation of the parameter is just the last step in evaluation of the parameter's expression.

But rule 21 which is talking about something else seems to imply that each sub-expression in a function call is indeterminately sequenced

Every expression in a comma-separated list of expressions in a parenthesized initializer is evaluated as if for a function call (indeterminately-sequenced)

So I'm a bit confused and any guidance is appreciated.

like image 753
john Avatar asked Dec 13 '20 22:12

john


People also ask

What is the order of evaluation in the expression?

"Order of evaluation" refers to when different subexpressions within the same expression are evaulated relative to each other. you have the usual precedence rules between multiplication and addition.

What is the evaluation order of the function parameters in C++?

The order of evaluation of function arguments is unspecified. Consequently, parameters of a function shall not be used in a default argument, even if they are not evaluated. The same verbiage is used by C++14 standard as well, and is found under the same section.

What is the operand evaluation order?

evaluation order and sequence points Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified (except where noted below).

What is the Order of evaluation of function arguments?

User-defined conversion. 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.

What is the Order of evaluation in C programming?

Order of evaluation. Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified (except where noted below). The compiler will evaluate them in any order,...

What is a function-call expression?

14) In a function-call expression, the expression that names the function is sequenced before every argument expression and every default argument.

What are the sequential-evaluation and function-call operators?

Only the sequential-evaluation (, ), logical-AND ( && ), logical-OR ( || ), conditional-expression (? : ), and function-call operators constitute sequence points, and therefore guarantee a particular order of evaluation for their operands. The function-call operator is the set of parentheses following the function identifier.


1 Answers

C++17 states in

8.2.2 Function call [expr.call]

4 ... The initialization and destruction of each parameter occurs within the context of the calling function.


5 ... Note: All side effects of argument evaluations are sequenced before the function is entered


5 ... The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.

I hope this (my bolding) is clear enough.

(ref: n4659, final C++17 draft)

like image 142
Tony Tannous Avatar answered Sep 21 '22 23:09

Tony Tannous