<?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: Handling Placeholders</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="./higher-order.html" class="navigation-link">Prev</a> <a href="./the-lambda-metafunction.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./higher-order.html" class="navigation-link">Back</a> <a href="./more-lambda-capabilities.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial-metafunctions.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></td> </tr></table><div class="header-separator"></div> <div class="section" id="handling-placeholders"> <h1><a class="toc-backref" href="./tutorial-metafunctions.html#id48" name="handling-placeholders">Handling Placeholders</a></h1> <p>Our implementation of <tt class="literal"><span class="pre">twice</span></tt> already works with metafunction classes. Ideally, we would like it to work with placeholder expressions too, much the same as <tt class="literal"><span class="pre">mpl::transform</span></tt> allows us to pass either form. For example, we would like to be able to write:</p> <pre class="literal-block"> template <class X> struct two_pointers : twice<<strong>boost::add_pointer<_1></strong>, X> {}; </pre> <!-- @ example.append('typedef two_pointers<int>::type intstar2;') compile('all', pop = 1, expect_error = True) --> <p>But when we look at the implementation of <tt class="literal"><span class="pre">boost::add_pointer</span></tt>, it becomes clear that the current definition of <tt class="literal"><span class="pre">twice</span></tt> can't work that way.</p> <pre class="literal-block"> template <class T> struct add_pointer { typedef T* type; }; </pre> <!-- @ compile() --> <p>To be invokable by <tt class="literal"><span class="pre">twice</span></tt>, <tt class="literal"><span class="pre">boost::add_pointer<_1></span></tt> would have to be a metafunction class, along the lines of <tt class="literal"><span class="pre">add_pointer_f</span></tt>. Instead, it's just a nullary metafunction returning the almost senseless type <tt class="literal"><span class="pre">_1*</span></tt>. Any attempt to use <tt class="literal"><span class="pre">two_pointers</span></tt> will fail when <tt class="literal"><span class="pre">apply1</span></tt> reaches for a nested <tt class="literal"><span class="pre">::apply</span></tt> metafunction in <tt class="literal"><span class="pre">boost::add_pointer<_1></span></tt> and finds that it doesn't exist.</p> <p>We've determined that we don't get the behavior we want automatically, so what next? Since <tt class="literal"><span class="pre">mpl::transform</span></tt> can do this sort of thing, there ought to be a way for us to do it too — and so there is.</p> <ul class="toc simple" id="outline"> <li><a class="reference" href="./the-lambda-metafunction.html" id="id49" name="id49">The <tt class="literal"><span class="pre">lambda</span></tt> Metafunction</a></li> <li><a class="reference" href="./the-apply-metafunction.html" id="id50" name="id50">The <tt class="literal"><span class="pre">apply</span></tt> Metafunction</a></li> </ul> </div> <div class="footer-separator"></div> <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./higher-order.html" class="navigation-link">Prev</a> <a href="./the-lambda-metafunction.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./higher-order.html" class="navigation-link">Back</a> <a href="./more-lambda-capabilities.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial-metafunctions.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>