Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template specialization of a single method from a templated class

Always considering that the following header, containing my templated class, is included in at least two .CPP files, this code compiles correctly:

template <class T> class TClass  { public:   void doSomething(std::vector<T> * v); };  template <class T> void TClass<T>::doSomething(std::vector<T> * v) {   // Do something with a vector of a generic T }  template <> inline void TClass<int>::doSomething(std::vector<int> * v) {   // Do something with a vector of int's } 

But note the inline in the specialization method. It is required to avoid a linker error (in VS2008 is LNK2005) due to the method being defined more then once. I understand this because AFAIK a full template specialization is the same as a simple method definition.

So, how do I remove that inline? The code should not be duplicated in every use of it. I've searched Google, read some questions here in SO and tried many of the suggested solutions but none successfully built (at least not in VS 2008).

Thanks!

like image 452
Chuim Avatar asked Nov 12 '09 16:11

Chuim


People also ask

What is meant by template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What does template <> mean in C++?

What Does Template Mean? A template is a C++ programming feature that permits function and class operations with generic types, which allows functionality with different data types without rewriting entire code blocks for each type.

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.

What is the specialty of a template function give example?

Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type.


2 Answers

As with simple functions you can use declaration and implementation. Put in your header declaration:

template <> void TClass<int>::doSomething(std::vector<int> * v); 

and put implementation into one of your cpp-files:

template <> void TClass<int>::doSomething(std::vector<int> * v) {  // Do somtehing with a vector of int's } 

Don't forget to remove inline (I forgot and thought this solution will not work :) ). Checked on VC++2005

like image 112
maxim1000 Avatar answered Sep 19 '22 14:09

maxim1000


You need to move specialization definition to CPP file. Specialization of member function of template class is allowed even if function is not declared as template.

like image 31
BostonLogan Avatar answered Sep 20 '22 14:09

BostonLogan