tti_detail_has_template.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Introspecting an inner class template</title>
  5. <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;The Type Traits Introspection Library">
  8. <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;The Type Traits Introspection Library">
  9. <link rel="prev" href="tti_detail_has_type.html" title="Introspecting an inner type">
  10. <link rel="next" href="tti_detail_has_template/tti_detail_has_template_metafunction.html" title="Using the has_template_(xxx) metafunction">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="tti_detail_has_type.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tti_detail_has_template/tti_detail_has_template_metafunction.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  27. <a name="the_type_traits_introspection_library.tti_detail_has_template"></a><a class="link" href="tti_detail_has_template.html" title="Introspecting an inner class template">Introspecting
  28. an inner class template</a>
  29. </h2></div></div></div>
  30. <div class="toc"><dl class="toc">
  31. <dt><span class="section"><a href="tti_detail_has_template.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro">Using
  32. the BOOST_TTI_HAS_TEMPLATE macro</a></span></dt>
  33. <dt><span class="section"><a href="tti_detail_has_template/tti_detail_has_template_metafunction.html">Using
  34. the has_template_(xxx) metafunction</a></span></dt>
  35. </dl></div>
  36. <div class="section">
  37. <div class="titlepage"><div><div><h3 class="title">
  38. <a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro"></a><a class="link" href="tti_detail_has_template.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro" title="Using the BOOST_TTI_HAS_TEMPLATE macro">Using
  39. the BOOST_TTI_HAS_TEMPLATE macro</a>
  40. </h3></div></div></div>
  41. <p>
  42. The TTI macro <code class="computeroutput"><a class="link" href="../BOOST_TTI_HAS_TEMPLATE.html" title="Macro BOOST_TTI_HAS_TEMPLATE">BOOST_TTI_HAS_TEMPLATE</a></code>
  43. introspects an inner class template of a class. The macro must specify, at
  44. the least, the name of the class template to introspect.
  45. </p>
  46. <h5>
  47. <a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.h0"></a>
  48. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.two_forms_of_introspection"></a></span><a class="link" href="tti_detail_has_template.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.two_forms_of_introspection">Two
  49. forms of introspection</a>
  50. </h5>
  51. <p>
  52. There are two general forms of template introspection which can be used.
  53. The first is to find a class template with any number of only template type
  54. parameters ( template parameters starting with <code class="computeroutput"><span class="keyword">class</span></code>
  55. or <code class="computeroutput"><span class="keyword">typename</span></code> ). In this form
  56. only the name of the class template needs to be specified when invoking the
  57. macro. We will call this form of the macro the <code class="computeroutput"><span class="keyword">template</span>
  58. <span class="identifier">type</span> <span class="identifier">parameters</span></code>
  59. form. An example of a class template of this form which could be successfully
  60. introspected would be:
  61. </p>
  62. <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">X</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">Y</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">Z</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">AClassTemplate</span> <span class="special">{</span> <span class="comment">/* etc. */</span> <span class="special">};</span>
  63. </pre>
  64. <p>
  65. The second is to find a class template with specific template parameters.
  66. In this form both the name of the class template and the template parameters
  67. are passed to the macro.
  68. </p>
  69. <p>
  70. We will call this form of the macro the <code class="computeroutput"><span class="identifier">specific</span>
  71. <span class="identifier">parameters</span></code> form. An example of
  72. a class template of this form which could be successfully introspected would
  73. be:
  74. </p>
  75. <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">X</span><span class="special">,</span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">Y</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">Z</span><span class="special">&gt;</span> <span class="identifier">BClassTemplate</span> <span class="special">{</span> <span class="comment">/* etc. */</span> <span class="special">};</span>
  76. </pre>
  77. <p>
  78. When using the specific form of the macro, there are two things which need
  79. to be understood when passing the template parameters to the macro. First,
  80. the actual names of the template parameters passed are irrelevant. They can
  81. be left out completely or be different from the names in the nested class
  82. template itself. Second, the use of 'typename' or 'class', when referring
  83. to a template type parameter, is completely interchangeable, as it is in
  84. the actual class template itself.
  85. </p>
  86. <h5>
  87. <a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.h1"></a>
  88. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.variadic_and_non_variadic_macro_usage"></a></span><a class="link" href="tti_detail_has_template.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.variadic_and_non_variadic_macro_usage">Variadic
  89. and non-variadic macro usage</a>
  90. </h5>
  91. <p>
  92. When using the BOOST_TTI_HAS_TEMPLATE macro we distinguish between compilers
  93. supporting variadic macros or not supporting variadic macros.
  94. </p>
  95. <p>
  96. The programmer can always tell whether or not the compiler supports variadic
  97. macros by checking the value of the macro BOOST_PP_VARIADIC after including
  98. the necessary header file <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">tti</span><span class="special">/</span><span class="identifier">has_template</span><span class="special">.</span><span class="identifier">hpp</span></code> in order to use the BOOST_TTI_TEMPLATE
  99. macro. A value of 1 indicates the compiler supports variadic macros while
  100. a value of 0 indicates the compiler does not support variadic macros.
  101. </p>
  102. <p>
  103. Modern C++ compilers, in supporting the latest C++11 standard, normally support
  104. variadic macros. Even before the latest C++11 standard a number of C++ compilers
  105. already supported variadic macros. If you feel your compiler supports variadic
  106. macros and BOOST_PP_VARIADIC is 0 even after including <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">tti</span><span class="special">/</span><span class="identifier">has_template</span><span class="special">.</span><span class="identifier">hpp</span></code>, you can predefine BOOST_PP_VARIADIC
  107. to 1 before including <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">tti</span><span class="special">/</span><span class="identifier">has_template</span><span class="special">.</span><span class="identifier">hpp</span></code>.
  108. </p>
  109. <h5>
  110. <a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.h2"></a>
  111. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.non_variadic_macro_usage"></a></span><a class="link" href="tti_detail_has_template.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.non_variadic_macro_usage">Non-variadic
  112. macro usage</a>
  113. </h5>
  114. <p>
  115. We start with syntax for compilers not supporting variadic macros since this
  116. syntax can also be used by compilers which do support variadic macros. The
  117. form for non-variadic macros always takes two macro parameters. The first
  118. macro parameter is always the name of the class template you are trying to
  119. introspect.
  120. </p>
  121. <p>
  122. The second macro parameter, when using the <code class="computeroutput"><span class="identifier">specific</span>
  123. <span class="identifier">parameters</span></code> form of the macro, is
  124. the template parameters in the form of a Boost preprocessor library array
  125. data type. When using the <code class="computeroutput"><span class="keyword">template</span>
  126. <span class="identifier">type</span> <span class="identifier">parameters</span></code>
  127. form of the macro the second macro parameter is BOOST_PP_NIL. If the second
  128. parameter is neither a Boost preprocessor library array data type or BOOS_PP_NIL
  129. you will get a compiler error if your compiler only supports non-variadic
  130. macros.
  131. </p>
  132. <p>
  133. The non-variadic macro form for introspecting the class templates above using
  134. the <code class="computeroutput"><span class="keyword">template</span> <span class="identifier">type</span>
  135. <span class="identifier">parameters</span></code> form would be:
  136. </p>
  137. <pre class="programlisting"><span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">AClassTemplate</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">)</span>
  138. <span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">BClassTemplate</span><span class="special">,</span><span class="identifier">BOOST_PP_NIL</span><span class="special">)</span>
  139. </pre>
  140. <p>
  141. Invoking the metafunction in the second case would always fail since the
  142. BClassTemplate does not have all template type parameters.
  143. </p>
  144. <p>
  145. The non-variadic macro form for introspecting the class templates above using
  146. the <code class="computeroutput"><span class="identifier">specific</span> <span class="identifier">parameters</span></code>
  147. form would be:
  148. </p>
  149. <pre class="programlisting"><span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">AClassTemplate</span><span class="special">,(</span><span class="number">4</span><span class="special">,(</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">typename</span><span class="special">,</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">typename</span><span class="special">)))</span>
  150. <span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">BClassTemplate</span><span class="special">,(</span><span class="number">3</span><span class="special">,(</span><span class="keyword">class</span><span class="special">,</span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">class</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)))</span>
  151. </pre>
  152. <p>
  153. You need to be careful using the non-variadic <code class="computeroutput"><span class="identifier">specific</span>
  154. <span class="identifier">parameters</span></code> form to specify the
  155. correct number of array parameters. This can sometimes be tricky if you have
  156. a template template parameter, or a non-type template parameter which has
  157. parentheses surrounding part of the type specification. In the latter case,
  158. when parentheses surround a comma ( ',' ), do not count that as creating
  159. another Boost PP array token. Two examples:
  160. </p>
  161. <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">void</span> <span class="special">(*</span><span class="identifier">FunctionPointer</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">long</span><span class="special">)&gt;</span> <span class="keyword">class</span> <span class="identifier">CClassTemplate</span> <span class="special">{</span> <span class="comment">/* etc. */</span> <span class="special">};</span>
  162. <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">DClassTemplate</span> <span class="special">{</span> <span class="comment">/* etc. */</span> <span class="special">};</span>
  163. <span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">CClassTemplate</span><span class="special">,(</span><span class="number">1</span><span class="special">,(</span><span class="keyword">void</span> <span class="special">(*)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">long</span><span class="special">))))</span>
  164. <span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">DClassTemplate</span><span class="special">,(</span><span class="number">2</span><span class="special">,(</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">class</span><span class="special">)))</span>
  165. </pre>
  166. <p>
  167. In the case of using the macro to introspect CClassTemplate the number of
  168. Boost PP array parameters is 1, even though there is a comma separating the
  169. tokens in <code class="computeroutput"><span class="keyword">void</span> <span class="special">(*</span><span class="identifier">FunctionPointer</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">long</span><span class="special">)</span></code>. This is because the comma is within parentheses.
  170. </p>
  171. <p>
  172. In the case of using the macro to introspect DClassTemplate the number of
  173. Boost PP array parameters is 2, because there is a comma separating the tokens
  174. in <code class="computeroutput"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">T</span></code>.
  175. </p>
  176. <h5>
  177. <a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.h3"></a>
  178. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.variadic_macro_usage"></a></span><a class="link" href="tti_detail_has_template.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.variadic_macro_usage">Variadic
  179. macro usage</a>
  180. </h5>
  181. <p>
  182. Having the ability to use variadic macros makes the syntax for using BOOST_TTI_TEMPLATE
  183. easier to specify in both the <code class="computeroutput"><span class="keyword">template</span>
  184. <span class="identifier">type</span> <span class="identifier">parameters</span></code>
  185. form and the <code class="computeroutput"><span class="identifier">specific</span> <span class="identifier">parameters</span></code>
  186. form of using the macro. This is because variadic macros can take a variable
  187. number of parameters. When using the variadic macro form the first macro
  188. parameter is always the name of the class template you are trying to introspect.
  189. You only specify further parameters when using the <code class="computeroutput"><span class="identifier">specific</span>
  190. <span class="identifier">parameters</span></code> form of the macro, in
  191. which case the further parameters to the macro are the specific template
  192. parameters.
  193. </p>
  194. <p>
  195. Introspecting the first class template above using the <code class="computeroutput"><span class="keyword">template</span>
  196. <span class="identifier">type</span> <span class="identifier">parameters</span></code>
  197. form the variadic macro would be:
  198. </p>
  199. <pre class="programlisting"><span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">AClassTemplate</span><span class="special">)</span>
  200. </pre>
  201. <p>
  202. Introspecting the other class templates above using the <code class="computeroutput"><span class="identifier">specific</span>
  203. <span class="identifier">parameters</span></code> form the variadic macros
  204. would be:
  205. </p>
  206. <pre class="programlisting"><span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">BClassTemplate</span><span class="special">,</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">class</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)</span>
  207. <span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">CClassTemplate</span><span class="special">,</span><span class="keyword">void</span> <span class="special">(*)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">long</span><span class="special">))</span>
  208. <span class="identifier">BOOST_TTI_TEMPLATE</span><span class="special">(</span><span class="identifier">DClassTemplate</span><span class="special">,</span><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">,</span><span class="keyword">class</span><span class="special">&gt;</span> <span class="keyword">class</span><span class="special">)</span>
  209. </pre>
  210. <p>
  211. Here we have no problem with counting the number of tuple tokens for the
  212. Boost PP array, nor do we have to specify BOOST_PP_NIL if we are using the
  213. <code class="computeroutput"><span class="keyword">template</span> <span class="identifier">type</span>
  214. <span class="identifier">parameters</span></code> form. Also for the specific
  215. parameters form we simply use the template parameters as the remaining tokens
  216. of the variadic macro.
  217. </p>
  218. <h5>
  219. <a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.h4"></a>
  220. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.the_resulting_metafunction"></a></span><a class="link" href="tti_detail_has_template.html#the_type_traits_introspection_library.tti_detail_has_template.tti_detail_has_template_macro.the_resulting_metafunction">The
  221. resulting metafunction</a>
  222. </h5>
  223. <p>
  224. Using either form of the macro, whether using variadic or non-variadic syntax,
  225. the macro generates a metafunction called "has_template_'name_of_inner_class_template'".
  226. </p>
  227. <p>
  228. The metafunction can be invoked by passing it the enclosing type to introspect.
  229. </p>
  230. <p>
  231. The metafunction returns a single type called 'type', which is a boost::mpl::bool_.
  232. As a convenience the metafunction returns the value of this type directly
  233. as a compile time bool constant called 'value'. This is true or false depending
  234. on whether the inner class template exists or not.
  235. </p>
  236. </div>
  237. </div>
  238. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  239. <td align="left"></td>
  240. <td align="right"><div class="copyright-footer">Copyright &#169; 2011-2013 Tropic Software
  241. East Inc<p>
  242. Distributed under the Boost Software License, Version 1.0. (See accompanying
  243. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  244. </p>
  245. </div></td>
  246. </tr></table>
  247. <hr>
  248. <div class="spirit-nav">
  249. <a accesskey="p" href="tti_detail_has_type.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tti_detail_has_template/tti_detail_has_template_metafunction.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  250. </div>
  251. </body>
  252. </html>