3

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

2 Answers 2

2

Function template partial specialization is not allowed. Do

template <int D>  
void test () {cout << C<D>::foo() << endl;}
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

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.
What if C == std::tuple? And D == Args... variadic template?))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.