2

I have the same implementation for 2 methods, they both insert argument to a text file.

void WriteToFile( double content) //for double
{
    streamFile << content;
    streamFile << flush;
}

void WriteToFile( int content) //for integer
{
    streamFile << content;
    streamFile << flush;
}  

The implementation is same, therefore I merge them to a template method:

template < class T>
void WriteToFile(T content)
{
    streamFile << content;
    streamFile << flush;
}  

But the WriteToFile() method should be virtual.
How can I deal with it?

yael aviv
  • 201
  • 1
  • 5
  • 9
  • 3
    Put them back to be separate functions... templates can not be `virtual`. – Tony Delroy Sep 01 '14 at 08:38
  • @TonyD, Isn't there a trick to do it? – yael aviv Sep 01 '14 at 08:40
  • @yaelaviv I've tried to do this. No, there isn't, unless you don't mind breaking the ability to have runtime polymorphism. Then you can (**kind of**) achieve this with [CRTP](http://eli.thegreenplace.net/2011/05/17/the-curiously-recurring-template-pattern-in-c/) – druckermanly Sep 01 '14 at 08:57
  • @yaelaviv: that depends what "it" is... if your use of templates is just meant to avoid duplicate code in the base class implementations, then you can have the non-templated member functions call a private non-virtual template member function that does the actual work, including calling virtual functions with known types if helpful. If you want to have a class magically allow new client code to call it for new types, and have extra `virtual` functions spring into being - no. Part of the idea with virtual functions is that virtual dispatch table is of known size and layout. – Tony Delroy Sep 01 '14 at 09:07
  • @TonyD, Thanks, What do you mean in "private non-virtual template member function"? – yael aviv Sep 01 '14 at 09:34
  • @yaelaviv: a member function with either the preceding access specified being `private:` or no access prior specifier in a `class`, that is not marked `virtual`, and is a `template`. See Yochai's example, and note that the `template` can be made `private` while the `virtual` functions remain `public`. – Tony Delroy Sep 01 '14 at 09:52

2 Answers2

2

You can't create a virtual templated method (yet).
But, if you want the template just so you don't have to have the code everywhere , you can just call the templated method from within the overloaded methods (Code reuse):

template < class T>
void WriteToFileT(T content)
{
    streamFile << content;
    streamFile << flush;
}  

virtual void WriteToFile( double content) //for double
{
   WriteToFileT(content);
}

virtual void WriteToFile( int content) //for integer
{
   WriteToFileT(content);
}

There are a lot of discussions about this...

Can a C++ class member function template be virtual?
C++ Virtual template method
How to achieve "virtual template function" in C++

Community
  • 1
  • 1
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

One trick to obtain a "templated virtual method" is to have a templated, non-virtual method in the base class, doing all the templatey stuff, then passing the results in a runtime-polymorphic form to a virtual helper function. For instance, here you could have a template<typename T> void WriteToFile(T) in the base class, which converted the argument to a string, then passed that string to a virtual void WriteToFile_aux(string) method.

Of course, this relies on being able to separate the parametric polymorphism from the subtype polymorphism.

Sneftel
  • 40,271
  • 12
  • 71
  • 104