Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in the C++11 standard does it prohibit 'template <typename T> class A {...}; template <typename T> class A<int> {...};' (if anywhere)?

I am attempting to more fully grasp template syntax and semantics by imagining arcane constructs. I believe that the following syntax is not allowed by the C++11 standard:

template <typename T>
class A
{...};

// phony "specialization"
template <typename T>
class A<int>
{...};

However, I cannot find in the C++11 standard where this syntax is disallowed.

Am I correct that the syntax shown is disallowed by the C++11 standard? If so, where can it be found that the syntax is disallowed?

like image 962
Dan Nissenbaum Avatar asked Oct 20 '22 20:10

Dan Nissenbaum


1 Answers

I'm quite surprised that there is no explicit statement in 14.5.5 [temp.class.spec] saying that all template parameters of a class template partial specialization must be used in the template-argument-list. That would make template<class T> class A<int> invalid because T is not used in the template-argument-list <int>.

I think your phony specialization is only implicitly invalid due to the fact that you can never match it, so it can never be used. If you instantiate A<int> then that matches the primary template. It can't match your specialization, because that has an additional template parameter, T, which cannot be deduced (you suggest it could be provided by saying A<int><double> but that is not valid C++ syntax, so doesn't help).

I've asked the standards committee for clarification why your phony specialization is invalid (obviously it is, but I can't see where it says so).

like image 56
Jonathan Wakely Avatar answered Oct 22 '22 11:10

Jonathan Wakely