operator precedence

P

pauldepstein

Take the following code snippet:

int x = 5;
int i = 7;

It's obvious to me what (x+=i )= 25; does and it's obvious to me what x
+=(i = 25); does.

However, (other than by trying it out), I'd have no idea which of the
two versions is meant by x+=i = 25;

Operator precedence doesn't seem to help because += and = have equal
precedence.
The order of the operators also isn't clear because = is right
associative.

My guess would have been "just go left to right" leading to (x+=i )=
25; However, experimentation shows this guess to be wrong. How
could this be known in advance?

Thanks,

Paul Epstein
 
J

James Kanze

Take the following code snippet:
int x = 5;
int i = 7;
It's obvious to me what (x+=i )= 25; does and it's obvious to me what x
+=(i = 25); does.
However, (other than by trying it out), I'd have no idea which of the
two versions is meant by x+=i = 25;

Assignment operators bind right to left, so it is the second.
Operator precedence doesn't seem to help because += and = have equal
precedence.
The order of the operators also isn't clear because = is right
associative.

What's not clear, then. Right associative means that "a op b op
c" is the same as "a op (b op c)".
My guess would have been "just go left to right" leading to (x+=i )=
25; However, experimentation shows this guess to be wrong. How
could this be known in advance?

By knowing the language. You actually mentionned the rule which
tells you: the assignment operators are right associative.
 
J

John Harrison

Take the following code snippet:

int x = 5;
int i = 7;

It's obvious to me what (x+=i )= 25; does and it's obvious to me what x
+=(i = 25); does.

(x += i) = 25;

might be obvious in term of parsing the expression. But it's behaviour
is undefined since it modifies the same location (i.e. x) twice between
sequence points.

With some exceptions this rule stops you modifying the same location
twice in one expression in legal C++.

On the other hand x += (i = 25); is fine assuming x and i are different
locations.

john
 
Z

Zeppe

John said:
(x += i) = 25;

might be obvious in term of parsing the expression. But it's behaviour
is undefined since it modifies the same location (i.e. x) twice between
sequence points.

With some exceptions this rule stops you modifying the same location
twice in one expression in legal C++.

On the other hand x += (i = 25); is fine assuming x and i are different
locations.

and probably is worth noticing that such an instruction is bad practice.
Much clearer to split i = 25; x += i;

Regards,

Zeppe
 
J

James Kanze

Good point.
and probably is worth noticing that such an instruction is bad practice.
Much clearer to split i = 25; x += i;

Agreed. About the only time such "chaining" of assignments
would be acceptable is things like:
x = y = 0 ;
to initialize several variables with the same value (and even
then, I'm not too happy about it).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top