Init
This commit is contained in:
581
cppdraft/class/ctor.md
Normal file
581
cppdraft/class/ctor.md
Normal file
@@ -0,0 +1,581 @@
|
||||
[class.ctor]
|
||||
|
||||
# 11 Classes [[class]](./#class)
|
||||
|
||||
## 11.4 Class members [[class.mem]](class.mem#class.ctor)
|
||||
|
||||
### 11.4.5 Constructors [class.ctor]
|
||||
|
||||
#### [11.4.5.1](#general) General [[class.ctor.general]](class.ctor.general)
|
||||
|
||||
[1](#general-1)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1240)
|
||||
|
||||
A [*declarator*](dcl.decl.general#nt:declarator "9.3.1 General [dcl.decl.general]") declares a [*constructor*](#def:constructor "11.4.5.1 General [class.ctor.general]") if it is a
|
||||
function declarator ([[dcl.fct]](dcl.fct "9.3.4.6 Functions")) of the form
|
||||
|
||||
[*ptr-declarator*](dcl.decl.general#nt:ptr-declarator "9.3.1 General [dcl.decl.general]") ( [*parameter-declaration-clause*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6 Functions [dcl.fct]") ) [*noexcept-specifier*](except.spec#nt:noexcept-specifier "14.5 Exception specifications [except.spec]")opt [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")opt
|
||||
|
||||
where the [*ptr-declarator*](dcl.decl.general#nt:ptr-declarator "9.3.1 General [dcl.decl.general]") consists solely of an[*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]"), an optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]"),
|
||||
and optional surrounding parentheses, and the [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") has
|
||||
one of the following forms:
|
||||
|
||||
- [(1.1)](#general-1.1)
|
||||
|
||||
in a friend declaration ([[class.friend]](class.friend "11.8.4 Friends")),
|
||||
the [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") is a [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3 Qualified names [expr.prim.id.qual]") that names a constructor ([[class.qual]](class.qual "6.5.5.2 Class members"));
|
||||
|
||||
- [(1.2)](#general-1.2)
|
||||
|
||||
otherwise, in a [*member-declaration*](class.mem.general#nt:member-declaration "11.4.1 General [class.mem.general]") that belongs to the[*member-specification*](class.mem.general#nt:member-specification "11.4.1 General [class.mem.general]") of a class or class template,
|
||||
the [*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") is the
|
||||
injected-class-name ([[class.pre]](class.pre "11.1 Preamble")) of the immediately-enclosing entity;
|
||||
|
||||
- [(1.3)](#general-1.3)
|
||||
|
||||
otherwise, the[*id-expression*](expr.prim.id.general#nt:id-expression "7.5.5.1 General [expr.prim.id.general]") is a [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3 Qualified names [expr.prim.id.qual]") whose [*unqualified-id*](expr.prim.id.unqual#nt:unqualified-id "7.5.5.2 Unqualified names [expr.prim.id.unqual]") is
|
||||
the injected-class-name of its lookup context[.](#general-1.sentence-1)
|
||||
|
||||
Constructors do not have names[.](#general-1.sentence-2)
|
||||
|
||||
In a constructor declaration, each [*decl-specifier*](dcl.spec.general#nt:decl-specifier "9.2.1 General [dcl.spec.general]") in the optional[*decl-specifier-seq*](dcl.spec.general#nt:decl-specifier-seq "9.2.1 General [dcl.spec.general]") shall befriend,inline,constexpr,consteval, or
|
||||
an [*explicit-specifier*](dcl.fct.spec#nt:explicit-specifier "9.2.3 Function specifiers [dcl.fct.spec]")[.](#general-1.sentence-3)
|
||||
|
||||
[*Example [1](#general-example-1)*: struct S { S(); // declares the constructor};
|
||||
|
||||
S::S() { } // defines the constructor â *end example*]
|
||||
|
||||
[2](#general-2)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1285)
|
||||
|
||||
A constructor is used to initialize objects of its class type[.](#general-2.sentence-1)
|
||||
|
||||
[*Note [1](#general-note-1)*:
|
||||
|
||||
Because constructors do not have names, they are never found during
|
||||
unqualified name lookup; however an explicit type conversion using the functional
|
||||
notation ([[expr.type.conv]](expr.type.conv "7.6.1.4 Explicit type conversion (functional notation)")) will cause a constructor to be called to
|
||||
initialize an object[.](#general-2.sentence-2)
|
||||
|
||||
The syntax looks like an explicit call of the constructor[.](#general-2.sentence-3)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[*Example [2](#general-example-2)*: complex zz = complex(1,2.3);
|
||||
cprint( complex(7.8,1.2) ); â *end example*]
|
||||
|
||||
[*Note [2](#general-note-2)*:
|
||||
|
||||
For initialization of objects of class type see [[class.init]](class.init "11.9 Initialization")[.](#general-2.sentence-4)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[3](#general-3)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1305)
|
||||
|
||||
An object created in this way is unnamed[.](#general-3.sentence-1)
|
||||
|
||||
[*Note [3](#general-note-3)*:
|
||||
|
||||
[[class.temporary]](class.temporary "6.8.7 Temporary objects") describes the lifetime of temporary objects[.](#general-3.sentence-2)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[*Note [4](#general-note-4)*:
|
||||
|
||||
Explicit constructor calls do not yield lvalues, see [[basic.lval]](basic.lval "7.2.1 Value category")[.](#general-3.sentence-3)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[4](#general-4)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1315)
|
||||
|
||||
[*Note [5](#general-note-5)*:
|
||||
|
||||
Some language constructs have special semantics when used during construction;
|
||||
see [[class.base.init]](class.base.init "11.9.3 Initializing bases and members") and [[class.cdtor]](class.cdtor "11.9.5 Construction and destruction")[.](#general-4.sentence-1)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[5](#general-5)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1322)
|
||||
|
||||
A constructor can be invoked for aconst,volatile orconstvolatile object[.](#general-5.sentence-1)
|
||||
|
||||
const andvolatile semantics ([[dcl.type.cv]](dcl.type.cv "9.2.9.2 The cv-qualifiers")) are not applied on an object under construction[.](#general-5.sentence-2)
|
||||
|
||||
They come into effect when the constructor for the
|
||||
most derived object ([[intro.object]](intro.object "6.8.2 Object model")) ends[.](#general-5.sentence-3)
|
||||
|
||||
[6](#general-6)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1340)
|
||||
|
||||
The address of a constructor shall not be taken[.](#general-6.sentence-1)
|
||||
|
||||
[*Note [6](#general-note-6)*:
|
||||
|
||||
A return statement in the body of a constructor
|
||||
cannot specify a return value ([[stmt.return]](stmt.return "8.8.4 The return statement"))[.](#general-6.sentence-2)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[7](#general-7)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1349)
|
||||
|
||||
A constructor shall not be a coroutine[.](#general-7.sentence-1)
|
||||
|
||||
[8](#general-8)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1352)
|
||||
|
||||
A constructor shall not have an explicit object parameter ([[dcl.fct]](dcl.fct "9.3.4.6 Functions"))[.](#general-8.sentence-1)
|
||||
|
||||
#### [11.4.5.2](#class.default.ctor) Default constructors [[class.default.ctor]](class.default.ctor)
|
||||
|
||||
[1](#class.default.ctor-1)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1357)
|
||||
|
||||
A [*default constructor*](#def:constructor,default "11.4.5.2 Default 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)[.](#class.default.ctor-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.6 Function definitions"))[.](#class.default.ctor-1.sentence-2)
|
||||
|
||||
An implicitly-declared default constructor is an
|
||||
inline public member of its class[.](#class.default.ctor-1.sentence-3)
|
||||
|
||||
[2](#class.default.ctor-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)](#class.default.ctor-2.1)
|
||||
|
||||
any non-static data member with no default member initializer ([[class.mem]](class.mem "11.4 Class members")) is
|
||||
of reference type,
|
||||
|
||||
- [(2.2)](#class.default.ctor-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.1 General [dcl.init.general]") is not const-default-constructible ([[dcl.init]](dcl.init "9.5 Initializers")),
|
||||
|
||||
- [(2.3)](#class.default.ctor-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.1 General [dcl.init.general]"),
|
||||
has class type M (or possibly multidimensional array thereof)
|
||||
and overload resolution ([[over.match]](over.match "12.2 Overload resolution"))
|
||||
as applied to find M's corresponding constructor
|
||||
does not result in a usable candidate ([[over.match.general]](over.match.general "12.2.1 General")), or
|
||||
|
||||
- [(2.4)](#class.default.ctor-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[.](#class.default.ctor-2.sentence-1)
|
||||
|
||||
[3](#class.default.ctor-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.2 Default constructors [class.default.ctor]") if it is not user-provided and if
|
||||
|
||||
- [(3.1)](#class.default.ctor-3.1)
|
||||
|
||||
X has no virtual functions ([[class.virtual]](class.virtual "11.7.3 Virtual functions")) and no virtual base
|
||||
classes ([[class.mi]](class.mi "11.7.2 Multiple base classes")), and
|
||||
|
||||
- [(3.2)](#class.default.ctor-3.2)
|
||||
|
||||
no non-static data member of X has
|
||||
a default member initializer ([[class.mem]](class.mem "11.4 Class members")), and
|
||||
|
||||
- [(3.3)](#class.default.ctor-3.3)
|
||||
|
||||
all the direct base classes of X have trivial default constructors, and
|
||||
|
||||
- [(3.4)](#class.default.ctor-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[.](#class.default.ctor-3.sentence-1)
|
||||
|
||||
Otherwise, the default constructor is[*non-trivial*](#def:constructor,default,non-trivial "11.4.5.2 Default constructors [class.default.ctor]")[.](#class.default.ctor-3.sentence-2)
|
||||
|
||||
[4](#class.default.ctor-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.1 General")),
|
||||
the default constructor of X begins the lifetime of that member
|
||||
if it is not the active member of its union[.](#class.default.ctor-4.sentence-1)
|
||||
|
||||
[*Note [1](#class.default.ctor-note-1)*:
|
||||
|
||||
It is already the active member if U was value-initialized[.](#class.default.ctor-4.sentence-2)
|
||||
|
||||
â *end note*]
|
||||
|
||||
Otherwise,
|
||||
an implicitly-defined ([[dcl.fct.def.default]](dcl.fct.def.default "9.6.2 Explicitly-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.3 Initializing bases and members [class.base.init]") ([[class.base.init]](class.base.init "11.9.3 Initializing bases and members")) and an empty[*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]")[.](#class.default.ctor-4.sentence-3)
|
||||
|
||||
If that user-written default constructor would be ill-formed,
|
||||
the program is ill-formed[.](#class.default.ctor-4.sentence-4)
|
||||
|
||||
If that user-written default constructor would be constexpr-suitable ([[dcl.constexpr]](dcl.constexpr "9.2.6 The constexpr and consteval specifiers")),
|
||||
the implicitly-defined
|
||||
default constructor is constexpr[.](#class.default.ctor-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[.](#class.default.ctor-4.sentence-6)
|
||||
|
||||
[*Note [2](#class.default.ctor-note-2)*:
|
||||
|
||||
An implicitly-declared default constructor has an
|
||||
exception specification ([[except.spec]](except.spec "14.5 Exception specifications"))[.](#class.default.ctor-4.sentence-7)
|
||||
|
||||
An explicitly-defaulted definition might have an
|
||||
implicit exception specification, see [[dcl.fct.def]](dcl.fct.def "9.6 Function definitions")[.](#class.default.ctor-4.sentence-8)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[5](#class.default.ctor-5)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1458)
|
||||
|
||||
[*Note [3](#class.default.ctor-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.1 General"))[.](#class.default.ctor-5.sentence-1)
|
||||
|
||||
Such a default constructor needs to be accessible ([[class.access]](class.access "11.8 Member access control"))[.](#class.default.ctor-5.sentence-2)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[6](#class.default.ctor-6)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1466)
|
||||
|
||||
[*Note [4](#class.default.ctor-note-4)*:
|
||||
|
||||
[[class.base.init]](class.base.init "11.9.3 Initializing 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[.](#class.default.ctor-6.sentence-1)
|
||||
|
||||
â *end note*]
|
||||
|
||||
#### [11.4.5.3](#class.copy.ctor) Copy/move constructors [[class.copy.ctor]](class.copy.ctor)
|
||||
|
||||
[1](#class.copy.ctor-1)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1477)
|
||||
|
||||
A non-template constructor for classX is
|
||||
a
|
||||
copy
|
||||
constructor if its first parameter is of typeX&,const X&,volatile X& orconst volatile X&,
|
||||
and either there are no other parameters
|
||||
or else all other parameters have default arguments ([[dcl.fct.default]](dcl.fct.default "9.3.4.7 Default arguments"))[.](#class.copy.ctor-1.sentence-1)
|
||||
|
||||
[*Example [1](#class.copy.ctor-example-1)*:
|
||||
|
||||
X::X(const X&) andX::X(X&,int=1) are copy constructors[.](#class.copy.ctor-1.sentence-2)
|
||||
|
||||
struct X { X(int);
|
||||
X(const X&, int = 1);};
|
||||
X a(1); // calls X(int); X b(a, 0); // calls X(const X&, int); X c = b; // calls X(const X&, int); â *end example*]
|
||||
|
||||
[2](#class.copy.ctor-2)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1512)
|
||||
|
||||
A non-template constructor for class X is a move constructor if its
|
||||
first parameter is of type X&&, const X&&,volatile X&&, or const volatile X&&, and either there are
|
||||
no other parameters or else all other parameters have default
|
||||
arguments ([[dcl.fct.default]](dcl.fct.default "9.3.4.7 Default arguments"))[.](#class.copy.ctor-2.sentence-1)
|
||||
|
||||
[*Example [2](#class.copy.ctor-example-2)*:
|
||||
|
||||
Y::Y(Y&&) is a move constructor[.](#class.copy.ctor-2.sentence-2)
|
||||
|
||||
struct Y { Y(const Y&);
|
||||
Y(Y&&);};extern Y f(int);
|
||||
Y d(f(1)); // calls Y(Y&&) Y e = d; // calls Y(const Y&) â *end example*]
|
||||
|
||||
[3](#class.copy.ctor-3)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1531)
|
||||
|
||||
[*Note [1](#class.copy.ctor-note-1)*:
|
||||
|
||||
All forms of copy/move constructor can be declared for a class[.](#class.copy.ctor-3.sentence-1)
|
||||
|
||||
[*Example [3](#class.copy.ctor-example-3)*: struct X { X(const X&);
|
||||
X(X&); // OK X(X&&);
|
||||
X(const X&&); // OK, but possibly not sensible}; â *end example*]
|
||||
|
||||
â *end note*]
|
||||
|
||||
[4](#class.copy.ctor-4)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1546)
|
||||
|
||||
[*Note [2](#class.copy.ctor-note-2)*:
|
||||
|
||||
If a classX only has a copy constructor with a parameter of typeX&,
|
||||
an initializer of typeconstX orvolatileX cannot initialize an object of typecv X[.](#class.copy.ctor-4.sentence-1)
|
||||
|
||||
[*Example [4](#class.copy.ctor-example-4)*: struct X { X(); // default constructor X(X&); // copy constructor with a non-const parameter};const X cx;
|
||||
X x = cx; // error: X::X(X&) cannot copy cx into x â *end example*]
|
||||
|
||||
â *end note*]
|
||||
|
||||
[5](#class.copy.ctor-5)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1572)
|
||||
|
||||
A declaration of a constructor for a classX is ill-formed if its first parameter is of typecv X and either there are no other parameters or else all other parameters have
|
||||
default arguments[.](#class.copy.ctor-5.sentence-1)
|
||||
|
||||
A member function template is never instantiated to
|
||||
produce such a constructor signature[.](#class.copy.ctor-5.sentence-2)
|
||||
|
||||
[*Example [5](#class.copy.ctor-example-5)*: struct S {template<typename T> S(T);
|
||||
S();};
|
||||
|
||||
S g;
|
||||
|
||||
void h() { S a(g); // does not instantiate the member template to produce S::S<S>(S);// uses the implicitly declared copy constructor} â *end example*]
|
||||
|
||||
[6](#class.copy.ctor-6)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1597)
|
||||
|
||||
If the class definition does not explicitly declare a copy constructor,
|
||||
a non-explicit one is declared [*implicitly*](#def:constructor,copy,implicitly_declared "11.4.5.3 Copy/move constructors [class.copy.ctor]")[.](#class.copy.ctor-6.sentence-1)
|
||||
|
||||
If the class definition declares a move
|
||||
constructor or move assignment operator, the implicitly declared copy
|
||||
constructor is defined as deleted; otherwise, it is
|
||||
defaulted ([[dcl.fct.def]](dcl.fct.def "9.6 Function definitions"))[.](#class.copy.ctor-6.sentence-2)
|
||||
|
||||
The latter case is deprecated if the class has a user-declared copy assignment
|
||||
operator or a user-declared destructor ([[depr.impldec]](depr.impldec "D.6 Implicit declaration of copy functions"))[.](#class.copy.ctor-6.sentence-3)
|
||||
|
||||
[7](#class.copy.ctor-7)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1607)
|
||||
|
||||
The implicitly-declared copy constructor for a classX will have the formX::X(const X&) if each potentially constructed subobject of a class typeM (or array thereof)
|
||||
has a copy constructor whose first parameter is of typeconstM& orconstvolatileM&[.](#class.copy.ctor-7.sentence-1)[89](#footnote-89 "This implies that the reference parameter of the implicitly-declared copy constructor cannot bind to a volatile lvalue; see [diff.class].")
|
||||
|
||||
Otherwise, the implicitly-declared copy constructor will have the formX::X(X&)
|
||||
|
||||
[8](#class.copy.ctor-8)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1636)
|
||||
|
||||
If the definition of a class X does not explicitly declare
|
||||
a move constructor, a non-explicit one will be
|
||||
implicitly declared as defaulted if and only if
|
||||
|
||||
- [(8.1)](#class.copy.ctor-8.1)
|
||||
|
||||
X does not have a user-declared copy constructor,
|
||||
|
||||
- [(8.2)](#class.copy.ctor-8.2)
|
||||
|
||||
X does not have a user-declared copy assignment operator,
|
||||
|
||||
- [(8.3)](#class.copy.ctor-8.3)
|
||||
|
||||
X does not have a user-declared move assignment operator, and
|
||||
|
||||
- [(8.4)](#class.copy.ctor-8.4)
|
||||
|
||||
X does not have a user-declared destructor[.](#class.copy.ctor-8.sentence-1)
|
||||
|
||||
[*Note [3](#class.copy.ctor-note-3)*:
|
||||
|
||||
When the move constructor is not implicitly declared or explicitly supplied,
|
||||
expressions that otherwise would have invoked the move constructor might instead invoke
|
||||
a copy constructor[.](#class.copy.ctor-8.sentence-2)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[9](#class.copy.ctor-9)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1661)
|
||||
|
||||
The implicitly-declared move constructor for class X will have the formX::X(X&&)
|
||||
|
||||
[10](#class.copy.ctor-10)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1667)
|
||||
|
||||
An implicitly-declared copy/move constructor is an
|
||||
inline public member of its class[.](#class.copy.ctor-10.sentence-1)
|
||||
|
||||
A defaulted copy/move constructor for a class X is defined as deleted ([[dcl.fct.def.delete]](dcl.fct.def.delete "9.6.3 Deleted definitions")) if X has:
|
||||
|
||||
- [(10.1)](#class.copy.ctor-10.1)
|
||||
|
||||
a potentially constructed subobject of type M (or possibly multidimensional array thereof) for which
|
||||
overload resolution ([[over.match]](over.match "12.2 Overload resolution")), as applied to find M's corresponding constructor,
|
||||
either does not result in a usable candidate ([[over.match.general]](over.match.general "12.2.1 General")) or,
|
||||
in the case of a variant member, selects a non-trivial function,
|
||||
|
||||
- [(10.2)](#class.copy.ctor-10.2)
|
||||
|
||||
any potentially constructed subobject of
|
||||
class type M (or possibly multidimensional array thereof)
|
||||
where M has
|
||||
a destructor that is deleted or inaccessible from the defaulted
|
||||
constructor, or,
|
||||
|
||||
- [(10.3)](#class.copy.ctor-10.3)
|
||||
|
||||
for the copy constructor, a non-static data member of rvalue reference type[.](#class.copy.ctor-10.sentence-2)
|
||||
|
||||
[*Note [4](#class.copy.ctor-note-4)*:
|
||||
|
||||
A defaulted move constructor that is defined as deleted is ignored by overload
|
||||
resolution ([[over.match]](over.match "12.2 Overload resolution"), [[over.over]](over.over "12.3 Address of an overload set"))[.](#class.copy.ctor-10.sentence-3)
|
||||
|
||||
Such a constructor would otherwise interfere with initialization from
|
||||
an rvalue which can use the copy constructor instead[.](#class.copy.ctor-10.sentence-4)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[11](#class.copy.ctor-11)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1696)
|
||||
|
||||
A copy/move constructor for classX is
|
||||
trivial
|
||||
if it is not user-provided and if
|
||||
|
||||
- [(11.1)](#class.copy.ctor-11.1)
|
||||
|
||||
classX has no virtual functions ([[class.virtual]](class.virtual "11.7.3 Virtual functions"))
|
||||
and no virtual base classes ([[class.mi]](class.mi "11.7.2 Multiple base classes")), and
|
||||
|
||||
- [(11.2)](#class.copy.ctor-11.2)
|
||||
|
||||
the constructor selected to copy/move each direct base class subobject is trivial, and
|
||||
|
||||
- [(11.3)](#class.copy.ctor-11.3)
|
||||
|
||||
for each non-static data member ofX that is of class type (or array thereof),
|
||||
the constructor selected to copy/move that member is trivial;
|
||||
|
||||
otherwise the copy/move constructor is[*non-trivial*](#def:constructor,copy,nontrivial "11.4.5.3 Copy/move constructors [class.copy.ctor]")[.](#class.copy.ctor-11.sentence-1)
|
||||
|
||||
[12](#class.copy.ctor-12)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1724)
|
||||
|
||||
[*Note [5](#class.copy.ctor-note-5)*:
|
||||
|
||||
The copy/move constructor is implicitly defined even if the implementation elided
|
||||
its odr-use ([[basic.def.odr]](basic.def.odr#term.odr.use "6.3 One-definition rule"), [[class.temporary]](class.temporary "6.8.7 Temporary objects"))[.](#class.copy.ctor-12.sentence-1)
|
||||
|
||||
â *end note*]
|
||||
|
||||
If an implicitly-defined ([[dcl.fct.def.default]](dcl.fct.def.default "9.6.2 Explicitly-defaulted functions")) constructor would be constexpr-suitable ([[dcl.constexpr]](dcl.constexpr "9.2.6 The constexpr and consteval specifiers")),
|
||||
the implicitly-defined
|
||||
constructor is constexpr[.](#class.copy.ctor-12.sentence-2)
|
||||
|
||||
[13](#class.copy.ctor-13)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1733)
|
||||
|
||||
Before the defaulted copy/move constructor for a class is
|
||||
implicitly defined,
|
||||
all non-user-provided copy/move constructors for its
|
||||
potentially constructed subobjects
|
||||
are implicitly defined[.](#class.copy.ctor-13.sentence-1)
|
||||
|
||||
[*Note [6](#class.copy.ctor-note-6)*:
|
||||
|
||||
An implicitly-declared copy/move constructor has an
|
||||
implied exception specification ([[except.spec]](except.spec "14.5 Exception specifications"))[.](#class.copy.ctor-13.sentence-2)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[14](#class.copy.ctor-14)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1744)
|
||||
|
||||
The implicitly-defined copy/move constructor for a non-union classX performs a memberwise copy/move of its bases and members[.](#class.copy.ctor-14.sentence-1)
|
||||
|
||||
[*Note [7](#class.copy.ctor-note-7)*:
|
||||
|
||||
Default member initializers of non-static data members are ignored[.](#class.copy.ctor-14.sentence-2)
|
||||
|
||||
â *end note*]
|
||||
|
||||
The order of initialization is the same as the order of initialization of bases
|
||||
and members in a user-defined constructor (see [[class.base.init]](class.base.init "11.9.3 Initializing bases and members"))[.](#class.copy.ctor-14.sentence-3)
|
||||
|
||||
Let x be either the parameter of the constructor or, for the move constructor, an
|
||||
xvalue referring to the parameter[.](#class.copy.ctor-14.sentence-4)
|
||||
|
||||
Each base or non-static data member
|
||||
is copied/moved in the manner appropriate to its type:
|
||||
|
||||
- [(14.1)](#class.copy.ctor-14.1)
|
||||
|
||||
if the member is an array, each element is
|
||||
direct-initialized with the corresponding subobject of x;
|
||||
|
||||
- [(14.2)](#class.copy.ctor-14.2)
|
||||
|
||||
if a member m has rvalue reference type T&&, it is direct-initialized withstatic_cast<T&&>(x.m);
|
||||
|
||||
- [(14.3)](#class.copy.ctor-14.3)
|
||||
|
||||
otherwise, the base or member is direct-initialized with the corresponding base or member of x[.](#class.copy.ctor-14.sentence-5)
|
||||
|
||||
Virtual base class subobjects shall be initialized only once by
|
||||
the implicitly-defined copy/move constructor (see [[class.base.init]](class.base.init "11.9.3 Initializing bases and members"))[.](#class.copy.ctor-14.sentence-6)
|
||||
|
||||
[15](#class.copy.ctor-15)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1774)
|
||||
|
||||
The implicitly-defined copy/move constructor for a unionX copies the object representation ([[basic.types.general]](basic.types.general#term.object.representation "6.9.1 General")) of X[.](#class.copy.ctor-15.sentence-1)
|
||||
|
||||
For each object nested within ([[intro.object]](intro.object "6.8.2 Object model"))
|
||||
the object that is the source of the copy,
|
||||
a corresponding object o nested within the destination
|
||||
is identified (if the object is a subobject) or created (otherwise),
|
||||
and the lifetime of o begins before the copy is performed[.](#class.copy.ctor-15.sentence-2)
|
||||
|
||||
[89)](#footnote-89)[89)](#footnoteref-89)
|
||||
|
||||
This implies that the reference parameter of the
|
||||
implicitly-declared copy constructor
|
||||
cannot bind to avolatile lvalue; see [[diff.class]](diff.class "C.7.7 [class]: classes")[.](#footnote-89.sentence-1)
|
||||
Reference in New Issue
Block a user