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?
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.