handling-placeholders.html 5.5 KB

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