Implementing a method without a template defined in a template class

When I want to define a method declared in a template class, but that the method does not depend on template parameters, should I define it in include files as:

template class<typename T> void MyClass::myMethod() { ... } 

or I can define it in the cpp file as:

 void MyClass::myMethod() { ... } 

?

Thanks.

+8
c ++ templates
source share
5 answers

You will need to define your method as follows:

 template class<typename T> void MyClass<T>::myMethod() { // Method Body } 

The reason for this is because the method actually depends on the template parameter. Remember that each method has access to a special this variable; while calling the method, this actually the parameter passed to the method. this changes depending on the template parameter specified at the time the object was created, and thus all methods must be template methods to accommodate all this forms.

+9
source share

Put it in the header file.

The member function is still a member of the class template, and you will need to write:

 template <typename T> void MyClass<T>::myMethod() { /* ... */ } 

Like all template member functions, this is not yet a real function; it generates a real function only when instantiating the class. Thus, complete template definitions should be visible to anyone who instantiates the template, and the usual way to do this is to put everything in the header.

(Note that member functions of class templates are themselves considered function templates, and you can really specify them: template <> void MyClass<int>::myMethod() { } .)

+4
source share

Well, if the method is independent of the template parameter, you can only do this with AFAIK inheritance.

Disadvantage: more code + inheritance

Potential: (much) less generated code, depending on which parts of your code actually depend on the template. In the example below, the NonDependentMethod method will generate only one assembly, and DependentMethod will generate as many different template parameters (only one in this case, but make MyClass<float> , and you have two, etc.).

 #include <iostream> using namespace std; class MyClassBase { public: void NonDependentMethod(); }; template <class T> class MyClass : public MyClassBase { public: void DependentMethod(T param); }; void MyClassBase::NonDependentMethod() { cout << "NonDependentMethod << endl; } template<class T> void MyClass<T>::DependentMethod(T param) { cout << "DependentMethod " << param << endl; } int main() { // your code goes here MyClass<int> cls; cls.NonDependentMethod(); cls.DependentMethod(2); return 0; } 
+4
source share

You need to do it like this:

 template class<typename T> void MyClass<T>::myMethod() { ... } 

This is not a template method, it is a class.

You can have a templated method in a non-standardized class, a non-templated method in a templated class (your case) and a templated method in a templated class, and, of course, not a templated method in a non-templated class.

+1
source share

You must define it in another way. The method itself cannot (directly) depend on the template parameter, but the class to which certaily belongs does not? Thus, the method indirectly also depends on the template parameter:

 template class<typename T> void MyClass<T>::myMethod() { // ^^^ -- note ... } 
0
source share

All Articles