A member of an explicitly specialized class is not implicitly instantiated
from the member declaration of the primary template. You have to
explicitly define members of a class template specialization. You
define members of an explicitly specialized template class as you would normal
classes, without the template<> prefix. In addition, you
can define the members of an explicit specialization inline; no special
template syntax is used in this case. The following example
demonstrates a class template specialization:
template<class T> class A { public: void f(T); }; template<> class A<int> { public: int g(int); }; int A<int>::g(int arg) { return 0; } int main() { A<int> a; a.g(1234); }
The explicit specialization A<int> contains the member function g(), which the primary template does not.
If you explicitly specialize a template, a member template, or the member of a class template, then you must declare this specialization before that specialization is implicitly instantiated. For example, the compiler will not allow the following code:
template<class T> class A { }; void f() { A<int> x; } template<> class A<int> { }; int main() { f(); }
The compiler will not allow the explicit specialization template<> class A<int> { }; because function f() uses this specialization (in the construction of x) before the specialization.
Related References