1

In the book "A Tour of C++" (Bjarne Stroustrup), section 1.4.1 it says,

"The order of evaluation of expressions is left to right, except for assignments, which are right to-left."

However, I have read in the CPP reference that C++ has no concept of order when evaluating an expression: it may evaluate the expression in any order. So I have two questions:

  1. What does the author mean by this?

  2. Even if the evaluation is done from left to right, should this be something I worry about? The precedence of operators will take place anyway, or am I wrong?

Adrian Mole
  • 30,672
  • 69
  • 32
  • 52
HNOONa
  • 33
  • 6
  • Does this answer your question? [What are the evaluation order guarantees introduced by C++17?](https://stackoverflow.com/questions/38501587/what-are-the-evaluation-order-guarantees-introduced-by-c17) – Quarra Dec 22 '20 at 12:32
  • 1
    I was just about to say that the author is wrong then I saw the author is Bjarne. I'll see myself out ... – bolov Dec 22 '20 at 12:37
  • 1
    C++ rules are complicated, so most of the "simple" sentence are simplification or context specific (which so can ignore specific cases). – Jarod42 Dec 22 '20 at 12:58
  • 2
    Apparently your **A Tour of C++** book is different than my book. My book has much more content and context in the quote, "The order of evaluation of is left-to right for `x.y`, `x->y`, `x(y)`, `x[y]`, `x<>y`, `x&&y`, and `x||y`. For assignments (e.g., `x+=y`), the order is right-to-left. The order of evaluation of other expressions (e.g., `f(x)+g(y)`) and of function arguments (e.g., `h(f(x),g(y))`) is unfortunately unspecified." – Eljay Dec 22 '20 at 12:59
  • @Eljay its very different, my e-book version doesn't have all this extra text, but this still doesn't make sense even for expressions like x>>y, technically the compiler should still be able to evaluate any operrand it may choose or are there some exceptions? – HNOONa Dec 22 '20 at 15:40
  • @Quarra i don't know, it will take me some time to read and understand the answer – HNOONa Dec 22 '20 at 15:41
  • 1
    1. the author meant exactly what he said, 2. as long as you don't do anything clever, you shouldn't need to worry about it; and yes, the precedence of operators will take place anyway. – Eljay Dec 22 '20 at 15:47
  • Is this statement made in general or about just certain operators? – Davis Herring Dec 22 '20 at 19:49

0 Answers0