Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical AND, OR: Is left-to-right evaluation guaranteed?

Is left-to-right evaluation of logical operators (&& ||) guaranteed?

Let's say I have this:

SDL_Event event;  if (SDL_PollEvent(&event)) {     if (event.type == SDL_QUIT) {             // do stuff     } } 

Is this guaranteed to be the same as this?

SDL_Event event;  if (SDL_PollEvent(&event) && event.type == SDL_QUIT) {     // do stuff } 

This can also be very important, let's say we have two requirements, a and b. Requirement a is much more likely to fail then b. Then it's more efficient to say if (a && b) than if (b && a).

like image 878
orlp Avatar asked Apr 15 '11 22:04

orlp


People also ask

Is && or || evaluated first?

The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .

Which statement about logical operators & and && is true?

The logical AND operator ( && ) returns true if both operands are true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool . Logical AND has left-to-right associativity.

What can a logical operator evaluate to?

The operator accepts a single argument and does the following: Converts the operand to boolean type: true/false . Returns the inverse value.

What is left to right evaluation?

When there are two (or more) operators of equal precedence, evaluate the expression from left to right. Since both operators are * , each has equal precedence, so calculate 2 * 7 first, then use that result with the second operator.


1 Answers

Yes, it's guaranteed, otherwise such operators would lose much of their usefulness.

Important notice: this is valid only for the builtin && and ||; if some criminal overloads them, they are treated as "regular" overloaded binary operators, so in this case both operands are always evaluated, and in unspecified order as usual. For this reason, never overload them - it breaks a hugely important assumption about the control flow of the program.


Relevant standard quotations

Builtin && and || have guaranteed short-circuit behavior

§5.14 ¶1

Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

§5.15 ¶1

Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.

If overloaded, they behave as "regular" binary operators (no short-circuit or guaranteed ordering of evaluation)

§13.5 ¶9

Operators not mentioned explicitly in subclauses 13.5.3 through 13.5.7 act as ordinary unary and binary operators obeying the rules of 13.5.1 or 13.5.2.

and && and || are not mentioned explicitly in these subclauses, so regular §13.5.2 holds:

§13.5.2 ¶1

A binary operator shall be implemented either by a non-static member function (9.3) with one parameter or by a non-member function with two parameters. Thus, for any binary operator @, x@y can be interpreted as either x.operator@(y) or operator@(x,y).

with no special provision for evaluating only one side or in a particular order.

(all quotations from the C++11 standard)

like image 193
Matteo Italia Avatar answered Oct 07 '22 00:10

Matteo Italia