tti_detail_has_type.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Introspecting an inner type</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.html" title="Macro Metafunctions">
  10. <link rel="next" href="tti_detail_has_template.html" title="Introspecting an inner class template">
  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.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.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_type"></a><a class="link" href="tti_detail_has_type.html" title="Introspecting an inner type">Introspecting
  28. an inner type</a>
  29. </h2></div></div></div>
  30. <p>
  31. The TTI macro <code class="computeroutput"><a class="link" href="../BOOST_TTI_HAS_TYPE.html" title="Macro BOOST_TTI_HAS_TYPE">BOOST_TTI_HAS_TYPE</a></code>
  32. introspects a nested type of a class.
  33. </p>
  34. <p>
  35. The BOOST_TTI_HAS_TYPE macro takes a single parameter which is the name of
  36. an inner type whose existence the programmer wants to check. The macro generates
  37. a metafunction called "has_type_'name_of_inner_type'".
  38. </p>
  39. <p>
  40. The main purpose of the generated metafunction is to check for the existence
  41. by name of the inner type. The metafunction can also be used to invoke an MPL
  42. lambda expression which is passed the inner type. One of the most common usages
  43. of the added functionality is to check whether or not the inner type is a typedef
  44. for another type.
  45. </p>
  46. <p>
  47. The metafunction is invoked by passing it the enclosing type to introspect.
  48. A second type may be passed to the metafunction, an MPL lambda expression taking
  49. the inner type and returning a boolean constant.
  50. </p>
  51. <p>
  52. The metafunction returns a single type called 'type', which is a boost::mpl::bool_.
  53. As a convenience the metafunction returns the value of this type directly as
  54. a compile time bool constant called 'value'. This value is true or false depending
  55. on whether the inner type exists or not.
  56. </p>
  57. <p>
  58. If a second optional type is passed, this type must be an MPL lambda expression
  59. and the expression will be invoked only if the inner type exists. In that case
  60. the metafunction returns true or false depending on whether the lambda expression
  61. returns true or false. If the inner type does not exist, the lambda expression,
  62. even if specified, is never invoked and the metafunction returns false.
  63. </p>
  64. <h4>
  65. <a name="the_type_traits_introspection_library.tti_detail_has_type.h0"></a>
  66. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_type.generating_the_metafunction"></a></span><a class="link" href="tti_detail_has_type.html#the_type_traits_introspection_library.tti_detail_has_type.generating_the_metafunction">Generating
  67. the metafunction</a>
  68. </h4>
  69. <p>
  70. You generate the metafunction by invoking the macro with the name of an inner
  71. type:
  72. </p>
  73. <pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_TYPE</span><span class="special">(</span><span class="identifier">AType</span><span class="special">)</span>
  74. </pre>
  75. <p>
  76. generates a metafunction called 'has_type_AType' in the current scope.
  77. </p>
  78. <h4>
  79. <a name="the_type_traits_introspection_library.tti_detail_has_type.h1"></a>
  80. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_type.invoking_the_metafunction"></a></span><a class="link" href="tti_detail_has_type.html#the_type_traits_introspection_library.tti_detail_has_type.invoking_the_metafunction">Invoking
  81. the metafunction</a>
  82. </h4>
  83. <p>
  84. You invoke the metafunction by instantiating the template with an enclosing
  85. type to introspect and, optionally, an MPL lambda expression. A return value
  86. called 'value' is a compile time bool constant.
  87. </p>
  88. <pre class="programlisting"><span class="identifier">has_type_AType</span><span class="special">&lt;</span><span class="identifier">Enclosing_Type</span><span class="special">&gt;::</span><span class="identifier">value</span>
  89. <span class="identifier">has_type_AType</span><span class="special">&lt;</span><span class="identifier">Enclosing_Type</span><span class="special">,</span><span class="identifier">ALambdaExpression</span><span class="special">&gt;::</span><span class="identifier">value</span>
  90. </pre>
  91. <h4>
  92. <a name="the_type_traits_introspection_library.tti_detail_has_type.h2"></a>
  93. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_type.examples"></a></span><a class="link" href="tti_detail_has_type.html#the_type_traits_introspection_library.tti_detail_has_type.examples">Examples</a>
  94. </h4>
  95. <p>
  96. First we generate metafunctions for various inner type names:
  97. </p>
  98. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">tti</span><span class="special">/</span><span class="identifier">has_type</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  99. <span class="identifier">BOOST_TTI_HAS_TYPE</span><span class="special">(</span><span class="identifier">MyTypeDef</span><span class="special">)</span>
  100. <span class="identifier">BOOST_TTI_HAS_TYPE</span><span class="special">(</span><span class="identifier">AType</span><span class="special">)</span>
  101. <span class="identifier">BOOST_TTI_HAS_TYPE</span><span class="special">(</span><span class="identifier">ATypeDef</span><span class="special">)</span>
  102. <span class="identifier">BOOST_TTI_HAS_TYPE</span><span class="special">(</span><span class="identifier">MyType</span><span class="special">)</span>
  103. </pre>
  104. <p>
  105. Next let us create some user-defined types we want to introspect.
  106. </p>
  107. <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Top</span>
  108. <span class="special">{</span>
  109. <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">MyTypeDef</span><span class="special">;</span>
  110. <span class="keyword">struct</span> <span class="identifier">AType</span> <span class="special">{</span> <span class="special">};</span>
  111. <span class="special">};</span>
  112. <span class="keyword">struct</span> <span class="identifier">Top2</span>
  113. <span class="special">{</span>
  114. <span class="keyword">typedef</span> <span class="keyword">long</span> <span class="identifier">ATypeDef</span><span class="special">;</span>
  115. <span class="keyword">struct</span> <span class="identifier">MyType</span> <span class="special">{</span> <span class="special">};</span>
  116. <span class="special">};</span>
  117. </pre>
  118. <p>
  119. Finally we invoke our metafunction and return our value.
  120. </p>
  121. <pre class="programlisting"><span class="identifier">has_type_MyTypeDef</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  122. <span class="identifier">has_type_MyTypeDef</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  123. <span class="identifier">has_type_AType</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  124. <span class="identifier">has_type_AType</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  125. <span class="identifier">has_type_ATypeDef</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  126. <span class="identifier">has_type_ATypeDef</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  127. <span class="identifier">has_type_MyType</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  128. <span class="identifier">has_type_MyType</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  129. </pre>
  130. <h4>
  131. <a name="the_type_traits_introspection_library.tti_detail_has_type.h3"></a>
  132. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_type.examples___using_lambda_expressions"></a></span><a class="link" href="tti_detail_has_type.html#the_type_traits_introspection_library.tti_detail_has_type.examples___using_lambda_expressions">Examples
  133. - using lambda expressions</a>
  134. </h4>
  135. <p>
  136. We can further invoke our metafunction with a second type, which is an MPL
  137. lambda expression.
  138. </p>
  139. <p>
  140. An MPL lambda expression, an extremely useful technique in template metaprogramming,
  141. allows us to pass a metafunction to other metafunctions. The metafunction we
  142. pass can be in the form of a placeholder expression or a metafunction class.
  143. In our case the metafunction passed to our has_type_'name_of_inner_type' metafunction
  144. as a lambda expression must return a boolean constant expression.
  145. </p>
  146. <h4>
  147. <a name="the_type_traits_introspection_library.tti_detail_has_type.h4"></a>
  148. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_type.example___using_a_lambda_expression_with_a_placeholder_expression"></a></span><a class="link" href="tti_detail_has_type.html#the_type_traits_introspection_library.tti_detail_has_type.example___using_a_lambda_expression_with_a_placeholder_expression">Example
  149. - using a lambda expression with a placeholder expression</a>
  150. </h4>
  151. <p>
  152. We will first illustrate the use of a lambda expression in the form of a placeholder
  153. expression being passed as the second template parameter to our has_type_'name_of_inner_type'
  154. metafunction. A popular and simple placeholder expression we can use is 'boost::is_same&lt;_1,SomeType&gt;'
  155. to check if the inner type found is a particular type. This is particularly
  156. useful when the inner type is a typedef for some other type.
  157. </p>
  158. <p>
  159. First we include some more header files and a using declaration for convenience.
  160. </p>
  161. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">placeholders</span><span class="special">.</span><span class="identifier">hpp</span>
  162. <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">is_same</span><span class="special">.</span><span class="identifier">hpp</span>
  163. <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">placeholders</span><span class="special">;</span>
  164. </pre>
  165. <p>
  166. Next we invoke our metafunction:
  167. </p>
  168. <pre class="programlisting"><span class="identifier">has_type_MyTypeDef</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_same</span><span class="special">&lt;</span><span class="identifier">_1</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  169. <span class="identifier">has_type_MyTypeDef</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_same</span><span class="special">&lt;</span><span class="identifier">_1</span><span class="special">,</span><span class="keyword">long</span><span class="special">&gt;</span> <span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  170. <span class="identifier">has_type_ATypeDef</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_same</span><span class="special">&lt;</span><span class="identifier">_1</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  171. <span class="identifier">has_type_ATypeDef</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_same</span><span class="special">&lt;</span><span class="identifier">_1</span><span class="special">,</span><span class="keyword">long</span><span class="special">&gt;</span> <span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  172. </pre>
  173. <h4>
  174. <a name="the_type_traits_introspection_library.tti_detail_has_type.h5"></a>
  175. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_type.example___using_a_lambda_expression_with_a_metafunction_class"></a></span><a class="link" href="tti_detail_has_type.html#the_type_traits_introspection_library.tti_detail_has_type.example___using_a_lambda_expression_with_a_metafunction_class">Example
  176. - using a lambda expression with a metafunction class</a>
  177. </h4>
  178. <p>
  179. We will next illustrate the use of a lambda expression in the form of a metafunction
  180. class being passed as the second template parameter to our has_type_'name_of_inner_type'
  181. metafunction.
  182. </p>
  183. <p>
  184. A metafunction class is a type which has a nested class template called 'apply'.
  185. For our metafunction class example we will check if the inner type is a built-in
  186. integer type. First let us write out metafunction class:
  187. </p>
  188. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_traits</span><span class="special">/</span><span class="identifier">is_integral</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  189. <span class="keyword">class</span> <span class="identifier">OurMetafunctionClass</span>
  190. <span class="special">{</span>
  191. <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">apply</span> <span class="special">:</span>
  192. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_integral</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span>
  193. <span class="special">{</span>
  194. <span class="special">};</span>
  195. <span class="special">};</span>
  196. </pre>
  197. <p>
  198. Now we can invoke our metafunction:
  199. </p>
  200. <pre class="programlisting"><span class="identifier">has_type_MyTypeDef</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="identifier">OurMetafunctionClass</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  201. <span class="identifier">has_type_AType</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="identifier">OurMetafunctionClass</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  202. <span class="identifier">has_type_ATypeDef</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="identifier">OurMetafunctionClass</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  203. <span class="identifier">has_type_MyType</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="identifier">OurMetafunctionClass</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  204. </pre>
  205. <h4>
  206. <a name="the_type_traits_introspection_library.tti_detail_has_type.h6"></a>
  207. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_type.metafunction_re_use"></a></span><a class="link" href="tti_detail_has_type.html#the_type_traits_introspection_library.tti_detail_has_type.metafunction_re_use">Metafunction
  208. re-use</a>
  209. </h4>
  210. <p>
  211. The macro encodes only the name of the inner type for which we are searching
  212. and the fact that we are introspecting for an inner type within an enclosing
  213. type.
  214. </p>
  215. <p>
  216. Because of this, once we create our metafunction for introspecting an inner
  217. type by name, we can reuse the metafunction for introspecting any enclosing
  218. type, having any inner type, for that name.
  219. </p>
  220. <p>
  221. Furthermore since we have only encoded the name of the inner type for which
  222. we are introspecting, we can not only introspect for that inner type by name
  223. but add different lambda expressions to inspect that inner type for whatever
  224. we want to find out about it using the same metafunction.
  225. </p>
  226. </div>
  227. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  228. <td align="left"></td>
  229. <td align="right"><div class="copyright-footer">Copyright &#169; 2011-2013 Tropic Software
  230. East Inc<p>
  231. Distributed under the Boost Software License, Version 1.0. (See accompanying
  232. 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>)
  233. </p>
  234. </div></td>
  235. </tr></table>
  236. <hr>
  237. <div class="spirit-nav">
  238. <a accesskey="p" href="tti_detail.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.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  239. </div>
  240. </body>
  241. </html>