2

Possible Duplicate:
Template partial specialization with multiple template argument error

Is is possible to specialize e template class as function level?

I'll give you and example with what I want to achieve, but I get compilation errors:

template<typename T1, typename T2>
class C
{
    public:
        void f();
};

template<typename T1, typename T2>
void C<T1, T2>::f()
{
}

template<typename T1, int>
void C<T1, int>::f()
{
}

Errors:

template argument list following class template name must list parameters in the order used in template parameter list
'void C<T1,T2>::f(void)' : function template has already been defined
'C<T1,T2>': template parameter 'T2' is incompatible with the declaration
Community
  • 1
  • 1
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211

1 Answers1

1

If you want to make the behaviour of C::f dependent on T2 you can try to put its implementation into a separate class like the following:

template <typename T>
class C_aux
{
public:
    void f() {}
};

template <>
class C_aux<int>
{
    void f() {}
};

template <typename T1, typename T2>
class C
{
    public:
        void f();
};

template <typename T1, typename T2>
void C<T1, T2>::f()
{
    C_aux<T2>::f();
}

Depending on the details of your needs C_aux::f has to take extra parameters. Or you can let class C inherit from class C_aux or vice versa.

coproc
  • 6,027
  • 2
  • 20
  • 31