tmp/tmpivy1od9c/{from.md → to.md}
RENAMED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 2 |
|
| 3 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 4 |
non-static non-template member function of class `X` with exactly one
|
| 5 |
-
parameter of type `X`, `X&`, `const
|
| 6 |
-
`volatile
|
| 7 |
|
| 8 |
[*Note 1*: An overloaded assignment operator must be declared to have
|
| 9 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 10 |
|
| 11 |
[*Note 2*: More than one form of copy assignment operator may be
|
|
@@ -37,27 +37,27 @@ void f() {
|
|
| 37 |
|
| 38 |
If the class definition does not explicitly declare a copy assignment
|
| 39 |
operator, one is declared *implicitly*. If the class definition declares
|
| 40 |
a move constructor or move assignment operator, the implicitly declared
|
| 41 |
copy assignment operator is defined as deleted; otherwise, it is defined
|
| 42 |
-
as defaulted
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
`X` will have the form
|
| 46 |
|
| 47 |
``` cpp
|
| 48 |
X& X::operator=(const X&)
|
| 49 |
```
|
| 50 |
|
| 51 |
if
|
| 52 |
|
| 53 |
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 54 |
-
parameter is of type `const
|
| 55 |
- for all the non-static data members of `X` that are of a class type
|
| 56 |
`M` (or array thereof), each such class type has a copy assignment
|
| 57 |
-
operator whose parameter is of type `const
|
| 58 |
-
|
| 59 |
|
| 60 |
Otherwise, the implicitly-declared copy assignment operator will have
|
| 61 |
the form
|
| 62 |
|
| 63 |
``` cpp
|
|
@@ -110,18 +110,18 @@ struct S {
|
|
| 110 |
|
| 111 |
The implicitly-declared move assignment operator for a class `X` will
|
| 112 |
have the form
|
| 113 |
|
| 114 |
``` cpp
|
| 115 |
-
X& X::operator=(X&&)
|
| 116 |
```
|
| 117 |
|
| 118 |
The implicitly-declared copy/move assignment operator for class `X` has
|
| 119 |
the return type `X&`; it returns the object for which the assignment
|
| 120 |
operator is invoked, that is, the object assigned to. An
|
| 121 |
-
implicitly-declared copy/move assignment operator is an
|
| 122 |
-
|
| 123 |
|
| 124 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 125 |
deleted if `X` has:
|
| 126 |
|
| 127 |
- a variant member with a non-trivial corresponding assignment operator
|
|
@@ -129,64 +129,65 @@ deleted if `X` has:
|
|
| 129 |
- a non-static data member of `const` non-class type (or array thereof),
|
| 130 |
or
|
| 131 |
- a non-static data member of reference type, or
|
| 132 |
- a direct non-static data member of class type `M` (or array thereof)
|
| 133 |
or a direct base class `M` that cannot be copied/moved because
|
| 134 |
-
overload resolution
|
| 135 |
corresponding assignment operator, results in an ambiguity or a
|
| 136 |
function that is deleted or inaccessible from the defaulted assignment
|
| 137 |
operator.
|
| 138 |
|
| 139 |
-
A defaulted move assignment operator that is defined as
|
| 140 |
-
ignored by overload resolution ([[over.match]],
|
|
|
|
| 141 |
|
| 142 |
Because a copy/move assignment operator is implicitly declared for a
|
| 143 |
class if not declared by the user, a base class copy/move assignment
|
| 144 |
operator is always hidden by the corresponding assignment operator of a
|
| 145 |
-
derived class
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
the derived class.
|
| 153 |
|
| 154 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 155 |
user-provided and if:
|
| 156 |
|
| 157 |
-
- class `X` has no virtual functions
|
| 158 |
-
base classes
|
| 159 |
- the assignment operator selected to copy/move each direct base class
|
| 160 |
subobject is trivial, and
|
| 161 |
- for each non-static data member of `X` that is of class type (or array
|
| 162 |
thereof), the assignment operator selected to copy/move that member is
|
| 163 |
trivial;
|
| 164 |
|
| 165 |
otherwise the copy/move assignment operator is *non-trivial*.
|
| 166 |
|
| 167 |
A copy/move assignment operator for a class `X` that is defaulted and
|
| 168 |
-
not defined as deleted is *implicitly defined* when it is odr-used
|
| 169 |
-
[[basic.def.odr]]
|
| 170 |
-
assign to an object of its class type)
|
| 171 |
-
|
| 172 |
-
|
|
|
|
| 173 |
|
| 174 |
- `X` is a literal type, and
|
| 175 |
- the assignment operator selected to copy/move each direct base class
|
| 176 |
subobject is a constexpr function, and
|
| 177 |
- for each non-static data member of `X` that is of class type (or array
|
| 178 |
thereof), the assignment operator selected to copy/move that member is
|
| 179 |
a constexpr function.
|
| 180 |
|
| 181 |
Before the defaulted copy/move assignment operator for a class is
|
| 182 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 183 |
-
for its direct base classes and its non-static data members
|
| 184 |
-
|
| 185 |
|
| 186 |
-
[*Note
|
| 187 |
-
implied exception specification
|
| 188 |
|
| 189 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 190 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 191 |
The direct base classes of `X` are assigned first, in the order of their
|
| 192 |
declaration in the *base-specifier-list*, and then the immediate
|
|
@@ -224,7 +225,11 @@ assigned twice by the implicitly-defined copy/move assignment operator
|
|
| 224 |
for `C`.
|
| 225 |
|
| 226 |
— *end example*]
|
| 227 |
|
| 228 |
The implicitly-defined copy assignment operator for a union `X` copies
|
| 229 |
-
the object representation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
|
|
|
| 1 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 2 |
|
| 3 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 4 |
non-static non-template member function of class `X` with exactly one
|
| 5 |
+
parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
|
| 6 |
+
`const volatile X&`.[^4]
|
| 7 |
|
| 8 |
[*Note 1*: An overloaded assignment operator must be declared to have
|
| 9 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 10 |
|
| 11 |
[*Note 2*: More than one form of copy assignment operator may be
|
|
|
|
| 37 |
|
| 38 |
If the class definition does not explicitly declare a copy assignment
|
| 39 |
operator, one is declared *implicitly*. If the class definition declares
|
| 40 |
a move constructor or move assignment operator, the implicitly declared
|
| 41 |
copy assignment operator is defined as deleted; otherwise, it is defined
|
| 42 |
+
as defaulted [[dcl.fct.def]]. The latter case is deprecated if the class
|
| 43 |
+
has a user-declared copy constructor or a user-declared destructor
|
| 44 |
+
[[depr.impldec]]. The implicitly-declared copy assignment operator for a
|
| 45 |
+
class `X` will have the form
|
| 46 |
|
| 47 |
``` cpp
|
| 48 |
X& X::operator=(const X&)
|
| 49 |
```
|
| 50 |
|
| 51 |
if
|
| 52 |
|
| 53 |
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 54 |
+
parameter is of type `const B&`, `const volatile B&`, or `B`, and
|
| 55 |
- for all the non-static data members of `X` that are of a class type
|
| 56 |
`M` (or array thereof), each such class type has a copy assignment
|
| 57 |
+
operator whose parameter is of type `const M&`, `const volatile M&`,
|
| 58 |
+
or `M`.[^5]
|
| 59 |
|
| 60 |
Otherwise, the implicitly-declared copy assignment operator will have
|
| 61 |
the form
|
| 62 |
|
| 63 |
``` cpp
|
|
|
|
| 110 |
|
| 111 |
The implicitly-declared move assignment operator for a class `X` will
|
| 112 |
have the form
|
| 113 |
|
| 114 |
``` cpp
|
| 115 |
+
X& X::operator=(X&&)
|
| 116 |
```
|
| 117 |
|
| 118 |
The implicitly-declared copy/move assignment operator for class `X` has
|
| 119 |
the return type `X&`; it returns the object for which the assignment
|
| 120 |
operator is invoked, that is, the object assigned to. An
|
| 121 |
+
implicitly-declared copy/move assignment operator is an inline public
|
| 122 |
+
member of its class.
|
| 123 |
|
| 124 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 125 |
deleted if `X` has:
|
| 126 |
|
| 127 |
- a variant member with a non-trivial corresponding assignment operator
|
|
|
|
| 129 |
- a non-static data member of `const` non-class type (or array thereof),
|
| 130 |
or
|
| 131 |
- a non-static data member of reference type, or
|
| 132 |
- a direct non-static data member of class type `M` (or array thereof)
|
| 133 |
or a direct base class `M` that cannot be copied/moved because
|
| 134 |
+
overload resolution [[over.match]], as applied to find `M`’s
|
| 135 |
corresponding assignment operator, results in an ambiguity or a
|
| 136 |
function that is deleted or inaccessible from the defaulted assignment
|
| 137 |
operator.
|
| 138 |
|
| 139 |
+
[*Note 6*: A defaulted move assignment operator that is defined as
|
| 140 |
+
deleted is ignored by overload resolution ([[over.match]],
|
| 141 |
+
[[over.over]]). — *end note*]
|
| 142 |
|
| 143 |
Because a copy/move assignment operator is implicitly declared for a
|
| 144 |
class if not declared by the user, a base class copy/move assignment
|
| 145 |
operator is always hidden by the corresponding assignment operator of a
|
| 146 |
+
derived class [[over.ass]]. A *using-declaration* [[namespace.udecl]]
|
| 147 |
+
that brings in from a base class an assignment operator with a parameter
|
| 148 |
+
type that could be that of a copy/move assignment operator for the
|
| 149 |
+
derived class is not considered an explicit declaration of such an
|
| 150 |
+
operator and does not suppress the implicit declaration of the derived
|
| 151 |
+
class operator; the operator introduced by the *using-declaration* is
|
| 152 |
+
hidden by the implicitly-declared operator in the derived class.
|
|
|
|
| 153 |
|
| 154 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 155 |
user-provided and if:
|
| 156 |
|
| 157 |
+
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 158 |
+
base classes [[class.mi]], and
|
| 159 |
- the assignment operator selected to copy/move each direct base class
|
| 160 |
subobject is trivial, and
|
| 161 |
- for each non-static data member of `X` that is of class type (or array
|
| 162 |
thereof), the assignment operator selected to copy/move that member is
|
| 163 |
trivial;
|
| 164 |
|
| 165 |
otherwise the copy/move assignment operator is *non-trivial*.
|
| 166 |
|
| 167 |
A copy/move assignment operator for a class `X` that is defaulted and
|
| 168 |
+
not defined as deleted is *implicitly defined* when it is odr-used
|
| 169 |
+
[[basic.def.odr]] (e.g., when it is selected by overload resolution to
|
| 170 |
+
assign to an object of its class type), when it is needed for constant
|
| 171 |
+
evaluation [[expr.const]], or when it is explicitly defaulted after its
|
| 172 |
+
first declaration. The implicitly-defined copy/move assignment operator
|
| 173 |
+
is `constexpr` if
|
| 174 |
|
| 175 |
- `X` is a literal type, and
|
| 176 |
- the assignment operator selected to copy/move each direct base class
|
| 177 |
subobject is a constexpr function, and
|
| 178 |
- for each non-static data member of `X` that is of class type (or array
|
| 179 |
thereof), the assignment operator selected to copy/move that member is
|
| 180 |
a constexpr function.
|
| 181 |
|
| 182 |
Before the defaulted copy/move assignment operator for a class is
|
| 183 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 184 |
+
for its direct base classes and its non-static data members are
|
| 185 |
+
implicitly defined.
|
| 186 |
|
| 187 |
+
[*Note 7*: An implicitly-declared copy/move assignment operator has an
|
| 188 |
+
implied exception specification [[except.spec]]. — *end note*]
|
| 189 |
|
| 190 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 191 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 192 |
The direct base classes of `X` are assigned first, in the order of their
|
| 193 |
declaration in the *base-specifier-list*, and then the immediate
|
|
|
|
| 225 |
for `C`.
|
| 226 |
|
| 227 |
— *end example*]
|
| 228 |
|
| 229 |
The implicitly-defined copy assignment operator for a union `X` copies
|
| 230 |
+
the object representation [[basic.types]] of `X`. If the source and
|
| 231 |
+
destination of the assignment are not the same object, then for each
|
| 232 |
+
object nested within [[intro.object]] the object that is the source of
|
| 233 |
+
the copy, a corresponding object o nested within the destination is
|
| 234 |
+
created, and the lifetime of o begins before the copy is performed.
|
| 235 |
|