placeholder-expression.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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: Placeholder Expression Definition</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="./placeholders.html" class="navigation-link">Prev</a>&nbsp;<a href="./lambda-and-non.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./placeholders.html" class="navigation-link">Back</a>&nbsp;<a href="./lambda-and-non.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./lambda-details.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="./lambda-details.html" class="navigation-link">Lambda Details</a> / <a href="./placeholder-expression.html" class="navigation-link">Placeholder Expression Definition</a></td>
  16. </tr></table><div class="header-separator"></div>
  17. <div class="section" id="placeholder-expression">
  18. <h1><a class="toc-backref" href="./lambda-details.html#id56" name="placeholder-expression">Placeholder Expression Definition</a></h1>
  19. <p>Now that you know just what <em>placeholder</em> means, we can define
  20. <em>placeholder expression</em>:</p>
  21. <div class="admonition-definition admonition">
  22. <p class="admonition-title first">Definition</p>
  23. <p>A placeholder expression is either:</p>
  24. <blockquote>
  25. <blockquote>
  26. <ul class="simple">
  27. <li>a placeholder</li>
  28. </ul>
  29. </blockquote>
  30. <p><em>or</em></p>
  31. <blockquote>
  32. <ul class="simple">
  33. <li>a template specialization with at least one argument that
  34. is a placeholder expression.</li>
  35. </ul>
  36. </blockquote>
  37. </blockquote>
  38. </div>
  39. <p>In other words, a placeholder expression always involves a
  40. placeholder.</p>
  41. <!-- DWA: I'm still not sure we shouldn't be at least mentioning the
  42. pitfall, but for now it's commented out.
  43. Lambda and Nullary Metafunctions
  44. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  45. The definition of *placeholder expression* above has an interesting
  46. implication: an ordinary nullary metafunction is never a placeholder
  47. expression. In other words, even though ``add_pointer<int>`` is a
  48. nullary metafunction, it won't be invoked in the expression below;
  49. the assertion will always fail:
  50. .. parsed-literal::
  51. BOOST_STATIC_ASSERT((
  52. mpl::apply<
  53. boost::is_same<**boost::add_pointer<int>**,_1>
  54. , int\*
  55. >::type::value
  56. ));
  57. In order to allow a nullary metafunction to be used as a lambda
  58. expression, MPL provides this definition of ``arg``:
  59. .. parsed-literal::
  60. // primary template definition (not a specialization)
  61. template <class F>
  62. struct arg
  63. {
  64. template <class A1 = void\_, class A2 = void\_, ... class *Am* = void\_>
  65. struct apply : F
  66. {
  67. };
  68. };
  69. When applied to a lambda expression's actual arguments, ``arg<F>``
  70. ignores them and simply returns ``F::type``. In other words, if
  71. ``F`` is a nullary metafunction, ``arg<F>`` is a metafunction class
  72. that invokes ``F`` and returns the result. So we can transform
  73. add_pointer<int> into a placeholder and get the desired result
  74. with:
  75. .. parsed-literal::
  76. BOOST_STATIC_ASSERT((
  77. mpl::apply<
  78. boost::is_same<
  79. **mpl::arg<boost::add_pointer<int> >**
  80. , _1
  81. >
  82. , int\*
  83. >::type::value
  84. )); -->
  85. </div>
  86. <div class="footer-separator"></div>
  87. <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./placeholders.html" class="navigation-link">Prev</a>&nbsp;<a href="./lambda-and-non.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./placeholders.html" class="navigation-link">Back</a>&nbsp;<a href="./lambda-and-non.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./lambda-details.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>
  88. </tr></table></body>
  89. </html>