Files
2025-10-25 03:02:53 +03:00

157 lines
6.6 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[class.default.ctor]
# 11 Classes [[class]](./#class)
## 11.4 Class members [[class.mem]](class.mem#class.default.ctor)
### 11.4.5 Constructors [[class.ctor]](class.ctor#class.default.ctor)
#### 11.4.5.2 Default constructors [class.default.ctor]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1357)
A [*default constructor*](#def:constructor,default "11.4.5.2Default constructors[class.default.ctor]") for a class X is a constructor of class X for which each parameter
that is not a function parameter pack
has a default argument
(including the case of a constructor with no parameters)[.](#1.sentence-1)
If there is no user-declared constructor or constructor template for classX,
a non-explicit constructor having no parameters is implicitly declared
as defaulted ([[dcl.fct.def]](dcl.fct.def "9.6Function definitions"))[.](#1.sentence-2)
An implicitly-declared default constructor is an
inline public member of its class[.](#1.sentence-3)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1374)
A defaulted default constructor for class X is defined as deleted if
- [(2.1)](#2.1)
any non-static data member with no default member initializer ([[class.mem]](class.mem "11.4Class members")) is
of reference type,
- [(2.2)](#2.2)
X is a non-union class and
any non-variant non-static data member of const-qualified type
(or possibly multidimensional array thereof)
with no [*brace-or-equal-initializer*](dcl.init.general#nt:brace-or-equal-initializer "9.5.1General[dcl.init.general]") is not const-default-constructible ([[dcl.init]](dcl.init "9.5Initializers")),
- [(2.3)](#2.3)
any non-variant potentially constructed subobject, except for a non-static data member
with a [*brace-or-equal-initializer*](dcl.init.general#nt:brace-or-equal-initializer "9.5.1General[dcl.init.general]"),
has class type M (or possibly multidimensional array thereof)
and overload resolution ([[over.match]](over.match "12.2Overload resolution"))
as applied to find M's corresponding constructor
does not result in a usable candidate ([[over.match.general]](over.match.general "12.2.1General")), or
- [(2.4)](#2.4)
any potentially constructed subobject S has
class type M (or possibly multidimensional array thereof),M has
a destructor that is deleted or inaccessible from the defaulted default
constructor, and
either S is non-variant or S has a default member initializer[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1401)
A default constructor for a class X is[*trivial*](#def:constructor,default,trivial "11.4.5.2Default constructors[class.default.ctor]") if it is not user-provided and if
- [(3.1)](#3.1)
X has no virtual functions ([[class.virtual]](class.virtual "11.7.3Virtual functions")) and no virtual base
classes ([[class.mi]](class.mi "11.7.2Multiple base classes")), and
- [(3.2)](#3.2)
no non-static data member of X has
a default member initializer ([[class.mem]](class.mem "11.4Class members")), and
- [(3.3)](#3.3)
all the direct base classes of X have trivial default constructors, and
- [(3.4)](#3.4)
either X is a union or
for all the non-variant non-static data members of X that are of class
type (or array thereof), each such class has a trivial default constructor[.](#3.sentence-1)
Otherwise, the default constructor is[*non-trivial*](#def:constructor,default,non-trivial "11.4.5.2Default constructors[class.default.ctor]")[.](#3.sentence-2)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1425)
If a default constructor of a union-like class X is trivial,
then for each union U that is either X or an anonymous union member of X,
if the first variant member, if any, of U has implicit-lifetime type ([[basic.types.general]](basic.types.general "6.9.1General")),
the default constructor of X begins the lifetime of that member
if it is not the active member of its union[.](#4.sentence-1)
[*Note [1](#note-1)*:
It is already the active member if U was value-initialized[.](#4.sentence-2)
— *end note*]
Otherwise,
an implicitly-defined ([[dcl.fct.def.default]](dcl.fct.def.default "9.6.2Explicitly-defaulted functions")) default constructor performs the set of
initializations of the class that would be performed by a user-written default
constructor for that class with no[*ctor-initializer*](class.base.init#nt:ctor-initializer "11.9.3Initializing bases and members[class.base.init]") ([[class.base.init]](class.base.init "11.9.3Initializing bases and members")) and an empty[*compound-statement*](stmt.block#nt:compound-statement "8.4Compound statement or block[stmt.block]")[.](#4.sentence-3)
If that user-written default constructor would be ill-formed,
the program is ill-formed[.](#4.sentence-4)
If that user-written default constructor would be constexpr-suitable ([[dcl.constexpr]](dcl.constexpr "9.2.6The constexpr and consteval specifiers")),
the implicitly-defined
default constructor is constexpr[.](#4.sentence-5)
Before the defaulted default constructor for a class is
implicitly defined,
all the non-user-provided default constructors for its base classes and
its non-static data members are implicitly defined[.](#4.sentence-6)
[*Note [2](#note-2)*:
An implicitly-declared default constructor has an
exception specification ([[except.spec]](except.spec "14.5Exception specifications"))[.](#4.sentence-7)
An explicitly-defaulted definition might have an
implicit exception specification, see [[dcl.fct.def]](dcl.fct.def "9.6Function definitions")[.](#4.sentence-8)
— *end note*]
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1458)
[*Note [3](#note-3)*:
A default constructor is implicitly invoked to initialize
a class object when no initializer is specified ([[dcl.init.general]](dcl.init.general "9.5.1General"))[.](#5.sentence-1)
Such a default constructor needs to be accessible ([[class.access]](class.access "11.8Member access control"))[.](#5.sentence-2)
— *end note*]
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1466)
[*Note [4](#note-4)*:
[[class.base.init]](class.base.init "11.9.3Initializing bases and members") describes the order in which constructors for base
classes and non-static data members are called and
describes how arguments can be specified for the calls to these constructors[.](#6.sentence-1)
— *end note*]