Skip to main content
A deleted function in C++ is a function explicitly declared with the = delete; specifier, instructing the compiler to intentionally disable its usage. When a function is marked as deleted, any attempt to call it, form a pointer to it, or otherwise reference it results in a hard compile-time error.

Overload Resolution Mechanics

Unlike functions that are simply undeclared, deleted functions are fully visible to the compiler during name lookup and actively participate in overload resolution. If a deleted function is evaluated as the best viable candidate for a given function call, the compiler terminates the compilation process and emits an error. This mechanism ensures that the compiler does not silently fall back to a less optimal, non-deleted overload or an implicit type conversion.

Applicability

The = delete specifier can be applied to any function type, including:
  • Special Member Functions: Default constructors, copy/move constructors, and copy/move assignment operators.
  • Normal Member Functions: Standard class methods.
  • Non-Member (Free) Functions: Functions declared outside of any class scope.
  • Function Templates: Entire templates or explicit template specializations.

Technical Rules and Constraints

1. First Declaration Requirement A function must be declared as deleted on its very first declaration within a translation unit. You cannot declare a function normally and then attempt to delete it in a subsequent redeclaration or definition.
2. One Definition Rule (ODR) A deleted function is considered to have a definition. Because it is defined (as deleted), providing a standard body { ... } elsewhere in the program violates the One Definition Rule and results in a compiler error. 3. Interaction with Access Specifiers In the C++ compilation pipeline, overload resolution occurs before access control checks. If a deleted function is declared private, and a caller outside the class attempts to invoke it, the compiler may emit an error regarding “accessing a private member” rather than “calling a deleted function.” To ensure the compiler yields the most accurate diagnostic regarding the deleted status, deleted functions are conventionally declared public. 4. Virtual Functions A virtual function can be marked as deleted, but the C++ standard ([class.virtual]) enforces strict symmetry regarding overrides. A function with a deleted definition cannot override a function that does not have a deleted definition, and a function without a deleted definition cannot override a function with a deleted definition. Both the base virtual function and the overriding derived function must either be deleted, or neither can be deleted.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More