Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of evaluation in C++ function parameters

Tags:

c++

standards

If we have three functions (foo, bar, and baz) that are composed like so...

foo(bar(), baz()) 

Is there any guarantee by the C++ standard that bar will be evaluated before baz?

like image 274
Clark Gaebel Avatar asked May 29 '10 11:05

Clark Gaebel


People also ask

Does the order of parameters in a function matter?

terminology - Name of Property: The Order Of Parameters in a Function Does Not Matter - Mathematics Stack Exchange.

What is order of evaluation explain with example?

Order of evaluation refers to the operator precedence and associativity rules according to which mathematical expressions are evaluated.


1 Answers

No, there's no such guarantee. It's unspecified according to the C++ standard.

Bjarne Stroustrup also says it explicitly in "The C++ Programming Language" 3rd edition section 6.2.2, with some reasoning:

Better code can be generated in the absence of restrictions on expression evaluation order

Although technically this refers to an earlier part of the same section which says that the order of evaluation of parts of an expression is also unspecified, i.e.

int x = f(2) + g(3);   // unspecified whether f() or g() is called first 
like image 159
Eli Bendersky Avatar answered Oct 06 '22 03:10

Eli Bendersky