Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Evaluation order and operator precedance

Reply
Thread Tools

Evaluation order and operator precedance

 
 
Frédéric
Guest
Posts: n/a
 
      12-05-2007
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.
 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      12-06-2007
On Dec 5, 3:53 pm, Frédéric <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Frédéric
Guest
Posts: n/a
 
      12-06-2007
On Wed, 05 Dec 2007 16:14:15 -0800, Owen Jacobson wrote:

> On Dec 5, 3:53 pm, Frédéric <(E-Mail Removed)> wrote:
>> 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?


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

Thanks for your time.
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      12-06-2007
Frédéric wrote:
>
> 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

--
Best regards,
Andrey Tarasevich


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-06-2007
On Dec 6, 12:53 am, Frédéric <(E-Mail Removed)> wrote:
> 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*().)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
operator precedance (&& or ||) vaysagekv C Programming 8 07-04-2010 07:41 PM
Parameter evaluation order on operator invocations andreas ames C++ 3 10-23-2006 04:34 PM
operator precedance: new MyClass.method() Robert Java 2 05-10-2004 06:10 PM
operator precedance Joseph C Programming 6 09-30-2003 01:01 PM
Order of evaluation -- clash w/sequentiel expression operator Jan Engelhardt C Programming 3 08-20-2003 09:07 PM



Advertisments