Evaluation order and operator precedance

Discussion in 'C++' started by Frédéric, Dec 5, 2007.

  1. Frédéric

    Frédéric Guest

    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.
    Frédéric, Dec 5, 2007
    #1
    1. Advertising

  2. On Dec 5, 3:53 pm, Frédéric <> 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.
    Owen Jacobson, Dec 6, 2007
    #2
    1. Advertising

  3. Frédéric

    Frédéric Guest

    On Wed, 05 Dec 2007 16:14:15 -0800, Owen Jacobson wrote:

    > On Dec 5, 3:53 pm, Frédéric <> 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.
    Frédéric, Dec 6, 2007
    #3
  4. 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
    Andrey Tarasevich, Dec 6, 2007
    #4
  5. Frédéric

    James Kanze Guest

    On Dec 6, 12:53 am, 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. 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:
    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
    James Kanze, Dec 6, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert
    Replies:
    2
    Views:
    426
    Robert
    May 10, 2004
  2. Jan Engelhardt
    Replies:
    3
    Views:
    372
    Mike Wahler
    Aug 20, 2003
  3. Joseph

    operator precedance

    Joseph, Sep 29, 2003, in forum: C Programming
    Replies:
    6
    Views:
    930
    Mark A. Odell
    Sep 30, 2003
  4. andreas ames
    Replies:
    3
    Views:
    368
    Victor Bazarov
    Oct 23, 2006
  5. vaysagekv

    operator precedance (&& or ||)

    vaysagekv, Jul 4, 2010, in forum: C Programming
    Replies:
    8
    Views:
    293
    iC and iC++
    Jul 4, 2010
Loading...

Share This Page