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 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.
return_type function_name(parameters) = delete;

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.
void process(int x);
void process(double x) = delete;

// If called with a double:
// process(3.14); // ERROR: Call to deleted function. 
// It does NOT implicitly convert 3.14 to int to call process(int).

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.
template <typename T>
void allocate(T* ptr);

// Deleting an explicit template specialization
template <>
void allocate<void>(void* ptr) = delete; 

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.
struct Entity {
    void initialize(); 
};

// ERROR: Cannot delete a function after it has been declared.
// void Entity::initialize() = delete; 
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.
struct Base {
    virtual void process() = delete;
    virtual void execute();
};

struct Derived : Base {
    // ERROR: Cannot override a deleted function with a non-deleted function
    // void process() override; 

    // ERROR: Cannot override a non-deleted function with a deleted function
    // void execute() override = delete; 
};
Master C++ with Deep Grasping Methodology!Learn More