-1

Edit to clarify: I am not asking about how sequencing works. This question is not a duplicate of the question that it was marked as duplicate of. I already understand that x++ * ++x is UB. I am asking about the meaning of the phrase "The order of evaluation of expressions is left to right".

This line is taken from Bjarne Stroustrup's "A Tour of C++", 2nd edition.

I would have expected this to mean that snippets like int x=2;int y=x++ * ++x are well-defined. Evaluating left-to-right means, in my understanding, that x++ is evaluated first (value 2), ++x is evaluated second (value 4), and the final value of y is 8.

But clang warns about multiple unsequenced modifications to 'x', so I think my understanding of what Stroustrup's quote means must be wrong.

Can anyone explain what he actually means?

Brennan Vincent
  • 10,736
  • 9
  • 32
  • 54

1 Answers1

0

"The order of expressions is left to right" means that int z = a / b * c; will first divide a by b, and then multiply the result with c. This will have a different result than int z = a * c / b;, because integer division will lose any fractions.

In 'real world ' math, the two expressions would be equivalent; in integer calculations they are not.

Aganju
  • 6,295
  • 1
  • 12
  • 23
  • 1
    Counterexample: in `x+y/z`, `y/z` is evaluated first, then the addition. Since the quote doesn't mention order of operations, I don't think this is what is meant. – Brennan Vincent Apr 23 '19 at 22:55