details.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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: Details</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="./the-importance-of-being.html" class="navigation-link">Prev</a>&nbsp;<a href="./exercises.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./lambda-details.html" class="navigation-link">Back</a>&nbsp;<a href="./exercises.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="./details.html" class="navigation-link">Details</a></td>
  16. </tr></table><div class="header-separator"></div>
  17. <div class="section" id="details">
  18. <h1><a class="toc-backref" href="./tutorial-metafunctions.html#id59" name="details">Details</a></h1>
  19. <p>By now you should have a fairly complete view of the fundamental
  20. concepts and language of both template metaprogramming in general
  21. and of the Boost Metaprogramming Library. This section
  22. reviews the highlights.</p>
  23. <dl>
  24. <dt>Metafunction forwarding.</dt>
  25. <dd>The technique of using public derivation to
  26. supply the nested <tt class="literal"><span class="pre">type</span></tt> of a metafunction by accessing the one
  27. provided by its base class.</dd>
  28. <dt>Metafunction class.</dt>
  29. <dd>The most basic way to formulate a compile-time
  30. function so that it can be treated as polymorphic metadata; that
  31. is, as a type. A metafunction class is a class with a nested
  32. metafunction called <tt class="literal"><span class="pre">apply</span></tt>.</dd>
  33. <dt>MPL.</dt>
  34. <dd><p class="first">Most of this book's examples will use the Boost
  35. Metaprogramming Library. Like the Boost type traits headers,
  36. MPL
  37. headers follow a simple convention:</p>
  38. <pre class="literal-block">
  39. #include &lt;boost/mpl/<em>component-name</em>.hpp&gt;
  40. </pre>
  41. <p class="last">If the component's name ends in an underscore, however, the
  42. corresponding MPL header name does not include the trailing
  43. underscore. For example, <tt class="literal"><span class="pre">mpl::bool_</span></tt> can be found in
  44. <tt class="literal"><span class="pre">&lt;boost/mpl/bool.hpp&gt;</span></tt>. Where the library deviates from this
  45. convention, we'll be sure to point it out to you.</p>
  46. </dd>
  47. </dl>
  48. <!-- @ignore() -->
  49. <dl>
  50. <dt>Higher-order function.</dt>
  51. <dd>A function that operates on or returns a function. Making
  52. metafunctions polymorphic with other metadata is a key
  53. ingredient in higher-order metaprogramming.</dd>
  54. <dt>Lambda expression.</dt>
  55. <dd>Simply put, a lambda expression is callable metadata. Without
  56. some form of callable metadata, higher-order metafunctions
  57. would be impossible. Lambda expressions have two basic forms:
  58. <em>metafunction classes</em> and <em>placeholder expressions</em>.</dd>
  59. <dt>Placeholder expression.</dt>
  60. <dd><p class="first">A kind of lambda expression that, through the use of
  61. placeholders, enables in-place <em>partial metafunction
  62. application</em> and <em>metafunction composition</em>. As you will see
  63. throughout this book, these features give us the truly amazing
  64. ability to build up almost any kind of complex type computation
  65. from more primitive metafunctions, right at its point of use:</p>
  66. <pre class="literal-block">
  67. // find the position of a type x in some_sequence such that:
  68. // x is convertible to 'int'
  69. // &amp;&amp; x is not 'char'
  70. // &amp;&amp; x is not a floating type
  71. typedef mpl::find_if&lt;
  72. some_sequence
  73. , mpl::and_&lt;
  74. boost::is_convertible&lt;_1,int&gt;
  75. , mpl::not_&lt;boost::is_same&lt;_1,char&gt; &gt;
  76. , mpl::not_&lt;boost::is_float&lt;_1&gt; &gt;
  77. &gt;
  78. &gt;::type iter;
  79. </pre>
  80. <p class="last">Placeholder expressions make good on the promise of algorithm reuse
  81. without forcing us to write new metafunction classes. The
  82. corresponding capability is often sorely missed in the runtime
  83. world of the STL, since it is often much easier to write a loop
  84. by hand than it is to use standard algorithms, despite their
  85. correctness and efficiency advantages.</p>
  86. </dd>
  87. </dl>
  88. <!-- @ example.prepend('''
  89. #include <boost/mpl/and.hpp>
  90. #include <boost/mpl/not.hpp>
  91. #include <boost/mpl/find_if.hpp>
  92. #include <boost/type_traits/is_convertible.hpp>
  93. #include <boost/type_traits/is_float.hpp>
  94. typedef mpl::vector<char, double, short, long> some_sequence;
  95. ''')
  96. compile() -->
  97. <dl>
  98. <dt>The <tt class="literal"><span class="pre">lambda</span></tt> metafunction.</dt>
  99. <dd>A metafunction that transforms a lambda expression into a
  100. corresponding metafunction class. For detailed information on
  101. <tt class="literal"><span class="pre">lambda</span></tt> and the lambda evaluation process,
  102. please see the <a class="reference" href="./reference-manual.html">the MPL reference manual</a>.</dd>
  103. <dt>The <tt class="literal"><span class="pre">apply</span></tt> metafunction.</dt>
  104. <dd>A metafunction that invokes its first argument, which must be a
  105. lambda expression, on its remaining arguments. In general, to
  106. invoke a lambda expression, you should always pass it to
  107. <tt class="literal"><span class="pre">mpl::apply</span></tt> along with the arguments you want to apply it
  108. to in lieu of using <tt class="literal"><span class="pre">lambda</span></tt> and invoking the result &quot;manually.&quot;</dd>
  109. <dt>Lazy evaluation.</dt>
  110. <dd>A strategy of delaying evaluation until a result is
  111. required, thereby avoiding any unnecessary computation and any
  112. associated unnecessary errors. Metafunctions are only invoked
  113. when we access their nested <tt class="literal"><span class="pre">::type</span></tt>s, so we can supply all
  114. of their arguments without performing any computation and
  115. delay evaluation to the last possible moment.</dd>
  116. </dl>
  117. </div>
  118. <div class="footer-separator"></div>
  119. <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./the-importance-of-being.html" class="navigation-link">Prev</a>&nbsp;<a href="./exercises.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./lambda-details.html" class="navigation-link">Back</a>&nbsp;<a href="./exercises.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>
  120. </tr></table></body>
  121. </html>