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 constructor is a constructor explicitly marked with the = delete specifier, instructing the compiler to intentionally disable its generation and prohibit its invocation. When a constructor is deleted, any attempt to instantiate the object using that specific constructor signature results in a hard compile-time error.
class Widget {
public:
    // Explicitly deleting the default constructor
    Widget() = delete; 

    // Explicitly deleting the copy constructor
    Widget(const Widget&) = delete; 

    // Explicitly deleting a parameterized constructor
    Widget(int, double) = delete; 
};

Compiler Mechanics and Overload Resolution

Unlike constructors that are simply omitted or hidden via access specifiers, deleted constructors actively participate in name lookup and overload resolution. When an object instantiation occurs, the compiler evaluates all available constructors. If the deleted constructor is determined to be the best match for the provided arguments, the compiler selects it and immediately halts compilation with an error. This mechanism ensures that the compiler does not silently fall back to a less optimal constructor or perform unintended implicit type conversions.
class DataHandler {
public:
    DataHandler(double) {}
    DataHandler(int) = delete; // Prevents implicit conversion from int to double
};

DataHandler d1(3.14); // Compiles: Calls DataHandler(double)
DataHandler d2(42);   // Error: Overload resolution selects DataHandler(int), which is deleted

Interaction with Special Member Functions

The C++ compiler automatically generates certain special member functions (such as the default constructor, copy constructor, and move constructor) under specific conditions. Applying = delete to these signatures suppresses this implicit generation. Once a special member function is deleted, the class ceases to possess that specific capability (e.g., becoming non-copyable or non-default-constructible).

Access Control vs. Deletion

While deleted constructors can technically be placed under private or protected access specifiers, standard practice dictates declaring them as public. Because C++ checks access control before checking for deletion, placing a deleted constructor in a private block can cause the compiler to emit an access violation error rather than a “use of deleted function” error. Declaring them public ensures the compiler generates the most accurate and direct diagnostic message.

Pre-C++11 Equivalence

Prior to the introduction of = delete in C++11, developers achieved a similar effect by declaring a constructor private and intentionally omitting its implementation. However, the = delete specifier is technically superior because:
  1. It guarantees a compile-time error, whereas the legacy approach could result in a deferred link-time error if a friend class or member function attempted an invocation.
  2. It explicitly communicates intent to both the compiler and other developers at the syntax level.
Master C++ with Deep Grasping Methodology!Learn More