<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost --> <!-- Software License, Version 1.0. (See accompanying --> <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" /> <title>THE BOOST MPL LIBRARY: The apply Metafunction</title> <link rel="stylesheet" href="../style.css" type="text/css" /> </head> <body class="docframe"> <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> <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> </tr></table><div class="header-separator"></div> <div class="section" id="the-apply-metafunction"> <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> <p>Invoking the result of <tt class="literal"><span class="pre">lambda</span></tt> is such a common pattern that MPL provides an <tt class="literal"><span class="pre">apply</span></tt> metafunction to do just 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> becomes:</p> <pre class="literal-block"> #include <boost/mpl/apply.hpp> template <class F, class X> struct twice : mpl::apply<F, typename mpl::apply<F,X>::type> {}; </pre> <!-- @ example.append(twice_test + ''' BOOST_MPL_ASSERT((boost::is_same<twice<boost::add_pointer<_1>,int>::type,int**>)); ''') compile() prefix.append('#include <boost/mpl/apply.hpp>') --> <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> template that we wrote, with two additional features:</p> <ol class="arabic"> <li><p class="first">While <tt class="literal"><span class="pre">apply1</span></tt> operates only on metafunction classes, the first argument to <tt class="literal"><span class="pre">mpl::apply</span></tt> can be any lambda expression (including those built with placeholders).</p> </li> <li><p class="first">While <tt class="literal"><span class="pre">apply1</span></tt> accepts only one additional argument to which the metafunction class will be applied, <tt class="literal"><span class="pre">mpl::apply</span></tt> can invoke its first argument on any number from zero to five additional arguments. <a class="footnote-reference" href="#arity" id="id11" name="id11">[5]</a> For example:</p> <pre class="literal-block"> // binary lambda expression applied to 2 additional arguments mpl::apply< mpl::plus<_1,_2> , <strong>mpl::int_<6></strong> , <strong>mpl::int_<7></strong> >::type::value // == 13 </pre> </li> </ol> <table class="footnote" frame="void" id="arity" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <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> for a description of how to change the maximum number of arguments handled by <tt class="literal"><span class="pre">mpl::apply</span></tt>.</td></tr> </tbody> </table> <!-- @ prefix+=['#include <boost/mpl/plus.hpp>'] example.wrap('enum { is13 = ','''}; BOOST_STATIC_ASSERT(is13 == 13);''') compile() --> <div class="admonition-guideline admonition"> <p class="admonition-title first">Guideline</p> <p>When writing a metafunction that invokes one of its arguments, use <tt class="literal"><span class="pre">mpl::apply</span></tt> so that it works with lambda expressions.</p> </div> </div> <div class="footer-separator"></div> <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> </tr></table></body> </html>