Tags

The following code shows how to provide an out of class definition for a template function in a template class. The whole thing consists of using two template directives in the definition, the first to specify the class’s template parameters and the second to specify the function’s template parameters.

#include<iostream>
#include <string>

template<typename ClassType>
struct Foo
{
   void f(ClassType i);

   template<typename MethodType>
   void g(ClassType i, MethodType j); 
};

template<typename ClassType>
void Foo<ClassType>::f(ClassType i)
{
   std::cerr << "In f(" << i << ")\n";
}

template<typename ClassType>
template<typename MethodType>
void Foo<ClassType>::
g(ClassType i, MethodType j)
{
   std::cerr << "In g(" << i << ", " << j << ")\n";
}

int main()
{
   Foo<int> f;
   f.f(1);
   f.g(1, 2.2);
};
Advertisements