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 | <head> |
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
6 | <meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" /> |
---|
7 | <title>THE BOOST MPL LIBRARY: The apply Metafunction</title> |
---|
8 | <link rel="stylesheet" href="../style.css" type="text/css" /> |
---|
9 | </head> |
---|
10 | <body class="docframe"> |
---|
11 | <table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./the-lambda-metafunction.html" class="navigation-link">Prev</a> <a href="./more-lambda-capabilities.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./the-lambda-metafunction.html" class="navigation-link">Back</a> Along</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> |
---|
12 | <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-apply-metafunction.html" class="navigation-link">The apply Metafunction</a></td> |
---|
13 | </tr></table><div class="header-separator"></div> |
---|
14 | <div class="section" id="the-apply-metafunction"> |
---|
15 | <h1><a class="toc-backref" href="./handling-placeholders.html#id50" name="the-apply-metafunction">The <tt class="literal"><span class="pre">apply</span></tt> Metafunction</a></h1> |
---|
16 | <p>Invoking the result of <tt class="literal"><span class="pre">lambda</span></tt> is such a common pattern |
---|
17 | that MPL provides an <tt class="literal"><span class="pre">apply</span></tt> metafunction to do just |
---|
18 | that. Using <tt class="literal"><span class="pre">mpl::apply</span></tt>, our flexible version of <tt class="literal"><span class="pre">twice</span></tt> |
---|
19 | becomes:</p> |
---|
20 | <pre class="literal-block"> |
---|
21 | #include <boost/mpl/apply.hpp> |
---|
22 | |
---|
23 | template <class F, class X> |
---|
24 | struct twice |
---|
25 | : mpl::apply<F, typename mpl::apply<F,X>::type> |
---|
26 | {}; |
---|
27 | </pre> |
---|
28 | <!-- @ example.append(twice_test + ''' |
---|
29 | BOOST_MPL_ASSERT((boost::is_same<twice<boost::add_pointer<_1>,int>::type,int**>)); |
---|
30 | ''') |
---|
31 | compile() |
---|
32 | prefix.append('#include <boost/mpl/apply.hpp>') --> |
---|
33 | <p>You can think of <tt class="literal"><span class="pre">mpl::apply</span></tt> as being just like the <tt class="literal"><span class="pre">apply1</span></tt> |
---|
34 | template that we wrote, with two additional features:</p> |
---|
35 | <ol class="arabic"> |
---|
36 | <li><p class="first">While <tt class="literal"><span class="pre">apply1</span></tt> operates only on metafunction classes, the first |
---|
37 | argument to <tt class="literal"><span class="pre">mpl::apply</span></tt> can be any lambda expression |
---|
38 | (including those built with placeholders).</p> |
---|
39 | </li> |
---|
40 | <li><p class="first">While <tt class="literal"><span class="pre">apply1</span></tt> accepts only one additional argument to which |
---|
41 | the metafunction class will be applied, <tt class="literal"><span class="pre">mpl::apply</span></tt> can |
---|
42 | invoke its first argument on any number from zero to five |
---|
43 | additional arguments. <a class="footnote-reference" href="#arity" id="id11" name="id11">[5]</a> For example:</p> |
---|
44 | <pre class="literal-block"> |
---|
45 | // binary lambda expression applied to 2 additional arguments |
---|
46 | mpl::apply< |
---|
47 | mpl::plus<_1,_2> |
---|
48 | , <strong>mpl::int_<6></strong> |
---|
49 | , <strong>mpl::int_<7></strong> |
---|
50 | >::type::value // == 13 |
---|
51 | </pre> |
---|
52 | </li> |
---|
53 | </ol> |
---|
54 | <table class="footnote" frame="void" id="arity" rules="none"> |
---|
55 | <colgroup><col class="label" /><col /></colgroup> |
---|
56 | <tbody valign="top"> |
---|
57 | <tr><td class="label"><a class="fn-backref" href="#id11" name="arity">[5]</a></td><td>See the Configuration Macros section of the <a class="reference" href="./reference-manual.html">the MPL reference manual</a> |
---|
58 | for a description of how to change the maximum number of |
---|
59 | arguments handled by <tt class="literal"><span class="pre">mpl::apply</span></tt>.</td></tr> |
---|
60 | </tbody> |
---|
61 | </table> |
---|
62 | <!-- @ prefix+=['#include <boost/mpl/plus.hpp>'] |
---|
63 | example.wrap('enum { is13 = ','''}; |
---|
64 | BOOST_STATIC_ASSERT(is13 == 13);''') |
---|
65 | compile() --> |
---|
66 | <div class="admonition-guideline admonition"> |
---|
67 | <p class="admonition-title first">Guideline</p> |
---|
68 | <p>When writing a metafunction that invokes one of its arguments, |
---|
69 | use <tt class="literal"><span class="pre">mpl::apply</span></tt> so that it works with lambda expressions.</p> |
---|
70 | </div> |
---|
71 | </div> |
---|
72 | |
---|
73 | <div class="footer-separator"></div> |
---|
74 | <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./the-lambda-metafunction.html" class="navigation-link">Prev</a> <a href="./more-lambda-capabilities.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./the-lambda-metafunction.html" class="navigation-link">Back</a> Along</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> |
---|
75 | </tr></table></body> |
---|
76 | </html> |
---|