4

Grouping operators and operands and Order of Evaluation are two important concepts of expression in C++.

Grouping

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.

Order

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

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

Question

Can parentheses override expressions' order of evaluation?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
SLN
  • 4,772
  • 2
  • 38
  • 79
  • 1
    This follows the basic principles of mathematics order of operations. Brackets, exponents, division, multiplication, addition, subtraction. Did you actually try it out? – brandonscript Feb 21 '18 at 14:49
  • x * (y + z) requires y + z to be computed first. Definitely. THere's no way to compute the answer otherwise. I'll let someone versed in C++-standard-language answer. – Jeffrey Feb 21 '18 at 14:50
  • @Jeffrey why not compute `x` first? – Quentin Feb 21 '18 at 14:52
  • 1
    You can influence the order of evaluation using parenthesis but only because they allows you to specify the order the operators are evaluated. It won't let you directly control the order those operators' operands are evaluated. – François Andrieux Feb 21 '18 at 14:52
  • 4
    I would attribute the quote to lax usage of terminology. Order of argument evaluation here is not changed by parenthesis, but the order of operator execution is. – SergeyA Feb 21 '18 at 14:53
  • 1
    @Jeffrey "y + z to be computed first" is still about the grouping, not the order of evaluation, the compiler might be evaluate x first and y and z and y + z and the production. – SLN Feb 21 '18 at 14:53
  • 3
    @JK, Microsoft is hardly an authoritative source; secondly, that link says nothing about order of evaluation – Toby Speight Feb 21 '18 at 14:58
  • @JK Precedence, Associativity and order of evaluation are different concept, the page you are given is about Precedence and Associativity. – SLN Feb 21 '18 at 14:58
  • I would say it is very likely that what you have quoted is erroneously conflating order of evaluation with operator precedence as others in this comment section are. This is further indicated by the precedence of the parentheses operator being explicitly referenced in the quote. – Garrett Gutierrez Feb 21 '18 at 15:02
  • 1
    @Quentin (and SLN) yeah, I badly worded it. the evaluation of + has to be done before the evaluation of *. First was not the right word to use. I guess this is the source of the confusion to the OP. The difference between evaluating x it self, +, *, and what liberties the compiler has and doesn't in the possible orders – Jeffrey Feb 21 '18 at 15:03

1 Answers1

1

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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103