]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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) --> | |
7 | <head> | |
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" /> | |
12 | </head> | |
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> | |
23 | struct two_pointers | |
24 | : twice<<strong>typename mpl::lambda<boost::add_pointer<_1> >::type</strong>, X> | |
25 | {}; | |
26 | ||
27 | BOOST_STATIC_ASSERT(( | |
28 | boost::is_same< | |
29 | two_pointers<int>::type | |
30 | , int** | |
31 | >::value | |
32 | )); | |
33 | </pre> | |
34 | <!-- @ prefix.append('#include <boost/mpl/lambda.hpp>') | |
35 | compile('all') --> | |
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> | |
46 | <tbody valign="top"> | |
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> | |
51 | </tbody> | |
52 | </table> | |
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> | |
63 | struct twice | |
64 | : apply1< | |
65 | typename mpl::lambda<F>::type | |
66 | , typename apply1< | |
67 | typename mpl::lambda<F>::type | |
68 | , X | |
69 | >::type | |
70 | > | |
71 | {}; | |
72 | </pre> | |
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"> | |
76 | int* x; | |
77 | ||
78 | twice<<strong>add_pointer_f</strong>, int>::type p = &x; | |
79 | twice<<strong>boost::add_pointer<_1></strong>, int>::type q = &x; | |
80 | </pre> | |
81 | <!-- @ stack[-2:] = [ apply1, stack[-2], add_pointer_f, stack[-1]] | |
82 | compile('all') --> | |
83 | </div> | |
84 | ||
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> | |
87 | </tr></table></body> | |
88 | </html> |