0

Hi guys so I need some help understanding how these compound assignment operators work for example

int x=6;
x += x -= x * x;

x turns out to be -60 can someone explain why and how this works?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    You modify `x` more than once between sequence points, leading to undefined behavior. Operator precedence is irrelevant here. – Jerry Coffin Feb 05 '16 at 01:12
  • For what it's worth, not quite a dupe, but closely related: http://stackoverflow.com/a/5475260/179910 – Jerry Coffin Feb 05 '16 at 01:18
  • All these are dups of the same: unreasonable code that nobody would actually write/use outside of issuing a homework exercise:(( – Martin James Feb 05 '16 at 01:21
  • disagree with marking this as duplicate of the generic sequencing thread, as it should be explained exactly what the sequencing error is in this example (which is not the same as the usual `x = x++` type cases) – M.M Feb 05 '16 at 01:32
  • @M.M: I'm not at all sure that's true. It is true that C++11 defines some expressions that weren't previously, but still not all. Specifically, what it guarantees is that: "In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression." [expr.ass]. I don't see where that's sufficient to give these defined behavior (which looks to me like it would really require that the right side be evaluated, then the left, then the assignment, and finally the value of the whole expression). – Jerry Coffin Feb 05 '16 at 01:32
  • @JerryCoffin you're right (I comment further under Jarod's answer) – M.M Feb 05 '16 at 01:35

1 Answers1

-2

Ignoring UB with sequence points:

x += x -= x * x;

is

(x += (x -= (x * x)));

so

x * x -> 36

x -= 36 -> x = -30

x += -30 -> x = -60

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    I believe this is UB after all. Basically it's the same as `x += ++x;` – Anton Savin Feb 05 '16 at 01:11
  • Thank you very much Jarod :) – CanyoucSharp Feb 05 '16 at 01:12
  • @AntonSavin since C++11, `+=` sequences evaluation of the operands before evaluation of the assignment. See [expr.ass]/1 in C++11 or later – M.M Feb 05 '16 at 01:21
  • @M.M yes, but evaluation of operands is unsequenced to each other. Right part (`x -= x * x`) has a side effect on `x`, and left part (`x`) reads the value of `x`. – Anton Savin Feb 05 '16 at 01:24
  • @AntonSavin You're right (I think). I was thinking of `x = ++x` (which is well-defined) but `x += ++x` means `x = x + ++x` which is not well-defined because the two operands of `+` are unsequenced – M.M Feb 05 '16 at 01:31
  • @M.M `x = ++x` is also UB. – Anton Savin Feb 05 '16 at 01:42
  • @AntonSavin disagree. Which two operations on `x` are unsequenced relative to each other? – M.M Feb 05 '16 at 01:49
  • @M.M value computation of `x` on the left side and side effect on the right side. – Anton Savin Feb 05 '16 at 02:05
  • @AntonSavin value computation of the left-hand side doesn't use the value of `x` so it doesn't cause UB (see [intro.execution]/15, UB only ccurs when there are two unsequenced side-effects, or an unsequenced side-effect and using of the value) – M.M Feb 05 '16 at 02:16