Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / tti / doc / html / the_type_traits_introspection_library / tti_detail_has_static_member_data.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Introspecting static member data</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_member_function.html" title="Introspecting member function">
10 <link rel="next" href="tti_detail_has_static_member_function.html" title="Introspecting static member function">
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_member_function.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_static_member_function.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_data"></a><a class="link" href="tti_detail_has_static_member_data.html" title="Introspecting static member data">Introspecting
28     static member data</a>
29 </h2></div></div></div>
30 <p>
31       The TTI macro <code class="computeroutput"><a class="link" href="../BOOST_TT_idm46187185698016.html" title="Macro BOOST_TTI_HAS_STATIC_MEMBER_DATA">BOOST_TTI_HAS_STATIC_MEMBER_DATA</a></code>
32       introspects static member data of a class.
33     </p>
34 <p>
35       BOOST_TTI_HAS_STATIC_MEMBER_DATA macro takes a single parameter which is the
36       name of an inner static member data whose existence the programmer wants to
37       check. The macro generates a metafunction called "has_static_member_data_'name_of_inner_static_member_data'".
38     </p>
39 <p>
40       The metafunction can be invoked by passing it the enclosing type to introspect
41       and the type of the static member data.
42     </p>
43 <p>
44       The metafunction returns a single type called 'type', which is a boost::mpl::bool_.
45       As a convenience the metafunction returns the value of this type directly as
46       a compile time bool constant called 'value'. This is true or false depending
47       on whether the inner static member data, of the specified type, exists or not.
48     </p>
49 <h4>
50 <a name="the_type_traits_introspection_library.tti_detail_has_static_member_data.h0"></a>
51       <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_static_member_data.generating_the_metafunction"></a></span><a class="link" href="tti_detail_has_static_member_data.html#the_type_traits_introspection_library.tti_detail_has_static_member_data.generating_the_metafunction">Generating
52       the metafunction</a>
53     </h4>
54 <p>
55       You generate the metafunction by invoking the macro with the name of an inner
56       static member data:
57     </p>
58 <pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_STATIC_MEMBER_DATA</span><span class="special">(</span><span class="identifier">AStaticMemberData</span><span class="special">)</span>
59 </pre>
60 <p>
61       generates a metafunction called 'has_static_member_data_AStaticMemberData'
62       in the current scope.
63     </p>
64 <h4>
65 <a name="the_type_traits_introspection_library.tti_detail_has_static_member_data.h1"></a>
66       <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_static_member_data.invoking_the_metafunction"></a></span><a class="link" href="tti_detail_has_static_member_data.html#the_type_traits_introspection_library.tti_detail_has_static_member_data.invoking_the_metafunction">Invoking
67       the metafunction</a>
68     </h4>
69 <p>
70       You invoke the metafunction by instantiating the template with an enclosing
71       type to introspect and the type of the static member data. A return value called
72       'value' is a compile time bool constant.
73     </p>
74 <pre class="programlisting"><span class="identifier">has_static_member_data_AStaticMemberData</span><span class="special">&lt;</span><span class="identifier">Enclosing_Type</span><span class="special">,</span><span class="identifier">StaticMemberData_Type</span><span class="special">&gt;::</span><span class="identifier">value</span>
75 </pre>
76 <h4>
77 <a name="the_type_traits_introspection_library.tti_detail_has_static_member_data.h2"></a>
78       <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_static_member_data.examples"></a></span><a class="link" href="tti_detail_has_static_member_data.html#the_type_traits_introspection_library.tti_detail_has_static_member_data.examples">Examples</a>
79     </h4>
80 <p>
81       First we generate metafunctions for various inner member data names:
82     </p>
83 <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_data</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
84
85 <span class="identifier">BOOST_TTI_HAS_STATIC_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data1</span><span class="special">)</span>
86 <span class="identifier">BOOST_TTI_HAS_STATIC_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data2</span><span class="special">)</span>
87 <span class="identifier">BOOST_TTI_HAS_STATIC_MEMBER_DATA</span><span class="special">(</span><span class="identifier">data3</span><span class="special">)</span>
88 </pre>
89 <p>
90       Next let us create some user-defined types we want to introspect.
91     </p>
92 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">AClass</span>
93   <span class="special">{</span>
94   <span class="special">};</span>
95 <span class="keyword">struct</span> <span class="identifier">Top</span>
96   <span class="special">{</span>
97   <span class="keyword">static</span> <span class="keyword">int</span> <span class="identifier">data1</span><span class="special">;</span>
98   <span class="keyword">static</span> <span class="identifier">AClass</span> <span class="special">*</span> <span class="identifier">data2</span><span class="special">;</span>
99   <span class="special">};</span>
100 <span class="keyword">struct</span> <span class="identifier">Top2</span>
101   <span class="special">{</span>
102   <span class="keyword">static</span> <span class="keyword">long</span> <span class="identifier">data1</span><span class="special">;</span>
103   <span class="keyword">static</span> <span class="identifier">Top</span> <span class="identifier">data3</span><span class="special">;</span>
104   <span class="special">};</span>
105 </pre>
106 <p>
107       Finally we invoke our metafunction and return our value. This all happens at
108       compile time, and can be used by programmers doing compile time template metaprogramming.
109     </p>
110 <pre class="programlisting"><span class="identifier">has_static_member_data_data1</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>
111 <span class="identifier">has_static_member_data_data1</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">long</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
112 <span class="identifier">has_static_member_data_data1</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>
113 <span class="identifier">has_static_member_data_data1</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">long</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
114
115 <span class="identifier">has_static_member_data_data2</span><span class="special">&lt;</span><span class="identifier">Top</span><span class="special">,</span><span class="identifier">AClass</span> <span class="special">*&gt;::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
116 <span class="identifier">has_static_member_data_data2</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">// false</span>
117
118 <span class="identifier">has_static_member_data_data3</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>
119 <span class="identifier">has_static_member_data_data3</span><span class="special">&lt;</span><span class="identifier">Top2</span><span class="special">,</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>
120 </pre>
121 <h4>
122 <a name="the_type_traits_introspection_library.tti_detail_has_static_member_data.h3"></a>
123       <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_static_member_data.metafunction_re_use"></a></span><a class="link" href="tti_detail_has_static_member_data.html#the_type_traits_introspection_library.tti_detail_has_static_member_data.metafunction_re_use">Metafunction
124       re-use</a>
125     </h4>
126 <p>
127       The macro encodes only the name of the static member data for which we are
128       searching and the fact that we are introspecting for static member data within
129       an enclosing type.
130     </p>
131 <p>
132       Because of this, once we create our metafunction for introspecting an inner
133       static member data by name, we can reuse the metafunction for introspecting
134       any enclosing type, having any inner static member data type, for that name.
135     </p>
136 </div>
137 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
138 <td align="left"></td>
139 <td align="right"><div class="copyright-footer">Copyright &#169; 2011-2013 Tropic Software
140       East Inc<p>
141         Distributed under the Boost Software License, Version 1.0. (See accompanying
142         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>)
143       </p>
144 </div></td>
145 </tr></table>
146 <hr>
147 <div class="spirit-nav">
148 <a accesskey="p" href="tti_detail_has_member_function.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_static_member_function.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
149 </div>
150 </body>
151 </html>