tag-dispatched-metafunction.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.7: http://docutils.sourceforge.net/" />
  7. <title>The MPL Reference Manual: Tag Dispatched Metafunction</title>
  8. <link rel="stylesheet" href="../style.css" type="text/css" />
  9. </head>
  10. <body class="docframe refmanual">
  11. <table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./placeholder-expression.html" class="navigation-link">Prev</a>&nbsp;<a href="./numeric-metafunction.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./placeholder-expression.html" class="navigation-link">Back</a>&nbsp;<a href="./numeric-metafunction.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./metafunctions-concepts.html" class="navigation-link">Up</a>&nbsp;<a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td>
  12. <td class="header-group page-location"><a href="../refmanual.html" class="navigation-link">Front Page</a> / <a href="./metafunctions.html" class="navigation-link">Metafunctions</a> / <a href="./metafunctions-concepts.html" class="navigation-link">Concepts</a> / <a href="./tag-dispatched-metafunction.html" class="navigation-link">Tag Dispatched Metafunction</a></td>
  13. </tr></table><div class="header-separator"></div>
  14. <div class="section" id="tag-dispatched-metafunction">
  15. <h1><a class="toc-backref" href="./metafunctions-concepts.html#id1516">Tag Dispatched Metafunction</a></h1>
  16. <div class="section" id="id840">
  17. <h3><a class="subsection-title" href="#summary" name="summary">Summary</a></h3>
  18. <p>A <a class="reference internal" href="./tag-dispatched-metafunction.html">Tag Dispatched Metafunction</a> is a <a class="reference internal" href="./metafunction.html">Metafunction</a> that employs a
  19. <em>tag dispatching</em> technique in its implementation to build an
  20. infrastructure for easy overriding/extenstion of the metafunction's
  21. behavior.</p>
  22. </div>
  23. <div class="section" id="notation">
  24. <h3>Notation</h3>
  25. <table border="1" class="docutils table" id="tag-metafunction">
  26. <colgroup>
  27. <col width="31%" />
  28. <col width="69%" />
  29. </colgroup>
  30. <thead valign="bottom">
  31. <tr><th class="head">Symbol</th>
  32. <th class="head">Legend</th>
  33. </tr>
  34. </thead>
  35. <tbody valign="top">
  36. <tr><td><em>name</em></td>
  37. <td>A placeholder token for the specific metafunction's name.</td>
  38. </tr>
  39. <tr><td><em>tag-metafunction</em></td>
  40. <td>A placeholder token for the tag metafunction's name.</td>
  41. </tr>
  42. <tr><td><em>tag</em></td>
  43. <td>A placeholder token for one of possible tag types
  44. returned by the tag metafunction.</td>
  45. </tr>
  46. </tbody>
  47. </table>
  48. </div>
  49. <div class="section" id="id841">
  50. <h3><a class="subsection-title" href="#synopsis" name="synopsis">Synopsis</a></h3>
  51. <pre class="literal-block">
  52. template&lt; typename Tag &gt; struct <em>name</em>_impl;
  53. template&lt;
  54. typename X
  55. <em>[, ...]</em>
  56. &gt;
  57. struct <em>name</em>
  58. : <em>name</em>_impl&lt; typename <em>tag-<a href="./metafunction.html" class="identifier">metafunction</a></em>&lt;X&gt;::type &gt;
  59. ::template <a href="./apply.html" class="identifier">apply</a>&lt;X <em>[, ...]</em>&gt;
  60. {
  61. };
  62. template&lt; typename Tag &gt; struct <em>name</em>_impl
  63. {
  64. template&lt; typename X <em>[, ...]</em> &gt; struct <a href="./apply.html" class="identifier">apply</a>
  65. {
  66. // <em>default implementation</em>
  67. };
  68. };
  69. template&lt;&gt; struct <em>name</em>_impl&lt;<em>tag</em>&gt;
  70. {
  71. template&lt; typename X <em>[, ...]</em> &gt; struct <a href="./apply.html" class="identifier">apply</a>
  72. {
  73. // <em>tag-specific implementation</em>
  74. };
  75. };
  76. </pre>
  77. </div>
  78. <div class="section" id="id842">
  79. <h3><a class="subsection-title" href="#description" name="description">Description</a></h3>
  80. <p>The usual mechanism for overriding a metafunction's behavior is class
  81. template specialization — given a library-defined metafunction <tt class="literal"><span class="pre">f</span></tt>,
  82. it's possible to write a specialization of <tt class="literal"><span class="pre">f</span></tt> for a specific type
  83. <tt class="literal"><span class="pre">user_type</span></tt> that would have the required semantics <a class="footnote-reference" href="#spec" id="id843">[4]</a>.</p>
  84. <p>While this mechanism is always available, it's not always the most
  85. convenient one, especially if it is desirable to specialize a
  86. metafunction's behavior for a <em>family</em> of related types. A typical
  87. example of it is numbered forms of sequence classes in MPL itself
  88. (<tt class="literal"><span class="pre">list0</span></tt>, ..., <tt class="literal"><span class="pre">list50</span></tt>, et al.), and sequence classes in general.</p>
  89. <p>A <a class="reference internal" href="./tag-dispatched-metafunction.html">Tag Dispatched Metafunction</a> is a concept name for an instance of
  90. the metafunction implementation infrastructure being employed by the
  91. library to make it easier for users and implementors to override the
  92. behavior of library's metafunctions operating on families of specific
  93. types.</p>
  94. <p>The infrastructure is built on a variation of the technique commonly
  95. known as <em>tag dispatching</em> (hence the concept name),
  96. and involves three entities: a metafunction itself, an associated
  97. tag-producing <a class="reference internal" href="./tag-dispatched-metafunction.html#tag-metafunction">tag metafunctions</a>, and the metafunction's
  98. implementation, in the form of a <a class="reference internal" href="./metafunction-class.html">Metafunction Class</a> template
  99. parametrized by a <tt class="literal"><span class="pre">Tag</span></tt> type parameter. The metafunction redirects
  100. to its implementation class template by invoking its specialization
  101. on a tag type produced by the tag metafunction with the original
  102. metafunction's parameters.</p>
  103. <table class="docutils footnote" frame="void" id="spec" rules="none">
  104. <colgroup><col class="label" /><col /></colgroup>
  105. <tbody valign="top">
  106. <tr><td class="label"><a class="fn-backref" href="#id843">[4]</a></td><td>Usually such user-defined specialization is still required
  107. to preserve the <tt class="literal"><span class="pre">f</span></tt>'s original invariants and complexity requirements.</td></tr>
  108. </tbody>
  109. </table>
  110. </div>
  111. <div class="section" id="id844">
  112. <h3><a class="subsection-title" href="#example" name="example">Example</a></h3>
  113. <pre class="literal-block">
  114. #include &lt;<a href="../../../../boost/mpl/size.hpp" class="header">boost/mpl/size.hpp</a>&gt;
  115. namespace user {
  116. struct bitset_tag;
  117. struct bitset0
  118. {
  119. typedef bitset_tag tag;
  120. // ...
  121. };
  122. template< typename B0 > struct bitset1
  123. {
  124. typedef bitset_tag tag;
  125. // ...
  126. };
  127. template< typename B0, <em>...,</em> typename B<em>n</em> &gt; struct bitset<em>n</em>
  128. {
  129. typedef bitset_tag tag;
  130. // ...
  131. };
  132. } // namespace user
  133. namespace boost { namespace mpl {
  134. template&lt;&gt; struct size_impl&lt;user::bitset_tag&gt;
  135. {
  136. template&lt; typename Bitset &gt; struct <a href="./apply.html" class="identifier">apply</a>
  137. {
  138. typedef typename Bitset::<a href="./size.html" class="identifier">size</a> type;
  139. };
  140. };
  141. }}
  142. </pre>
  143. </div>
  144. <div class="section" id="id845">
  145. <h3><a class="subsection-title" href="#models" name="models">Models</a></h3>
  146. <ul class="simple">
  147. <li><a class="reference internal" href="./sequence-tag.html">sequence_tag</a></li>
  148. </ul>
  149. </div>
  150. <div class="section" id="id846">
  151. <h3><a class="subsection-title" href="#see-also" name="see-also">See also</a></h3>
  152. <p><a class="reference internal" href="./metafunction.html">Metafunction</a>, <a class="reference internal" href="./metafunction-class.html">Metafunction Class</a>, <a class="reference internal" href="./numeric-metafunction.html">Numeric Metafunction</a></p>
  153. <!-- Metafunctions/Concepts//Numeric Metafunction |60 -->
  154. </div>
  155. </div>
  156. <div class="footer-separator"></div>
  157. <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./placeholder-expression.html" class="navigation-link">Prev</a>&nbsp;<a href="./numeric-metafunction.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./placeholder-expression.html" class="navigation-link">Back</a>&nbsp;<a href="./numeric-metafunction.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./metafunctions-concepts.html" class="navigation-link">Up</a>&nbsp;<a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td>
  158. <td><div class="copyright-footer"><div class="copyright">Copyright © 2001-2009 Aleksey Gurtovoy and David Abrahams</div>
  159. Distributed under the Boost Software License, Version 1.0. (See accompanying
  160. file LICENSE_1_0.txt or copy at <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)</div></td></tr></table></body>
  161. </html>