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?


Solution 1:

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!

Solution 2:

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

Yes. Perfectly legal.

Solution 3:

Think about what a template class is -- it is not a class itself, but a template the compiler can use to create classes.

As such, there's no reason you can't include a virtual function (pure or otherwise) in the template class definition, because that, in and of itself, does not generate any code, including the virtual table.

When we actually instantiate the template class, e.g. DataSource<int>, then the compiler only needs to build the virtual table for that one selected type, so it's not any different than a (pure or otherwise) virtual function for a non-templated class.