20
template <size_t size, typename ...Params>
void doStuff(Params...) {
}

template <>
void doStuff<size_t(1), int, bool>(int, bool) {

}

int main(int, char**) {
    doStuff<1,int,bool>(1, false);
    return 0;
}

This doesn't compile, the second doStuff declaration gives me error: template-id ‘doStuff<1u, int, bool>’ for ‘void doStuff(int, bool)’ does not match any template declaration but it clearly matches the first declaration with variadic template arguments.

How to specialize variadic templates?

1 Answer 1

12

The syntax is correct (afaik, and clang++ accepts it), but your compiler is probably just not up2date yet.

If you use gcc, its variadic template support is quite incomplete, and even very recent svn versions don't support specialization yet (That is just how it is when you use bleeding edge technology, and sadly gcc implemented only a very early incomplete variadic template proposal and since then didn't keep up much, while clang started pretty late, but got pretty complete)

3
  • I use gcc 4.5.2. Thanks for your answer (I'm using template overloading instead now).
    – coyotte508
    Oct 14, 2011 at 12:23
  • Alternatively you could wrap the function in a template<..> struct Do { static void Stuff(..) { } };.
    – Kerrek SB
    Oct 14, 2011 at 12:24
  • @coyotte508: That is probably the best for function templates, some people even say that function template specialization is always wrong...
    – PlasmaHH
    Oct 14, 2011 at 12:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.