|
-
November 17th, 2008, 04:42 AM
#1
[RESOLVED] order of evaluation of condition expression.
Hi all,
I have written some production level code assuming that if there is something like
Code:
if ( condition A || condition B || condition C)
or
if ( condition A && condition B && condition C)
the conditions will be evaluated left to right. i.e. A first, B next and C last.
Someone who reviewed my code from Ireland has commented that the standards don't guarantee any such thing.
I have found this in the K&R, which says: C does not guarantee the order of evaluation of expressions other than for ||, &&, comma and the ?: operators.
Can someone who is conversant with the current standards let me know if I am right or wrong?
I
-
November 17th, 2008, 05:00 AM
#2
Re: order of evaluation of condition expression.
Logical operators, WHEN NOT OVERLOADED, have some special property: they are evaluated left to right, and when evaluation yields the result which will be not affected by following expressions, it is terminated. So when you have something like this:
Code:
T* object = getObject(); //returns NULL on failure
if(object && object->getSomeProperty() == "value") {
//...
}
it is guarenteed that program will not try to invoke member function, if pointer points to NULL. It is called short-circuit evaluation and occurs only for non-overloaded operators && and ||.
Comma and conditional operators also have guaranted order of expression evaluation, unless overloaded.
Remaining operators, either overloaded or not, have unspecified order of subexpression evaluation.
Cheers
Last edited by Hobson; November 17th, 2008 at 05:34 AM.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
November 17th, 2008, 05:04 AM
#3
Re: order of evaluation of condition expression.
The operators || and && are "short-circuit" operators. This means that as soon as the result is guaranteed (working from left to right), it stops evaluating. Essentially it means that this
Code:
if (ptr && ptr->mem_fun())
is safe - if ptr == 0, then it will not attempt to dereference it.
And yes, the comma and ternary operators behave similarly.
Here's the entry for the logical AND operator from the standard:
 Originally Posted by C++ standard 5.14
1. The && operator groups left-to-right. The operands are both implicitly converted to type bool (clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.
2. The result is a bool. All side effects of the first expression except for destruction of temporaries (12.2) happen before the second expression is evaluated.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 17th, 2008, 05:06 AM
#4
Re: order of evaluation of condition expression.
Both C++03 and C99 guarantee left-to-right evaluation for || and &&. If you want to quote them:
 Originally Posted by C++03 Section 5.14 Paragraph 1
The && operator groups left-to-right. The operands are both implicitly converted to type bool (clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.
 Originally Posted by C++03 Section 5.15 Paragraph 1
The || operator groups left-to-right. The operands are both implicitly converted to bool (clause 4). It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.
 Originally Posted by C99 Section 6.5.13 Paragraph 4
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.
 Originally Posted by C99 Section 6.5.14 Paragraph 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.
-
November 17th, 2008, 06:03 AM
#5
Re: order of evaluation of condition expression.
Thanks a LOT Graham and particularly laserlight for giving me the exact references.
I
-
November 17th, 2008, 08:41 AM
#6
Re: [RESOLVED] order of evaluation of condition expression.
Just remember (as previously pointed out), the quoted parts of the standard ONLY apply for "non-overloaded" operators. This means that if you create your own "operator &&" or ""operator ||" you will NOT have the short-circuit behaviour.
So without knowing the TYPES of "ConditionA", "ConditionB" and "ConditionC" it is actually impossible to tell if the ordering and short-circuit apply...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 17th, 2008, 09:46 AM
#7
Re: [RESOLVED] order of evaluation of condition expression.
And this is why Meyers recommends never overloading logical AND, logical OR or comma (you can't overload the ternary operator).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
November 18th, 2008, 12:25 AM
#8
Re: [RESOLVED] order of evaluation of condition expression.
Meyer's is....can't find words ( all good words fall short of describing his metel).
I
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|