Evaluation order and operator precedance

F

Frédéric

Hi,

I'm stuck with the order of evaluation of operands. With this simple
statement :

some_type foobar = foo() * bar();

gcc 4.1 executes :
bar
foo
operator*

Is the order in which all functions are executed standard ? May I rely on
such a detail ?

Thanks.
 
O

Owen Jacobson

Hi,

I'm stuck with the order of evaluation of operands. With this simple
statement :

some_type foobar = foo() * bar();

gcc 4.1 executes :
bar
foo
operator*

Is the order in which all functions are executed standard ? May I rely on
such a detail ?

The fact that you're asking this question says a lot about the wisdom
of relying on the order of side-effects in a single expression. If
you don't know, do you expect the maintenance programmer who follows
you to know?

If foo() and bar() must be evaluated in a specific order, write the
calls to them in separate statements. It's much clearer about what's
going on.
 
F

Frédéric

------

The fact that you're asking this question says a lot about the wisdom of
relying on the order of side-effects in a single expression. If you
don't know, do you expect the maintenance programmer who follows you to
know?

The answer was expected, hence the "detail" where evil lies. I was just
wondering if I missed something.

Thanks for your time.
 
A

Andrey Tarasevich

Frédéric said:
I'm stuck with the order of evaluation of operands. With this simple
statement :

some_type foobar = foo() * bar();

gcc 4.1 executes :
bar
foo
operator*

Is the order in which all functions are executed standard ? May I rely on
such a detail ?
...

No and no. Read the FAQ on "sequence points".

In practice people sometimes write expressions like

int r = rand() + rand() * 2;

and when the program starts behaving differently on different platforms, they
immediately assume that the problem is caused by different implementations of
'rand()' in each platform's standard library. So they replace the standard
'rand() with their custom 'my_rand()', uniform across all platforms

int r = my_rand() + my_rand() * 2;

and then observe, puzzled and amazed, that the program still continues to behave
differently for different platforms/builds/compiler options :)
 
J

James Kanze

I'm stuck with the order of evaluation of operands. With this
simple statement :
some_type foobar = foo() * bar();
gcc 4.1 executes :
bar
foo
operator*
Is the order in which all functions are executed standard ?
May I rely on such a detail ?

No. It's unspecified, and may vary---even in the same
implementation. Change the form of the expression, or the level
of optimization, and the order may change. All you're really
guaranteed is that bar and foo will be called before operator*
(for obvious reasons, and of course, that operator* will be
called before operator=), and that all of the functions will be
called before the next encompassing sequence point. And that
the functions will be called in some order; the compiler is not
allowed to run them in parallel on a multi-core machine, or
interleaf there execution in any way. (Note that it can
interleaf parts of sub-expressions other than function calls.
If you write a()*b() + c()/d(), a, b, c and d can be called in
any order---in addition, addition, there are no ordering
constrains between a(), b() and operator/(), nor between c(),
d() and operator*().)
 

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