If I write:
if(somePtr != NULL && somePtr->someFun() == SUCCESS )
{
/**/
}
Will it be assured that somePtr != NULL
will be checked before somePtr->someFun() == SUCCESS
?
Is there any chance that my compiler will reorder these two?
Is there any chance that my compiler will reorder these two?
Nope. It is guaranteed that &&
evaluates the second expression only if the first one is true
(incidentally, it also introduces a sequence point into the whole expression).
The && operator groups left-to-right. The operands are both contextually 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.The result is a bool. If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression.
(C++11, [expr.log.and]; emphasis added)