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: Handling Placeholders</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="./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> |
---|
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></td> |
---|
13 | </tr></table><div class="header-separator"></div> |
---|
14 | <div class="section" id="handling-placeholders"> |
---|
15 | <h1><a class="toc-backref" href="./tutorial-metafunctions.html#id48" name="handling-placeholders">Handling Placeholders</a></h1> |
---|
16 | <p>Our implementation of <tt class="literal"><span class="pre">twice</span></tt> already works with metafunction |
---|
17 | classes. Ideally, we would |
---|
18 | like it to work with placeholder expressions too, much the same as |
---|
19 | <tt class="literal"><span class="pre">mpl::transform</span></tt> allows us to pass either form. For example, we |
---|
20 | would like to be able to write:</p> |
---|
21 | <pre class="literal-block"> |
---|
22 | template <class X> |
---|
23 | struct two_pointers |
---|
24 | : twice<<strong>boost::add_pointer<_1></strong>, X> |
---|
25 | {}; |
---|
26 | </pre> |
---|
27 | <!-- @ example.append('typedef two_pointers<int>::type intstar2;') |
---|
28 | compile('all', pop = 1, expect_error = True) --> |
---|
29 | <p>But when we look at the implementation of <tt class="literal"><span class="pre">boost::add_pointer</span></tt>, |
---|
30 | it becomes clear that the current definition of <tt class="literal"><span class="pre">twice</span></tt> can't |
---|
31 | work that way.</p> |
---|
32 | <pre class="literal-block"> |
---|
33 | template <class T> |
---|
34 | struct add_pointer |
---|
35 | { |
---|
36 | typedef T* type; |
---|
37 | }; |
---|
38 | </pre> |
---|
39 | <!-- @ compile() --> |
---|
40 | <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 |
---|
41 | to be a metafunction class, along the lines of <tt class="literal"><span class="pre">add_pointer_f</span></tt>. |
---|
42 | Instead, it's just a nullary metafunction returning the almost |
---|
43 | 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 |
---|
44 | fail when <tt class="literal"><span class="pre">apply1</span></tt> reaches for a nested <tt class="literal"><span class="pre">::apply</span></tt> |
---|
45 | metafunction in <tt class="literal"><span class="pre">boost::add_pointer<_1></span></tt> and finds that it |
---|
46 | doesn't exist.</p> |
---|
47 | <p>We've determined that we don't get the behavior we want |
---|
48 | automatically, so what next? Since <tt class="literal"><span class="pre">mpl::transform</span></tt> can do this |
---|
49 | sort of thing, there ought to be a way for us to do it too — and |
---|
50 | so there is.</p> |
---|
51 | <ul class="toc simple" id="outline"> |
---|
52 | <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> |
---|
53 | <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> |
---|
54 | </ul> |
---|
55 | </div> |
---|
56 | |
---|
57 | <div class="footer-separator"></div> |
---|
58 | <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> |
---|
59 | </tr></table></body> |
---|
60 | </html> |
---|