Evaluation order of comma operator

Discussion in 'C++' started by mr.gsingh@gmail.com, Dec 15, 2006.

  1. Guest

    My program looks something like this:

    int x = 23;
    if ( (func(x), func1(x), func2(x)) <= (foo(x), foo1(x), foo2(x)))
    {
    std::cout << "............";
    }
    else
    {
    std::cout<< "..............";
    }

    where func(x), func1(x) and func2(x) return reference to 'x' and foo(),
    foo1() and foo2() return integer. I understand this is bad programming
    but I am doing this to understand the execution semantics of comma
    operator.

    g++ gives me the follwoing execution order:

    Function foo.
    Function foo1.
    Function func.
    Function func1.
    Function func2.
    Function foo2.

    This looks weird to me. I was expecting s'thing like

    func, func1, func2, foo, foo1, foo2

    Any explanations for this behavior will be highly appreciated.

    Thanks
    Gagan
    , Dec 15, 2006
    #1
    1. Advertising

  2. Gavin Deane Guest

    wrote:
    > My program looks something like this:
    >
    > int x = 23;
    > if ( (func(x), func1(x), func2(x)) <= (foo(x), foo1(x), foo2(x)))
    > {
    > std::cout << "............";
    > }
    > else
    > {
    > std::cout<< "..............";
    > }
    >
    > where func(x), func1(x) and func2(x) return reference to 'x' and foo(),
    > foo1() and foo2() return integer. I understand this is bad programming
    > but I am doing this to understand the execution semantics of comma
    > operator.
    >
    > g++ gives me the follwoing execution order:
    >
    > Function foo.
    > Function foo1.
    > Function func.
    > Function func1.
    > Function func2.
    > Function foo2.
    >
    > This looks weird to me. I was expecting s'thing like


    It's one allowed possibility.

    > func, func1, func2, foo, foo1, foo2


    That's another allowed possibility.

    > Any explanations for this behavior will be highly appreciated.


    The compiler is required to evaluate the left-hand operand of the comma
    operator before the right hand. So in your code, foo(x) must be
    evaluated before foo1(x), which must be evaluated before foo2(x). And
    similar for func(x), funx1(x) and func2(x). Other than that there is no
    requirement or guarantee on the order of evaluation of your functions.
    So

    foo, foo1, foo2, func, func1, func2
    would be equally acceptable.

    foo, foo1, func, func2, foo2, func1
    would not be allowed. But only because func2 happens before func1.
    Everything else about that order is possible.

    Gavin Deane
    Gavin Deane, Dec 15, 2006
    #2
    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. Jan Engelhardt
    Replies:
    3
    Views:
    371
    Mike Wahler
    Aug 20, 2003
  2. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    488
    Barry Schwarz
    Feb 8, 2005
  3. andreas ames
    Replies:
    3
    Views:
    366
    Victor Bazarov
    Oct 23, 2006
  4. Frédéric
    Replies:
    4
    Views:
    316
    James Kanze
    Dec 6, 2007
  5. Replies:
    16
    Views:
    532
    James Kanze
    May 15, 2011
Loading...

Share This Page