Evaluation order for nested function calls

C

cheeser

Hello all,

Consider nested function calls:

foo1(
foo2( foo3()),

foo4(
foo5(),
foo6(foo7())
),

foo8()
);

There are obvious dependencies here such as that foo3() must be called
before foo2(). Other than the required dependencies of this ilk, is there
any guarantee that these 8 function calls will be made in any particular
order?

The two (slightly different) orderings shown below meet the obvious
dependencies just described. What I would like to know though is if they
are *both* within the realm of Standard-conforming behavior.

ORDER 1:
foo7()
foo8()
foo3()
foo5()
foo2()
foo6()
foo4()
foo1()

ORDER 2:
foo7()
foo3()
foo5()
foo2()
foo6()
foo4()
foo8()
foo1()

The obvious concern here is that the lack of a completly well-defined order
could cause the same program to behave differently under two different
compilers.

Thanks,
Dave
 
A

Alf P. Steinbach

Consider nested function calls:

foo1(
foo2( foo3()),

foo4(
foo5(),
foo6(foo7())
),

foo8()
);

There are obvious dependencies here such as that foo3() must be called
before foo2(). Other than the required dependencies of this ilk, is there
any guarantee that these 8 function calls will be made in any particular
order?
No.



...
The obvious concern here is that the lack of a completly well-defined order
could cause the same program to behave differently under two different
compilers.

That's generally the case when you use side-effects.

Avoid side-effects.
 
M

Mike Wahler

cheeser said:
Hello all,

Consider nested function calls:

foo1(
foo2( foo3()),

foo4(
foo5(),
foo6(foo7())
),

foo8()
);

There are obvious dependencies here such as that foo3() must be called
before foo2(). Other than the required dependencies of this ilk, is there
any guarantee that these 8 function calls will be made in any particular
order?
No.


The two (slightly different) orderings shown below meet the obvious
dependencies just described. What I would like to know though is if they
are *both* within the realm of Standard-conforming behavior.

ORDER 1:
foo7()
foo8()
foo3()
foo5()
foo2()
foo6()
foo4()
foo1()

ORDER 2:
foo7()
foo3()
foo5()
foo2()
foo6()
foo4()
foo8()
foo1()

The obvious concern here is that the lack of a completly well-defined order
could cause the same program to behave differently under two different
compilers.

Yes it can.

#include <iostream>

int foo(int i, int j)
{
return i - j;
}

int main()
{
int x = 0;
int y = 0;

std::cout << foo(x = 42, y = x);
return 0;
}

Output could be 42 or 0.

-Mike
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

cheeser escribió:
The obvious concern here is that the lack of a completly well-defined order
could cause the same program to behave differently under two different
compilers.

The solution is simple: don't write a program like that. If you need a
precise order call the functions as desired, with sequence points
between, storing the results in temporaries and the order of execution
will be guaranteed.

If the order of the calls has no effect in the behaviour you can leave
as tou write, and there is a chance that the optimizer generate a better
code.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top