Velocity Reviews > C++ > Operator evaluation order

# Operator evaluation order

Johannes Schaub
Guest
Posts: n/a

 05-12-2011
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

Joshua Maurice
Guest
Posts: n/a

 05-12-2011
On May 12, 4:56*am, Victor Bazarov <(E-Mail Removed)> 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).

Michael Doubez
Guest
Posts: n/a

 05-12-2011
On 12 mai, 17:14, Johannes Schaub <(E-Mail Removed)>
wrote:
> (E-Mail Removed) 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
Guest
Posts: n/a

 05-12-2011
On 12 mai, 15:51, Victor Bazarov <(E-Mail Removed)> wrote:
> On 5/12/2011 9:25 AM, Michael Doubez wrote:
>
> > On 12 mai, 12:24, (E-Mail Removed) wrote:
> >> On Thu, 12 May 2011 03:21:07 -0700 (PDT)

>
> >> Michael Doubez<(E-Mail Removed)> *wrote:
> >>> On 12 mai, 12:06, (E-Mail Removed) 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=.

until after I posted my message.

--
Michael

Michael Doubez
Guest
Posts: n/a

 05-12-2011
On 12 mai, 23:00, Michael Doubez <(E-Mail Removed)> wrote:
> On 12 mai, 15:51, Victor Bazarov <(E-Mail Removed)> wrote:
>
>
>
>
>
>
>
>
>
> > On 5/12/2011 9:25 AM, Michael Doubez wrote:

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

>
> > >> Michael Doubez<(E-Mail Removed)> *wrote:
> > >>> On 12 mai, 12:06, (E-Mail Removed) 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=.
>
> until after I posted my message.

I mean 13h48. Well, whatever.

--
Michael

Nobody
Guest
Posts: n/a

 05-13-2011
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".

James Kanze
Guest
Posts: n/a

 05-15-2011
On May 12, 11:24 am, (E-Mail Removed) wrote:
> On Thu, 12 May 2011 03:21:07 -0700 (PDT)
> Michael Doubez <(E-Mail Removed)> wrote:
> >On 12 mai, 12:06, (E-Mail Removed) 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