Operator evaluation order

Discussion in 'C++' started by boltar2003@boltar.world, May 12, 2011.

  1. Guest

    Perhaps this is a stupid question, but can someone explain why the
    operator evaluation order differs for assignment compared to mathematic
    operators when there is only a single operator type in the expression?

    Eg:

    a1 = a2 = a3 = a4 will evaluate as a3=a4, a2=a3, a1=a2

    but

    a1 + a2 + a3 + a4 will evaluate as a1=a2, a1=a3, a1=a4

    Why the difference?

    B2003
     
    , May 12, 2011
    #1
    1. Advertising

  2. Guest

    On Thu, 12 May 2011 10:06:32 +0000 (UTC)
    wrote:
    >a1 + a2 + a3 + a4 will evaluate as a1=a2, a1=a3, a1=a4


    Sorry , that should have read a1+a2, a1+a3, a1+a4

    B2003
     
    , May 12, 2011
    #2
    1. Advertising

  3. On 12 mai, 12:06, wrote:
    > Perhaps this is a stupid question, but can someone explain why the
    > operator evaluation order differs for assignment compared to mathematic
    > operators when there is only a single operator type in the expression?
    >
    > Eg:
    >
    > a1 = a2 = a3 = a4 will evaluate as a3=a4, a2=a3, a1=a2
    >
    > but
    >
    > a1 + a2 + a3 + a4 will evaluate as a1+a2, a1+a3, a1+a4
    >
    > Why the difference?


    Because the standard says that = operator groups right-to-left
    (§5.17/1) while + operator groups left-to-right (§5.7/1).

    --
    Michael
     
    Michael Doubez, May 12, 2011
    #3
  4. Guest

    On Thu, 12 May 2011 03:21:07 -0700 (PDT)
    Michael Doubez <> wrote:
    >On 12 mai, 12:06, wrote:
    >> Perhaps this is a stupid question, but can someone explain why the
    >> operator evaluation order differs for assignment compared to mathematic
    >> operators when there is only a single operator type in the expression?
    >>
    >> Eg:
    >>
    >> a1 =3D a2 =3D a3 =3D a4 will evaluate as a3=3Da4, a2=3Da3, a1=3Da2
    >>
    >> but
    >>
    >> a1 + a2 + a3 + a4 will evaluate as a1+a2, a1+a3, a1+a4
    >>
    >> Why the difference?

    >
    >Because the standard says that =3D operator groups right-to-left
    >(=A75.17/1) while + operator groups left-to-right (=A75.7/1).


    Fair enough. Do you know why assignment doesn't group left to right too?

    B2003
     
    , May 12, 2011
    #4
  5. Qi Guest

    On 2011-5-12 18:24, wrote:
    >
    > Fair enough. Do you know why assignment doesn't group left to right too?


    Then you have to write "5 = n" to assign 5 to n.


    --
    WQ
     
    Qi, May 12, 2011
    #5
  6. Guest

    On Thu, 12 May 2011 18:29:28 +0800
    Qi <> wrote:
    >On 2011-5-12 18:24, wrote:
    >>
    >> Fair enough. Do you know why assignment doesn't group left to right too?

    >
    >Then you have to write "5 = n" to assign 5 to n.


    No you wouldn't. You're confusing the grouping order with left hand side and
    right hand side in the actual expression.

    B2003
     
    , May 12, 2011
    #6
  7. Nobody Guest

    On Thu, 12 May 2011 10:24:09 +0000, boltar2003 wrote:

    > Fair enough. Do you know why assignment doesn't group left to right too?


    Because the LHS of an assignment must be an lvalue (roughly, something
    which has associated storage: a variable, a structure field, the result of
    dereferencing a pointer with * or [], etc).

    If "=" was left-associative, the expression:

    a1 = a2 = a3 = a4

    would be equivalent to:

    ((a1 = a2) = a3) = a4

    But "(a1 = a2) = a3" makes no more sense than "(1 + 2) = a3".

    OTOH, the expression:

    a1 = (a2 = (a3 = a4))

    doesn't have this problem, as the LHS is always an lvalue.
     
    Nobody, May 12, 2011
    #7
  8. On 5/12/2011 6:24 AM, wrote:
    > On Thu, 12 May 2011 03:21:07 -0700 (PDT)
    > Michael Doubez<> wrote:
    >> On 12 mai, 12:06, wrote:
    >>> Perhaps this is a stupid question, but can someone explain why the
    >>> operator evaluation order differs for assignment compared to mathematic
    >>> operators when there is only a single operator type in the expression?
    >>>
    >>> Eg:
    >>>
    >>> a1 =3D a2 =3D a3 =3D a4 will evaluate as a3=3Da4, a2=3Da3, a1=3Da2
    >>>
    >>> but
    >>>
    >>> a1 + a2 + a3 + a4 will evaluate as a1+a2, a1+a3, a1+a4
    >>>
    >>> Why the difference?

    >>
    >> Because the standard says that =3D operator groups right-to-left
    >> (=A75.17/1) while + operator groups left-to-right (=A75.7/1).

    >
    > Fair enough. Do you know why assignment doesn't group left to right too?


    <shrug> Seems rather logical to conclude that

    a = b = c = 5;

    actually means "all values become 5" instead of "'a' becomes the same as
    'b', then the same as 'c', then 5", since the latter sequence (which is
    what it would be if assignment grouped left to right) doesn't change the
    values of 'b' and 'c' and changes 'a' thrice (which seems rather
    unnecessary).

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 12, 2011
    #8
  9. On 5/12/2011 7:47 AM, Nobody wrote:
    > On Thu, 12 May 2011 10:24:09 +0000, boltar2003 wrote:
    >
    >> Fair enough. Do you know why assignment doesn't group left to right too?

    >
    > Because the LHS of an assignment must be an lvalue (roughly, something
    > which has associated storage: a variable, a structure field, the result of
    > dereferencing a pointer with * or [], etc).
    >
    > If "=" was left-associative, the expression:
    >
    > a1 = a2 = a3 = a4
    >
    > would be equivalent to:
    >
    > ((a1 = a2) = a3) = a4
    >
    > But "(a1 = a2) = a3" makes no more sense than "(1 + 2) = a3".


    It makes *some* sense. You actually are allowed to write that (try it,
    you might be surprised). Since assignment returns a reference, you can
    keep assigning to it. The expression

    (a1 = a2) = a3

    is *logically* equivalent to (a1 = a2, a1 = a3), except for the sequence
    point, which doesn't exist in the first expression, and thus gives the
    first expression, while legitimate syntactically, undefined behavior
    (unless I missed something).

    >
    > OTOH, the expression:
    >
    > a1 = (a2 = (a3 = a4))
    >
    > doesn't have this problem, as the LHS is always an lvalue.


    The result of an assignment is an lvalue with built-in types and with
    the compiler-provided assignment operator for classes. That's why you
    can do

    SomeClass a, b, c;
    (a = b) = c;

    without a problem since the assignment for classes is a function call
    and as such provides an extra sequence point (unlike with built-in
    types), so no undefined behavior there.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 12, 2011
    #9
  10. On 12 mai, 12:24, wrote:
    > On Thu, 12 May 2011 03:21:07 -0700 (PDT)
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > Michael Doubez <> wrote:
    > >On 12 mai, 12:06, wrote:
    > >> Perhaps this is a stupid question, but can someone explain why the
    > >> operator evaluation order differs for assignment compared to mathematic
    > >> operators when there is only a single operator type in the expression?

    >
    > >> Eg:

    >
    > >> a1 =3D a2 =3D a3 =3D a4 will evaluate as a3=3Da4, a2=3Da3, a1=3Da2

    >
    > >> but

    >
    > >> a1 + a2 + a3 + a4 will evaluate as a1+a2, a1+a3, a1+a4

    >
    > >> Why the difference?

    >
    > >Because the standard says that =3D operator groups right-to-left
    > >(=A75.17/1) while + operator groups left-to-right (=A75.7/1).

    >
    > Fair enough. Do you know why assignment doesn't group left to right too?


    I don't understand the others' answer. I would simply say that the
    semantic intended from base type is that all variable get the same
    value. But you only now it from the right most value, thus it makes
    sense to have right grouping:
    a1 = ( a2 = ( a3 = a4 ) );

    Concerning the arithmetic operators, I guess it is because of
    asymmetric operators such as '-' which group left in math notation: 9
    - 3 - 2 != 9 - ( 3 - 2 ). In order to be coherent with mathematical
    notation, asymmetric arithmetic operators group left and the same
    convention was kept for symmetric ones.

    --
    Michael
     
    Michael Doubez, May 12, 2011
    #10
  11. wrote:

    > Perhaps this is a stupid question, but can someone explain why the
    > operator evaluation order differs for assignment compared to mathematic
    > operators when there is only a single operator type in the expression?
    >
    > Eg:
    >
    > a1 = a2 = a3 = a4 will evaluate as a3=a4, a2=a3, a1=a2
    >


    This is not true with regard to evaluation order.

    The compiler can evaluate "a1" before evaluating "a3". It is true merely
    with regard to assignment. That is, it will assign to a1 the value of a2
    that it has after assigning to a2 the value of a3 that it has after
    assigning to a3 the value of a4.
     
    Johannes Schaub, May 12, 2011
    #11
  12. On May 12, 4:56 am, Victor Bazarov <> wrote:
    > On 5/12/2011 7:47 AM, Nobody wrote:
    > > If "=" was left-associative, the expression:

    >
    > >    a1 = a2 = a3 = a4

    >
    > > would be equivalent to:

    >
    > >    ((a1 = a2) = a3) = a4

    >
    > > But "(a1 = a2) = a3" makes no more sense than "(1 + 2) = a3".

    >
    > It makes *some* sense.  You actually are allowed to write that (try it,
    > you might be surprised).  Since assignment returns a reference, you can
    > keep assigning to it.  The expression
    >
    >     (a1 = a2) = a3
    >
    > is *logically* equivalent to (a1 = a2, a1 = a3), except for the sequence
    > point, which doesn't exist in the first expression, and thus gives the
    > first expression, while legitimate syntactically, undefined behavior
    > (unless I missed something).


    If the operator= is a built-in operator on fundamental types, then
    yes. If the operator= is an overloaded operator, then it's a function
    call, which brings in sequence points, making it not UB (but still bad
    code style).
     
    Joshua Maurice, May 12, 2011
    #12
  13. On 12 mai, 17:14, Johannes Schaub <>
    wrote:
    > wrote:
    > > Perhaps this is a stupid question, but can someone explain why the
    > > operator evaluation order differs for assignment compared to mathematic
    > > operators when there is only a single operator type in the expression?

    >
    > > Eg:

    >
    > > a1 = a2 = a3 = a4 will evaluate as a3=a4, a2=a3, a1=a2

    >
    > This is not true with regard to evaluation order.
    >
    > The compiler can evaluate "a1" before evaluating "a3".


    What do you mean ? If a1 is a variable, there is nothing to evaluate.

    > It is true merely
    > with regard to assignment. That is, it will assign to a1 the value of a2
    > that it has after assigning to a2 the value of a3 that it has after
    > assigning to a3 the value of a4.


    I think there are many factors concerning the evaluation of an
    expression and we are all catching on something different.

    Just to sum up things. When evaluating an expression, there are:
    1 the precedence of operators: some operators are evaluated before
    others; ex: * is evaluated before +
    2 the grouping between operators of same precedence: some are
    grouped from left to right other right to left
    3 the evaluation of the parameters: for a given resolved expression
    regarding precedence and grouping, parameters are evaluated in any
    order (I left out shortcuts)
    4 the sequence point: some operators define a sequence others don't

    --
    Michael
     
    Michael Doubez, May 12, 2011
    #13
  14. On 12 mai, 15:51, Victor Bazarov <> wrote:
    > On 5/12/2011 9:25 AM, Michael Doubez wrote:
    >
    > > On 12 mai, 12:24, wrote:
    > >> On Thu, 12 May 2011 03:21:07 -0700 (PDT)

    >
    > >> Michael Doubez<>  wrote:
    > >>> On 12 mai, 12:06, wrote:
    > >>>> Perhaps this is a stupid question, but can someone explain why the
    > >>>> operator evaluation order differs for assignment compared to mathematic
    > >>>> operators when there is only a single operator type in the expression?

    >
    > >>>> Eg:

    >
    > >>>> a1 =3D a2 =3D a3 =3D a4 will evaluate as a3=3Da4, a2=3Da3,a1=3Da2

    >
    > >>>> but

    >
    > >>>> a1 + a2 + a3 + a4 will evaluate as a1+a2, a1+a3, a1+a4

    >
    > >>>> Why the difference?

    >
    > >>> Because the standard says that =3D operator groups right-to-left
    > >>> (=A75.17/1) while + operator groups left-to-right (=A75.7/1).

    >
    > >> Fair enough. Do you know why assignment doesn't group left to right too?

    >
    > > I don't understand the others' answer.

    >
    > Perhaps you could state what in "the others' answer" is not clear to you.


    That from a question regarding grouping, we end up with consideration
    about sequence points and exotics use of operator=.

    That doesn't concern your answer at 13h56 which my reader didn't show
    until after I posted my message.

    --
    Michael
     
    Michael Doubez, May 12, 2011
    #14
  15. On 12 mai, 23:00, Michael Doubez <> wrote:
    > On 12 mai, 15:51, Victor Bazarov <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On 5/12/2011 9:25 AM, Michael Doubez wrote:

    >
    > > > On 12 mai, 12:24, wrote:
    > > >> On Thu, 12 May 2011 03:21:07 -0700 (PDT)

    >
    > > >> Michael Doubez<>  wrote:
    > > >>> On 12 mai, 12:06, wrote:
    > > >>>> Perhaps this is a stupid question, but can someone explain why the
    > > >>>> operator evaluation order differs for assignment compared to mathematic
    > > >>>> operators when there is only a single operator type in the expression?

    >
    > > >>>> Eg:

    >
    > > >>>> a1 =3D a2 =3D a3 =3D a4 will evaluate as a3=3Da4, a2=3Da3, a1=3Da2

    >
    > > >>>> but

    >
    > > >>>> a1 + a2 + a3 + a4 will evaluate as a1+a2, a1+a3, a1+a4

    >
    > > >>>> Why the difference?

    >
    > > >>> Because the standard says that =3D operator groups right-to-left
    > > >>> (=A75.17/1) while + operator groups left-to-right (=A75.7/1).

    >
    > > >> Fair enough. Do you know why assignment doesn't group left to right too?

    >
    > > > I don't understand the others' answer.

    >
    > > Perhaps you could state what in "the others' answer" is not clear to you.

    >
    > That from a question regarding grouping, we end up with consideration
    > about sequence points and exotics use of operator=.
    >
    > That doesn't concern your answer at 13h56 which my reader didn't show
    > until after I posted my message.


    I mean 13h48. Well, whatever.

    --
    Michael
     
    Michael Doubez, May 12, 2011
    #15
  16. Nobody Guest

    On Thu, 12 May 2011 07:56:25 -0400, Victor Bazarov wrote:

    >> But "(a1 = a2) = a3" makes no more sense than "(1 + 2) = a3".

    >
    > It makes *some* sense. You actually are allowed to write that (try it,
    > you might be surprised). Since assignment returns a reference, you can
    > keep assigning to it.


    Oops. I thought that I was on c.l.c.

    For C++, the answer (regarding associativity) is "because that's how C
    behaves".
     
    Nobody, May 13, 2011
    #16
  17. James Kanze Guest

    On May 12, 11:24 am, wrote:
    > On Thu, 12 May 2011 03:21:07 -0700 (PDT)
    > Michael Doubez <> wrote:
    > >On 12 mai, 12:06, wrote:
    > >> Perhaps this is a stupid question, but can someone explain why the
    > >> operator evaluation order differs for assignment compared to mathematic
    > >> operators when there is only a single operator type in the expression?


    > >> Eg:


    > >> a1 = a2 = a3 = a4 will evaluate as a3=a4, a2=a3, a1=a2


    > >> but


    > >> a1 + a2 + a3 + a4 will evaluate as a1+a2, a1+a3, a1+a4


    > >> Why the difference?


    > >Because the standard says that = operator groups right-to-left
    > >(=A75.17/1) while + operator groups left-to-right (=A75.7/1).


    > Fair enough. Do you know why assignment doesn't group left to right too?


    Probably utility considerations. What use would multiple
    assignments be if "a = b = c" meant "(a = b) = c"?

    --
    James Kanze
     
    James Kanze, May 15, 2011
    #17
    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:
    374
    Mike Wahler
    Aug 20, 2003
  2. Ilias Lazaridis
    Replies:
    2
    Views:
    393
    Ilias Lazaridis
    Apr 24, 2005
  3. andreas ames
    Replies:
    3
    Views:
    370
    Victor Bazarov
    Oct 23, 2006
  4. Replies:
    1
    Views:
    383
    Gavin Deane
    Dec 15, 2006
  5. Frédéric
    Replies:
    4
    Views:
    321
    James Kanze
    Dec 6, 2007
Loading...

Share This Page