higher-order.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: Higher-Order Metafunctions</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="./implementing-division.html" class="navigation-link">Prev</a>&nbsp;<a href="./handling-placeholders.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Back</a>&nbsp;<a href="./handling-placeholders.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="./higher-order.html" class="navigation-link">Higher-Order Metafunctions</a></td>
  16. </tr></table><div class="header-separator"></div>
  17. <div class="section" id="higher-order">
  18. <h1><a class="toc-backref" href="./tutorial-metafunctions.html#id47" name="higher-order">Higher-Order Metafunctions</a></h1>
  19. <p>In the previous section we used two different forms —
  20. metafunction classes and placeholder expressions —
  21. to pass and return metafunctions just like any other metadata.
  22. Bundling metafunctions into &quot;first class metadata&quot; allows
  23. <tt class="literal"><span class="pre">transform</span></tt> to perform an infinite variety of different
  24. operations: in our case, multiplication and division of dimensions.
  25. Though the idea of using functions to manipulate other functions
  26. may seem simple, its great power and flexibility <a class="citation-reference" href="#hudak89" id="id9" name="id9">[Hudak89]</a> has
  27. earned it a fancy title: <strong>higher-order functional programming</strong>.
  28. A function that operates on another function is known as a
  29. <strong>higher-order function</strong>. It follows that <tt class="literal"><span class="pre">transform</span></tt> is a
  30. higher-order
  31. metafunction: a metafunction that operates on another metafunction.</p>
  32. <table class="citation" frame="void" id="hudak89" rules="none">
  33. <colgroup><col class="label" /><col /></colgroup>
  34. <tbody valign="top">
  35. <tr><td class="label"><a class="fn-backref" href="#id9" name="hudak89">[Hudak89]</a></td><td>Paul Hudak. &quot;Conception, Evolution, and Application of
  36. Functional Programming Languages,&quot; ACM Computing Surveys 21,
  37. no. 3 Pages: 359 - 411. New York: ACM Press. 1989.
  38. ISSN:0360-0300. http://doi.acm.org/10.1145/72551.72554.</td></tr>
  39. </tbody>
  40. </table>
  41. <p>Now that we've seen the power of higher-order metafunctions at
  42. work, it would be good to be able to create new ones. In order to
  43. explore the basic mechanisms, let's try a simple example. Our task
  44. is to write a metafunction called <tt class="literal"><span class="pre">twice</span></tt>, which — given a unary
  45. metafunction <em>f</em> and arbitrary metadata <em>x</em> — computes:</p>
  46. <blockquote>
  47. <em>twice</em>(<em>f</em>, <em>x</em>) := <em>f</em>(<em>f</em>(<em>x</em>))</blockquote>
  48. <p>This might seem like a trivial example, and in fact it is. You
  49. won't find much use for <tt class="literal"><span class="pre">twice</span></tt> in real code. We hope you'll
  50. bear with us anyway: Because it doesn't do much more than accept
  51. and invoke a metafunction, <tt class="literal"><span class="pre">twice</span></tt> captures all the essential
  52. elements of &quot;higher-orderness&quot; without any distracting details.</p>
  53. <p>If <em>f</em> is a metafunction class, the definition of <tt class="literal"><span class="pre">twice</span></tt> is
  54. straightforward:</p>
  55. <pre class="literal-block">
  56. template &lt;class F, class X&gt;
  57. struct twice
  58. {
  59. typedef typename F::template apply&lt;X&gt;::type once; // f(x)
  60. typedef typename F::template apply&lt;once&gt;::type type; // f(f(x))
  61. };
  62. </pre>
  63. <!-- @ prefix.append(
  64. '''#include <boost/type_traits/add_pointer.hpp>
  65. #include <boost/static_assert.hpp>
  66. #include <boost/type_traits/is_same.hpp>''')
  67. twice_test = '''
  68. #include <boost/mpl/assert.hpp>
  69. struct add_pointer_f
  70. {
  71. template <class T> struct apply : boost::add_pointer<T>
  72. {};
  73. };
  74. BOOST_MPL_ASSERT((boost::is_same<twice<add_pointer_f,int>::type,int**>));
  75. '''
  76. example.append(twice_test)
  77. compile() -->
  78. <!-- @litre_translator.line_offset -= 7 -->
  79. <p>Or, applying metafunction forwarding:</p>
  80. <pre class="literal-block">
  81. template &lt;class F, class X&gt;
  82. struct twice
  83. : F::template apply&lt;
  84. typename F::template apply&lt;X&gt;::type
  85. &gt;
  86. {};
  87. </pre>
  88. <!-- @ example.append(twice_test)
  89. compile() -->
  90. <div class="admonition-c-language-note admonition">
  91. <p class="admonition-title first">C++ Language Note</p>
  92. <p>The C++ standard requires the <tt class="literal"><span class="pre">template</span></tt> keyword when we use a
  93. <strong>dependent name</strong> that refers to a member template.
  94. <tt class="literal"><span class="pre">F::apply</span></tt> may or may not name a template, <em>depending</em> on the
  95. particular <tt class="literal"><span class="pre">F</span></tt> that is passed. See <a class="reference" href="./resources.html">the book's</a> Appendix B for more
  96. information about <tt class="literal"><span class="pre">template</span></tt>.</p>
  97. </div>
  98. <p>Given the need to sprinkle our code with the <tt class="literal"><span class="pre">template</span></tt> keyword,
  99. it would be nice to reduce the syntactic burden of invoking
  100. metafunction classes. As usual, the solution is to factor the
  101. pattern into a metafunction:</p>
  102. <pre class="literal-block">
  103. template &lt;class UnaryMetaFunctionClass, class Arg&gt;
  104. struct apply1
  105. : UnaryMetaFunctionClass::template apply&lt;Arg&gt;
  106. {};
  107. </pre>
  108. <p>Now <tt class="literal"><span class="pre">twice</span></tt> is just:</p>
  109. <pre class="literal-block">
  110. template &lt;class F, class X&gt;
  111. struct twice
  112. : apply1&lt;F, typename apply1&lt;F,X&gt;::type&gt;
  113. {};
  114. </pre>
  115. <p>To see <tt class="literal"><span class="pre">twice</span></tt> at work, we can apply it to a little metafunction
  116. class built around the <tt class="literal"><span class="pre">add_pointer</span></tt> metafunction:</p>
  117. <pre class="literal-block">
  118. struct add_pointer_f
  119. {
  120. template &lt;class T&gt;
  121. struct apply : boost::add_pointer&lt;T&gt; {};
  122. };
  123. </pre>
  124. <!-- @litre_translator.line_offset -= 7 -->
  125. <p>Now we can use <tt class="literal"><span class="pre">twice</span></tt> with <tt class="literal"><span class="pre">add_pointer_f</span></tt> to build
  126. pointers-to-pointers:</p>
  127. <pre class="literal-block">
  128. BOOST_STATIC_ASSERT((
  129. boost::is_same&lt;
  130. twice&lt;add_pointer_f, int&gt;::type
  131. , int**
  132. &gt;::value
  133. ));
  134. </pre>
  135. <!-- @ apply1 = stack[-4]
  136. add_pointer_f = stack[-2]
  137. compile('all', pop = 0) -->
  138. </div>
  139. <div class="footer-separator"></div>
  140. <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./implementing-division.html" class="navigation-link">Prev</a>&nbsp;<a href="./handling-placeholders.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Back</a>&nbsp;<a href="./handling-placeholders.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>
  141. </tr></table></body>
  142. </html>