Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ compiler error on template specialization

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

like image 897
user231536 Avatar asked Dec 04 '25 01:12

user231536


1 Answers

Function template partial specialization is not allowed. Do

template <int D>  
void test () {cout << C<D>::foo() << endl;}
like image 94
log0 Avatar answered Dec 05 '25 15:12

log0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!