Pure virtual (abstract) final functions in C++

Today I ran across an interesting little quirk of C++11. You can declare a pure virtual function which has no implementation and which is final. That means the class can never be instantiated or inherited, and the function will never have a body.

For example:

class Widget
    virtual void foo() final = 0;

It certainly looks odd at first glance, but it’s legal in C++, and seems to behave correctly on recent compilers. Admittedly, it’s completely useless for most purposes. However, there’s a similar pattern which I have found useful:

class Widget final
    virtual ~Widget() = 0;

To all extents and purposes, this is the same as the first example. It’s simply a little more readable. It’s the class rather than the function which is declared as final, so it’s easier to spot that specifier and understand what it means. Also, I’ve used the destructor instead of some other arbitrary function so that there’s no chance of naming conflicts.

The only use I’ve found for this is in template metaprogramming. Sometimes classes are declared solely for compile-time traits information, or to wrap a static function which needs partial specialization. In these cases, there’s no reason to instantiate the class in any form, even as a base class. There’s not necessarily any reason to prevent it as such, but it at least means that if it happens by mistake then it will be noticed right away.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.