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
// 开发者_JS百科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?
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.
You have a third option, provide a constructor for MyType
which knows how to construct itself from a std::string
. Or better yet a pair of iterators, so if you need to construct a sequence of MyType
s from the string, you can use the range.
I see first possibility as more flexible.
In the 2nd possibility I don't see the interest of unneeded encapsulating extractor as class member. You also have more coupling between MyClass and Extractor, which is not a good thing. templating reduces coupling (in some way), so if you have the choice it's a better one.
This sounds like a case for the Strategy Pattern -- your class has an operation whose implementation may vary.
Here's the trade-offs I see in the different approaches.
The template solution will avoid having to declare an abstract interface class, and using the vtbl
to figure out which implementation to use. But it will force you to lock into the application at compile-time.
The inheritance solution will force you to declare an abstract interface class, and take the performance hit of looking up the implementation in the vtbl.
But it will allow you to choose the extracting implementation at run time.
Not knowing how critical performance is for your application and how flexible you need to be, I would probably choose the inheritance solution, since I like the clarity of defining the interface in an abstract class and coding to that.
精彩评论