C++ short-circuiting of booleans

tinkertime picture tinkertime · Nov 25, 2009 · Viewed 13.3k times · Source

I'm new to c++ and am curious how the compiler handles lazy evaluation of booleans. For example,

if(A == 1 || B == 2){...}

If A does equal 1, is the B==2 part ever evaluated?

Answer

James McNellis picture James McNellis · Nov 25, 2009

No, the B==2 part is not evaluated. This is called short-circuit evaluation.

Edit: As Robert C. Cartaino rightly points out, if the logical operator is overloaded, short-circuit evaluation does not take place (that having been said, why someone would overload a logical operator is beyond me).