I’ve two functions
MultiplyVerison1(T x, T y); // in class A
MultiplyVersion1(T x, T y); // in class B
Above functions are in separate non-template classes.
Now, as part of refactoring I’m trying to create a base class of A and B and creating a pure virtual MultiplyVersion1 but a templte function cannot be marked virtual.
So, how can we achieve the same with template functions?
You can’t. There’s no way to call a function template in a derived class through a pointer-to-base, that’s what “function templates cannot be virtual” means.
You can think of this as being because it’s the call that triggers the instantiation of the function template with a particular type
T— if you call it withint, but the dynamic type of the object you’re calling it on isn’t known until runtime (whether it’s A or B or something else), then there’s no way for the compiler to know that it needs to instantiateA::MultiplyVersion1<int>orB::MultiplyVersion1<int>or something else. Actually there’s more to it than that, but I think that’s enough.You can bodge around particular cases, but you won’t get the full effect of a virtual function. Something like:
Now when you call
MultiplyVersion1<int>via a pointer-to-base, bothA::MultiplyVersion1<int>andB::MutiplyVersion1<int>are instantiated. But of course you can’t easily add new derived classes, which is a serious restriction.You could also re-consider whether you really need dynamic polymorphism at all, but that depends entirely on how you’re planning to use that base class. You seem to have done OK without it so far.
If all you want from the base class is code re-use for some other functions, then you don’t need dynamic polymorphism. Leave
MultiplyVersion1out of the base class entirely (and maybe don’t inherit publicly from theBase, instead inherit privately and bring in the functions you want to re-use withusingstatements). If the functions you want to define for re-use callMultiplyVersion1, then consider simulated dynamic binding via CRTP: