1[/ 2 (C) Copyright Edward Diener 2020 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_member_function_template Introspecting member function template] 9 10We can introspect a member function template of a user-defined type 11using the TTI functionality we shall now explain. 12 13A member function template is a function template that is a non-static memmber 14of a user-defined type. An example of a member function template would be: 15 16 struct AType 17 { 18 template<class X,class Y,class Z> double AFuncTemplate(X x,Y * y,Z & z) 19 { ...some code using x,y,z; return 0.0; } 20 }; 21 22In order to introspect the function template we use some theoretical valid instantiation of 23the member function template `AFuncTemplate`. An instantiation of a function template 24was previously explained in the topic [link sectti_function_templates "Introspecting function templates technique"]. 25 26For the purposes of illustration the instantiation we will use is: 27 28 double AFuncTemplate<int,long,bool>(int,long *,bool &) 29 30What we have now which the TTI will need in order to introspect the member function 31template `template<class X,class Y,class Z> double AFuncTemplate(X,Y *,Z &)` 32within the `AType` struct is: 33 34* The name of `AFuncTemplate` 35* The template parameters of `int,long,bool` 36* The enclosing type of `AType` 37* The return type of `double` 38* The function parameters of `int,long *,bool &` 39 40[heading Generating the metafunction] 41 42As with all TTI functionality for introspecting entities within a user-defined 43type introspecting a member function template is a two step process. The first 44process is using a macro to generate a metafunction. The macro for 45member function templates is [macroref BOOST_TTI_HAS_MEMBER_FUNCTION_TEMPLATE]. 46This macro takes the name of the member function template and the instantiated 47template parameters, the first two items in our list above: 48 49 BOOST_TTI_HAS_MEMBER_FUNCTION_TEMPLATE(AFuncTemplate,int,long,bool) 50 51An alternative form for compilers which do not support variadic macros, and which will 52also work with compilers which do support variadic macros, is to specify 53the template parameters of the instantiation as a single macro argument using a 54Boost PP array: 55 56 BOOST_TTI_HAS_MEMBER_FUNCTION_TEMPLATE(AFuncTemplate,(3,(int,long,bool))) 57 58The macro generates a metafunction based on the pattern of 59"has_member_function_template_'name_of_inner_member_function_template'", 60which in our example case would be `has_member_function_template_AFuncTemplate`. 61 62[heading Invoking the metafunction] 63 64To use this macro to test whether our member function template exists 65the metafunction the macro creates is invoked with the enclosing type, the instantiated return type, 66and the instantiated function parameters, with the resulting `value` being a compile time 67boolean constant which is `true` if the member function template exists, 68or `false` otherwise. There are two ways to do this. We can either 69use each of our needed types as separate parameters, with the function parameters 70being enclosed in an MPL forward sequence, or we can compose our needed type 71in the form of a pointer to member function type. In the first case we would have: 72 73 has_member_function_template_AFuncTemplate<AType,double,boost::mpl::vector<int,long *,bool &> >::value 74 75and in the second case we would have: 76 77 has_member_function_template_AFuncTemplate<double (AType::*) (int,long *,bool &)>::value 78 79Both invocations are equivalent in functionality. 80 81[heading Other considerations] 82 83The macro for generating the metafunction for introspecting member function templates 84also has, like other macros in the TTI library, a complex macro form where the 85end-user can directly specify the name of the metafunction to be generated. The 86corresponding macro is BOOST_TTI_TRAIT_HAS_MEMBER_FUNCTION_TEMPLATE, 87where the first parameter is the name of the metafunction to be generated, 88the second parameter is the member function template name, and the remaining parameters 89are the instantiated template parameters. For our example we could have 90 91 BOOST_TTI_TRAIT_HAS_MEMBER_FUNCTION_TEMPLATE(AMetafunctionName,AFuncTemplate,int,long,bool) 92 93or for the non-variadic macro form 94 95 BOOST_TTI_TRAIT_HAS_MEMBER_FUNCTION_TEMPLATE(AMetafunctionName,AFuncTemplate,(3,(int,long,bool))) 96 97which generates a metafunction whose name would be `AMetafunctionName`. 98 99In all other respects the resulting metafunction generated works exactly the same 100as when using the simpler macro form previously illustrated. 101 102If you do use the simple macro form, which generates the metafunction name 103from the name of the function template you are introspecting, you can use 104a corresponding macro, taking the name of the member function template as a single 105parameter, to create the appropriate metafunction name if you do not want to 106remember the pattern for generating the metafunction name. This macro name is 107`BOOST_TTI_HAS_MEMBER_FUNCTION_TEMPLATE_GEN` as in 108 109 BOOST_TTI_HAS_MEMBER_FUNCTION_TEMPLATE_GEN(AFuncTemplate) 110 111which would generate the name `has_member_function_template_AFuncTemplate`. 112 113When invoking the appropriate metafunction using the long form of an enclosing 114type, instantiated return type, and instantiated function parameters, a fourth 115template argument may optionally be given which holds a Boost FunctionTypes tag 116type to specify cv-qualification. This means you can add 'const', 'volatile', or 117both by specifying an appropriate tag type. An alternate to using the tag type 118is to specify the enclosing type as 'const', 'volatile', or both. 119As an example if you specify the tag type as 120'boost::function_types::const_qualified' or if you specify the enclosing 121type as 'const YourEnclosingType', the member function template which you are 122introspecting must be a const function template to match correctly. 123 124When invoking the metafunction using the shorter form of a pointer to member function 125you can simply add a possible cv-qualification, such as `const`, to the end of the 126pointer to member function syntax. 127 128[endsect] 129