[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
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
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:
Quote:
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.
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:
Quote:
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.
Quote:
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.
Quote:
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.
Quote:
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.
Re: order of evaluation of condition expression.
Thanks a LOT Graham and particularly laserlight for giving me the exact references.
I
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...
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).
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