Short-circuiting in logical operations is a very useful and an often used feature:
if (cond_a() && cond_b())
Should cond_a()
evaluate to false
, cond_b()
is guaranteed not to be evaluated. This is useful for two reasons. One is performance: if cond_b()
is an expensive operation we do not want to evaluate it if we can determine the final result from only evaluating cond_a()
. The other reason is program correctness:
if (ptr && ptr->is_ready())
Here the first operand is a precondition for the second operand, and we do not want the latter to be evaluated if the former isn’t true. Short-circuiting makes this work as desired.
For similar reasons, we might want to use short-circuiting of logical operations in meta-functions. However, in meta-functions this looks and works differently. This is what this post is about. Continue reading