56

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?

Anthony
  • 9,451
  • 9
  • 45
  • 72
  • 5
    It sounds like you've already figured out the answer to this... – Oliver Charlesworth Jan 19 '12 at 00:36
  • 3
    Like I said, I swear to myself that I tried it before and had issues. When it suddenly worked, I wanted to make sure it wasn't just the because I was on a different compiler, different warning / error flags, etc and that it was defined - preferably - that it was okay by the standard. – Anthony Jan 19 '12 at 00:40
  • 2
    Also, for what its worth, I couldn't find anything online that definitively said one way or another. What better place to put this information than SO? – Anthony Jan 19 '12 at 00:41
  • possible duplicate of [Is it safe if a template contains virtual function?](http://stackoverflow.com/questions/7962570/is-it-safe-if-a-template-contains-virtual-function) and [Template and Virtual functions in C++ ? allowed ?](http://stackoverflow.com/questions/7212818/template-and-virtual-functions-in-c-allowed) – Greg Hewgill Jan 19 '12 at 01:02

4 Answers4

62

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!

Gelldur
  • 11,187
  • 7
  • 57
  • 68
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
8

A Class template with virtual functions are absolutely fine. But, template functions with virtual keyword prefixed with in a class or template class is not allowed. Below would help you get that:

//This is perfectly fine.
template <type T>
class myClass{
     virtual void function() = 0;
};

//This is NOT OK...
template<type T>
class myClass{
      template <type T>
      virtual void function() = 0;
};
Apte
  • 129
  • 1
  • 4
  • Just wanted to ensure this was legal. Logically, a class template gets instantiated to a specific class, and that specific class should then be able to have virtual functions. A template function on the other hand can't be virtual, because the template parameters are part of its signature. Furthermore, virtual tables couldn't fascilitate virtual templates. – user13947194 Mar 03 '23 at 03:16
8

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

Yes. Perfectly legal.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
8

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.

JohnMcG
  • 8,709
  • 6
  • 42
  • 49