C ++ - member-function class prefix-label (also template)

Given the following class declaration:

template <int N>
class MyClass {

public:
    MyClass();
    ~MyClass();
};

      

and the following definitions:

template <int N> 
MyClass<N>::MyClass() {
}

template <int N>
MyClass<N>::~MyClass() {
}

      

I have two questions:

  • Do I have to write MyClass::

    every time (with and without templates)?
  • Should I write template <int N>

    before every constructor and destructor?
+3


source to share


1 answer


From [class.mfct]:

If the definition of a member function is lexically outside the scope of its class definition, the name of the member function must be assigned to its class name using the :: operator.



If you are defining your member functions outside of the class definition, you have to write MyClass::

every time. Since it MyClass

is a template, you need to define that you are defining member functions template <int N> MyClass<N>

. The answer to both of your questions is yes (although for # 2 this is for every member function, not just constructor and destructor).

+2


source







All Articles