0

Possible Duplicate:
Safety concerns about short circuit evaluation

What does the standard say about evaluating && expressions - does it guarantee that evaluation of parameters will stop at the first false?

E.g.:

Foo* p;
//....
if ( p && p->f() )
{
    //do something
}

is the f() guaranteed not to be called if p == NULL?

Also, is the order of evaluation guaranteed to be the order of appearence in the clause?

Might the optimizer change something like:

int x;
Foo* p;
//...
if ( p->doSomethingReallyExpensive() && x == 3 )
{
    //....
}

to a form where it evaluates x==3 first? Or will it always execute the really expensive function first?

I know that on most compilers (probably all) evaluation stops after the first false is encountered, but what does the standard say about it?

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625

4 Answers4

3

What does the standard say about evaluating && expressions - does it guarantee that evaluation of parameters will stop at the first false?

Yes. That is called short-circuiting.

Also, is the order of evaluation guaranteed to be the order of appearence in the clause?

Yes. From left to right. The operand before which the expression short-circuited doesn't get evaluated.

int a = 0;
int b = 10;
if ( a != 0 && (b=100)) {}

cout << b << endl; //prints 10, not 100

In fact, the above two points are the keypoint in my solution here:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
3

In the ANSI C standard 3.3.13:

Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation; there is a sequence point after the
evaluation of the first operand.  If the first operand compares equal
to 0, the second operand is not evaluated.

There is an equivalent statement in the C++ standard

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
1

&& (and ||) establish sequence points. So the expression on the left-hand side will get evaluated before the right-hand side. Also, yes, if the left-hand side is false/true (for &&/||), the right-hand side is not evaluated.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
0

What does the standard say about evaluating && expressions - does it guarantee that evaluation of parameters will stop at the first false?

Also, is the order of evaluation guaranteed to be the order of appearence in the clause?

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

This only works for the standard && operator, user defined overloads of operator && don't have this guarantee, they behave like regular function call semantics.

Might the optimizer change something like: if ( p->doSomethingReallyExpensive() && x == 3 ) to a form where it evaluates x==3 first?

An optimizer may decide to evaluate x == 3 first since it is an expression with no side-effects associated if x is not modified by p->doSomethingReallyExpensive(), or even evaluate it after p->doSomethingReallyExpensive() already returned false. However, the visible behavior is guaranteed to be the previously specified: Left to right evaluation and short-circuit. That means that while x == 3 may be evaluated first and return false the implementation still has to evaluate p->doSomethingReallyExpensive().

K-ballo
  • 80,396
  • 20
  • 159
  • 169