When C++ Stops Evaluating for If()

Programming, for all ages and all languages.
Post Reply
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

When C++ Stops Evaluating for If()

Post 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
User avatar
gaf
Member
Member
Posts: 349
Joined: Thu Oct 21, 2004 11:00 pm
Location: Munich, Germany

Post 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
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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.
Post Reply