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 1. The Type Traits Introspection Library">
8 <link rel="up" href="../index.html" title="Chapter 1. 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">
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>
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>
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>
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.
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'".
40 The metafunction can be invoked by passing it the enclosing type to introspect
41 and the type of the static member data.
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.
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
55 You generate the metafunction by invoking the macro with the name of an inner
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>
61 generates a metafunction called 'has_static_member_data_AStaticMemberData'
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
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.
74 <pre class="programlisting"><span class="identifier">has_static_member_data_AStaticMemberData</span><span class="special"><</span><span class="identifier">Enclosing_Type</span><span class="special">,</span><span class="identifier">StaticMemberData_Type</span><span class="special">>::</span><span class="identifier">value</span>
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>
81 First we generate metafunctions for various inner member data names:
83 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">></span>
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>
90 Next let us create some user-defined types we want to introspect.
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>
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.
110 <pre class="programlisting"><span class="identifier">has_static_member_data_data1</span><span class="special"><</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">int</span><span class="special">>::</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"><</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">long</span><span class="special">>::</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"><</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">int</span><span class="special">>::</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"><</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">long</span><span class="special">>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true</span>
115 <span class="identifier">has_static_member_data_data2</span><span class="special"><</span><span class="identifier">Top</span><span class="special">,</span><span class="identifier">AClass</span> <span class="special">*>::</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"><</span><span class="identifier">Top</span><span class="special">,</span><span class="keyword">int</span> <span class="special">*>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// false</span>
118 <span class="identifier">has_static_member_data_data3</span><span class="special"><</span><span class="identifier">Top2</span><span class="special">,</span><span class="keyword">int</span><span class="special">>::</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"><</span><span class="identifier">Top2</span><span class="special">,</span><span class="identifier">Top</span><span class="special">>::</span><span class="identifier">value</span><span class="special">;</span> <span class="comment">// true;</span>
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
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
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.
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 © 2011-2013 Tropic Software
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>)
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>