1 <?xml version=
"1.0" encoding=
"utf-8" ?>
2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
4 <!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost -->
5 <!-- Software License, Version 1.0. (See accompanying -->
6 <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
8 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8" />
9 <meta name=
"generator" content=
"Docutils 0.3.6: http://docutils.sourceforge.net/" />
10 <title>THE BOOST MPL LIBRARY: The Importance of Being Lazy
</title>
11 <link rel=
"stylesheet" href=
"../style.css" type=
"text/css" />
13 <body class=
"docframe">
14 <table class=
"header"><tr class=
"header"><td class=
"header-group navigation-bar"><span class=
"navigation-group"><a href=
"./lambda-and-non.html" class=
"navigation-link">Prev
</a> <a href=
"./details.html" class=
"navigation-link">Next
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./lambda-and-non.html" class=
"navigation-link">Back
</a> Along
</span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./lambda-details.html" class=
"navigation-link">Up
</a> <a href=
"../index.html" class=
"navigation-link">Home
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./tutorial_toc.html" class=
"navigation-link">Full TOC
</a></span></td>
15 <td class=
"header-group page-location"><a href=
"../index.html" class=
"navigation-link">Front Page
</a> /
<a href=
"./tutorial-metafunctions.html" class=
"navigation-link">Tutorial: Metafunctions and Higher-Order Metaprogramming
</a> /
<a href=
"./lambda-details.html" class=
"navigation-link">Lambda Details
</a> /
<a href=
"./the-importance-of-being.html" class=
"navigation-link">The Importance of Being Lazy
</a></td>
16 </tr></table><div class=
"header-separator"></div>
17 <div class=
"section" id=
"the-importance-of-being">
18 <h1><a class=
"toc-backref" href=
"./lambda-details.html#id58" name=
"the-importance-of-being">The Importance of Being Lazy
</a></h1>
19 <p>Recall the definition of
<tt class=
"literal"><span class=
"pre">always_int
</span></tt> from the previous chapter:
</p>
20 <pre class=
"literal-block">
26 <p>Nullary metafunctions might not seem very important at first, since
27 something like
<tt class=
"literal"><span class=
"pre">add_pointer
<int
></span></tt> could be replaced by
<tt class=
"literal"><span class=
"pre">int*
</span></tt> in
28 any lambda expression where it appears. Not all nullary
29 metafunctions are that simple, though:
</p>
30 <pre class=
"literal-block">
31 typedef mpl::vector
<int, char*, double
&> seq;
32 typedef
<strong>mpl::transform
<seq, boost::add_pointer
<_
> ></strong> calc_ptr_seq;
34 <!-- @ example.prepend('''
35 #include <boost/mpl/vector.hpp>
36 #include <boost/mpl/transform.hpp>
39 <p>Note that
<tt class=
"literal"><span class=
"pre">calc_ptr_seq
</span></tt> is a nullary metafunction, since it has
40 <tt class=
"literal"><span class=
"pre">transform
</span></tt>'s nested
<tt class=
"literal"><span class=
"pre">::type
</span></tt>. A C++ template is not
41 instantiated until we actually
"look inside it,
" though. Just
42 naming
<tt class=
"literal"><span class=
"pre">calc_ptr_seq
</span></tt> does not cause it to be evaluated, since we
43 haven't accessed its
<tt class=
"literal"><span class=
"pre">::type
</span></tt> yet.
</p>
44 <p>Metafunctions can be invoked
<em>lazily
</em>, rather than immediately upon
45 supplying all of their arguments. We can use
<strong>lazy evaluation
</strong> to
46 improve compilation time when a metafunction result is only going
47 to be used conditionally. We can sometimes also avoid contorting
48 program structure by
<em>naming
</em> an invalid computation without
49 actually performing it. That's what we've done with
50 <tt class=
"literal"><span class=
"pre">calc_ptr_seq
</span></tt> above, since you can't legally form
<tt class=
"literal"><span class=
"pre">double
&*
</span></tt>.
51 Laziness and all of its virtues will be a recurring theme
52 throughout this book.
</p>
55 <div class=
"footer-separator"></div>
56 <table class=
"footer"><tr class=
"footer"><td class=
"header-group navigation-bar"><span class=
"navigation-group"><a href=
"./lambda-and-non.html" class=
"navigation-link">Prev
</a> <a href=
"./details.html" class=
"navigation-link">Next
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./lambda-and-non.html" class=
"navigation-link">Back
</a> Along
</span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./lambda-details.html" class=
"navigation-link">Up
</a> <a href=
"../index.html" class=
"navigation-link">Home
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./tutorial_toc.html" class=
"navigation-link">Full TOC
</a></span></td>