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 lambda Metafunction
</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=
"./handling-placeholders.html" class=
"navigation-link">Prev
</a> <a href=
"./the-apply-metafunction.html" class=
"navigation-link">Next
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group">Back
<a href=
"./the-apply-metafunction.html" class=
"navigation-link">Along
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./handling-placeholders.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=
"./handling-placeholders.html" class=
"navigation-link">Handling Placeholders
</a> /
<a href=
"./the-lambda-metafunction.html" class=
"navigation-link">The lambda Metafunction
</a></td>
16 </tr></table><div class=
"header-separator"></div>
17 <div class=
"section" id=
"the-lambda-metafunction">
18 <h1><a class=
"toc-backref" href=
"./handling-placeholders.html#id49" name=
"the-lambda-metafunction">The
<tt class=
"literal"><span class=
"pre">lambda
</span></tt> Metafunction
</a></h1>
19 <p>We can
<em>generate
</em> a metafunction class from
20 <tt class=
"literal"><span class=
"pre">boost::add_pointer
<_1
></span></tt>, using MPL's
<tt class=
"literal"><span class=
"pre">lambda
</span></tt> metafunction:
</p>
21 <pre class=
"literal-block">
22 template
<class X
>
24 : twice
<<strong>typename mpl::lambda
<boost::add_pointer
<_1
> >::type
</strong>, X
>
29 two_pointers
<int
>::type
34 <!-- @ prefix.append('#include <boost/mpl/lambda.hpp>')
36 <p>We'll refer to metafunction classes like
<tt class=
"literal"><span class=
"pre">add_pointer_f
</span></tt> and
37 placeholder expressions like
<tt class=
"literal"><span class=
"pre">boost::add_pointer
<_1
></span></tt>
38 as
<strong>lambda expressions
</strong>. The term, meaning
"unnamed function
39 object,
" was introduced in the
1930s by the logician Alonzo Church
40 as part of a fundamental theory of computation he called the
41 <em>lambda-calculus
</em>.
<a class=
"footnote-reference" href=
"#lambda" id=
"id10" name=
"id10">[
4]
</a> MPL uses the somewhat obscure word
42 <tt class=
"literal"><span class=
"pre">lambda
</span></tt> because of its well-established precedent in functional
43 programming languages.
</p>
44 <table class=
"footnote" frame=
"void" id=
"lambda" rules=
"none">
45 <colgroup><col class=
"label" /><col /></colgroup>
47 <tr><td class=
"label"><a class=
"fn-backref" href=
"#id10" name=
"lambda">[
4]
</a></td><td>See
<a class=
"reference" href=
"http://en.wikipedia.org/wiki/Lambda_calculus" target=
"_top">http://en.wikipedia.org/wiki/Lambda_calculus
</a> for
48 an in-depth treatment, including a reference to Church's paper
49 proving that the equivalence of lambda expressions is in general
50 not decidable.
</td></tr>
53 <p>Although its primary purpose is to turn placeholder expressions
54 into metafunction classes,
<tt class=
"literal"><span class=
"pre">mpl::lambda
</span></tt> can accept any lambda
55 expression, even if it's already a metafunction class. In that
56 case,
<tt class=
"literal"><span class=
"pre">lambda
</span></tt> returns its argument unchanged. MPL algorithms
57 like
<tt class=
"literal"><span class=
"pre">transform
</span></tt> call
<tt class=
"literal"><span class=
"pre">lambda
</span></tt> internally, before invoking the
58 resulting metafunction class, so that they work equally well with
59 either kind of lambda expression. We can apply the same strategy
60 to
<tt class=
"literal"><span class=
"pre">twice
</span></tt>:
</p>
61 <pre class=
"literal-block">
62 template
<class F, class X
>
65 typename mpl::lambda
<F
>::type
67 typename mpl::lambda
<F
>::type
73 <p>Now we can use
<tt class=
"literal"><span class=
"pre">twice
</span></tt> with metafunction classes
<em>and
</em>
74 placeholder expressions:
</p>
75 <pre class=
"literal-block">
78 twice
<<strong>add_pointer_f
</strong>, int
>::type p =
&x;
79 twice
<<strong>boost::add_pointer
<_1
></strong>, int
>::type q =
&x;
81 <!-- @ stack[-2:] = [ apply1, stack[-2], add_pointer_f, stack[-1]]
85 <div class=
"footer-separator"></div>
86 <table class=
"footer"><tr class=
"footer"><td class=
"header-group navigation-bar"><span class=
"navigation-group"><a href=
"./handling-placeholders.html" class=
"navigation-link">Prev
</a> <a href=
"./the-apply-metafunction.html" class=
"navigation-link">Next
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group">Back
<a href=
"./the-apply-metafunction.html" class=
"navigation-link">Along
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./handling-placeholders.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>