3

C++ template specialization rules mentioned specialization has to be more specialized than the primary template. Following #1 code snippet causes compilation error which says the second line is not more specialized, but the last snippet (#2) works which looks very close to #1. Both code snippets specialized int N as 0, so why the first snippet is complained as "not more specialized"?

// #1
template<int N, typename T1, typename... Ts> struct B;
template<typename... Ts> struct B<0, Ts...> { }; // Error: not more specialized
// #2
template<int N, typename T1, typename... Ts> struct B;
template<typename T, typename... Ts> struct B<0, T, Ts...> { }; // this works
Thomson
  • 20,586
  • 28
  • 90
  • 134

1 Answers1

2

For more specialized:

Informally "A is more specialized than B" means "A accepts a subset of the types that B accepts".

Whatever types the specialization could accept are supposed to be subset of types the primary template could accept. But for B<0>, it could be accepted by the specialization only, while the primary template can't, because the template parameter T1 is essential.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405