templated method on T inside a templated class on TT : Is that possible/correct.

Posted by paercebal on Stack Overflow See other posts from Stack Overflow or by paercebal
Published on 2010-04-28T12:52:05Z Indexed on 2010/04/28 13:03 UTC
Read the original article Hit count: 176

Filed under:
|
|
|

I have a class MyClass which is templated on typename T. But inside, I want a method which is templated on another type TT (which is unrelated to T).

After reading/tinkering, I found the following notation:

template <typename T>
class MyClass
{
   public :
      template<typename TT>
      void MyMethod(const TT & param) ;
} ;

For stylistic reasons (I like to have my templated class declaration in one header file, and the method definitions in another header file), I won't define the method inside the class declaration. So, I have to write it as:

template <typename T>     // this is the type of the class
template <typename TT>    // this is the type of the method
void MyClass<T>::MyMethod(const TT & param)
{
   // etc.
}

I knew I had to "declare" the typenames used in the method, but didn't know how exactly, and found through trials and errors.

The code above compiles on Visual C++ 2008, but: Is this the correct way to have a method templated on TT inside a class templated on T?

As a bonus: Are there hidden problems/surprises/constraints behind this kind of code? (I guess the specializations can be quite amusing to write)

© Stack Overflow or respective owner

Related posts about template

Related posts about c++