2 (C) Copyright Edward Diener 2011,2012
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at
5 http://www.boost.org/LICENSE_1_0.txt).
8 [section:tti_usingMM An example using the Macro Metafunctions]
11 Using the macro metafunctions can be illustrated by first creating some hypothetical
12 user-defined type with corresponding nested types and other inner elements.
13 With this type we can illustrate the use of the macro metafunctions. This is
14 just meant to serve as a model for what a type T might entail from within
15 a class or function template where 'T' is a type passed to the template.
24 typedef int AnIntType; // as a typedef
26 struct BType // as a nested type
35 template <class> struct AMemberTemplate { };
36 template <class,class,class> struct AnotherMemberTemplate { };
37 template <class,class,int,class,template <class> class,class,long> struct ManyParameters { };
38 template <class,class,int,short,class,template <class,int> class,class> struct MoreParameters { };
46 int IntFunction(short) { return 0; }
50 static short DSMember;
54 static int SIntFunction(long,double) { return 2; }
58 I will be using the type above just to illustrate the sort of
59 metaprogramming questions we can ask of some type T which is passed
60 to the template programmer in a class template. Here is what the
61 class template might look like:
63 #include <boost/tti/tti.hpp>
66 struct OurTemplateClass
69 // compile-time template code regarding T
73 Now let us create and invoke the macro metafunctions for each of our inner element types,
74 to see if type T above corresponds to our hypothetical type above. Imagine this being
75 within 'OurTemplateClass' above. In the examples below the same macro is invoked just once
76 to avoid ODR violations.
80 Does T have a nested type called 'AnIntType' ?
82 BOOST_TTI_HAS_TYPE(AnIntType)
89 Does T have a nested type called 'BType' ?
91 BOOST_TTI_HAS_TYPE(BType)
98 [heading Type checking the typedef using a lambda expression]
100 Does T have a nested typedef called 'AnIntType' whose type is an 'int' ?
102 #include <boost/mpl/placeholders.hpp
103 #include <boost/type_traits/is_same.hpp
104 using namespace boost::mpl::placeholders;
109 boost::is_same<_1,int>
114 Does T have a nested class template called 'AMemberTemplate' whose template
115 parameters are all types ('class' or 'typename') ?
117 BOOST_TTI_HAS_TEMPLATE(AMemberTemplate,BOOST_PP_NIL)
119 has_template_AMemberTemplate
124 [heading Template using variadic macros]
126 Does T have a nested class template called 'AMemberTemplate' whose template
127 parameters are all types ('class' or 'typename') ?
129 BOOST_TTI_HAS_TEMPLATE(AnotherMemberTemplate)
131 has_template_AnotherMemberTemplate
136 [heading Template with params]
138 Does T have a nested class template called 'MoreParameters' whose template
139 parameters are specified exactly ?
141 BOOST_TTI_HAS_TEMPLATE(MoreParameters,(8,(class,class,int,short,class,template <class,int> class,class)))
143 has_template_MoreParameters
148 [heading Template with params using variadic macros]
150 Does T have a nested class template called 'ManyParameters' whose template
151 parameters are specified exactly ?
153 BOOST_TTI_HAS_TEMPLATE(ManyParameters,class,class,int,class,template <class> class,class,long)
155 has_template_ManyParameters
160 [heading Member data]
162 Does T have a member data called 'IntBT' whose type is 'AType::BType' ?
164 BOOST_TTI_HAS_MEMBER_DATA(IntBT)
166 has_member_data_IntBT
172 [heading Member data with composite type]
174 Does T have a member data called 'IntBT' whose type is 'AType::BType' ?
176 BOOST_TTI_HAS_MEMBER_DATA(IntBT)
178 has_member_data_IntBT
183 [heading Member function with individual types]
185 Does T have a member function called 'IntFunction' whose type is
188 BOOST_TTI_HAS_MEMBER_FUNCTION(IntFunction)
190 has_member_function_IntFunction
194 boost::mpl::vector<short>
197 [heading Member function with composite type]
199 Does T have a member function called 'IntFunction' whose type is
202 BOOST_TTI_HAS_MEMBER_FUNCTION(IntFunction)
203 has_member_function_IntFunction
208 [heading Static member data]
210 Does T have a static member data called 'DSMember' whose type is 'short' ?
212 BOOST_TTI_HAS_STATIC_MEMBER_DATA(DSMember)
214 has_static_member_data_DSMember
220 [heading Static member function with individual types]
222 Does T have a static member function called 'SIntFunction' whose type
223 is 'int (long,double)' ?
225 BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(SIntFunction)
227 has_static_member_function_SIntFunction
231 boost::mpl::vector<long,double>
234 [heading Static member function with composite type]
236 Does T have a static member function called 'SIntFunction' whose type
237 is 'int (long,double)' ?
239 BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(SIntFunction)
241 has_static_member_function_SIntFunction
249 Does T have a member data or static member data called 'DSMember' whose type is 'short' ?
251 BOOST_TTI_HAS_DATA(DSMember)
253 has_static_member_data_DSMember
261 Does T have a member function or a static member function called 'IntFunction' whose type is
264 BOOST_TTI_HAS_FUNCTION(IntFunction)
266 has_function_IntFunction
270 boost::mpl::vector<short>
273 [heading Member type]
275 Create a nested type T::BType::CType without creating a compiler error
276 if T does not have the nested type BType::CType ?
278 BOOST_TTI_MEMBER_TYPE(BType)
279 BOOST_TTI_MEMBER_TYPE(CType)
291 [heading Member type existence]
293 Does a nested type T::BType::CType, created without creating a compiler error
294 if T does not have the nested type BType::CType, actually exist ?
296 BOOST_TTI_MEMBER_TYPE(BType)
297 BOOST_TTI_MEMBER_TYPE(CType)
310 boost::tti::valid_member_type