> ## 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.

# C++ Defaulted Destructor

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:**

```cpp theme={"dark"}
class InlineDefault {
public:
    ~InlineDefault() = default; // Compiler generates the body inline
};
```

**Out-of-Line Declaration:**

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
