Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A defaulted destructor is an explicitly declared destructor appended with the = default; specifier, instructing the C++ compiler to generate its standard, implicitly-defined implementation. Introduced in C++11, it allows a class to explicitly declare a destructor without classifying it as “user-provided,” thereby preserving specific compiler-generated traits such as trivial destructibility.

Syntax

A defaulted destructor can be declared inline within the class definition or out-of-line in an implementation file. Inline Declaration:
class InlineDefault {
public:
    ~InlineDefault() = default; // Compiler generates the body inline
};
Out-of-Line Declaration:
class OutOfLineDefault {
public:
    ~OutOfLineDefault(); // Declaration only
};

// Definition in a translation unit (.cpp file)
OutOfLineDefault::~OutOfLineDefault() = default;

Technical Mechanics

1. Triviality vs. User-Provided Bodies The primary mechanical distinction between an empty destructor body ({}) and a defaulted destructor (= default;) is how the compiler categorizes the class type.
  • ~ClassName() {} is considered user-provided. It forces the class to be non-trivially destructible.
  • ~ClassName() = default; is not user-provided (if defaulted on its first declaration). If all base classes and non-static data members are trivially destructible, the class remains trivially destructible. This allows the compiler to optimize object destruction, often omitting the destructor call entirely.
2. Virtual Defaulted Destructors A defaulted destructor can be declared virtual. The compiler will generate the standard destruction sequence (destroying members in reverse order of initialization, followed by base classes) while populating the vtable appropriately.
class Base {
public:
    virtual ~Base() = default; 
};
3. Exception Specification An explicitly defaulted destructor implicitly inherits the exception specification that the compiler would have generated for an implicitly declared destructor. In standard C++, this is almost universally noexcept(true), unless a base class or member variable has a destructor marked noexcept(false). 4. Access Control Unlike an implicitly generated destructor, which is always public, an explicitly defaulted destructor can be placed under protected or private access specifiers. This restricts where the object can be destroyed (and thus allocated) while still relying on the compiler-generated destruction logic.
class RestrictedDestruction {
protected:
    ~RestrictedDestruction() = default;
};
5. Interaction with the Rule of Five Declaring a defaulted destructor explicitly declares a destructor for the class. According to C++ standard rules, the explicit declaration of a destructor suppresses the implicit generation of the move constructor and move assignment operator. It does not suppress the implicit generation of the copy constructor or copy assignment operator, though relying on this behavior is deprecated.
Master C++ with Deep Grasping Methodology!Learn More