0
test1() && test2() != test3();

In this example despite of precedence of != heigher than && the order of evaluation is always: if test1() then test2 != test3().

Why does c++ && operator need priority? Could you provide an example where operator show its pryority but not an evaluation?

Igor
  • 1,029
  • 9
  • 21
  • That's known as short-circuit Boolean evaluation. – 500 - Internal Server Error Jul 12 '19 at 18:10
  • Are you asking why the `&&` operator has short-circuiting behavior? – Miles Budnek Jul 12 '19 at 18:10
  • 1
    you want this behaviour: `if (a != null && a->foo() == 1)` – OznOg Jul 12 '19 at 18:12
  • I ask about example where operator `&&` uses its priority in expression but not a short-circuit behaviour – Igor Jul 12 '19 at 18:17
  • @Igor I don't know what distinction you're trying to make. The fact the the left-hand side of an `&&` operator is evaluated first and the right-hand side is evaluated only if the left-hand side was `true` _is_ short-circuiting. There is no distinct concept of priority. – Miles Budnek Jul 12 '19 at 18:20
  • @Igor The precedence defines how the sub-expressions will be grouped, not when they will be evaluated. – Gilles-Philippe Paillé Jul 12 '19 at 18:25
  • @MilesBudnek If it is used only for short-circuiting then why does it have priority? – Igor Jul 12 '19 at 18:26
  • @Igor I really don't know what you mean by priority. For short-circuiting to work the entire left-hand side must be evaluated before any of the right-hand side. That's all that's going on here. The left-hand expression `test1()` is evaluated, and if it's `false` no part of the right-hand expression `test2() != test3()` gets evaluated. As others have said, operator precedence only defines how arguments are grouped, not the order in which they're evaluated. – Miles Budnek Jul 12 '19 at 18:32
  • 2
    @Igor It needs a notion of precedence (priority) to define how the expression will be evaluated. For example, `&&` has priority over `||`, so `a||b&&c` will be evaluated as `a||(b&&c)`. However, `==` has priority over `&&`, so `a==b&&c` will be evaluated as `(a==b)&&c`. Thus, the notion of precedence is still very important, regardless of the order of evaluation. – Gilles-Philippe Paillé Jul 12 '19 at 18:35

2 Answers2

3

The question verbiage is misleading, and it took a me a while to understand what OP is asking about. When rules of precedence applied to this statement, and != having higher precedence than &&, it means that

test1() && test2() != test3();

Is sematically equivalent to

test1() && (test2() != test3());

Rather than

(test1() && (test2()) != test3();

However, having short-circuit rules in place, compiler will first execute test1(), and when found false, will not execute test2() or test3(). This doesn't mean that the rules of precedence are not followed.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
2

The higher precedence of != means that

test1() && test2() != test3()

is equivalent tomain top operation of expression

test1() && (test2() != test3())

rather than

(test1() && test2()) != test3()

So, this is an expression of form subexpr1 && subexpr2. Where the != operation is in the second subexpression. The logical AND operation is specified so that left hand operand is executed first, and second operator is executed second (or not at all if left result is false).

eerorika
  • 232,697
  • 12
  • 197
  • 326