Search code examples
c++templatesexplicit-instantiation

template template function instantiation


template < typename T >
class CLASS_TEMPLATE { } ;

template < template < typename T > class CLASS >
void funcI ( ) { } 

template void funcI < CLASS_TEMPLATE > () ;

How does compiler instantiate the function, if he does not have any hint about CLASS_TEMPLATE template arguments?


My assumptions about template template were wrong.

Formal template parameter of funcI is template with one template parameter.

template < template < typename... > class CONTAINER >
void funcII ( ) 
{
   CONTAINER< int > container0 ;
   CONTAINER< float > container1 ;
   /* ... */
}

template void funcII < std::vector > () ; will instantiate funcII template as { std::vector< int > container0 ; std::vector< float > container1 ; /* ... */ } ;


Solution

  • By explicitly instantiating

    template void func < CLASS_TEMPLATE > () ;
    

    You are effectively doing this:

    template <>
    void func<CLASS_TEMPLATE>() { }
    

    There is no need to have the template parameters for CLASS_TEMPLATE to do that.