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 som开发者_如何学Pythonething 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?
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:
- Find maximum of three number in C without using conditional statement and ternary operator
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
&&
(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.
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()
.
精彩评论