Are pure virtual methods allowed within a template class?

Anthony picture Anthony · Jan 19, 2012 · Viewed 32.4k times · Source

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?

Answer

templatetypedef picture templatetypedef · Jan 19, 2012

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!