Search code examples
c++language-designvirtual-destructor

Are there any specific reasons to use non-virtual destructors?


As I know, any class that is designated to have subclasses should be declared with virtual destructor, so class instances can be destroyed properly when accessing them through pointers.

But why it's even possible to declare such class with non-virtual destructor? I believe compiler can decide when to use virtual destructors. So, is it a C++ design oversight, or am I missing something?


Solution

  • Are there any specific reasons to use non-virtual destructors?

    Yes, there are.

    Mainly, it boils down to performance. A virtual function cannot be inlined, instead you must first determined the correct function to invoke (which requires runtime information) and then invoke that function.

    In performance sensitive code, the difference between no code and a "simple" function call can make a difference. Unlike many languages C++ does not assume that this difference is trivial.

    But why it's even possible to declare such class with non-virtual destructor?

    Because it is hard to know (for the compiler) if the class requires a virtual destructor or not.

    A virtual destructor is required when:

    • you invoke delete on a pointer
    • to a derived object via a base class

    When the compiler sees the class definition:

    • it cannot know that you intend to derive from this class -- you can after all derive from classes without virtual methods
    • but even more daunting: it cannot know that you intend to invoke delete on this class

    Many people assume that polymorphism requires newing the instance, which is just sheer lack of imagination:

    class Base { public: virtual void foo() const = 0; protected: ~Base() {} };
    
    class Derived: public Base {
      public: virtual void foo() const { std::cout << "Hello, World!\n"; }
    };
    
    void print(Base const& b) { b.foo(); }
    
    int main() {
      Derived d;
      print(d);
    }
    

    In this case, there is no need to pay for a virtual destructor because there is no polymorphism involved at the destruction time.

    In the end, it is a matter of philosophy. Where practical, C++ opts for performance and minimal service by default (the main exception being RTTI).


    With regards to warning. There are two warnings that can be leveraged to spot the issue:

    • -Wnon-virtual-dtor (gcc, Clang): warns whenever a class with virtual function does not declare a virtual destructor, unless the destructor in the base class is made protected. It is a pessimistic warning, but at least you do not miss anything.

    • -Wdelete-non-virtual-dtor (Clang, ported to gcc too): warns whenever delete is invoked on a pointer to a class that has virtual functions but no virtual destructor, unless the class is marked final. It has a 0% false positive rate, but warns "late" (and possibly several times).