File: tti_detail_has_member_function.qbk

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (141 lines) | stat: -rw-r--r-- 5,461 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
[/ 
  (C) Copyright Edward Diener 2011,2012,2014
  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE_1_0.txt or copy at
  http://www.boost.org/LICENSE_1_0.txt).
]

[section:tti_detail_has_member_function Introspecting member function]

The TTI macro [macroref BOOST_TTI_HAS_MEMBER_FUNCTION] introspects
a member function of a class.

BOOST_TTI_HAS_MEMBER_FUNCTION takes a single
parameter which is the name of an inner member function whose existence
the programmer wants to check. The macro generates a metafunction
called "has_member_function_'name_of_inner_member_function'". 

The metafunction can be invoked in two different ways.

The first way of invoking the metafunction is by passing it the enclosing 
type to introspect and a signature for the member function as a series of 
separate template arguments. The signature for the member function consists 
of the template arguments of a return type, of optional parameter types in 
the form of a boost::mpl forward sequence of types, and of an optional Boost 
FunctionTypes tag type. A typical boost::mpl forward sequence of types is 
a boost::mpl::vector<>.

The optional Boost FunctionTypes tag type may be used to specify 
cv-qualification. This means you can add 'const', 'volatile', or both by 
specifying an appropriate tag type. An alternate to using the tag type 
is to specify the enclosing type as 'const', 'volatile', or both.
As an example if you specify the tag type as 
'boost::function_types::const_qualified' or if you specify the enclosing
type as 'const T', the member function which you are introspecting 
must be a const function.

The second way of invoking the metafunction is by passing it a single 
parameter, which is a pointer to member function. This type has the form of:

 Return_Type ( Enclosing_Type::* ) ( Parameter_Types ) cv_qualifier(s)
 
where the Parameter_Types may be empty, or a comma-separated 
list of parameter types if there are more than one parameter type.
The cv-qualifier may be 'const', 'volatile', or 'const volatile'.

The metafunction returns a single type called 'type', which is a 
boost::mpl::bool_. As a convenience the metafunction 
returns the value of this type directly as a compile time bool constant 
called 'value'. This 'value' is true or false depending on whether the inner 
member function, of the specified signature, exists or not.

[heading Generating the metafunction]

You generate the metafunction by invoking the macro with the name 
of an inner member function:

  BOOST_TTI_HAS_MEMBER_FUNCTION(AMemberFunction)
  
generates a metafunction called 'has_member_function_AMemberFunction' in the current scope.

[heading Invoking the metafunction]

You invoke the metafunction by instantiating the template with an enclosing 
type to introspect and the signature of the member function as a series of template
parameters. Alternatively you can invoke the metafunction by passing it a single 
type which is a pointer to member function.

A return value called 'value' is a compile time bool constant.

  has_member_function_AMemberFunction
    <
    Enclosing_Type,
    MemberFunction_ReturnType,
    boost::mpl::vector<MemberFunction_ParameterTypes>, // optional, can be any mpl forward sequence
    boost::function_types::SomeTagType                 // optional, can be any FunctionTypes tag type
    >::value
    
  OR  
  
  has_member_function_AMemberFunction
    <
    MemberFunction_ReturnType (Enclosing_Type::*) (MemberFunction_ParameterTypes) optional_cv_qualification
    >::value
    
[heading Examples]

First we generate metafunctions for various inner member function names: 

 #include <boost/tti/has_member_function.hpp>
 
 BOOST_TTI_HAS_MEMBER_FUNCTION(function1)
 BOOST_TTI_HAS_MEMBER_FUNCTION(function2)
 BOOST_TTI_HAS_MEMBER_FUNCTION(function3)
 
Next let us create some user-defined types we want to introspect. 

 struct AClass 
   { 
   };
 struct Top
   {
   int function1();
   AClass function2(double,short *);
   };
 struct Top2
   {
   long function2(Top &,int,bool,short,float);
   Top * function3(long,int,AClass &);
   };
   
Finally we invoke our metafunction and return our value.
This all happens at compile time, and can be used by 
programmers doing compile time template metaprogramming.
  
We will show both forms in the following examples. 
Both forms are completely interchangeable as to the result
desired.

 has_member_function_function1<Top,int>::value; // true
 has_member_function_function1<Top,int,boost::mpl::vector<> >::value; // true
 has_member_function_function1<Top2,int>::value; // false
 
 has_member_function_function2<AClass (Top::*) (double,short *)>::value; // true
 has_member_function_function2<AClass (Top2::*) (double,short *)>::value; // false
 has_member_function_function2<long (Top2::*) (Top &,int,bool,short,float)>::value; // true
 
 has_member_function_function3<int (Top2::*) ()>::value; // false
 has_member_function_function3<Top2,Top *,boost::mpl::vector<long,int,AClass &> >::value; // true;
   
[heading Metafunction re-use]

The macro encodes only the name of the member function for which
we are searching and the fact that we are introspecting for a
member function within an enclosing type.

Because of this, once we create our metafunction for 
introspecting a member function by name, we can reuse the 
metafunction for introspecting any enclosing type, having any 
member function, for that name.

[endsect]