Short Circuit
Does C++ short circuit? (MS VC++ Express)
This means that this will work:
Foo* foo = NULL;
if (foo != NULL && foo->Bar())
{
....
}
Cause it will not evaluate foo->Bar() if foo == NULL since that comes first.
Yes. I'm not sure on how guaranteed that is, or in what cases it might fail, but I've always seen it work, and I've never seen people more knowledgable than me comment on it, making me suspect it is something guaranteed.
Yes, it guaranteed by standards compliant C++ compilers. "...&& guarantees left-to-right evaluation; the second operand is not evaluated if the first operand is false." (Section 5.14 paragraph 1 in ISO/IEC 14882:2003)
Quote:Original post by SiCrane
Yes, it guaranteed by standards compliant C++ compilers. "...&& guarantees left-to-right evaluation; the second operand is not evaluated if the first operand is false." (Section 5.14 paragraph 1 in ISO/IEC 14882:2003)
Also I believe that, similarly, the || operator will not evaluate the second operand if the first is true.
"Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true." (Section 5.15 paragraph 1)
So yes, or works the same way.
So yes, or works the same way.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement