Abruzzo Forte e Gentile
Abruzzo Forte e Gentile

Reputation: 14869

class template with multiple template parameter

I am trying to define the same template class in several version, each with a different template parameter. The compiler is giving me the error

 previous declaration ‘template<class T1> class A’ used

Is there a way to circumvent this problem? Unfortunately I cannot use Cx++11 where I can assign a default template parameter. What is a good practice to tackle this issue? I am using old g++4.4.7

#include <iostream>

template <class T1>
class A{};

template <class T1,class T2>
class A{};

int main() { return 0; }

Upvotes: 3

Views: 168

Answers (2)

vsoftco
vsoftco

Reputation: 56547

If you cannot use C++11 variadic templates, you can use a "mock" type

struct NoType{};

template <class T1, class T2 = NoType> // or class T2 = void, doesn't really matter
class A{};

then specialize it as needed.

PS: as @Barry mentioned in the comment, default template parameters for classes ARE ALLOWED in pre C++11 . The novelty in this respect introduced by C++11 is to allow default template parameters in functions.

Upvotes: 3

Kerrek SB
Kerrek SB

Reputation: 476990

Make a variadic template and add partial specializations:

template <typename ...> class A;      // leave primary template undefined

template <typename T> class A<T>
{
    // ...
};

template <typename T1, typename T2> class A<T1, T2>
{
    // ...
};

Upvotes: 5

Related Questions