- tmp/tmp4cqx8qi5/{from.md → to.md} +990 -635
tmp/tmp4cqx8qi5/{from.md → to.md}
RENAMED
|
@@ -1,22 +1,29 @@
|
|
| 1 |
# Special member functions <a id="special">[[special]]</a>
|
| 2 |
|
| 3 |
The default constructor ([[class.ctor]]), copy constructor and copy
|
| 4 |
assignment operator ([[class.copy]]), move constructor and move
|
| 5 |
assignment operator ([[class.copy]]), and destructor ([[class.dtor]])
|
| 6 |
-
are *special member functions*.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
implicitly-declared special member functions.
|
| 14 |
|
| 15 |
Programs may explicitly refer to implicitly-declared special member
|
| 16 |
-
functions.
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
``` cpp
|
| 20 |
struct A { }; // implicitly declared A::operator=
|
| 21 |
struct B : A {
|
| 22 |
B& operator=(const B &);
|
|
@@ -25,31 +32,36 @@ B& B::operator=(const B& s) {
|
|
| 25 |
this->A::operator=(s); // well formed
|
| 26 |
return *this;
|
| 27 |
}
|
| 28 |
```
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
Special member functions obey the usual access rules (Clause
|
| 36 |
-
[[class.access]]).
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
For a class, its non-static data members, its non-virtual direct base
|
| 40 |
classes, and, if the class is not abstract ([[class.abstract]]), its
|
| 41 |
virtual base classes are called its *potentially constructed
|
| 42 |
subobjects*.
|
| 43 |
|
| 44 |
## Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 45 |
|
| 46 |
-
Constructors do not have names.
|
| 47 |
-
function declarator ([[dcl.fct]]) of the form
|
| 48 |
|
| 49 |
``` bnf
|
| 50 |
-
ptr-declarator '(' parameter-declaration-clause ')'
|
| 51 |
```
|
| 52 |
|
| 53 |
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 54 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 55 |
parentheses, and the *id-expression* has one of the following forms:
|
|
@@ -69,66 +81,78 @@ parentheses, and the *id-expression* has one of the following forms:
|
|
| 69 |
|
| 70 |
The *class-name* shall not be a *typedef-name*. In a constructor
|
| 71 |
declaration, each *decl-specifier* in the optional *decl-specifier-seq*
|
| 72 |
shall be `friend`, `inline`, `explicit`, or `constexpr`.
|
| 73 |
|
|
|
|
|
|
|
| 74 |
``` cpp
|
| 75 |
struct S {
|
| 76 |
S(); // declares the constructor
|
| 77 |
};
|
| 78 |
|
| 79 |
S::S() { } // defines the constructor
|
| 80 |
```
|
| 81 |
|
|
|
|
|
|
|
| 82 |
A constructor is used to initialize objects of its class type. Because
|
| 83 |
constructors do not have names, they are never found during name lookup;
|
| 84 |
however an explicit type conversion using the functional notation (
|
| 85 |
[[expr.type.conv]]) will cause a constructor to be called to initialize
|
| 86 |
-
an object.
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
| 89 |
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 90 |
`volatile` object. `const` and `volatile` semantics ([[dcl.type.cv]])
|
| 91 |
are not applied on an object under construction. They come into effect
|
| 92 |
when the constructor for the most derived object ([[intro.object]])
|
| 93 |
ends.
|
| 94 |
|
| 95 |
A *default* constructor for a class `X` is a constructor of class `X`
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
- any non-variant non-static data member of const-qualified type (or
|
| 108 |
array thereof) with no *brace-or-equal-initializer* does not have a
|
| 109 |
user-provided default constructor,
|
| 110 |
- `X` is a union and all of its variant members are of const-qualified
|
| 111 |
type (or array thereof),
|
| 112 |
- `X` is a non-union class and all members of any anonymous union member
|
| 113 |
are of const-qualified type (or array thereof),
|
| 114 |
- any potentially constructed subobject, except for a non-static data
|
| 115 |
member with a *brace-or-equal-initializer*, has class type `M` (or
|
| 116 |
array thereof) and either `M` has no default constructor or overload
|
| 117 |
-
resolution ([[over.match]]) as applied to `M`’s
|
| 118 |
-
results in an ambiguity or in a function that is deleted
|
| 119 |
-
inaccessible from the defaulted default constructor, or
|
| 120 |
- any potentially constructed subobject has a type with a destructor
|
| 121 |
that is deleted or inaccessible from the defaulted default
|
| 122 |
constructor.
|
| 123 |
|
| 124 |
-
A default constructor is trivial if it is not user-provided and if:
|
| 125 |
|
| 126 |
- its class has no virtual functions ([[class.virtual]]) and no virtual
|
| 127 |
base classes ([[class.mi]]), and
|
| 128 |
-
- no non-static data member of its class has a
|
| 129 |
-
|
| 130 |
- all the direct base classes of its class have trivial default
|
| 131 |
constructors, and
|
| 132 |
- for all the non-static data members of its class that are of class
|
| 133 |
type (or array thereof), each such class has a trivial default
|
| 134 |
constructor.
|
|
@@ -142,19 +166,21 @@ defaulted after its first declaration. The implicitly-defined default
|
|
| 142 |
constructor performs the set of initializations of the class that would
|
| 143 |
be performed by a user-written default constructor for that class with
|
| 144 |
no *ctor-initializer* ([[class.base.init]]) and an empty
|
| 145 |
*compound-statement*. If that user-written default constructor would be
|
| 146 |
ill-formed, the program is ill-formed. If that user-written default
|
| 147 |
-
constructor would satisfy the requirements of a
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
|
| 157 |
Default constructors are called implicitly to create class objects of
|
| 158 |
static, thread, or automatic storage duration ([[basic.stc.static]],
|
| 159 |
[[basic.stc.thread]], [[basic.stc.auto]]) defined without an
|
| 160 |
initializer ([[dcl.init]]), are called to create class objects of
|
|
@@ -163,37 +189,51 @@ dynamic storage duration ([[basic.stc.dynamic]]) created by a
|
|
| 163 |
[[expr.new]]), or are called when the explicit type conversion syntax (
|
| 164 |
[[expr.type.conv]]) is used. A program is ill-formed if the default
|
| 165 |
constructor for an object is implicitly used and the constructor is not
|
| 166 |
accessible (Clause [[class.access]]).
|
| 167 |
|
| 168 |
-
[[class.base.init]] describes the order in which
|
| 169 |
-
classes and non-static data members are called and
|
| 170 |
-
arguments can be specified for the calls to these
|
|
|
|
| 171 |
|
| 172 |
A `return` statement in the body of a constructor shall not specify a
|
| 173 |
return value. The address of a constructor shall not be taken.
|
| 174 |
|
| 175 |
A functional notation type conversion ([[expr.type.conv]]) can be used
|
| 176 |
-
to create new objects of its type.
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
``` cpp
|
| 180 |
complex zz = complex(1,2.3);
|
| 181 |
cprint( complex(7.8,1.2) );
|
| 182 |
```
|
| 183 |
|
| 184 |
-
|
| 185 |
-
the lifetime of temporary objects. Explicit constructor calls do not
|
| 186 |
-
yield lvalues, see [[basic.lval]].
|
| 187 |
|
| 188 |
-
|
| 189 |
-
construction; see [[class.base.init]] and [[class.cdtor]].
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
``` cpp
|
| 197 |
struct C;
|
| 198 |
void no_opt(C*);
|
| 199 |
|
|
@@ -208,29 +248,67 @@ void no_opt(C* cptr) {
|
|
| 208 |
int i = cobj.c * 100; // value of cobj.c is unspecified
|
| 209 |
cptr->c = 1;
|
| 210 |
cout << cobj.c * 100 // value of cobj.c is unspecified
|
| 211 |
<< '\n';
|
| 212 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
```
|
| 214 |
|
|
|
|
|
|
|
| 215 |
## Temporary objects <a id="class.temporary">[[class.temporary]]</a>
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
[[
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
[[except.throw]].
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
Consider the following code:
|
| 234 |
|
| 235 |
``` cpp
|
| 236 |
class X {
|
|
@@ -257,24 +335,34 @@ void h() {
|
|
| 257 |
Y c = g(Y(3));
|
| 258 |
a = f(a);
|
| 259 |
}
|
| 260 |
```
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
`
|
| 265 |
-
|
| 266 |
-
`
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
When an implementation introduces a temporary object of a class that has
|
| 278 |
a non-trivial constructor ([[class.ctor]], [[class.copy]]), it shall
|
| 279 |
ensure that a constructor is called for the temporary object. Similarly,
|
| 280 |
the destructor shall be called for a temporary with a non-trivial
|
|
@@ -284,42 +372,46 @@ last step in evaluating the full-expression ([[intro.execution]]) that
|
|
| 284 |
even if that evaluation ends in throwing an exception. The value
|
| 285 |
computations and side effects of destroying a temporary object are
|
| 286 |
associated only with the full-expression, not with any specific
|
| 287 |
subexpression.
|
| 288 |
|
| 289 |
-
There are
|
| 290 |
-
point than the end of the full-expression. The first context
|
| 291 |
-
default constructor is called to initialize an element of an
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
-
The
|
| 297 |
temporary to which the reference is bound or the temporary that is the
|
| 298 |
complete object of a subobject to which the reference is bound persists
|
| 299 |
for the lifetime of the reference except:
|
| 300 |
|
| 301 |
-
- A temporary bound to a reference
|
| 302 |
-
*ctor-initializer* ([[class.base.init]]) persists until the
|
| 303 |
-
constructor exits.
|
| 304 |
-
- A temporary bound to a reference parameter in a function call (
|
| 305 |
[[expr.call]]) persists until the completion of the full-expression
|
| 306 |
containing the call.
|
| 307 |
- The lifetime of a temporary bound to the returned value in a function
|
| 308 |
return statement ([[stmt.return]]) is not extended; the temporary is
|
| 309 |
destroyed at the end of the full-expression in the return statement.
|
| 310 |
- A temporary bound to a reference in a *new-initializer* (
|
| 311 |
[[expr.new]]) persists until the completion of the full-expression
|
| 312 |
containing the *new-initializer*.
|
|
|
|
| 313 |
``` cpp
|
| 314 |
struct S { int mi; const std::pair<int,int>& mp; };
|
| 315 |
S a { 1, {2,3} };
|
| 316 |
S* p = new S{ 1, {2,3} }; // Creates dangling reference
|
| 317 |
```
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
|
|
|
|
|
|
| 321 |
|
| 322 |
The destruction of a temporary whose lifetime is not extended by being
|
| 323 |
bound to a reference is sequenced before the destruction of every
|
| 324 |
temporary which is constructed earlier in the same full-expression. If
|
| 325 |
the lifetime of two or more temporaries to which references are bound
|
|
@@ -333,10 +425,12 @@ storage duration as the temporary and created before the temporary is
|
|
| 333 |
created the temporary shall be destroyed before `obj1` is destroyed; if
|
| 334 |
`obj2` is an object with the same storage duration as the temporary and
|
| 335 |
created after the temporary is created the temporary shall be destroyed
|
| 336 |
after `obj2` is destroyed.
|
| 337 |
|
|
|
|
|
|
|
| 338 |
``` cpp
|
| 339 |
struct S {
|
| 340 |
S();
|
| 341 |
S(int);
|
| 342 |
friend S operator+(const S&, const S&);
|
|
@@ -363,10 +457,12 @@ order in which `T3` is destroyed takes into account the destruction
|
|
| 363 |
order of other objects with static storage duration. That is, because
|
| 364 |
`obj1` is constructed before `T3`, and `T3` is constructed before
|
| 365 |
`obj2`, `obj2` shall be destroyed before `T3`, and `T3` shall be
|
| 366 |
destroyed before `obj1`.
|
| 367 |
|
|
|
|
|
|
|
| 368 |
## Conversions <a id="class.conv">[[class.conv]]</a>
|
| 369 |
|
| 370 |
Type conversions of class objects can be specified by constructors and
|
| 371 |
by conversion functions. These conversions are called *user-defined
|
| 372 |
conversions* and are used for implicit type conversions (Clause
|
|
@@ -376,58 +472,67 @@ conversions ([[expr.cast]], [[expr.static.cast]]).
|
|
| 376 |
User-defined conversions are applied only where they are unambiguous (
|
| 377 |
[[class.member.lookup]], [[class.conv.fct]]). Conversions obey the
|
| 378 |
access control rules (Clause [[class.access]]). Access control is
|
| 379 |
applied after ambiguity resolution ([[basic.lookup]]).
|
| 380 |
|
| 381 |
-
See [[over.match]] for a discussion of the use of
|
| 382 |
-
function calls as well as examples below.
|
| 383 |
|
| 384 |
At most one user-defined conversion (constructor or conversion function)
|
| 385 |
is implicitly applied to a single value.
|
| 386 |
|
|
|
|
|
|
|
| 387 |
``` cpp
|
| 388 |
struct X {
|
| 389 |
operator int();
|
| 390 |
};
|
| 391 |
|
| 392 |
struct Y {
|
| 393 |
operator X();
|
| 394 |
};
|
| 395 |
|
| 396 |
Y a;
|
| 397 |
-
int b = a; // error
|
| 398 |
-
// a.operator X().operator int() not tried
|
| 399 |
int c = X(a); // OK: a.operator X().operator int()
|
| 400 |
```
|
| 401 |
|
|
|
|
|
|
|
| 402 |
User-defined conversions are used implicitly only if they are
|
| 403 |
unambiguous. A conversion function in a derived class does not hide a
|
| 404 |
conversion function in a base class unless the two functions convert to
|
| 405 |
the same type. Function overload resolution ([[over.match.best]])
|
| 406 |
selects the best conversion function to perform the conversion.
|
| 407 |
|
|
|
|
|
|
|
| 408 |
``` cpp
|
| 409 |
struct X {
|
| 410 |
operator int();
|
| 411 |
};
|
| 412 |
|
| 413 |
struct Y : X {
|
| 414 |
operator char();
|
| 415 |
};
|
| 416 |
|
| 417 |
void f(Y& a) {
|
| 418 |
-
if (a) { // ill-formed:
|
| 419 |
-
// X::operator int() or Y::operator char()
|
| 420 |
}
|
| 421 |
}
|
| 422 |
```
|
| 423 |
|
|
|
|
|
|
|
| 424 |
### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 425 |
|
| 426 |
A constructor declared without the *function-specifier* `explicit`
|
| 427 |
-
specifies a conversion from the types of its parameters
|
| 428 |
-
its class. Such a constructor is called a *converting
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
``` cpp
|
| 431 |
struct X {
|
| 432 |
X(int);
|
| 433 |
X(const char*, int =0);
|
|
@@ -441,37 +546,52 @@ void f(X arg) {
|
|
| 441 |
f(3); // f(X(3))
|
| 442 |
f({1, 2}); // f(X(1,2))
|
| 443 |
}
|
| 444 |
```
|
| 445 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
An explicit constructor constructs objects just like non-explicit
|
| 447 |
constructors, but does so only where the direct-initialization syntax (
|
| 448 |
[[dcl.init]]) or where casts ([[expr.static.cast]], [[expr.cast]]) are
|
| 449 |
-
explicitly used. A default constructor
|
| 450 |
-
such a constructor will be used to
|
| 451 |
-
value-initialization ([[dcl.init]]).
|
|
|
|
|
|
|
| 452 |
|
| 453 |
``` cpp
|
| 454 |
struct Z {
|
| 455 |
explicit Z();
|
| 456 |
explicit Z(int);
|
| 457 |
explicit Z(int, int);
|
| 458 |
};
|
| 459 |
|
| 460 |
Z a; // OK: default-initialization performed
|
|
|
|
|
|
|
| 461 |
Z a1 = 1; // error: no implicit conversion
|
| 462 |
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 463 |
Z a2(1); // OK: direct initialization syntax used
|
| 464 |
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 465 |
Z a4 = (Z)1; // OK: explicit cast used
|
| 466 |
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
| 467 |
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 468 |
```
|
| 469 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 471 |
-
constructor.
|
| 472 |
-
|
|
|
|
|
|
|
|
|
|
| 473 |
|
| 474 |
### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 475 |
|
| 476 |
A member function of a class `X` having no parameters with a name of the
|
| 477 |
form
|
|
@@ -490,22 +610,26 @@ conversion-type-id:
|
|
| 490 |
conversion-declarator:
|
| 491 |
ptr-operator conversion-declaratorₒₚₜ
|
| 492 |
```
|
| 493 |
|
| 494 |
specifies a conversion from `X` to the type specified by the
|
| 495 |
-
*conversion-type-id*. Such functions are called conversion functions.
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
reference to it), or to
|
|
|
|
|
|
|
|
|
|
| 503 |
|
| 504 |
``` cpp
|
| 505 |
struct X {
|
| 506 |
operator int();
|
|
|
|
| 507 |
};
|
| 508 |
|
| 509 |
void f(X a) {
|
| 510 |
int i = int(a);
|
| 511 |
i = (int)a;
|
|
@@ -514,16 +638,20 @@ void f(X a) {
|
|
| 514 |
```
|
| 515 |
|
| 516 |
In all three cases the value assigned will be converted by
|
| 517 |
`X::operator int()`.
|
| 518 |
|
|
|
|
|
|
|
| 519 |
A conversion function may be explicit ([[dcl.fct.spec]]), in which case
|
| 520 |
it is only considered as a user-defined conversion for
|
| 521 |
direct-initialization ([[dcl.init]]). Otherwise, user-defined
|
| 522 |
conversions are not restricted to use in assignments and
|
| 523 |
initializations.
|
| 524 |
|
|
|
|
|
|
|
| 525 |
``` cpp
|
| 526 |
class Y { };
|
| 527 |
struct Z {
|
| 528 |
explicit operator Y() const;
|
| 529 |
};
|
|
@@ -540,37 +668,72 @@ void g(X a, X b) {
|
|
| 540 |
if (a) {
|
| 541 |
}
|
| 542 |
}
|
| 543 |
```
|
| 544 |
|
|
|
|
|
|
|
| 545 |
The *conversion-type-id* shall not represent a function type nor an
|
| 546 |
array type. The *conversion-type-id* in a *conversion-function-id* is
|
| 547 |
-
the longest
|
| 548 |
-
|
| 549 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
|
| 551 |
``` cpp
|
| 552 |
&ac.operator int*i; // syntax error:
|
| 553 |
// parsed as: &(ac.operator int *)i
|
| 554 |
// not as: &(ac.operator int)*i
|
| 555 |
```
|
| 556 |
|
| 557 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 558 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
Conversion functions are inherited.
|
| 560 |
|
| 561 |
Conversion functions can be virtual.
|
| 562 |
|
| 563 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
|
| 565 |
## Destructors <a id="class.dtor">[[class.dtor]]</a>
|
| 566 |
|
| 567 |
-
|
| 568 |
-
of the form
|
| 569 |
|
| 570 |
``` bnf
|
| 571 |
-
ptr-declarator '(' parameter-declaration-clause ')'
|
| 572 |
```
|
| 573 |
|
| 574 |
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 575 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 576 |
parentheses, and the *id-expression* has one of the following forms:
|
|
@@ -588,24 +751,24 @@ parentheses, and the *id-expression* has one of the following forms:
|
|
| 588 |
- in a declaration at namespace scope or in a friend declaration, the
|
| 589 |
*id-expression* is *nested-name-specifier* `~`*class-name* and the
|
| 590 |
*class-name* names the same class as the *nested-name-specifier*.
|
| 591 |
|
| 592 |
The *class-name* shall not be a *typedef-name*. A destructor shall take
|
| 593 |
-
no arguments ([[dcl.fct]]).
|
| 594 |
-
*decl-specifier* of
|
| 595 |
-
`inline`, or `virtual`.
|
| 596 |
|
| 597 |
A destructor is used to destroy objects of its class type. The address
|
| 598 |
of a destructor shall not be taken. A destructor can be invoked for a
|
| 599 |
`const`, `volatile` or `const` `volatile` object. `const` and `volatile`
|
| 600 |
semantics ([[dcl.type.cv]]) are not applied on an object under
|
| 601 |
destruction. They stop being in effect when the destructor for the most
|
| 602 |
derived object ([[intro.object]]) starts.
|
| 603 |
|
| 604 |
-
A declaration of a destructor that does not have
|
| 605 |
-
*
|
| 606 |
-
|
| 607 |
|
| 608 |
If a class has no user-declared destructor, a destructor is implicitly
|
| 609 |
declared as defaulted ([[dcl.fct.def]]). An implicitly-declared
|
| 610 |
destructor is an `inline` `public` member of its class.
|
| 611 |
|
|
@@ -629,41 +792,41 @@ A destructor is trivial if it is not user-provided and if:
|
|
| 629 |
type (or array thereof), each such class has a trivial destructor.
|
| 630 |
|
| 631 |
Otherwise, the destructor is *non-trivial*.
|
| 632 |
|
| 633 |
A destructor that is defaulted and not defined as deleted is *implicitly
|
| 634 |
-
defined* when it is odr-used ([[basic.def.odr]])
|
| 635 |
-
|
| 636 |
-
after its first declaration.
|
| 637 |
|
| 638 |
Before the defaulted destructor for a class is implicitly defined, all
|
| 639 |
the non-user-provided destructors for its base classes and its
|
| 640 |
non-static data members shall have been implicitly defined.
|
| 641 |
|
| 642 |
After executing the body of the destructor and destroying any automatic
|
| 643 |
objects allocated within the body, a destructor for class `X` calls the
|
| 644 |
destructors for `X`’s direct non-variant non-static data members, the
|
| 645 |
-
destructors for `X`’s direct base classes and, if `X` is the
|
| 646 |
-
most derived class ([[class.base.init]]), its destructor
|
| 647 |
-
destructors for `X`’s virtual base classes. All destructors
|
| 648 |
-
as if they were referenced with a qualified name, that is,
|
| 649 |
-
possible virtual overriding destructors in more derived
|
| 650 |
-
and members are destroyed in the reverse order of the
|
| 651 |
-
their constructor (see [[class.base.init]]). A `return`
|
| 652 |
-
[[stmt.return]]) in a destructor might not directly return
|
| 653 |
-
caller; before transferring control to the caller, the
|
| 654 |
-
the members and bases are called. Destructors for
|
| 655 |
-
are called in reverse order of their construction
|
|
|
|
| 656 |
|
| 657 |
A destructor can be declared `virtual` ([[class.virtual]]) or pure
|
| 658 |
`virtual` ([[class.abstract]]); if any objects of that class or any
|
| 659 |
derived class are created in the program, the destructor shall be
|
| 660 |
defined. If a class has a base class with a virtual destructor, its
|
| 661 |
destructor (whether user- or implicitly-declared) is virtual.
|
| 662 |
|
| 663 |
-
|
| 664 |
-
destruction; see [[class.cdtor]].
|
| 665 |
|
| 666 |
A destructor is invoked implicitly
|
| 667 |
|
| 668 |
- for a constructed object with static storage duration (
|
| 669 |
[[basic.stc.static]]) at program termination ([[basic.start.term]]),
|
|
@@ -671,44 +834,50 @@ A destructor is invoked implicitly
|
|
| 671 |
[[basic.stc.thread]]) at thread exit,
|
| 672 |
- for a constructed object with automatic storage duration (
|
| 673 |
[[basic.stc.auto]]) when the block in which an object is created
|
| 674 |
exits ([[stmt.dcl]]),
|
| 675 |
- for a constructed temporary object when its lifetime ends (
|
| 676 |
-
[[class.temporary]]).
|
| 677 |
|
| 678 |
In each case, the context of the invocation is the context of the
|
| 679 |
construction of the object. A destructor is also invoked implicitly
|
| 680 |
through use of a *delete-expression* ([[expr.delete]]) for a
|
| 681 |
constructed object allocated by a *new-expression* ([[expr.new]]); the
|
| 682 |
-
context of the invocation is the *delete-expression*.
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
|
|
|
|
|
|
|
|
|
| 689 |
|
| 690 |
At the point of definition of a virtual destructor (including an
|
| 691 |
implicit definition ([[class.copy]])), the non-array deallocation
|
| 692 |
-
function is
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
deallocation function or a function with a deleted definition (
|
| 697 |
-
[[dcl.fct.def]]), the program is ill-formed. This assures that a
|
| 698 |
-
deallocation function corresponding to the dynamic type of an object is
|
| 699 |
-
available for the *delete-expression* ([[class.free]]).
|
| 700 |
|
| 701 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
followed by a *type-name* or *decltype-specifier* that denotes the
|
| 703 |
destructor’s class type. The invocation of a destructor is subject to
|
| 704 |
the usual rules for member functions ([[class.mfct]]); that is, if the
|
| 705 |
object is not of the destructor’s class type and not of a class derived
|
| 706 |
from the destructor’s class type (including when the destructor is
|
| 707 |
invoked via a null pointer value), the program has undefined behavior.
|
| 708 |
-
|
| 709 |
-
[
|
|
|
|
|
|
|
|
|
|
| 710 |
|
| 711 |
``` cpp
|
| 712 |
struct B {
|
| 713 |
virtual ~B() { }
|
| 714 |
};
|
|
@@ -727,20 +896,25 @@ void f() {
|
|
| 727 |
B_ptr->B_alias::~B(); // calls B's destructor
|
| 728 |
B_ptr->B_alias::~B_alias(); // calls B's destructor
|
| 729 |
}
|
| 730 |
```
|
| 731 |
|
| 732 |
-
|
| 733 |
-
operator ([[expr.ref]]) or a qualified-id ([[expr.prim]]); in
|
| 734 |
-
particular, the *unary-expression* `~X()` in a member function is not an
|
| 735 |
-
explicit destructor call ([[expr.unary.op]]).
|
| 736 |
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 742 |
|
| 743 |
``` cpp
|
| 744 |
void* operator new(std::size_t, void* p) { return p; }
|
| 745 |
struct X {
|
| 746 |
X(int);
|
|
@@ -754,33 +928,43 @@ void g() { // rare, specialized use:
|
|
| 754 |
f(p);
|
| 755 |
p->X::~X(); // cleanup
|
| 756 |
}
|
| 757 |
```
|
| 758 |
|
|
|
|
|
|
|
| 759 |
Once a destructor is invoked for an object, the object no longer exists;
|
| 760 |
the behavior is undefined if the destructor is invoked for an object
|
| 761 |
-
whose lifetime has ended ([[basic.life]]).
|
| 762 |
-
automatic object is explicitly invoked, and the block is subsequently
|
| 763 |
-
left in a manner that would ordinarily invoke implicit destruction of
|
| 764 |
-
the object, the behavior is undefined.
|
| 765 |
|
| 766 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
scalar type name ([[expr.pseudo]]). Allowing this makes it possible to
|
| 768 |
write code without having to know if a destructor exists for a given
|
| 769 |
-
type. For example
|
| 770 |
|
| 771 |
``` cpp
|
| 772 |
typedef int I;
|
| 773 |
I* p;
|
| 774 |
p->I::~I();
|
| 775 |
```
|
| 776 |
|
|
|
|
|
|
|
| 777 |
## Free store <a id="class.free">[[class.free]]</a>
|
| 778 |
|
| 779 |
Any allocation function for a class `T` is a static member (even if not
|
| 780 |
explicitly declared `static`).
|
| 781 |
|
|
|
|
|
|
|
| 782 |
``` cpp
|
| 783 |
class Arena;
|
| 784 |
struct B {
|
| 785 |
void* operator new(std::size_t, Arena*);
|
| 786 |
};
|
|
@@ -793,12 +977,14 @@ void foo(int i) {
|
|
| 793 |
new D1[i]; // calls ::operator new[](std::size_t)
|
| 794 |
new D1; // ill-formed: ::operator new(std::size_t) hidden
|
| 795 |
}
|
| 796 |
```
|
| 797 |
|
|
|
|
|
|
|
| 798 |
When an object is deleted with a *delete-expression* ([[expr.delete]]),
|
| 799 |
-
a
|
| 800 |
`operator delete[]()` for arrays) is (implicitly) called to reclaim the
|
| 801 |
storage occupied by the object ([[basic.stc.dynamic.deallocation]]).
|
| 802 |
|
| 803 |
Class-specific deallocation function lookup is a part of general
|
| 804 |
deallocation function lookup ([[expr.delete]]) and occurs as follows.
|
|
@@ -815,10 +1001,12 @@ inaccessible, or if the lookup selects a placement deallocation
|
|
| 815 |
function, the program is ill-formed.
|
| 816 |
|
| 817 |
Any deallocation function for a class `X` is a static member (even if
|
| 818 |
not explicitly declared `static`).
|
| 819 |
|
|
|
|
|
|
|
| 820 |
``` cpp
|
| 821 |
class X {
|
| 822 |
void operator delete(void*);
|
| 823 |
void operator delete[](void*, std::size_t);
|
| 824 |
};
|
|
@@ -827,16 +1015,22 @@ class Y {
|
|
| 827 |
void operator delete(void*, std::size_t);
|
| 828 |
void operator delete[](void*);
|
| 829 |
};
|
| 830 |
```
|
| 831 |
|
|
|
|
|
|
|
| 832 |
Since member allocation and deallocation functions are `static` they
|
| 833 |
-
cannot be virtual.
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 838 |
|
| 839 |
``` cpp
|
| 840 |
struct B {
|
| 841 |
virtual ~B();
|
| 842 |
void operator delete(void*, std::size_t);
|
|
@@ -851,14 +1045,19 @@ void f() {
|
|
| 851 |
delete bp; // 1: uses D::operator delete(void*)
|
| 852 |
}
|
| 853 |
```
|
| 854 |
|
| 855 |
Here, storage for the non-array object of class `D` is deallocated by
|
| 856 |
-
`D::operator delete()`, due to the virtual destructor.
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
|
| 861 |
``` cpp
|
| 862 |
struct B {
|
| 863 |
virtual ~B();
|
| 864 |
void operator delete[](void*, std::size_t);
|
|
@@ -874,19 +1073,23 @@ void f(int i) {
|
|
| 874 |
B* bp = new D[i];
|
| 875 |
delete[] bp; // undefined behavior
|
| 876 |
}
|
| 877 |
```
|
| 878 |
|
|
|
|
|
|
|
| 879 |
Access to the deallocation function is checked statically. Hence, even
|
| 880 |
though a different one might actually be executed, the statically
|
| 881 |
-
visible deallocation function is required to be accessible.
|
| 882 |
-
on line //1 above, if `B::operator delete()` had been `private`, the
|
| 883 |
-
delete expression would have been ill-formed.
|
| 884 |
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 888 |
|
| 889 |
## Initialization <a id="class.init">[[class.init]]</a>
|
| 890 |
|
| 891 |
When no initializer is specified for an object of (possibly
|
| 892 |
cv-qualified) class type (or array thereof), or the initializer has the
|
|
@@ -896,12 +1099,14 @@ An object of class type (or array thereof) can be explicitly
|
|
| 896 |
initialized; see [[class.expl.init]] and [[class.base.init]].
|
| 897 |
|
| 898 |
When an array of class objects is initialized (either explicitly or
|
| 899 |
implicitly) and the elements are initialized by constructor, the
|
| 900 |
constructor shall be called for each element of the array, following the
|
| 901 |
-
subscript order; see [[dcl.array]].
|
| 902 |
-
|
|
|
|
|
|
|
| 903 |
|
| 904 |
### Explicit initialization <a id="class.expl.init">[[class.expl.init]]</a>
|
| 905 |
|
| 906 |
An object of class type can be initialized with a parenthesized
|
| 907 |
*expression-list*, where the *expression-list* is construed as an
|
|
@@ -909,43 +1114,42 @@ argument list for a constructor that is called to initialize the object.
|
|
| 909 |
Alternatively, a single *assignment-expression* can be specified as an
|
| 910 |
*initializer* using the `=` form of initialization. Either
|
| 911 |
direct-initialization semantics or copy-initialization semantics apply;
|
| 912 |
see [[dcl.init]].
|
| 913 |
|
|
|
|
|
|
|
| 914 |
``` cpp
|
| 915 |
struct complex {
|
| 916 |
complex();
|
| 917 |
complex(double);
|
| 918 |
complex(double,double);
|
| 919 |
};
|
| 920 |
|
| 921 |
complex sqrt(complex,complex);
|
| 922 |
|
| 923 |
-
complex a(1); // initialize by a call of
|
| 924 |
-
// complex(double)
|
| 925 |
complex b = a; // initialize by a copy of a
|
| 926 |
-
complex c = complex(1,2); // construct complex(1,2)
|
| 927 |
-
// using complex(double,double)
|
| 928 |
// copy/move it into c
|
| 929 |
-
complex d = sqrt(b,c); // call sqrt(complex,complex)
|
| 930 |
-
|
| 931 |
-
complex
|
| 932 |
-
|
| 933 |
-
complex f = 3; // construct complex(3) using
|
| 934 |
-
// complex(double)
|
| 935 |
-
// copy/move it into f
|
| 936 |
-
complex g = { 1, 2 }; // initialize by a call of
|
| 937 |
-
// complex(double, double)
|
| 938 |
```
|
| 939 |
|
| 940 |
-
|
| 941 |
-
|
|
|
|
|
|
|
| 942 |
|
| 943 |
An object of class type can also be initialized by a *braced-init-list*.
|
| 944 |
List-initialization semantics apply; see [[dcl.init]] and
|
| 945 |
[[dcl.init.list]].
|
| 946 |
|
|
|
|
|
|
|
| 947 |
``` cpp
|
| 948 |
complex v[6] = { 1, complex(1,2), complex(), 2 };
|
| 949 |
```
|
| 950 |
|
| 951 |
Here, `complex::complex(double)` is called for the initialization of
|
|
@@ -961,37 +1165,41 @@ struct X {
|
|
| 961 |
} x = { 99, 88.8, 77.7 };
|
| 962 |
```
|
| 963 |
|
| 964 |
Here, `x.i` is initialized with 99, `x.f` is initialized with 88.8, and
|
| 965 |
`complex::complex(double)` is called for the initialization of `x.c`.
|
| 966 |
-
Braces can be elided in the *initializer-list* for any aggregate, even
|
| 967 |
-
if the aggregate has members of a class type with user-defined type
|
| 968 |
-
conversions; see [[dcl.init.aggr]].
|
| 969 |
|
| 970 |
-
|
| 971 |
-
an object of type `T` (or array thereof) is ill-formed if no
|
| 972 |
-
*initializer* is explicitly specified (see [[class.init]] and
|
| 973 |
-
[[dcl.init]]).
|
| 974 |
|
| 975 |
-
|
| 976 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 977 |
|
| 978 |
### Initializing bases and members <a id="class.base.init">[[class.base.init]]</a>
|
| 979 |
|
| 980 |
In the definition of a constructor for a class, initializers for direct
|
| 981 |
-
and virtual base subobjects and non-static data members can be
|
| 982 |
-
by a *ctor-initializer*, which has the form
|
| 983 |
|
| 984 |
``` bnf
|
| 985 |
ctor-initializer:
|
| 986 |
':' mem-initializer-list
|
| 987 |
```
|
| 988 |
|
| 989 |
``` bnf
|
| 990 |
mem-initializer-list:
|
| 991 |
mem-initializer '...'ₒₚₜ
|
| 992 |
-
mem-initializer '...'ₒₚₜ
|
| 993 |
```
|
| 994 |
|
| 995 |
``` bnf
|
| 996 |
mem-initializer:
|
| 997 |
mem-initializer-id '(' expression-listₒₚₜ ')'
|
|
@@ -1005,123 +1213,163 @@ mem-initializer-id:
|
|
| 1005 |
```
|
| 1006 |
|
| 1007 |
In a *mem-initializer-id* an initial unqualified *identifier* is looked
|
| 1008 |
up in the scope of the constructor’s class and, if not found in that
|
| 1009 |
scope, it is looked up in the scope containing the constructor’s
|
| 1010 |
-
definition.
|
|
|
|
|
|
|
| 1011 |
name as a direct or virtual base class of the class, a
|
| 1012 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 1013 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 1014 |
-
the hidden base class may be specified using a qualified
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
|
|
|
|
|
|
| 1018 |
|
| 1019 |
A *mem-initializer-list* can initialize a base class using any
|
| 1020 |
*class-or-decltype* that denotes that base class type.
|
| 1021 |
|
|
|
|
|
|
|
| 1022 |
``` cpp
|
| 1023 |
struct A { A(); };
|
| 1024 |
typedef A global_A;
|
| 1025 |
struct B { };
|
| 1026 |
struct C: public A, public B { C(); };
|
| 1027 |
C::C(): global_A() { } // mem-initializer for base A
|
| 1028 |
```
|
| 1029 |
|
|
|
|
|
|
|
| 1030 |
If a *mem-initializer-id* is ambiguous because it designates both a
|
| 1031 |
direct non-virtual base class and an inherited virtual base class, the
|
| 1032 |
*mem-initializer* is ill-formed.
|
| 1033 |
|
|
|
|
|
|
|
| 1034 |
``` cpp
|
| 1035 |
struct A { A(); };
|
| 1036 |
struct B: public virtual A { };
|
| 1037 |
struct C: public A, public B { C(); };
|
| 1038 |
C::C(): A() { } // ill-formed: which A?
|
| 1039 |
```
|
| 1040 |
|
|
|
|
|
|
|
| 1041 |
A *ctor-initializer* may initialize a variant member of the
|
| 1042 |
constructor’s class. If a *ctor-initializer* specifies more than one
|
| 1043 |
*mem-initializer* for the same member or for the same base class, the
|
| 1044 |
*ctor-initializer* is ill-formed.
|
| 1045 |
|
| 1046 |
A *mem-initializer-list* can delegate to another constructor of the
|
| 1047 |
constructor’s class using any *class-or-decltype* that denotes the
|
| 1048 |
-
constructor’s class itself. If a designates the
|
| 1049 |
-
shall be the only
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
|
| 1058 |
|
| 1059 |
``` cpp
|
| 1060 |
struct C {
|
| 1061 |
C( int ) { } // #1: non-delegating constructor
|
| 1062 |
C(): C(42) { } // #2: delegates to #1
|
| 1063 |
C( char c ) : C(42.0) { } // #3: ill-formed due to recursion with #4
|
| 1064 |
C( double d ) : C('a') { } // #4: ill-formed due to recursion with #3
|
| 1065 |
};
|
| 1066 |
```
|
| 1067 |
|
|
|
|
|
|
|
| 1068 |
The *expression-list* or *braced-init-list* in a *mem-initializer* is
|
| 1069 |
used to initialize the designated subobject (or, in the case of a
|
| 1070 |
delegating constructor, the complete class object) according to the
|
| 1071 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 1072 |
|
|
|
|
|
|
|
| 1073 |
``` cpp
|
| 1074 |
-
struct B1 { B1(int);
|
| 1075 |
-
struct B2 { B2(int);
|
| 1076 |
struct D : B1, B2 {
|
| 1077 |
D(int);
|
| 1078 |
B1 b;
|
| 1079 |
const int c;
|
| 1080 |
};
|
| 1081 |
|
| 1082 |
-
D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4)
|
| 1083 |
-
{ /* ... */ }
|
| 1084 |
D d(10);
|
| 1085 |
```
|
| 1086 |
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
| 1090 |
-
|
| 1091 |
-
|
| 1092 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1093 |
|
| 1094 |
In a non-delegating constructor, if a given potentially constructed
|
| 1095 |
subobject is not designated by a *mem-initializer-id* (including the
|
| 1096 |
case where there is no *mem-initializer-list* because the constructor
|
| 1097 |
has no *ctor-initializer*), then
|
| 1098 |
|
| 1099 |
-
- if the entity is a non-static data member that has a
|
| 1100 |
-
|
| 1101 |
- the constructor’s class is a union ([[class.union]]), and no other
|
| 1102 |
variant member of that union is designated by a *mem-initializer-id*
|
| 1103 |
or
|
| 1104 |
- the constructor’s class is not a union, and, if the entity is a
|
| 1105 |
member of an anonymous union, no other member of that union is
|
| 1106 |
designated by a *mem-initializer-id*,
|
| 1107 |
|
| 1108 |
-
the entity is initialized
|
|
|
|
| 1109 |
- otherwise, if the entity is an anonymous union or a variant member (
|
| 1110 |
-
[[class.union]]), no initialization is performed;
|
| 1111 |
- otherwise, the entity is default-initialized ([[dcl.init]]).
|
| 1112 |
|
| 1113 |
-
An abstract class ([[class.abstract]]) is never a most
|
| 1114 |
-
thus its constructors never initialize virtual base
|
| 1115 |
-
the corresponding *mem-initializer*s may be
|
| 1116 |
-
|
| 1117 |
-
|
| 1118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1119 |
constructor was not invoked as part of value-initialization and a member
|
| 1120 |
of `X` is neither initialized nor given a value during execution of the
|
| 1121 |
*compound-statement* of the body of the constructor, the member has an
|
| 1122 |
-
indeterminate value.
|
|
|
|
|
|
|
| 1123 |
|
| 1124 |
``` cpp
|
| 1125 |
struct A {
|
| 1126 |
A();
|
| 1127 |
};
|
|
@@ -1137,32 +1385,59 @@ struct C {
|
|
| 1137 |
int i; // OK: i has indeterminate value
|
| 1138 |
int j = 5; // OK: j has the value 5
|
| 1139 |
};
|
| 1140 |
```
|
| 1141 |
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1146 |
|
| 1147 |
``` cpp
|
| 1148 |
struct A {
|
| 1149 |
int i = /* some integer expression with side effects */ ;
|
| 1150 |
A(int arg) : i(arg) { }
|
| 1151 |
// ...
|
| 1152 |
};
|
| 1153 |
```
|
| 1154 |
|
| 1155 |
the `A(int)` constructor will simply initialize `i` to the value of
|
| 1156 |
-
`arg`, and the side effects in `i`’s
|
| 1157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1158 |
|
| 1159 |
In a non-delegating constructor, the destructor for each potentially
|
| 1160 |
constructed subobject of class type is potentially invoked (
|
| 1161 |
-
[[class.dtor]]).
|
| 1162 |
-
|
| 1163 |
-
[
|
|
|
|
|
|
|
| 1164 |
|
| 1165 |
In a non-delegating constructor, initialization proceeds in the
|
| 1166 |
following order:
|
| 1167 |
|
| 1168 |
- First, and only for the constructor of the most derived class (
|
|
@@ -1171,18 +1446,21 @@ following order:
|
|
| 1171 |
acyclic graph of base classes, where “left-to-right” is the order of
|
| 1172 |
appearance of the base classes in the derived class
|
| 1173 |
*base-specifier-list*.
|
| 1174 |
- Then, direct base classes are initialized in declaration order as they
|
| 1175 |
appear in the *base-specifier-list* (regardless of the order of the
|
| 1176 |
-
*mem-
|
| 1177 |
- Then, non-static data members are initialized in the order they were
|
| 1178 |
declared in the class definition (again regardless of the order of the
|
| 1179 |
-
*mem-
|
| 1180 |
- Finally, the *compound-statement* of the constructor body is executed.
|
| 1181 |
|
| 1182 |
-
The declaration order is mandated to ensure that base and
|
| 1183 |
-
subobjects are destroyed in the reverse order of
|
|
|
|
|
|
|
|
|
|
| 1184 |
|
| 1185 |
``` cpp
|
| 1186 |
struct V {
|
| 1187 |
V();
|
| 1188 |
V(int);
|
|
@@ -1201,24 +1479,28 @@ struct B : virtual V {
|
|
| 1201 |
struct C : A, B, virtual V {
|
| 1202 |
C();
|
| 1203 |
C(int);
|
| 1204 |
};
|
| 1205 |
|
| 1206 |
-
A::A(int i) : V(i) {
|
| 1207 |
-
B::B(int i) {
|
| 1208 |
-
C::C(int i) {
|
| 1209 |
|
| 1210 |
V v(1); // use V(int)
|
| 1211 |
A a(2); // use V(int)
|
| 1212 |
B b(3); // use V()
|
| 1213 |
C c(4); // use V()
|
| 1214 |
```
|
| 1215 |
|
|
|
|
|
|
|
| 1216 |
Names in the *expression-list* or *braced-init-list* of a
|
| 1217 |
*mem-initializer* are evaluated in the scope of the constructor for
|
| 1218 |
which the *mem-initializer* is specified.
|
| 1219 |
|
|
|
|
|
|
|
| 1220 |
``` cpp
|
| 1221 |
class X {
|
| 1222 |
int a;
|
| 1223 |
int b;
|
| 1224 |
int i;
|
|
@@ -1231,22 +1513,28 @@ public:
|
|
| 1231 |
|
| 1232 |
initializes `X::r` to refer to `X::a`, initializes `X::b` with the value
|
| 1233 |
of the constructor parameter `i`, initializes `X::i` with the value of
|
| 1234 |
the constructor parameter `i`, and initializes `X::j` with the value of
|
| 1235 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 1236 |
-
|
| 1237 |
-
|
| 1238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1239 |
|
| 1240 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 1241 |
can be called for an object under construction. Similarly, an object
|
| 1242 |
under construction can be the operand of the `typeid` operator (
|
| 1243 |
[[expr.typeid]]) or of a `dynamic_cast` ([[expr.dynamic.cast]]).
|
| 1244 |
However, if these operations are performed in a *ctor-initializer* (or
|
| 1245 |
in a function called directly or indirectly from a *ctor-initializer*)
|
| 1246 |
before all the *mem-initializer*s for base classes have completed, the
|
| 1247 |
-
|
|
|
|
|
|
|
| 1248 |
|
| 1249 |
``` cpp
|
| 1250 |
class A {
|
| 1251 |
public:
|
| 1252 |
A(int);
|
|
@@ -1254,12 +1542,11 @@ public:
|
|
| 1254 |
|
| 1255 |
class B : public A {
|
| 1256 |
int j;
|
| 1257 |
public:
|
| 1258 |
int f();
|
| 1259 |
-
B() : A(f()), // undefined: calls member function
|
| 1260 |
-
// but base A not yet initialized
|
| 1261 |
j(f()) { } // well-defined: bases are all initialized
|
| 1262 |
};
|
| 1263 |
|
| 1264 |
class C {
|
| 1265 |
public:
|
|
@@ -1267,42 +1554,156 @@ public:
|
|
| 1267 |
};
|
| 1268 |
|
| 1269 |
class D : public B, C {
|
| 1270 |
int i;
|
| 1271 |
public:
|
| 1272 |
-
D() : C(f()), // undefined: calls member function
|
| 1273 |
-
// but base C not yet initialized
|
| 1274 |
i(f()) { } // well-defined: bases are all initialized
|
| 1275 |
};
|
| 1276 |
```
|
| 1277 |
|
| 1278 |
-
|
| 1279 |
-
|
| 1280 |
-
|
| 1281 |
-
construction
|
|
|
|
|
|
|
| 1282 |
|
| 1283 |
A *mem-initializer* followed by an ellipsis is a pack expansion (
|
| 1284 |
[[temp.variadic]]) that initializes the base classes specified by a pack
|
| 1285 |
expansion in the *base-specifier-list* for the class.
|
| 1286 |
|
|
|
|
|
|
|
| 1287 |
``` cpp
|
| 1288 |
template<class... Mixins>
|
| 1289 |
class X : public Mixins... {
|
| 1290 |
public:
|
| 1291 |
X(const Mixins&... mixins) : Mixins(mixins)... { }
|
| 1292 |
};
|
| 1293 |
```
|
| 1294 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1295 |
## Construction and destruction <a id="class.cdtor">[[class.cdtor]]</a>
|
| 1296 |
|
| 1297 |
For an object with a non-trivial constructor, referring to any
|
| 1298 |
non-static member or base class of the object before the constructor
|
| 1299 |
begins execution results in undefined behavior. For an object with a
|
| 1300 |
non-trivial destructor, referring to any non-static member or base class
|
| 1301 |
of the object after the destructor finishes execution results in
|
| 1302 |
undefined behavior.
|
| 1303 |
|
|
|
|
|
|
|
| 1304 |
``` cpp
|
| 1305 |
struct X { int i; };
|
| 1306 |
struct Y : X { Y(); }; // non-trivial
|
| 1307 |
struct A { int a; };
|
| 1308 |
struct B : public A { int j; Y y; }; // non-trivial
|
|
@@ -1331,10 +1732,12 @@ struct Y {
|
|
| 1331 |
Y() : p(&x.j) { // undefined, x is not yet constructed
|
| 1332 |
}
|
| 1333 |
};
|
| 1334 |
```
|
| 1335 |
|
|
|
|
|
|
|
| 1336 |
To explicitly or implicitly convert a pointer (a glvalue) referring to
|
| 1337 |
an object of class `X` to a pointer (reference) to a direct or indirect
|
| 1338 |
base class `B` of `X`, the construction of `X` and the construction of
|
| 1339 |
all of its direct or indirect bases that directly or indirectly derive
|
| 1340 |
from `B` shall have started and the destruction of these classes shall
|
|
@@ -1343,31 +1746,32 @@ behavior. To form a pointer to (or access the value of) a direct
|
|
| 1343 |
non-static member of an object `obj`, the construction of `obj` shall
|
| 1344 |
have started and its destruction shall not have completed, otherwise the
|
| 1345 |
computation of the pointer value (or accessing the member value) results
|
| 1346 |
in undefined behavior.
|
| 1347 |
|
|
|
|
|
|
|
| 1348 |
``` cpp
|
| 1349 |
struct A { };
|
| 1350 |
struct B : virtual A { };
|
| 1351 |
struct C : B { };
|
| 1352 |
struct D : virtual A { D(A*); };
|
| 1353 |
struct X { X(A*); };
|
| 1354 |
|
| 1355 |
struct E : C, D, X {
|
| 1356 |
-
E() : D(this), // undefined: upcast from E* to A*
|
| 1357 |
-
// might use path E* → D* → A*
|
| 1358 |
// but D is not constructed
|
| 1359 |
-
|
| 1360 |
-
// E* → C* defined because E() has started
|
| 1361 |
-
// and C* → A* defined because
|
| 1362 |
-
|
| 1363 |
-
X(this) {
|
| 1364 |
-
// C/B/D/A sublattice is fully constructed
|
| 1365 |
-
}
|
| 1366 |
};
|
| 1367 |
```
|
| 1368 |
|
|
|
|
|
|
|
| 1369 |
Member functions, including virtual functions ([[class.virtual]]), can
|
| 1370 |
be called during construction or destruction ([[class.base.init]]).
|
| 1371 |
When a virtual function is called directly or indirectly from a
|
| 1372 |
constructor or from a destructor, including during the construction or
|
| 1373 |
destruction of the class’s non-static data members, and the object to
|
|
@@ -1377,10 +1781,12 @@ constructor’s or destructor’s class and not one overriding it in a
|
|
| 1377 |
more-derived class. If the virtual function call uses an explicit class
|
| 1378 |
member access ([[expr.ref]]) and the object expression refers to the
|
| 1379 |
complete object of `x` or one of that object’s base class subobjects but
|
| 1380 |
not `x` or one of its base class subobjects, the behavior is undefined.
|
| 1381 |
|
|
|
|
|
|
|
| 1382 |
``` cpp
|
| 1383 |
struct V {
|
| 1384 |
virtual void f();
|
| 1385 |
virtual void g();
|
| 1386 |
};
|
|
@@ -1406,36 +1812,39 @@ B::B(V* v, A* a) {
|
|
| 1406 |
v->g(); // v is base of B, the call is well-defined, calls B::g
|
| 1407 |
a->f(); // undefined behavior, a's type not a base of B
|
| 1408 |
}
|
| 1409 |
```
|
| 1410 |
|
|
|
|
|
|
|
| 1411 |
The `typeid` operator ([[expr.typeid]]) can be used during construction
|
| 1412 |
or destruction ([[class.base.init]]). When `typeid` is used in a
|
| 1413 |
-
constructor (including the *mem-initializer* or
|
| 1414 |
-
|
| 1415 |
destructor, or used in a function called (directly or indirectly) from a
|
| 1416 |
constructor or destructor, if the operand of `typeid` refers to the
|
| 1417 |
object under construction or destruction, `typeid` yields the
|
| 1418 |
`std::type_info` object representing the constructor or destructor’s
|
| 1419 |
class. If the operand of `typeid` refers to the object under
|
| 1420 |
construction or destruction and the static type of the operand is
|
| 1421 |
neither the constructor or destructor’s class nor one of its bases, the
|
| 1422 |
-
|
| 1423 |
|
| 1424 |
`dynamic_cast`s ([[expr.dynamic.cast]]) can be used during construction
|
| 1425 |
or destruction ([[class.base.init]]). When a `dynamic_cast` is used in
|
| 1426 |
-
a constructor (including the *mem-initializer* or
|
| 1427 |
-
|
| 1428 |
-
|
| 1429 |
-
|
| 1430 |
-
|
| 1431 |
-
|
| 1432 |
-
|
| 1433 |
-
|
| 1434 |
-
|
| 1435 |
-
|
| 1436 |
-
|
|
|
|
| 1437 |
|
| 1438 |
``` cpp
|
| 1439 |
struct V {
|
| 1440 |
virtual void f();
|
| 1441 |
};
|
|
@@ -1450,34 +1859,48 @@ struct D : A, B {
|
|
| 1450 |
D() : B((A*)this, this) { }
|
| 1451 |
};
|
| 1452 |
|
| 1453 |
B::B(V* v, A* a) {
|
| 1454 |
typeid(*this); // type_info for B
|
| 1455 |
-
typeid(*v); // well-defined: *v has type V, a base of B
|
| 1456 |
-
// yields type_info for B
|
| 1457 |
typeid(*a); // undefined behavior: type A not a base of B
|
| 1458 |
-
dynamic_cast<B*>(v); // well-defined: v of type V*, V base of B
|
| 1459 |
-
|
| 1460 |
-
dynamic_cast<B*>(a); // undefined behavior,
|
| 1461 |
-
// a has type A*, A not a base of B
|
| 1462 |
}
|
| 1463 |
```
|
| 1464 |
|
|
|
|
|
|
|
| 1465 |
## Copying and moving class objects <a id="class.copy">[[class.copy]]</a>
|
| 1466 |
|
| 1467 |
A class object can be copied or moved in two ways: by initialization (
|
| 1468 |
[[class.ctor]], [[dcl.init]]), including for function argument passing (
|
| 1469 |
[[expr.call]]) and for function value return ([[stmt.return]]); and by
|
| 1470 |
assignment ([[expr.ass]]). Conceptually, these two operations are
|
| 1471 |
implemented by a copy/move constructor ([[class.ctor]]) and copy/move
|
| 1472 |
assignment operator ([[over.ass]]).
|
| 1473 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1474 |
A non-template constructor for class `X` is a copy constructor if its
|
| 1475 |
first parameter is of type `X&`, `const X&`, `volatile X&` or
|
| 1476 |
`const volatile X&`, and either there are no other parameters or else
|
| 1477 |
all other parameters have default arguments ([[dcl.fct.default]]).
|
| 1478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1479 |
|
| 1480 |
``` cpp
|
| 1481 |
struct X {
|
| 1482 |
X(int);
|
| 1483 |
X(const X&, int = 1);
|
|
@@ -1485,14 +1908,19 @@ struct X {
|
|
| 1485 |
X a(1); // calls X(int);
|
| 1486 |
X b(a, 0); // calls X(const X&, int);
|
| 1487 |
X c = b; // calls X(const X&, int);
|
| 1488 |
```
|
| 1489 |
|
|
|
|
|
|
|
| 1490 |
A non-template constructor for class `X` is a move constructor if its
|
| 1491 |
first parameter is of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 1492 |
`const volatile X&&`, and either there are no other parameters or else
|
| 1493 |
all other parameters have default arguments ([[dcl.fct.default]]).
|
|
|
|
|
|
|
|
|
|
| 1494 |
`Y::Y(Y&&)` is a move constructor.
|
| 1495 |
|
| 1496 |
``` cpp
|
| 1497 |
struct Y {
|
| 1498 |
Y(const Y&);
|
|
@@ -1501,40 +1929,60 @@ struct Y {
|
|
| 1501 |
extern Y f(int);
|
| 1502 |
Y d(f(1)); // calls Y(Y&&)
|
| 1503 |
Y e = d; // calls Y(const Y&)
|
| 1504 |
```
|
| 1505 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1506 |
All forms of copy/move constructor may be declared for a class.
|
| 1507 |
|
|
|
|
|
|
|
| 1508 |
``` cpp
|
| 1509 |
struct X {
|
| 1510 |
X(const X&);
|
| 1511 |
X(X&); // OK
|
| 1512 |
X(X&&);
|
| 1513 |
X(const X&&); // OK, but possibly not sensible
|
| 1514 |
};
|
| 1515 |
```
|
| 1516 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1517 |
If a class `X` only has a copy constructor with a parameter of type
|
| 1518 |
`X&`, an initializer of type `const` `X` or `volatile` `X` cannot
|
| 1519 |
initialize an object of type (possibly cv-qualified) `X`.
|
| 1520 |
|
|
|
|
|
|
|
| 1521 |
``` cpp
|
| 1522 |
struct X {
|
| 1523 |
X(); // default constructor
|
| 1524 |
-
X(X&); // copy constructor with a
|
| 1525 |
};
|
| 1526 |
const X cx;
|
| 1527 |
X x = cx; // error: X::X(X&) cannot copy cx into x
|
| 1528 |
```
|
| 1529 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1530 |
A declaration of a constructor for a class `X` is ill-formed if its
|
| 1531 |
first parameter is of type (optionally cv-qualified) `X` and either
|
| 1532 |
there are no other parameters or else all other parameters have default
|
| 1533 |
arguments. A member function template is never instantiated to produce
|
| 1534 |
such a constructor signature.
|
| 1535 |
|
|
|
|
|
|
|
| 1536 |
``` cpp
|
| 1537 |
struct S {
|
| 1538 |
template<typename T> S(T);
|
| 1539 |
S();
|
| 1540 |
};
|
|
@@ -1545,17 +1993,19 @@ void h() {
|
|
| 1545 |
S a(g); // does not instantiate the member template to produce S::S<S>(S);
|
| 1546 |
// uses the implicitly declared copy constructor
|
| 1547 |
}
|
| 1548 |
```
|
| 1549 |
|
|
|
|
|
|
|
| 1550 |
If the class definition does not explicitly declare a copy constructor,
|
| 1551 |
-
one is declared *implicitly*. If the class definition
|
| 1552 |
-
constructor or move assignment operator, the implicitly
|
| 1553 |
-
constructor is defined as deleted; otherwise, it is
|
| 1554 |
-
defaulted ([[dcl.fct.def]]). The latter case is deprecated
|
| 1555 |
-
has a user-declared copy assignment operator or a
|
| 1556 |
-
destructor.
|
| 1557 |
|
| 1558 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 1559 |
form
|
| 1560 |
|
| 1561 |
``` cpp
|
|
@@ -1570,20 +2020,21 @@ copy constructor will have the form
|
|
| 1570 |
``` cpp
|
| 1571 |
X::X(X&)
|
| 1572 |
```
|
| 1573 |
|
| 1574 |
If the definition of a class `X` does not explicitly declare a move
|
| 1575 |
-
constructor, one will be implicitly declared as defaulted
|
|
|
|
| 1576 |
|
| 1577 |
- `X` does not have a user-declared copy constructor,
|
| 1578 |
- `X` does not have a user-declared copy assignment operator,
|
| 1579 |
- `X` does not have a user-declared move assignment operator, and
|
| 1580 |
- `X` does not have a user-declared destructor.
|
| 1581 |
|
| 1582 |
-
When the move constructor is not implicitly declared or
|
| 1583 |
-
supplied, expressions that otherwise would have invoked the
|
| 1584 |
-
constructor may instead invoke a copy constructor.
|
| 1585 |
|
| 1586 |
The implicitly-declared move constructor for class `X` will have the
|
| 1587 |
form
|
| 1588 |
|
| 1589 |
``` cpp
|
|
@@ -1596,64 +2047,69 @@ is defined as deleted ([[dcl.fct.def.delete]]) if `X` has:
|
|
| 1596 |
|
| 1597 |
- a variant member with a non-trivial corresponding constructor and `X`
|
| 1598 |
is a union-like class,
|
| 1599 |
- a potentially constructed subobject type `M` (or array thereof) that
|
| 1600 |
cannot be copied/moved because overload resolution ([[over.match]]),
|
| 1601 |
-
as applied to `M`’s corresponding constructor, results in an
|
| 1602 |
-
or a function that is deleted or inaccessible from the
|
| 1603 |
-
constructor,
|
| 1604 |
- any potentially constructed subobject of a type with a destructor that
|
| 1605 |
is deleted or inaccessible from the defaulted constructor, or,
|
| 1606 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 1607 |
type.
|
| 1608 |
|
| 1609 |
A defaulted move constructor that is defined as deleted is ignored by
|
| 1610 |
-
overload resolution ([[over.match]], [[over.over]]).
|
| 1611 |
-
|
| 1612 |
-
|
|
|
|
|
|
|
| 1613 |
|
| 1614 |
A copy/move constructor for class `X` is trivial if it is not
|
| 1615 |
-
user-provided
|
| 1616 |
-
parameter-type-list of an implicit declaration, and if
|
| 1617 |
|
| 1618 |
- class `X` has no virtual functions ([[class.virtual]]) and no virtual
|
| 1619 |
base classes ([[class.mi]]), and
|
| 1620 |
-
- class `X` has no non-static data members of volatile-qualified type,
|
| 1621 |
-
and
|
| 1622 |
- the constructor selected to copy/move each direct base class subobject
|
| 1623 |
is trivial, and
|
| 1624 |
- for each non-static data member of `X` that is of class type (or array
|
| 1625 |
thereof), the constructor selected to copy/move that member is
|
| 1626 |
trivial;
|
| 1627 |
|
| 1628 |
otherwise the copy/move constructor is *non-trivial*.
|
| 1629 |
|
| 1630 |
A copy/move constructor that is defaulted and not defined as deleted is
|
| 1631 |
*implicitly defined* if it is odr-used ([[basic.def.odr]]) or when it
|
| 1632 |
-
is explicitly defaulted after its first declaration.
|
| 1633 |
-
|
| 1634 |
-
|
| 1635 |
-
|
| 1636 |
-
|
|
|
|
|
|
|
|
|
|
| 1637 |
constructor is `constexpr`.
|
| 1638 |
|
| 1639 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 1640 |
defined, all non-user-provided copy/move constructors for its
|
| 1641 |
potentially constructed subobjects shall have been implicitly defined.
|
| 1642 |
-
|
| 1643 |
-
*
|
|
|
|
| 1644 |
|
| 1645 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 1646 |
performs a memberwise copy/move of its bases and members.
|
| 1647 |
-
|
| 1648 |
-
|
| 1649 |
-
|
| 1650 |
-
|
| 1651 |
-
|
| 1652 |
-
|
| 1653 |
-
|
| 1654 |
-
|
|
|
|
|
|
|
| 1655 |
|
| 1656 |
- if the member is an array, each element is direct-initialized with the
|
| 1657 |
corresponding subobject of `x`;
|
| 1658 |
- if a member `m` has rvalue reference type `T&&`, it is
|
| 1659 |
direct-initialized with `static_cast<T&&>(x.m)`;
|
|
@@ -1664,19 +2120,30 @@ Virtual base class subobjects shall be initialized only once by the
|
|
| 1664 |
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 1665 |
|
| 1666 |
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 1667 |
object representation ([[basic.types]]) of `X`.
|
| 1668 |
|
|
|
|
|
|
|
| 1669 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 1670 |
non-static non-template member function of class `X` with exactly one
|
| 1671 |
parameter of type `X`, `X&`, `const` `X&`, `volatile` `X&` or `const`
|
| 1672 |
-
`volatile` `X&`.[^5]
|
| 1673 |
-
|
| 1674 |
-
|
| 1675 |
-
only
|
| 1676 |
-
|
| 1677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1678 |
|
| 1679 |
``` cpp
|
| 1680 |
struct X {
|
| 1681 |
X();
|
| 1682 |
X& operator=(X&);
|
|
@@ -1686,10 +2153,14 @@ X x;
|
|
| 1686 |
void f() {
|
| 1687 |
x = cx; // error: X::operator=(X&) cannot assign cx into x
|
| 1688 |
}
|
| 1689 |
```
|
| 1690 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1691 |
If the class definition does not explicitly declare a copy assignment
|
| 1692 |
operator, one is declared *implicitly*. If the class definition declares
|
| 1693 |
a move constructor or move assignment operator, the implicitly declared
|
| 1694 |
copy assignment operator is defined as deleted; otherwise, it is defined
|
| 1695 |
as defaulted ([[dcl.fct.def]]). The latter case is deprecated if the
|
|
@@ -1717,24 +2188,29 @@ the form
|
|
| 1717 |
X& X::operator=(X&)
|
| 1718 |
```
|
| 1719 |
|
| 1720 |
A user-declared move assignment operator `X::operator=` is a non-static
|
| 1721 |
non-template member function of class `X` with exactly one parameter of
|
| 1722 |
-
type `X&&`, `const X&&`, `volatile X&&`, or `const volatile X&&`.
|
| 1723 |
-
|
| 1724 |
-
|
| 1725 |
-
|
|
|
|
|
|
|
|
|
|
| 1726 |
|
| 1727 |
If the definition of a class `X` does not explicitly declare a move
|
| 1728 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1729 |
only if
|
| 1730 |
|
| 1731 |
- `X` does not have a user-declared copy constructor,
|
| 1732 |
- `X` does not have a user-declared move constructor,
|
| 1733 |
- `X` does not have a user-declared copy assignment operator, and
|
| 1734 |
- `X` does not have a user-declared destructor.
|
| 1735 |
|
|
|
|
|
|
|
| 1736 |
The class definition
|
| 1737 |
|
| 1738 |
``` cpp
|
| 1739 |
struct S {
|
| 1740 |
int a;
|
|
@@ -1752,10 +2228,12 @@ struct S {
|
|
| 1752 |
S& operator=(const S&) = default;
|
| 1753 |
S& operator=(S&&) = default;
|
| 1754 |
};
|
| 1755 |
```
|
| 1756 |
|
|
|
|
|
|
|
| 1757 |
The implicitly-declared move assignment operator for a class `X` will
|
| 1758 |
have the form
|
| 1759 |
|
| 1760 |
``` cpp
|
| 1761 |
X& X::operator=(X&&);
|
|
@@ -1773,15 +2251,16 @@ deleted if `X` has:
|
|
| 1773 |
- a variant member with a non-trivial corresponding assignment operator
|
| 1774 |
and `X` is a union-like class, or
|
| 1775 |
- a non-static data member of `const` non-class type (or array thereof),
|
| 1776 |
or
|
| 1777 |
- a non-static data member of reference type, or
|
| 1778 |
-
- a
|
| 1779 |
-
|
| 1780 |
-
[[over.match]]), as applied to `M`’s
|
| 1781 |
-
operator, results in an ambiguity or a
|
| 1782 |
-
inaccessible from the defaulted assignment
|
|
|
|
| 1783 |
|
| 1784 |
A defaulted move assignment operator that is defined as deleted is
|
| 1785 |
ignored by overload resolution ([[over.match]], [[over.over]]).
|
| 1786 |
|
| 1787 |
Because a copy/move assignment operator is implicitly declared for a
|
|
@@ -1795,17 +2274,14 @@ declaration of such an operator and does not suppress the implicit
|
|
| 1795 |
declaration of the derived class operator; the operator introduced by
|
| 1796 |
the *using-declaration* is hidden by the implicitly-declared operator in
|
| 1797 |
the derived class.
|
| 1798 |
|
| 1799 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1800 |
-
user-provided
|
| 1801 |
-
parameter-type-list of an implicit declaration, and if
|
| 1802 |
|
| 1803 |
- class `X` has no virtual functions ([[class.virtual]]) and no virtual
|
| 1804 |
base classes ([[class.mi]]), and
|
| 1805 |
-
- class `X` has no non-static data members of volatile-qualified type,
|
| 1806 |
-
and
|
| 1807 |
- the assignment operator selected to copy/move each direct base class
|
| 1808 |
subobject is trivial, and
|
| 1809 |
- for each non-static data member of `X` that is of class type (or array
|
| 1810 |
thereof), the assignment operator selected to copy/move that member is
|
| 1811 |
trivial;
|
|
@@ -1819,20 +2295,22 @@ assign to an object of its class type) or when it is explicitly
|
|
| 1819 |
defaulted after its first declaration. The implicitly-defined copy/move
|
| 1820 |
assignment operator is `constexpr` if
|
| 1821 |
|
| 1822 |
- `X` is a literal type, and
|
| 1823 |
- the assignment operator selected to copy/move each direct base class
|
| 1824 |
-
subobject is a
|
| 1825 |
- for each non-static data member of `X` that is of class type (or array
|
| 1826 |
thereof), the assignment operator selected to copy/move that member is
|
| 1827 |
-
a
|
| 1828 |
|
| 1829 |
Before the defaulted copy/move assignment operator for a class is
|
| 1830 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1831 |
for its direct base classes and its non-static data members shall have
|
| 1832 |
-
been implicitly defined.
|
| 1833 |
-
|
|
|
|
|
|
|
| 1834 |
|
| 1835 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 1836 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 1837 |
The direct base classes of `X` are assigned first, in the order of their
|
| 1838 |
declaration in the *base-specifier-list*, and then the immediate
|
|
@@ -1851,71 +2329,79 @@ type:
|
|
| 1851 |
appropriate to the element type;
|
| 1852 |
- if the subobject is of scalar type, the built-in assignment operator
|
| 1853 |
is used.
|
| 1854 |
|
| 1855 |
It is unspecified whether subobjects representing virtual base classes
|
| 1856 |
-
are assigned more than once by the implicitly-defined copy
|
| 1857 |
-
operator.
|
|
|
|
|
|
|
| 1858 |
|
| 1859 |
``` cpp
|
| 1860 |
struct V { };
|
| 1861 |
struct A : virtual V { };
|
| 1862 |
struct B : virtual V { };
|
| 1863 |
struct C : B, A { };
|
| 1864 |
```
|
| 1865 |
|
| 1866 |
It is unspecified whether the virtual base class subobject `V` is
|
| 1867 |
-
assigned twice by the implicitly-defined copy assignment operator
|
| 1868 |
-
`C`.
|
| 1869 |
-
|
|
|
|
| 1870 |
|
| 1871 |
The implicitly-defined copy assignment operator for a union `X` copies
|
| 1872 |
the object representation ([[basic.types]]) of `X`.
|
| 1873 |
|
| 1874 |
-
|
| 1875 |
-
assignment operator for an object is implicitly odr-used and the special
|
| 1876 |
-
member function is not accessible (Clause [[class.access]]).
|
| 1877 |
-
Copying/moving one object into another using the copy/move constructor
|
| 1878 |
-
or the copy/move assignment operator does not change the layout or size
|
| 1879 |
-
of either object.
|
| 1880 |
|
| 1881 |
When certain criteria are met, an implementation is allowed to omit the
|
| 1882 |
copy/move construction of a class object, even if the constructor
|
| 1883 |
selected for the copy/move operation and/or the destructor for the
|
| 1884 |
object have side effects. In such cases, the implementation treats the
|
| 1885 |
source and target of the omitted copy/move operation as simply two
|
| 1886 |
-
different ways of referring to the same object
|
| 1887 |
-
|
| 1888 |
-
|
| 1889 |
-
|
| 1890 |
-
|
| 1891 |
-
|
|
|
|
|
|
|
| 1892 |
|
| 1893 |
- in a `return` statement in a function with a class return type, when
|
| 1894 |
-
the expression is the name of a non-volatile automatic object (other
|
| 1895 |
-
than a function or
|
| 1896 |
-
|
| 1897 |
-
|
| 1898 |
-
|
| 1899 |
-
|
| 1900 |
-
|
| 1901 |
-
|
| 1902 |
-
|
| 1903 |
-
|
| 1904 |
-
|
| 1905 |
-
|
| 1906 |
-
|
| 1907 |
-
|
| 1908 |
-
|
| 1909 |
-
|
| 1910 |
-
|
| 1911 |
-
|
| 1912 |
-
|
| 1913 |
-
|
| 1914 |
-
|
| 1915 |
-
|
| 1916 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1917 |
|
| 1918 |
``` cpp
|
| 1919 |
class Thing {
|
| 1920 |
public:
|
| 1921 |
Thing();
|
|
@@ -1927,37 +2413,67 @@ Thing f() {
|
|
| 1927 |
Thing t;
|
| 1928 |
return t;
|
| 1929 |
}
|
| 1930 |
|
| 1931 |
Thing t2 = f();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1932 |
```
|
| 1933 |
|
| 1934 |
-
Here the criteria for elision can
|
| 1935 |
-
|
| 1936 |
-
|
| 1937 |
-
|
| 1938 |
-
`t2`
|
| 1939 |
-
|
| 1940 |
-
|
| 1941 |
-
|
| 1942 |
-
construction from the temporary object to `t2` that is elided.
|
| 1943 |
|
| 1944 |
-
|
| 1945 |
-
|
| 1946 |
-
the
|
| 1947 |
-
|
| 1948 |
-
|
| 1949 |
-
|
| 1950 |
-
|
| 1951 |
-
|
| 1952 |
-
|
| 1953 |
-
|
| 1954 |
-
|
| 1955 |
-
|
| 1956 |
-
|
| 1957 |
-
the
|
| 1958 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1959 |
|
| 1960 |
``` cpp
|
| 1961 |
class Thing {
|
| 1962 |
public:
|
| 1963 |
Thing();
|
|
@@ -1972,203 +2488,33 @@ Thing f(bool b) {
|
|
| 1972 |
if (b)
|
| 1973 |
throw t; // OK: Thing(Thing&&) used (or elided) to throw t
|
| 1974 |
return t; // OK: Thing(Thing&&) used (or elided) to return t
|
| 1975 |
}
|
| 1976 |
|
| 1977 |
-
Thing t2 = f(false);
|
| 1978 |
-
```
|
| 1979 |
|
| 1980 |
-
|
| 1981 |
-
|
| 1982 |
-
|
| 1983 |
-
implicitly declares a set of *inheriting constructors*. The *candidate
|
| 1984 |
-
set of inherited constructors* from the class `X` named in the
|
| 1985 |
-
*using-declaration* consists of actual constructors and notional
|
| 1986 |
-
constructors that result from the transformation of defaulted parameters
|
| 1987 |
-
as follows:
|
| 1988 |
-
|
| 1989 |
-
- all non-template constructors of `X`, and
|
| 1990 |
-
- for each non-template constructor of `X` that has at least one
|
| 1991 |
-
parameter with a default argument, the set of constructors that
|
| 1992 |
-
results from omitting any ellipsis parameter specification and
|
| 1993 |
-
successively omitting parameters with a default argument from the end
|
| 1994 |
-
of the parameter-type-list, and
|
| 1995 |
-
- all constructor templates of `X`, and
|
| 1996 |
-
- for each constructor template of `X` that has at least one parameter
|
| 1997 |
-
with a default argument, the set of constructor templates that results
|
| 1998 |
-
from omitting any ellipsis parameter specification and successively
|
| 1999 |
-
omitting parameters with a default argument from the end of the
|
| 2000 |
-
parameter-type-list.
|
| 2001 |
-
|
| 2002 |
-
The *constructor characteristics* of a constructor or constructor
|
| 2003 |
-
template are
|
| 2004 |
-
|
| 2005 |
-
- the template parameter list ([[temp.param]]), if any,
|
| 2006 |
-
- the *parameter-type-list* ([[dcl.fct]]),
|
| 2007 |
-
- absence or presence of `explicit` ([[class.conv.ctor]]), and
|
| 2008 |
-
- absence or presence of `constexpr` ([[dcl.constexpr]]).
|
| 2009 |
-
|
| 2010 |
-
For each non-template constructor in the candidate set of inherited
|
| 2011 |
-
constructors other than a constructor having no parameters or a
|
| 2012 |
-
copy/move constructor having a single parameter, a constructor is
|
| 2013 |
-
implicitly declared with the same constructor characteristics unless
|
| 2014 |
-
there is a user-declared constructor with the same signature in the
|
| 2015 |
-
complete class where the *using-declaration* appears or the constructor
|
| 2016 |
-
would be a default, copy, or move constructor for that class. Similarly,
|
| 2017 |
-
for each constructor template in the candidate set of inherited
|
| 2018 |
-
constructors, a constructor template is implicitly declared with the
|
| 2019 |
-
same constructor characteristics unless there is an equivalent
|
| 2020 |
-
user-declared constructor template ([[temp.over.link]]) in the complete
|
| 2021 |
-
class where the *using-declaration* appears. Default arguments are not
|
| 2022 |
-
inherited. An *exception-specification* is implied as specified in
|
| 2023 |
-
[[except.spec]].
|
| 2024 |
-
|
| 2025 |
-
A constructor so declared has the same access as the corresponding
|
| 2026 |
-
constructor in `X`. It is deleted if the corresponding constructor in
|
| 2027 |
-
`X` is deleted ([[dcl.fct.def]]). An inheriting constructor shall not
|
| 2028 |
-
be explicitly instantiated ([[temp.explicit]]) or explicitly
|
| 2029 |
-
specialized ([[temp.expl.spec]]).
|
| 2030 |
-
|
| 2031 |
-
Default and copy/move constructors may be implicitly declared as
|
| 2032 |
-
specified in [[class.ctor]] and [[class.copy]].
|
| 2033 |
-
|
| 2034 |
-
``` cpp
|
| 2035 |
-
struct B1 {
|
| 2036 |
-
B1(int);
|
| 2037 |
-
};
|
| 2038 |
-
|
| 2039 |
-
struct B2 {
|
| 2040 |
-
B2(int = 13, int = 42);
|
| 2041 |
-
};
|
| 2042 |
-
|
| 2043 |
-
struct D1 : B1 {
|
| 2044 |
-
using B1::B1;
|
| 2045 |
-
};
|
| 2046 |
-
|
| 2047 |
-
struct D2 : B2 {
|
| 2048 |
-
using B2::B2;
|
| 2049 |
-
};
|
| 2050 |
-
```
|
| 2051 |
-
|
| 2052 |
-
The candidate set of inherited constructors in `D1` for `B1` is
|
| 2053 |
-
|
| 2054 |
-
- `B1(const B1&)`
|
| 2055 |
-
- `B1(B1&&)`
|
| 2056 |
-
- `B1(int)`
|
| 2057 |
-
|
| 2058 |
-
The set of constructors present in `D1` is
|
| 2059 |
-
|
| 2060 |
-
- `D1()`, implicitly-declared default constructor, ill-formed if
|
| 2061 |
-
odr-used
|
| 2062 |
-
- `D1(const D1&)`, implicitly-declared copy constructor, not inherited
|
| 2063 |
-
- `D1(D1&&)`, implicitly-declared move constructor, not inherited
|
| 2064 |
-
- `D1(int)`, implicitly-declared inheriting constructor
|
| 2065 |
-
|
| 2066 |
-
The candidate set of inherited constructors in `D2` for `B2` is
|
| 2067 |
-
|
| 2068 |
-
- `B2(const B2&)`
|
| 2069 |
-
- `B2(B2&&)`
|
| 2070 |
-
- `B2(int = 13, int = 42)`
|
| 2071 |
-
- `B2(int = 13)`
|
| 2072 |
-
- `B2()`
|
| 2073 |
-
|
| 2074 |
-
The set of constructors present in `D2` is
|
| 2075 |
-
|
| 2076 |
-
- `D2()`, implicitly-declared default constructor, not inherited
|
| 2077 |
-
- `D2(const D2&)`, implicitly-declared copy constructor, not inherited
|
| 2078 |
-
- `D2(D2&&)`, implicitly-declared move constructor, not inherited
|
| 2079 |
-
- `D2(int, int)`, implicitly-declared inheriting constructor
|
| 2080 |
-
- `D2(int)`, implicitly-declared inheriting constructor
|
| 2081 |
-
|
| 2082 |
-
If two *using-declaration*s declare inheriting constructors with the
|
| 2083 |
-
same signatures, the program is ill-formed ([[class.mem]],
|
| 2084 |
-
[[over.load]]), because an implicitly-declared constructor introduced by
|
| 2085 |
-
the first *using-declaration* is not a user-declared constructor and
|
| 2086 |
-
thus does not preclude another declaration of a constructor with the
|
| 2087 |
-
same signature by a subsequent *using-declaration*.
|
| 2088 |
-
|
| 2089 |
-
``` cpp
|
| 2090 |
-
struct B1 {
|
| 2091 |
-
B1(int);
|
| 2092 |
-
};
|
| 2093 |
-
|
| 2094 |
-
struct B2 {
|
| 2095 |
-
B2(int);
|
| 2096 |
-
};
|
| 2097 |
-
|
| 2098 |
-
struct D1 : B1, B2 {
|
| 2099 |
-
using B1::B1;
|
| 2100 |
-
using B2::B2;
|
| 2101 |
-
}; // ill-formed: attempts to declare D1(int) twice
|
| 2102 |
-
|
| 2103 |
-
struct D2 : B1, B2 {
|
| 2104 |
-
using B1::B1;
|
| 2105 |
-
using B2::B2;
|
| 2106 |
-
D2(int); // OK: user declaration supersedes both implicit declarations
|
| 2107 |
-
};
|
| 2108 |
-
```
|
| 2109 |
-
|
| 2110 |
-
An inheriting constructor for a class is implicitly defined when it is
|
| 2111 |
-
odr-used ([[basic.def.odr]]) to create an object of its class type (
|
| 2112 |
-
[[intro.object]]). An implicitly-defined inheriting constructor performs
|
| 2113 |
-
the set of initializations of the class that would be performed by a
|
| 2114 |
-
user-written `inline` constructor for that class with a
|
| 2115 |
-
*mem-initializer-list* whose only *mem-initializer* has a
|
| 2116 |
-
*mem-initializer-id* that names the base class denoted in the
|
| 2117 |
-
*nested-name-specifier* of the *using-declaration* and an
|
| 2118 |
-
*expression-list* as specified below, and where the *compound-statement*
|
| 2119 |
-
in its function body is empty ([[class.base.init]]). If that
|
| 2120 |
-
user-written constructor would be ill-formed, the program is ill-formed.
|
| 2121 |
-
Each *expression* in the *expression-list* is of the form
|
| 2122 |
-
`static_cast<T&&>(p)`, where `p` is the name of the corresponding
|
| 2123 |
-
constructor parameter and `T` is the declared type of `p`.
|
| 2124 |
-
|
| 2125 |
-
``` cpp
|
| 2126 |
-
struct B1 {
|
| 2127 |
-
B1(int) { }
|
| 2128 |
-
};
|
| 2129 |
-
|
| 2130 |
-
struct B2 {
|
| 2131 |
-
B2(double) { }
|
| 2132 |
-
};
|
| 2133 |
-
|
| 2134 |
-
struct D1 : B1 {
|
| 2135 |
-
using B1::B1; // implicitly declares D1(int)
|
| 2136 |
-
int x;
|
| 2137 |
};
|
| 2138 |
|
| 2139 |
-
|
| 2140 |
-
|
| 2141 |
-
|
| 2142 |
}
|
| 2143 |
-
|
| 2144 |
-
struct D2 : B2 {
|
| 2145 |
-
using B2::B2; // OK: implicitly declares D2(double)
|
| 2146 |
-
B1 b;
|
| 2147 |
-
};
|
| 2148 |
-
|
| 2149 |
-
D2 f(1.0); // error: B1 has no default constructor
|
| 2150 |
-
|
| 2151 |
-
template< class T >
|
| 2152 |
-
struct D : T {
|
| 2153 |
-
using T::T; // declares all constructors from class T
|
| 2154 |
-
~D() { std::clog << "Destroying wrapper" << std::endl; }
|
| 2155 |
-
};
|
| 2156 |
```
|
| 2157 |
|
| 2158 |
-
|
| 2159 |
-
while writing a message to the standard log whenever an object of class
|
| 2160 |
-
`D` is destroyed.
|
| 2161 |
|
| 2162 |
<!-- Link reference definitions -->
|
| 2163 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 2164 |
[basic.life]: basic.md#basic.life
|
| 2165 |
[basic.lookup]: basic.md#basic.lookup
|
| 2166 |
[basic.lval]: basic.md#basic.lval
|
| 2167 |
-
[basic.start.
|
|
|
|
| 2168 |
[basic.start.term]: basic.md#basic.start.term
|
| 2169 |
-
[basic.stc]: basic.md#basic.stc
|
| 2170 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 2171 |
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 2172 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 2173 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 2174 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
|
@@ -2180,27 +2526,32 @@ while writing a message to the standard log whenever an object of class
|
|
| 2180 |
[class.cdtor]: #class.cdtor
|
| 2181 |
[class.conv]: #class.conv
|
| 2182 |
[class.conv.ctor]: #class.conv.ctor
|
| 2183 |
[class.conv.fct]: #class.conv.fct
|
| 2184 |
[class.copy]: #class.copy
|
|
|
|
|
|
|
|
|
|
| 2185 |
[class.ctor]: #class.ctor
|
| 2186 |
[class.dtor]: #class.dtor
|
| 2187 |
[class.expl.init]: #class.expl.init
|
| 2188 |
[class.free]: #class.free
|
| 2189 |
[class.friend]: class.md#class.friend
|
| 2190 |
-
[class.inhctor]: #class.inhctor
|
| 2191 |
[class.init]: #class.init
|
| 2192 |
[class.mem]: class.md#class.mem
|
| 2193 |
[class.member.lookup]: class.md#class.member.lookup
|
| 2194 |
[class.mfct]: class.md#class.mfct
|
| 2195 |
[class.mi]: class.md#class.mi
|
| 2196 |
[class.qual]: basic.md#class.qual
|
| 2197 |
[class.temporary]: #class.temporary
|
| 2198 |
[class.union]: class.md#class.union
|
|
|
|
| 2199 |
[class.virtual]: class.md#class.virtual
|
| 2200 |
[conv]: conv.md#conv
|
| 2201 |
-
[conv.
|
|
|
|
| 2202 |
[dcl.array]: dcl.md#dcl.array
|
| 2203 |
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 2204 |
[dcl.fct]: dcl.md#dcl.fct
|
| 2205 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 2206 |
[dcl.fct.def.delete]: dcl.md#dcl.fct.def.delete
|
|
@@ -2208,53 +2559,57 @@ while writing a message to the standard log whenever an object of class
|
|
| 2208 |
[dcl.fct.spec]: dcl.md#dcl.fct.spec
|
| 2209 |
[dcl.init]: dcl.md#dcl.init
|
| 2210 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 2211 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 2212 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
|
|
|
| 2213 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 2214 |
[diff.special]: compatibility.md#diff.special
|
| 2215 |
[except]: except.md#except
|
| 2216 |
[except.ctor]: except.md#except.ctor
|
|
|
|
| 2217 |
[except.spec]: except.md#except.spec
|
| 2218 |
[except.throw]: except.md#except.throw
|
| 2219 |
[expr]: expr.md#expr
|
| 2220 |
[expr.ass]: expr.md#expr.ass
|
| 2221 |
[expr.call]: expr.md#expr.call
|
| 2222 |
[expr.cast]: expr.md#expr.cast
|
|
|
|
| 2223 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 2224 |
[expr.delete]: expr.md#expr.delete
|
| 2225 |
[expr.dynamic.cast]: expr.md#expr.dynamic.cast
|
|
|
|
| 2226 |
[expr.new]: expr.md#expr.new
|
| 2227 |
[expr.prim]: expr.md#expr.prim
|
|
|
|
| 2228 |
[expr.pseudo]: expr.md#expr.pseudo
|
| 2229 |
[expr.ref]: expr.md#expr.ref
|
|
|
|
| 2230 |
[expr.static.cast]: expr.md#expr.static.cast
|
|
|
|
|
|
|
| 2231 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 2232 |
[expr.typeid]: expr.md#expr.typeid
|
| 2233 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 2234 |
[intro.execution]: intro.md#intro.execution
|
| 2235 |
[intro.object]: intro.md#intro.object
|
| 2236 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 2237 |
[over.ass]: over.md#over.ass
|
| 2238 |
[over.best.ics]: over.md#over.best.ics
|
| 2239 |
[over.ics.ref]: over.md#over.ics.ref
|
| 2240 |
-
[over.load]: over.md#over.load
|
| 2241 |
[over.match]: over.md#over.match
|
| 2242 |
[over.match.best]: over.md#over.match.best
|
|
|
|
| 2243 |
[over.over]: over.md#over.over
|
| 2244 |
[special]: #special
|
| 2245 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 2246 |
[stmt.return]: stmt.md#stmt.return
|
| 2247 |
[temp.dep.type]: temp.md#temp.dep.type
|
| 2248 |
-
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 2249 |
-
[temp.explicit]: temp.md#temp.explicit
|
| 2250 |
-
[temp.over.link]: temp.md#temp.over.link
|
| 2251 |
-
[temp.param]: temp.md#temp.param
|
| 2252 |
[temp.variadic]: temp.md#temp.variadic
|
| 2253 |
|
| 2254 |
[^1]: The same rules apply to initialization of an `initializer_list`
|
| 2255 |
-
object ([[dcl.init.list]]) with its underlying temporary array
|
| 2256 |
|
| 2257 |
[^2]: These conversions are considered as standard conversions for the
|
| 2258 |
purposes of overload resolution ([[over.best.ics]],
|
| 2259 |
[[over.ics.ref]]) and therefore initialization ([[dcl.init]]) and
|
| 2260 |
explicit casts ([[expr.static.cast]]). A conversion to `void` does
|
|
|
|
| 1 |
# Special member functions <a id="special">[[special]]</a>
|
| 2 |
|
| 3 |
The default constructor ([[class.ctor]]), copy constructor and copy
|
| 4 |
assignment operator ([[class.copy]]), move constructor and move
|
| 5 |
assignment operator ([[class.copy]]), and destructor ([[class.dtor]])
|
| 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 ([[basic.def.odr]]). See [[class.ctor]], [[class.dtor]] and
|
| 12 |
+
[[class.copy]]. — *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, take the address of, or form a pointer to
|
| 24 |
+
member to an implicitly-declared special member function.
|
| 25 |
|
| 26 |
``` cpp
|
| 27 |
struct A { }; // implicitly declared A::operator=
|
| 28 |
struct B : A {
|
| 29 |
B& operator=(const B &);
|
|
|
|
| 32 |
this->A::operator=(s); // well formed
|
| 33 |
return *this;
|
| 34 |
}
|
| 35 |
```
|
| 36 |
|
| 37 |
+
— *end example*]
|
| 38 |
+
|
| 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 (Clause
|
| 45 |
+
[[class.access]]).
|
| 46 |
+
|
| 47 |
+
[*Example 2*: Declaring a constructor `protected` ensures that only
|
| 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 ([[class.abstract]]), its
|
| 53 |
virtual base classes are called its *potentially constructed
|
| 54 |
subobjects*.
|
| 55 |
|
| 56 |
## Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 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:
|
|
|
|
| 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.
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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 {
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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&);
|
|
|
|
| 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
|
|
|
|
| 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);
|
|
|
|
| 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
|
|
|
|
| 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;
|
|
|
|
| 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 |
};
|
|
|
|
| 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:
|
|
|
|
| 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 |
|
|
|
|
| 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]]),
|
|
|
|
| 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 |
};
|
|
|
|
| 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);
|
|
|
|
| 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 |
};
|
|
|
|
| 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.
|
|
|
|
| 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 |
};
|
|
|
|
| 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);
|
|
|
|
| 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);
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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ₒₚₜ ')'
|
|
|
|
| 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 |
};
|
|
|
|
| 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 (
|
|
|
|
| 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);
|
|
|
|
| 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;
|
|
|
|
| 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);
|
|
|
|
| 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:
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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 |
};
|
|
|
|
| 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 |
};
|
|
|
|
| 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);
|
|
|
|
| 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&);
|
|
|
|
| 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 |
};
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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)`;
|
|
|
|
| 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&);
|
|
|
|
| 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
|
|
|
|
| 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;
|
|
|
|
| 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&&);
|
|
|
|
| 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
|
|
|
|
| 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;
|
|
|
|
| 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
|
|
|
|
| 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();
|
|
|
|
| 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();
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|