<tdclass="header-group page-location"><ahref="../index.html"class="navigation-link">Front Page</a> / <ahref="./tutorial-metafunctions.html"class="navigation-link">Tutorial: Metafunctions and Higher-Order Metaprogramming</a> / <ahref="./lambda-details.html"class="navigation-link">Lambda Details</a> / <ahref="./the-importance-of-being.html"class="navigation-link">The Importance of Being Lazy</a></td>
</tr></table><divclass="header-separator"></div>
<divclass="section"id="the-importance-of-being">
<h1><aclass="toc-backref"href="./lambda-details.html#id58"name="the-importance-of-being">The Importance of Being Lazy</a></h1>
<p>Recall the definition of <ttclass="literal"><spanclass="pre">always_int</span></tt> from the previous chapter:</p>
<preclass="literal-block">
struct always_int
{
typedef int type;
};
</pre>
<p>Nullary metafunctions might not seem very important at first, since
something like <ttclass="literal"><spanclass="pre">add_pointer<int></span></tt> could be replaced by <ttclass="literal"><spanclass="pre">int*</span></tt> in
any lambda expression where it appears. Not all nullary
<p>Note that <ttclass="literal"><spanclass="pre">calc_ptr_seq</span></tt> is a nullary metafunction, since it has
<ttclass="literal"><spanclass="pre">transform</span></tt>'s nested <ttclass="literal"><spanclass="pre">::type</span></tt>. A C++ template is not
instantiated until we actually "look inside it," though. Just
naming <ttclass="literal"><spanclass="pre">calc_ptr_seq</span></tt> does not cause it to be evaluated, since we
haven't accessed its <ttclass="literal"><spanclass="pre">::type</span></tt> yet.</p>
<p>Metafunctions can be invoked <em>lazily</em>, rather than immediately upon
supplying all of their arguments. We can use <strong>lazy evaluation</strong> to
improve compilation time when a metafunction result is only going
to be used conditionally. We can sometimes also avoid contorting
program structure by <em>naming</em> an invalid computation without
actually performing it. That's what we've done with
<ttclass="literal"><spanclass="pre">calc_ptr_seq</span></tt> above, since you can't legally form <ttclass="literal"><spanclass="pre">double&*</span></tt>.
Laziness and all of its virtues will be a recurring theme