Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does more specialized mean when specializing a primary template?

Tags:

c++

templates

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
like image 889
Thomson Avatar asked Oct 15 '25 12:10

Thomson


1 Answers

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.

like image 101
songyuanyao Avatar answered Oct 18 '25 11:10

songyuanyao



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!