]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/tti/doc/tti_detail_has_template.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / tti / doc / tti_detail_has_template.qbk
CommitLineData
7c673cae
FG
1[/
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).
6]
7
8[section:tti_detail_has_template Introspecting an inner class template]
9
10[section:tti_detail_has_template_macro Using the BOOST_TTI_HAS_TEMPLATE macro]
11
12The TTI macro [macroref BOOST_TTI_HAS_TEMPLATE] introspects
13an inner class template of a class. The macro must specify,
14at the least, the name of the class template to introspect.
15
16[heading Two forms of introspection]
17
18There are two general forms of template introspection which can be used.
19The first is to find a class template with any number of only
20template type parameters ( template parameters starting with `class`
21or `typename` ). In this form only the name of the class template
22needs to be specified when invoking the macro. We will call this form
23of the macro the `template type parameters` form. An example of a class
24template of this form which could be successfully introspected would be:
25
26 template<class X,typename Y,class Z,typename T> class AClassTemplate { /* etc. */ };
27
28The second is to find a class template with specific template parameters.
29In this form both the name of the class template and the template parameters
30are passed to the macro.
31
32We will call this form of the macro the `specific parameters` form. An example
33of a class template of this form which could be successfully introspected would be:
34
35 template<class X, template<class> class Y, int Z> BClassTemplate { /* etc. */ };
36
37When using the specific form of the macro, there are two things which
38need to be understood when passing the template parameters to the macro.
39First, the actual names of the template parameters passed are irrelevant.
40They can be left out completely or be different from the names in the
41nested class template itself. Second, the use of 'typename' or 'class',
42when referring to a template type parameter, is completely interchangeable,
43as it is in the actual class template itself.
44
45[heading Variadic and non-variadic macro usage]
46
47When using the BOOST_TTI_HAS_TEMPLATE macro we distinguish between compilers
48supporting variadic macros or not supporting variadic macros.
49
50The programmer can always tell whether or not the compiler
51supports variadic macros by checking the value of the macro
52BOOST_PP_VARIADIC after including the necessary header file
53`boost/tti/has_template.hpp` in order to use the BOOST_TTI_TEMPLATE
54macro. A value of 1 indicates the compiler supports variadic macros
55while a value of 0 indicates the compiler does not support variadic
56macros.
57
58Modern C++ compilers, in supporting the latest C++11 standard,
59normally support variadic macros. Even before the latest C++11 standard
60a number of C++ compilers already supported variadic macros. If you feel
61your compiler supports variadic macros and BOOST_PP_VARIADIC is 0 even
62after including `boost/tti/has_template.hpp`, you can predefine BOOST_PP_VARIADIC
63to 1 before including `boost/tti/has_template.hpp`.
64
65[heading Non-variadic macro usage]
66
67We start with syntax for compilers not supporting variadic macros since this
68syntax can also be used by compilers which do support variadic macros. The
69form for non-variadic macros always takes two macro parameters. The first
70macro parameter is always the name of the class template you are trying to
71introspect.
72
73The second macro parameter, when using the `specific parameters` form of the
74macro, is the template parameters in the form of a Boost preprocessor library
75array data type. When using the `template type parameters` form of the macro
76the second macro parameter is BOOST_PP_NIL. If the second parameter is neither
77a Boost preprocessor library array data type or BOOS_PP_NIL you will get a
78compiler error if your compiler only supports non-variadic macros.
79
80The non-variadic macro form for introspecting the class templates above
81using the `template type parameters` form would be:
82
83 BOOST_TTI_TEMPLATE(AClassTemplate,BOOST_PP_NIL)
84 BOOST_TTI_TEMPLATE(BClassTemplate,BOOST_PP_NIL)
85
86Invoking the metafunction in the second case would always fail since the
87BClassTemplate does not have all template type parameters.
88
89The non-variadic macro form for introspecting the class templates above
90using the `specific parameters` form would be:
91
92 BOOST_TTI_TEMPLATE(AClassTemplate,(4,(class,typename,class,typename)))
93 BOOST_TTI_TEMPLATE(BClassTemplate,(3,(class, template<class> class, int)))
94
95You need to be careful using the non-variadic `specific parameters` form
96to specify the correct number of array parameters. This can sometimes be
97tricky if you have a template template parameter, or a
98non-type template parameter which has parentheses
99surrounding part of the type specification. In the latter case,
100when parentheses surround a comma ( ',' ), do not count that as
101creating another Boost PP array token. Two examples:
102
103 template<void (*FunctionPointer)(int,long)> class CClassTemplate { /* etc. */ };
104 template<template<class,class> class T> class DClassTemplate { /* etc. */ };
105
106 BOOST_TTI_TEMPLATE(CClassTemplate,(1,(void (*)(int,long))))
107 BOOST_TTI_TEMPLATE(DClassTemplate,(2,(template<class,class> class)))
108
109In the case of using the macro to introspect CClassTemplate the number of
110Boost PP array parameters is 1, even though there is a comma separating
111the tokens in `void (*FunctionPointer)(int,long)`. This is because the
112comma is within parentheses.
113
114In the case of using the macro to introspect DClassTemplate the number of
115Boost PP array parameters is 2, because there is a comma separating the
116tokens in `template<class,class> class T`.
117
118[heading Variadic macro usage]
119
120Having the ability to use variadic macros makes the syntax for using
121BOOST_TTI_TEMPLATE easier to specify in both the `template type parameters`
122form and the `specific parameters` form of using the macro.
123This is because variadic macros can take a variable number of parameters.
124When using the variadic macro form the first macro parameter is always the name
125of the class template you are trying to introspect. You only specify
126further parameters when using the `specific parameters` form of the macro,
127in which case the further parameters to the macro are the specific template
128parameters.
129
130Introspecting the first class template above using the
131`template type parameters` form the variadic macro would be:
132
133 BOOST_TTI_TEMPLATE(AClassTemplate)
134
135Introspecting the other class templates above using the
136`specific parameters` form the variadic macros would be:
137
138 BOOST_TTI_TEMPLATE(BClassTemplate,class,template<class> class, int)
139 BOOST_TTI_TEMPLATE(CClassTemplate,void (*)(int,long))
140 BOOST_TTI_TEMPLATE(DClassTemplate,template<class,class> class)
141
142Here we have no problem with counting the number of tuple tokens
143for the Boost PP array, nor do we have to specify BOOST_PP_NIL if
144we are using the `template type parameters` form. Also for the
145specific parameters form we simply use the template parameters as
146the remaining tokens of the variadic macro.
147
148[heading The resulting metafunction]
149
150Using either form of the macro, whether using variadic or non-variadic
151syntax, the macro generates a metafunction called
152'has_template_'name_of_inner_class_template'.
153
154The metafunction can be invoked by passing it the enclosing type
155to introspect.
156
157The metafunction returns a single type called 'type', which is a
158boost::mpl::bool_. As a convenience the metafunction returns the
159value of this type directly as a compile time bool constant
160called 'value'. This is true or false depending on whether the inner
161class template exists or not.
162
163[endsect]
164
165[section:tti_detail_has_template_metafunction Using the has_template_(xxx) metafunction]
166
167[heading Generating the metafunction]
168
169You generate the metafunction by invoking the macro with the name
170of an inner class template:
171
172 // `template type parameters` form
173
174 BOOST_TTI_HAS_TEMPLATE(AClassTemplate,BOOST_PP_NIL) // non-variadic macro
175 BOOST_TTI_HAS_TEMPLATE(AClassTemplate) // variadic macro
176
177 // `specific parameters` form
178
179 BOOST_TTI_HAS_TEMPLATE(AClassTemplate,(2,(class,int))) // non-variadic macro
180 BOOST_TTI_HAS_TEMPLATE(AClassTemplate,class,int) // variadic macro
181
182generates a metafunction called 'has_template_AClassTemplate' in the current scope.
183
184If you want to introspect the same class template name using both the
185`template type parameters` form and the `specific parameters` form
186you will have the problem that you will be generating a metafunction
187of the same name and violating the C++ ODR rule. In this particular
188case you can use the alternate BOOST_TTI_TRAIT_HAS_TEMPLATE macro
189to name the particular metafunction which will be generated.
190
191[heading Invoking the metafunction]
192
193You invoke the metafunction by instantiating the template with an enclosing
194type to introspect. A return value called 'value' is a compile time bool constant.
195
196 has_template_AType<Enclosing_Type>::value
197
198[heading Examples]
199
200First we generate metafunctions for various inner class template names:
201
202 #include <boost/tti/has_template.hpp>
203
204 // Using variadic macro, `template type parameters`
205
206 BOOST_TTI_HAS_TEMPLATE(Template1)
207 BOOST_TTI_HAS_TEMPLATE(Template2)
208 BOOST_TTI_HAS_TEMPLATE(Template3)
209 BOOST_TTI_HAS_TEMPLATE(Template4)
210 BOOST_TTI_HAS_TEMPLATE(Template5)
211
212 // or using non-variadic macro, `template type parameters`
213
214 BOOST_TTI_HAS_TEMPLATE(Template1,BOOST_PP_NIL)
215 BOOST_TTI_HAS_TEMPLATE(Template2,BOOST_PP_NIL)
216 BOOST_TTI_HAS_TEMPLATE(Template3,BOOST_PP_NIL)
217 BOOST_TTI_HAS_TEMPLATE(Template4,BOOST_PP_NIL)
218 BOOST_TTI_HAS_TEMPLATE(Template5,BOOST_PP_NIL)
219
220 // Using variadic macro, `specific parameters`
221
222 BOOST_TTI_HAS_TEMPLATE(Template6,class,int)
223 BOOST_TTI_HAS_TEMPLATE(Template7,typename,template<class,class> struct,long)
224 BOOST_TTI_HAS_TEMPLATE(Template8,double,typename)
225 BOOST_TTI_HAS_TEMPLATE(Template9,typename,class,typename,class,typename,short)
226
227 // or using non-variadic macro, `specific parameters`
228
229 BOOST_TTI_HAS_TEMPLATE(Template6,(2,(class,int)))
230 BOOST_TTI_HAS_TEMPLATE(Template7,(4,(typename,template<class,class> struct,long)))
231 BOOST_TTI_HAS_TEMPLATE(Template8,(2,(double,typename)))
232 BOOST_TTI_HAS_TEMPLATE(Template9,(6,(typename,class,typename,class,typename,short)))
233
234Next let us create some user-defined types we want to introspect.
235
236 struct Top
237 {
238 template <class X> struct Template1 { };
239 template <typename A,typename B,typename C> class Template2 { };
240 template <typename A,typename B,typename C,int D> class Template3 { };
241 };
242 struct Top2
243 {
244 template <typename A,typename B,typename C,class D> class Template3 { };
245 template <class X,typename Y> struct Template4 { };
246 template <typename A,class B,typename C,class D,typename E> class Template5 { };
247 };
248 struct Top3
249 {
250 template <class X,int Y> struct Template6 { };
251 template <typename A,template<class,class> struct B,long C> class Template7 { };
252 };
253 struct Top4
254 {
255 template <double X,typename Y> struct Template8 { };
256 template <typename A,class B,typename C,class D,typename E,short F> class Template9 { };
257 };
258
259Finally we invoke our metafunction and return our value.
260This all happens at compile time, and can be used by
261programmers doing compile time template metaprogramming.
262
263 has_template_Template1<Top>::value; // true
264 has_template_Template1<Top2>::value; // false
265
266 has_template_Template2<Top>::value; // true
267 has_template_Template2<Top2>::value; // false
268
269 has_template_Template3<Top>::value; // false, not all typename/class template parameters
270 has_template_Template3<Top2>::value; // true
271
272 has_template_Template4<Top>::value; // false
273 has_template_Template4<Top2>::value; // true
274
275 has_template_Template5<Top>::value; // false
276 has_template_Template5<Top2>::value; // true
277
278 has_template_Template6<Top3>::value; // true
279 has_template_Template6<Top4>::value; // false
280
281 has_template_Template7<Top3>::value; // true
282 has_template_Template7<Top4>::value; // false
283
284 has_template_Template8<Top3>::value; // false
285 has_template_Template8<Top4>::value; // true
286
287 has_template_Template9<Top3>::value; // false
288 has_template_Template9<Top4>::value; // true
289
290[heading Metafunction re-use]
291
292The macro encodes the name of the inner class template for
293which we are searching, the fact that we are introspecting for
294a class template within an enclosing type, and optionally the
295template parameters for that class template.
296
297Once we create our metafunction for introspecting an inner class
298template by name, we can reuse the metafunction for introspecting
299any enclosing type, having any inner class template, for that name.
300
301However we need to understand that we are restricted in our reuse
302of the metafunction by whether we originally use the template type
303parameters form or the specific form. In either case we are always
304introspecting an inner class template which matches that form.
305In the case of the template type parameters form, any inner class
306template for which we are introspecting must have all template type
307parameters, as well as the correct name. In the case of the specific
308parameters form, any inner class template for which we are
309introspecting must have template parameters which match the specific
310template parameters passed to the macro, as well as the correct name.
311
312[endsect]
313
314[endsect]