I have heard that C++ class member function templates can't be virtual. Is this true?
If they can be virtual, what is an example of a scenario in which one would use such a function?
I have heard that C++ class member function templates can't be virtual. Is this true?
If they can be virtual, what is an example of a scenario in which one would use such a function?
From C++ Templates The Complete Guide:
C++ doesn't allow virtual template member functions right now. The most likely reason is the complexity of implementing it. Rajendra gives good reason why it can't be done right now but it could be possible with reasonable changes of the standard. Especially working out how many instantiations of a templated function actually exist and building up the vtable seems difficult if you consider the place of the virtual function call. Standards people just have a lot of other things to do right now and C++1x is a lot of work for the compiler writers as well.
When would you need a templated member function? I once came across such a situation where I tried to refactor a hierarchy with a pure virtual base class. It was a poor style for implementing different strategies. I wanted to change the argument of one of the virtual functions to a numeric type and instead of overloading the member function and override every overload in all sub-classes I tried to use virtual template functions (and had to find out they don't exist.)
To answer the second part of the question:
This is not an unreasonable thing to want to do. For instance, Java (where every method is virtual) has no problems with generic methods.
One example in C++ of wanting a virtual function template is a member function that accepts a generic iterator. Or a member function that accepts a generic function object.
The solution to this problem is to use type erasure with boost::any_range and boost::function, which will allow you to accept a generic iterator or functor without the need to make your function a template.
Virtual Function Tables
Let's begin with some background on virtual function tables and how they work (source):
My problem, or how I came here
I'm attempting to use something like this now for a cubefile base class with templated optimized load functions which will be implemented differently for different types of cubes (some stored by pixel, some by image, etc).
Some code:
What I'd like it to be, but it won't compile due to a virtual templated combo:
I ended up moving the template declaration to the class level. This solution would have forced programs to know about specific types of data they would read before they read them, which is unacceptable.Solution
warning, this isn't very pretty but it allowed me to remove repetitive execution code
1) in the base class
2) and in the children classes
Note that LoadAnyCube is not declared in the base class.
Here's another stack overflow answer with a work around: need a virtual template member workaround.
There is a workaround for 'virtual template method' if set of types for the template method is known in advance.
To show the idea, in the example below only two types are used (
int
anddouble
).There, a 'virtual' template method (
Base::Method
) calls corresponding virtual method (one ofBase::VMethod
) which, in turn, calls template method implementation (Impl::TMethod
).One only needs to implement template method
TMethod
in derived implementations (AImpl
,BImpl
) and useDerived<*Impl>
.Output:
NB:
Base::Method
is actually surplus for real code (VMethod
can be made public and used directly). I added it so it looks like as an actual 'virtual' template method.No they can't. But:
has much the same effect if all you want to do is have a common interface and defer implementation to subclasses.