Sign in to follow this  

Short Circuit

This topic is 4471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites
Sign in to follow this