Member function syntax in class template specialization

I have a class template, call it A , which has the abc() member function:

 template <typename T> class A{ public: T value; void abc(); }; 

I can implement the abc() member function outside the class declaration using the following syntax:

 template <typename T> void A<T>::abc() { value++; } 

What I want to do is create a template specialization for this class, say int .

 template <> class A<int>{ public: int value; void abc(); }; 

Question: what is the correct syntax for implementing abc() for a specialized class?

I tried using the following syntax:

 template <> void A<int>::abc() { value += 2; } 

However, this does not compile.

+4
source share
2 answers
 void A<int>::abc() { value += 2; } 

since A<int> is an explicit specialisation of A<T> .

http://liveworkspace.org/code/982c66b2cbfdb56305180914266831d1

n3337 14.7.3 / 5

Members of an explicitly specialized class template are defined in the same way as members of regular classes, and without using the template syntax <> .

[Example:

 template<class T> struct A { struct B { }; template<class U> struct C { }; }; template<> struct A<int> { void f(int); }; void h() { A<int> a; af(16); } // A<int>::f must be defined somewhere // template<> not used for a member of an // explicitly specialized class template void A<int>::f(int) { /∗ ... ∗/ } 

+4
source

Remove template<> :

 void A<int>::abc() { value += 2; } 
+4
source

Source: https://habr.com/ru/post/1432223/


All Articles