| Home | Libraries | People | FAQ | More

## Introspecting an inner class template

### Using the BOOST_TTI_HAS_TEMPLATE macro

The TTI macro `BOOST_TTI_HAS_TEMPLATE` introspects an inner class template of a class. The macro must specify, at the least, the name of the class template to introspect.

##### Two forms of introspection

There are two general forms of template introspection which can be used. The first is to find a class template with any number of only template type parameters ( template parameters starting with `class` or `typename` ). In this form only the name of the class template needs to be specified when invoking the macro. We will call this form of the macro the `template type parameters` form. An example of a class template of this form which could be successfully introspected would be:

    template<class X,typename Y,class Z,typename T> class AClassTemplate { /* etc. */ };

The second is to find a class template with specific template parameters. In this form both the name of the class template and the template parameters are passed to the macro.

We will call this form of the macro the `specific parameters` form. An example of a class template of this form which could be successfully introspected would be:

    template<class X, template<class> class Y, int Z> BClassTemplate { /* etc. */ };

When using the specific form of the macro, there are two things which need to be understood when passing the template parameters to the macro. First, the actual names of the template parameters passed are irrelevant. They can be left out completely or be different from the names in the nested class template itself. Second, the use of 'typename' or 'class', when referring to a template type parameter, is completely interchangeable, as it is in the actual class template itself.

##### Variadic and non-variadic macro usage

When using the BOOST_TTI_HAS_TEMPLATE macro we distinguish between compilers supporting variadic macros or not supporting variadic macros.

The programmer can always tell whether or not the compiler supports variadic macros by checking the value of the macro BOOST_PP_VARIADIC after including the necessary header file `boost/tti/has_template.hpp` in order to use the BOOST_TTI_TEMPLATE macro. A value of 1 indicates the compiler supports variadic macros while a value of 0 indicates the compiler does not support variadic macros.

Modern C++ compilers, in supporting the latest C++11 standard, normally support variadic macros. Even before the latest C++11 standard a number of C++ compilers already supported variadic macros. If you feel your compiler supports variadic macros and BOOST_PP_VARIADIC is 0 even after including `boost/tti/has_template.hpp`, you can predefine BOOST_PP_VARIADIC to 1 before including `boost/tti/has_template.hpp`.

##### Non-variadic macro usage

We start with syntax for compilers not supporting variadic macros since this syntax can also be used by compilers which do support variadic macros. The form for non-variadic macros always takes two macro parameters. The first macro parameter is always the name of the class template you are trying to introspect.

The second macro parameter, when using the `specific parameters` form of the macro, is the template parameters in the form of a Boost preprocessor library array data type. When using the `template type parameters` form of the macro the second macro parameter is BOOST_PP_NIL. If the second parameter is neither a Boost preprocessor library array data type or BOOS_PP_NIL you will get a compiler error if your compiler only supports non-variadic macros.

The non-variadic macro form for introspecting the class templates above using the `template type parameters` form would be:

    BOOST_TTI_TEMPLATE(AClassTemplate,BOOST_PP_NIL)
    BOOST_TTI_TEMPLATE(BClassTemplate,BOOST_PP_NIL)

Invoking the metafunction in the second case would always fail since the BClassTemplate does not have all template type parameters.

The non-variadic macro form for introspecting the class templates above using the `specific parameters` form would be:

    BOOST_TTI_TEMPLATE(AClassTemplate,(4,(class,typename,class,typename)))
    BOOST_TTI_TEMPLATE(BClassTemplate,(3,(class, template<class> class, int)))

You need to be careful using the non-variadic `specific parameters` form to specify the correct number of array parameters. This can sometimes be tricky if you have a template template parameter, or a non-type template parameter which has parentheses surrounding part of the type specification. In the latter case, when parentheses surround a comma ( ',' ), do not count that as creating another Boost PP array token. Two examples:

    template<void (*FunctionPointer)(int,long)> class CClassTemplate { /* etc. */ };
    template<template<class,class> class T> class DClassTemplate { /* etc. */ };

    BOOST_TTI_TEMPLATE(CClassTemplate,(1,(void (*)(int,long))))
    BOOST_TTI_TEMPLATE(DClassTemplate,(2,(template<class,class> class)))

In the case of using the macro to introspect CClassTemplate the number of Boost PP array parameters is 1, even though there is a comma separating the tokens in `void (*FunctionPointer)(int,long)`. This is because the comma is within parentheses.

In the case of using the macro to introspect DClassTemplate the number of Boost PP array parameters is 2, because there is a comma separating the tokens in `template<class,class> class T`.

##### Variadic macro usage

Having the ability to use variadic macros makes the syntax for using BOOST_TTI_TEMPLATE easier to specify in both the `template type parameters` form and the `specific parameters` form of using the macro. This is because variadic macros can take a variable number of parameters. When using the variadic macro form the first macro parameter is always the name of the class template you are trying to introspect. You only specify further parameters when using the `specific parameters` form of the macro, in which case the further parameters to the macro are the specific template parameters.

Introspecting the first class template above using the `template type parameters` form the variadic macro would be:

    BOOST_TTI_TEMPLATE(AClassTemplate)

Introspecting the other class templates above using the `specific parameters` form the variadic macros would be:

    BOOST_TTI_TEMPLATE(BClassTemplate,class,template<class> class, int)
    BOOST_TTI_TEMPLATE(CClassTemplate,void (*)(int,long))
    BOOST_TTI_TEMPLATE(DClassTemplate,template<class,class> class)

Here we have no problem with counting the number of tuple tokens for the Boost PP array, nor do we have to specify BOOST_PP_NIL if we are using the `template type parameters` form. Also for the specific parameters form we simply use the template parameters as the remaining tokens of the variadic macro.

##### The resulting metafunction

Using either form of the macro, whether using variadic or non-variadic syntax, the macro generates a metafunction called "has_template_'name_of_inner_class_template'".

The metafunction can be invoked by passing it the enclosing type to introspect.

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 is true or false depending on whether the inner class template exists or not.