cpp/language/default constructor

A default constructor is a which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is.

Syntax
Where must name the current class (or current instantiation of a class template), or, when declared at namespace scope or in a friend declaration, it must be a qualified class name.

Explanation
@1@ Declaration of a default constructor inside of class definition. @2@ Definition of the constructor outside of class definition (the class must contain a declaration ). See for details on the constructor. @3@ Deleted default constructor: if it is selected by, the program fails to compile. @4@ Defaulted default constructor: the compiler will define the implicit default constructor even if other constructors are present. @5@ Defaulted default constructor outside of class definition (the class must contain a declaration ). Such constructor is treated as user-provided (see below and ).

Default constructors are called during s and s.

Implicitly-declared default constructor
If no user-declared constructors of any kind are provided for a class type (,, or ), the compiler will always declare a default constructor as an member of its class.

The implicitly-declared (or defaulted on its first declaration) default constructor has an exception specification as described in.

Implicitly-defined default constructor
If the implicitly-declared default constructor is not defined as deleted, it is defined (that is, a function body is generated and compiled) by the compiler if, and it 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 or defaulted default constructor during.

Deleted implicitly-declared default constructor
The implicitly-declared default constructor for class  is  if any of the following is true:
 * has a member of reference type.
 * has a non- member.
 * has a member which has a deleted default constructor, or its default constructor is ambiguous or inaccessible from this constructor.
 * has a direct or virtual base which has a deleted default constructor, or it is ambiguous or inaccessible from this constructor.
 * has a direct or virtual base or a non-static data member which has a deleted destructor, or a destructor that is inaccessible from this constructor.


 * is a and all of its variant members are.

Trivial default constructor
The default constructor for class is trivial (i.e. performs no action) if all of the following is true:
 * The constructor is not user-provided (i.e., is implicitly-defined or defaulted on its first declaration).
 * has no virtual member functions.
 * has no virtual base classes.


 * Every direct base of has a trivial default constructor.
 * Every 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.

Eligible default constructor
Triviality of eligible default constructors determines whether the class is an, and whether the class is a trivial type.