In c++, it is not possible to create virtual template functions. Reason as I understand is that, we cannot create a base class with the virtual table with infinite number of matching functions. What I want to clarify is, whether this is a limitation of current c++ compilers? Or is it purposely enforced rule to avoid some other resulting problems?
When I tried to use virtual template functions in my code, compiler throws an error. "error: templates may not be ‘virtual’"
p.s. sample code
class A {
public:
template <typename T>
virtual void doStuff(T value) const {
cout << "A:doStuff " << value << endl;
}
virtual ~A() {}
};
class B : public A {
public:
template <typename T>
void doStuff(T value) const override {
cout << "B:doStuff " << value << endl;
}
};
int main() {
A* a1 = new B();
a1->doStuff(1);
A* a2 = new B();
a2->doStuff(1.1);
delete a1;
delete a2;
return 0;
}