Evaluation order in a chain call in C ++

Say we have class A :

 class A { public: A& func1( int ) { return *this; } A& func2( int ) { return *this; } }; 

and 2 separate functions:

 int func3(); int func4(); 

now in this code:

 A a; a.func1( func3() ).func2( func4() ); 

- the order of evaluation of functions func3() and func4() defined?

According to this answer, Undefined behavior and sequence points are one of the points in the sequence:

  • when calling a function (regardless of whether the function is built-in), after evaluating all the arguments of the function (if any) that are executed before executing any expressions or statements in the body of the function ( ยง1.9/17 ).

So, "evaluating all the arguments of the function" means that func3() needs to be called before func4() , since the evaluation of the arguments func1() must occur before calling func2() ?

+8
c ++ order-of-evaluation
source share
2 answers

Its essence is that in the function call X(Y, Z) ; the estimate of all X , Y , Z indefinitely sequenced relative to each other. The only sequencing is that Y and Z sequenced - before calling the function that X rated.

Let's pretend that:

 typedef void (*fptr)(int, double); fptr a(); int b(); double c(); a()(b(), c()); 

Three functions a , b , c can be called in any order. Of course, this all recursively applies to any subexpressions.

+6
source share

No, func3 and func4 can be evaluated in any order (but do not alternate).

+3
source share

All Articles