Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a non template method defined in a template class

Tags:

c++

templates

When I want to define a method declared in a template class, but that the method doesn't depend on the template parameters, have I to define it in the include files as :

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

or may I define it in the cpp file as :

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

?

Thanks.

like image 235
Oodini Avatar asked Dec 09 '11 13:12

Oodini


People also ask

Can a non-template class have a template method?

A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.

Can a template class inherit from a non-template class?

Deriving from a non-template base class There is no requirement that your base class be a template. It is quite possible to have a template class inherit from a 'normal' class. This mechanism is recommended if your template class has a lot of non-template attributes and operations.

Can we pass non-type parameters to templates?

Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.

How do you implement a template function in C++?

A function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.


2 Answers

You'll need to define your method like this:

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

The reason for this is that the method actually is dependent on the template parameter. Remember that every method has access to the special variable this; during the method call this is actually a parameter passed to the method. this changes in type depending on the template parameter specified during object instantiation and, as such, all methods must be template methods in order to accommodate all forms of this.

like image 165
wrren Avatar answered Nov 16 '22 04:11

wrren


Well, if the method doesn't depend on template parameter, you can only do that with inheritance AFAIK.

The downside: more code + inheritance

The upside: (much) less code generated, depending on what parts of your code actually are template dependent. In the below example, method NonDependentMethod will only generate one assembly while DependentMethod will generate as many as there are different template parameters (just one in this case, but make a 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;
}
like image 40
velis Avatar answered Nov 16 '22 04:11

velis