Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are pure virtual methods allowed within a template class?

Once before, I was certain that you couldn't do this, but the other day I was playing around with some code and it seemed to compile and work. I just want to verify that I am not just getting lucky. Can a template class have a pure virtual function - which I guess would also mean just plain virtual methods would be valid as well for the destructor?

template <typename WordType> class DataSource { public:     DataSource();     DataSource(DataSource const& other);     virtual ~DataSource();      virtual void Put(         WordType const* const data,         unsigned int const wordCount) = 0; } 

I've tried looking it up online and all that I've been able to find is that you cannot have a virtual method (pure or otherwise) in a normal class such as this:

class DataSource { public:     DataSource();     DataSource(DataSource const& other);     virtual ~DataSource();      template <typename WordType>     virtual void Put(         WordType const* const data,         unsigned int const wordCount) = 0; } 

And that this is due to the imposibility of managing a virtual table to reference all the different types of possible types this method would be instanciated with.

However, when it came to a virtual member function of a template class, it seems to be different because the whole class itself is "created" via the template parameter when the template class variable is instanciated. At this point, the virtual method is just like any other virual method of a class due to the "find-and-replace" nature of templates.

Anyway, stating the question again in case it got lost in there: Are virtual (pure and/or normal) virtual functions allowed within a tempate class?

like image 807
Anthony Avatar asked Jan 19 '12 00:01

Anthony


People also ask

Can template be virtual method?

No, template member functions cannot be virtual.

Can a template method be virtual C++?

C++ expressly forbids virtual template functions because the virtual tables that would have to be built are way too complex. Luckily, C++ offers a way around this. It's a programming paradigm called Policy Based Design.

Can a virtual function be reimplemented in a derived class?

Yes, Its correct that a Derived class has to OVERRIDE the function which is Pure Virtual in the Parent Class.

What is the difference between a class template and an ordinary class?

An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated. Note the distinction between the terms class template and template class: Class template.


2 Answers

A class template can indeed contain virtual or pure virtual functions. This was employed by Andrei Alexandresu in "Modern C++ Design" to implement the visitor pattern using templates and type lists. You can see the code here in his Loki library if you're interested.

With most standard C++ implementations, this is fine, because when the template is instantiated the virtual function ends up being one single function. Consequently, the number of slots needed in the vtable can be known within the translation unit, so a vtable can be generated.

As you mentioned, you cannot have a virtual template member function because the number of vtable slots wouldn't be known within the translation unit.

Hope this helps!

like image 131
templatetypedef Avatar answered Oct 09 '22 10:10

templatetypedef


Are virtual (pure and/or normal) virtual functions allowed within a tempate class?

Yes. Perfectly legal.

like image 39
jpalecek Avatar answered Oct 09 '22 08:10

jpalecek