order of operations

Discussion in 'C++' started by pauldepstein@att.net, Jul 27, 2008.

  1. Guest

    Assuming variables and functions have been defined correctly and x is
    an int, is it unambiguous to write

    x = f(x) + g(x);

    Or does this lead to the possibility that the compiler will do x =
    f(x) and then substitute that f(x) value into g(x)?

    Thanks,

    Paul Epstein
     
    , Jul 27, 2008
    #1
    1. Advertising

  2. kwikius Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    >* :
    >> Assuming variables and functions have been defined correctly and x is
    >> an int, is it unambiguous to write
    >>
    >> x = f(x) + g(x);

    >
    > It's unambigious if neither function has side-effects.
    >
    > However, if there are side-effects, then it becomes relevant that the
    > order of evaluation is unspecified.


    What happens if both f(x) and g(x) are executed in parallel? Is that allowed
    by the C++ standard?

    regards
    Andy Little
     
    kwikius, Jul 27, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Jul 27, 6:35 pm, "kwikius" <> wrote:
    > "Alf P. Steinbach" <> wrote in
    > messagenews:...


    > >* :
    > >> Assuming variables and functions have been defined correctly and x is
    > >> an int, is it unambiguous to write


    > >> x = f(x) + g(x);


    > > It's unambigious if neither function has side-effects.


    > > However, if there are side-effects, then it becomes relevant that the
    > > order of evaluation is unspecified.


    > What happens if both f(x) and g(x) are executed in parallel?
    > Is that allowed by the C++ standard?


    Only if the compiler can determine that it doesn't matter. A
    function call is a sequence point, and you can't continue with
    the rest of the calling expression (including, in this case,
    calling g(x)) until the function returns. Modulo the "as if"
    rule.

    --
    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, Jul 27, 2008
    #3
    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. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,303
    robert
    Feb 11, 2006
  2. Tim923
    Replies:
    4
    Views:
    540
    Tim923
    Apr 8, 2005
  3. Jack Christensen

    Order of operations

    Jack Christensen, Jun 9, 2005, in forum: Ruby
    Replies:
    2
    Views:
    120
    Robert Klemme
    Jun 9, 2005
  4. Gina Engli
    Replies:
    0
    Views:
    299
    Gina Engli
    Jun 22, 2012
  5. Stefan Ram
    Replies:
    0
    Views:
    288
    Stefan Ram
    Jun 22, 2012
Loading...

Share This Page