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_static_member_function Introspecting static member function] 9 10The TTI macro [macroref BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION] introspects 11a static member function of a class. 12 13BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION takes a single 14parameter which is the name of an inner static member function whose existence 15the programmer wants to check. The macro generates a metafunction 16called "has_static_member_function_'name_of_inner_static_member_function'". 17 18The metafunction can be invoked in two different ways. 19 20The first way is by passing it the enclosing type to introspect and a 21signature for the static member function as separate template 22arguments. The signature for the static member function consists of a return 23type, optional parameter types in the form of a boost::mpl forward 24sequence of types, and an optional Boost FunctionTypes tag type. A 25typical boost::mpl forward sequence of types is a boost::mpl::vector<>. 26 27The second way is by passing it the enclosing type to introspect and a 28signature for the static member function as a function. The function 29has the form of: 30 31 Return_Type ( Parameter_Types ) 32 33where the Parameter_Types may be empty, or a comma-separated 34list of parameter types if there are more than one parameter type. 35 36The metafunction returns a single type called 'type', which is a 37boost::mpl::bool_. As a convenience the metafunction 38returns the value of this type directly as a compile time bool constant 39called 'value'. This is true or false depending on whether the inner 40static member function, of the specified signature, exists or not. 41 42[heading Generating the metafunction] 43 44You generate the metafunction by invoking the macro with the name 45of an inner static member function: 46 47 BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(AStaticMemberFunction) 48 49generates a metafunction called 'has_static_member_function_AStaticMemberFunction' in the current scope. 50 51[heading Invoking the metafunction] 52 53You invoke the metafunction by instantiating the template with an enclosing 54type to introspect and the signature of the static member function as a series of template 55parameters. Alternatively you can invoke the metafunction by passing it an enclosing type 56and the signature of the static member function as a single function type. 57 58A return value called 'value' is a compile time bool constant. 59 60 has_static_member_function_AStaticMemberFunction 61 < 62 Enclosing_Type, 63 StaticMemberFunction_ReturnType, 64 boost::mpl::vector<StaticMemberFunction_ParameterTypes>, // optional, can be any mpl forward sequence 65 boost::function_types::SomeTagType // optional, can be any FunctionTypes tag type 66 >::value 67 68 OR 69 70 has_static_member_function_AStaticMemberFunction 71 < 72 Enclosing_Type, 73 Return_Type ( Parameter_Types ) 74 >::value 75 76[heading Examples] 77 78First we generate metafunctions for various inner static member function names: 79 80 #include <boost/tti/has_static_member_function.hpp> 81 82 BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(function1) 83 BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(function2) 84 BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(function3) 85 86Next let us create some user-defined types we want to introspect. 87 88 struct AClass { }; 89 struct Top 90 { 91 static int function1(); 92 static AClass function2(double,short *); 93 }; 94 struct Top2 95 { 96 static long function2(Top &,int,bool,short,float); 97 static Top * function3(long,int,AClass &); 98 }; 99 100Finally we invoke our metafunction and return our value. 101This all happens at compile time, and can be used by 102programmers doing compile time template metaprogramming. 103 104We will show both forms in the following examples. 105Both forms are completely interchangeable as to the result 106desired. 107 108 has_static_member_function_function1<Top,int>::value; // true 109 has_static_member_function_function1<Top,int ()>::value; // true 110 has_static_member_function_function1<Top2,int>::value; // false 111 112 has_static_member_function_function2<Top,AClass,boost::mpl::vector<double,short *> >::value; // true 113 has_static_member_function_function2<Top2,AClass,boost::mpl::vector<double,short *> >::value; // false 114 has_static_member_function_function2<Top2,long (Top &,int,bool,short,float)>::value; // true 115 116 has_static_member_function_function3<Top2,int ()>::value; // false 117 has_static_member_function_function3<Top2,Top * (long,int,AClass &)>::value; // true; 118 119[heading Metafunction re-use] 120 121The macro encodes only the name of the static member function 122for which we are searching and the fact that we are 123introspecting for a static member function within an enclosing type. 124 125Because of this, once we create our metafunction for 126introspecting a static member function by name, we can reuse the 127metafunction for introspecting any enclosing type, having any 128static member function, for that name. 129 130[endsect] 131