Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Introspecting static member function template

We can introspect a static member function template of a user-defined type using the TTI functionality we shall now explain.

A static member function template is a function template that is a static memmber of a user-defined type. An example of a static member function template would be:

struct AnotherType
  {
  template<class X,int Y> static int AStaticFuncTemplate(X x) { ...some code using x; return Y; }
  };

In order to introspect the function template we use some theoretical valid instantiation of the static member function template AStaticFuncTemplate. An instantiation of a function template was previously explained in the topic "Introspecting function templates technique".

For the purposes of illustration the instantiation we will use is:

int AStaticFuncTemplate<char,483>(char)

What we have now which the TTI will need in order to introspect the static member function template template<class X,int Y> static int AStaticFuncTemplate(X) within the AnotherType struct is:

Generating the metafunction

As with all TTI functionality for introspecting entities within a user-defined type introspecting a static member function template is a two step process. The first process is using a macro to generate a metafunction. The macro for static member function templates is BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION_TEMPLATE. This macro takes the name of the member function template and the instantiated template parameters, the first two items in our list above:

BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION_TEMPLATE(AStaticFuncTemplate,char,483)

An alternative form for compilers which do not support variadic macros, and which will also work with compilers which do support variadic macros, is to specify the template parameters of the instantiation as a single macro argument using a Boost PP array:

BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION_TEMPLATE(AStaticFuncTemplate,(2,(char,483)))

The macro generates a metafunction based on the pattern of "has_static_member_function_template_'name_of_inner_static_member_function_template'", which in our example case would be has_static_member_function_template_AStaticFuncTemplate.

Invoking the metafunction

To use this macro to test whether our static member function template exists the metafunction the macro creates is invoked with the enclosing type, the instantiated return type, and the instantiated function parameters, with the resulting value being a compile time boolean constant which is true if the static member function template exists, or false otherwise. There are two ways to do this. We can either use each of our needed types as separate parameters, with the instantiated function parameters being enclosed in an MPL forward sequence, or we can compose our needed type in the form of an enclosing type and a function type. In the first case we would have:

has_static_member_function_template_AStaticFuncTemplate<AnotherType,int,boost::mpl::vector<char> >::value

and in the second case we would have:

has_static_member_function_template_AStaticFuncTemplate<AnotherType,int (char) >::value

Both invocations are equivalent in functionality.

Other considerations

The macro for generating the metafunction for introspecting member function templates also has, like other macros in the TTI library, a complex macro form where the end-user can directly specify the name of the metafunction to be generated. The corresponding macro is BOOST_TTI_TRAIT_HAS_STATIC_MEMBER_FUNCTION_TEMPLATE, where the first parameter is the name of the metafunction to be generated, the second parameter is the static member function template name, and the remaining parameters are the instantiated template parameters. For our example we could have

BOOST_TTI_TRAIT_HAS_STATIC_MEMBER_FUNCTION_TEMPLATE(AMetafunctionName,AStaticFuncTemplate,char,483)

or for the non-variadic macro form

BOOST_TTI_TRAIT_HAS_STATIC_MEMBER_FUNCTION_TEMPLATE(AMetafunctionName,AStaticFuncTemplate,(2,(char,483)))

which generates a metafunction whose name would be AMetafunctionName.

In all other respects the resulting metafunction generated works exactly the same as when using the simpler macro form previously illustrated.

If you do use the simple macro form, which generates the metafunction name from the name of the function template you are introspecting, you can use a corresponding macro, taking the name of the static member function template as a single parameter, to create the appropriate metafunction name if you do not want to remember the pattern for generating the metafunction name. This macro name is BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION_TEMPLATE_GEN as in

BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION_TEMPLATE_GEN(AStaticFuncTemplate)

which would generate the name has_static_member_function_template_AStaticFuncTemplate.

When invoking the appropriate metafunction using the long form of an enclosing type, instantiated return type, and instantiated function parameters, a fourth template argument may optionally be given which holds a Boost FunctionTypes tag type. This optional template argument is of much less use for static member function templates than for non-static member function templates since static member function templates, like static member functions, can not have cv-qualifications. which a number of Boost FunctionTypes tags provide. Nonetheless this optional Boost FunctionTypes tag is available for end-user use and may come in handy in certain rare cases, as when some calling convention qualification for the static member function template needs to be specified.

When invoking the metafunction using the shorter form of an enclosing type and a function type any possibly needed qualification can be added to the end of the function type.


PrevUpHomeNext