tmp/tmpe_zuup8p/{from.md → to.md}
RENAMED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
| 2 |
|
| 3 |
A non-template constructor for class `X` is a copy constructor if its
|
| 4 |
first parameter is of type `X&`, `const X&`, `volatile X&` or
|
| 5 |
`const volatile X&`, and either there are no other parameters or else
|
| 6 |
-
all other parameters have default arguments
|
| 7 |
|
| 8 |
[*Example 1*:
|
| 9 |
|
| 10 |
`X::X(const X&)`
|
| 11 |
|
|
@@ -24,11 +24,11 @@ X c = b; // calls X(const X&, int);
|
|
| 24 |
— *end example*]
|
| 25 |
|
| 26 |
A non-template constructor for class `X` is a move constructor if its
|
| 27 |
first parameter is of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 28 |
`const volatile X&&`, and either there are no other parameters or else
|
| 29 |
-
all other parameters have default arguments
|
| 30 |
|
| 31 |
[*Example 2*:
|
| 32 |
|
| 33 |
`Y::Y(Y&&)` is a move constructor.
|
| 34 |
|
|
@@ -65,11 +65,11 @@ struct X {
|
|
| 65 |
|
| 66 |
[*Note 2*:
|
| 67 |
|
| 68 |
If a class `X` only has a copy constructor with a parameter of type
|
| 69 |
`X&`, an initializer of type `const` `X` or `volatile` `X` cannot
|
| 70 |
-
initialize an object of type
|
| 71 |
|
| 72 |
[*Example 4*:
|
| 73 |
|
| 74 |
``` cpp
|
| 75 |
struct X {
|
|
@@ -83,14 +83,14 @@ X x = cx; // error: X::X(X&) cannot copy cx into x
|
|
| 83 |
— *end example*]
|
| 84 |
|
| 85 |
— *end note*]
|
| 86 |
|
| 87 |
A declaration of a constructor for a class `X` is ill-formed if its
|
| 88 |
-
first parameter is of type
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
[*Example 5*:
|
| 94 |
|
| 95 |
``` cpp
|
| 96 |
struct S {
|
|
@@ -110,24 +110,24 @@ void h() {
|
|
| 110 |
|
| 111 |
If the class definition does not explicitly declare a copy constructor,
|
| 112 |
a non-explicit one is declared *implicitly*. If the class definition
|
| 113 |
declares a move constructor or move assignment operator, the implicitly
|
| 114 |
declared copy constructor is defined as deleted; otherwise, it is
|
| 115 |
-
defined as defaulted
|
| 116 |
-
|
| 117 |
-
user-declared destructor.
|
| 118 |
|
| 119 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 120 |
form
|
| 121 |
|
| 122 |
``` cpp
|
| 123 |
X::X(const X&)
|
| 124 |
```
|
| 125 |
|
| 126 |
if each potentially constructed subobject of a class type `M` (or array
|
| 127 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 128 |
-
`M&` or `const` `volatile` `M&`.[^
|
| 129 |
copy constructor will have the form
|
| 130 |
|
| 131 |
``` cpp
|
| 132 |
X::X(X&)
|
| 133 |
```
|
|
@@ -150,64 +150,63 @@ form
|
|
| 150 |
|
| 151 |
``` cpp
|
| 152 |
X::X(X&&)
|
| 153 |
```
|
| 154 |
|
| 155 |
-
An implicitly-declared copy/move constructor is an
|
| 156 |
-
|
| 157 |
-
|
| 158 |
|
| 159 |
-
- a variant member with a non-trivial corresponding constructor and `X`
|
| 160 |
-
is a union-like class,
|
| 161 |
- a potentially constructed subobject type `M` (or array thereof) that
|
| 162 |
-
cannot be copied/moved because overload resolution
|
| 163 |
-
|
| 164 |
ambiguity or a function that is deleted or inaccessible from the
|
| 165 |
defaulted constructor,
|
|
|
|
|
|
|
| 166 |
- any potentially constructed subobject of a type with a destructor that
|
| 167 |
is deleted or inaccessible from the defaulted constructor, or,
|
| 168 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 169 |
type.
|
| 170 |
|
| 171 |
-
A defaulted move constructor that is defined as deleted is
|
| 172 |
-
overload resolution ([[over.match]], [[over.over]]).
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
initialization from an rvalue which can use the copy constructor
|
| 176 |
-
instead. — *end note*]
|
| 177 |
|
| 178 |
A copy/move constructor for class `X` is trivial if it is not
|
| 179 |
user-provided and if:
|
| 180 |
|
| 181 |
-
- class `X` has no virtual functions
|
| 182 |
-
base classes
|
| 183 |
- the constructor selected to copy/move each direct base class subobject
|
| 184 |
is trivial, and
|
| 185 |
- for each non-static data member of `X` that is of class type (or array
|
| 186 |
thereof), the constructor selected to copy/move that member is
|
| 187 |
trivial;
|
| 188 |
|
| 189 |
otherwise the copy/move constructor is *non-trivial*.
|
| 190 |
|
| 191 |
A copy/move constructor that is defaulted and not defined as deleted is
|
| 192 |
-
*implicitly defined*
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 196 |
-
implementation elided its odr-use
|
| 197 |
[[class.temporary]]). — *end note*]
|
| 198 |
|
| 199 |
If the implicitly-defined constructor would satisfy the requirements of
|
| 200 |
-
a constexpr constructor
|
| 201 |
constructor is `constexpr`.
|
| 202 |
|
| 203 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 204 |
defined, all non-user-provided copy/move constructors for its
|
| 205 |
-
potentially constructed subobjects
|
| 206 |
|
| 207 |
[*Note 6*: An implicitly-declared copy/move constructor has an implied
|
| 208 |
-
exception specification
|
| 209 |
|
| 210 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 211 |
performs a memberwise copy/move of its bases and members.
|
| 212 |
|
| 213 |
[*Note 7*: Default member initializers of non-static data members are
|
|
@@ -229,7 +228,11 @@ to its type:
|
|
| 229 |
|
| 230 |
Virtual base class subobjects shall be initialized only once by the
|
| 231 |
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 232 |
|
| 233 |
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 234 |
-
object representation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
|
|
|
| 1 |
+
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
| 2 |
|
| 3 |
A non-template constructor for class `X` is a copy constructor if its
|
| 4 |
first parameter is of type `X&`, `const X&`, `volatile X&` or
|
| 5 |
`const volatile X&`, and either there are no other parameters or else
|
| 6 |
+
all other parameters have default arguments [[dcl.fct.default]].
|
| 7 |
|
| 8 |
[*Example 1*:
|
| 9 |
|
| 10 |
`X::X(const X&)`
|
| 11 |
|
|
|
|
| 24 |
— *end example*]
|
| 25 |
|
| 26 |
A non-template constructor for class `X` is a move constructor if its
|
| 27 |
first parameter is of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 28 |
`const volatile X&&`, and either there are no other parameters or else
|
| 29 |
+
all other parameters have default arguments [[dcl.fct.default]].
|
| 30 |
|
| 31 |
[*Example 2*:
|
| 32 |
|
| 33 |
`Y::Y(Y&&)` is a move constructor.
|
| 34 |
|
|
|
|
| 65 |
|
| 66 |
[*Note 2*:
|
| 67 |
|
| 68 |
If a class `X` only has a copy constructor with a parameter of type
|
| 69 |
`X&`, an initializer of type `const` `X` or `volatile` `X` cannot
|
| 70 |
+
initialize an object of type cv `X`.
|
| 71 |
|
| 72 |
[*Example 4*:
|
| 73 |
|
| 74 |
``` cpp
|
| 75 |
struct X {
|
|
|
|
| 83 |
— *end example*]
|
| 84 |
|
| 85 |
— *end note*]
|
| 86 |
|
| 87 |
A declaration of a constructor for a class `X` is ill-formed if its
|
| 88 |
+
first parameter is of type cv `X` and either there are no other
|
| 89 |
+
parameters or else all other parameters have default arguments. A member
|
| 90 |
+
function template is never instantiated to produce such a constructor
|
| 91 |
+
signature.
|
| 92 |
|
| 93 |
[*Example 5*:
|
| 94 |
|
| 95 |
``` cpp
|
| 96 |
struct S {
|
|
|
|
| 110 |
|
| 111 |
If the class definition does not explicitly declare a copy constructor,
|
| 112 |
a non-explicit one is declared *implicitly*. If the class definition
|
| 113 |
declares a move constructor or move assignment operator, the implicitly
|
| 114 |
declared copy constructor is defined as deleted; otherwise, it is
|
| 115 |
+
defined as defaulted [[dcl.fct.def]]. The latter case is deprecated if
|
| 116 |
+
the class has a user-declared copy assignment operator or a
|
| 117 |
+
user-declared destructor [[depr.impldec]].
|
| 118 |
|
| 119 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 120 |
form
|
| 121 |
|
| 122 |
``` cpp
|
| 123 |
X::X(const X&)
|
| 124 |
```
|
| 125 |
|
| 126 |
if each potentially constructed subobject of a class type `M` (or array
|
| 127 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 128 |
+
`M&` or `const` `volatile` `M&`.[^3] Otherwise, the implicitly-declared
|
| 129 |
copy constructor will have the form
|
| 130 |
|
| 131 |
``` cpp
|
| 132 |
X::X(X&)
|
| 133 |
```
|
|
|
|
| 150 |
|
| 151 |
``` cpp
|
| 152 |
X::X(X&&)
|
| 153 |
```
|
| 154 |
|
| 155 |
+
An implicitly-declared copy/move constructor is an inline public member
|
| 156 |
+
of its class. A defaulted copy/move constructor for a class `X` is
|
| 157 |
+
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 158 |
|
|
|
|
|
|
|
| 159 |
- a potentially constructed subobject type `M` (or array thereof) that
|
| 160 |
+
cannot be copied/moved because overload resolution [[over.match]], as
|
| 161 |
+
applied to find `M`’s corresponding constructor, results in an
|
| 162 |
ambiguity or a function that is deleted or inaccessible from the
|
| 163 |
defaulted constructor,
|
| 164 |
+
- a variant member whose corresponding constructor as selected by
|
| 165 |
+
overload resolution is non-trivial,
|
| 166 |
- any potentially constructed subobject of a type with a destructor that
|
| 167 |
is deleted or inaccessible from the defaulted constructor, or,
|
| 168 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 169 |
type.
|
| 170 |
|
| 171 |
+
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 172 |
+
ignored by overload resolution ([[over.match]], [[over.over]]). Such a
|
| 173 |
+
constructor would otherwise interfere with initialization from an rvalue
|
| 174 |
+
which can use the copy constructor instead. — *end note*]
|
|
|
|
|
|
|
| 175 |
|
| 176 |
A copy/move constructor for class `X` is trivial if it is not
|
| 177 |
user-provided and if:
|
| 178 |
|
| 179 |
+
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 180 |
+
base classes [[class.mi]], and
|
| 181 |
- the constructor selected to copy/move each direct base class subobject
|
| 182 |
is trivial, and
|
| 183 |
- for each non-static data member of `X` that is of class type (or array
|
| 184 |
thereof), the constructor selected to copy/move that member is
|
| 185 |
trivial;
|
| 186 |
|
| 187 |
otherwise the copy/move constructor is *non-trivial*.
|
| 188 |
|
| 189 |
A copy/move constructor that is defaulted and not defined as deleted is
|
| 190 |
+
*implicitly defined* when it is odr-used [[basic.def.odr]], when it is
|
| 191 |
+
needed for constant evaluation [[expr.const]], or when it is explicitly
|
| 192 |
+
defaulted after its first declaration.
|
| 193 |
|
| 194 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 195 |
+
implementation elided its odr-use ([[basic.def.odr]],
|
| 196 |
[[class.temporary]]). — *end note*]
|
| 197 |
|
| 198 |
If the implicitly-defined constructor would satisfy the requirements of
|
| 199 |
+
a constexpr constructor [[dcl.constexpr]], the implicitly-defined
|
| 200 |
constructor is `constexpr`.
|
| 201 |
|
| 202 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 203 |
defined, all non-user-provided copy/move constructors for its
|
| 204 |
+
potentially constructed subobjects are implicitly defined.
|
| 205 |
|
| 206 |
[*Note 6*: An implicitly-declared copy/move constructor has an implied
|
| 207 |
+
exception specification [[except.spec]]. — *end note*]
|
| 208 |
|
| 209 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 210 |
performs a memberwise copy/move of its bases and members.
|
| 211 |
|
| 212 |
[*Note 7*: Default member initializers of non-static data members are
|
|
|
|
| 228 |
|
| 229 |
Virtual base class subobjects shall be initialized only once by the
|
| 230 |
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 231 |
|
| 232 |
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 233 |
+
object representation [[basic.types]] of `X`. For each object nested
|
| 234 |
+
within [[intro.object]] the object that is the source of the copy, a
|
| 235 |
+
corresponding object o nested within the destination is identified (if
|
| 236 |
+
the object is a subobject) or created (otherwise), and the lifetime of o
|
| 237 |
+
begins before the copy is performed.
|
| 238 |
|