Short Circuit

Started by
4 comments, last by Dranith 18 years, 5 months ago
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.
Advertisement
Yes, I believe it's called lazy evaluation.
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

"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.

This topic is closed to new replies.

Advertisement