tti_detail_has_static_member_function.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Introspecting static member function</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_static_member_data.html" title="Introspecting static member data">
  10. <link rel="next" href="tti_detail_has_data.html" title="Introspecting inner data">
  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_static_member_data.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_data.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_static_member_function"></a><a class="link" href="tti_detail_has_static_member_function.html" title="Introspecting static member function">Introspecting
  28. static member function</a>
  29. </h2></div></div></div>
  30. <p>
  31. The TTI macro <code class="computeroutput"><a class="link" href="../BOOST_TT_idm46187185690608.html" title="Macro BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION">BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION</a></code>
  32. introspects a static member function of a class.
  33. </p>
  34. <p>
  35. BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION takes a single parameter which is the
  36. name of an inner static member function whose existence the programmer wants
  37. to check. The macro generates a metafunction called "has_static_member_function_'name_of_inner_static_member_function'".
  38. </p>
  39. <p>
  40. The metafunction can be invoked in two different ways.
  41. </p>
  42. <p>
  43. The first way is by passing it the enclosing type to introspect and a signature
  44. for the static member function as separate template arguments. The signature
  45. for the static member function consists of a return type, optional parameter
  46. types in the form of a boost::mpl forward sequence of types, and an optional
  47. Boost FunctionTypes tag type. A typical boost::mpl forward sequence of types
  48. is a boost::mpl::vector&lt;&gt;.
  49. </p>
  50. <p>
  51. The second way is by passing it the enclosing type to introspect and a signature
  52. for the static member function as a function. The function has the form of:
  53. </p>
  54. <pre class="programlisting"><span class="identifier">Return_Type</span> <span class="special">(</span> <span class="identifier">Parameter_Types</span> <span class="special">)</span>
  55. </pre>
  56. <p>
  57. where the Parameter_Types may be empty, or a comma-separated list of parameter
  58. types if there are more than one parameter type.
  59. </p>
  60. <p>
  61. The metafunction returns a single type called 'type', which is a boost::mpl::bool_.
  62. As a convenience the metafunction returns the value of this type directly as
  63. a compile time bool constant called 'value'. This is true or false depending
  64. on whether the inner static member function, of the specified signature, exists
  65. or not.
  66. </p>
  67. <h4>
  68. <a name="the_type_traits_introspection_library.tti_detail_has_static_member_function.h0"></a>
  69. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_static_member_function.generating_the_metafunction"></a></span><a class="link" href="tti_detail_has_static_member_function.html#the_type_traits_introspection_library.tti_detail_has_static_member_function.generating_the_metafunction">Generating
  70. the metafunction</a>
  71. </h4>
  72. <p>
  73. You generate the metafunction by invoking the macro with the name of an inner
  74. static member function:
  75. </p>
  76. <pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION</span><span class="special">(</span><span class="identifier">AStaticMemberFunction</span><span class="special">)</span>
  77. </pre>
  78. <p>
  79. generates a metafunction called 'has_static_member_function_AStaticMemberFunction'
  80. in the current scope.
  81. </p>
  82. <h4>
  83. <a name="the_type_traits_introspection_library.tti_detail_has_static_member_function.h1"></a>
  84. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_static_member_function.invoking_the_metafunction"></a></span><a class="link" href="tti_detail_has_static_member_function.html#the_type_traits_introspection_library.tti_detail_has_static_member_function.invoking_the_metafunction">Invoking
  85. the metafunction</a>
  86. </h4>
  87. <p>
  88. You invoke the metafunction by instantiating the template with an enclosing
  89. type to introspect and the signature of the static member function as a series
  90. of template parameters. Alternatively you can invoke the metafunction by passing
  91. it an enclosing type and the signature of the static member function as a single
  92. function type.
  93. </p>
  94. <p>
  95. A return value called 'value' is a compile time bool constant.
  96. </p>
  97. <pre class="programlisting"><span class="identifier">has_static_member_function_AStaticMemberFunction</span>
  98. <span class="special">&lt;</span>
  99. <span class="identifier">Enclosing_Type</span><span class="special">,</span>
  100. <span class="identifier">StaticMemberFunction_ReturnType</span><span class="special">,</span>
  101. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">StaticMemberFunction_ParameterTypes</span><span class="special">&gt;,</span> <span class="comment">// optional, can be any mpl forward sequence</span>
  102. <span class="identifier">boost</span><span class="special">::</span><span class="identifier">function_types</span><span class="special">::</span><span class="identifier">SomeTagType</span> <span class="comment">// optional, can be any FunctionTypes tag type</span>
  103. <span class="special">&gt;::</span><span class="identifier">value</span>
  104. <span class="identifier">OR</span>
  105. <span class="identifier">has_static_member_function_AStaticMemberFunction</span>
  106. <span class="special">&lt;</span>
  107. <span class="identifier">Enclosing_Type</span><span class="special">,</span>
  108. <span class="identifier">Return_Type</span> <span class="special">(</span> <span class="identifier">Parameter_Types</span> <span class="special">)</span>
  109. <span class="special">&gt;::</span><span class="identifier">value</span>
  110. </pre>
  111. <h4>
  112. <a name="the_type_traits_introspection_library.tti_detail_has_static_member_function.h2"></a>
  113. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_static_member_function.examples"></a></span><a class="link" href="tti_detail_has_static_member_function.html#the_type_traits_introspection_library.tti_detail_has_static_member_function.examples">Examples</a>
  114. </h4>
  115. <p>
  116. First we generate metafunctions for various inner static member function names:
  117. </p>
  118. <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_static_member_function</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  119. <span class="identifier">BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION</span><span class="special">(</span><span class="identifier">function1</span><span class="special">)</span>
  120. <span class="identifier">BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION</span><span class="special">(</span><span class="identifier">function2</span><span class="special">)</span>
  121. <span class="identifier">BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION</span><span class="special">(</span><span class="identifier">function3</span><span class="special">)</span>
  122. </pre>
  123. <p>
  124. Next let us create some user-defined types we want to introspect.
  125. </p>
  126. <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">AClass</span> <span class="special">{</span> <span class="special">};</span>
  127. <span class="keyword">struct</span> <span class="identifier">Top</span>
  128. <span class="special">{</span>
  129. <span class="keyword">static</span> <span class="keyword">int</span> <span class="identifier">function1</span><span class="special">();</span>
  130. <span class="keyword">static</span> <span class="identifier">AClass</span> <span class="identifier">function2</span><span class="special">(</span><span class="keyword">double</span><span class="special">,</span><span class="keyword">short</span> <span class="special">*);</span>
  131. <span class="special">};</span>
  132. <span class="keyword">struct</span> <span class="identifier">Top2</span>
  133. <span class="special">{</span>
  134. <span class="keyword">static</span> <span class="keyword">long</span> <span class="identifier">function2</span><span class="special">(</span><span class="identifier">Top</span> <span class="special">&amp;,</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">bool</span><span class="special">,</span><span class="keyword">short</span><span class="special">,</span><span class="keyword">float</span><span class="special">);</span>
  135. <span class="keyword">static</span> <span class="identifier">Top</span> <span class="special">*</span> <span class="identifier">function3</span><span class="special">(</span><span class="keyword">long</span><span class="special">,</span><span class="keyword">int</span><span class="special">,</span><span class="identifier">AClass</span> <span class="special">&amp;);</span>
  136. <span class="special">};</span>
  137. </pre>
  138. <p>
  139. Finally we invoke our metafunction and return our value. This all happens at
  140. compile time, and can be used by programmers doing compile time template metaprogramming.
  141. </p>
  142. <p>
  143. We will show both forms in the following examples. Both forms are completely
  144. interchangeable as to the result desired.
  145. </p>
  146. <pre class="programlisting"><span class="identifier">has_static_member_function_function1</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  147. <span class="identifier">has_static_member_function_function1</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">int</span> <span class="special">()&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  148. <span class="identifier">has_static_member_function_function1</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  149. <span class="identifier">has_static_member_function_function2</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="identifier">AClass</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span><span class="keyword">short</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>
  150. <span class="identifier">has_static_member_function_function2</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="identifier">AClass</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span><span class="keyword">short</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>
  151. <span class="identifier">has_static_member_function_function2</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">long</span> <span class="special">(</span><span class="identifier">Top</span> <span class="special">&amp;,</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">bool</span><span class="special">,</span><span class="keyword">short</span><span class="special">,</span><span class="keyword">float</span><span class="special">)&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
  152. <span class="identifier">has_static_member_function_function3</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">int</span> <span class="special">()&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
  153. <span class="identifier">has_static_member_function_function3</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="identifier">Top</span> <span class="special">*</span> <span class="special">(</span><span class="keyword">long</span><span class="special">,</span><span class="keyword">int</span><span class="special">,</span><span class="identifier">AClass</span> <span class="special">&amp;)&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true;</span>
  154. </pre>
  155. <h4>
  156. <a name="the_type_traits_introspection_library.tti_detail_has_static_member_function.h3"></a>
  157. <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_static_member_function.metafunction_re_use"></a></span><a class="link" href="tti_detail_has_static_member_function.html#the_type_traits_introspection_library.tti_detail_has_static_member_function.metafunction_re_use">Metafunction
  158. re-use</a>
  159. </h4>
  160. <p>
  161. The macro encodes only the name of the static member function for which we
  162. are searching and the fact that we are introspecting for a static member function
  163. within an enclosing type.
  164. </p>
  165. <p>
  166. Because of this, once we create our metafunction for introspecting a static
  167. member function by name, we can reuse the metafunction for introspecting any
  168. enclosing type, having any static member function, for that name.
  169. </p>
  170. </div>
  171. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  172. <td align="left"></td>
  173. <td align="right"><div class="copyright-footer">Copyright &#169; 2011-2013 Tropic Software
  174. East Inc<p>
  175. Distributed under the Boost Software License, Version 1.0. (See accompanying
  176. 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>)
  177. </p>
  178. </div></td>
  179. </tr></table>
  180. <hr>
  181. <div class="spirit-nav">
  182. <a accesskey="p" href="tti_detail_has_static_member_data.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_data.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  183. </div>
  184. </body>
  185. </html>