Page 1 of 1
When C++ Stops Evaluating for If()
Posted: Mon Jun 25, 2007 4:24 am
by AJ
Hi,
I was just wondering if there is a standard that defines when C++ (and C, for that matter) aborts evaluation for an if() or while() statement. For example:
Code: Select all
if(( !myclassptr ) || (!myclassptr->element))
{
...
dosomething();
...
}
As you can see, if the first expression is true (i.e. myclassptr is NULL), you don't want the second expression to be evaluated as this would, in all likelyhood, cause a PFE.
I know I can tell what order my compiler evaluates the expressions in and whether it stops after the first evaluation, but is there a standard that
guarantees my code will still work if I use a different compiler, for example, or is this compiler-specific?
Cheers,
Adam
Posted: Mon Jun 25, 2007 5:03 am
by gaf
I know I can tell what order my compiler evaluates the expressions in and whether it stops after the first evaluation, but is there a standard that guarantees my code will still work if I use a different compiler, for example, or is this compiler-specific?
The C++ standard guarantees that all expressions involving logical AND (&&), logical OR (||) and Comma (,) are evaluated in the given order. If any part of the expression returns false evaluation is aborted immediately, so that the rest of the expression never gets executed
Have a look at the wikipedia article about
short circuit evaluation for some examples and more details
regards,
gaf
Posted: Mon Jun 25, 2007 5:15 am
by jnc100
C99 specification wrote:6.5.14 Logical OR operator
Syntax
1 logical-OR-expression:
logical-AND-expression
logical-OR-expression || logical-AND-expression
Constraints
2 Each of the operands shall have scalar type.
Semantics
3 The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it
yields 0. The result has type int.
4 Unlike the bitwise | 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
unequal to 0, the second operand is not evaluated.
See
http://www.open-std.org/JTC1/SC22/WG14/ ... /n1124.pdf. I couldn't seem to find the full iso c90 spec without paying about 350 swiss francs.
Regards,
John.
Posted: Mon Jun 25, 2007 5:15 am
by AJ
Thanks for the explanation and thanks for the reference! That's very useful to know. And I know the proper term for it now, too!
Cheers,
Adam
Posted: Mon Jun 25, 2007 11:25 am
by Candy
gaf wrote:I know I can tell what order my compiler evaluates the expressions in and whether it stops after the first evaluation, but is there a standard that guarantees my code will still work if I use a different compiler, for example, or is this compiler-specific?
The C++ standard guarantees that all expressions involving logical AND (&&), logical OR (||) and Comma (,) are evaluated in the given order. If any part of the expression returns false evaluation is aborted immediately, so that the rest of the expression never gets executed
Given operator overloading, that can be invalidated as they need to evaluate both halves to be able to call your overridden function. That's pretty much the reason why overloading operator|| and operator&& is usually an error.
You CAN overload operator, but I'm not quite sure what you'd want to do with it.