2

I have a class Foo that provides some sort of functionality. In order to maintain modularity, Foo is to be an interface (That is, a C++ class with only abstract methods), and an implementation of Foo can choose how exactly to implement the functionality. However, my interface includes a template method. That is,

class Foo
{
public:
        template<class T>
        void functionality(const T&);
};

It is impossible in C++ to have template methods virtual. Is there any technique that can achieve a similar result (Modularity and Polymorphism) with template methods?

Edgepo1nt
  • 303
  • 2
  • 13
  • 1
    Do you have a finite, pre-determined set of types you need this to work with? Are there any properties of `T`, like "must be an integer-like type" or "must be a functor-like type" or "must be a number-like type", that you can provide us? Do you require "3rd party" extensions via traits classes or the like support? What, *exactly*, does `functionality` do? Exactly how important is performance in this case? (and not just "I like things fast": ie "this function has to run once per pixel on a 7 Mpx image at 60 hz"). Does the algorithm basically ignore the value of `T` acting as a carrier? – Yakk - Adam Nevraumont Apr 19 '13 at 17:57
  • http://stackoverflow.com/questions/7968023/c-virtual-template-method – didierc Apr 19 '13 at 17:58
  • http://stackoverflow.com/questions/2354210/can-a-member-function-template-be-virtual – didierc Apr 19 '13 at 18:01
  • Foo implements a `send` and `recv` messages from N different processes. My initial implementation is using a message queue for each message type, and T is to be classes that can be sent as messages (All such classes extend an abstract class `Message`). The reason such a `send` and `recv` methods are template is so that queue management can be set automatically in compile time with some run-time registration of message types. My wish is, if for some reason in the future one wants the communication to be over the internet rather than IPC. – Edgepo1nt Apr 19 '13 at 18:05
  • There are some limited techniques. You can have a fixed set of derived classes and an unlimited set of potential template instantiations, or you can have an unlimited set of potential derived classes and a fixed set of template instantiations, but you can't have both sets unlimited. Pick one. – n. m. could be an AI Apr 19 '13 at 18:21
  • 1
    Imagine a client invents a new type `Bob` that derives from `Message`. The server, complied years ago, would have no knowledge of `Bob`. So it could not have a queue created -- it cannot have a queue created for every possible type ever created in the future, for there are an unbounded number of them! Now, you can do this if you somehow named each of those classes uniquely, and nothing in the server cared about the details of the `Message` other than the `Message` interface, but that doesn't require a template interface now does it? – Yakk - Adam Nevraumont Apr 19 '13 at 18:24
  • @Yakk Of course, but in that case, the message classes must be created in a way that such uniqueness of messages would be in `Message` interface. In my case, that is rather strange as the definition of messages is declared by the parties conducting a protocol rather than the networking that is conducted when sending message (The server). However, I solved my problem by giving up template support and use RTTI to increase modularity of Communication and Messages being apart. Thank you everyone! ;) – Edgepo1nt Apr 20 '13 at 14:15

1 Answers1

1

You can't have run-time polymorphism mixed directly with templating. However another layer of indirection might help. Here's a contrived example:

template <typename T>
inline void *New( void )
{
  return new T( );
}

This function will allow the user to dynamically allocate any type of object so long as a default constructor exists. Now take a function pointer and assign it to this new function:

void *(*NewFuncPtr)( void ) = New<int>;
int *i = (int *)NewFuncPtr( );

The function pointer itself isn't templated, but can point to a function that is templated. Perhaps this can solve your situation too.

RandyGaul
  • 1,915
  • 14
  • 21
  • Although I decided to solve the problem without templating (And with a "string getType()" method), this solution could be indeed helpful, and so I accept it. Thanks! – Edgepo1nt Apr 29 '13 at 07:48