I am facing a design problem where (in C ++) I would like a template member function (of a non-classical class) to be virtual, and I wonder if there is a nice, elegant way to solve the problem.
The script is on, I have machines that handle common elements. I use an abstract base class for machines with a virtual process function (Item) so that each machine can define its own unique processing method. The problem is that elements are also βuniversalβ because they provide specific interfaces for how they can be processed. For reasons (mainly for performance ... no vtable overhead), I would like to use compile-time polymorphism for these elements. So now every machine will have such an interface as:
class Machine { public: template <typename T> virtual void process(T& item) = 0; };
However, this is not possible in C ++, because template member functions cannot be virtual. of course, I can make a machine class a template like Item T, but it adds more headaches for me in a larger design scheme, and in fact no other part of the Machine class depends on Item ... this is just an argument to the process () function ,
Is there a better way around this or any suggestions on how to provide such a common family of machines that handle a family of common elements (where the elements use compile-time polymorphism). I am with a deep end in terms of my design.
Rate any suggestions
c ++ virtual templates
innocent_bystander
source share