Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Referring to objects of incomplete class type, also known as forward declarations, is a common practice. One such common usage is with the "pimpl idiom" [Sutter 00] whereby an opaque pointer is used to hide implementation details from a public-facing API. However, attempting to delete a pointer to an object of incomplete class type can lead to undefined behavior. The C++ Standard, [expr.delete], paragraph 5 [ISO/IEC 14882-2014], states:

If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined.

...

[Dewhurst 03]Gotcha #39, "Casting Incomplete Types"
[ISO/IEC 14882-2014]

Subclause 4.10, "Pointer Conversions"
Subclause 5.2.9, "Static Cast"
Subclause 5.2.10, "Reinterpret Cast"
Subclause 5.3.5, "Delete"
Subclause 5.4, "Explicit Type Conversion (Cast Notation)"

[Sutter 00]"Compiler Firewalls and the Pimpl Idiom"

...