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 apply 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=
"./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>
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-apply-metafunction.html" class=
"navigation-link">The apply Metafunction
</a></td>
16 </tr></table><div class=
"header-separator"></div>
17 <div class=
"section" id=
"the-apply-metafunction">
18 <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>
19 <p>Invoking the result of
<tt class=
"literal"><span class=
"pre">lambda
</span></tt> is such a common pattern
20 that MPL provides an
<tt class=
"literal"><span class=
"pre">apply
</span></tt> metafunction to do just
21 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>
23 <pre class=
"literal-block">
24 #include
<boost/mpl/apply.hpp
>
26 template
<class F, class X
>
28 : mpl::apply
<F, typename mpl::apply
<F,X
>::type
>
31 <!-- @ example.append(twice_test + '''
32 BOOST_MPL_ASSERT((boost::is_same<twice<boost::add_pointer<_1>,int>::type,int**>));
35 prefix.append('#include <boost/mpl/apply.hpp>') -->
36 <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>
37 template that we wrote, with two additional features:
</p>
39 <li><p class=
"first">While
<tt class=
"literal"><span class=
"pre">apply1
</span></tt> operates only on metafunction classes, the first
40 argument to
<tt class=
"literal"><span class=
"pre">mpl::apply
</span></tt> can be any lambda expression
41 (including those built with placeholders).
</p>
43 <li><p class=
"first">While
<tt class=
"literal"><span class=
"pre">apply1
</span></tt> accepts only one additional argument to which
44 the metafunction class will be applied,
<tt class=
"literal"><span class=
"pre">mpl::apply
</span></tt> can
45 invoke its first argument on any number from zero to five
46 additional arguments.
<a class=
"footnote-reference" href=
"#arity" id=
"id11" name=
"id11">[
5]
</a> For example:
</p>
47 <pre class=
"literal-block">
48 // binary lambda expression applied to
2 additional arguments
50 mpl::plus
<_1,_2
>
51 ,
<strong>mpl::int_
<6></strong>
52 ,
<strong>mpl::int_
<7></strong>
53 >::type::value // ==
13
57 <table class=
"footnote" frame=
"void" id=
"arity" rules=
"none">
58 <colgroup><col class=
"label" /><col /></colgroup>
60 <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>
61 for a description of how to change the maximum number of
62 arguments handled by
<tt class=
"literal"><span class=
"pre">mpl::apply
</span></tt>.
</td></tr>
65 <!-- @ prefix+=['#include <boost/mpl/plus.hpp>']
66 example.wrap('enum { is13 = ','''};
67 BOOST_STATIC_ASSERT(is13 == 13);''')
69 <div class=
"admonition-guideline admonition">
70 <p class=
"admonition-title first">Guideline
</p>
71 <p>When writing a metafunction that invokes one of its arguments,
72 use
<tt class=
"literal"><span class=
"pre">mpl::apply
</span></tt> so that it works with lambda expressions.
</p>
76 <div class=
"footer-separator"></div>
77 <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>