C++ Template - Multiple types

template <class T>
template <class T2> 
void MyClass<T>::MyFunc2(T2* pData) 
{ 
  //...implementation goes here 
}

EDIT 2:

$14.5.2/1 - "A template can be declared within a class or class template; such a template is called a member template. A member template can be defined within or outside its class definition or class template definition. A member template of a class template that is defined outside of its class template definition shall be specified with the template-parameters of the class template followed by the template-parameters of the member template."


What you're doing is fine, try this out:

template <typename S,typename T>
struct Structure
{
  S s ;
  T t ;

} ;

int main(int argc, const char * argv[])
{
  Structure<int,double> ss ;
  ss.s = 200 ;
  ss.t = 5.4 ;

  return 1;
}

This code works. If you're getting strange errors, see if you forward declared Structure using only 1 template parameter (that's what I was doing).


Try this one :

template <class T, class T2>
class MyClass
{
public:
    static void MyFunc2(T2* data);
};

template <class T, class T2>
void MyClass<T, T2>::MyFunc2(T2* pData)
{
    cout << "dummy " << *pData<< "\n";
}

Then

int main()
{
    cout << "Hello World!\n"; 
    MyClass<int, int> a;
    int *b = (int*)malloc(sizeof(int));
    *b = 5;
    a.MyFunc2(b);
}

Output

Hello World!
dummy 5

Tags:

C++

Templates