A default constructor is a constructor which can be called with no arguments.
[edit] Syntax class-name (
parameter-list (optional));
(1) class-name (
parameter-list (optional))
function-body (2) class-name () = default;
(3) (since C++11) class-name (
parameter-list (optional)) = delete;
(4) (since C++11) class-name ::
class-name (
parameter-list (optional))
function-body (5) class-name ::
class-name () = default;
(6) (since C++11) [edit] Explanation
1) Declaration of a default constructor inside of class definition.
2-4) Definition of a default constructor inside of class definition.
3) The default constructor is explicitly-defaulted.
4) The default constructor is deleted.
5,6) Definition of a default constructor outside of class definition (the class must contain a declaration (1)).
6) The default constructor is explicitly-defaulted.
Default constructors are called during default initializations and value initializations.
[edit] Implicitly-declared default constructorIf there is no user-declared constructor or constructor template for a class type, the compiler will implicitly declare a default constructor as an inline public member of its class.
The implicitly-declared (or defaulted on its first declaration) default constructor has an exception specification as described in dynamic exception specification(until C++17) noexcept specification(since C++17).
[edit] Implicitly-defined default constructorIf the constructor is implicitly-declared(until C++11)the implicitly-declared or explicitly-defaulted default constructor is not defined as deleted(since C++11), it is implicitly-defined by the compiler when odr-used or needed for constant evaluation or when it is explicitly defaulted after its first declaration(since C++11).
If a default constructor of a union-like class T
is trivial, then for each union U
that is either T
or an anonymous union member of T
, if the first variant member (if any) of U
has implicit-lifetime type, the default constructor of T
begins the lifetime of that member if it is not the active member of its union.
An(until C++26)Otherwise, an(since C++26) implicitly-defined default constructor has the same effect as a user-defined constructor with empty body and empty initializer list. That is, it calls the default constructors of the bases and of the non-static members of this class. Class types with an empty user-provided constructor may get treated differently than those with an implicitly-defined default constructor during value initialization.
If this satisfies the requirements of a constexpr constructor(until C++23)constexpr function(since C++23), the generated constructor is constexpr.
If some user-defined constructors are present, the user may still force the automatic generation of a default constructor by the compiler that would be implicitly-declared otherwise with the keyword default.
(since C++11) Deleted default constructorThe implicitly-declared or explicitly-defaulted default constructor for class T
is defined as deleted if any of the following conditions is satisfied:
T
has a non-static data member of reference type without a default initializer.T
is a non-union class and(since C++26) has a non-variant non-static non-const-default-constructible data member of const-qualified type (or possibly multidimensional array thereof) without a default member initializer.T
is a union and all of its variant members are of const-qualified type (or possibly multidimensional array thereof).T
is a non-union class and all members of any anonymous union member are of const-qualified type (or possibly multidimensional array thereof).M
, T
has a potentially constructed subobject obj of type M
(or possibly multidimensional array thereof), and any of the following conditions is satisfied:M
has a destructor that is deleted or inaccessible from the default constructor, and either obj is non-variant or obj has a default member initializer(since C++26).M
's default constructor does not result in a usable candidate, or in the case of obj being a variant member, selects a non-trivial function(until C++26).If no user-defined constructors are present and the implicitly-declared default constructor is not trivial, the user may still inhibit the automatic generation of an implicitly-defined default constructor by the compiler with the keyword delete.
(since C++11) [edit] Trivial default constructorThe default constructor for class T
is trivial if all following conditions are satisfied:
T
has no virtual member functions.T
has no virtual base classes.T
has no non-static members with default initializers.T
has a trivial default constructor.T
is a union, or every non-variant non-static member of class type (or array thereof) has a trivial default constructor.A trivial default constructor is a constructor that performs no action. All data types compatible with the C language (POD types) are trivially default-constructible.
[edit] Eligible default constructorA default constructor is eligible if it is either user-declared or both implicitly-declared and definable.
(until C++11)A default constructor is eligible if it is not deleted.
(since C++11)A default constructor is eligible if all following conditions are satisfied:
Triviality of eligible default constructors determines whether the class is an implicit-lifetime type, and whether the class is a trivially copyable type.
[edit] Notes Feature-test macro Value Std Feature__cpp_trivial_union
202502L
(C++26) Relaxing the triviality requirements for special member functions of unions [edit] Example
struct A { int x; A(int x = 1): x(x) {} // user-defined default constructor }; struct B : A { // B::B() is implicitly-defined, calls A::A() }; struct C { A a; // C::C() is implicitly-defined, calls A::A() }; struct D : A { D(int y) : A(y) {} // D::D() is not declared because another constructor exists }; struct E : A { E(int y) : A(y) {} E() = default; // explicitly defaulted, calls A::A() }; struct F { int& ref; // reference member const int c; // const member // F::F() is implicitly defined as deleted }; // user declared copy constructor (either user-provided, deleted or defaulted) // prevents the implicit generation of a default constructor struct G { G(const G&) {} // G::G() is implicitly defined as deleted }; struct H { H(const H&) = delete; // H::H() is implicitly defined as deleted }; struct I { I(const I&) = default; // I::I() is implicitly defined as deleted }; int main() { A a; B b; C c; // D d; // compile error E e; // F f; // compile error // G g; // compile error // H h; // compile error // I i; // compile error }[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior CWG 1353 C++11 the conditions where implicitly-declared default constructors areRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4