| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
| Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
| ||||||||||||||||
| Storage duration specifiers | ||||||||||||||||
| Initialization | ||||||||||||||||
| Expressions | ||||||||||||||||
| Alternative representations | ||||||||||||||||
| Literals | ||||||||||||||||
| Boolean -Integer -Floating-point | ||||||||||||||||
| Character -String -nullptr(C++11) | ||||||||||||||||
| User-defined(C++11) | ||||||||||||||||
| Utilities | ||||||||||||||||
| Attributes(C++11) | ||||||||||||||||
| Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
| Type alias declaration(C++11) | ||||||||||||||||
| Casts | ||||||||||||||||
| Memory allocation | ||||||||||||||||
| Classes | ||||||||||||||||
| Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
| Special member functions | ||||||||||||||||
| ||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
A default constructor is aconstructor which can be called with no arguments.
Contents |
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) | |||||||
| class-name | - | the class whose default constructor is being declared |
| parameter-list | - | aparameter list where all parameters (exceptparameter packs)(since C++11) havedefault arguments |
| function-body | - | thefunction body of the default constructor |
Default constructors are called duringdefault initializations andvalue initializations.
If there is no user-declared constructor or constructor template for a class type, the compiler will implicitly declare a default constructor as aninlinepublic member of its class.
The implicitly-declared (or defaulted on its first declaration) default constructor has an exception specification as described indynamic exception specification(until C++17)noexcept specification(since C++17).
Ifthe 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 whenodr-used orneeded for constant evaluation or when it is explicitly defaulted after its first declaration(since C++11).
If a default constructor of aunion-like class | (since C++26) |
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 duringvalue initialization.
If this satisfies the requirements of aconstexpr constructor(until C++23)constexpr function(since C++23), the generated constructor isconstexpr. 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 keyworddefault. | (since C++11) |
Deleted default constructorThe implicitly-declared or explicitly-defaulted default constructor for class
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 keyworddelete. | (since C++11) |
The default constructor for classT is trivial if all following conditions are satisfied:
T has no virtual member functions.T has no virtual base classes.
| (since C++11) |
T has a trivial default constructor.
| (until C++26) |
| (since C++26) |
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.
A 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) (until C++20) |
A default constructor is eligible if all following conditions are satisfied:
| (since C++20) |
Triviality of eligible default constructors determines whether the class is animplicit-lifetime type, and whether the class is atrivially copyable type.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_trivial_union | 202502L | (C++26) | Relaxing the triviality requirements for special member functions of unions |
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 memberconstint 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}
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 are defined as deleted did not consider multidimensional array types | consider these types |
| CWG 2084 | C++11 | default member initializers had no effect on whether a defaulted default constructor of a union is deleted | they prevent the defaulted default constructor from being deleted |
| CWG 2595 | C++20 | a default constructor was not eligible if there is another default constructor which is more constrained but does not satisfy its associated constraints | it can be eligible in this case |
| CWG 2871 | C++98 | a default constructor would be implicitly declared even if there is a user-declared constructor template | no implicit declaration in this case |