views:

108

answers:

2

I would like to specialize a template method for a class C that is itself templated by an int parameter.

How do I do this?

template <int D=1>
class C {
    static std::string  foo () { stringstream ss; ss << D << endl; return ss.str();}    
};

template <class X>
void test() { cout << "This is a test" << endl;}

template <>
template <int D>
void test<C<D> > () {cout << C<D>::foo() << endl;}

The specialization for test() fails with "Too many template parameter lists in declaration of void test()".

+1  A: 

You don't want the first template<> on your partial specialisation of test<C<D>>. Moreover, you can only partially specialise class templates, not function templates. Something like this might work:

template <class X>
struct thing
{
    static void test() { cout << "This is a test" << endl;}
};

template <int D>
struct thing<C<D>>
{
    static void test() {cout << C<D>::foo() << endl;}
};

If your function template took an argument, and used that to infer the template argument, then you could get a similar effect using overloading, something like:

template <class X>
void test(const X&) { cout << "This is a test" << endl;}

template <int D>
void test(const C<D>&) {cout << C<D>::foo() << endl;}

test(3);  // calls first version
test(C<3>()); // calls second version
Mike Seymour
That does not work. Now I get a message:partial specialization 'test<C<D> >' is not allowed
Oh yes, I'd forgotten that. You can only partially specialise class templates.
Mike Seymour
+1  A: 

Function template partial specialization is not allowed. Do

template <int D>  
void test () {cout << C<D>::foo() << endl;}
Ugo