- tmp/tmpbvzszv8l/{from.md → to.md} +33 -2602
tmp/tmpbvzszv8l/{from.md → to.md}
RENAMED
|
@@ -1,37 +1,37 @@
|
|
| 1 |
-
# Special member functions <a id="special">[[special]]</a>
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
assignment
|
| 6 |
-
are *special member functions*.
|
| 7 |
|
| 8 |
[*Note 1*: The implementation will implicitly declare these member
|
| 9 |
functions for some class types when the program does not explicitly
|
| 10 |
declare them. The implementation will implicitly define them if they are
|
| 11 |
-
odr-used
|
| 12 |
-
[[
|
| 13 |
|
| 14 |
An implicitly-declared special member function is declared at the
|
| 15 |
closing `}` of the *class-specifier*. Programs shall not define
|
| 16 |
implicitly-declared special member functions.
|
| 17 |
|
| 18 |
Programs may explicitly refer to implicitly-declared special member
|
| 19 |
functions.
|
| 20 |
|
| 21 |
[*Example 1*:
|
| 22 |
|
| 23 |
-
A program may explicitly call
|
| 24 |
-
|
| 25 |
|
| 26 |
``` cpp
|
| 27 |
struct A { }; // implicitly declared A::operator=
|
| 28 |
struct B : A {
|
| 29 |
B& operator=(const B &);
|
| 30 |
};
|
| 31 |
B& B::operator=(const B& s) {
|
| 32 |
-
this->A::operator=(s); // well
|
| 33 |
return *this;
|
| 34 |
}
|
| 35 |
```
|
| 36 |
|
| 37 |
— *end example*]
|
|
@@ -39,2606 +39,37 @@ B& B::operator=(const B& s) {
|
|
| 39 |
[*Note 2*: The special member functions affect the way objects of class
|
| 40 |
type are created, copied, moved, and destroyed, and how values can be
|
| 41 |
converted to values of other types. Often such special member functions
|
| 42 |
are called implicitly. — *end note*]
|
| 43 |
|
| 44 |
-
Special member functions obey the usual access rules
|
| 45 |
-
[[class.access]]).
|
| 46 |
|
| 47 |
-
[*Example 2*: Declaring a constructor
|
| 48 |
derived classes and friends can create objects using
|
| 49 |
it. — *end example*]
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
For a class, its non-static data members, its non-virtual direct base
|
| 52 |
-
classes, and, if the class is not abstract
|
| 53 |
virtual base classes are called its *potentially constructed
|
| 54 |
subobjects*.
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
Constructors do not have names. In a declaration of a constructor, the
|
| 59 |
-
*declarator* is a function declarator ([[dcl.fct]]) of the form
|
| 60 |
-
|
| 61 |
-
``` bnf
|
| 62 |
-
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 63 |
-
```
|
| 64 |
-
|
| 65 |
-
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 66 |
-
optional *attribute-specifier-seq*, and optional surrounding
|
| 67 |
-
parentheses, and the *id-expression* has one of the following forms:
|
| 68 |
-
|
| 69 |
-
- in a *member-declaration* that belongs to the *member-specification*
|
| 70 |
-
of a class but is not a friend declaration ([[class.friend]]), the
|
| 71 |
-
*id-expression* is the injected-class-name (Clause [[class]]) of the
|
| 72 |
-
immediately-enclosing class;
|
| 73 |
-
- in a *member-declaration* that belongs to the *member-specification*
|
| 74 |
-
of a class template but is not a friend declaration, the
|
| 75 |
-
*id-expression* is a *class-name* that names the current
|
| 76 |
-
instantiation ([[temp.dep.type]]) of the immediately-enclosing class
|
| 77 |
-
template; or
|
| 78 |
-
- in a declaration at namespace scope or in a friend declaration, the
|
| 79 |
-
*id-expression* is a *qualified-id* that names a constructor (
|
| 80 |
-
[[class.qual]]).
|
| 81 |
-
|
| 82 |
-
The *class-name* shall not be a *typedef-name*. In a constructor
|
| 83 |
-
declaration, each *decl-specifier* in the optional *decl-specifier-seq*
|
| 84 |
-
shall be `friend`, `inline`, `explicit`, or `constexpr`.
|
| 85 |
-
|
| 86 |
-
[*Example 1*:
|
| 87 |
-
|
| 88 |
-
``` cpp
|
| 89 |
-
struct S {
|
| 90 |
-
S(); // declares the constructor
|
| 91 |
-
};
|
| 92 |
-
|
| 93 |
-
S::S() { } // defines the constructor
|
| 94 |
-
```
|
| 95 |
-
|
| 96 |
-
— *end example*]
|
| 97 |
-
|
| 98 |
-
A constructor is used to initialize objects of its class type. Because
|
| 99 |
-
constructors do not have names, they are never found during name lookup;
|
| 100 |
-
however an explicit type conversion using the functional notation (
|
| 101 |
-
[[expr.type.conv]]) will cause a constructor to be called to initialize
|
| 102 |
-
an object.
|
| 103 |
-
|
| 104 |
-
[*Note 1*: For initialization of objects of class type see
|
| 105 |
-
[[class.init]]. — *end note*]
|
| 106 |
-
|
| 107 |
-
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 108 |
-
`volatile` object. `const` and `volatile` semantics ([[dcl.type.cv]])
|
| 109 |
-
are not applied on an object under construction. They come into effect
|
| 110 |
-
when the constructor for the most derived object ([[intro.object]])
|
| 111 |
-
ends.
|
| 112 |
-
|
| 113 |
-
A *default* constructor for a class `X` is a constructor of class `X`
|
| 114 |
-
for which each parameter that is not a function parameter pack has a
|
| 115 |
-
default argument (including the case of a constructor with no
|
| 116 |
-
parameters). If there is no user-declared constructor for class `X`, a
|
| 117 |
-
non-explicit constructor having no parameters is implicitly declared as
|
| 118 |
-
defaulted ([[dcl.fct.def]]). An implicitly-declared default constructor
|
| 119 |
-
is an `inline` `public` member of its class.
|
| 120 |
-
|
| 121 |
-
A defaulted default constructor for class `X` is defined as deleted if:
|
| 122 |
-
|
| 123 |
-
- `X` is a union that has a variant member with a non-trivial default
|
| 124 |
-
constructor and no variant member of `X` has a default member
|
| 125 |
-
initializer,
|
| 126 |
-
- `X` is a non-union class that has a variant member `M` with a
|
| 127 |
-
non-trivial default constructor and no variant member of the anonymous
|
| 128 |
-
union containing `M` has a default member initializer,
|
| 129 |
-
- any non-static data member with no default member initializer (
|
| 130 |
-
[[class.mem]]) is of reference type,
|
| 131 |
-
- any non-variant non-static data member of const-qualified type (or
|
| 132 |
-
array thereof) with no *brace-or-equal-initializer* does not have a
|
| 133 |
-
user-provided default constructor,
|
| 134 |
-
- `X` is a union and all of its variant members are of const-qualified
|
| 135 |
-
type (or array thereof),
|
| 136 |
-
- `X` is a non-union class and all members of any anonymous union member
|
| 137 |
-
are of const-qualified type (or array thereof),
|
| 138 |
-
- any potentially constructed subobject, except for a non-static data
|
| 139 |
-
member with a *brace-or-equal-initializer*, has class type `M` (or
|
| 140 |
-
array thereof) and either `M` has no default constructor or overload
|
| 141 |
-
resolution ([[over.match]]) as applied to find `M`’s corresponding
|
| 142 |
-
constructor results in an ambiguity or in a function that is deleted
|
| 143 |
-
or inaccessible from the defaulted default constructor, or
|
| 144 |
-
- any potentially constructed subobject has a type with a destructor
|
| 145 |
-
that is deleted or inaccessible from the defaulted default
|
| 146 |
-
constructor.
|
| 147 |
-
|
| 148 |
-
A default constructor is *trivial* if it is not user-provided and if:
|
| 149 |
-
|
| 150 |
-
- its class has no virtual functions ([[class.virtual]]) and no virtual
|
| 151 |
-
base classes ([[class.mi]]), and
|
| 152 |
-
- no non-static data member of its class has a default member
|
| 153 |
-
initializer ([[class.mem]]), and
|
| 154 |
-
- all the direct base classes of its class have trivial default
|
| 155 |
-
constructors, and
|
| 156 |
-
- for all the non-static data members of its class that are of class
|
| 157 |
-
type (or array thereof), each such class has a trivial default
|
| 158 |
-
constructor.
|
| 159 |
-
|
| 160 |
-
Otherwise, the default constructor is *non-trivial*.
|
| 161 |
-
|
| 162 |
-
A default constructor that is defaulted and not defined as deleted is
|
| 163 |
-
*implicitly defined* when it is odr-used ([[basic.def.odr]]) to create
|
| 164 |
-
an object of its class type ([[intro.object]]) or when it is explicitly
|
| 165 |
-
defaulted after its first declaration. The implicitly-defined default
|
| 166 |
-
constructor performs the set of initializations of the class that would
|
| 167 |
-
be performed by a user-written default constructor for that class with
|
| 168 |
-
no *ctor-initializer* ([[class.base.init]]) and an empty
|
| 169 |
-
*compound-statement*. If that user-written default constructor would be
|
| 170 |
-
ill-formed, the program is ill-formed. If that user-written default
|
| 171 |
-
constructor would satisfy the requirements of a constexpr constructor (
|
| 172 |
-
[[dcl.constexpr]]), the implicitly-defined default constructor is
|
| 173 |
-
`constexpr`. Before the defaulted default constructor for a class is
|
| 174 |
-
implicitly defined, all the non-user-provided default constructors for
|
| 175 |
-
its base classes and its non-static data members shall have been
|
| 176 |
-
implicitly defined.
|
| 177 |
-
|
| 178 |
-
[*Note 2*: An implicitly-declared default constructor has an exception
|
| 179 |
-
specification ([[except.spec]]). An explicitly-defaulted definition
|
| 180 |
-
might have an implicit exception specification, see
|
| 181 |
-
[[dcl.fct.def]]. — *end note*]
|
| 182 |
-
|
| 183 |
-
Default constructors are called implicitly to create class objects of
|
| 184 |
-
static, thread, or automatic storage duration ([[basic.stc.static]],
|
| 185 |
-
[[basic.stc.thread]], [[basic.stc.auto]]) defined without an
|
| 186 |
-
initializer ([[dcl.init]]), are called to create class objects of
|
| 187 |
-
dynamic storage duration ([[basic.stc.dynamic]]) created by a
|
| 188 |
-
*new-expression* in which the *new-initializer* is omitted (
|
| 189 |
-
[[expr.new]]), or are called when the explicit type conversion syntax (
|
| 190 |
-
[[expr.type.conv]]) is used. A program is ill-formed if the default
|
| 191 |
-
constructor for an object is implicitly used and the constructor is not
|
| 192 |
-
accessible (Clause [[class.access]]).
|
| 193 |
-
|
| 194 |
-
[*Note 3*: [[class.base.init]] describes the order in which
|
| 195 |
-
constructors for base classes and non-static data members are called and
|
| 196 |
-
describes how arguments can be specified for the calls to these
|
| 197 |
-
constructors. — *end note*]
|
| 198 |
-
|
| 199 |
-
A `return` statement in the body of a constructor shall not specify a
|
| 200 |
-
return value. The address of a constructor shall not be taken.
|
| 201 |
-
|
| 202 |
-
A functional notation type conversion ([[expr.type.conv]]) can be used
|
| 203 |
-
to create new objects of its type.
|
| 204 |
-
|
| 205 |
-
[*Note 4*: The syntax looks like an explicit call of the
|
| 206 |
-
constructor. — *end note*]
|
| 207 |
-
|
| 208 |
-
[*Example 2*:
|
| 209 |
-
|
| 210 |
-
``` cpp
|
| 211 |
-
complex zz = complex(1,2.3);
|
| 212 |
-
cprint( complex(7.8,1.2) );
|
| 213 |
-
```
|
| 214 |
-
|
| 215 |
-
— *end example*]
|
| 216 |
-
|
| 217 |
-
An object created in this way is unnamed.
|
| 218 |
-
|
| 219 |
-
[*Note 5*: [[class.temporary]] describes the lifetime of temporary
|
| 220 |
-
objects. — *end note*]
|
| 221 |
-
|
| 222 |
-
[*Note 6*: Explicit constructor calls do not yield lvalues, see
|
| 223 |
-
[[basic.lval]]. — *end note*]
|
| 224 |
-
|
| 225 |
-
[*Note 7*: Some language constructs have special semantics when used
|
| 226 |
-
during construction; see [[class.base.init]] and
|
| 227 |
-
[[class.cdtor]]. — *end note*]
|
| 228 |
-
|
| 229 |
-
During the construction of an object, if the value of the object or any
|
| 230 |
-
of its subobjects is accessed through a glvalue that is not obtained,
|
| 231 |
-
directly or indirectly, from the constructor’s `this` pointer, the value
|
| 232 |
-
of the object or subobject thus obtained is unspecified.
|
| 233 |
-
|
| 234 |
-
[*Example 3*:
|
| 235 |
-
|
| 236 |
-
``` cpp
|
| 237 |
-
struct C;
|
| 238 |
-
void no_opt(C*);
|
| 239 |
-
|
| 240 |
-
struct C {
|
| 241 |
-
int c;
|
| 242 |
-
C() : c(0) { no_opt(this); }
|
| 243 |
-
};
|
| 244 |
-
|
| 245 |
-
const C cobj;
|
| 246 |
-
|
| 247 |
-
void no_opt(C* cptr) {
|
| 248 |
-
int i = cobj.c * 100; // value of cobj.c is unspecified
|
| 249 |
-
cptr->c = 1;
|
| 250 |
-
cout << cobj.c * 100 // value of cobj.c is unspecified
|
| 251 |
-
<< '\n';
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
extern struct D d;
|
| 255 |
-
struct D {
|
| 256 |
-
D(int a) : a(a), b(d.a) {}
|
| 257 |
-
int a, b;
|
| 258 |
-
};
|
| 259 |
-
D d = D(1); // value of d.b is unspecified
|
| 260 |
-
```
|
| 261 |
-
|
| 262 |
-
— *end example*]
|
| 263 |
-
|
| 264 |
-
## Temporary objects <a id="class.temporary">[[class.temporary]]</a>
|
| 265 |
-
|
| 266 |
-
Temporary objects are created
|
| 267 |
-
|
| 268 |
-
- when a prvalue is materialized so that it can be used as a glvalue (
|
| 269 |
-
[[conv.rval]]),
|
| 270 |
-
- when needed by the implementation to pass or return an object of
|
| 271 |
-
trivially-copyable type (see below), and
|
| 272 |
-
- when throwing an exception ([[except.throw]]). \[*Note 1*: The
|
| 273 |
-
lifetime of exception objects is described in
|
| 274 |
-
[[except.throw]]. — *end note*]
|
| 275 |
-
|
| 276 |
-
Even when the creation of the temporary object is unevaluated (Clause
|
| 277 |
-
[[expr]]), all the semantic restrictions shall be respected as if the
|
| 278 |
-
temporary object had been created and later destroyed.
|
| 279 |
-
|
| 280 |
-
[*Note 2*: This includes accessibility (Clause [[class.access]]) and
|
| 281 |
-
whether it is deleted, for the constructor selected and for the
|
| 282 |
-
destructor. However, in the special case of the operand of a
|
| 283 |
-
*decltype-specifier* ([[expr.call]]), no temporary is introduced, so
|
| 284 |
-
the foregoing does not apply to such a prvalue. — *end note*]
|
| 285 |
-
|
| 286 |
-
The materialization of a temporary object is generally delayed as long
|
| 287 |
-
as possible in order to avoid creating unnecessary temporary objects.
|
| 288 |
-
|
| 289 |
-
[*Note 3*:
|
| 290 |
-
|
| 291 |
-
Temporary objects are materialized:
|
| 292 |
-
|
| 293 |
-
- when binding a reference to a prvalue ([[dcl.init.ref]],
|
| 294 |
-
[[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]],
|
| 295 |
-
[[expr.const.cast]], [[expr.cast]]),
|
| 296 |
-
- when performing member access on a class prvalue ([[expr.ref]],
|
| 297 |
-
[[expr.mptr.oper]]),
|
| 298 |
-
- when performing an array-to-pointer conversion or subscripting on an
|
| 299 |
-
array prvalue ([[conv.array]], [[expr.sub]]),
|
| 300 |
-
- when initializing an object of type `std::initializer_list<T>` from a
|
| 301 |
-
*braced-init-list* ([[dcl.init.list]]),
|
| 302 |
-
- for certain unevaluated operands ([[expr.typeid]], [[expr.sizeof]]),
|
| 303 |
-
and
|
| 304 |
-
- when a prvalue appears as a discarded-value expression (Clause
|
| 305 |
-
[[expr]]).
|
| 306 |
-
|
| 307 |
-
— *end note*]
|
| 308 |
-
|
| 309 |
-
[*Example 1*:
|
| 310 |
-
|
| 311 |
-
Consider the following code:
|
| 312 |
-
|
| 313 |
-
``` cpp
|
| 314 |
-
class X {
|
| 315 |
-
public:
|
| 316 |
-
X(int);
|
| 317 |
-
X(const X&);
|
| 318 |
-
X& operator=(const X&);
|
| 319 |
-
~X();
|
| 320 |
-
};
|
| 321 |
-
|
| 322 |
-
class Y {
|
| 323 |
-
public:
|
| 324 |
-
Y(int);
|
| 325 |
-
Y(Y&&);
|
| 326 |
-
~Y();
|
| 327 |
-
};
|
| 328 |
-
|
| 329 |
-
X f(X);
|
| 330 |
-
Y g(Y);
|
| 331 |
-
|
| 332 |
-
void h() {
|
| 333 |
-
X a(1);
|
| 334 |
-
X b = f(X(2));
|
| 335 |
-
Y c = g(Y(3));
|
| 336 |
-
a = f(a);
|
| 337 |
-
}
|
| 338 |
-
```
|
| 339 |
-
|
| 340 |
-
`X(2)` is constructed in the space used to hold `f()`’s argument and
|
| 341 |
-
`Y(3)` is constructed in the space used to hold `g()`’s argument.
|
| 342 |
-
Likewise, `f()`’s result is constructed directly in `b` and `g()`’s
|
| 343 |
-
result is constructed directly in `c`. On the other hand, the expression
|
| 344 |
-
`a = f(a)` requires a temporary for the result of `f(a)`, which is
|
| 345 |
-
materialized so that the reference parameter of `A::operator=(const A&)`
|
| 346 |
-
can bind to it.
|
| 347 |
-
|
| 348 |
-
— *end example*]
|
| 349 |
-
|
| 350 |
-
When an object of class type `X` is passed to or returned from a
|
| 351 |
-
function, if each copy constructor, move constructor, and destructor of
|
| 352 |
-
`X` is either trivial or deleted, and `X` has at least one non-deleted
|
| 353 |
-
copy or move constructor, implementations are permitted to create a
|
| 354 |
-
temporary object to hold the function parameter or result object. The
|
| 355 |
-
temporary object is constructed from the function argument or return
|
| 356 |
-
value, respectively, and the function’s parameter or return object is
|
| 357 |
-
initialized as if by using the non-deleted trivial constructor to copy
|
| 358 |
-
the temporary (even if that constructor is inaccessible or would not be
|
| 359 |
-
selected by overload resolution to perform a copy or move of the
|
| 360 |
-
object).
|
| 361 |
-
|
| 362 |
-
[*Note 4*: This latitude is granted to allow objects of class type to
|
| 363 |
-
be passed to or returned from functions in registers. — *end note*]
|
| 364 |
-
|
| 365 |
-
When an implementation introduces a temporary object of a class that has
|
| 366 |
-
a non-trivial constructor ([[class.ctor]], [[class.copy]]), it shall
|
| 367 |
-
ensure that a constructor is called for the temporary object. Similarly,
|
| 368 |
-
the destructor shall be called for a temporary with a non-trivial
|
| 369 |
-
destructor ([[class.dtor]]). Temporary objects are destroyed as the
|
| 370 |
-
last step in evaluating the full-expression ([[intro.execution]]) that
|
| 371 |
-
(lexically) contains the point where they were created. This is true
|
| 372 |
-
even if that evaluation ends in throwing an exception. The value
|
| 373 |
-
computations and side effects of destroying a temporary object are
|
| 374 |
-
associated only with the full-expression, not with any specific
|
| 375 |
-
subexpression.
|
| 376 |
-
|
| 377 |
-
There are three contexts in which temporaries are destroyed at a
|
| 378 |
-
different point than the end of the full-expression. The first context
|
| 379 |
-
is when a default constructor is called to initialize an element of an
|
| 380 |
-
array with no corresponding initializer ([[dcl.init]]). The second
|
| 381 |
-
context is when a copy constructor is called to copy an element of an
|
| 382 |
-
array while the entire array is copied ([[expr.prim.lambda.capture]],
|
| 383 |
-
[[class.copy]]). In either case, if the constructor has one or more
|
| 384 |
-
default arguments, the destruction of every temporary created in a
|
| 385 |
-
default argument is sequenced before the construction of the next array
|
| 386 |
-
element, if any.
|
| 387 |
-
|
| 388 |
-
The third context is when a reference is bound to a temporary.[^1] The
|
| 389 |
-
temporary to which the reference is bound or the temporary that is the
|
| 390 |
-
complete object of a subobject to which the reference is bound persists
|
| 391 |
-
for the lifetime of the reference except:
|
| 392 |
-
|
| 393 |
-
- A temporary object bound to a reference parameter in a function call (
|
| 394 |
-
[[expr.call]]) persists until the completion of the full-expression
|
| 395 |
-
containing the call.
|
| 396 |
-
- The lifetime of a temporary bound to the returned value in a function
|
| 397 |
-
return statement ([[stmt.return]]) is not extended; the temporary is
|
| 398 |
-
destroyed at the end of the full-expression in the return statement.
|
| 399 |
-
- A temporary bound to a reference in a *new-initializer* (
|
| 400 |
-
[[expr.new]]) persists until the completion of the full-expression
|
| 401 |
-
containing the *new-initializer*.
|
| 402 |
-
\[*Example 2*:
|
| 403 |
-
``` cpp
|
| 404 |
-
struct S { int mi; const std::pair<int,int>& mp; };
|
| 405 |
-
S a { 1, {2,3} };
|
| 406 |
-
S* p = new S{ 1, {2,3} }; // Creates dangling reference
|
| 407 |
-
```
|
| 408 |
-
|
| 409 |
-
— *end example*]
|
| 410 |
-
\[*Note 5*: This may introduce a dangling reference, and
|
| 411 |
-
implementations are encouraged to issue a warning in such a
|
| 412 |
-
case. — *end note*]
|
| 413 |
-
|
| 414 |
-
The destruction of a temporary whose lifetime is not extended by being
|
| 415 |
-
bound to a reference is sequenced before the destruction of every
|
| 416 |
-
temporary which is constructed earlier in the same full-expression. If
|
| 417 |
-
the lifetime of two or more temporaries to which references are bound
|
| 418 |
-
ends at the same point, these temporaries are destroyed at that point in
|
| 419 |
-
the reverse order of the completion of their construction. In addition,
|
| 420 |
-
the destruction of temporaries bound to references shall take into
|
| 421 |
-
account the ordering of destruction of objects with static, thread, or
|
| 422 |
-
automatic storage duration ([[basic.stc.static]], [[basic.stc.thread]],
|
| 423 |
-
[[basic.stc.auto]]); that is, if `obj1` is an object with the same
|
| 424 |
-
storage duration as the temporary and created before the temporary is
|
| 425 |
-
created the temporary shall be destroyed before `obj1` is destroyed; if
|
| 426 |
-
`obj2` is an object with the same storage duration as the temporary and
|
| 427 |
-
created after the temporary is created the temporary shall be destroyed
|
| 428 |
-
after `obj2` is destroyed.
|
| 429 |
-
|
| 430 |
-
[*Example 3*:
|
| 431 |
-
|
| 432 |
-
``` cpp
|
| 433 |
-
struct S {
|
| 434 |
-
S();
|
| 435 |
-
S(int);
|
| 436 |
-
friend S operator+(const S&, const S&);
|
| 437 |
-
~S();
|
| 438 |
-
};
|
| 439 |
-
S obj1;
|
| 440 |
-
const S& cr = S(16)+S(23);
|
| 441 |
-
S obj2;
|
| 442 |
-
```
|
| 443 |
-
|
| 444 |
-
the expression `S(16) + S(23)` creates three temporaries: a first
|
| 445 |
-
temporary `T1` to hold the result of the expression `S(16)`, a second
|
| 446 |
-
temporary `T2` to hold the result of the expression `S(23)`, and a third
|
| 447 |
-
temporary `T3` to hold the result of the addition of these two
|
| 448 |
-
expressions. The temporary `T3` is then bound to the reference `cr`. It
|
| 449 |
-
is unspecified whether `T1` or `T2` is created first. On an
|
| 450 |
-
implementation where `T1` is created before `T2`, `T2` shall be
|
| 451 |
-
destroyed before `T1`. The temporaries `T1` and `T2` are bound to the
|
| 452 |
-
reference parameters of `operator+`; these temporaries are destroyed at
|
| 453 |
-
the end of the full-expression containing the call to `operator+`. The
|
| 454 |
-
temporary `T3` bound to the reference `cr` is destroyed at the end of
|
| 455 |
-
`cr`’s lifetime, that is, at the end of the program. In addition, the
|
| 456 |
-
order in which `T3` is destroyed takes into account the destruction
|
| 457 |
-
order of other objects with static storage duration. That is, because
|
| 458 |
-
`obj1` is constructed before `T3`, and `T3` is constructed before
|
| 459 |
-
`obj2`, `obj2` shall be destroyed before `T3`, and `T3` shall be
|
| 460 |
-
destroyed before `obj1`.
|
| 461 |
-
|
| 462 |
-
— *end example*]
|
| 463 |
-
|
| 464 |
-
## Conversions <a id="class.conv">[[class.conv]]</a>
|
| 465 |
-
|
| 466 |
-
Type conversions of class objects can be specified by constructors and
|
| 467 |
-
by conversion functions. These conversions are called *user-defined
|
| 468 |
-
conversions* and are used for implicit type conversions (Clause
|
| 469 |
-
[[conv]]), for initialization ([[dcl.init]]), and for explicit type
|
| 470 |
-
conversions ([[expr.cast]], [[expr.static.cast]]).
|
| 471 |
-
|
| 472 |
-
User-defined conversions are applied only where they are unambiguous (
|
| 473 |
-
[[class.member.lookup]], [[class.conv.fct]]). Conversions obey the
|
| 474 |
-
access control rules (Clause [[class.access]]). Access control is
|
| 475 |
-
applied after ambiguity resolution ([[basic.lookup]]).
|
| 476 |
-
|
| 477 |
-
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 478 |
-
conversions in function calls as well as examples below. — *end note*]
|
| 479 |
-
|
| 480 |
-
At most one user-defined conversion (constructor or conversion function)
|
| 481 |
-
is implicitly applied to a single value.
|
| 482 |
-
|
| 483 |
-
[*Example 1*:
|
| 484 |
-
|
| 485 |
-
``` cpp
|
| 486 |
-
struct X {
|
| 487 |
-
operator int();
|
| 488 |
-
};
|
| 489 |
-
|
| 490 |
-
struct Y {
|
| 491 |
-
operator X();
|
| 492 |
-
};
|
| 493 |
-
|
| 494 |
-
Y a;
|
| 495 |
-
int b = a; // error, a.operator X().operator int() not tried
|
| 496 |
-
int c = X(a); // OK: a.operator X().operator int()
|
| 497 |
-
```
|
| 498 |
-
|
| 499 |
-
— *end example*]
|
| 500 |
-
|
| 501 |
-
User-defined conversions are used implicitly only if they are
|
| 502 |
-
unambiguous. A conversion function in a derived class does not hide a
|
| 503 |
-
conversion function in a base class unless the two functions convert to
|
| 504 |
-
the same type. Function overload resolution ([[over.match.best]])
|
| 505 |
-
selects the best conversion function to perform the conversion.
|
| 506 |
-
|
| 507 |
-
[*Example 2*:
|
| 508 |
-
|
| 509 |
-
``` cpp
|
| 510 |
-
struct X {
|
| 511 |
-
operator int();
|
| 512 |
-
};
|
| 513 |
-
|
| 514 |
-
struct Y : X {
|
| 515 |
-
operator char();
|
| 516 |
-
};
|
| 517 |
-
|
| 518 |
-
void f(Y& a) {
|
| 519 |
-
if (a) { // ill-formed: X::operator int() or Y::operator char()
|
| 520 |
-
}
|
| 521 |
-
}
|
| 522 |
-
```
|
| 523 |
-
|
| 524 |
-
— *end example*]
|
| 525 |
-
|
| 526 |
-
### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 527 |
-
|
| 528 |
-
A constructor declared without the *function-specifier* `explicit`
|
| 529 |
-
specifies a conversion from the types of its parameters (if any) to the
|
| 530 |
-
type of its class. Such a constructor is called a *converting
|
| 531 |
-
constructor*.
|
| 532 |
-
|
| 533 |
-
[*Example 1*:
|
| 534 |
-
|
| 535 |
-
``` cpp
|
| 536 |
-
struct X {
|
| 537 |
-
X(int);
|
| 538 |
-
X(const char*, int =0);
|
| 539 |
-
X(int, int);
|
| 540 |
-
};
|
| 541 |
-
|
| 542 |
-
void f(X arg) {
|
| 543 |
-
X a = 1; // a = X(1)
|
| 544 |
-
X b = "Jessie"; // b = X("Jessie",0)
|
| 545 |
-
a = 2; // a = X(2)
|
| 546 |
-
f(3); // f(X(3))
|
| 547 |
-
f({1, 2}); // f(X(1,2))
|
| 548 |
-
}
|
| 549 |
-
```
|
| 550 |
-
|
| 551 |
-
— *end example*]
|
| 552 |
-
|
| 553 |
-
[*Note 1*:
|
| 554 |
-
|
| 555 |
-
An explicit constructor constructs objects just like non-explicit
|
| 556 |
-
constructors, but does so only where the direct-initialization syntax (
|
| 557 |
-
[[dcl.init]]) or where casts ([[expr.static.cast]], [[expr.cast]]) are
|
| 558 |
-
explicitly used; see also [[over.match.copy]]. A default constructor
|
| 559 |
-
may be an explicit constructor; such a constructor will be used to
|
| 560 |
-
perform default-initialization or value-initialization ([[dcl.init]]).
|
| 561 |
-
|
| 562 |
-
[*Example 2*:
|
| 563 |
-
|
| 564 |
-
``` cpp
|
| 565 |
-
struct Z {
|
| 566 |
-
explicit Z();
|
| 567 |
-
explicit Z(int);
|
| 568 |
-
explicit Z(int, int);
|
| 569 |
-
};
|
| 570 |
-
|
| 571 |
-
Z a; // OK: default-initialization performed
|
| 572 |
-
Z b{}; // OK: direct initialization syntax used
|
| 573 |
-
Z c = {}; // error: copy-list-initialization
|
| 574 |
-
Z a1 = 1; // error: no implicit conversion
|
| 575 |
-
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 576 |
-
Z a2(1); // OK: direct initialization syntax used
|
| 577 |
-
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 578 |
-
Z a4 = (Z)1; // OK: explicit cast used
|
| 579 |
-
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
| 580 |
-
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 581 |
-
```
|
| 582 |
-
|
| 583 |
-
— *end example*]
|
| 584 |
-
|
| 585 |
-
— *end note*]
|
| 586 |
-
|
| 587 |
-
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 588 |
-
constructor.
|
| 589 |
-
|
| 590 |
-
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 591 |
-
explicit constructor; it may be called for implicit type
|
| 592 |
-
conversions. — *end note*]
|
| 593 |
-
|
| 594 |
-
### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 595 |
-
|
| 596 |
-
A member function of a class `X` having no parameters with a name of the
|
| 597 |
-
form
|
| 598 |
-
|
| 599 |
-
``` bnf
|
| 600 |
-
conversion-function-id:
|
| 601 |
-
'operator' conversion-type-id
|
| 602 |
-
```
|
| 603 |
-
|
| 604 |
-
``` bnf
|
| 605 |
-
conversion-type-id:
|
| 606 |
-
type-specifier-seq conversion-declaratorₒₚₜ
|
| 607 |
-
```
|
| 608 |
-
|
| 609 |
-
``` bnf
|
| 610 |
-
conversion-declarator:
|
| 611 |
-
ptr-operator conversion-declaratorₒₚₜ
|
| 612 |
-
```
|
| 613 |
-
|
| 614 |
-
specifies a conversion from `X` to the type specified by the
|
| 615 |
-
*conversion-type-id*. Such functions are called *conversion functions*.
|
| 616 |
-
A *decl-specifier* in the *decl-specifier-seq* of a conversion function
|
| 617 |
-
(if any) shall be neither a *defining-type-specifier* nor `static`. The
|
| 618 |
-
type of the conversion function ([[dcl.fct]]) is “function taking no
|
| 619 |
-
parameter returning *conversion-type-id*”. A conversion function is
|
| 620 |
-
never used to convert a (possibly cv-qualified) object to the (possibly
|
| 621 |
-
cv-qualified) same object type (or a reference to it), to a (possibly
|
| 622 |
-
cv-qualified) base class of that type (or a reference to it), or to
|
| 623 |
-
(possibly cv-qualified) void.[^2]
|
| 624 |
-
|
| 625 |
-
[*Example 1*:
|
| 626 |
-
|
| 627 |
-
``` cpp
|
| 628 |
-
struct X {
|
| 629 |
-
operator int();
|
| 630 |
-
operator auto() -> short; // error: trailing return type
|
| 631 |
-
};
|
| 632 |
-
|
| 633 |
-
void f(X a) {
|
| 634 |
-
int i = int(a);
|
| 635 |
-
i = (int)a;
|
| 636 |
-
i = a;
|
| 637 |
-
}
|
| 638 |
-
```
|
| 639 |
-
|
| 640 |
-
In all three cases the value assigned will be converted by
|
| 641 |
-
`X::operator int()`.
|
| 642 |
-
|
| 643 |
-
— *end example*]
|
| 644 |
-
|
| 645 |
-
A conversion function may be explicit ([[dcl.fct.spec]]), in which case
|
| 646 |
-
it is only considered as a user-defined conversion for
|
| 647 |
-
direct-initialization ([[dcl.init]]). Otherwise, user-defined
|
| 648 |
-
conversions are not restricted to use in assignments and
|
| 649 |
-
initializations.
|
| 650 |
-
|
| 651 |
-
[*Example 2*:
|
| 652 |
-
|
| 653 |
-
``` cpp
|
| 654 |
-
class Y { };
|
| 655 |
-
struct Z {
|
| 656 |
-
explicit operator Y() const;
|
| 657 |
-
};
|
| 658 |
-
|
| 659 |
-
void h(Z z) {
|
| 660 |
-
Y y1(z); // OK: direct-initialization
|
| 661 |
-
Y y2 = z; // ill-formed: copy-initialization
|
| 662 |
-
Y y3 = (Y)z; // OK: cast notation
|
| 663 |
-
}
|
| 664 |
-
|
| 665 |
-
void g(X a, X b) {
|
| 666 |
-
int i = (a) ? 1+a : 0;
|
| 667 |
-
int j = (a&&b) ? a+b : i;
|
| 668 |
-
if (a) {
|
| 669 |
-
}
|
| 670 |
-
}
|
| 671 |
-
```
|
| 672 |
-
|
| 673 |
-
— *end example*]
|
| 674 |
-
|
| 675 |
-
The *conversion-type-id* shall not represent a function type nor an
|
| 676 |
-
array type. The *conversion-type-id* in a *conversion-function-id* is
|
| 677 |
-
the longest sequence of tokens that could possibly form a
|
| 678 |
-
*conversion-type-id*.
|
| 679 |
-
|
| 680 |
-
[*Note 1*:
|
| 681 |
-
|
| 682 |
-
This prevents ambiguities between the declarator operator `*` and its
|
| 683 |
-
expression counterparts.
|
| 684 |
-
|
| 685 |
-
[*Example 3*:
|
| 686 |
-
|
| 687 |
-
``` cpp
|
| 688 |
-
&ac.operator int*i; // syntax error:
|
| 689 |
-
// parsed as: &(ac.operator int *)i
|
| 690 |
-
// not as: &(ac.operator int)*i
|
| 691 |
-
```
|
| 692 |
-
|
| 693 |
-
The `*` is the pointer declarator and not the multiplication operator.
|
| 694 |
-
|
| 695 |
-
— *end example*]
|
| 696 |
-
|
| 697 |
-
This rule also prevents ambiguities for attributes.
|
| 698 |
-
|
| 699 |
-
[*Example 4*:
|
| 700 |
-
|
| 701 |
-
``` cpp
|
| 702 |
-
operator int [[noreturn]] (); // error: noreturn attribute applied to a type
|
| 703 |
-
```
|
| 704 |
-
|
| 705 |
-
— *end example*]
|
| 706 |
-
|
| 707 |
-
— *end note*]
|
| 708 |
-
|
| 709 |
-
Conversion functions are inherited.
|
| 710 |
-
|
| 711 |
-
Conversion functions can be virtual.
|
| 712 |
-
|
| 713 |
-
A conversion function template shall not have a deduced return type (
|
| 714 |
-
[[dcl.spec.auto]]).
|
| 715 |
-
|
| 716 |
-
[*Example 5*:
|
| 717 |
-
|
| 718 |
-
``` cpp
|
| 719 |
-
struct S {
|
| 720 |
-
operator auto() const { return 10; } // OK
|
| 721 |
-
template<class T>
|
| 722 |
-
operator auto() const { return 1.2; } // error: conversion function template
|
| 723 |
-
};
|
| 724 |
-
```
|
| 725 |
-
|
| 726 |
-
— *end example*]
|
| 727 |
-
|
| 728 |
-
## Destructors <a id="class.dtor">[[class.dtor]]</a>
|
| 729 |
-
|
| 730 |
-
In a declaration of a destructor, the *declarator* is a function
|
| 731 |
-
declarator ([[dcl.fct]]) of the form
|
| 732 |
-
|
| 733 |
-
``` bnf
|
| 734 |
-
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 735 |
-
```
|
| 736 |
-
|
| 737 |
-
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 738 |
-
optional *attribute-specifier-seq*, and optional surrounding
|
| 739 |
-
parentheses, and the *id-expression* has one of the following forms:
|
| 740 |
-
|
| 741 |
-
- in a *member-declaration* that belongs to the *member-specification*
|
| 742 |
-
of a class but is not a friend declaration ([[class.friend]]), the
|
| 743 |
-
*id-expression* is `~`*class-name* and the *class-name* is the
|
| 744 |
-
injected-class-name (Clause [[class]]) of the immediately-enclosing
|
| 745 |
-
class;
|
| 746 |
-
- in a *member-declaration* that belongs to the *member-specification*
|
| 747 |
-
of a class template but is not a friend declaration, the
|
| 748 |
-
*id-expression* is `~`*class-name* and the *class-name* names the
|
| 749 |
-
current instantiation ([[temp.dep.type]]) of the
|
| 750 |
-
immediately-enclosing class template; or
|
| 751 |
-
- in a declaration at namespace scope or in a friend declaration, the
|
| 752 |
-
*id-expression* is *nested-name-specifier* `~`*class-name* and the
|
| 753 |
-
*class-name* names the same class as the *nested-name-specifier*.
|
| 754 |
-
|
| 755 |
-
The *class-name* shall not be a *typedef-name*. A destructor shall take
|
| 756 |
-
no arguments ([[dcl.fct]]). Each *decl-specifier* of the
|
| 757 |
-
*decl-specifier-seq* of a destructor declaration (if any) shall be
|
| 758 |
-
`friend`, `inline`, or `virtual`.
|
| 759 |
-
|
| 760 |
-
A destructor is used to destroy objects of its class type. The address
|
| 761 |
-
of a destructor shall not be taken. A destructor can be invoked for a
|
| 762 |
-
`const`, `volatile` or `const` `volatile` object. `const` and `volatile`
|
| 763 |
-
semantics ([[dcl.type.cv]]) are not applied on an object under
|
| 764 |
-
destruction. They stop being in effect when the destructor for the most
|
| 765 |
-
derived object ([[intro.object]]) starts.
|
| 766 |
-
|
| 767 |
-
[*Note 1*: A declaration of a destructor that does not have a
|
| 768 |
-
*noexcept-specifier* has the same exception specification as if had been
|
| 769 |
-
implicitly declared ([[except.spec]]). — *end note*]
|
| 770 |
-
|
| 771 |
-
If a class has no user-declared destructor, a destructor is implicitly
|
| 772 |
-
declared as defaulted ([[dcl.fct.def]]). An implicitly-declared
|
| 773 |
-
destructor is an `inline` `public` member of its class.
|
| 774 |
-
|
| 775 |
-
A defaulted destructor for a class `X` is defined as deleted if:
|
| 776 |
-
|
| 777 |
-
- `X` is a union-like class that has a variant member with a non-trivial
|
| 778 |
-
destructor,
|
| 779 |
-
- any potentially constructed subobject has class type `M` (or array
|
| 780 |
-
thereof) and `M` has a deleted destructor or a destructor that is
|
| 781 |
-
inaccessible from the defaulted destructor,
|
| 782 |
-
- or, for a virtual destructor, lookup of the non-array deallocation
|
| 783 |
-
function results in an ambiguity or in a function that is deleted or
|
| 784 |
-
inaccessible from the defaulted destructor.
|
| 785 |
-
|
| 786 |
-
A destructor is trivial if it is not user-provided and if:
|
| 787 |
-
|
| 788 |
-
- the destructor is not `virtual`,
|
| 789 |
-
- all of the direct base classes of its class have trivial destructors,
|
| 790 |
-
and
|
| 791 |
-
- for all of the non-static data members of its class that are of class
|
| 792 |
-
type (or array thereof), each such class has a trivial destructor.
|
| 793 |
-
|
| 794 |
-
Otherwise, the destructor is *non-trivial*.
|
| 795 |
-
|
| 796 |
-
A destructor that is defaulted and not defined as deleted is *implicitly
|
| 797 |
-
defined* when it is odr-used ([[basic.def.odr]]) or when it is
|
| 798 |
-
explicitly defaulted after its first declaration.
|
| 799 |
-
|
| 800 |
-
Before the defaulted destructor for a class is implicitly defined, all
|
| 801 |
-
the non-user-provided destructors for its base classes and its
|
| 802 |
-
non-static data members shall have been implicitly defined.
|
| 803 |
-
|
| 804 |
-
After executing the body of the destructor and destroying any automatic
|
| 805 |
-
objects allocated within the body, a destructor for class `X` calls the
|
| 806 |
-
destructors for `X`’s direct non-variant non-static data members, the
|
| 807 |
-
destructors for `X`’s non-virtual direct base classes and, if `X` is the
|
| 808 |
-
type of the most derived class ([[class.base.init]]), its destructor
|
| 809 |
-
calls the destructors for `X`’s virtual base classes. All destructors
|
| 810 |
-
are called as if they were referenced with a qualified name, that is,
|
| 811 |
-
ignoring any possible virtual overriding destructors in more derived
|
| 812 |
-
classes. Bases and members are destroyed in the reverse order of the
|
| 813 |
-
completion of their constructor (see [[class.base.init]]). A `return`
|
| 814 |
-
statement ([[stmt.return]]) in a destructor might not directly return
|
| 815 |
-
to the caller; before transferring control to the caller, the
|
| 816 |
-
destructors for the members and bases are called. Destructors for
|
| 817 |
-
elements of an array are called in reverse order of their construction
|
| 818 |
-
(see [[class.init]]).
|
| 819 |
-
|
| 820 |
-
A destructor can be declared `virtual` ([[class.virtual]]) or pure
|
| 821 |
-
`virtual` ([[class.abstract]]); if any objects of that class or any
|
| 822 |
-
derived class are created in the program, the destructor shall be
|
| 823 |
-
defined. If a class has a base class with a virtual destructor, its
|
| 824 |
-
destructor (whether user- or implicitly-declared) is virtual.
|
| 825 |
-
|
| 826 |
-
[*Note 2*: Some language constructs have special semantics when used
|
| 827 |
-
during destruction; see [[class.cdtor]]. — *end note*]
|
| 828 |
-
|
| 829 |
-
A destructor is invoked implicitly
|
| 830 |
-
|
| 831 |
-
- for a constructed object with static storage duration (
|
| 832 |
-
[[basic.stc.static]]) at program termination ([[basic.start.term]]),
|
| 833 |
-
- for a constructed object with thread storage duration (
|
| 834 |
-
[[basic.stc.thread]]) at thread exit,
|
| 835 |
-
- for a constructed object with automatic storage duration (
|
| 836 |
-
[[basic.stc.auto]]) when the block in which an object is created
|
| 837 |
-
exits ([[stmt.dcl]]),
|
| 838 |
-
- for a constructed temporary object when its lifetime ends (
|
| 839 |
-
[[conv.rval]], [[class.temporary]]).
|
| 840 |
-
|
| 841 |
-
In each case, the context of the invocation is the context of the
|
| 842 |
-
construction of the object. A destructor is also invoked implicitly
|
| 843 |
-
through use of a *delete-expression* ([[expr.delete]]) for a
|
| 844 |
-
constructed object allocated by a *new-expression* ([[expr.new]]); the
|
| 845 |
-
context of the invocation is the *delete-expression*.
|
| 846 |
-
|
| 847 |
-
[*Note 3*: An array of class type contains several subobjects for each
|
| 848 |
-
of which the destructor is invoked. — *end note*]
|
| 849 |
-
|
| 850 |
-
A destructor can also be invoked explicitly. A destructor is
|
| 851 |
-
*potentially invoked* if it is invoked or as specified in [[expr.new]],
|
| 852 |
-
[[class.base.init]], and [[except.throw]]. A program is ill-formed if a
|
| 853 |
-
destructor that is potentially invoked is deleted or not accessible from
|
| 854 |
-
the context of the invocation.
|
| 855 |
-
|
| 856 |
-
At the point of definition of a virtual destructor (including an
|
| 857 |
-
implicit definition ([[class.copy]])), the non-array deallocation
|
| 858 |
-
function is determined as if for the expression `delete this` appearing
|
| 859 |
-
in a non-virtual destructor of the destructor’s class (see
|
| 860 |
-
[[expr.delete]]). If the lookup fails or if the deallocation function
|
| 861 |
-
has a deleted definition ([[dcl.fct.def]]), the program is ill-formed.
|
| 862 |
-
|
| 863 |
-
[*Note 4*: This assures that a deallocation function corresponding to
|
| 864 |
-
the dynamic type of an object is available for the *delete-expression* (
|
| 865 |
-
[[class.free]]). — *end note*]
|
| 866 |
-
|
| 867 |
-
In an explicit destructor call, the destructor is specified by a `~`
|
| 868 |
-
followed by a *type-name* or *decltype-specifier* that denotes the
|
| 869 |
-
destructor’s class type. The invocation of a destructor is subject to
|
| 870 |
-
the usual rules for member functions ([[class.mfct]]); that is, if the
|
| 871 |
-
object is not of the destructor’s class type and not of a class derived
|
| 872 |
-
from the destructor’s class type (including when the destructor is
|
| 873 |
-
invoked via a null pointer value), the program has undefined behavior.
|
| 874 |
-
|
| 875 |
-
[*Note 5*: Invoking `delete` on a null pointer does not call the
|
| 876 |
-
destructor; see [[expr.delete]]. — *end note*]
|
| 877 |
-
|
| 878 |
-
[*Example 1*:
|
| 879 |
-
|
| 880 |
-
``` cpp
|
| 881 |
-
struct B {
|
| 882 |
-
virtual ~B() { }
|
| 883 |
-
};
|
| 884 |
-
struct D : B {
|
| 885 |
-
~D() { }
|
| 886 |
-
};
|
| 887 |
-
|
| 888 |
-
D D_object;
|
| 889 |
-
typedef B B_alias;
|
| 890 |
-
B* B_ptr = &D_object;
|
| 891 |
-
|
| 892 |
-
void f() {
|
| 893 |
-
D_object.B::~B(); // calls B's destructor
|
| 894 |
-
B_ptr->~B(); // calls D's destructor
|
| 895 |
-
B_ptr->~B_alias(); // calls D's destructor
|
| 896 |
-
B_ptr->B_alias::~B(); // calls B's destructor
|
| 897 |
-
B_ptr->B_alias::~B_alias(); // calls B's destructor
|
| 898 |
-
}
|
| 899 |
-
```
|
| 900 |
-
|
| 901 |
-
— *end example*]
|
| 902 |
-
|
| 903 |
-
[*Note 6*: An explicit destructor call must always be written using a
|
| 904 |
-
member access operator ([[expr.ref]]) or a *qualified-id* (
|
| 905 |
-
[[expr.prim]]); in particular, the *unary-expression* `~X()` in a member
|
| 906 |
-
function is not an explicit destructor call (
|
| 907 |
-
[[expr.unary.op]]). — *end note*]
|
| 908 |
-
|
| 909 |
-
[*Note 7*:
|
| 910 |
-
|
| 911 |
-
Explicit calls of destructors are rarely needed. One use of such calls
|
| 912 |
-
is for objects placed at specific addresses using a placement
|
| 913 |
-
*new-expression*. Such use of explicit placement and destruction of
|
| 914 |
-
objects can be necessary to cope with dedicated hardware resources and
|
| 915 |
-
for writing memory management facilities. For example,
|
| 916 |
-
|
| 917 |
-
``` cpp
|
| 918 |
-
void* operator new(std::size_t, void* p) { return p; }
|
| 919 |
-
struct X {
|
| 920 |
-
X(int);
|
| 921 |
-
~X();
|
| 922 |
-
};
|
| 923 |
-
void f(X* p);
|
| 924 |
-
|
| 925 |
-
void g() { // rare, specialized use:
|
| 926 |
-
char* buf = new char[sizeof(X)];
|
| 927 |
-
X* p = new(buf) X(222); // use buf[] and initialize
|
| 928 |
-
f(p);
|
| 929 |
-
p->X::~X(); // cleanup
|
| 930 |
-
}
|
| 931 |
-
```
|
| 932 |
-
|
| 933 |
-
— *end note*]
|
| 934 |
-
|
| 935 |
-
Once a destructor is invoked for an object, the object no longer exists;
|
| 936 |
-
the behavior is undefined if the destructor is invoked for an object
|
| 937 |
-
whose lifetime has ended ([[basic.life]]).
|
| 938 |
-
|
| 939 |
-
[*Example 2*: If the destructor for an automatic object is explicitly
|
| 940 |
-
invoked, and the block is subsequently left in a manner that would
|
| 941 |
-
ordinarily invoke implicit destruction of the object, the behavior is
|
| 942 |
-
undefined. — *end example*]
|
| 943 |
-
|
| 944 |
-
[*Note 8*:
|
| 945 |
-
|
| 946 |
-
The notation for explicit call of a destructor can be used for any
|
| 947 |
-
scalar type name ([[expr.pseudo]]). Allowing this makes it possible to
|
| 948 |
-
write code without having to know if a destructor exists for a given
|
| 949 |
-
type. For example:
|
| 950 |
-
|
| 951 |
-
``` cpp
|
| 952 |
-
typedef int I;
|
| 953 |
-
I* p;
|
| 954 |
-
p->I::~I();
|
| 955 |
-
```
|
| 956 |
-
|
| 957 |
-
— *end note*]
|
| 958 |
-
|
| 959 |
-
## Free store <a id="class.free">[[class.free]]</a>
|
| 960 |
-
|
| 961 |
-
Any allocation function for a class `T` is a static member (even if not
|
| 962 |
-
explicitly declared `static`).
|
| 963 |
-
|
| 964 |
-
[*Example 1*:
|
| 965 |
-
|
| 966 |
-
``` cpp
|
| 967 |
-
class Arena;
|
| 968 |
-
struct B {
|
| 969 |
-
void* operator new(std::size_t, Arena*);
|
| 970 |
-
};
|
| 971 |
-
struct D1 : B {
|
| 972 |
-
};
|
| 973 |
-
|
| 974 |
-
Arena* ap;
|
| 975 |
-
void foo(int i) {
|
| 976 |
-
new (ap) D1; // calls B::operator new(std::size_t, Arena*)
|
| 977 |
-
new D1[i]; // calls ::operator new[](std::size_t)
|
| 978 |
-
new D1; // ill-formed: ::operator new(std::size_t) hidden
|
| 979 |
-
}
|
| 980 |
-
```
|
| 981 |
-
|
| 982 |
-
— *end example*]
|
| 983 |
-
|
| 984 |
-
When an object is deleted with a *delete-expression* ([[expr.delete]]),
|
| 985 |
-
a deallocation function (`operator delete()` for non-array objects or
|
| 986 |
-
`operator delete[]()` for arrays) is (implicitly) called to reclaim the
|
| 987 |
-
storage occupied by the object ([[basic.stc.dynamic.deallocation]]).
|
| 988 |
-
|
| 989 |
-
Class-specific deallocation function lookup is a part of general
|
| 990 |
-
deallocation function lookup ([[expr.delete]]) and occurs as follows.
|
| 991 |
-
If the *delete-expression* is used to deallocate a class object whose
|
| 992 |
-
static type has a virtual destructor, the deallocation function is the
|
| 993 |
-
one selected at the point of definition of the dynamic type’s virtual
|
| 994 |
-
destructor ([[class.dtor]]).[^3] Otherwise, if the *delete-expression*
|
| 995 |
-
is used to deallocate an object of class `T` or array thereof, the
|
| 996 |
-
static and dynamic types of the object shall be identical and the
|
| 997 |
-
deallocation function’s name is looked up in the scope of `T`. If this
|
| 998 |
-
lookup fails to find the name, general deallocation function lookup (
|
| 999 |
-
[[expr.delete]]) continues. If the result of the lookup is ambiguous or
|
| 1000 |
-
inaccessible, or if the lookup selects a placement deallocation
|
| 1001 |
-
function, the program is ill-formed.
|
| 1002 |
-
|
| 1003 |
-
Any deallocation function for a class `X` is a static member (even if
|
| 1004 |
-
not explicitly declared `static`).
|
| 1005 |
-
|
| 1006 |
-
[*Example 2*:
|
| 1007 |
-
|
| 1008 |
-
``` cpp
|
| 1009 |
-
class X {
|
| 1010 |
-
void operator delete(void*);
|
| 1011 |
-
void operator delete[](void*, std::size_t);
|
| 1012 |
-
};
|
| 1013 |
-
|
| 1014 |
-
class Y {
|
| 1015 |
-
void operator delete(void*, std::size_t);
|
| 1016 |
-
void operator delete[](void*);
|
| 1017 |
-
};
|
| 1018 |
-
```
|
| 1019 |
-
|
| 1020 |
-
— *end example*]
|
| 1021 |
-
|
| 1022 |
-
Since member allocation and deallocation functions are `static` they
|
| 1023 |
-
cannot be virtual.
|
| 1024 |
-
|
| 1025 |
-
[*Note 1*:
|
| 1026 |
-
|
| 1027 |
-
However, when the *cast-expression* of a *delete-expression* refers to
|
| 1028 |
-
an object of class type, because the deallocation function actually
|
| 1029 |
-
called is looked up in the scope of the class that is the dynamic type
|
| 1030 |
-
of the object, if the destructor is virtual, the effect is the same. For
|
| 1031 |
-
example,
|
| 1032 |
-
|
| 1033 |
-
``` cpp
|
| 1034 |
-
struct B {
|
| 1035 |
-
virtual ~B();
|
| 1036 |
-
void operator delete(void*, std::size_t);
|
| 1037 |
-
};
|
| 1038 |
-
|
| 1039 |
-
struct D : B {
|
| 1040 |
-
void operator delete(void*);
|
| 1041 |
-
};
|
| 1042 |
-
|
| 1043 |
-
void f() {
|
| 1044 |
-
B* bp = new D;
|
| 1045 |
-
delete bp; // 1: uses D::operator delete(void*)
|
| 1046 |
-
}
|
| 1047 |
-
```
|
| 1048 |
-
|
| 1049 |
-
Here, storage for the non-array object of class `D` is deallocated by
|
| 1050 |
-
`D::operator delete()`, due to the virtual destructor.
|
| 1051 |
-
|
| 1052 |
-
— *end note*]
|
| 1053 |
-
|
| 1054 |
-
[*Note 2*:
|
| 1055 |
-
|
| 1056 |
-
Virtual destructors have no effect on the deallocation function actually
|
| 1057 |
-
called when the *cast-expression* of a *delete-expression* refers to an
|
| 1058 |
-
array of objects of class type. For example,
|
| 1059 |
-
|
| 1060 |
-
``` cpp
|
| 1061 |
-
struct B {
|
| 1062 |
-
virtual ~B();
|
| 1063 |
-
void operator delete[](void*, std::size_t);
|
| 1064 |
-
};
|
| 1065 |
-
|
| 1066 |
-
struct D : B {
|
| 1067 |
-
void operator delete[](void*, std::size_t);
|
| 1068 |
-
};
|
| 1069 |
-
|
| 1070 |
-
void f(int i) {
|
| 1071 |
-
D* dp = new D[i];
|
| 1072 |
-
delete [] dp; // uses D::operator delete[](void*, std::size_t)
|
| 1073 |
-
B* bp = new D[i];
|
| 1074 |
-
delete[] bp; // undefined behavior
|
| 1075 |
-
}
|
| 1076 |
-
```
|
| 1077 |
-
|
| 1078 |
-
— *end note*]
|
| 1079 |
-
|
| 1080 |
-
Access to the deallocation function is checked statically. Hence, even
|
| 1081 |
-
though a different one might actually be executed, the statically
|
| 1082 |
-
visible deallocation function is required to be accessible.
|
| 1083 |
-
|
| 1084 |
-
[*Example 3*: For the call on line “// 1” above, if
|
| 1085 |
-
`B::operator delete()` had been `private`, the delete expression would
|
| 1086 |
-
have been ill-formed. — *end example*]
|
| 1087 |
-
|
| 1088 |
-
[*Note 3*: If a deallocation function has no explicit
|
| 1089 |
-
*noexcept-specifier*, it has a non-throwing exception specification (
|
| 1090 |
-
[[except.spec]]). — *end note*]
|
| 1091 |
-
|
| 1092 |
-
## Initialization <a id="class.init">[[class.init]]</a>
|
| 1093 |
-
|
| 1094 |
-
When no initializer is specified for an object of (possibly
|
| 1095 |
-
cv-qualified) class type (or array thereof), or the initializer has the
|
| 1096 |
-
form `()`, the object is initialized as specified in [[dcl.init]].
|
| 1097 |
-
|
| 1098 |
-
An object of class type (or array thereof) can be explicitly
|
| 1099 |
-
initialized; see [[class.expl.init]] and [[class.base.init]].
|
| 1100 |
-
|
| 1101 |
-
When an array of class objects is initialized (either explicitly or
|
| 1102 |
-
implicitly) and the elements are initialized by constructor, the
|
| 1103 |
-
constructor shall be called for each element of the array, following the
|
| 1104 |
-
subscript order; see [[dcl.array]].
|
| 1105 |
-
|
| 1106 |
-
[*Note 1*: Destructors for the array elements are called in reverse
|
| 1107 |
-
order of their construction. — *end note*]
|
| 1108 |
-
|
| 1109 |
-
### Explicit initialization <a id="class.expl.init">[[class.expl.init]]</a>
|
| 1110 |
-
|
| 1111 |
-
An object of class type can be initialized with a parenthesized
|
| 1112 |
-
*expression-list*, where the *expression-list* is construed as an
|
| 1113 |
-
argument list for a constructor that is called to initialize the object.
|
| 1114 |
-
Alternatively, a single *assignment-expression* can be specified as an
|
| 1115 |
-
*initializer* using the `=` form of initialization. Either
|
| 1116 |
-
direct-initialization semantics or copy-initialization semantics apply;
|
| 1117 |
-
see [[dcl.init]].
|
| 1118 |
-
|
| 1119 |
-
[*Example 1*:
|
| 1120 |
-
|
| 1121 |
-
``` cpp
|
| 1122 |
-
struct complex {
|
| 1123 |
-
complex();
|
| 1124 |
-
complex(double);
|
| 1125 |
-
complex(double,double);
|
| 1126 |
-
};
|
| 1127 |
-
|
| 1128 |
-
complex sqrt(complex,complex);
|
| 1129 |
-
|
| 1130 |
-
complex a(1); // initialize by a call of complex(double)
|
| 1131 |
-
complex b = a; // initialize by a copy of a
|
| 1132 |
-
complex c = complex(1,2); // construct complex(1,2) using complex(double,double),
|
| 1133 |
-
// copy/move it into c
|
| 1134 |
-
complex d = sqrt(b,c); // call sqrt(complex,complex) and copy/move the result into d
|
| 1135 |
-
complex e; // initialize by a call of complex()
|
| 1136 |
-
complex f = 3; // construct complex(3) using complex(double), copy/move it into f
|
| 1137 |
-
complex g = { 1, 2 }; // initialize by a call of complex(double, double)
|
| 1138 |
-
```
|
| 1139 |
-
|
| 1140 |
-
— *end example*]
|
| 1141 |
-
|
| 1142 |
-
[*Note 1*: Overloading of the assignment operator ([[over.ass]]) has
|
| 1143 |
-
no effect on initialization. — *end note*]
|
| 1144 |
-
|
| 1145 |
-
An object of class type can also be initialized by a *braced-init-list*.
|
| 1146 |
-
List-initialization semantics apply; see [[dcl.init]] and
|
| 1147 |
-
[[dcl.init.list]].
|
| 1148 |
-
|
| 1149 |
-
[*Example 2*:
|
| 1150 |
-
|
| 1151 |
-
``` cpp
|
| 1152 |
-
complex v[6] = { 1, complex(1,2), complex(), 2 };
|
| 1153 |
-
```
|
| 1154 |
-
|
| 1155 |
-
Here, `complex::complex(double)` is called for the initialization of
|
| 1156 |
-
`v[0]` and `v[3]`, `complex::complex({}double, double)` is called for
|
| 1157 |
-
the initialization of `v[1]`, `complex::complex()` is called for the
|
| 1158 |
-
initialization `v[2]`, `v[4]`, and `v[5]`. For another example,
|
| 1159 |
-
|
| 1160 |
-
``` cpp
|
| 1161 |
-
struct X {
|
| 1162 |
-
int i;
|
| 1163 |
-
float f;
|
| 1164 |
-
complex c;
|
| 1165 |
-
} x = { 99, 88.8, 77.7 };
|
| 1166 |
-
```
|
| 1167 |
-
|
| 1168 |
-
Here, `x.i` is initialized with 99, `x.f` is initialized with 88.8, and
|
| 1169 |
-
`complex::complex(double)` is called for the initialization of `x.c`.
|
| 1170 |
-
|
| 1171 |
-
— *end example*]
|
| 1172 |
-
|
| 1173 |
-
[*Note 2*: Braces can be elided in the *initializer-list* for any
|
| 1174 |
-
aggregate, even if the aggregate has members of a class type with
|
| 1175 |
-
user-defined type conversions; see [[dcl.init.aggr]]. — *end note*]
|
| 1176 |
-
|
| 1177 |
-
[*Note 3*: If `T` is a class type with no default constructor, any
|
| 1178 |
-
declaration of an object of type `T` (or array thereof) is ill-formed if
|
| 1179 |
-
no *initializer* is explicitly specified (see [[class.init]] and
|
| 1180 |
-
[[dcl.init]]). — *end note*]
|
| 1181 |
-
|
| 1182 |
-
[*Note 4*: The order in which objects with static or thread storage
|
| 1183 |
-
duration are initialized is described in [[basic.start.dynamic]] and
|
| 1184 |
-
[[stmt.dcl]]. — *end note*]
|
| 1185 |
-
|
| 1186 |
-
### Initializing bases and members <a id="class.base.init">[[class.base.init]]</a>
|
| 1187 |
-
|
| 1188 |
-
In the definition of a constructor for a class, initializers for direct
|
| 1189 |
-
and virtual base class subobjects and non-static data members can be
|
| 1190 |
-
specified by a *ctor-initializer*, which has the form
|
| 1191 |
-
|
| 1192 |
-
``` bnf
|
| 1193 |
-
ctor-initializer:
|
| 1194 |
-
':' mem-initializer-list
|
| 1195 |
-
```
|
| 1196 |
-
|
| 1197 |
-
``` bnf
|
| 1198 |
-
mem-initializer-list:
|
| 1199 |
-
mem-initializer '...'ₒₚₜ
|
| 1200 |
-
mem-initializer-list ',' mem-initializer '...'ₒₚₜ
|
| 1201 |
-
```
|
| 1202 |
-
|
| 1203 |
-
``` bnf
|
| 1204 |
-
mem-initializer:
|
| 1205 |
-
mem-initializer-id '(' expression-listₒₚₜ ')'
|
| 1206 |
-
mem-initializer-id braced-init-list
|
| 1207 |
-
```
|
| 1208 |
-
|
| 1209 |
-
``` bnf
|
| 1210 |
-
mem-initializer-id:
|
| 1211 |
-
class-or-decltype
|
| 1212 |
-
identifier
|
| 1213 |
-
```
|
| 1214 |
-
|
| 1215 |
-
In a *mem-initializer-id* an initial unqualified *identifier* is looked
|
| 1216 |
-
up in the scope of the constructor’s class and, if not found in that
|
| 1217 |
-
scope, it is looked up in the scope containing the constructor’s
|
| 1218 |
-
definition.
|
| 1219 |
-
|
| 1220 |
-
[*Note 1*: If the constructor’s class contains a member with the same
|
| 1221 |
-
name as a direct or virtual base class of the class, a
|
| 1222 |
-
*mem-initializer-id* naming the member or base class and composed of a
|
| 1223 |
-
single identifier refers to the class member. A *mem-initializer-id* for
|
| 1224 |
-
the hidden base class may be specified using a qualified
|
| 1225 |
-
name. — *end note*]
|
| 1226 |
-
|
| 1227 |
-
Unless the *mem-initializer-id* names the constructor’s class, a
|
| 1228 |
-
non-static data member of the constructor’s class, or a direct or
|
| 1229 |
-
virtual base of that class, the *mem-initializer* is ill-formed.
|
| 1230 |
-
|
| 1231 |
-
A *mem-initializer-list* can initialize a base class using any
|
| 1232 |
-
*class-or-decltype* that denotes that base class type.
|
| 1233 |
-
|
| 1234 |
-
[*Example 1*:
|
| 1235 |
-
|
| 1236 |
-
``` cpp
|
| 1237 |
-
struct A { A(); };
|
| 1238 |
-
typedef A global_A;
|
| 1239 |
-
struct B { };
|
| 1240 |
-
struct C: public A, public B { C(); };
|
| 1241 |
-
C::C(): global_A() { } // mem-initializer for base A
|
| 1242 |
-
```
|
| 1243 |
-
|
| 1244 |
-
— *end example*]
|
| 1245 |
-
|
| 1246 |
-
If a *mem-initializer-id* is ambiguous because it designates both a
|
| 1247 |
-
direct non-virtual base class and an inherited virtual base class, the
|
| 1248 |
-
*mem-initializer* is ill-formed.
|
| 1249 |
-
|
| 1250 |
-
[*Example 2*:
|
| 1251 |
-
|
| 1252 |
-
``` cpp
|
| 1253 |
-
struct A { A(); };
|
| 1254 |
-
struct B: public virtual A { };
|
| 1255 |
-
struct C: public A, public B { C(); };
|
| 1256 |
-
C::C(): A() { } // ill-formed: which A?
|
| 1257 |
-
```
|
| 1258 |
-
|
| 1259 |
-
— *end example*]
|
| 1260 |
-
|
| 1261 |
-
A *ctor-initializer* may initialize a variant member of the
|
| 1262 |
-
constructor’s class. If a *ctor-initializer* specifies more than one
|
| 1263 |
-
*mem-initializer* for the same member or for the same base class, the
|
| 1264 |
-
*ctor-initializer* is ill-formed.
|
| 1265 |
-
|
| 1266 |
-
A *mem-initializer-list* can delegate to another constructor of the
|
| 1267 |
-
constructor’s class using any *class-or-decltype* that denotes the
|
| 1268 |
-
constructor’s class itself. If a *mem-initializer-id* designates the
|
| 1269 |
-
constructor’s class, it shall be the only *mem-initializer*; the
|
| 1270 |
-
constructor is a *delegating constructor*, and the constructor selected
|
| 1271 |
-
by the *mem-initializer* is the *target constructor*. The target
|
| 1272 |
-
constructor is selected by overload resolution. Once the target
|
| 1273 |
-
constructor returns, the body of the delegating constructor is executed.
|
| 1274 |
-
If a constructor delegates to itself directly or indirectly, the program
|
| 1275 |
-
is ill-formed, no diagnostic required.
|
| 1276 |
-
|
| 1277 |
-
[*Example 3*:
|
| 1278 |
-
|
| 1279 |
-
``` cpp
|
| 1280 |
-
struct C {
|
| 1281 |
-
C( int ) { } // #1: non-delegating constructor
|
| 1282 |
-
C(): C(42) { } // #2: delegates to #1
|
| 1283 |
-
C( char c ) : C(42.0) { } // #3: ill-formed due to recursion with #4
|
| 1284 |
-
C( double d ) : C('a') { } // #4: ill-formed due to recursion with #3
|
| 1285 |
-
};
|
| 1286 |
-
```
|
| 1287 |
-
|
| 1288 |
-
— *end example*]
|
| 1289 |
-
|
| 1290 |
-
The *expression-list* or *braced-init-list* in a *mem-initializer* is
|
| 1291 |
-
used to initialize the designated subobject (or, in the case of a
|
| 1292 |
-
delegating constructor, the complete class object) according to the
|
| 1293 |
-
initialization rules of [[dcl.init]] for direct-initialization.
|
| 1294 |
-
|
| 1295 |
-
[*Example 4*:
|
| 1296 |
-
|
| 1297 |
-
``` cpp
|
| 1298 |
-
struct B1 { B1(int); ... };
|
| 1299 |
-
struct B2 { B2(int); ... };
|
| 1300 |
-
struct D : B1, B2 {
|
| 1301 |
-
D(int);
|
| 1302 |
-
B1 b;
|
| 1303 |
-
const int c;
|
| 1304 |
-
};
|
| 1305 |
-
|
| 1306 |
-
D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4) { ... }
|
| 1307 |
-
D d(10);
|
| 1308 |
-
```
|
| 1309 |
-
|
| 1310 |
-
— *end example*]
|
| 1311 |
-
|
| 1312 |
-
[*Note 2*: The initialization performed by each *mem-initializer*
|
| 1313 |
-
constitutes a full-expression ([[intro.execution]]). Any expression in
|
| 1314 |
-
a *mem-initializer* is evaluated as part of the full-expression that
|
| 1315 |
-
performs the initialization. — *end note*]
|
| 1316 |
-
|
| 1317 |
-
A *mem-initializer* where the *mem-initializer-id* denotes a virtual
|
| 1318 |
-
base class is ignored during execution of a constructor of any class
|
| 1319 |
-
that is not the most derived class.
|
| 1320 |
-
|
| 1321 |
-
A temporary expression bound to a reference member in a
|
| 1322 |
-
*mem-initializer* is ill-formed.
|
| 1323 |
-
|
| 1324 |
-
[*Example 5*:
|
| 1325 |
-
|
| 1326 |
-
``` cpp
|
| 1327 |
-
struct A {
|
| 1328 |
-
A() : v(42) { } // error
|
| 1329 |
-
const int& v;
|
| 1330 |
-
};
|
| 1331 |
-
```
|
| 1332 |
-
|
| 1333 |
-
— *end example*]
|
| 1334 |
-
|
| 1335 |
-
In a non-delegating constructor, if a given potentially constructed
|
| 1336 |
-
subobject is not designated by a *mem-initializer-id* (including the
|
| 1337 |
-
case where there is no *mem-initializer-list* because the constructor
|
| 1338 |
-
has no *ctor-initializer*), then
|
| 1339 |
-
|
| 1340 |
-
- if the entity is a non-static data member that has a default member
|
| 1341 |
-
initializer ([[class.mem]]) and either
|
| 1342 |
-
- the constructor’s class is a union ([[class.union]]), and no other
|
| 1343 |
-
variant member of that union is designated by a *mem-initializer-id*
|
| 1344 |
-
or
|
| 1345 |
-
- the constructor’s class is not a union, and, if the entity is a
|
| 1346 |
-
member of an anonymous union, no other member of that union is
|
| 1347 |
-
designated by a *mem-initializer-id*,
|
| 1348 |
-
|
| 1349 |
-
the entity is initialized from its default member initializer as
|
| 1350 |
-
specified in [[dcl.init]];
|
| 1351 |
-
- otherwise, if the entity is an anonymous union or a variant member (
|
| 1352 |
-
[[class.union.anon]]), no initialization is performed;
|
| 1353 |
-
- otherwise, the entity is default-initialized ([[dcl.init]]).
|
| 1354 |
-
|
| 1355 |
-
[*Note 3*: An abstract class ([[class.abstract]]) is never a most
|
| 1356 |
-
derived class, thus its constructors never initialize virtual base
|
| 1357 |
-
classes, therefore the corresponding *mem-initializer*s may be
|
| 1358 |
-
omitted. — *end note*]
|
| 1359 |
-
|
| 1360 |
-
An attempt to initialize more than one non-static data member of a union
|
| 1361 |
-
renders the program ill-formed.
|
| 1362 |
-
|
| 1363 |
-
[*Note 4*: After the call to a constructor for class `X` for an object
|
| 1364 |
-
with automatic or dynamic storage duration has completed, if the
|
| 1365 |
-
constructor was not invoked as part of value-initialization and a member
|
| 1366 |
-
of `X` is neither initialized nor given a value during execution of the
|
| 1367 |
-
*compound-statement* of the body of the constructor, the member has an
|
| 1368 |
-
indeterminate value. — *end note*]
|
| 1369 |
-
|
| 1370 |
-
[*Example 6*:
|
| 1371 |
-
|
| 1372 |
-
``` cpp
|
| 1373 |
-
struct A {
|
| 1374 |
-
A();
|
| 1375 |
-
};
|
| 1376 |
-
|
| 1377 |
-
struct B {
|
| 1378 |
-
B(int);
|
| 1379 |
-
};
|
| 1380 |
-
|
| 1381 |
-
struct C {
|
| 1382 |
-
C() { } // initializes members as follows:
|
| 1383 |
-
A a; // OK: calls A::A()
|
| 1384 |
-
const B b; // error: B has no default constructor
|
| 1385 |
-
int i; // OK: i has indeterminate value
|
| 1386 |
-
int j = 5; // OK: j has the value 5
|
| 1387 |
-
};
|
| 1388 |
-
```
|
| 1389 |
-
|
| 1390 |
-
— *end example*]
|
| 1391 |
-
|
| 1392 |
-
If a given non-static data member has both a default member initializer
|
| 1393 |
-
and a *mem-initializer*, the initialization specified by the
|
| 1394 |
-
*mem-initializer* is performed, and the non-static data member’s default
|
| 1395 |
-
member initializer is ignored.
|
| 1396 |
-
|
| 1397 |
-
[*Example 7*:
|
| 1398 |
-
|
| 1399 |
-
Given
|
| 1400 |
-
|
| 1401 |
-
``` cpp
|
| 1402 |
-
struct A {
|
| 1403 |
-
int i = /* some integer expression with side effects */ ;
|
| 1404 |
-
A(int arg) : i(arg) { }
|
| 1405 |
-
// ...
|
| 1406 |
-
};
|
| 1407 |
-
```
|
| 1408 |
-
|
| 1409 |
-
the `A(int)` constructor will simply initialize `i` to the value of
|
| 1410 |
-
`arg`, and the side effects in `i`’s default member initializer will not
|
| 1411 |
-
take place.
|
| 1412 |
-
|
| 1413 |
-
— *end example*]
|
| 1414 |
-
|
| 1415 |
-
A temporary expression bound to a reference member from a default member
|
| 1416 |
-
initializer is ill-formed.
|
| 1417 |
-
|
| 1418 |
-
[*Example 8*:
|
| 1419 |
-
|
| 1420 |
-
``` cpp
|
| 1421 |
-
struct A {
|
| 1422 |
-
A() = default; // OK
|
| 1423 |
-
A(int v) : v(v) { } // OK
|
| 1424 |
-
const int& v = 42; // OK
|
| 1425 |
-
};
|
| 1426 |
-
A a1; // error: ill-formed binding of temporary to reference
|
| 1427 |
-
A a2(1); // OK, unfortunately
|
| 1428 |
-
```
|
| 1429 |
-
|
| 1430 |
-
— *end example*]
|
| 1431 |
-
|
| 1432 |
-
In a non-delegating constructor, the destructor for each potentially
|
| 1433 |
-
constructed subobject of class type is potentially invoked (
|
| 1434 |
-
[[class.dtor]]).
|
| 1435 |
-
|
| 1436 |
-
[*Note 5*: This provision ensures that destructors can be called for
|
| 1437 |
-
fully-constructed subobjects in case an exception is thrown (
|
| 1438 |
-
[[except.ctor]]). — *end note*]
|
| 1439 |
-
|
| 1440 |
-
In a non-delegating constructor, initialization proceeds in the
|
| 1441 |
-
following order:
|
| 1442 |
-
|
| 1443 |
-
- First, and only for the constructor of the most derived class (
|
| 1444 |
-
[[intro.object]]), virtual base classes are initialized in the order
|
| 1445 |
-
they appear on a depth-first left-to-right traversal of the directed
|
| 1446 |
-
acyclic graph of base classes, where “left-to-right” is the order of
|
| 1447 |
-
appearance of the base classes in the derived class
|
| 1448 |
-
*base-specifier-list*.
|
| 1449 |
-
- Then, direct base classes are initialized in declaration order as they
|
| 1450 |
-
appear in the *base-specifier-list* (regardless of the order of the
|
| 1451 |
-
*mem-initializer*s).
|
| 1452 |
-
- Then, non-static data members are initialized in the order they were
|
| 1453 |
-
declared in the class definition (again regardless of the order of the
|
| 1454 |
-
*mem-initializer*s).
|
| 1455 |
-
- Finally, the *compound-statement* of the constructor body is executed.
|
| 1456 |
-
|
| 1457 |
-
[*Note 6*: The declaration order is mandated to ensure that base and
|
| 1458 |
-
member subobjects are destroyed in the reverse order of
|
| 1459 |
-
initialization. — *end note*]
|
| 1460 |
-
|
| 1461 |
-
[*Example 9*:
|
| 1462 |
-
|
| 1463 |
-
``` cpp
|
| 1464 |
-
struct V {
|
| 1465 |
-
V();
|
| 1466 |
-
V(int);
|
| 1467 |
-
};
|
| 1468 |
-
|
| 1469 |
-
struct A : virtual V {
|
| 1470 |
-
A();
|
| 1471 |
-
A(int);
|
| 1472 |
-
};
|
| 1473 |
-
|
| 1474 |
-
struct B : virtual V {
|
| 1475 |
-
B();
|
| 1476 |
-
B(int);
|
| 1477 |
-
};
|
| 1478 |
-
|
| 1479 |
-
struct C : A, B, virtual V {
|
| 1480 |
-
C();
|
| 1481 |
-
C(int);
|
| 1482 |
-
};
|
| 1483 |
-
|
| 1484 |
-
A::A(int i) : V(i) { ... }
|
| 1485 |
-
B::B(int i) { ... }
|
| 1486 |
-
C::C(int i) { ... }
|
| 1487 |
-
|
| 1488 |
-
V v(1); // use V(int)
|
| 1489 |
-
A a(2); // use V(int)
|
| 1490 |
-
B b(3); // use V()
|
| 1491 |
-
C c(4); // use V()
|
| 1492 |
-
```
|
| 1493 |
-
|
| 1494 |
-
— *end example*]
|
| 1495 |
-
|
| 1496 |
-
Names in the *expression-list* or *braced-init-list* of a
|
| 1497 |
-
*mem-initializer* are evaluated in the scope of the constructor for
|
| 1498 |
-
which the *mem-initializer* is specified.
|
| 1499 |
-
|
| 1500 |
-
[*Example 10*:
|
| 1501 |
-
|
| 1502 |
-
``` cpp
|
| 1503 |
-
class X {
|
| 1504 |
-
int a;
|
| 1505 |
-
int b;
|
| 1506 |
-
int i;
|
| 1507 |
-
int j;
|
| 1508 |
-
public:
|
| 1509 |
-
const int& r;
|
| 1510 |
-
X(int i): r(a), b(i), i(i), j(this->i) { }
|
| 1511 |
-
};
|
| 1512 |
-
```
|
| 1513 |
-
|
| 1514 |
-
initializes `X::r` to refer to `X::a`, initializes `X::b` with the value
|
| 1515 |
-
of the constructor parameter `i`, initializes `X::i` with the value of
|
| 1516 |
-
the constructor parameter `i`, and initializes `X::j` with the value of
|
| 1517 |
-
`X::i`; this takes place each time an object of class `X` is created.
|
| 1518 |
-
|
| 1519 |
-
— *end example*]
|
| 1520 |
-
|
| 1521 |
-
[*Note 7*: Because the *mem-initializer* are evaluated in the scope of
|
| 1522 |
-
the constructor, the `this` pointer can be used in the *expression-list*
|
| 1523 |
-
of a *mem-initializer* to refer to the object being
|
| 1524 |
-
initialized. — *end note*]
|
| 1525 |
-
|
| 1526 |
-
Member functions (including virtual member functions, [[class.virtual]])
|
| 1527 |
-
can be called for an object under construction. Similarly, an object
|
| 1528 |
-
under construction can be the operand of the `typeid` operator (
|
| 1529 |
-
[[expr.typeid]]) or of a `dynamic_cast` ([[expr.dynamic.cast]]).
|
| 1530 |
-
However, if these operations are performed in a *ctor-initializer* (or
|
| 1531 |
-
in a function called directly or indirectly from a *ctor-initializer*)
|
| 1532 |
-
before all the *mem-initializer*s for base classes have completed, the
|
| 1533 |
-
program has undefined behavior.
|
| 1534 |
-
|
| 1535 |
-
[*Example 11*:
|
| 1536 |
-
|
| 1537 |
-
``` cpp
|
| 1538 |
-
class A {
|
| 1539 |
-
public:
|
| 1540 |
-
A(int);
|
| 1541 |
-
};
|
| 1542 |
-
|
| 1543 |
-
class B : public A {
|
| 1544 |
-
int j;
|
| 1545 |
-
public:
|
| 1546 |
-
int f();
|
| 1547 |
-
B() : A(f()), // undefined: calls member function but base A not yet initialized
|
| 1548 |
-
j(f()) { } // well-defined: bases are all initialized
|
| 1549 |
-
};
|
| 1550 |
-
|
| 1551 |
-
class C {
|
| 1552 |
-
public:
|
| 1553 |
-
C(int);
|
| 1554 |
-
};
|
| 1555 |
-
|
| 1556 |
-
class D : public B, C {
|
| 1557 |
-
int i;
|
| 1558 |
-
public:
|
| 1559 |
-
D() : C(f()), // undefined: calls member function but base C not yet initialized
|
| 1560 |
-
i(f()) { } // well-defined: bases are all initialized
|
| 1561 |
-
};
|
| 1562 |
-
```
|
| 1563 |
-
|
| 1564 |
-
— *end example*]
|
| 1565 |
-
|
| 1566 |
-
[*Note 8*: [[class.cdtor]] describes the result of virtual function
|
| 1567 |
-
calls, `typeid` and `dynamic_cast`s during construction for the
|
| 1568 |
-
well-defined cases; that is, describes the *polymorphic behavior* of an
|
| 1569 |
-
object under construction. — *end note*]
|
| 1570 |
-
|
| 1571 |
-
A *mem-initializer* followed by an ellipsis is a pack expansion (
|
| 1572 |
-
[[temp.variadic]]) that initializes the base classes specified by a pack
|
| 1573 |
-
expansion in the *base-specifier-list* for the class.
|
| 1574 |
-
|
| 1575 |
-
[*Example 12*:
|
| 1576 |
-
|
| 1577 |
-
``` cpp
|
| 1578 |
-
template<class... Mixins>
|
| 1579 |
-
class X : public Mixins... {
|
| 1580 |
-
public:
|
| 1581 |
-
X(const Mixins&... mixins) : Mixins(mixins)... { }
|
| 1582 |
-
};
|
| 1583 |
-
```
|
| 1584 |
-
|
| 1585 |
-
— *end example*]
|
| 1586 |
-
|
| 1587 |
-
### Initialization by inherited constructor <a id="class.inhctor.init">[[class.inhctor.init]]</a>
|
| 1588 |
-
|
| 1589 |
-
When a constructor for type `B` is invoked to initialize an object of a
|
| 1590 |
-
different type `D` (that is, when the constructor was inherited (
|
| 1591 |
-
[[namespace.udecl]])), initialization proceeds as if a defaulted default
|
| 1592 |
-
constructor were used to initialize the `D` object and each base class
|
| 1593 |
-
subobject from which the constructor was inherited, except that the `B`
|
| 1594 |
-
subobject is initialized by the invocation of the inherited constructor.
|
| 1595 |
-
The complete initialization is considered to be a single function call;
|
| 1596 |
-
in particular, the initialization of the inherited constructor’s
|
| 1597 |
-
parameters is sequenced before the initialization of any part of the `D`
|
| 1598 |
-
object.
|
| 1599 |
-
|
| 1600 |
-
[*Example 1*:
|
| 1601 |
-
|
| 1602 |
-
``` cpp
|
| 1603 |
-
struct B1 {
|
| 1604 |
-
B1(int, ...) { }
|
| 1605 |
-
};
|
| 1606 |
-
|
| 1607 |
-
struct B2 {
|
| 1608 |
-
B2(double) { }
|
| 1609 |
-
};
|
| 1610 |
-
|
| 1611 |
-
int get();
|
| 1612 |
-
|
| 1613 |
-
struct D1 : B1 {
|
| 1614 |
-
using B1::B1; // inherits B1(int, ...)
|
| 1615 |
-
int x;
|
| 1616 |
-
int y = get();
|
| 1617 |
-
};
|
| 1618 |
-
|
| 1619 |
-
void test() {
|
| 1620 |
-
D1 d(2, 3, 4); // OK: B1 is initialized by calling B1(2, 3, 4),
|
| 1621 |
-
// then d.x is default-initialized (no initialization is performed),
|
| 1622 |
-
// then d.y is initialized by calling get()
|
| 1623 |
-
D1 e; // error: D1 has a deleted default constructor
|
| 1624 |
-
}
|
| 1625 |
-
|
| 1626 |
-
struct D2 : B2 {
|
| 1627 |
-
using B2::B2;
|
| 1628 |
-
B1 b;
|
| 1629 |
-
};
|
| 1630 |
-
|
| 1631 |
-
D2 f(1.0); // error: B1 has a deleted default constructor
|
| 1632 |
-
|
| 1633 |
-
struct W { W(int); };
|
| 1634 |
-
struct X : virtual W { using W::W; X() = delete; };
|
| 1635 |
-
struct Y : X { using X::X; };
|
| 1636 |
-
struct Z : Y, virtual W { using Y::Y; };
|
| 1637 |
-
Z z(0); // OK: initialization of Y does not invoke default constructor of X
|
| 1638 |
-
|
| 1639 |
-
template<class T> struct Log : T {
|
| 1640 |
-
using T::T; // inherits all constructors from class T
|
| 1641 |
-
~Log() { std::clog << "Destroying wrapper" << std::endl; }
|
| 1642 |
-
};
|
| 1643 |
-
```
|
| 1644 |
-
|
| 1645 |
-
Class template `Log` wraps any class and forwards all of its
|
| 1646 |
-
constructors, while writing a message to the standard log whenever an
|
| 1647 |
-
object of class `Log` is destroyed.
|
| 1648 |
-
|
| 1649 |
-
— *end example*]
|
| 1650 |
-
|
| 1651 |
-
If the constructor was inherited from multiple base class subobjects of
|
| 1652 |
-
type `B`, the program is ill-formed.
|
| 1653 |
-
|
| 1654 |
-
[*Example 2*:
|
| 1655 |
-
|
| 1656 |
-
``` cpp
|
| 1657 |
-
struct A { A(int); };
|
| 1658 |
-
struct B : A { using A::A; };
|
| 1659 |
-
|
| 1660 |
-
struct C1 : B { using B::B; };
|
| 1661 |
-
struct C2 : B { using B::B; };
|
| 1662 |
-
|
| 1663 |
-
struct D1 : C1, C2 {
|
| 1664 |
-
using C1::C1;
|
| 1665 |
-
using C2::C2;
|
| 1666 |
-
};
|
| 1667 |
-
|
| 1668 |
-
struct V1 : virtual B { using B::B; };
|
| 1669 |
-
struct V2 : virtual B { using B::B; };
|
| 1670 |
-
|
| 1671 |
-
struct D2 : V1, V2 {
|
| 1672 |
-
using V1::V1;
|
| 1673 |
-
using V2::V2;
|
| 1674 |
-
};
|
| 1675 |
-
|
| 1676 |
-
D1 d1(0); // ill-formed: ambiguous
|
| 1677 |
-
D2 d2(0); // OK: initializes virtual B base class, which initializes the A base class
|
| 1678 |
-
// then initializes the V1 and V2 base classes as if by a defaulted default constructor
|
| 1679 |
-
|
| 1680 |
-
struct M { M(); M(int); };
|
| 1681 |
-
struct N : M { using M::M; };
|
| 1682 |
-
struct O : M {};
|
| 1683 |
-
struct P : N, O { using N::N; using O::O; };
|
| 1684 |
-
P p(0); // OK: use M(0) to initialize N's base class,
|
| 1685 |
-
// use M() to initialize O's base class
|
| 1686 |
-
```
|
| 1687 |
-
|
| 1688 |
-
— *end example*]
|
| 1689 |
-
|
| 1690 |
-
When an object is initialized by an inherited constructor,
|
| 1691 |
-
initialization of the object is complete when the initialization of all
|
| 1692 |
-
subobjects is complete.
|
| 1693 |
-
|
| 1694 |
-
## Construction and destruction <a id="class.cdtor">[[class.cdtor]]</a>
|
| 1695 |
-
|
| 1696 |
-
For an object with a non-trivial constructor, referring to any
|
| 1697 |
-
non-static member or base class of the object before the constructor
|
| 1698 |
-
begins execution results in undefined behavior. For an object with a
|
| 1699 |
-
non-trivial destructor, referring to any non-static member or base class
|
| 1700 |
-
of the object after the destructor finishes execution results in
|
| 1701 |
-
undefined behavior.
|
| 1702 |
-
|
| 1703 |
-
[*Example 1*:
|
| 1704 |
-
|
| 1705 |
-
``` cpp
|
| 1706 |
-
struct X { int i; };
|
| 1707 |
-
struct Y : X { Y(); }; // non-trivial
|
| 1708 |
-
struct A { int a; };
|
| 1709 |
-
struct B : public A { int j; Y y; }; // non-trivial
|
| 1710 |
-
|
| 1711 |
-
extern B bobj;
|
| 1712 |
-
B* pb = &bobj; // OK
|
| 1713 |
-
int* p1 = &bobj.a; // undefined, refers to base class member
|
| 1714 |
-
int* p2 = &bobj.y.i; // undefined, refers to member's member
|
| 1715 |
-
|
| 1716 |
-
A* pa = &bobj; // undefined, upcast to a base class type
|
| 1717 |
-
B bobj; // definition of bobj
|
| 1718 |
-
|
| 1719 |
-
extern X xobj;
|
| 1720 |
-
int* p3 = &xobj.i; // OK, X is a trivial class
|
| 1721 |
-
X xobj;
|
| 1722 |
-
```
|
| 1723 |
-
|
| 1724 |
-
For another example,
|
| 1725 |
-
|
| 1726 |
-
``` cpp
|
| 1727 |
-
struct W { int j; };
|
| 1728 |
-
struct X : public virtual W { };
|
| 1729 |
-
struct Y {
|
| 1730 |
-
int* p;
|
| 1731 |
-
X x;
|
| 1732 |
-
Y() : p(&x.j) { // undefined, x is not yet constructed
|
| 1733 |
-
}
|
| 1734 |
-
};
|
| 1735 |
-
```
|
| 1736 |
-
|
| 1737 |
-
— *end example*]
|
| 1738 |
-
|
| 1739 |
-
To explicitly or implicitly convert a pointer (a glvalue) referring to
|
| 1740 |
-
an object of class `X` to a pointer (reference) to a direct or indirect
|
| 1741 |
-
base class `B` of `X`, the construction of `X` and the construction of
|
| 1742 |
-
all of its direct or indirect bases that directly or indirectly derive
|
| 1743 |
-
from `B` shall have started and the destruction of these classes shall
|
| 1744 |
-
not have completed, otherwise the conversion results in undefined
|
| 1745 |
-
behavior. To form a pointer to (or access the value of) a direct
|
| 1746 |
-
non-static member of an object `obj`, the construction of `obj` shall
|
| 1747 |
-
have started and its destruction shall not have completed, otherwise the
|
| 1748 |
-
computation of the pointer value (or accessing the member value) results
|
| 1749 |
-
in undefined behavior.
|
| 1750 |
-
|
| 1751 |
-
[*Example 2*:
|
| 1752 |
-
|
| 1753 |
-
``` cpp
|
| 1754 |
-
struct A { };
|
| 1755 |
-
struct B : virtual A { };
|
| 1756 |
-
struct C : B { };
|
| 1757 |
-
struct D : virtual A { D(A*); };
|
| 1758 |
-
struct X { X(A*); };
|
| 1759 |
-
|
| 1760 |
-
struct E : C, D, X {
|
| 1761 |
-
E() : D(this), // undefined: upcast from E* to A* might use path E* → D* → A*
|
| 1762 |
-
// but D is not constructed
|
| 1763 |
-
|
| 1764 |
-
// ``D((C*)this)'' would be defined: E* → C* is defined because E() has started,
|
| 1765 |
-
// and C* → A* is defined because C is fully constructed
|
| 1766 |
-
|
| 1767 |
-
X(this) {} // defined: upon construction of X, C/B/D/A sublattice is fully constructed
|
| 1768 |
-
};
|
| 1769 |
-
```
|
| 1770 |
-
|
| 1771 |
-
— *end example*]
|
| 1772 |
-
|
| 1773 |
-
Member functions, including virtual functions ([[class.virtual]]), can
|
| 1774 |
-
be called during construction or destruction ([[class.base.init]]).
|
| 1775 |
-
When a virtual function is called directly or indirectly from a
|
| 1776 |
-
constructor or from a destructor, including during the construction or
|
| 1777 |
-
destruction of the class’s non-static data members, and the object to
|
| 1778 |
-
which the call applies is the object (call it `x`) under construction or
|
| 1779 |
-
destruction, the function called is the final overrider in the
|
| 1780 |
-
constructor’s or destructor’s class and not one overriding it in a
|
| 1781 |
-
more-derived class. If the virtual function call uses an explicit class
|
| 1782 |
-
member access ([[expr.ref]]) and the object expression refers to the
|
| 1783 |
-
complete object of `x` or one of that object’s base class subobjects but
|
| 1784 |
-
not `x` or one of its base class subobjects, the behavior is undefined.
|
| 1785 |
-
|
| 1786 |
-
[*Example 3*:
|
| 1787 |
-
|
| 1788 |
-
``` cpp
|
| 1789 |
-
struct V {
|
| 1790 |
-
virtual void f();
|
| 1791 |
-
virtual void g();
|
| 1792 |
-
};
|
| 1793 |
-
|
| 1794 |
-
struct A : virtual V {
|
| 1795 |
-
virtual void f();
|
| 1796 |
-
};
|
| 1797 |
-
|
| 1798 |
-
struct B : virtual V {
|
| 1799 |
-
virtual void g();
|
| 1800 |
-
B(V*, A*);
|
| 1801 |
-
};
|
| 1802 |
-
|
| 1803 |
-
struct D : A, B {
|
| 1804 |
-
virtual void f();
|
| 1805 |
-
virtual void g();
|
| 1806 |
-
D() : B((A*)this, this) { }
|
| 1807 |
-
};
|
| 1808 |
-
|
| 1809 |
-
B::B(V* v, A* a) {
|
| 1810 |
-
f(); // calls V::f, not A::f
|
| 1811 |
-
g(); // calls B::g, not D::g
|
| 1812 |
-
v->g(); // v is base of B, the call is well-defined, calls B::g
|
| 1813 |
-
a->f(); // undefined behavior, a's type not a base of B
|
| 1814 |
-
}
|
| 1815 |
-
```
|
| 1816 |
-
|
| 1817 |
-
— *end example*]
|
| 1818 |
-
|
| 1819 |
-
The `typeid` operator ([[expr.typeid]]) can be used during construction
|
| 1820 |
-
or destruction ([[class.base.init]]). When `typeid` is used in a
|
| 1821 |
-
constructor (including the *mem-initializer* or default member
|
| 1822 |
-
initializer ([[class.mem]]) for a non-static data member) or in a
|
| 1823 |
-
destructor, or used in a function called (directly or indirectly) from a
|
| 1824 |
-
constructor or destructor, if the operand of `typeid` refers to the
|
| 1825 |
-
object under construction or destruction, `typeid` yields the
|
| 1826 |
-
`std::type_info` object representing the constructor or destructor’s
|
| 1827 |
-
class. If the operand of `typeid` refers to the object under
|
| 1828 |
-
construction or destruction and the static type of the operand is
|
| 1829 |
-
neither the constructor or destructor’s class nor one of its bases, the
|
| 1830 |
-
behavior is undefined.
|
| 1831 |
-
|
| 1832 |
-
`dynamic_cast`s ([[expr.dynamic.cast]]) can be used during construction
|
| 1833 |
-
or destruction ([[class.base.init]]). When a `dynamic_cast` is used in
|
| 1834 |
-
a constructor (including the *mem-initializer* or default member
|
| 1835 |
-
initializer for a non-static data member) or in a destructor, or used in
|
| 1836 |
-
a function called (directly or indirectly) from a constructor or
|
| 1837 |
-
destructor, if the operand of the `dynamic_cast` refers to the object
|
| 1838 |
-
under construction or destruction, this object is considered to be a
|
| 1839 |
-
most derived object that has the type of the constructor or destructor’s
|
| 1840 |
-
class. If the operand of the `dynamic_cast` refers to the object under
|
| 1841 |
-
construction or destruction and the static type of the operand is not a
|
| 1842 |
-
pointer to or object of the constructor or destructor’s own class or one
|
| 1843 |
-
of its bases, the `dynamic_cast` results in undefined behavior.
|
| 1844 |
-
|
| 1845 |
-
[*Example 4*:
|
| 1846 |
-
|
| 1847 |
-
``` cpp
|
| 1848 |
-
struct V {
|
| 1849 |
-
virtual void f();
|
| 1850 |
-
};
|
| 1851 |
-
|
| 1852 |
-
struct A : virtual V { };
|
| 1853 |
-
|
| 1854 |
-
struct B : virtual V {
|
| 1855 |
-
B(V*, A*);
|
| 1856 |
-
};
|
| 1857 |
-
|
| 1858 |
-
struct D : A, B {
|
| 1859 |
-
D() : B((A*)this, this) { }
|
| 1860 |
-
};
|
| 1861 |
-
|
| 1862 |
-
B::B(V* v, A* a) {
|
| 1863 |
-
typeid(*this); // type_info for B
|
| 1864 |
-
typeid(*v); // well-defined: *v has type V, a base of B yields type_info for B
|
| 1865 |
-
typeid(*a); // undefined behavior: type A not a base of B
|
| 1866 |
-
dynamic_cast<B*>(v); // well-defined: v of type V*, V base of B results in B*
|
| 1867 |
-
dynamic_cast<B*>(a); // undefined behavior, a has type A*, A not a base of B
|
| 1868 |
-
}
|
| 1869 |
-
```
|
| 1870 |
-
|
| 1871 |
-
— *end example*]
|
| 1872 |
-
|
| 1873 |
-
## Copying and moving class objects <a id="class.copy">[[class.copy]]</a>
|
| 1874 |
-
|
| 1875 |
-
A class object can be copied or moved in two ways: by initialization (
|
| 1876 |
-
[[class.ctor]], [[dcl.init]]), including for function argument passing (
|
| 1877 |
-
[[expr.call]]) and for function value return ([[stmt.return]]); and by
|
| 1878 |
-
assignment ([[expr.ass]]). Conceptually, these two operations are
|
| 1879 |
-
implemented by a copy/move constructor ([[class.ctor]]) and copy/move
|
| 1880 |
-
assignment operator ([[over.ass]]).
|
| 1881 |
-
|
| 1882 |
-
A program is ill-formed if the copy/move constructor or the copy/move
|
| 1883 |
-
assignment operator for an object is implicitly odr-used and the special
|
| 1884 |
-
member function is not accessible (Clause [[class.access]]).
|
| 1885 |
-
|
| 1886 |
-
[*Note 1*: Copying/moving one object into another using the copy/move
|
| 1887 |
-
constructor or the copy/move assignment operator does not change the
|
| 1888 |
-
layout or size of either object. — *end note*]
|
| 1889 |
-
|
| 1890 |
-
### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
| 1891 |
-
|
| 1892 |
-
A non-template constructor for class `X` is a copy constructor if its
|
| 1893 |
-
first parameter is of type `X&`, `const X&`, `volatile X&` or
|
| 1894 |
-
`const volatile X&`, and either there are no other parameters or else
|
| 1895 |
-
all other parameters have default arguments ([[dcl.fct.default]]).
|
| 1896 |
-
|
| 1897 |
-
[*Example 1*:
|
| 1898 |
-
|
| 1899 |
-
`X::X(const X&)`
|
| 1900 |
-
|
| 1901 |
-
and `X::X(X&,int=1)` are copy constructors.
|
| 1902 |
-
|
| 1903 |
-
``` cpp
|
| 1904 |
-
struct X {
|
| 1905 |
-
X(int);
|
| 1906 |
-
X(const X&, int = 1);
|
| 1907 |
-
};
|
| 1908 |
-
X a(1); // calls X(int);
|
| 1909 |
-
X b(a, 0); // calls X(const X&, int);
|
| 1910 |
-
X c = b; // calls X(const X&, int);
|
| 1911 |
-
```
|
| 1912 |
-
|
| 1913 |
-
— *end example*]
|
| 1914 |
-
|
| 1915 |
-
A non-template constructor for class `X` is a move constructor if its
|
| 1916 |
-
first parameter is of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 1917 |
-
`const volatile X&&`, and either there are no other parameters or else
|
| 1918 |
-
all other parameters have default arguments ([[dcl.fct.default]]).
|
| 1919 |
-
|
| 1920 |
-
[*Example 2*:
|
| 1921 |
-
|
| 1922 |
-
`Y::Y(Y&&)` is a move constructor.
|
| 1923 |
-
|
| 1924 |
-
``` cpp
|
| 1925 |
-
struct Y {
|
| 1926 |
-
Y(const Y&);
|
| 1927 |
-
Y(Y&&);
|
| 1928 |
-
};
|
| 1929 |
-
extern Y f(int);
|
| 1930 |
-
Y d(f(1)); // calls Y(Y&&)
|
| 1931 |
-
Y e = d; // calls Y(const Y&)
|
| 1932 |
-
```
|
| 1933 |
-
|
| 1934 |
-
— *end example*]
|
| 1935 |
-
|
| 1936 |
-
[*Note 1*:
|
| 1937 |
-
|
| 1938 |
-
All forms of copy/move constructor may be declared for a class.
|
| 1939 |
-
|
| 1940 |
-
[*Example 3*:
|
| 1941 |
-
|
| 1942 |
-
``` cpp
|
| 1943 |
-
struct X {
|
| 1944 |
-
X(const X&);
|
| 1945 |
-
X(X&); // OK
|
| 1946 |
-
X(X&&);
|
| 1947 |
-
X(const X&&); // OK, but possibly not sensible
|
| 1948 |
-
};
|
| 1949 |
-
```
|
| 1950 |
-
|
| 1951 |
-
— *end example*]
|
| 1952 |
-
|
| 1953 |
-
— *end note*]
|
| 1954 |
-
|
| 1955 |
-
[*Note 2*:
|
| 1956 |
-
|
| 1957 |
-
If a class `X` only has a copy constructor with a parameter of type
|
| 1958 |
-
`X&`, an initializer of type `const` `X` or `volatile` `X` cannot
|
| 1959 |
-
initialize an object of type (possibly cv-qualified) `X`.
|
| 1960 |
-
|
| 1961 |
-
[*Example 4*:
|
| 1962 |
-
|
| 1963 |
-
``` cpp
|
| 1964 |
-
struct X {
|
| 1965 |
-
X(); // default constructor
|
| 1966 |
-
X(X&); // copy constructor with a non-const parameter
|
| 1967 |
-
};
|
| 1968 |
-
const X cx;
|
| 1969 |
-
X x = cx; // error: X::X(X&) cannot copy cx into x
|
| 1970 |
-
```
|
| 1971 |
-
|
| 1972 |
-
— *end example*]
|
| 1973 |
-
|
| 1974 |
-
— *end note*]
|
| 1975 |
-
|
| 1976 |
-
A declaration of a constructor for a class `X` is ill-formed if its
|
| 1977 |
-
first parameter is of type (optionally cv-qualified) `X` and either
|
| 1978 |
-
there are no other parameters or else all other parameters have default
|
| 1979 |
-
arguments. A member function template is never instantiated to produce
|
| 1980 |
-
such a constructor signature.
|
| 1981 |
-
|
| 1982 |
-
[*Example 5*:
|
| 1983 |
-
|
| 1984 |
-
``` cpp
|
| 1985 |
-
struct S {
|
| 1986 |
-
template<typename T> S(T);
|
| 1987 |
-
S();
|
| 1988 |
-
};
|
| 1989 |
-
|
| 1990 |
-
S g;
|
| 1991 |
-
|
| 1992 |
-
void h() {
|
| 1993 |
-
S a(g); // does not instantiate the member template to produce S::S<S>(S);
|
| 1994 |
-
// uses the implicitly declared copy constructor
|
| 1995 |
-
}
|
| 1996 |
-
```
|
| 1997 |
-
|
| 1998 |
-
— *end example*]
|
| 1999 |
-
|
| 2000 |
-
If the class definition does not explicitly declare a copy constructor,
|
| 2001 |
-
a non-explicit one is declared *implicitly*. If the class definition
|
| 2002 |
-
declares a move constructor or move assignment operator, the implicitly
|
| 2003 |
-
declared copy constructor is defined as deleted; otherwise, it is
|
| 2004 |
-
defined as defaulted ([[dcl.fct.def]]). The latter case is deprecated
|
| 2005 |
-
if the class has a user-declared copy assignment operator or a
|
| 2006 |
-
user-declared destructor.
|
| 2007 |
-
|
| 2008 |
-
The implicitly-declared copy constructor for a class `X` will have the
|
| 2009 |
-
form
|
| 2010 |
-
|
| 2011 |
-
``` cpp
|
| 2012 |
-
X::X(const X&)
|
| 2013 |
-
```
|
| 2014 |
-
|
| 2015 |
-
if each potentially constructed subobject of a class type `M` (or array
|
| 2016 |
-
thereof) has a copy constructor whose first parameter is of type `const`
|
| 2017 |
-
`M&` or `const` `volatile` `M&`.[^4] Otherwise, the implicitly-declared
|
| 2018 |
-
copy constructor will have the form
|
| 2019 |
-
|
| 2020 |
-
``` cpp
|
| 2021 |
-
X::X(X&)
|
| 2022 |
-
```
|
| 2023 |
-
|
| 2024 |
-
If the definition of a class `X` does not explicitly declare a move
|
| 2025 |
-
constructor, a non-explicit one will be implicitly declared as defaulted
|
| 2026 |
-
if and only if
|
| 2027 |
-
|
| 2028 |
-
- `X` does not have a user-declared copy constructor,
|
| 2029 |
-
- `X` does not have a user-declared copy assignment operator,
|
| 2030 |
-
- `X` does not have a user-declared move assignment operator, and
|
| 2031 |
-
- `X` does not have a user-declared destructor.
|
| 2032 |
-
|
| 2033 |
-
[*Note 3*: When the move constructor is not implicitly declared or
|
| 2034 |
-
explicitly supplied, expressions that otherwise would have invoked the
|
| 2035 |
-
move constructor may instead invoke a copy constructor. — *end note*]
|
| 2036 |
-
|
| 2037 |
-
The implicitly-declared move constructor for class `X` will have the
|
| 2038 |
-
form
|
| 2039 |
-
|
| 2040 |
-
``` cpp
|
| 2041 |
-
X::X(X&&)
|
| 2042 |
-
```
|
| 2043 |
-
|
| 2044 |
-
An implicitly-declared copy/move constructor is an `inline` `public`
|
| 2045 |
-
member of its class. A defaulted copy/move constructor for a class `X`
|
| 2046 |
-
is defined as deleted ([[dcl.fct.def.delete]]) if `X` has:
|
| 2047 |
-
|
| 2048 |
-
- a variant member with a non-trivial corresponding constructor and `X`
|
| 2049 |
-
is a union-like class,
|
| 2050 |
-
- a potentially constructed subobject type `M` (or array thereof) that
|
| 2051 |
-
cannot be copied/moved because overload resolution ([[over.match]]),
|
| 2052 |
-
as applied to find `M`’s corresponding constructor, results in an
|
| 2053 |
-
ambiguity or a function that is deleted or inaccessible from the
|
| 2054 |
-
defaulted constructor,
|
| 2055 |
-
- any potentially constructed subobject of a type with a destructor that
|
| 2056 |
-
is deleted or inaccessible from the defaulted constructor, or,
|
| 2057 |
-
- for the copy constructor, a non-static data member of rvalue reference
|
| 2058 |
-
type.
|
| 2059 |
-
|
| 2060 |
-
A defaulted move constructor that is defined as deleted is ignored by
|
| 2061 |
-
overload resolution ([[over.match]], [[over.over]]).
|
| 2062 |
-
|
| 2063 |
-
[*Note 4*: A deleted move constructor would otherwise interfere with
|
| 2064 |
-
initialization from an rvalue which can use the copy constructor
|
| 2065 |
-
instead. — *end note*]
|
| 2066 |
-
|
| 2067 |
-
A copy/move constructor for class `X` is trivial if it is not
|
| 2068 |
-
user-provided and if:
|
| 2069 |
-
|
| 2070 |
-
- class `X` has no virtual functions ([[class.virtual]]) and no virtual
|
| 2071 |
-
base classes ([[class.mi]]), and
|
| 2072 |
-
- the constructor selected to copy/move each direct base class subobject
|
| 2073 |
-
is trivial, and
|
| 2074 |
-
- for each non-static data member of `X` that is of class type (or array
|
| 2075 |
-
thereof), the constructor selected to copy/move that member is
|
| 2076 |
-
trivial;
|
| 2077 |
-
|
| 2078 |
-
otherwise the copy/move constructor is *non-trivial*.
|
| 2079 |
-
|
| 2080 |
-
A copy/move constructor that is defaulted and not defined as deleted is
|
| 2081 |
-
*implicitly defined* if it is odr-used ([[basic.def.odr]]) or when it
|
| 2082 |
-
is explicitly defaulted after its first declaration.
|
| 2083 |
-
|
| 2084 |
-
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 2085 |
-
implementation elided its odr-use ([[basic.def.odr]],
|
| 2086 |
-
[[class.temporary]]). — *end note*]
|
| 2087 |
-
|
| 2088 |
-
If the implicitly-defined constructor would satisfy the requirements of
|
| 2089 |
-
a constexpr constructor ([[dcl.constexpr]]), the implicitly-defined
|
| 2090 |
-
constructor is `constexpr`.
|
| 2091 |
-
|
| 2092 |
-
Before the defaulted copy/move constructor for a class is implicitly
|
| 2093 |
-
defined, all non-user-provided copy/move constructors for its
|
| 2094 |
-
potentially constructed subobjects shall have been implicitly defined.
|
| 2095 |
-
|
| 2096 |
-
[*Note 6*: An implicitly-declared copy/move constructor has an implied
|
| 2097 |
-
exception specification ([[except.spec]]). — *end note*]
|
| 2098 |
-
|
| 2099 |
-
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 2100 |
-
performs a memberwise copy/move of its bases and members.
|
| 2101 |
-
|
| 2102 |
-
[*Note 7*: Default member initializers of non-static data members are
|
| 2103 |
-
ignored. See also the example in [[class.base.init]]. — *end note*]
|
| 2104 |
-
|
| 2105 |
-
The order of initialization is the same as the order of initialization
|
| 2106 |
-
of bases and members in a user-defined constructor (see
|
| 2107 |
-
[[class.base.init]]). Let `x` be either the parameter of the constructor
|
| 2108 |
-
or, for the move constructor, an xvalue referring to the parameter. Each
|
| 2109 |
-
base or non-static data member is copied/moved in the manner appropriate
|
| 2110 |
-
to its type:
|
| 2111 |
-
|
| 2112 |
-
- if the member is an array, each element is direct-initialized with the
|
| 2113 |
-
corresponding subobject of `x`;
|
| 2114 |
-
- if a member `m` has rvalue reference type `T&&`, it is
|
| 2115 |
-
direct-initialized with `static_cast<T&&>(x.m)`;
|
| 2116 |
-
- otherwise, the base or member is direct-initialized with the
|
| 2117 |
-
corresponding base or member of `x`.
|
| 2118 |
-
|
| 2119 |
-
Virtual base class subobjects shall be initialized only once by the
|
| 2120 |
-
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 2121 |
-
|
| 2122 |
-
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 2123 |
-
object representation ([[basic.types]]) of `X`.
|
| 2124 |
-
|
| 2125 |
-
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 2126 |
-
|
| 2127 |
-
A user-declared *copy* assignment operator `X::operator=` is a
|
| 2128 |
-
non-static non-template member function of class `X` with exactly one
|
| 2129 |
-
parameter of type `X`, `X&`, `const` `X&`, `volatile` `X&` or `const`
|
| 2130 |
-
`volatile` `X&`.[^5]
|
| 2131 |
-
|
| 2132 |
-
[*Note 1*: An overloaded assignment operator must be declared to have
|
| 2133 |
-
only one parameter; see [[over.ass]]. — *end note*]
|
| 2134 |
-
|
| 2135 |
-
[*Note 2*: More than one form of copy assignment operator may be
|
| 2136 |
-
declared for a class. — *end note*]
|
| 2137 |
-
|
| 2138 |
-
[*Note 3*:
|
| 2139 |
-
|
| 2140 |
-
If a class `X` only has a copy assignment operator with a parameter of
|
| 2141 |
-
type `X&`, an expression of type const `X` cannot be assigned to an
|
| 2142 |
-
object of type `X`.
|
| 2143 |
-
|
| 2144 |
-
[*Example 1*:
|
| 2145 |
-
|
| 2146 |
-
``` cpp
|
| 2147 |
-
struct X {
|
| 2148 |
-
X();
|
| 2149 |
-
X& operator=(X&);
|
| 2150 |
-
};
|
| 2151 |
-
const X cx;
|
| 2152 |
-
X x;
|
| 2153 |
-
void f() {
|
| 2154 |
-
x = cx; // error: X::operator=(X&) cannot assign cx into x
|
| 2155 |
-
}
|
| 2156 |
-
```
|
| 2157 |
-
|
| 2158 |
-
— *end example*]
|
| 2159 |
-
|
| 2160 |
-
— *end note*]
|
| 2161 |
-
|
| 2162 |
-
If the class definition does not explicitly declare a copy assignment
|
| 2163 |
-
operator, one is declared *implicitly*. If the class definition declares
|
| 2164 |
-
a move constructor or move assignment operator, the implicitly declared
|
| 2165 |
-
copy assignment operator is defined as deleted; otherwise, it is defined
|
| 2166 |
-
as defaulted ([[dcl.fct.def]]). The latter case is deprecated if the
|
| 2167 |
-
class has a user-declared copy constructor or a user-declared
|
| 2168 |
-
destructor. The implicitly-declared copy assignment operator for a class
|
| 2169 |
-
`X` will have the form
|
| 2170 |
-
|
| 2171 |
-
``` cpp
|
| 2172 |
-
X& X::operator=(const X&)
|
| 2173 |
-
```
|
| 2174 |
-
|
| 2175 |
-
if
|
| 2176 |
-
|
| 2177 |
-
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 2178 |
-
parameter is of type `const` `B&`, `const` `volatile` `B&` or `B`, and
|
| 2179 |
-
- for all the non-static data members of `X` that are of a class type
|
| 2180 |
-
`M` (or array thereof), each such class type has a copy assignment
|
| 2181 |
-
operator whose parameter is of type `const` `M&`, `const` `volatile`
|
| 2182 |
-
`M&` or `M`.[^6]
|
| 2183 |
-
|
| 2184 |
-
Otherwise, the implicitly-declared copy assignment operator will have
|
| 2185 |
-
the form
|
| 2186 |
-
|
| 2187 |
-
``` cpp
|
| 2188 |
-
X& X::operator=(X&)
|
| 2189 |
-
```
|
| 2190 |
-
|
| 2191 |
-
A user-declared move assignment operator `X::operator=` is a non-static
|
| 2192 |
-
non-template member function of class `X` with exactly one parameter of
|
| 2193 |
-
type `X&&`, `const X&&`, `volatile X&&`, or `const volatile X&&`.
|
| 2194 |
-
|
| 2195 |
-
[*Note 4*: An overloaded assignment operator must be declared to have
|
| 2196 |
-
only one parameter; see [[over.ass]]. — *end note*]
|
| 2197 |
-
|
| 2198 |
-
[*Note 5*: More than one form of move assignment operator may be
|
| 2199 |
-
declared for a class. — *end note*]
|
| 2200 |
-
|
| 2201 |
-
If the definition of a class `X` does not explicitly declare a move
|
| 2202 |
-
assignment operator, one will be implicitly declared as defaulted if and
|
| 2203 |
-
only if
|
| 2204 |
-
|
| 2205 |
-
- `X` does not have a user-declared copy constructor,
|
| 2206 |
-
- `X` does not have a user-declared move constructor,
|
| 2207 |
-
- `X` does not have a user-declared copy assignment operator, and
|
| 2208 |
-
- `X` does not have a user-declared destructor.
|
| 2209 |
-
|
| 2210 |
-
[*Example 2*:
|
| 2211 |
-
|
| 2212 |
-
The class definition
|
| 2213 |
-
|
| 2214 |
-
``` cpp
|
| 2215 |
-
struct S {
|
| 2216 |
-
int a;
|
| 2217 |
-
S& operator=(const S&) = default;
|
| 2218 |
-
};
|
| 2219 |
-
```
|
| 2220 |
-
|
| 2221 |
-
will not have a default move assignment operator implicitly declared
|
| 2222 |
-
because the copy assignment operator has been user-declared. The move
|
| 2223 |
-
assignment operator may be explicitly defaulted.
|
| 2224 |
-
|
| 2225 |
-
``` cpp
|
| 2226 |
-
struct S {
|
| 2227 |
-
int a;
|
| 2228 |
-
S& operator=(const S&) = default;
|
| 2229 |
-
S& operator=(S&&) = default;
|
| 2230 |
-
};
|
| 2231 |
-
```
|
| 2232 |
-
|
| 2233 |
-
— *end example*]
|
| 2234 |
-
|
| 2235 |
-
The implicitly-declared move assignment operator for a class `X` will
|
| 2236 |
-
have the form
|
| 2237 |
-
|
| 2238 |
-
``` cpp
|
| 2239 |
-
X& X::operator=(X&&);
|
| 2240 |
-
```
|
| 2241 |
-
|
| 2242 |
-
The implicitly-declared copy/move assignment operator for class `X` has
|
| 2243 |
-
the return type `X&`; it returns the object for which the assignment
|
| 2244 |
-
operator is invoked, that is, the object assigned to. An
|
| 2245 |
-
implicitly-declared copy/move assignment operator is an `inline`
|
| 2246 |
-
`public` member of its class.
|
| 2247 |
-
|
| 2248 |
-
A defaulted copy/move assignment operator for class `X` is defined as
|
| 2249 |
-
deleted if `X` has:
|
| 2250 |
-
|
| 2251 |
-
- a variant member with a non-trivial corresponding assignment operator
|
| 2252 |
-
and `X` is a union-like class, or
|
| 2253 |
-
- a non-static data member of `const` non-class type (or array thereof),
|
| 2254 |
-
or
|
| 2255 |
-
- a non-static data member of reference type, or
|
| 2256 |
-
- a direct non-static data member of class type `M` (or array thereof)
|
| 2257 |
-
or a direct base class `M` that cannot be copied/moved because
|
| 2258 |
-
overload resolution ([[over.match]]), as applied to find `M`’s
|
| 2259 |
-
corresponding assignment operator, results in an ambiguity or a
|
| 2260 |
-
function that is deleted or inaccessible from the defaulted assignment
|
| 2261 |
-
operator.
|
| 2262 |
-
|
| 2263 |
-
A defaulted move assignment operator that is defined as deleted is
|
| 2264 |
-
ignored by overload resolution ([[over.match]], [[over.over]]).
|
| 2265 |
-
|
| 2266 |
-
Because a copy/move assignment operator is implicitly declared for a
|
| 2267 |
-
class if not declared by the user, a base class copy/move assignment
|
| 2268 |
-
operator is always hidden by the corresponding assignment operator of a
|
| 2269 |
-
derived class ([[over.ass]]). A *using-declaration* (
|
| 2270 |
-
[[namespace.udecl]]) that brings in from a base class an assignment
|
| 2271 |
-
operator with a parameter type that could be that of a copy/move
|
| 2272 |
-
assignment operator for the derived class is not considered an explicit
|
| 2273 |
-
declaration of such an operator and does not suppress the implicit
|
| 2274 |
-
declaration of the derived class operator; the operator introduced by
|
| 2275 |
-
the *using-declaration* is hidden by the implicitly-declared operator in
|
| 2276 |
-
the derived class.
|
| 2277 |
-
|
| 2278 |
-
A copy/move assignment operator for class `X` is trivial if it is not
|
| 2279 |
-
user-provided and if:
|
| 2280 |
-
|
| 2281 |
-
- class `X` has no virtual functions ([[class.virtual]]) and no virtual
|
| 2282 |
-
base classes ([[class.mi]]), and
|
| 2283 |
-
- the assignment operator selected to copy/move each direct base class
|
| 2284 |
-
subobject is trivial, and
|
| 2285 |
-
- for each non-static data member of `X` that is of class type (or array
|
| 2286 |
-
thereof), the assignment operator selected to copy/move that member is
|
| 2287 |
-
trivial;
|
| 2288 |
-
|
| 2289 |
-
otherwise the copy/move assignment operator is *non-trivial*.
|
| 2290 |
-
|
| 2291 |
-
A copy/move assignment operator for a class `X` that is defaulted and
|
| 2292 |
-
not defined as deleted is *implicitly defined* when it is odr-used (
|
| 2293 |
-
[[basic.def.odr]]) (e.g., when it is selected by overload resolution to
|
| 2294 |
-
assign to an object of its class type) or when it is explicitly
|
| 2295 |
-
defaulted after its first declaration. The implicitly-defined copy/move
|
| 2296 |
-
assignment operator is `constexpr` if
|
| 2297 |
-
|
| 2298 |
-
- `X` is a literal type, and
|
| 2299 |
-
- the assignment operator selected to copy/move each direct base class
|
| 2300 |
-
subobject is a constexpr function, and
|
| 2301 |
-
- for each non-static data member of `X` that is of class type (or array
|
| 2302 |
-
thereof), the assignment operator selected to copy/move that member is
|
| 2303 |
-
a constexpr function.
|
| 2304 |
-
|
| 2305 |
-
Before the defaulted copy/move assignment operator for a class is
|
| 2306 |
-
implicitly defined, all non-user-provided copy/move assignment operators
|
| 2307 |
-
for its direct base classes and its non-static data members shall have
|
| 2308 |
-
been implicitly defined.
|
| 2309 |
-
|
| 2310 |
-
[*Note 6*: An implicitly-declared copy/move assignment operator has an
|
| 2311 |
-
implied exception specification ([[except.spec]]). — *end note*]
|
| 2312 |
-
|
| 2313 |
-
The implicitly-defined copy/move assignment operator for a non-union
|
| 2314 |
-
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 2315 |
-
The direct base classes of `X` are assigned first, in the order of their
|
| 2316 |
-
declaration in the *base-specifier-list*, and then the immediate
|
| 2317 |
-
non-static data members of `X` are assigned, in the order in which they
|
| 2318 |
-
were declared in the class definition. Let `x` be either the parameter
|
| 2319 |
-
of the function or, for the move operator, an xvalue referring to the
|
| 2320 |
-
parameter. Each subobject is assigned in the manner appropriate to its
|
| 2321 |
-
type:
|
| 2322 |
-
|
| 2323 |
-
- if the subobject is of class type, as if by a call to `operator=` with
|
| 2324 |
-
the subobject as the object expression and the corresponding subobject
|
| 2325 |
-
of `x` as a single function argument (as if by explicit qualification;
|
| 2326 |
-
that is, ignoring any possible virtual overriding functions in more
|
| 2327 |
-
derived classes);
|
| 2328 |
-
- if the subobject is an array, each element is assigned, in the manner
|
| 2329 |
-
appropriate to the element type;
|
| 2330 |
-
- if the subobject is of scalar type, the built-in assignment operator
|
| 2331 |
-
is used.
|
| 2332 |
-
|
| 2333 |
-
It is unspecified whether subobjects representing virtual base classes
|
| 2334 |
-
are assigned more than once by the implicitly-defined copy/move
|
| 2335 |
-
assignment operator.
|
| 2336 |
-
|
| 2337 |
-
[*Example 3*:
|
| 2338 |
-
|
| 2339 |
-
``` cpp
|
| 2340 |
-
struct V { };
|
| 2341 |
-
struct A : virtual V { };
|
| 2342 |
-
struct B : virtual V { };
|
| 2343 |
-
struct C : B, A { };
|
| 2344 |
-
```
|
| 2345 |
-
|
| 2346 |
-
It is unspecified whether the virtual base class subobject `V` is
|
| 2347 |
-
assigned twice by the implicitly-defined copy/move assignment operator
|
| 2348 |
-
for `C`.
|
| 2349 |
-
|
| 2350 |
-
— *end example*]
|
| 2351 |
-
|
| 2352 |
-
The implicitly-defined copy assignment operator for a union `X` copies
|
| 2353 |
-
the object representation ([[basic.types]]) of `X`.
|
| 2354 |
-
|
| 2355 |
-
### Copy/move elision <a id="class.copy.elision">[[class.copy.elision]]</a>
|
| 2356 |
-
|
| 2357 |
-
When certain criteria are met, an implementation is allowed to omit the
|
| 2358 |
-
copy/move construction of a class object, even if the constructor
|
| 2359 |
-
selected for the copy/move operation and/or the destructor for the
|
| 2360 |
-
object have side effects. In such cases, the implementation treats the
|
| 2361 |
-
source and target of the omitted copy/move operation as simply two
|
| 2362 |
-
different ways of referring to the same object. If the first parameter
|
| 2363 |
-
of the selected constructor is an rvalue reference to the object’s type,
|
| 2364 |
-
the destruction of that object occurs when the target would have been
|
| 2365 |
-
destroyed; otherwise, the destruction occurs at the later of the times
|
| 2366 |
-
when the two objects would have been destroyed without the
|
| 2367 |
-
optimization.[^7] This elision of copy/move operations, called *copy
|
| 2368 |
-
elision*, is permitted in the following circumstances (which may be
|
| 2369 |
-
combined to eliminate multiple copies):
|
| 2370 |
-
|
| 2371 |
-
- in a `return` statement in a function with a class return type, when
|
| 2372 |
-
the *expression* is the name of a non-volatile automatic object (other
|
| 2373 |
-
than a function parameter or a variable introduced by the
|
| 2374 |
-
*exception-declaration* of a *handler* ([[except.handle]])) with the
|
| 2375 |
-
same type (ignoring cv-qualification) as the function return type, the
|
| 2376 |
-
copy/move operation can be omitted by constructing the automatic
|
| 2377 |
-
object directly into the function call’s return object
|
| 2378 |
-
- in a *throw-expression* ([[expr.throw]]), when the operand is the
|
| 2379 |
-
name of a non-volatile automatic object (other than a function or
|
| 2380 |
-
catch-clause parameter) whose scope does not extend beyond the end of
|
| 2381 |
-
the innermost enclosing *try-block* (if there is one), the copy/move
|
| 2382 |
-
operation from the operand to the exception object ([[except.throw]])
|
| 2383 |
-
can be omitted by constructing the automatic object directly into the
|
| 2384 |
-
exception object
|
| 2385 |
-
- when the *exception-declaration* of an exception handler (Clause
|
| 2386 |
-
[[except]]) declares an object of the same type (except for
|
| 2387 |
-
cv-qualification) as the exception object ([[except.throw]]), the
|
| 2388 |
-
copy operation can be omitted by treating the *exception-declaration*
|
| 2389 |
-
as an alias for the exception object if the meaning of the program
|
| 2390 |
-
will be unchanged except for the execution of constructors and
|
| 2391 |
-
destructors for the object declared by the *exception-declaration*.
|
| 2392 |
-
\[*Note 1*: There cannot be a move from the exception object because
|
| 2393 |
-
it is always an lvalue. — *end note*]
|
| 2394 |
-
|
| 2395 |
-
Copy elision is required where an expression is evaluated in a context
|
| 2396 |
-
requiring a constant expression ([[expr.const]]) and in constant
|
| 2397 |
-
initialization ([[basic.start.static]]).
|
| 2398 |
-
|
| 2399 |
-
[*Note 2*: Copy elision might not be performed if the same expression
|
| 2400 |
-
is evaluated in another context. — *end note*]
|
| 2401 |
-
|
| 2402 |
-
[*Example 1*:
|
| 2403 |
-
|
| 2404 |
-
``` cpp
|
| 2405 |
-
class Thing {
|
| 2406 |
-
public:
|
| 2407 |
-
Thing();
|
| 2408 |
-
~Thing();
|
| 2409 |
-
Thing(const Thing&);
|
| 2410 |
-
};
|
| 2411 |
-
|
| 2412 |
-
Thing f() {
|
| 2413 |
-
Thing t;
|
| 2414 |
-
return t;
|
| 2415 |
-
}
|
| 2416 |
-
|
| 2417 |
-
Thing t2 = f();
|
| 2418 |
-
|
| 2419 |
-
struct A {
|
| 2420 |
-
void *p;
|
| 2421 |
-
constexpr A(): p(this) {}
|
| 2422 |
-
};
|
| 2423 |
-
|
| 2424 |
-
constexpr A g() {
|
| 2425 |
-
A a;
|
| 2426 |
-
return a;
|
| 2427 |
-
}
|
| 2428 |
-
|
| 2429 |
-
constexpr A a; // well-formed, a.p points to a
|
| 2430 |
-
constexpr A b = g(); // well-formed, b.p points to b
|
| 2431 |
-
|
| 2432 |
-
void g() {
|
| 2433 |
-
A c = g(); // well-formed, c.p may point to c or to an ephemeral temporary
|
| 2434 |
-
}
|
| 2435 |
-
```
|
| 2436 |
-
|
| 2437 |
-
Here the criteria for elision can eliminate the copying of the local
|
| 2438 |
-
automatic object `t` into the result object for the function call `f()`,
|
| 2439 |
-
which is the global object `t2`. Effectively, the construction of the
|
| 2440 |
-
local object `t` can be viewed as directly initializing the global
|
| 2441 |
-
object `t2`, and that object’s destruction will occur at program exit.
|
| 2442 |
-
Adding a move constructor to `Thing` has the same effect, but it is the
|
| 2443 |
-
move construction from the local automatic object to `t2` that is
|
| 2444 |
-
elided.
|
| 2445 |
-
|
| 2446 |
-
— *end example*]
|
| 2447 |
-
|
| 2448 |
-
In the following copy-initialization contexts, a move operation might be
|
| 2449 |
-
used instead of a copy operation:
|
| 2450 |
-
|
| 2451 |
-
- If the *expression* in a `return` statement ([[stmt.return]]) is a
|
| 2452 |
-
(possibly parenthesized) *id-expression* that names an object with
|
| 2453 |
-
automatic storage duration declared in the body or
|
| 2454 |
-
*parameter-declaration-clause* of the innermost enclosing function or
|
| 2455 |
-
*lambda-expression*, or
|
| 2456 |
-
- if the operand of a *throw-expression* ([[expr.throw]]) is the name
|
| 2457 |
-
of a non-volatile automatic object (other than a function or
|
| 2458 |
-
catch-clause parameter) whose scope does not extend beyond the end of
|
| 2459 |
-
the innermost enclosing *try-block* (if there is one),
|
| 2460 |
-
|
| 2461 |
-
overload resolution to select the constructor for the copy is first
|
| 2462 |
-
performed as if the object were designated by an rvalue. If the first
|
| 2463 |
-
overload resolution fails or was not performed, or if the type of the
|
| 2464 |
-
first parameter of the selected constructor is not an rvalue reference
|
| 2465 |
-
to the object’s type (possibly cv-qualified), overload resolution is
|
| 2466 |
-
performed again, considering the object as an lvalue.
|
| 2467 |
-
|
| 2468 |
-
[*Note 3*: This two-stage overload resolution must be performed
|
| 2469 |
-
regardless of whether copy elision will occur. It determines the
|
| 2470 |
-
constructor to be called if elision is not performed, and the selected
|
| 2471 |
-
constructor must be accessible even if the call is
|
| 2472 |
-
elided. — *end note*]
|
| 2473 |
-
|
| 2474 |
-
[*Example 2*:
|
| 2475 |
-
|
| 2476 |
-
``` cpp
|
| 2477 |
-
class Thing {
|
| 2478 |
-
public:
|
| 2479 |
-
Thing();
|
| 2480 |
-
~Thing();
|
| 2481 |
-
Thing(Thing&&);
|
| 2482 |
-
private:
|
| 2483 |
-
Thing(const Thing&);
|
| 2484 |
-
};
|
| 2485 |
-
|
| 2486 |
-
Thing f(bool b) {
|
| 2487 |
-
Thing t;
|
| 2488 |
-
if (b)
|
| 2489 |
-
throw t; // OK: Thing(Thing&&) used (or elided) to throw t
|
| 2490 |
-
return t; // OK: Thing(Thing&&) used (or elided) to return t
|
| 2491 |
-
}
|
| 2492 |
-
|
| 2493 |
-
Thing t2 = f(false); // OK: no extra copy/move performed, t2 constructed by call to f
|
| 2494 |
-
|
| 2495 |
-
struct Weird {
|
| 2496 |
-
Weird();
|
| 2497 |
-
Weird(Weird&);
|
| 2498 |
-
};
|
| 2499 |
-
|
| 2500 |
-
Weird g() {
|
| 2501 |
-
Weird w;
|
| 2502 |
-
return w; // OK: first overload resolution fails, second overload resolution selects Weird(Weird&)
|
| 2503 |
-
}
|
| 2504 |
-
```
|
| 2505 |
-
|
| 2506 |
-
— *end example*]
|
| 2507 |
-
|
| 2508 |
-
<!-- Link reference definitions -->
|
| 2509 |
-
[basic.def.odr]: basic.md#basic.def.odr
|
| 2510 |
-
[basic.life]: basic.md#basic.life
|
| 2511 |
-
[basic.lookup]: basic.md#basic.lookup
|
| 2512 |
-
[basic.lval]: basic.md#basic.lval
|
| 2513 |
-
[basic.start.dynamic]: basic.md#basic.start.dynamic
|
| 2514 |
-
[basic.start.static]: basic.md#basic.start.static
|
| 2515 |
-
[basic.start.term]: basic.md#basic.start.term
|
| 2516 |
-
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 2517 |
-
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 2518 |
-
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 2519 |
-
[basic.stc.static]: basic.md#basic.stc.static
|
| 2520 |
-
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 2521 |
-
[basic.types]: basic.md#basic.types
|
| 2522 |
-
[class]: class.md#class
|
| 2523 |
-
[class.abstract]: class.md#class.abstract
|
| 2524 |
-
[class.access]: class.md#class.access
|
| 2525 |
-
[class.base.init]: #class.base.init
|
| 2526 |
-
[class.cdtor]: #class.cdtor
|
| 2527 |
-
[class.conv]: #class.conv
|
| 2528 |
-
[class.conv.ctor]: #class.conv.ctor
|
| 2529 |
-
[class.conv.fct]: #class.conv.fct
|
| 2530 |
-
[class.copy]: #class.copy
|
| 2531 |
-
[class.copy.assign]: #class.copy.assign
|
| 2532 |
-
[class.copy.ctor]: #class.copy.ctor
|
| 2533 |
-
[class.copy.elision]: #class.copy.elision
|
| 2534 |
-
[class.ctor]: #class.ctor
|
| 2535 |
-
[class.dtor]: #class.dtor
|
| 2536 |
-
[class.expl.init]: #class.expl.init
|
| 2537 |
-
[class.free]: #class.free
|
| 2538 |
-
[class.friend]: class.md#class.friend
|
| 2539 |
-
[class.inhctor.init]: #class.inhctor.init
|
| 2540 |
-
[class.init]: #class.init
|
| 2541 |
-
[class.mem]: class.md#class.mem
|
| 2542 |
-
[class.member.lookup]: class.md#class.member.lookup
|
| 2543 |
-
[class.mfct]: class.md#class.mfct
|
| 2544 |
-
[class.mi]: class.md#class.mi
|
| 2545 |
-
[class.qual]: basic.md#class.qual
|
| 2546 |
-
[class.temporary]: #class.temporary
|
| 2547 |
-
[class.union]: class.md#class.union
|
| 2548 |
-
[class.union.anon]: class.md#class.union.anon
|
| 2549 |
-
[class.virtual]: class.md#class.virtual
|
| 2550 |
-
[conv]: conv.md#conv
|
| 2551 |
-
[conv.array]: conv.md#conv.array
|
| 2552 |
-
[conv.rval]: conv.md#conv.rval
|
| 2553 |
-
[dcl.array]: dcl.md#dcl.array
|
| 2554 |
-
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 2555 |
-
[dcl.fct]: dcl.md#dcl.fct
|
| 2556 |
-
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 2557 |
-
[dcl.fct.def.delete]: dcl.md#dcl.fct.def.delete
|
| 2558 |
-
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 2559 |
-
[dcl.fct.spec]: dcl.md#dcl.fct.spec
|
| 2560 |
-
[dcl.init]: dcl.md#dcl.init
|
| 2561 |
-
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 2562 |
-
[dcl.init.list]: dcl.md#dcl.init.list
|
| 2563 |
-
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 2564 |
-
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 2565 |
-
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 2566 |
-
[diff.special]: compatibility.md#diff.special
|
| 2567 |
-
[except]: except.md#except
|
| 2568 |
-
[except.ctor]: except.md#except.ctor
|
| 2569 |
-
[except.handle]: except.md#except.handle
|
| 2570 |
-
[except.spec]: except.md#except.spec
|
| 2571 |
-
[except.throw]: except.md#except.throw
|
| 2572 |
-
[expr]: expr.md#expr
|
| 2573 |
-
[expr.ass]: expr.md#expr.ass
|
| 2574 |
-
[expr.call]: expr.md#expr.call
|
| 2575 |
-
[expr.cast]: expr.md#expr.cast
|
| 2576 |
-
[expr.const]: expr.md#expr.const
|
| 2577 |
-
[expr.const.cast]: expr.md#expr.const.cast
|
| 2578 |
-
[expr.delete]: expr.md#expr.delete
|
| 2579 |
-
[expr.dynamic.cast]: expr.md#expr.dynamic.cast
|
| 2580 |
-
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 2581 |
-
[expr.new]: expr.md#expr.new
|
| 2582 |
-
[expr.prim]: expr.md#expr.prim
|
| 2583 |
-
[expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
|
| 2584 |
-
[expr.pseudo]: expr.md#expr.pseudo
|
| 2585 |
-
[expr.ref]: expr.md#expr.ref
|
| 2586 |
-
[expr.sizeof]: expr.md#expr.sizeof
|
| 2587 |
-
[expr.static.cast]: expr.md#expr.static.cast
|
| 2588 |
-
[expr.sub]: expr.md#expr.sub
|
| 2589 |
-
[expr.throw]: expr.md#expr.throw
|
| 2590 |
-
[expr.type.conv]: expr.md#expr.type.conv
|
| 2591 |
-
[expr.typeid]: expr.md#expr.typeid
|
| 2592 |
-
[expr.unary.op]: expr.md#expr.unary.op
|
| 2593 |
-
[intro.execution]: intro.md#intro.execution
|
| 2594 |
-
[intro.object]: intro.md#intro.object
|
| 2595 |
-
[namespace.udecl]: dcl.md#namespace.udecl
|
| 2596 |
-
[over.ass]: over.md#over.ass
|
| 2597 |
-
[over.best.ics]: over.md#over.best.ics
|
| 2598 |
-
[over.ics.ref]: over.md#over.ics.ref
|
| 2599 |
-
[over.match]: over.md#over.match
|
| 2600 |
-
[over.match.best]: over.md#over.match.best
|
| 2601 |
-
[over.match.copy]: over.md#over.match.copy
|
| 2602 |
-
[over.over]: over.md#over.over
|
| 2603 |
-
[special]: #special
|
| 2604 |
-
[stmt.dcl]: stmt.md#stmt.dcl
|
| 2605 |
-
[stmt.return]: stmt.md#stmt.return
|
| 2606 |
-
[temp.dep.type]: temp.md#temp.dep.type
|
| 2607 |
-
[temp.variadic]: temp.md#temp.variadic
|
| 2608 |
-
|
| 2609 |
-
[^1]: The same rules apply to initialization of an `initializer_list`
|
| 2610 |
-
object ([[dcl.init.list]]) with its underlying temporary array.
|
| 2611 |
-
|
| 2612 |
-
[^2]: These conversions are considered as standard conversions for the
|
| 2613 |
-
purposes of overload resolution ([[over.best.ics]],
|
| 2614 |
-
[[over.ics.ref]]) and therefore initialization ([[dcl.init]]) and
|
| 2615 |
-
explicit casts ([[expr.static.cast]]). A conversion to `void` does
|
| 2616 |
-
not invoke any conversion function ([[expr.static.cast]]). Even
|
| 2617 |
-
though never directly called to perform a conversion, such
|
| 2618 |
-
conversion functions can be declared and can potentially be reached
|
| 2619 |
-
through a call to a virtual conversion function in a base class.
|
| 2620 |
-
|
| 2621 |
-
[^3]: A similar provision is not needed for the array version of
|
| 2622 |
-
`operator` `delete` because [[expr.delete]] requires that in this
|
| 2623 |
-
situation, the static type of the object to be deleted be the same
|
| 2624 |
-
as its dynamic type.
|
| 2625 |
-
|
| 2626 |
-
[^4]: This implies that the reference parameter of the
|
| 2627 |
-
implicitly-declared copy constructor cannot bind to a `volatile`
|
| 2628 |
-
lvalue; see [[diff.special]].
|
| 2629 |
-
|
| 2630 |
-
[^5]: Because a template assignment operator or an assignment operator
|
| 2631 |
-
taking an rvalue reference parameter is never a copy assignment
|
| 2632 |
-
operator, the presence of such an assignment operator does not
|
| 2633 |
-
suppress the implicit declaration of a copy assignment operator.
|
| 2634 |
-
Such assignment operators participate in overload resolution with
|
| 2635 |
-
other assignment operators, including copy assignment operators,
|
| 2636 |
-
and, if selected, will be used to assign an object.
|
| 2637 |
-
|
| 2638 |
-
[^6]: This implies that the reference parameter of the
|
| 2639 |
-
implicitly-declared copy assignment operator cannot bind to a
|
| 2640 |
-
`volatile` lvalue; see [[diff.special]].
|
| 2641 |
-
|
| 2642 |
-
[^7]: Because only one object is destroyed instead of two, and one
|
| 2643 |
-
copy/move constructor is not executed, there is still one object
|
| 2644 |
-
destroyed for each one constructed.
|
|
|
|
| 1 |
+
### Special member functions <a id="special">[[special]]</a>
|
| 2 |
|
| 3 |
+
Default constructors [[class.default.ctor]], copy constructors, move
|
| 4 |
+
constructors [[class.copy.ctor]], copy assignment operators, move
|
| 5 |
+
assignment operators [[class.copy.assign]], and prospective destructors
|
| 6 |
+
[[class.dtor]] are *special member functions*.
|
| 7 |
|
| 8 |
[*Note 1*: The implementation will implicitly declare these member
|
| 9 |
functions for some class types when the program does not explicitly
|
| 10 |
declare them. The implementation will implicitly define them if they are
|
| 11 |
+
odr-used [[basic.def.odr]] or needed for constant evaluation
|
| 12 |
+
[[expr.const]]. — *end note*]
|
| 13 |
|
| 14 |
An implicitly-declared special member function is declared at the
|
| 15 |
closing `}` of the *class-specifier*. Programs shall not define
|
| 16 |
implicitly-declared special member functions.
|
| 17 |
|
| 18 |
Programs may explicitly refer to implicitly-declared special member
|
| 19 |
functions.
|
| 20 |
|
| 21 |
[*Example 1*:
|
| 22 |
|
| 23 |
+
A program may explicitly call or form a pointer to member to an
|
| 24 |
+
implicitly-declared special member function.
|
| 25 |
|
| 26 |
``` cpp
|
| 27 |
struct A { }; // implicitly declared A::operator=
|
| 28 |
struct B : A {
|
| 29 |
B& operator=(const B &);
|
| 30 |
};
|
| 31 |
B& B::operator=(const B& s) {
|
| 32 |
+
this->A::operator=(s); // well-formed
|
| 33 |
return *this;
|
| 34 |
}
|
| 35 |
```
|
| 36 |
|
| 37 |
— *end example*]
|
|
|
|
| 39 |
[*Note 2*: The special member functions affect the way objects of class
|
| 40 |
type are created, copied, moved, and destroyed, and how values can be
|
| 41 |
converted to values of other types. Often such special member functions
|
| 42 |
are called implicitly. — *end note*]
|
| 43 |
|
| 44 |
+
Special member functions obey the usual access rules [[class.access]].
|
|
|
|
| 45 |
|
| 46 |
+
[*Example 2*: Declaring a constructor protected ensures that only
|
| 47 |
derived classes and friends can create objects using
|
| 48 |
it. — *end example*]
|
| 49 |
|
| 50 |
+
Two special member functions are of the same kind if:
|
| 51 |
+
|
| 52 |
+
- they are both default constructors,
|
| 53 |
+
- they are both copy or move constructors with the same first parameter
|
| 54 |
+
type, or
|
| 55 |
+
- they are both copy or move assignment operators with the same first
|
| 56 |
+
parameter type and the same *cv-qualifier*s and *ref-qualifier*, if
|
| 57 |
+
any.
|
| 58 |
+
|
| 59 |
+
An *eligible special member function* is a special member function for
|
| 60 |
+
which:
|
| 61 |
+
|
| 62 |
+
- the function is not deleted,
|
| 63 |
+
- the associated constraints [[temp.constr]], if any, are satisfied, and
|
| 64 |
+
- no special member function of the same kind is more constrained
|
| 65 |
+
[[temp.constr.order]].
|
| 66 |
+
|
| 67 |
For a class, its non-static data members, its non-virtual direct base
|
| 68 |
+
classes, and, if the class is not abstract [[class.abstract]], its
|
| 69 |
virtual base classes are called its *potentially constructed
|
| 70 |
subobjects*.
|
| 71 |
|
| 72 |
+
A defaulted special member function is *constexpr-compatible* if the
|
| 73 |
+
corresponding implicitly-declared special member function would be a
|
| 74 |
+
constexpr function.
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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