wrote:
> dj wrote:
>> I somehow understand why member function templates cannot be virtual,
>> but my question is how I could achieve something similar to this:
>>
>> class A {
>> public:
>> template<typename T> virtual f() = 0;
>> }
>>
>> class B : public A {
>> public:
>> template<typename T> f();
>> }
>>
>> That is, I would like to force subclasses to implement a templated
>> member function f.
>
> Forcing subclasses isn't the problem. How are you going to convince
> the compiler to instantiate B::f<int>? The compiler only sees a call to
> A::f<int>.
>
> HTH,
> Michiel Salters
>
I think I understand what you are saying. If I use:
B b;
A* pb = &b;
pb->f<int>();
then the compiler would instantiate A::f<int>, but not B::f<int>. If f
is pure this would be illegal anyway, so that is probably why templated
virtuals are not allowed in the first place.
However, let me repeat my question - how could I force the subclasses to
implement some function f (which is what i use the pure virtual for),
which itself should be templated? I could declare all possible function
prototypes instead of using a template, but is there a more elegant
solution?