Question

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()".

Was it helpful?

Solution

Function template partial specialization is not allowed. Do

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top