Lazy evaluation means that the function is not called until we really need its result. Knowledge of this recipe is highly recommended for writing good metafunctions. The importance of lazy evaluation will be shown in the following example.
Imagine that we are writing some metafunction that accepts a function Func
, a parameter Param
, and a condition Cond
. The resulting type of that function must be a fallback
type if applying the Cond
to Param
returns false
, otherwise the result must be a Func
applied to Param
:
struct fallback; template < class Func, class Param, class Cond, class Fallback = fallback> struct apply_if;
This metafunction is the place where we cannot live without lazy evaluation, because it may be impossible to apply Func
to Param
if the Cond
is not met. Such attempts will always result in compilation failure and Fallback
is never returned.
Reading Chapter 4, Compile-time Tricks, is highly recommended...