tti_functionality.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>General Functionality</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_terminology.html" title="Terminology">
  10. <link rel="next" href="tti_detail.html" title="Macro Metafunctions">
  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_terminology.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.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_functionality"></a><a class="link" href="tti_functionality.html" title="General Functionality">General
  28. Functionality</a>
  29. </h2></div></div></div>
  30. <div class="toc"><dl class="toc"><dt><span class="section"><a href="tti_functionality.html#the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen">Macro
  31. metafunction name generation considerations</a></span></dt></dl></div>
  32. <p>
  33. The elements of a type about which a template metaprogrammer might be interested
  34. in finding out at compile time are:
  35. </p>
  36. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  37. <li class="listitem">
  38. Does it have a nested type with a particular name ?
  39. </li>
  40. <li class="listitem">
  41. Does it have a nested type with a particular name which fulfills some other
  42. possibility for that nested type.
  43. </li>
  44. <li class="listitem">
  45. Does it have a nested class template with a particular name ?
  46. </li>
  47. <li class="listitem">
  48. Does it have a nested class template with a particular name and a particular
  49. signature ?
  50. </li>
  51. <li class="listitem">
  52. Does it have a member function with a particular name and a particular
  53. signature ?
  54. </li>
  55. <li class="listitem">
  56. Does it have member data with a particular name and of a particular type
  57. ?
  58. </li>
  59. <li class="listitem">
  60. Does it have a static member function with a particular name and a particular
  61. signature ?
  62. </li>
  63. <li class="listitem">
  64. Does it have static member data with a particular name and of a particular
  65. type ?
  66. </li>
  67. <li class="listitem">
  68. Does it have either member data or static member data with a particular
  69. name and of a particular type ?
  70. </li>
  71. <li class="listitem">
  72. Does it have either a member function or static member function with a
  73. particular name and of a particular type ?
  74. </li>
  75. </ul></div>
  76. <p>
  77. These are some of the compile-time questions which the TTI library answers.
  78. It does this by creating metafunctions, which can be used at compile-time,
  79. using C++ macros. Each of the metafunctions created returns a compile time
  80. constant bool value which answers one of the above questions at compile time.
  81. When the particular element above exists the value is 'true', or more precisely
  82. boost::mpl::true_, while if the element does not exist the value is 'false',
  83. or more precisely boost::mpl::false_. In either case the type of this value
  84. is boost::mpl::bool_.
  85. </p>
  86. <p>
  87. This constant bool value, in the terminology of the Boost MPL library, is called
  88. an 'integral constant wrapper' and the metafunction generated is called a 'numerical
  89. metafunction'. The results from calling the metafunction can be passed to other
  90. metafunctions for type selection, the most popular of these being the boolean-valued
  91. operators in the Boost MPL library.
  92. </p>
  93. <p>
  94. All of the questions above attempt to find an answer about an inner element
  95. with a particular name. In order to do this using template metaprogramming,
  96. macros are used so that the name of the inner element can be passed to the
  97. macro. The macro will then generate an appropriate metafunction, which the
  98. template metaprogrammer can then use to introspect the information that is
  99. needed. The name itself of the inner element is always passed to the macro
  100. as a macro parameter, but other macro parameters may also be needed in some
  101. cases.
  102. </p>
  103. <p>
  104. All of the macros start with the prefix <code class="computeroutput"><span class="identifier">BOOST_TTI_</span></code>,
  105. create their metafunctions as class templates in whatever scope the user invokes
  106. the macro, and come in two forms:
  107. </p>
  108. <div class="orderedlist"><ol class="orderedlist" type="1">
  109. <li class="listitem">
  110. In the simplest macro form, which I call the simple macro form, the 'name'
  111. of the inner element is used directly to generate the name of the metafunction
  112. as well as serving as the 'name' to introspect. In generating the name
  113. of the metafunction from the macro name, the <code class="computeroutput"><span class="identifier">BOOST_TTI_</span></code>
  114. prefix is removed, the rest of the macro name is changed to lower case,
  115. and an underscore ( '_' ) followed by the 'name' is appended. As an example,
  116. for the macro <code class="computeroutput"><span class="identifier">BOOST_TTI_HAS_TYPE</span><span class="special">(</span><span class="identifier">MyType</span><span class="special">)</span></code> the name of the metafunction is <code class="computeroutput"><span class="identifier">has_type_MyType</span></code> and it will look for
  117. an inner type called 'MyType'.
  118. </li>
  119. <li class="listitem">
  120. In a more complicated macro form, which I call the complex macro form,
  121. the macro starts with <code class="computeroutput"><span class="identifier">BOOST_TTI_TRAIT_</span></code>
  122. and a 'trait' name is passed as the first parameter, with the 'name' of
  123. the inner element as the second parameter. The 'trait' name serves solely
  124. to completely name the metafunction in whatever scope the macro is invoked.
  125. As an example, for the macro <code class="computeroutput"><span class="identifier">BOOST_TTI_TRAIT_HAS_TYPE</span><span class="special">(</span><span class="identifier">MyTrait</span><span class="special">,</span><span class="identifier">MyType</span><span class="special">)</span></code> the name of the metafunction is <code class="computeroutput"><span class="identifier">MyTrait</span></code> and it will look for an inner
  126. type called <code class="computeroutput"><span class="identifier">MyType</span></code>.
  127. </li>
  128. </ol></div>
  129. <p>
  130. Every macro metafunction has a simple macro form and a corresponding complex
  131. macro form. Once either of these two macro forms are used for a particular
  132. type of inner element, the corresponding macro metafunction works exactly the
  133. same way and has the exact same functionality.
  134. </p>
  135. <p>
  136. In the succeeding documentation all macro metafunctions will be referred by
  137. their simple form name, but remember that the complex form can always be used
  138. instead. The complex form is useful whenever using the simple form could create
  139. a duplicate name in the same name space, thereby violating the C++ one definition
  140. rule.
  141. </p>
  142. <h4>
  143. <a name="the_type_traits_introspection_library.tti_functionality.h0"></a>
  144. <span class="phrase"><a name="the_type_traits_introspection_library.tti_functionality.macro_metafunction_name_generation"></a></span><a class="link" href="tti_functionality.html#the_type_traits_introspection_library.tti_functionality.macro_metafunction_name_generation">Macro
  145. Metafunction Name Generation</a>
  146. </h4>
  147. <p>
  148. For the simple macro form, even though it is fairly easy to remember the algorithm
  149. by which the generated metafunction is named, TTI also provides, for each macro
  150. metafunction, a corresponding 'naming' macro which the end-user can use and
  151. whose sole purpose is to expand to the metafunction name. The naming macro
  152. for each macro metafunction has the form: 'corresponding-macro'_GEN(name).
  153. </p>
  154. <p>
  155. As an example, BOOST_TTI_HAS_TYPE(MyType) creates a metafunction which looks
  156. for a nested type called 'MyType' within some enclosing type. The name of the
  157. metafunction generated, given our rule above is 'has_type_MyType'. A corresponding
  158. macro called BOOST_TTI_HAS_TYPE_GEN, invoked as BOOST_TTI_HAS_TYPE_GEN(MyType)
  159. in our example, expands to the same 'has_type_MyType' name. These name generating
  160. macros, for each of the metafunction generating macros, are purely a convenience
  161. for end-users who find using them easier than remembering the name-generating
  162. rule given above.
  163. </p>
  164. <div class="section">
  165. <div class="titlepage"><div><div><h3 class="title">
  166. <a name="the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen"></a><a class="link" href="tti_functionality.html#the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen" title="Macro metafunction name generation considerations">Macro
  167. metafunction name generation considerations</a>
  168. </h3></div></div></div>
  169. <p>
  170. Because having a double underscore ( __ ) in a name is reserved by the C++
  171. implementation, creating C++ identifiers with double underscores should be
  172. avoided by the end-user. When using a TTI macro to generate a metafunction
  173. using the simple macro form, TTI appends a single underscore to the macro
  174. name preceding the name of the element that is being introspected. The reason
  175. for doing this is because Boost discourages as non-portable C++ identifiers
  176. with mixed case letters and the underscore then becomes the normal way to
  177. separate parts of an identifier name so that it looks understandable. Because
  178. of this decision to use the underscore to generate the metafunction name
  179. from the macro name, any inner element starting with an underscore will cause
  180. the identifier for the metafunction name being generated to contain a double
  181. underscore.
  182. </p>
  183. <p>
  184. A rule to avoid this problem is:
  185. </p>
  186. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
  187. When the name of the inner element to be introspected begins with an
  188. underscore, use the complex macro form, where the name of the metafunction
  189. is specifically given.
  190. </li></ul></div>
  191. <p>
  192. Furthermore because TTI often generates not only a metafunction for the end-user
  193. to use but some supporting detail metafunctions whose identifier, for reasons
  194. of programming clarity, is the same as the metafunction with further letters
  195. appended to it separated by an underscore, another rule is:
  196. </p>
  197. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
  198. When using the complex macro form, which fully gives the name of the
  199. generated macro metafunction, that name should not end with an underscore.
  200. </li></ul></div>
  201. <p>
  202. Following these two simple rules will avoid names with double underscores
  203. being generated by TTI.
  204. </p>
  205. <h5>
  206. <a name="the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen.h0"></a>
  207. <span class="phrase"><a name="the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen.reusing_the_named_metafunction"></a></span><a class="link" href="tti_functionality.html#the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen.reusing_the_named_metafunction">Reusing
  208. the named metafunction</a>
  209. </h5>
  210. <p>
  211. When the end-user uses the TTI macros to generate a metafunction for introspecting
  212. an inner element of a particular type, that metafunction can be re-used with
  213. any combination of valid template parameters which involve the same type
  214. of inner element of a particular name.
  215. </p>
  216. <p>
  217. As one example of this let's consider two different types called 'AType'
  218. and 'BType', for each of which we want to determine whether an inner type
  219. called 'InnerType' exists. For both these types we need only generate a single
  220. macro metafunction in the current scope by using:
  221. </p>
  222. <pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_TYPE</span><span class="special">(</span><span class="identifier">InnerType</span><span class="special">)</span>
  223. </pre>
  224. <p>
  225. We now have a metafunction, which is a C++ class template, in the current
  226. scope whose C++ identifier is 'has_type_InnerType'. We can use this same
  227. metafunction to introspect the existence of the nested type 'InnerType' in
  228. either 'AType' or 'BType' at compile time. Although the syntax for doing
  229. this has no yet been explained, I will give it here so that you can see how
  230. 'has_type_InnerType' is reused:
  231. </p>
  232. <div class="orderedlist"><ol class="orderedlist" type="1">
  233. <li class="listitem">
  234. 'has_type_InnerType&lt;AType&gt;::value' is a compile time constant telling
  235. us whether 'InnerType' is a type which is nested within 'AType'.
  236. </li>
  237. <li class="listitem">
  238. 'has_type_InnerType&lt;BType&gt;::value' is a compile time constant telling
  239. us whether 'InnerType' is a type which is nested within 'BType'.
  240. </li>
  241. </ol></div>
  242. <p>
  243. As another example of metafunction reuse let's consider a single type, called
  244. 'CType', for which we want to determine if it has either of two overloaded
  245. member functions with the same name of 'AMemberFunction' but with the different
  246. function signatures of 'int (int)' and 'double (long)'. For both these member
  247. functions we need only generate a single macro metafunction in the current
  248. scope by using:
  249. </p>
  250. <pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_MEMBER_FUNCTION</span><span class="special">(</span><span class="identifier">AMemberFunction</span><span class="special">)</span>
  251. </pre>
  252. <p>
  253. We now have a metafunction, which is a C++ class template, in the current
  254. scope whose C++ identifier is 'has_member_function_AMemberFunction'. We can
  255. use this same metafunction to introspect the existence of the member function
  256. 'AMemberFunction' with either the function signature of 'int (int)' or 'double
  257. (long)' in 'CType' at compile time. Although the syntax for doing this has
  258. no yet been explained, I will give it here so that you can see how 'has_type_InnerType'
  259. is reused:
  260. </p>
  261. <div class="orderedlist"><ol class="orderedlist" type="1">
  262. <li class="listitem">
  263. 'has_member_function_AMemberFunction&lt;CType,int,boost::mpl::vector&lt;int&gt;
  264. &gt;::value' is a compile time constant telling us whether 'AMemberFunction'
  265. is a member function of type 'CType' whose function signature is 'int
  266. (int)'.
  267. </li>
  268. <li class="listitem">
  269. 'has_member_function_AMemberFunction&lt;CType,double,boost::mpl::vector&lt;long&gt;
  270. &gt;::value' is a compile time constant telling us whether 'AMemberFunction'
  271. is a member function of type 'CType' whose function signature is 'double
  272. (long)'.
  273. </li>
  274. </ol></div>
  275. <p>
  276. These are just two examples of the ways a particular macro metafunction can
  277. be reused. The two 'constants' when generating a macro metafunction are the
  278. 'name' and 'type of inner element'. Once the macro metafunction for a particular
  279. name and inner element type has been generated, it can be reused for introspecting
  280. the inner element(s) of any enclosing type which correspond to that name
  281. and inner element type.
  282. </p>
  283. <h5>
  284. <a name="the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen.h1"></a>
  285. <span class="phrase"><a name="the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen.avoiding_odr_violations"></a></span><a class="link" href="tti_functionality.html#the_type_traits_introspection_library.tti_functionality.tti_functionality_nm_gen.avoiding_odr_violations">Avoiding
  286. ODR violations</a>
  287. </h5>
  288. <p>
  289. The TTI macro metafunctions are generating directly in the enclosing scope
  290. in which the corresponding macro is invoked. This can be any C++ scope in
  291. which a class template can be specified. Within this enclosing scope the
  292. name of the metafunction being generated must be unique or else a C++ ODR
  293. ( One Definition Rule ) violation will occur. This is extremely important
  294. to remember, especially when the enclosing scope can occur in more than one
  295. translation unit, which is the case for namespaces and the global scope.
  296. </p>
  297. <p>
  298. Because of ODR, and the way that the simple macro form metafunction name
  299. is directly dependent on the inner element and name of the element being
  300. introspected, it is the responsibility of the programmer using the TTI macros
  301. to generate metafunctions to avoid ODR within a module ( application or library
  302. ). There are a few general methods for doing this:
  303. </p>
  304. <div class="orderedlist"><ol class="orderedlist" type="1">
  305. <li class="listitem">
  306. Create unique namespace names in which to generate the macro metafunctions
  307. and/or generate the macro metafunctions in C++ scopes which can not extend
  308. across translation units. The most obvious example of this latter is
  309. within C++ classes.
  310. </li>
  311. <li class="listitem">
  312. Use the complex macro form to specifically name the metafunction generated
  313. in order to provide a unique name.
  314. </li>
  315. <li class="listitem">
  316. Avoid using the TTI macros in the global scope.
  317. </li>
  318. </ol></div>
  319. <p>
  320. For anyone using TTI in a library which others will eventually use, it is
  321. important to generate metafunction names which are unique to that library.
  322. </p>
  323. <p>
  324. TTI also reserves not only the name generated by the macro metafunction for
  325. its use but also any C++ identifier sequence which begins with that name.
  326. In other words if the metafunction being generated by TTI is named 'MyMetafunction'
  327. using the complex macro form, do not create any C++ construct with an identifier
  328. starting with 'MyMetaFunction', such as 'MyMetaFunction_Enumeration' or 'MyMetaFunctionHelper'
  329. in the same scope. All names starting with the metafunction name in the current
  330. scope should be considered out of bounds for the programmer using TTI.
  331. </p>
  332. </div>
  333. </div>
  334. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  335. <td align="left"></td>
  336. <td align="right"><div class="copyright-footer">Copyright &#169; 2011-2013 Tropic Software
  337. East Inc<p>
  338. Distributed under the Boost Software License, Version 1.0. (See accompanying
  339. 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>)
  340. </p>
  341. </div></td>
  342. </tr></table>
  343. <hr>
  344. <div class="spirit-nav">
  345. <a accesskey="p" href="tti_terminology.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.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  346. </div>
  347. </body>
  348. </html>