Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Templating vs Inheritance

I just realize how hard asking questions can be... Hopefully I can give examples that are both, sufficiently precise to demonstrate my problem and short enough not to mess everything up... At least there's the possibility to edit.

So this is kind of my situation at the moment. Of course I altered it a bit in terms of logic / structure (and in terms of naming anyway) trying to focus on the essence of my question:

// MyClass deals with lists (actually several data structures) of the
// type MyType which should support different types and has to be 
// efficiently dealt with. Templating seems just right here
class MyClass
{
  ...

  void doSomething<class MyType>(vector<MyType> someList);

  ...

  // At some point I have to extract elements of the type MyType.
  // The extractor obviously depends on MyType but it is not possible to
  // Create a general version that could use templates itself 
  // (unless I use a specialization for each possible MyType)

  // I am stuck between these two alternatives:

  // Possibility1:
  // Let the client pass the right extractor and template it.
  template<class Extractor, class MyType>
  void extract(const Extractor& extractor, const string& source, 
               vector<MyType>* dest)
  {
     extractor.extract(source, dest);
  }

  // Possibility2:
  // Use a member _extractor of some base type that has to be set
  // to a specialization. The ExtractorBase has a virtual method
  // template<T> void extract(const string& source, vector<T>* myType) = 0
  // with no definition that is only defined in subclasses wrt certain
  // postings.
  ExtractorBase _extractor;

  template<class MyType>
  void extract(const string& source, vector<MyType>* dest)
  {
     _extractor.extract(source, dest);
  }
}

At the moment I would prefer possibility1, because I don't have to mess with inheritance in Extractor for all the variants of MyType and associated Extractor I want to try out in the future.

On the other hand extractors may require complex code and several members (something like huge maps that map certain inputs on certain values). So there will be no performance gain by using templates. Particularly using header file only Extractors and probably even functors that are supposed to get inlined, are kind of out of the question. In the past, this has been a strong pointer to me that templating will only increase code complexity (having to deal with instantiation, making life harder for client code, etc.) and that I should try to avoid it altogether.

Or is there a third possibility I didn't think of at all?

like image 854
b.buchhold Avatar asked Jul 06 '11 08:07

b.buchhold


People also ask

Can template class be inherited?

Inheriting from a template classIt is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.

When should I use template classes?

Templates can be used in conjunction with abstract datatypes in order to allow them to handle any type of data. For example, you could make a templated stack class that can handle a stack of any datatype, rather than having to create a stack class for every different datatype for which you want the stack to function.

When should I use a template C++?

Templates are very useful when implementing generic constructs like vectors, stacks, lists, queues which can be used with any arbitrary type. C++ templates provide a way to re-use source code as opposed to inheritance and composition which provide a way to re-use object code.

Can you template a namespace?

The new entity that is proposed in this paper is called a namespace template. It is a template used to create one or more namespaces. A usage of that template would be a “namespace template instantiation” which is a namespace.


1 Answers

It's better to go with the first option. It's cleaner and maintainable.

Because from your comments, I am making out that you are making a wrong assumption for the 2nd option:

// The ExtractorBase has a virtual method
// template<T> void extract(const string& source, vector<T>* myType) = 0;

NO. That's not possible; a template function can never be virtual. So to implement the 2nd option you have to opt for some dirty and hard to maintain ways, which is not a good idea.

like image 162
iammilind Avatar answered Sep 30 '22 08:09

iammilind