Grouping operators and operands and Order of Evaluation are two important concepts of expression in C++.
For expression with multiple operators, how the operands grouped with the specific operators is decided by the precedence and associativity of the operators and may depend on the order of evaluation.
In C++, only 4 operators have the specified order of evaluations (logical AND, logical OR, conditional and comma operator). For the other operators, the evaluation order is unspecified.
Parentheses can override the precedence and associativity, and therefore specify the grouping of a compound expression.
However, the book by Peter Gottschling claims the parentheses can change the order of the evaluation. I personally doubt it; I think it's an error! In the example from the quotation below, the parentheses do not tell which expression of x
, y
and z
is evaluated first, which one is later and which one is the last. It only groups the expression y + z
as the left operand of the *
operator.
An expression surrounded by parentheses is an expression as well, e.g.,
(x + y)
. As this grouping by parentheses precedes all operators, we can change the order of evaluation to suit our needs:x * (y + z)
computes the addition first. Discovering Modern C++, Chapter 1.4.1
Can parentheses override expressions' order of evaluation?
The quoted sentence is poorly worded. The author didn't mean that the order of evaluation is changed, or even specified; I think the word "order" was meant in terms of how a human might read the expression (i.e. precedence).
Of course, if the three variables are independent and reading them has no side-effects, the "as if" rule makes the unspecified order irrelevant, as it wouldn't change the value of the expression.