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 defaulted constructor is a special member function explicitly instructed to use the compiler-generated default implementation via the = default specifier, introduced in C++11. It allows developers to explicitly declare a default, copy, or move constructor without defining a function body, thereby retaining the exact semantics, compiler optimizations, and type traits (such as trivial default constructibility) of an implicitly generated constructor.

Syntax

The = default specifier is appended to the constructor declaration in place of a function body.
class Widget {
public:
    // Explicitly defaulted default constructor
    Widget() = default;

    // Explicitly defaulted copy constructor
    Widget(const Widget&) = default;

    // Explicitly defaulted move constructor
    Widget(Widget&&) = default;
    
    // Parameterized constructor (suppresses implicit default constructor)
    Widget(int id); 
};

Technical Mechanics and Rules

1. Triviality A primary mechanical distinction between a defaulted constructor (Widget() = default;) and a user-provided empty constructor (Widget() {}) is the effect on trivial default constructibility. An explicitly defaulted constructor defined on its first declaration inside the class body allows the class to remain trivially default constructible (provided all base classes and non-static members are also trivially default constructible). A user-provided empty body makes the constructor non-trivial, which immediately disqualifies the class from satisfying the std::is_trivial type trait. 2. Suppression Override According to C++ standard rules, if any user-declared constructor (parameterized, copy, or move) is present, the compiler suppresses the implicit generation of the default constructor. Applying = default to the default constructor signature forces the compiler to generate it regardless of other constructor declarations. 3. Inline vs. Out-of-Line Definition A constructor can be declared in the class definition and defaulted in the implementation file.
// Header file
class Widget {
public:
    Widget();
};

// Implementation file
Widget::Widget() = default;
Note: Defaulting a constructor out-of-line means it is no longer implicitly inline, and it loses its trivial status because the compiler cannot verify its triviality at the point of the class definition. 4. Access Control The = default specifier respects standard access modifiers. A defaulted constructor can be declared as protected or private to restrict instantiation, while still relying on the compiler’s internal generation logic for the implementation.
class Singleton {
private:
    Singleton() = default; // Compiler-generated, but inaccessible externally

public:
    // Explicitly delete copy semantics to prevent implicit public generation
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;
};
5. Constexpr Evaluation An explicitly defaulted constructor is implicitly constexpr if the implicitly generated constructor would have satisfied all constexpr requirements. If you explicitly mark a defaulted constructor as constexpr but the class members do not support constant expression initialization, the behavior depends on the C++ standard version:
  • C++11 through C++20: The program is ill-formed.
  • C++23 and later: The declaration is well-formed (due to the relaxation of constexpr restrictions in proposal P2448R2). The constructor is treated as a constexpr function, but it simply cannot be evaluated in a constant expression.
6. Deleted by Default If a class contains members that cannot be default-constructed (e.g., reference members, const members without in-class initializers, or members with deleted default constructors), explicitly declaring ClassName() = default; will result in the compiler defining the constructor as implicitly deleted (= delete).
Master C++ with Deep Grasping Methodology!Learn More