- tmp/tmpivn6qv63/{from.md → to.md} +498 -530
tmp/tmpivn6qv63/{from.md → to.md}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
## Class members <a id="class.mem">[[class.mem]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
``` bnf
|
| 4 |
member-specification:
|
| 5 |
member-declaration member-specificationₒₚₜ
|
| 6 |
access-specifier ':' member-specificationₒₚₜ
|
| 7 |
```
|
|
@@ -51,27 +53,29 @@ virt-specifier:
|
|
| 51 |
pure-specifier:
|
| 52 |
'=' '0'
|
| 53 |
```
|
| 54 |
|
| 55 |
The *member-specification* in a class definition declares the full set
|
| 56 |
-
of members of the class; no member can be added elsewhere. A
|
| 57 |
-
member* of a class `X` is a member of `X` that was first
|
| 58 |
-
the *member-specification* of `X`, including anonymous
|
| 59 |
-
[[class.union.anon]] and direct members thereof. Members
|
| 60 |
-
data members, member functions [[class.mfct]], nested
|
| 61 |
-
enumerators, and member templates [[temp.mem]] and
|
| 62 |
-
thereof.
|
| 63 |
|
| 64 |
[*Note 1*: A specialization of a static data member template is a
|
| 65 |
static data member. A specialization of a member function template is a
|
| 66 |
member function. A specialization of a member class template is a nested
|
| 67 |
class. — *end note*]
|
| 68 |
|
| 69 |
A *member-declaration* does not declare new members of the class if it
|
| 70 |
is
|
| 71 |
|
| 72 |
- a friend declaration [[class.friend]],
|
|
|
|
|
|
|
| 73 |
- a *static_assert-declaration*,
|
| 74 |
- a *using-declaration* [[namespace.udecl]], or
|
| 75 |
- an *empty-declaration*.
|
| 76 |
|
| 77 |
For any other *member-declaration*, each declared entity that is not an
|
|
@@ -79,23 +83,23 @@ unnamed bit-field [[class.bit]] is a member of the class, and each such
|
|
| 79 |
*member-declaration* shall either declare at least one member name of
|
| 80 |
the class or declare at least one unnamed bit-field.
|
| 81 |
|
| 82 |
A *data member* is a non-function member introduced by a
|
| 83 |
*member-declarator*. A *member function* is a member that is a function.
|
| 84 |
-
Nested types are classes
|
| 85 |
-
|
| 86 |
-
|
| 87 |
*alias-declaration*. The enumerators of an unscoped enumeration
|
| 88 |
[[dcl.enum]] defined in the class are members of the class.
|
| 89 |
|
| 90 |
A data member or member function may be declared `static` in its
|
| 91 |
*member-declaration*, in which case it is a *static member* (see
|
| 92 |
[[class.static]]) (a *static data member* [[class.static.data]] or
|
| 93 |
*static member function* [[class.static.mfct]], respectively) of the
|
| 94 |
class. Any other data member or member function is a *non-static member*
|
| 95 |
-
(a *non-static data member* or *non-static member function*
|
| 96 |
-
[[class.mfct.non
|
| 97 |
|
| 98 |
[*Note 2*: A non-static data member of non-reference type is a member
|
| 99 |
subobject of a class object [[intro.object]]. — *end note*]
|
| 100 |
|
| 101 |
A member shall not be declared twice in the *member-specification*,
|
|
@@ -105,31 +109,36 @@ except that
|
|
| 105 |
defined, and
|
| 106 |
- an enumeration can be introduced with an *opaque-enum-declaration* and
|
| 107 |
later redeclared with an *enum-specifier*.
|
| 108 |
|
| 109 |
[*Note 3*: A single name can denote several member functions provided
|
| 110 |
-
their types are sufficiently different
|
|
|
|
| 111 |
|
| 112 |
-
A
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
- function body [[dcl.fct.def.general]],
|
| 115 |
- default argument [[dcl.fct.default]],
|
|
|
|
| 116 |
- *noexcept-specifier* [[except.spec]], or
|
| 117 |
- default member initializer
|
| 118 |
|
| 119 |
-
within the *member-specification* of the class.
|
| 120 |
|
| 121 |
[*Note 4*: A complete-class context of a nested class is also a
|
| 122 |
complete-class context of any enclosing class, if the nested class is
|
| 123 |
defined within the *member-specification* of the enclosing
|
| 124 |
class. — *end note*]
|
| 125 |
|
| 126 |
-
A class is
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
otherwise it is regarded as incomplete within its own class
|
| 130 |
-
*member-specification*.
|
| 131 |
|
| 132 |
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 133 |
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 134 |
has function type, otherwise it is interpreted as introducing a
|
| 135 |
*brace-or-equal-initializer*.
|
|
@@ -137,12 +146,12 @@ has function type, otherwise it is interpreted as introducing a
|
|
| 137 |
[*Example 1*:
|
| 138 |
|
| 139 |
``` cpp
|
| 140 |
struct S {
|
| 141 |
using T = void();
|
| 142 |
-
T * p = 0; // OK
|
| 143 |
-
virtual T f = 0; // OK
|
| 144 |
};
|
| 145 |
```
|
| 146 |
|
| 147 |
— *end example*]
|
| 148 |
|
|
@@ -172,11 +181,15 @@ data member. (For static data members, see [[class.static.data]]; for
|
|
| 172 |
non-static data members, see [[class.base.init]] and
|
| 173 |
[[dcl.init.aggr]]). A *brace-or-equal-initializer* for a non-static data
|
| 174 |
member specifies a *default member initializer* for the member, and
|
| 175 |
shall not directly or indirectly cause the implicit definition of a
|
| 176 |
defaulted default constructor for the enclosing class or the exception
|
| 177 |
-
specification of that constructor.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
A member shall not be declared with the `extern`
|
| 180 |
*storage-class-specifier*. Within a class definition, a member shall not
|
| 181 |
be declared with the `thread_local` *storage-class-specifier* unless
|
| 182 |
also declared `static`.
|
|
@@ -197,12 +210,12 @@ it shall not appear if the optional *member-declarator-list* is omitted.
|
|
| 197 |
A *virt-specifier-seq* shall contain at most one of each
|
| 198 |
*virt-specifier*. A *virt-specifier-seq* shall appear only in the first
|
| 199 |
declaration of a virtual member function [[class.virtual]].
|
| 200 |
|
| 201 |
The type of a non-static data member shall not be an incomplete type
|
| 202 |
-
[[
|
| 203 |
-
(possibly
|
| 204 |
|
| 205 |
[*Note 5*: In particular, a class `C` cannot contain a non-static
|
| 206 |
member of class `C`, but it can contain a pointer or reference to an
|
| 207 |
object of class `C`. — *end note*]
|
| 208 |
|
|
@@ -241,45 +254,47 @@ object to which `sp` points; `s.left` refers to the `left` subtree
|
|
| 241 |
pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
| 242 |
character of the `tword` member of the `right` subtree of `s`.
|
| 243 |
|
| 244 |
— *end example*]
|
| 245 |
|
| 246 |
-
[*Note 8*: Non-static data members of
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
immediately after each other; so might requirements for space for
|
| 253 |
-
managing virtual functions [[class.virtual]] and virtual base classes
|
| 254 |
[[class.mi]]. — *end note*]
|
| 255 |
|
| 256 |
If `T` is the name of a class, then each of the following shall have a
|
| 257 |
name different from `T`:
|
| 258 |
|
| 259 |
- every static data member of class `T`;
|
| 260 |
-
- every member function of class `T` \[*Note 9*: This restriction does
|
| 261 |
not apply to constructors, which do not have names
|
| 262 |
-
[[class.ctor]] — *end note*]
|
| 263 |
- every member of class `T` that is itself a type;
|
| 264 |
- every member template of class `T`;
|
| 265 |
- every enumerator of every member of class `T` that is an unscoped
|
| 266 |
-
|
| 267 |
- every member of every anonymous union that is a member of class `T`.
|
| 268 |
|
| 269 |
In addition, if class `T` has a user-declared constructor
|
| 270 |
[[class.ctor]], every non-static data member of class `T` shall have a
|
| 271 |
name different from `T`.
|
| 272 |
|
| 273 |
The *common initial sequence* of two standard-layout struct
|
| 274 |
[[class.prop]] types is the longest sequence of non-static data members
|
| 275 |
and bit-fields in declaration order, starting with the first such entity
|
| 276 |
-
in each of the structs, such that
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
[*Example 4*:
|
| 283 |
|
| 284 |
``` cpp
|
| 285 |
struct A { int a; char b; };
|
|
@@ -300,11 +315,12 @@ Two standard-layout struct [[class.prop]] types are *layout-compatible
|
|
| 300 |
classes* if their common initial sequence comprises all members and
|
| 301 |
bit-fields of both classes [[basic.types]].
|
| 302 |
|
| 303 |
Two standard-layout unions are layout-compatible if they have the same
|
| 304 |
number of non-static data members and corresponding non-static data
|
| 305 |
-
members (in any order) have layout-compatible types
|
|
|
|
| 306 |
|
| 307 |
In a standard-layout union with an active member [[class.union]] of
|
| 308 |
struct type `T1`, it is permitted to read a non-static data member `m`
|
| 309 |
of another union member of struct type `T2` provided `m` is part of the
|
| 310 |
common initial sequence of `T1` and `T2`; the behavior is as if the
|
|
@@ -330,55 +346,27 @@ type has undefined behavior [[dcl.type.cv]]. — *end note*]
|
|
| 330 |
If a standard-layout class object has any non-static data members, its
|
| 331 |
address is the same as the address of its first non-static data member
|
| 332 |
if that member is not a bit-field. Its address is also the same as the
|
| 333 |
address of each of its base class subobjects.
|
| 334 |
|
| 335 |
-
[*Note 11*: There
|
| 336 |
standard-layout struct object inserted by an implementation, but not at
|
| 337 |
its beginning, as necessary to achieve appropriate
|
| 338 |
alignment. — *end note*]
|
| 339 |
|
| 340 |
[*Note 12*: The object and its first subobject are
|
| 341 |
-
pointer-interconvertible
|
| 342 |
-
[[expr.static.cast]]
|
| 343 |
|
| 344 |
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
if it is attached to the global module, or it may be defined outside of
|
| 349 |
-
its class definition if it has already been declared but not defined in
|
| 350 |
-
its class definition.
|
| 351 |
|
| 352 |
[*Note 1*: A member function is also inline if it is declared `inline`,
|
| 353 |
`constexpr`, or `consteval`. — *end note*]
|
| 354 |
|
| 355 |
-
A member function definition that appears outside of the class
|
| 356 |
-
definition shall appear in a namespace scope enclosing the class
|
| 357 |
-
definition. Except for member function definitions that appear outside
|
| 358 |
-
of a class definition, and except for explicit specializations of member
|
| 359 |
-
functions of class templates and member function templates [[temp.spec]]
|
| 360 |
-
appearing outside of the class definition, a member function shall not
|
| 361 |
-
be redeclared.
|
| 362 |
-
|
| 363 |
-
[*Note 2*: There can be at most one definition of a non-inline member
|
| 364 |
-
function in a program. There may be more than one inline member function
|
| 365 |
-
definition in a program. See [[basic.def.odr]] and
|
| 366 |
-
[[dcl.inline]]. — *end note*]
|
| 367 |
-
|
| 368 |
-
[*Note 3*: Member functions of a class have the linkage of the name of
|
| 369 |
-
the class. See [[basic.link]]. — *end note*]
|
| 370 |
-
|
| 371 |
-
If the definition of a member function is lexically outside its class
|
| 372 |
-
definition, the member function name shall be qualified by its class
|
| 373 |
-
name using the `::` operator.
|
| 374 |
-
|
| 375 |
-
[*Note 4*: A name used in a member function definition (that is, in the
|
| 376 |
-
*parameter-declaration-clause* including the default arguments
|
| 377 |
-
[[dcl.fct.default]] or in the member function body) is looked up as
|
| 378 |
-
described in [[basic.lookup]]. — *end note*]
|
| 379 |
-
|
| 380 |
[*Example 1*:
|
| 381 |
|
| 382 |
``` cpp
|
| 383 |
struct X {
|
| 384 |
typedef int T;
|
|
@@ -386,30 +374,23 @@ struct X {
|
|
| 386 |
void f(T);
|
| 387 |
};
|
| 388 |
void X::f(T t = count) { }
|
| 389 |
```
|
| 390 |
|
| 391 |
-
The member function `f` of class `X`
|
| 392 |
-
notation `X::f`
|
| 393 |
-
and in the scope of class `X`. In the function
|
| 394 |
-
type `T` refers to the typedef member `T`
|
| 395 |
-
|
| 396 |
-
declared in class `X`.
|
| 397 |
|
| 398 |
— *end example*]
|
| 399 |
|
| 400 |
-
[*Note 5*: A `static` local variable or local type in a member function
|
| 401 |
-
always refers to the same entity, whether or not the member function is
|
| 402 |
-
inline. — *end note*]
|
| 403 |
-
|
| 404 |
-
Previously declared member functions may be mentioned in friend
|
| 405 |
-
declarations.
|
| 406 |
-
|
| 407 |
Member functions of a local class shall be defined inline in their class
|
| 408 |
definition, if they are defined at all.
|
| 409 |
|
| 410 |
-
[*Note
|
| 411 |
|
| 412 |
A member function can be declared (but not defined) using a typedef for
|
| 413 |
a function type. The resulting member function has exactly the same type
|
| 414 |
as it would have if the function declarator were provided explicitly,
|
| 415 |
see [[dcl.fct]]. For example,
|
|
@@ -429,34 +410,29 @@ fvc S::* pmfv3 = &S::memfunc3;
|
|
| 429 |
|
| 430 |
Also see [[temp.arg]].
|
| 431 |
|
| 432 |
— *end note*]
|
| 433 |
|
| 434 |
-
### Non-static member functions <a id="class.mfct.non
|
| 435 |
|
| 436 |
A non-static member function may be called for an object of its class
|
| 437 |
type, or for an object of a class derived [[class.derived]] from its
|
| 438 |
-
class type, using the class member access syntax
|
| 439 |
-
[[over.match.call]]
|
| 440 |
-
directly using the function call syntax
|
| 441 |
-
[[over.match.call]]
|
| 442 |
-
class, or a member thereof, as described below.
|
| 443 |
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
non-static non-type member of some class `C`, and if either the
|
| 454 |
-
*id-expression* is potentially evaluated or `C` is `X` or a base class
|
| 455 |
-
of `X`, the *id-expression* is transformed into a class member access
|
| 456 |
-
expression [[expr.ref]] using `(*this)` [[class.this]] as the
|
| 457 |
-
*postfix-expression* to the left of the `.` operator.
|
| 458 |
|
| 459 |
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 460 |
access expression is ill-formed. — *end note*]
|
| 461 |
|
| 462 |
This transformation does not apply in the template definition context
|
|
@@ -495,113 +471,29 @@ which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
|
| 495 |
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 496 |
not members of the class `tnode` and should be declared elsewhere.[^2]
|
| 497 |
|
| 498 |
— *end example*]
|
| 499 |
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
member function*, a member function declared `volatile` is a *volatile
|
| 505 |
-
member function* and a member function declared `const` `volatile` is a
|
| 506 |
-
*const volatile member function*.
|
| 507 |
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
``` cpp
|
| 511 |
-
struct X {
|
| 512 |
-
void g() const;
|
| 513 |
-
void h() const volatile;
|
| 514 |
-
};
|
| 515 |
-
```
|
| 516 |
-
|
| 517 |
-
`X::g` is a const member function and `X::h` is a const volatile member
|
| 518 |
-
function.
|
| 519 |
-
|
| 520 |
-
— *end example*]
|
| 521 |
-
|
| 522 |
-
A non-static member function may be declared with a *ref-qualifier*
|
| 523 |
-
[[dcl.fct]]; see [[over.match.funcs]].
|
| 524 |
-
|
| 525 |
-
A non-static member function may be declared virtual [[class.virtual]]
|
| 526 |
-
or pure virtual [[class.abstract]].
|
| 527 |
-
|
| 528 |
-
#### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 529 |
-
|
| 530 |
-
In the body of a non-static [[class.mfct]] member function, the keyword
|
| 531 |
-
`this` is a prvalue whose value is a pointer to the object for which the
|
| 532 |
-
function is called. The type of `this` in a member function whose type
|
| 533 |
-
has a *cv-qualifier-seq* cv and whose class is `X` is “pointer to cv
|
| 534 |
-
`X`”.
|
| 535 |
-
|
| 536 |
-
[*Note 1*: Thus in a const member function, the object for which the
|
| 537 |
-
function is called is accessed through a const access
|
| 538 |
-
path. — *end note*]
|
| 539 |
-
|
| 540 |
-
[*Example 1*:
|
| 541 |
-
|
| 542 |
-
``` cpp
|
| 543 |
-
struct s {
|
| 544 |
-
int a;
|
| 545 |
-
int f() const;
|
| 546 |
-
int g() { return a++; }
|
| 547 |
-
int h() const { return a++; } // error
|
| 548 |
-
};
|
| 549 |
-
|
| 550 |
-
int s::f() const { return a; }
|
| 551 |
-
```
|
| 552 |
-
|
| 553 |
-
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 554 |
-
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 555 |
-
in a const member function because `this` is a pointer to `const`; that
|
| 556 |
-
is, `*this` has `const` type.
|
| 557 |
-
|
| 558 |
-
— *end example*]
|
| 559 |
-
|
| 560 |
-
[*Note 2*: Similarly, `volatile` semantics [[dcl.type.cv]] apply in
|
| 561 |
-
volatile member functions when accessing the object and its non-static
|
| 562 |
-
data members. — *end note*]
|
| 563 |
-
|
| 564 |
-
A member function whose type has a *cv-qualifier-seq* *cv1* can be
|
| 565 |
-
called on an object expression [[expr.ref]] of type *cv2* `T` only if
|
| 566 |
-
*cv1* is the same as or more cv-qualified than *cv2*
|
| 567 |
-
[[basic.type.qualifier]].
|
| 568 |
-
|
| 569 |
-
[*Example 2*:
|
| 570 |
-
|
| 571 |
-
``` cpp
|
| 572 |
-
void k(s& x, const s& y) {
|
| 573 |
-
x.f();
|
| 574 |
-
x.g();
|
| 575 |
-
y.f();
|
| 576 |
-
y.g(); // error
|
| 577 |
-
}
|
| 578 |
-
```
|
| 579 |
-
|
| 580 |
-
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 581 |
-
non-const member function, that is, `s::g()` is less-qualified than the
|
| 582 |
-
object expression `y`.
|
| 583 |
-
|
| 584 |
-
— *end example*]
|
| 585 |
-
|
| 586 |
-
[*Note 3*: Constructors and destructors cannot be declared `const`,
|
| 587 |
-
`volatile`, or `const` `volatile`. However, these functions can be
|
| 588 |
-
invoked to create and destroy objects with cv-qualified types; see
|
| 589 |
-
[[class.ctor]] and [[class.dtor]]. — *end note*]
|
| 590 |
|
| 591 |
### Special member functions <a id="special">[[special]]</a>
|
| 592 |
|
| 593 |
Default constructors [[class.default.ctor]], copy constructors, move
|
| 594 |
constructors [[class.copy.ctor]], copy assignment operators, move
|
| 595 |
assignment operators [[class.copy.assign]], and prospective destructors
|
| 596 |
[[class.dtor]] are *special member functions*.
|
| 597 |
|
| 598 |
[*Note 1*: The implementation will implicitly declare these member
|
| 599 |
functions for some class types when the program does not explicitly
|
| 600 |
-
declare them. The implementation will implicitly define them
|
| 601 |
-
|
| 602 |
-
[[expr.const]]. — *end note*]
|
| 603 |
|
| 604 |
An implicitly-declared special member function is declared at the
|
| 605 |
closing `}` of the *class-specifier*. Programs shall not define
|
| 606 |
implicitly-declared special member functions.
|
| 607 |
|
|
@@ -657,38 +549,37 @@ which:
|
|
| 657 |
For a class, its non-static data members, its non-virtual direct base
|
| 658 |
classes, and, if the class is not abstract [[class.abstract]], its
|
| 659 |
virtual base classes are called its *potentially constructed
|
| 660 |
subobjects*.
|
| 661 |
|
| 662 |
-
A defaulted special member function is *constexpr-compatible* if the
|
| 663 |
-
corresponding implicitly-declared special member function would be a
|
| 664 |
-
constexpr function.
|
| 665 |
-
|
| 666 |
### Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 667 |
|
| 668 |
-
|
| 669 |
-
|
|
|
|
|
|
|
| 670 |
|
| 671 |
``` bnf
|
| 672 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 673 |
```
|
| 674 |
|
| 675 |
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 676 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 677 |
parentheses, and the *id-expression* has one of the following forms:
|
| 678 |
|
| 679 |
-
- in a
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
-
|
| 684 |
-
|
| 685 |
-
|
|
|
|
| 686 |
|
| 687 |
Constructors do not have names. In a constructor declaration, each
|
| 688 |
*decl-specifier* in the optional *decl-specifier-seq* shall be `friend`,
|
| 689 |
-
`inline`, `constexpr`, or an *explicit-specifier*.
|
| 690 |
|
| 691 |
[*Example 1*:
|
| 692 |
|
| 693 |
``` cpp
|
| 694 |
struct S {
|
|
@@ -698,18 +589,17 @@ struct S {
|
|
| 698 |
S::S() { } // defines the constructor
|
| 699 |
```
|
| 700 |
|
| 701 |
— *end example*]
|
| 702 |
|
| 703 |
-
A constructor is used to initialize objects of its class type.
|
| 704 |
-
constructors do not have names, they are never found during name lookup;
|
| 705 |
-
however an explicit type conversion using the functional notation
|
| 706 |
-
[[expr.type.conv]] will cause a constructor to be called to initialize
|
| 707 |
-
an object.
|
| 708 |
|
| 709 |
-
[*Note 1*:
|
| 710 |
-
|
|
|
|
|
|
|
|
|
|
| 711 |
|
| 712 |
[*Example 2*:
|
| 713 |
|
| 714 |
``` cpp
|
| 715 |
complex zz = complex(1,2.3);
|
|
@@ -736,15 +626,19 @@ during construction; see [[class.base.init]] and
|
|
| 736 |
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 737 |
`volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
|
| 738 |
not applied on an object under construction. They come into effect when
|
| 739 |
the constructor for the most derived object [[intro.object]] ends.
|
| 740 |
|
| 741 |
-
|
| 742 |
-
|
|
|
|
|
|
|
| 743 |
|
| 744 |
A constructor shall not be a coroutine.
|
| 745 |
|
|
|
|
|
|
|
| 746 |
#### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
|
| 747 |
|
| 748 |
A *default constructor* for a class `X` is a constructor of class `X`
|
| 749 |
for which each parameter that is not a function parameter pack has a
|
| 750 |
default argument (including the case of a constructor with no
|
|
@@ -792,42 +686,33 @@ A default constructor is *trivial* if it is not user-provided and if:
|
|
| 792 |
type (or array thereof), each such class has a trivial default
|
| 793 |
constructor.
|
| 794 |
|
| 795 |
Otherwise, the default constructor is *non-trivial*.
|
| 796 |
|
| 797 |
-
|
| 798 |
-
*implicitly defined* when it is odr-used [[basic.def.odr]] to create an
|
| 799 |
-
object of its class type [[intro.object]], when it is needed for
|
| 800 |
-
constant evaluation [[expr.const]], or when it is explicitly defaulted
|
| 801 |
-
after its first declaration. The implicitly-defined default constructor
|
| 802 |
performs the set of initializations of the class that would be performed
|
| 803 |
by a user-written default constructor for that class with no
|
| 804 |
*ctor-initializer* [[class.base.init]] and an empty
|
| 805 |
*compound-statement*. If that user-written default constructor would be
|
| 806 |
ill-formed, the program is ill-formed. If that user-written default
|
| 807 |
-
constructor would
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
|
| 813 |
[*Note 1*: An implicitly-declared default constructor has an exception
|
| 814 |
specification [[except.spec]]. An explicitly-defaulted definition might
|
| 815 |
have an implicit exception specification, see
|
| 816 |
[[dcl.fct.def]]. — *end note*]
|
| 817 |
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
[[
|
| 822 |
-
duration [[basic.stc.dynamic]] created by a *new-expression* in which
|
| 823 |
-
the *new-initializer* is omitted [[expr.new]], or are called when the
|
| 824 |
-
explicit type conversion syntax [[expr.type.conv]] is used. A program is
|
| 825 |
-
ill-formed if the default constructor for an object is implicitly used
|
| 826 |
-
and the constructor is not accessible [[class.access]].
|
| 827 |
|
| 828 |
-
[*Note
|
| 829 |
constructors for base classes and non-static data members are called and
|
| 830 |
describes how arguments can be specified for the calls to these
|
| 831 |
constructors. — *end note*]
|
| 832 |
|
| 833 |
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
|
@@ -876,11 +761,11 @@ Y e = d; // calls Y(const Y&)
|
|
| 876 |
|
| 877 |
— *end example*]
|
| 878 |
|
| 879 |
[*Note 1*:
|
| 880 |
|
| 881 |
-
All forms of copy/move constructor
|
| 882 |
|
| 883 |
[*Example 3*:
|
| 884 |
|
| 885 |
``` cpp
|
| 886 |
struct X {
|
|
@@ -942,25 +827,26 @@ void h() {
|
|
| 942 |
|
| 943 |
If the class definition does not explicitly declare a copy constructor,
|
| 944 |
a non-explicit one is declared *implicitly*. If the class definition
|
| 945 |
declares a move constructor or move assignment operator, the implicitly
|
| 946 |
declared copy constructor is defined as deleted; otherwise, it is
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
| 950 |
|
| 951 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 952 |
form
|
| 953 |
|
| 954 |
``` cpp
|
| 955 |
X::X(const X&)
|
| 956 |
```
|
| 957 |
|
| 958 |
if each potentially constructed subobject of a class type `M` (or array
|
| 959 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 960 |
-
`M&` or `const` `volatile` `M&`.[^3]
|
| 961 |
-
|
|
|
|
| 962 |
|
| 963 |
``` cpp
|
| 964 |
X::X(X&)
|
| 965 |
```
|
| 966 |
|
|
@@ -973,11 +859,11 @@ if and only if
|
|
| 973 |
- `X` does not have a user-declared move assignment operator, and
|
| 974 |
- `X` does not have a user-declared destructor.
|
| 975 |
|
| 976 |
[*Note 3*: When the move constructor is not implicitly declared or
|
| 977 |
explicitly supplied, expressions that otherwise would have invoked the
|
| 978 |
-
move constructor
|
| 979 |
|
| 980 |
The implicitly-declared move constructor for class `X` will have the
|
| 981 |
form
|
| 982 |
|
| 983 |
``` cpp
|
|
@@ -986,24 +872,24 @@ X::X(X&&)
|
|
| 986 |
|
| 987 |
An implicitly-declared copy/move constructor is an inline public member
|
| 988 |
of its class. A defaulted copy/move constructor for a class `X` is
|
| 989 |
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 990 |
|
| 991 |
-
- a potentially constructed subobject type `M` (or array thereof)
|
| 992 |
-
cannot be copied/moved because overload resolution
|
| 993 |
-
applied to find `M`’s corresponding constructor,
|
| 994 |
-
ambiguity or a function that is deleted or inaccessible
|
| 995 |
-
defaulted constructor,
|
| 996 |
- a variant member whose corresponding constructor as selected by
|
| 997 |
overload resolution is non-trivial,
|
| 998 |
- any potentially constructed subobject of a type with a destructor that
|
| 999 |
is deleted or inaccessible from the defaulted constructor, or,
|
| 1000 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 1001 |
type.
|
| 1002 |
|
| 1003 |
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 1004 |
-
ignored by overload resolution
|
| 1005 |
constructor would otherwise interfere with initialization from an rvalue
|
| 1006 |
which can use the copy constructor instead. — *end note*]
|
| 1007 |
|
| 1008 |
A copy/move constructor for class `X` is trivial if it is not
|
| 1009 |
user-provided and if:
|
|
@@ -1016,22 +902,17 @@ user-provided and if:
|
|
| 1016 |
thereof), the constructor selected to copy/move that member is
|
| 1017 |
trivial;
|
| 1018 |
|
| 1019 |
otherwise the copy/move constructor is *non-trivial*.
|
| 1020 |
|
| 1021 |
-
A copy/move constructor that is defaulted and not defined as deleted is
|
| 1022 |
-
*implicitly defined* when it is odr-used [[basic.def.odr]], when it is
|
| 1023 |
-
needed for constant evaluation [[expr.const]], or when it is explicitly
|
| 1024 |
-
defaulted after its first declaration.
|
| 1025 |
-
|
| 1026 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 1027 |
-
implementation elided its odr-use
|
| 1028 |
-
[[class.temporary]]
|
| 1029 |
|
| 1030 |
-
If
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
|
| 1034 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 1035 |
defined, all non-user-provided copy/move constructors for its
|
| 1036 |
potentially constructed subobjects are implicitly defined.
|
| 1037 |
|
|
@@ -1060,27 +941,27 @@ to its type:
|
|
| 1060 |
|
| 1061 |
Virtual base class subobjects shall be initialized only once by the
|
| 1062 |
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 1063 |
|
| 1064 |
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 1065 |
-
object representation [[
|
| 1066 |
-
within [[intro.object]] the object that is the source of
|
| 1067 |
-
corresponding object o nested within the destination is
|
| 1068 |
-
the object is a subobject) or created (otherwise), and
|
| 1069 |
-
begins before the copy is performed.
|
| 1070 |
|
| 1071 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 1072 |
|
| 1073 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 1074 |
non-static non-template member function of class `X` with exactly one
|
| 1075 |
-
parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
|
| 1076 |
`const volatile X&`.[^4]
|
| 1077 |
|
| 1078 |
[*Note 1*: An overloaded assignment operator must be declared to have
|
| 1079 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 1080 |
|
| 1081 |
-
[*Note 2*: More than one form of copy assignment operator
|
| 1082 |
declared for a class. — *end note*]
|
| 1083 |
|
| 1084 |
[*Note 3*:
|
| 1085 |
|
| 1086 |
If a class `X` only has a copy assignment operator with a parameter of
|
|
@@ -1106,12 +987,12 @@ void f() {
|
|
| 1106 |
— *end note*]
|
| 1107 |
|
| 1108 |
If the class definition does not explicitly declare a copy assignment
|
| 1109 |
operator, one is declared *implicitly*. If the class definition declares
|
| 1110 |
a move constructor or move assignment operator, the implicitly declared
|
| 1111 |
-
copy assignment operator is defined as deleted; otherwise, it is
|
| 1112 |
-
|
| 1113 |
has a user-declared copy constructor or a user-declared destructor
|
| 1114 |
[[depr.impldec]]. The implicitly-declared copy assignment operator for a
|
| 1115 |
class `X` will have the form
|
| 1116 |
|
| 1117 |
``` cpp
|
|
@@ -1133,17 +1014,18 @@ the form
|
|
| 1133 |
``` cpp
|
| 1134 |
X& X::operator=(X&)
|
| 1135 |
```
|
| 1136 |
|
| 1137 |
A user-declared move assignment operator `X::operator=` is a non-static
|
| 1138 |
-
non-template member function of class `X` with exactly one
|
| 1139 |
-
type `X&&`, `const X&&`, `volatile X&&`, or
|
|
|
|
| 1140 |
|
| 1141 |
[*Note 4*: An overloaded assignment operator must be declared to have
|
| 1142 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 1143 |
|
| 1144 |
-
[*Note 5*: More than one form of move assignment operator
|
| 1145 |
declared for a class. — *end note*]
|
| 1146 |
|
| 1147 |
If the definition of a class `X` does not explicitly declare a move
|
| 1148 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1149 |
only if
|
|
@@ -1184,14 +1066,12 @@ have the form
|
|
| 1184 |
``` cpp
|
| 1185 |
X& X::operator=(X&&)
|
| 1186 |
```
|
| 1187 |
|
| 1188 |
The implicitly-declared copy/move assignment operator for class `X` has
|
| 1189 |
-
the return type `X&`
|
| 1190 |
-
operator is
|
| 1191 |
-
implicitly-declared copy/move assignment operator is an inline public
|
| 1192 |
-
member of its class.
|
| 1193 |
|
| 1194 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 1195 |
deleted if `X` has:
|
| 1196 |
|
| 1197 |
- a variant member with a non-trivial corresponding assignment operator
|
|
@@ -1205,23 +1085,23 @@ deleted if `X` has:
|
|
| 1205 |
corresponding assignment operator, results in an ambiguity or a
|
| 1206 |
function that is deleted or inaccessible from the defaulted assignment
|
| 1207 |
operator.
|
| 1208 |
|
| 1209 |
[*Note 6*: A defaulted move assignment operator that is defined as
|
| 1210 |
-
deleted is ignored by overload resolution
|
| 1211 |
-
[[over.over]]
|
| 1212 |
|
| 1213 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1214 |
class if not declared by the user, a base class copy/move assignment
|
| 1215 |
operator is always hidden by the corresponding assignment operator of a
|
| 1216 |
-
derived class [[over.ass]].
|
| 1217 |
-
|
| 1218 |
-
|
| 1219 |
-
|
| 1220 |
-
|
| 1221 |
-
|
| 1222 |
-
|
| 1223 |
|
| 1224 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1225 |
user-provided and if:
|
| 1226 |
|
| 1227 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
|
@@ -1232,31 +1112,19 @@ user-provided and if:
|
|
| 1232 |
thereof), the assignment operator selected to copy/move that member is
|
| 1233 |
trivial;
|
| 1234 |
|
| 1235 |
otherwise the copy/move assignment operator is *non-trivial*.
|
| 1236 |
|
| 1237 |
-
|
| 1238 |
-
|
| 1239 |
-
[[basic.def.odr]] (e.g., when it is selected by overload resolution to
|
| 1240 |
-
assign to an object of its class type), when it is needed for constant
|
| 1241 |
-
evaluation [[expr.const]], or when it is explicitly defaulted after its
|
| 1242 |
-
first declaration. The implicitly-defined copy/move assignment operator
|
| 1243 |
-
is `constexpr` if
|
| 1244 |
-
|
| 1245 |
-
- `X` is a literal type, and
|
| 1246 |
-
- the assignment operator selected to copy/move each direct base class
|
| 1247 |
-
subobject is a constexpr function, and
|
| 1248 |
-
- for each non-static data member of `X` that is of class type (or array
|
| 1249 |
-
thereof), the assignment operator selected to copy/move that member is
|
| 1250 |
-
a constexpr function.
|
| 1251 |
|
| 1252 |
Before the defaulted copy/move assignment operator for a class is
|
| 1253 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1254 |
for its direct base classes and its non-static data members are
|
| 1255 |
implicitly defined.
|
| 1256 |
|
| 1257 |
-
[*Note
|
| 1258 |
implied exception specification [[except.spec]]. — *end note*]
|
| 1259 |
|
| 1260 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 1261 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 1262 |
The direct base classes of `X` are assigned first, in the order of their
|
|
@@ -1294,21 +1162,27 @@ It is unspecified whether the virtual base class subobject `V` is
|
|
| 1294 |
assigned twice by the implicitly-defined copy/move assignment operator
|
| 1295 |
for `C`.
|
| 1296 |
|
| 1297 |
— *end example*]
|
| 1298 |
|
| 1299 |
-
The implicitly-defined copy assignment operator for a union `X`
|
| 1300 |
-
the object representation [[
|
| 1301 |
-
destination of the assignment are not the same object,
|
| 1302 |
-
object nested within [[intro.object]] the object that is
|
| 1303 |
-
the copy, a corresponding object o nested within the
|
| 1304 |
-
created, and the lifetime of o begins before the copy is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1305 |
|
| 1306 |
### Destructors <a id="class.dtor">[[class.dtor]]</a>
|
| 1307 |
|
| 1308 |
-
A
|
| 1309 |
-
|
|
|
|
| 1310 |
|
| 1311 |
``` bnf
|
| 1312 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1313 |
```
|
| 1314 |
|
|
@@ -1319,13 +1193,13 @@ parentheses, and the *id-expression* has one of the following forms:
|
|
| 1319 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 1320 |
of a class or class template but is not a friend declaration
|
| 1321 |
[[class.friend]], the *id-expression* is `~`*class-name* and the
|
| 1322 |
*class-name* is the injected-class-name [[class.pre]] of the
|
| 1323 |
immediately-enclosing entity or
|
| 1324 |
-
-
|
| 1325 |
-
*
|
| 1326 |
-
|
| 1327 |
|
| 1328 |
A prospective destructor shall take no arguments [[dcl.fct]]. Each
|
| 1329 |
*decl-specifier* of the *decl-specifier-seq* of a prospective destructor
|
| 1330 |
declaration (if any) shall be `friend`, `inline`, `virtual`,
|
| 1331 |
`constexpr`, or `consteval`.
|
|
@@ -1345,21 +1219,25 @@ the form
|
|
| 1345 |
At the end of the definition of a class, overload resolution is
|
| 1346 |
performed among the prospective destructors declared in that class with
|
| 1347 |
an empty argument list to select the *destructor* for the class, also
|
| 1348 |
known as the *selected destructor*. The program is ill-formed if
|
| 1349 |
overload resolution fails. Destructor selection does not constitute a
|
| 1350 |
-
reference to, or odr-use [[
|
| 1351 |
and in particular, the selected destructor may be deleted
|
| 1352 |
[[dcl.fct.def.delete]].
|
| 1353 |
|
| 1354 |
-
The address of a destructor shall not be taken.
|
| 1355 |
-
invoked for a `const`, `volatile` or `const` `volatile` object. `const`
|
| 1356 |
-
and `volatile` semantics [[dcl.type.cv]] are not applied on an object
|
| 1357 |
-
under destruction. They stop being in effect when the destructor for the
|
| 1358 |
-
most derived object [[intro.object]] starts.
|
| 1359 |
|
| 1360 |
-
[*Note 1*: A
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1361 |
*noexcept-specifier* has the same exception specification as if it had
|
| 1362 |
been implicitly declared [[except.spec]]. — *end note*]
|
| 1363 |
|
| 1364 |
A defaulted destructor for a class `X` is defined as deleted if:
|
| 1365 |
|
|
@@ -1372,37 +1250,31 @@ A defaulted destructor for a class `X` is defined as deleted if:
|
|
| 1372 |
function results in an ambiguity or in a function that is deleted or
|
| 1373 |
inaccessible from the defaulted destructor.
|
| 1374 |
|
| 1375 |
A destructor is trivial if it is not user-provided and if:
|
| 1376 |
|
| 1377 |
-
- the destructor is not
|
| 1378 |
- all of the direct base classes of its class have trivial destructors,
|
| 1379 |
and
|
| 1380 |
- for all of the non-static data members of its class that are of class
|
| 1381 |
type (or array thereof), each such class has a trivial destructor.
|
| 1382 |
|
| 1383 |
Otherwise, the destructor is *non-trivial*.
|
| 1384 |
|
| 1385 |
-
A defaulted destructor is a constexpr destructor if it
|
| 1386 |
-
|
| 1387 |
-
|
| 1388 |
-
A destructor that is defaulted and not defined as deleted is *implicitly
|
| 1389 |
-
defined* when it is odr-used [[basic.def.odr]] or when it is explicitly
|
| 1390 |
-
defaulted after its first declaration.
|
| 1391 |
|
| 1392 |
Before a defaulted destructor for a class is implicitly defined, all the
|
| 1393 |
non-user-provided destructors for its base classes and its non-static
|
| 1394 |
data members are implicitly defined.
|
| 1395 |
|
| 1396 |
-
A prospective destructor can be declared `virtual` [[class.virtual]]
|
| 1397 |
-
|
| 1398 |
-
virtual and any objects of that class or any derived class are
|
| 1399 |
-
in the program, the destructor shall be defined.
|
| 1400 |
-
class with a virtual destructor, its destructor (whether user- or
|
| 1401 |
-
implicitly-declared) is virtual.
|
| 1402 |
|
| 1403 |
-
[*Note
|
| 1404 |
during destruction; see [[class.cdtor]]. — *end note*]
|
| 1405 |
|
| 1406 |
After executing the body of the destructor and destroying any objects
|
| 1407 |
with automatic storage duration allocated within the body, a destructor
|
| 1408 |
for class `X` calls the destructors for `X`’s direct non-variant
|
|
@@ -1410,53 +1282,56 @@ non-static data members, the destructors for `X`’s non-virtual direct
|
|
| 1410 |
base classes and, if `X` is the most derived class [[class.base.init]],
|
| 1411 |
its destructor calls the destructors for `X`’s virtual base classes. All
|
| 1412 |
destructors are called as if they were referenced with a qualified name,
|
| 1413 |
that is, ignoring any possible virtual overriding destructors in more
|
| 1414 |
derived classes. Bases and members are destroyed in the reverse order of
|
| 1415 |
-
the completion of their constructor (see [[class.base.init]]).
|
| 1416 |
-
|
| 1417 |
-
|
| 1418 |
-
|
| 1419 |
-
|
| 1420 |
-
|
|
|
|
|
|
|
|
|
|
| 1421 |
|
| 1422 |
A destructor is invoked implicitly
|
| 1423 |
|
| 1424 |
- for a constructed object with static storage duration
|
| 1425 |
[[basic.stc.static]] at program termination [[basic.start.term]],
|
| 1426 |
- for a constructed object with thread storage duration
|
| 1427 |
[[basic.stc.thread]] at thread exit,
|
| 1428 |
- for a constructed object with automatic storage duration
|
| 1429 |
[[basic.stc.auto]] when the block in which an object is created exits
|
| 1430 |
[[stmt.dcl]],
|
| 1431 |
-
- for a constructed temporary object when its lifetime ends
|
| 1432 |
-
[[conv.rval]], [[class.temporary]]
|
| 1433 |
|
| 1434 |
In each case, the context of the invocation is the context of the
|
| 1435 |
construction of the object. A destructor may also be invoked implicitly
|
| 1436 |
through use of a *delete-expression* [[expr.delete]] for a constructed
|
| 1437 |
object allocated by a *new-expression* [[expr.new]]; the context of the
|
| 1438 |
invocation is the *delete-expression*.
|
| 1439 |
|
| 1440 |
-
[*Note
|
| 1441 |
of which the destructor is invoked. — *end note*]
|
| 1442 |
|
| 1443 |
A destructor can also be invoked explicitly. A destructor is
|
| 1444 |
*potentially invoked* if it is invoked or as specified in [[expr.new]],
|
| 1445 |
[[stmt.return]], [[dcl.init.aggr]], [[class.base.init]], and
|
| 1446 |
[[except.throw]]. A program is ill-formed if a destructor that is
|
| 1447 |
potentially invoked is deleted or not accessible from the context of the
|
| 1448 |
invocation.
|
| 1449 |
|
| 1450 |
At the point of definition of a virtual destructor (including an
|
| 1451 |
-
implicit definition
|
| 1452 |
-
|
| 1453 |
-
|
| 1454 |
-
|
| 1455 |
-
|
| 1456 |
|
| 1457 |
-
[*Note
|
| 1458 |
the dynamic type of an object is available for the *delete-expression*
|
| 1459 |
[[class.free]]. — *end note*]
|
| 1460 |
|
| 1461 |
In an explicit destructor call, the destructor is specified by a `~`
|
| 1462 |
followed by a *type-name* or *decltype-specifier* that denotes the
|
|
@@ -1464,11 +1339,11 @@ destructor’s class type. The invocation of a destructor is subject to
|
|
| 1464 |
the usual rules for member functions [[class.mfct]]; that is, if the
|
| 1465 |
object is not of the destructor’s class type and not of a class derived
|
| 1466 |
from the destructor’s class type (including when the destructor is
|
| 1467 |
invoked via a null pointer value), the program has undefined behavior.
|
| 1468 |
|
| 1469 |
-
[*Note
|
| 1470 |
destructor; see [[expr.delete]]. — *end note*]
|
| 1471 |
|
| 1472 |
[*Example 1*:
|
| 1473 |
|
| 1474 |
``` cpp
|
|
@@ -1492,17 +1367,17 @@ void f() {
|
|
| 1492 |
}
|
| 1493 |
```
|
| 1494 |
|
| 1495 |
— *end example*]
|
| 1496 |
|
| 1497 |
-
[*Note
|
| 1498 |
member access operator [[expr.ref]] or a *qualified-id*
|
| 1499 |
[[expr.prim.id.qual]]; in particular, the *unary-expression* `~X()` in a
|
| 1500 |
member function is not an explicit destructor call
|
| 1501 |
[[expr.unary.op]]. — *end note*]
|
| 1502 |
|
| 1503 |
-
[*Note
|
| 1504 |
|
| 1505 |
Explicit calls of destructors are rarely needed. One use of such calls
|
| 1506 |
is for objects placed at specific addresses using a placement
|
| 1507 |
*new-expression*. Such use of explicit placement and destruction of
|
| 1508 |
objects can be necessary to cope with dedicated hardware resources and
|
|
@@ -1524,20 +1399,20 @@ void g() { // rare, specialized use:
|
|
| 1524 |
}
|
| 1525 |
```
|
| 1526 |
|
| 1527 |
— *end note*]
|
| 1528 |
|
| 1529 |
-
Once a destructor is invoked for an object, the object
|
| 1530 |
the behavior is undefined if the destructor is invoked for an object
|
| 1531 |
whose lifetime has ended [[basic.life]].
|
| 1532 |
|
| 1533 |
[*Example 2*: If the destructor for an object with automatic storage
|
| 1534 |
duration is explicitly invoked, and the block is subsequently left in a
|
| 1535 |
manner that would ordinarily invoke implicit destruction of the object,
|
| 1536 |
the behavior is undefined. — *end example*]
|
| 1537 |
|
| 1538 |
-
[*Note
|
| 1539 |
|
| 1540 |
The notation for explicit call of a destructor can be used for any
|
| 1541 |
scalar type name [[expr.prim.id.dtor]]. Allowing this makes it possible
|
| 1542 |
to write code without having to know if a destructor exists for a given
|
| 1543 |
type. For example:
|
|
@@ -1552,19 +1427,21 @@ p->I::~I();
|
|
| 1552 |
|
| 1553 |
A destructor shall not be a coroutine.
|
| 1554 |
|
| 1555 |
### Conversions <a id="class.conv">[[class.conv]]</a>
|
| 1556 |
|
|
|
|
|
|
|
| 1557 |
Type conversions of class objects can be specified by constructors and
|
| 1558 |
by conversion functions. These conversions are called *user-defined
|
| 1559 |
conversions* and are used for implicit type conversions [[conv]], for
|
| 1560 |
-
initialization [[dcl.init]], and for explicit type conversions
|
| 1561 |
-
[[expr.type.conv]], [[expr.cast]], [[expr.static.cast]]
|
| 1562 |
|
| 1563 |
-
User-defined conversions are applied only where they are unambiguous
|
| 1564 |
-
[[class.member.lookup]], [[class.conv.fct]]
|
| 1565 |
-
|
| 1566 |
ambiguity resolution [[basic.lookup]].
|
| 1567 |
|
| 1568 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 1569 |
conversions in function calls as well as examples below. — *end note*]
|
| 1570 |
|
|
@@ -1582,36 +1459,11 @@ struct Y {
|
|
| 1582 |
operator X();
|
| 1583 |
};
|
| 1584 |
|
| 1585 |
Y a;
|
| 1586 |
int b = a; // error: no viable conversion (a.operator X().operator int() not considered)
|
| 1587 |
-
int c = X(a); // OK
|
| 1588 |
-
```
|
| 1589 |
-
|
| 1590 |
-
— *end example*]
|
| 1591 |
-
|
| 1592 |
-
User-defined conversions are used implicitly only if they are
|
| 1593 |
-
unambiguous. A conversion function in a derived class does not hide a
|
| 1594 |
-
conversion function in a base class unless the two functions convert to
|
| 1595 |
-
the same type. Function overload resolution [[over.match.best]] selects
|
| 1596 |
-
the best conversion function to perform the conversion.
|
| 1597 |
-
|
| 1598 |
-
[*Example 2*:
|
| 1599 |
-
|
| 1600 |
-
``` cpp
|
| 1601 |
-
struct X {
|
| 1602 |
-
operator int();
|
| 1603 |
-
};
|
| 1604 |
-
|
| 1605 |
-
struct Y : X {
|
| 1606 |
-
operator char();
|
| 1607 |
-
};
|
| 1608 |
-
|
| 1609 |
-
void f(Y& a) {
|
| 1610 |
-
if (a) { // error: ambiguous between X::operator int() and Y::operator char()
|
| 1611 |
-
}
|
| 1612 |
-
}
|
| 1613 |
```
|
| 1614 |
|
| 1615 |
— *end example*]
|
| 1616 |
|
| 1617 |
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
|
@@ -1642,13 +1494,13 @@ void f(X arg) {
|
|
| 1642 |
|
| 1643 |
[*Note 1*:
|
| 1644 |
|
| 1645 |
An explicit constructor constructs objects just like non-explicit
|
| 1646 |
constructors, but does so only where the direct-initialization syntax
|
| 1647 |
-
[[dcl.init]] or where casts
|
| 1648 |
explicitly used; see also [[over.match.copy]]. A default constructor
|
| 1649 |
-
|
| 1650 |
perform default-initialization or value-initialization [[dcl.init]].
|
| 1651 |
|
| 1652 |
[*Example 2*:
|
| 1653 |
|
| 1654 |
``` cpp
|
|
@@ -1656,19 +1508,19 @@ struct Z {
|
|
| 1656 |
explicit Z();
|
| 1657 |
explicit Z(int);
|
| 1658 |
explicit Z(int, int);
|
| 1659 |
};
|
| 1660 |
|
| 1661 |
-
Z a; // OK
|
| 1662 |
-
Z b{}; // OK
|
| 1663 |
Z c = {}; // error: copy-list-initialization
|
| 1664 |
Z a1 = 1; // error: no implicit conversion
|
| 1665 |
-
Z a3 = Z(1); // OK
|
| 1666 |
-
Z a2(1); // OK
|
| 1667 |
-
Z* p = new Z(1); // OK
|
| 1668 |
-
Z a4 = (Z)1; // OK
|
| 1669 |
-
Z a5 = static_cast<Z>(1); // OK
|
| 1670 |
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 1671 |
```
|
| 1672 |
|
| 1673 |
— *end example*]
|
| 1674 |
|
|
@@ -1676,18 +1528,15 @@ Z a6 = { 3, 4 }; // error: no implicit conversion
|
|
| 1676 |
|
| 1677 |
A non-explicit copy/move constructor [[class.copy.ctor]] is a converting
|
| 1678 |
constructor.
|
| 1679 |
|
| 1680 |
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 1681 |
-
explicit constructor; it
|
| 1682 |
conversions. — *end note*]
|
| 1683 |
|
| 1684 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 1685 |
|
| 1686 |
-
A member function of a class `X` having no parameters with a name of the
|
| 1687 |
-
form
|
| 1688 |
-
|
| 1689 |
``` bnf
|
| 1690 |
conversion-function-id:
|
| 1691 |
operator conversion-type-id
|
| 1692 |
```
|
| 1693 |
|
|
@@ -1699,20 +1548,44 @@ conversion-type-id:
|
|
| 1699 |
``` bnf
|
| 1700 |
conversion-declarator:
|
| 1701 |
ptr-operator conversion-declaratorₒₚₜ
|
| 1702 |
```
|
| 1703 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1704 |
specifies a conversion from `X` to the type specified by the
|
| 1705 |
-
*conversion-type-id*
|
| 1706 |
-
|
| 1707 |
-
(if any) shall be
|
| 1708 |
-
|
| 1709 |
-
|
| 1710 |
-
|
| 1711 |
-
|
| 1712 |
-
|
| 1713 |
-
cv
|
|
|
|
|
|
|
|
|
|
| 1714 |
|
| 1715 |
[*Example 1*:
|
| 1716 |
|
| 1717 |
``` cpp
|
| 1718 |
struct X {
|
|
@@ -1744,13 +1617,13 @@ class Y { };
|
|
| 1744 |
struct Z {
|
| 1745 |
explicit operator Y() const;
|
| 1746 |
};
|
| 1747 |
|
| 1748 |
void h(Z z) {
|
| 1749 |
-
Y y1(z); // OK
|
| 1750 |
Y y2 = z; // error: no conversion function candidate for copy-initialization
|
| 1751 |
-
Y y3 = (Y)z; // OK
|
| 1752 |
}
|
| 1753 |
|
| 1754 |
void g(X a, X b) {
|
| 1755 |
int i = (a) ? 1+a : 0;
|
| 1756 |
int j = (a&&b) ? a+b : i;
|
|
@@ -1793,18 +1666,47 @@ operator int [[noreturn]] (); // error: noreturn attribute applied to a type
|
|
| 1793 |
|
| 1794 |
— *end example*]
|
| 1795 |
|
| 1796 |
— *end note*]
|
| 1797 |
|
| 1798 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1799 |
|
| 1800 |
Conversion functions can be virtual.
|
| 1801 |
|
| 1802 |
A conversion function template shall not have a deduced return type
|
| 1803 |
[[dcl.spec.auto]].
|
| 1804 |
|
| 1805 |
-
[*Example
|
| 1806 |
|
| 1807 |
``` cpp
|
| 1808 |
struct S {
|
| 1809 |
operator auto() const { return 10; } // OK
|
| 1810 |
template<class T>
|
|
@@ -1814,10 +1716,12 @@ struct S {
|
|
| 1814 |
|
| 1815 |
— *end example*]
|
| 1816 |
|
| 1817 |
### Static members <a id="class.static">[[class.static]]</a>
|
| 1818 |
|
|
|
|
|
|
|
| 1819 |
A static member `s` of class `X` may be referred to using the
|
| 1820 |
*qualified-id* expression `X::s`; it is not necessary to use the class
|
| 1821 |
member access syntax [[expr.ref]] to refer to a static member. A static
|
| 1822 |
member may be referred to using the class member access syntax, in which
|
| 1823 |
case the object expression is evaluated.
|
|
@@ -1829,39 +1733,17 @@ struct process {
|
|
| 1829 |
static void reschedule();
|
| 1830 |
};
|
| 1831 |
process& g();
|
| 1832 |
|
| 1833 |
void f() {
|
| 1834 |
-
process::reschedule(); // OK
|
| 1835 |
g().reschedule(); // g() is called
|
| 1836 |
}
|
| 1837 |
```
|
| 1838 |
|
| 1839 |
— *end example*]
|
| 1840 |
|
| 1841 |
-
A static member may be referred to directly in the scope of its class or
|
| 1842 |
-
in the scope of a class derived [[class.derived]] from its class; in
|
| 1843 |
-
this case, the static member is referred to as if a *qualified-id*
|
| 1844 |
-
expression was used, with the *nested-name-specifier* of the
|
| 1845 |
-
*qualified-id* naming the class scope from which the static member is
|
| 1846 |
-
referenced.
|
| 1847 |
-
|
| 1848 |
-
[*Example 2*:
|
| 1849 |
-
|
| 1850 |
-
``` cpp
|
| 1851 |
-
int g();
|
| 1852 |
-
struct X {
|
| 1853 |
-
static int g();
|
| 1854 |
-
};
|
| 1855 |
-
struct Y : X {
|
| 1856 |
-
static int i;
|
| 1857 |
-
};
|
| 1858 |
-
int Y::i = g(); // equivalent to Y::g();
|
| 1859 |
-
```
|
| 1860 |
-
|
| 1861 |
-
— *end example*]
|
| 1862 |
-
|
| 1863 |
Static members obey the usual class member access rules
|
| 1864 |
[[class.access]]. When used in the declaration of a class member, the
|
| 1865 |
`static` specifier shall only be used in the member declarations that
|
| 1866 |
appear within the *member-specification* of the class definition.
|
| 1867 |
|
|
@@ -1872,16 +1754,12 @@ namespace scope. — *end note*]
|
|
| 1872 |
|
| 1873 |
[*Note 1*: The rules described in [[class.mfct]] apply to static
|
| 1874 |
member functions. — *end note*]
|
| 1875 |
|
| 1876 |
[*Note 2*: A static member function does not have a `this` pointer
|
| 1877 |
-
[[
|
| 1878 |
-
|
| 1879 |
-
A static member function shall not be `virtual`. There shall not be a
|
| 1880 |
-
static and a non-static member function with the same name and the same
|
| 1881 |
-
parameter types [[over.load]]. A static member function shall not be
|
| 1882 |
-
declared `const`, `volatile`, or `const volatile`.
|
| 1883 |
|
| 1884 |
#### Static data members <a id="class.static.data">[[class.static.data]]</a>
|
| 1885 |
|
| 1886 |
A static data member is not part of the subobjects of a class. If a
|
| 1887 |
static data member is declared `thread_local` there is one copy of the
|
|
@@ -1894,17 +1772,14 @@ member shall not be a direct member [[class.mem]] of an unnamed
|
|
| 1894 |
[[class.pre]] or local [[class.local]] class or of a (possibly
|
| 1895 |
indirectly) nested class [[class.nest]] thereof.
|
| 1896 |
|
| 1897 |
The declaration of a non-inline static data member in its class
|
| 1898 |
definition is not a definition and may be of an incomplete type other
|
| 1899 |
-
than cv `void`.
|
| 1900 |
-
|
| 1901 |
-
|
| 1902 |
-
|
| 1903 |
-
class name using the `::` operator. The *initializer* expression in the
|
| 1904 |
-
definition of a static data member is in the scope of its class
|
| 1905 |
-
[[basic.scope.class]].
|
| 1906 |
|
| 1907 |
[*Example 1*:
|
| 1908 |
|
| 1909 |
``` cpp
|
| 1910 |
class process {
|
|
@@ -1914,20 +1789,20 @@ class process {
|
|
| 1914 |
|
| 1915 |
process* process::running = get_main();
|
| 1916 |
process* process::run_chain = running;
|
| 1917 |
```
|
| 1918 |
|
| 1919 |
-
The static data member `run_chain` of class `process`
|
| 1920 |
-
global scope; the notation `process::run_chain`
|
| 1921 |
-
member `run_chain` is a member of class `process` and in the
|
| 1922 |
-
class `process`. In the static data member definition, the
|
| 1923 |
-
expression refers to the static data member `running` of
|
| 1924 |
-
`process`.
|
| 1925 |
|
| 1926 |
— *end example*]
|
| 1927 |
|
| 1928 |
-
[*Note
|
| 1929 |
|
| 1930 |
Once the static data member has been defined, it exists even if no
|
| 1931 |
objects of its class have been created.
|
| 1932 |
|
| 1933 |
[*Example 2*:
|
|
@@ -1935,56 +1810,57 @@ objects of its class have been created.
|
|
| 1935 |
In the example above, `run_chain` and `running` exist even if no objects
|
| 1936 |
of class `process` are created by the program.
|
| 1937 |
|
| 1938 |
— *end example*]
|
| 1939 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1940 |
— *end note*]
|
| 1941 |
|
| 1942 |
If a non-volatile non-inline `const` static data member is of integral
|
| 1943 |
or enumeration type, its declaration in the class definition can specify
|
| 1944 |
a *brace-or-equal-initializer* in which every *initializer-clause* that
|
| 1945 |
is an *assignment-expression* is a constant expression [[expr.const]].
|
| 1946 |
The member shall still be defined in a namespace scope if it is odr-used
|
| 1947 |
-
[[
|
| 1948 |
-
|
| 1949 |
-
|
| 1950 |
*brace-or-equal-initializer*. If the member is declared with the
|
| 1951 |
`constexpr` specifier, it may be redeclared in namespace scope with no
|
| 1952 |
initializer (this usage is deprecated; see [[depr.static.constexpr]]).
|
| 1953 |
Declarations of other static data members shall not specify a
|
| 1954 |
*brace-or-equal-initializer*.
|
| 1955 |
|
| 1956 |
-
[*Note
|
| 1957 |
-
is odr-used [[
|
| 1958 |
|
| 1959 |
-
[*Note
|
| 1960 |
linkage of the name of the class [[basic.link]]. — *end note*]
|
| 1961 |
|
| 1962 |
-
Static data members are initialized and destroyed exactly like non-local
|
| 1963 |
-
variables ([[basic.start.static]], [[basic.start.dynamic]],
|
| 1964 |
-
[[basic.start.term]]).
|
| 1965 |
-
|
| 1966 |
### Bit-fields <a id="class.bit">[[class.bit]]</a>
|
| 1967 |
|
| 1968 |
A *member-declarator* of the form
|
| 1969 |
|
| 1970 |
``` bnf
|
| 1971 |
identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
|
| 1972 |
```
|
| 1973 |
|
| 1974 |
specifies a bit-field. The optional *attribute-specifier-seq* appertains
|
| 1975 |
to the entity being declared. A bit-field shall not be a static member.
|
| 1976 |
-
A bit-field shall have integral or
|
| 1977 |
-
semantic property is not part of the type of the
|
| 1978 |
-
*constant-expression* shall be an integral constant
|
| 1979 |
-
value greater than or equal to zero and is called the
|
| 1980 |
-
bit-field. If the width of a bit-field is larger than the
|
| 1981 |
-
bit-field’s type (or, in case of an enumeration type, of
|
| 1982 |
-
type), the extra bits are padding bits
|
| 1983 |
-
bit-fields within a class object is
|
| 1984 |
-
|
| 1985 |
-
|
|
|
|
| 1986 |
|
| 1987 |
[*Note 1*: Bit-fields straddle allocation units on some machines and
|
| 1988 |
not on others. Bit-fields are assigned right-to-left on some machines,
|
| 1989 |
left-to-right on others. — *end note*]
|
| 1990 |
|
|
@@ -1999,12 +1875,12 @@ externally-imposed layouts. — *end note*]
|
|
| 1999 |
As a special case, an unnamed bit-field with a width of zero specifies
|
| 2000 |
alignment of the next bit-field at an allocation unit boundary. Only
|
| 2001 |
when declaring an unnamed bit-field may the width be zero.
|
| 2002 |
|
| 2003 |
The address-of operator `&` shall not be applied to a bit-field, so
|
| 2004 |
-
there are no pointers to bit-fields. A non-const reference shall not
|
| 2005 |
-
|
| 2006 |
|
| 2007 |
[*Note 3*: If the initializer for a reference of type `const` `T&` is
|
| 2008 |
an lvalue that refers to a bit-field, the reference is bound to a
|
| 2009 |
temporary initialized to hold the value of the bit-field; the reference
|
| 2010 |
is not bound to the bit-field directly. See
|
|
@@ -2037,16 +1913,139 @@ void f() {
|
|
| 2037 |
}
|
| 2038 |
```
|
| 2039 |
|
| 2040 |
— *end example*]
|
| 2041 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2042 |
### Nested class declarations <a id="class.nest">[[class.nest]]</a>
|
| 2043 |
|
| 2044 |
A class can be declared within another class. A class declared within
|
| 2045 |
-
another is called a *nested class*.
|
| 2046 |
-
to its enclosing class. The nested class is in the scope of its
|
| 2047 |
-
enclosing class.
|
| 2048 |
|
| 2049 |
[*Note 1*: See [[expr.prim.id]] for restrictions on the use of
|
| 2050 |
non-static data members and non-static member functions. — *end note*]
|
| 2051 |
|
| 2052 |
[*Example 1*:
|
|
@@ -2059,29 +2058,33 @@ struct enclose {
|
|
| 2059 |
int x;
|
| 2060 |
static int s;
|
| 2061 |
|
| 2062 |
struct inner {
|
| 2063 |
void f(int i) {
|
| 2064 |
-
int a = sizeof(x); // OK
|
| 2065 |
x = i; // error: assign to enclose::x
|
| 2066 |
-
s = i; // OK
|
| 2067 |
-
::x = i; // OK
|
| 2068 |
-
y = i; // OK
|
| 2069 |
}
|
| 2070 |
void g(enclose* p, int i) {
|
| 2071 |
-
p->x = i; // OK
|
| 2072 |
}
|
| 2073 |
};
|
| 2074 |
};
|
| 2075 |
|
| 2076 |
-
inner* p = 0; // error: inner not
|
| 2077 |
```
|
| 2078 |
|
| 2079 |
— *end example*]
|
| 2080 |
|
| 2081 |
-
|
| 2082 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2083 |
|
| 2084 |
[*Example 2*:
|
| 2085 |
|
| 2086 |
``` cpp
|
| 2087 |
struct enclose {
|
|
@@ -2092,56 +2095,21 @@ struct enclose {
|
|
| 2092 |
};
|
| 2093 |
|
| 2094 |
int enclose::inner::x = 1;
|
| 2095 |
|
| 2096 |
void enclose::inner::f(int i) { ... }
|
| 2097 |
-
```
|
| 2098 |
|
| 2099 |
-
— *end example*]
|
| 2100 |
-
|
| 2101 |
-
If class `X` is defined in a namespace scope, a nested class `Y` may be
|
| 2102 |
-
declared in class `X` and later defined in the definition of class `X`
|
| 2103 |
-
or be later defined in a namespace scope enclosing the definition of
|
| 2104 |
-
class `X`.
|
| 2105 |
-
|
| 2106 |
-
[*Example 3*:
|
| 2107 |
-
|
| 2108 |
-
``` cpp
|
| 2109 |
class E {
|
| 2110 |
class I1; // forward declaration of nested class
|
| 2111 |
class I2;
|
| 2112 |
class I1 { }; // definition of nested class
|
| 2113 |
};
|
| 2114 |
class E::I2 { }; // definition of nested class
|
| 2115 |
```
|
| 2116 |
|
| 2117 |
— *end example*]
|
| 2118 |
|
| 2119 |
-
|
| 2120 |
-
within a nested class is in the lexical scope of that class; it obeys
|
| 2121 |
-
the same rules for name binding as a static member function of that
|
| 2122 |
-
class [[class.static]], but it has no special access rights to members
|
| 2123 |
-
of an enclosing class.
|
| 2124 |
|
| 2125 |
-
|
| 2126 |
-
|
| 2127 |
-
Type names obey exactly the same scope rules as other names. In
|
| 2128 |
-
particular, type names defined within a class definition cannot be used
|
| 2129 |
-
outside their class without qualification.
|
| 2130 |
-
|
| 2131 |
-
[*Example 1*:
|
| 2132 |
-
|
| 2133 |
-
``` cpp
|
| 2134 |
-
struct X {
|
| 2135 |
-
typedef int I;
|
| 2136 |
-
class Y { ... };
|
| 2137 |
-
I a;
|
| 2138 |
-
};
|
| 2139 |
-
|
| 2140 |
-
I b; // error
|
| 2141 |
-
Y c; // error
|
| 2142 |
-
X::Y d; // OK
|
| 2143 |
-
X::I e; // OK
|
| 2144 |
-
```
|
| 2145 |
-
|
| 2146 |
-
— *end example*]
|
| 2147 |
|
|
|
|
| 1 |
## Class members <a id="class.mem">[[class.mem]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="class.mem.general">[[class.mem.general]]</a>
|
| 4 |
+
|
| 5 |
``` bnf
|
| 6 |
member-specification:
|
| 7 |
member-declaration member-specificationₒₚₜ
|
| 8 |
access-specifier ':' member-specificationₒₚₜ
|
| 9 |
```
|
|
|
|
| 53 |
pure-specifier:
|
| 54 |
'=' '0'
|
| 55 |
```
|
| 56 |
|
| 57 |
The *member-specification* in a class definition declares the full set
|
| 58 |
+
of members of the class; no member can be added elsewhere. A
|
| 59 |
+
*direct member* of a class `X` is a member of `X` that was first
|
| 60 |
+
declared within the *member-specification* of `X`, including anonymous
|
| 61 |
+
union members [[class.union.anon]] and direct members thereof. Members
|
| 62 |
+
of a class are data members, member functions [[class.mfct]], nested
|
| 63 |
+
types, enumerators, and member templates [[temp.mem]] and
|
| 64 |
+
specializations thereof.
|
| 65 |
|
| 66 |
[*Note 1*: A specialization of a static data member template is a
|
| 67 |
static data member. A specialization of a member function template is a
|
| 68 |
member function. A specialization of a member class template is a nested
|
| 69 |
class. — *end note*]
|
| 70 |
|
| 71 |
A *member-declaration* does not declare new members of the class if it
|
| 72 |
is
|
| 73 |
|
| 74 |
- a friend declaration [[class.friend]],
|
| 75 |
+
- a *deduction-guide* [[temp.deduct.guide]],
|
| 76 |
+
- a *template-declaration* whose *declaration* is one of the above,
|
| 77 |
- a *static_assert-declaration*,
|
| 78 |
- a *using-declaration* [[namespace.udecl]], or
|
| 79 |
- an *empty-declaration*.
|
| 80 |
|
| 81 |
For any other *member-declaration*, each declared entity that is not an
|
|
|
|
| 83 |
*member-declaration* shall either declare at least one member name of
|
| 84 |
the class or declare at least one unnamed bit-field.
|
| 85 |
|
| 86 |
A *data member* is a non-function member introduced by a
|
| 87 |
*member-declarator*. A *member function* is a member that is a function.
|
| 88 |
+
Nested types are classes [[class.name]], [[class.nest]] and enumerations
|
| 89 |
+
[[dcl.enum]] declared in the class and arbitrary types declared as
|
| 90 |
+
members by use of a typedef declaration [[dcl.typedef]] or
|
| 91 |
*alias-declaration*. The enumerators of an unscoped enumeration
|
| 92 |
[[dcl.enum]] defined in the class are members of the class.
|
| 93 |
|
| 94 |
A data member or member function may be declared `static` in its
|
| 95 |
*member-declaration*, in which case it is a *static member* (see
|
| 96 |
[[class.static]]) (a *static data member* [[class.static.data]] or
|
| 97 |
*static member function* [[class.static.mfct]], respectively) of the
|
| 98 |
class. Any other data member or member function is a *non-static member*
|
| 99 |
+
(a *non-static data member* or *non-static member function*
|
| 100 |
+
[[class.mfct.non.static]], respectively).
|
| 101 |
|
| 102 |
[*Note 2*: A non-static data member of non-reference type is a member
|
| 103 |
subobject of a class object [[intro.object]]. — *end note*]
|
| 104 |
|
| 105 |
A member shall not be declared twice in the *member-specification*,
|
|
|
|
| 109 |
defined, and
|
| 110 |
- an enumeration can be introduced with an *opaque-enum-declaration* and
|
| 111 |
later redeclared with an *enum-specifier*.
|
| 112 |
|
| 113 |
[*Note 3*: A single name can denote several member functions provided
|
| 114 |
+
their types are sufficiently different
|
| 115 |
+
[[basic.scope.scope]]. — *end note*]
|
| 116 |
|
| 117 |
+
A redeclaration of a class member outside its class definition shall be
|
| 118 |
+
a definition, an explicit specialization, or an explicit instantiation
|
| 119 |
+
[[temp.expl.spec]], [[temp.explicit]]. The member shall not be a
|
| 120 |
+
non-static data member.
|
| 121 |
+
|
| 122 |
+
A *complete-class context* of a class (template) is a
|
| 123 |
|
| 124 |
- function body [[dcl.fct.def.general]],
|
| 125 |
- default argument [[dcl.fct.default]],
|
| 126 |
+
- default template argument [[temp.param]],
|
| 127 |
- *noexcept-specifier* [[except.spec]], or
|
| 128 |
- default member initializer
|
| 129 |
|
| 130 |
+
within the *member-specification* of the class or class template.
|
| 131 |
|
| 132 |
[*Note 4*: A complete-class context of a nested class is also a
|
| 133 |
complete-class context of any enclosing class, if the nested class is
|
| 134 |
defined within the *member-specification* of the enclosing
|
| 135 |
class. — *end note*]
|
| 136 |
|
| 137 |
+
A class is regarded as complete where its definition is reachable and
|
| 138 |
+
within its complete-class contexts; otherwise it is regarded as
|
| 139 |
+
incomplete within its own class *member-specification*.
|
|
|
|
|
|
|
| 140 |
|
| 141 |
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 142 |
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 143 |
has function type, otherwise it is interpreted as introducing a
|
| 144 |
*brace-or-equal-initializer*.
|
|
|
|
| 146 |
[*Example 1*:
|
| 147 |
|
| 148 |
``` cpp
|
| 149 |
struct S {
|
| 150 |
using T = void();
|
| 151 |
+
T * p = 0; // OK, brace-or-equal-initializer
|
| 152 |
+
virtual T f = 0; // OK, pure-specifier
|
| 153 |
};
|
| 154 |
```
|
| 155 |
|
| 156 |
— *end example*]
|
| 157 |
|
|
|
|
| 181 |
non-static data members, see [[class.base.init]] and
|
| 182 |
[[dcl.init.aggr]]). A *brace-or-equal-initializer* for a non-static data
|
| 183 |
member specifies a *default member initializer* for the member, and
|
| 184 |
shall not directly or indirectly cause the implicit definition of a
|
| 185 |
defaulted default constructor for the enclosing class or the exception
|
| 186 |
+
specification of that constructor. An immediate invocation
|
| 187 |
+
[[expr.const]] that is a potentially-evaluated subexpression
|
| 188 |
+
[[intro.execution]] of a default member initializer is neither evaluated
|
| 189 |
+
nor checked for whether it is a constant expression at the point where
|
| 190 |
+
the subexpression appears.
|
| 191 |
|
| 192 |
A member shall not be declared with the `extern`
|
| 193 |
*storage-class-specifier*. Within a class definition, a member shall not
|
| 194 |
be declared with the `thread_local` *storage-class-specifier* unless
|
| 195 |
also declared `static`.
|
|
|
|
| 210 |
A *virt-specifier-seq* shall contain at most one of each
|
| 211 |
*virt-specifier*. A *virt-specifier-seq* shall appear only in the first
|
| 212 |
declaration of a virtual member function [[class.virtual]].
|
| 213 |
|
| 214 |
The type of a non-static data member shall not be an incomplete type
|
| 215 |
+
[[term.incomplete.type]], an abstract class type [[class.abstract]], or
|
| 216 |
+
a (possibly multidimensional) array thereof.
|
| 217 |
|
| 218 |
[*Note 5*: In particular, a class `C` cannot contain a non-static
|
| 219 |
member of class `C`, but it can contain a pointer or reference to an
|
| 220 |
object of class `C`. — *end note*]
|
| 221 |
|
|
|
|
| 254 |
pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
| 255 |
character of the `tword` member of the `right` subtree of `s`.
|
| 256 |
|
| 257 |
— *end example*]
|
| 258 |
|
| 259 |
+
[*Note 8*: Non-variant non-static data members of non-zero size
|
| 260 |
+
[[intro.object]] are allocated so that later members have higher
|
| 261 |
+
addresses within a class object [[expr.rel]]. Implementation alignment
|
| 262 |
+
requirements can cause two adjacent members not to be allocated
|
| 263 |
+
immediately after each other; so can requirements for space for managing
|
| 264 |
+
virtual functions [[class.virtual]] and virtual base classes
|
|
|
|
|
|
|
| 265 |
[[class.mi]]. — *end note*]
|
| 266 |
|
| 267 |
If `T` is the name of a class, then each of the following shall have a
|
| 268 |
name different from `T`:
|
| 269 |
|
| 270 |
- every static data member of class `T`;
|
| 271 |
+
- every member function of class `T`; \[*Note 9*: This restriction does
|
| 272 |
not apply to constructors, which do not have names
|
| 273 |
+
[[class.ctor]] — *end note*]
|
| 274 |
- every member of class `T` that is itself a type;
|
| 275 |
- every member template of class `T`;
|
| 276 |
- every enumerator of every member of class `T` that is an unscoped
|
| 277 |
+
enumeration type; and
|
| 278 |
- every member of every anonymous union that is a member of class `T`.
|
| 279 |
|
| 280 |
In addition, if class `T` has a user-declared constructor
|
| 281 |
[[class.ctor]], every non-static data member of class `T` shall have a
|
| 282 |
name different from `T`.
|
| 283 |
|
| 284 |
The *common initial sequence* of two standard-layout struct
|
| 285 |
[[class.prop]] types is the longest sequence of non-static data members
|
| 286 |
and bit-fields in declaration order, starting with the first such entity
|
| 287 |
+
in each of the structs, such that
|
| 288 |
+
|
| 289 |
+
- corresponding entities have layout-compatible types [[basic.types]],
|
| 290 |
+
- corresponding entities have the same alignment requirements
|
| 291 |
+
[[basic.align]],
|
| 292 |
+
- either both entities are declared with the `no_unique_address`
|
| 293 |
+
attribute [[dcl.attr.nouniqueaddr]] or neither is, and
|
| 294 |
+
- either both entities are bit-fields with the same width or neither is
|
| 295 |
+
a bit-field.
|
| 296 |
|
| 297 |
[*Example 4*:
|
| 298 |
|
| 299 |
``` cpp
|
| 300 |
struct A { int a; char b; };
|
|
|
|
| 315 |
classes* if their common initial sequence comprises all members and
|
| 316 |
bit-fields of both classes [[basic.types]].
|
| 317 |
|
| 318 |
Two standard-layout unions are layout-compatible if they have the same
|
| 319 |
number of non-static data members and corresponding non-static data
|
| 320 |
+
members (in any order) have layout-compatible types
|
| 321 |
+
[[term.layout.compatible.type]].
|
| 322 |
|
| 323 |
In a standard-layout union with an active member [[class.union]] of
|
| 324 |
struct type `T1`, it is permitted to read a non-static data member `m`
|
| 325 |
of another union member of struct type `T2` provided `m` is part of the
|
| 326 |
common initial sequence of `T1` and `T2`; the behavior is as if the
|
|
|
|
| 346 |
If a standard-layout class object has any non-static data members, its
|
| 347 |
address is the same as the address of its first non-static data member
|
| 348 |
if that member is not a bit-field. Its address is also the same as the
|
| 349 |
address of each of its base class subobjects.
|
| 350 |
|
| 351 |
+
[*Note 11*: There can therefore be unnamed padding within a
|
| 352 |
standard-layout struct object inserted by an implementation, but not at
|
| 353 |
its beginning, as necessary to achieve appropriate
|
| 354 |
alignment. — *end note*]
|
| 355 |
|
| 356 |
[*Note 12*: The object and its first subobject are
|
| 357 |
+
pointer-interconvertible
|
| 358 |
+
[[basic.compound]], [[expr.static.cast]]. — *end note*]
|
| 359 |
|
| 360 |
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 361 |
|
| 362 |
+
If a member function is attached to the global module and is defined
|
| 363 |
+
[[dcl.fct.def]] in its class definition, it is inline [[dcl.inline]].
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
[*Note 1*: A member function is also inline if it is declared `inline`,
|
| 366 |
`constexpr`, or `consteval`. — *end note*]
|
| 367 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
[*Example 1*:
|
| 369 |
|
| 370 |
``` cpp
|
| 371 |
struct X {
|
| 372 |
typedef int T;
|
|
|
|
| 374 |
void f(T);
|
| 375 |
};
|
| 376 |
void X::f(T t = count) { }
|
| 377 |
```
|
| 378 |
|
| 379 |
+
The definition of the member function `f` of class `X` inhabits the
|
| 380 |
+
global scope; the notation `X::f` indicates that the function `f` is a
|
| 381 |
+
member of class `X` and in the scope of class `X`. In the function
|
| 382 |
+
definition, the parameter type `T` refers to the typedef member `T`
|
| 383 |
+
declared in class `X` and the default argument `count` refers to the
|
| 384 |
+
static data member `count` declared in class `X`.
|
| 385 |
|
| 386 |
— *end example*]
|
| 387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
Member functions of a local class shall be defined inline in their class
|
| 389 |
definition, if they are defined at all.
|
| 390 |
|
| 391 |
+
[*Note 2*:
|
| 392 |
|
| 393 |
A member function can be declared (but not defined) using a typedef for
|
| 394 |
a function type. The resulting member function has exactly the same type
|
| 395 |
as it would have if the function declarator were provided explicitly,
|
| 396 |
see [[dcl.fct]]. For example,
|
|
|
|
| 410 |
|
| 411 |
Also see [[temp.arg]].
|
| 412 |
|
| 413 |
— *end note*]
|
| 414 |
|
| 415 |
+
### Non-static member functions <a id="class.mfct.non.static">[[class.mfct.non.static]]</a>
|
| 416 |
|
| 417 |
A non-static member function may be called for an object of its class
|
| 418 |
type, or for an object of a class derived [[class.derived]] from its
|
| 419 |
+
class type, using the class member access syntax
|
| 420 |
+
[[expr.ref]], [[over.match.call]]. A non-static member function may also
|
| 421 |
+
be called directly using the function call syntax
|
| 422 |
+
[[expr.call]], [[over.match.call]] from within its class or a class
|
| 423 |
+
derived from its class, or a member thereof, as described below.
|
| 424 |
|
| 425 |
+
When an *id-expression* [[expr.prim.id]] that is neither part of a class
|
| 426 |
+
member access syntax [[expr.ref]] nor the unparenthesized operand of the
|
| 427 |
+
unary `&` operator [[expr.unary.op]] is used where the current class is
|
| 428 |
+
`X` [[expr.prim.this]], if name lookup [[basic.lookup]] resolves the
|
| 429 |
+
name in the *id-expression* to a non-static non-type member of some
|
| 430 |
+
class `C`, and if either the *id-expression* is potentially evaluated or
|
| 431 |
+
`C` is `X` or a base class of `X`, the *id-expression* is transformed
|
| 432 |
+
into a class member access expression [[expr.ref]] using `(*this)` as
|
| 433 |
+
the *postfix-expression* to the left of the `.` operator.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
|
| 435 |
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 436 |
access expression is ill-formed. — *end note*]
|
| 437 |
|
| 438 |
This transformation does not apply in the template definition context
|
|
|
|
| 471 |
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 472 |
not members of the class `tnode` and should be declared elsewhere.[^2]
|
| 473 |
|
| 474 |
— *end example*]
|
| 475 |
|
| 476 |
+
[*Note 2*: An implicit object member function can be declared with
|
| 477 |
+
*cv-qualifier*s, which affect the type of the `this` pointer
|
| 478 |
+
[[expr.prim.this]], and/or a *ref-qualifier* [[dcl.fct]]; both affect
|
| 479 |
+
overload resolution [[over.match.funcs]] — *end note*]
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
+
An implicit object member function may be declared virtual
|
| 482 |
+
[[class.virtual]] or pure virtual [[class.abstract]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
|
| 484 |
### Special member functions <a id="special">[[special]]</a>
|
| 485 |
|
| 486 |
Default constructors [[class.default.ctor]], copy constructors, move
|
| 487 |
constructors [[class.copy.ctor]], copy assignment operators, move
|
| 488 |
assignment operators [[class.copy.assign]], and prospective destructors
|
| 489 |
[[class.dtor]] are *special member functions*.
|
| 490 |
|
| 491 |
[*Note 1*: The implementation will implicitly declare these member
|
| 492 |
functions for some class types when the program does not explicitly
|
| 493 |
+
declare them. The implementation will implicitly define them as needed
|
| 494 |
+
[[dcl.fct.def.default]]. — *end note*]
|
|
|
|
| 495 |
|
| 496 |
An implicitly-declared special member function is declared at the
|
| 497 |
closing `}` of the *class-specifier*. Programs shall not define
|
| 498 |
implicitly-declared special member functions.
|
| 499 |
|
|
|
|
| 549 |
For a class, its non-static data members, its non-virtual direct base
|
| 550 |
classes, and, if the class is not abstract [[class.abstract]], its
|
| 551 |
virtual base classes are called its *potentially constructed
|
| 552 |
subobjects*.
|
| 553 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
### Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 555 |
|
| 556 |
+
#### General <a id="class.ctor.general">[[class.ctor.general]]</a>
|
| 557 |
+
|
| 558 |
+
A *declarator* declares a *constructor* if it is a function declarator
|
| 559 |
+
[[dcl.fct]] of the form
|
| 560 |
|
| 561 |
``` bnf
|
| 562 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 563 |
```
|
| 564 |
|
| 565 |
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 566 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 567 |
parentheses, and the *id-expression* has one of the following forms:
|
| 568 |
|
| 569 |
+
- in a friend declaration [[class.friend]], the *id-expression* is a
|
| 570 |
+
*qualified-id* that names a constructor [[class.qual]];
|
| 571 |
+
- otherwise, in a *member-declaration* that belongs to the
|
| 572 |
+
*member-specification* of a class or class template, the
|
| 573 |
+
*id-expression* is the injected-class-name [[class.pre]] of the
|
| 574 |
+
immediately-enclosing entity;
|
| 575 |
+
- otherwise, the *id-expression* is a *qualified-id* whose
|
| 576 |
+
*unqualified-id* is the injected-class-name of its lookup context.
|
| 577 |
|
| 578 |
Constructors do not have names. In a constructor declaration, each
|
| 579 |
*decl-specifier* in the optional *decl-specifier-seq* shall be `friend`,
|
| 580 |
+
`inline`, `constexpr`, `consteval`, or an *explicit-specifier*.
|
| 581 |
|
| 582 |
[*Example 1*:
|
| 583 |
|
| 584 |
``` cpp
|
| 585 |
struct S {
|
|
|
|
| 589 |
S::S() { } // defines the constructor
|
| 590 |
```
|
| 591 |
|
| 592 |
— *end example*]
|
| 593 |
|
| 594 |
+
A constructor is used to initialize objects of its class type.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
|
| 596 |
+
[*Note 1*: Because constructors do not have names, they are never found
|
| 597 |
+
during unqualified name lookup; however an explicit type conversion
|
| 598 |
+
using the functional notation [[expr.type.conv]] will cause a
|
| 599 |
+
constructor to be called to initialize an object. The syntax looks like
|
| 600 |
+
an explicit call of the constructor. — *end note*]
|
| 601 |
|
| 602 |
[*Example 2*:
|
| 603 |
|
| 604 |
``` cpp
|
| 605 |
complex zz = complex(1,2.3);
|
|
|
|
| 626 |
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 627 |
`volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
|
| 628 |
not applied on an object under construction. They come into effect when
|
| 629 |
the constructor for the most derived object [[intro.object]] ends.
|
| 630 |
|
| 631 |
+
The address of a constructor shall not be taken.
|
| 632 |
+
|
| 633 |
+
[*Note 6*: A `return` statement in the body of a constructor cannot
|
| 634 |
+
specify a return value [[stmt.return]]. — *end note*]
|
| 635 |
|
| 636 |
A constructor shall not be a coroutine.
|
| 637 |
|
| 638 |
+
A constructor shall not have an explicit object parameter [[dcl.fct]].
|
| 639 |
+
|
| 640 |
#### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
|
| 641 |
|
| 642 |
A *default constructor* for a class `X` is a constructor of class `X`
|
| 643 |
for which each parameter that is not a function parameter pack has a
|
| 644 |
default argument (including the case of a constructor with no
|
|
|
|
| 686 |
type (or array thereof), each such class has a trivial default
|
| 687 |
constructor.
|
| 688 |
|
| 689 |
Otherwise, the default constructor is *non-trivial*.
|
| 690 |
|
| 691 |
+
An implicitly-defined [[dcl.fct.def.default]] default constructor
|
|
|
|
|
|
|
|
|
|
|
|
|
| 692 |
performs the set of initializations of the class that would be performed
|
| 693 |
by a user-written default constructor for that class with no
|
| 694 |
*ctor-initializer* [[class.base.init]] and an empty
|
| 695 |
*compound-statement*. If that user-written default constructor would be
|
| 696 |
ill-formed, the program is ill-formed. If that user-written default
|
| 697 |
+
constructor would be constexpr-suitable [[dcl.constexpr]], the
|
| 698 |
+
implicitly-defined default constructor is `constexpr`. Before the
|
| 699 |
+
defaulted default constructor for a class is implicitly defined, all the
|
| 700 |
+
non-user-provided default constructors for its base classes and its
|
| 701 |
+
non-static data members are implicitly defined.
|
| 702 |
|
| 703 |
[*Note 1*: An implicitly-declared default constructor has an exception
|
| 704 |
specification [[except.spec]]. An explicitly-defaulted definition might
|
| 705 |
have an implicit exception specification, see
|
| 706 |
[[dcl.fct.def]]. — *end note*]
|
| 707 |
|
| 708 |
+
[*Note 2*: A default constructor is implicitly invoked to initialize a
|
| 709 |
+
class object when no initializer is specified [[dcl.init.general]]. Such
|
| 710 |
+
a default constructor is required to be accessible
|
| 711 |
+
[[class.access]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
|
| 713 |
+
[*Note 3*: [[class.base.init]] describes the order in which
|
| 714 |
constructors for base classes and non-static data members are called and
|
| 715 |
describes how arguments can be specified for the calls to these
|
| 716 |
constructors. — *end note*]
|
| 717 |
|
| 718 |
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
|
|
|
| 761 |
|
| 762 |
— *end example*]
|
| 763 |
|
| 764 |
[*Note 1*:
|
| 765 |
|
| 766 |
+
All forms of copy/move constructor can be declared for a class.
|
| 767 |
|
| 768 |
[*Example 3*:
|
| 769 |
|
| 770 |
``` cpp
|
| 771 |
struct X {
|
|
|
|
| 827 |
|
| 828 |
If the class definition does not explicitly declare a copy constructor,
|
| 829 |
a non-explicit one is declared *implicitly*. If the class definition
|
| 830 |
declares a move constructor or move assignment operator, the implicitly
|
| 831 |
declared copy constructor is defined as deleted; otherwise, it is
|
| 832 |
+
defaulted [[dcl.fct.def]]. The latter case is deprecated if the class
|
| 833 |
+
has a user-declared copy assignment operator or a user-declared
|
| 834 |
+
destructor [[depr.impldec]].
|
| 835 |
|
| 836 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 837 |
form
|
| 838 |
|
| 839 |
``` cpp
|
| 840 |
X::X(const X&)
|
| 841 |
```
|
| 842 |
|
| 843 |
if each potentially constructed subobject of a class type `M` (or array
|
| 844 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 845 |
+
`M&` or `const` `volatile` `M&`.[^3]
|
| 846 |
+
|
| 847 |
+
Otherwise, the implicitly-declared copy constructor will have the form
|
| 848 |
|
| 849 |
``` cpp
|
| 850 |
X::X(X&)
|
| 851 |
```
|
| 852 |
|
|
|
|
| 859 |
- `X` does not have a user-declared move assignment operator, and
|
| 860 |
- `X` does not have a user-declared destructor.
|
| 861 |
|
| 862 |
[*Note 3*: When the move constructor is not implicitly declared or
|
| 863 |
explicitly supplied, expressions that otherwise would have invoked the
|
| 864 |
+
move constructor might instead invoke a copy constructor. — *end note*]
|
| 865 |
|
| 866 |
The implicitly-declared move constructor for class `X` will have the
|
| 867 |
form
|
| 868 |
|
| 869 |
``` cpp
|
|
|
|
| 872 |
|
| 873 |
An implicitly-declared copy/move constructor is an inline public member
|
| 874 |
of its class. A defaulted copy/move constructor for a class `X` is
|
| 875 |
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 876 |
|
| 877 |
+
- a potentially constructed subobject of type `M` (or array thereof)
|
| 878 |
+
that cannot be copied/moved because overload resolution
|
| 879 |
+
[[over.match]], as applied to find `M`’s corresponding constructor,
|
| 880 |
+
results in an ambiguity or a function that is deleted or inaccessible
|
| 881 |
+
from the defaulted constructor,
|
| 882 |
- a variant member whose corresponding constructor as selected by
|
| 883 |
overload resolution is non-trivial,
|
| 884 |
- any potentially constructed subobject of a type with a destructor that
|
| 885 |
is deleted or inaccessible from the defaulted constructor, or,
|
| 886 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 887 |
type.
|
| 888 |
|
| 889 |
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 890 |
+
ignored by overload resolution [[over.match]], [[over.over]]. Such a
|
| 891 |
constructor would otherwise interfere with initialization from an rvalue
|
| 892 |
which can use the copy constructor instead. — *end note*]
|
| 893 |
|
| 894 |
A copy/move constructor for class `X` is trivial if it is not
|
| 895 |
user-provided and if:
|
|
|
|
| 902 |
thereof), the constructor selected to copy/move that member is
|
| 903 |
trivial;
|
| 904 |
|
| 905 |
otherwise the copy/move constructor is *non-trivial*.
|
| 906 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 907 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 908 |
+
implementation elided its odr-use
|
| 909 |
+
[[term.odr.use]], [[class.temporary]]. — *end note*]
|
| 910 |
|
| 911 |
+
If an implicitly-defined [[dcl.fct.def.default]] constructor would be
|
| 912 |
+
constexpr-suitable [[dcl.constexpr]], the implicitly-defined constructor
|
| 913 |
+
is `constexpr`.
|
| 914 |
|
| 915 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 916 |
defined, all non-user-provided copy/move constructors for its
|
| 917 |
potentially constructed subobjects are implicitly defined.
|
| 918 |
|
|
|
|
| 941 |
|
| 942 |
Virtual base class subobjects shall be initialized only once by the
|
| 943 |
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 944 |
|
| 945 |
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 946 |
+
object representation [[term.object.representation]] of `X`. For each
|
| 947 |
+
object nested within [[intro.object]] the object that is the source of
|
| 948 |
+
the copy, a corresponding object o nested within the destination is
|
| 949 |
+
identified (if the object is a subobject) or created (otherwise), and
|
| 950 |
+
the lifetime of o begins before the copy is performed.
|
| 951 |
|
| 952 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 953 |
|
| 954 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 955 |
non-static non-template member function of class `X` with exactly one
|
| 956 |
+
non-object parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
|
| 957 |
`const volatile X&`.[^4]
|
| 958 |
|
| 959 |
[*Note 1*: An overloaded assignment operator must be declared to have
|
| 960 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 961 |
|
| 962 |
+
[*Note 2*: More than one form of copy assignment operator can be
|
| 963 |
declared for a class. — *end note*]
|
| 964 |
|
| 965 |
[*Note 3*:
|
| 966 |
|
| 967 |
If a class `X` only has a copy assignment operator with a parameter of
|
|
|
|
| 987 |
— *end note*]
|
| 988 |
|
| 989 |
If the class definition does not explicitly declare a copy assignment
|
| 990 |
operator, one is declared *implicitly*. If the class definition declares
|
| 991 |
a move constructor or move assignment operator, the implicitly declared
|
| 992 |
+
copy assignment operator is defined as deleted; otherwise, it is
|
| 993 |
+
defaulted [[dcl.fct.def]]. The latter case is deprecated if the class
|
| 994 |
has a user-declared copy constructor or a user-declared destructor
|
| 995 |
[[depr.impldec]]. The implicitly-declared copy assignment operator for a
|
| 996 |
class `X` will have the form
|
| 997 |
|
| 998 |
``` cpp
|
|
|
|
| 1014 |
``` cpp
|
| 1015 |
X& X::operator=(X&)
|
| 1016 |
```
|
| 1017 |
|
| 1018 |
A user-declared move assignment operator `X::operator=` is a non-static
|
| 1019 |
+
non-template member function of class `X` with exactly one non-object
|
| 1020 |
+
parameter of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 1021 |
+
`const volatile X&&`.
|
| 1022 |
|
| 1023 |
[*Note 4*: An overloaded assignment operator must be declared to have
|
| 1024 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 1025 |
|
| 1026 |
+
[*Note 5*: More than one form of move assignment operator can be
|
| 1027 |
declared for a class. — *end note*]
|
| 1028 |
|
| 1029 |
If the definition of a class `X` does not explicitly declare a move
|
| 1030 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1031 |
only if
|
|
|
|
| 1066 |
``` cpp
|
| 1067 |
X& X::operator=(X&&)
|
| 1068 |
```
|
| 1069 |
|
| 1070 |
The implicitly-declared copy/move assignment operator for class `X` has
|
| 1071 |
+
the return type `X&`. An implicitly-declared copy/move assignment
|
| 1072 |
+
operator is an inline public member of its class.
|
|
|
|
|
|
|
| 1073 |
|
| 1074 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 1075 |
deleted if `X` has:
|
| 1076 |
|
| 1077 |
- a variant member with a non-trivial corresponding assignment operator
|
|
|
|
| 1085 |
corresponding assignment operator, results in an ambiguity or a
|
| 1086 |
function that is deleted or inaccessible from the defaulted assignment
|
| 1087 |
operator.
|
| 1088 |
|
| 1089 |
[*Note 6*: A defaulted move assignment operator that is defined as
|
| 1090 |
+
deleted is ignored by overload resolution
|
| 1091 |
+
[[over.match]], [[over.over]]. — *end note*]
|
| 1092 |
|
| 1093 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1094 |
class if not declared by the user, a base class copy/move assignment
|
| 1095 |
operator is always hidden by the corresponding assignment operator of a
|
| 1096 |
+
derived class [[over.ass]].
|
| 1097 |
+
|
| 1098 |
+
[*Note 7*: A *using-declaration* in a derived class `C` that names an
|
| 1099 |
+
assignment operator from a base class never suppresses the implicit
|
| 1100 |
+
declaration of an assignment operator of `C`, even if the base class
|
| 1101 |
+
assignment operator would be a copy or move assignment operator if
|
| 1102 |
+
declared as a member of `C`. — *end note*]
|
| 1103 |
|
| 1104 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1105 |
user-provided and if:
|
| 1106 |
|
| 1107 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
|
|
|
| 1112 |
thereof), the assignment operator selected to copy/move that member is
|
| 1113 |
trivial;
|
| 1114 |
|
| 1115 |
otherwise the copy/move assignment operator is *non-trivial*.
|
| 1116 |
|
| 1117 |
+
An implicitly-defined [[dcl.fct.def.default]] copy/move assignment
|
| 1118 |
+
operator is `constexpr`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1119 |
|
| 1120 |
Before the defaulted copy/move assignment operator for a class is
|
| 1121 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1122 |
for its direct base classes and its non-static data members are
|
| 1123 |
implicitly defined.
|
| 1124 |
|
| 1125 |
+
[*Note 8*: An implicitly-declared copy/move assignment operator has an
|
| 1126 |
implied exception specification [[except.spec]]. — *end note*]
|
| 1127 |
|
| 1128 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 1129 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 1130 |
The direct base classes of `X` are assigned first, in the order of their
|
|
|
|
| 1162 |
assigned twice by the implicitly-defined copy/move assignment operator
|
| 1163 |
for `C`.
|
| 1164 |
|
| 1165 |
— *end example*]
|
| 1166 |
|
| 1167 |
+
The implicitly-defined copy/move assignment operator for a union `X`
|
| 1168 |
+
copies the object representation [[term.object.representation]] of `X`.
|
| 1169 |
+
If the source and destination of the assignment are not the same object,
|
| 1170 |
+
then for each object nested within [[intro.object]] the object that is
|
| 1171 |
+
the source of the copy, a corresponding object o nested within the
|
| 1172 |
+
destination is created, and the lifetime of o begins before the copy is
|
| 1173 |
+
performed.
|
| 1174 |
+
|
| 1175 |
+
The implicitly-defined copy/move assignment operator for a class returns
|
| 1176 |
+
the object for which the assignment operator is invoked, that is, the
|
| 1177 |
+
object assigned to.
|
| 1178 |
|
| 1179 |
### Destructors <a id="class.dtor">[[class.dtor]]</a>
|
| 1180 |
|
| 1181 |
+
A declaration whose *declarator-id* has an *unqualified-id* that begins
|
| 1182 |
+
with a `~` declares a *prospective destructor*; its *declarator* shall
|
| 1183 |
+
be a function declarator [[dcl.fct]] of the form
|
| 1184 |
|
| 1185 |
``` bnf
|
| 1186 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1187 |
```
|
| 1188 |
|
|
|
|
| 1193 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 1194 |
of a class or class template but is not a friend declaration
|
| 1195 |
[[class.friend]], the *id-expression* is `~`*class-name* and the
|
| 1196 |
*class-name* is the injected-class-name [[class.pre]] of the
|
| 1197 |
immediately-enclosing entity or
|
| 1198 |
+
- otherwise, the *id-expression* is *nested-name-specifier*
|
| 1199 |
+
`~`*class-name* and the *class-name* is the injected-class-name of the
|
| 1200 |
+
class nominated by the *nested-name-specifier*.
|
| 1201 |
|
| 1202 |
A prospective destructor shall take no arguments [[dcl.fct]]. Each
|
| 1203 |
*decl-specifier* of the *decl-specifier-seq* of a prospective destructor
|
| 1204 |
declaration (if any) shall be `friend`, `inline`, `virtual`,
|
| 1205 |
`constexpr`, or `consteval`.
|
|
|
|
| 1219 |
At the end of the definition of a class, overload resolution is
|
| 1220 |
performed among the prospective destructors declared in that class with
|
| 1221 |
an empty argument list to select the *destructor* for the class, also
|
| 1222 |
known as the *selected destructor*. The program is ill-formed if
|
| 1223 |
overload resolution fails. Destructor selection does not constitute a
|
| 1224 |
+
reference to, or odr-use [[term.odr.use]] of, the selected destructor,
|
| 1225 |
and in particular, the selected destructor may be deleted
|
| 1226 |
[[dcl.fct.def.delete]].
|
| 1227 |
|
| 1228 |
+
The address of a destructor shall not be taken.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1229 |
|
| 1230 |
+
[*Note 1*: A `return` statement in the body of a destructor cannot
|
| 1231 |
+
specify a return value [[stmt.return]]. — *end note*]
|
| 1232 |
+
|
| 1233 |
+
A destructor can be invoked for a `const`, `volatile` or `const`
|
| 1234 |
+
`volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
|
| 1235 |
+
not applied on an object under destruction. They stop being in effect
|
| 1236 |
+
when the destructor for the most derived object [[intro.object]] starts.
|
| 1237 |
+
|
| 1238 |
+
[*Note 2*: A declaration of a destructor that does not have a
|
| 1239 |
*noexcept-specifier* has the same exception specification as if it had
|
| 1240 |
been implicitly declared [[except.spec]]. — *end note*]
|
| 1241 |
|
| 1242 |
A defaulted destructor for a class `X` is defined as deleted if:
|
| 1243 |
|
|
|
|
| 1250 |
function results in an ambiguity or in a function that is deleted or
|
| 1251 |
inaccessible from the defaulted destructor.
|
| 1252 |
|
| 1253 |
A destructor is trivial if it is not user-provided and if:
|
| 1254 |
|
| 1255 |
+
- the destructor is not virtual,
|
| 1256 |
- all of the direct base classes of its class have trivial destructors,
|
| 1257 |
and
|
| 1258 |
- for all of the non-static data members of its class that are of class
|
| 1259 |
type (or array thereof), each such class has a trivial destructor.
|
| 1260 |
|
| 1261 |
Otherwise, the destructor is *non-trivial*.
|
| 1262 |
|
| 1263 |
+
A defaulted destructor is a constexpr destructor if it is
|
| 1264 |
+
constexpr-suitable [[dcl.constexpr]].
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1265 |
|
| 1266 |
Before a defaulted destructor for a class is implicitly defined, all the
|
| 1267 |
non-user-provided destructors for its base classes and its non-static
|
| 1268 |
data members are implicitly defined.
|
| 1269 |
|
| 1270 |
+
A prospective destructor can be declared `virtual` [[class.virtual]] and
|
| 1271 |
+
with a *pure-specifier* [[class.abstract]]. If the destructor of a class
|
| 1272 |
+
is virtual and any objects of that class or any derived class are
|
| 1273 |
+
created in the program, the destructor shall be defined.
|
|
|
|
|
|
|
| 1274 |
|
| 1275 |
+
[*Note 3*: Some language constructs have special semantics when used
|
| 1276 |
during destruction; see [[class.cdtor]]. — *end note*]
|
| 1277 |
|
| 1278 |
After executing the body of the destructor and destroying any objects
|
| 1279 |
with automatic storage duration allocated within the body, a destructor
|
| 1280 |
for class `X` calls the destructors for `X`’s direct non-variant
|
|
|
|
| 1282 |
base classes and, if `X` is the most derived class [[class.base.init]],
|
| 1283 |
its destructor calls the destructors for `X`’s virtual base classes. All
|
| 1284 |
destructors are called as if they were referenced with a qualified name,
|
| 1285 |
that is, ignoring any possible virtual overriding destructors in more
|
| 1286 |
derived classes. Bases and members are destroyed in the reverse order of
|
| 1287 |
+
the completion of their constructor (see [[class.base.init]]).
|
| 1288 |
+
|
| 1289 |
+
[*Note 4*: A `return` statement [[stmt.return]] in a destructor might
|
| 1290 |
+
not directly return to the caller; before transferring control to the
|
| 1291 |
+
caller, the destructors for the members and bases are
|
| 1292 |
+
called. — *end note*]
|
| 1293 |
+
|
| 1294 |
+
Destructors for elements of an array are called in reverse order of
|
| 1295 |
+
their construction (see [[class.init]]).
|
| 1296 |
|
| 1297 |
A destructor is invoked implicitly
|
| 1298 |
|
| 1299 |
- for a constructed object with static storage duration
|
| 1300 |
[[basic.stc.static]] at program termination [[basic.start.term]],
|
| 1301 |
- for a constructed object with thread storage duration
|
| 1302 |
[[basic.stc.thread]] at thread exit,
|
| 1303 |
- for a constructed object with automatic storage duration
|
| 1304 |
[[basic.stc.auto]] when the block in which an object is created exits
|
| 1305 |
[[stmt.dcl]],
|
| 1306 |
+
- for a constructed temporary object when its lifetime ends
|
| 1307 |
+
[[conv.rval]], [[class.temporary]].
|
| 1308 |
|
| 1309 |
In each case, the context of the invocation is the context of the
|
| 1310 |
construction of the object. A destructor may also be invoked implicitly
|
| 1311 |
through use of a *delete-expression* [[expr.delete]] for a constructed
|
| 1312 |
object allocated by a *new-expression* [[expr.new]]; the context of the
|
| 1313 |
invocation is the *delete-expression*.
|
| 1314 |
|
| 1315 |
+
[*Note 5*: An array of class type contains several subobjects for each
|
| 1316 |
of which the destructor is invoked. — *end note*]
|
| 1317 |
|
| 1318 |
A destructor can also be invoked explicitly. A destructor is
|
| 1319 |
*potentially invoked* if it is invoked or as specified in [[expr.new]],
|
| 1320 |
[[stmt.return]], [[dcl.init.aggr]], [[class.base.init]], and
|
| 1321 |
[[except.throw]]. A program is ill-formed if a destructor that is
|
| 1322 |
potentially invoked is deleted or not accessible from the context of the
|
| 1323 |
invocation.
|
| 1324 |
|
| 1325 |
At the point of definition of a virtual destructor (including an
|
| 1326 |
+
implicit definition), the non-array deallocation function is determined
|
| 1327 |
+
as if for the expression `delete this` appearing in a non-virtual
|
| 1328 |
+
destructor of the destructor’s class (see [[expr.delete]]). If the
|
| 1329 |
+
lookup fails or if the deallocation function has a deleted definition
|
| 1330 |
+
[[dcl.fct.def]], the program is ill-formed.
|
| 1331 |
|
| 1332 |
+
[*Note 6*: This assures that a deallocation function corresponding to
|
| 1333 |
the dynamic type of an object is available for the *delete-expression*
|
| 1334 |
[[class.free]]. — *end note*]
|
| 1335 |
|
| 1336 |
In an explicit destructor call, the destructor is specified by a `~`
|
| 1337 |
followed by a *type-name* or *decltype-specifier* that denotes the
|
|
|
|
| 1339 |
the usual rules for member functions [[class.mfct]]; that is, if the
|
| 1340 |
object is not of the destructor’s class type and not of a class derived
|
| 1341 |
from the destructor’s class type (including when the destructor is
|
| 1342 |
invoked via a null pointer value), the program has undefined behavior.
|
| 1343 |
|
| 1344 |
+
[*Note 7*: Invoking `delete` on a null pointer does not call the
|
| 1345 |
destructor; see [[expr.delete]]. — *end note*]
|
| 1346 |
|
| 1347 |
[*Example 1*:
|
| 1348 |
|
| 1349 |
``` cpp
|
|
|
|
| 1367 |
}
|
| 1368 |
```
|
| 1369 |
|
| 1370 |
— *end example*]
|
| 1371 |
|
| 1372 |
+
[*Note 8*: An explicit destructor call must always be written using a
|
| 1373 |
member access operator [[expr.ref]] or a *qualified-id*
|
| 1374 |
[[expr.prim.id.qual]]; in particular, the *unary-expression* `~X()` in a
|
| 1375 |
member function is not an explicit destructor call
|
| 1376 |
[[expr.unary.op]]. — *end note*]
|
| 1377 |
|
| 1378 |
+
[*Note 9*:
|
| 1379 |
|
| 1380 |
Explicit calls of destructors are rarely needed. One use of such calls
|
| 1381 |
is for objects placed at specific addresses using a placement
|
| 1382 |
*new-expression*. Such use of explicit placement and destruction of
|
| 1383 |
objects can be necessary to cope with dedicated hardware resources and
|
|
|
|
| 1399 |
}
|
| 1400 |
```
|
| 1401 |
|
| 1402 |
— *end note*]
|
| 1403 |
|
| 1404 |
+
Once a destructor is invoked for an object, the object’s lifetime ends;
|
| 1405 |
the behavior is undefined if the destructor is invoked for an object
|
| 1406 |
whose lifetime has ended [[basic.life]].
|
| 1407 |
|
| 1408 |
[*Example 2*: If the destructor for an object with automatic storage
|
| 1409 |
duration is explicitly invoked, and the block is subsequently left in a
|
| 1410 |
manner that would ordinarily invoke implicit destruction of the object,
|
| 1411 |
the behavior is undefined. — *end example*]
|
| 1412 |
|
| 1413 |
+
[*Note 10*:
|
| 1414 |
|
| 1415 |
The notation for explicit call of a destructor can be used for any
|
| 1416 |
scalar type name [[expr.prim.id.dtor]]. Allowing this makes it possible
|
| 1417 |
to write code without having to know if a destructor exists for a given
|
| 1418 |
type. For example:
|
|
|
|
| 1427 |
|
| 1428 |
A destructor shall not be a coroutine.
|
| 1429 |
|
| 1430 |
### Conversions <a id="class.conv">[[class.conv]]</a>
|
| 1431 |
|
| 1432 |
+
#### General <a id="class.conv.general">[[class.conv.general]]</a>
|
| 1433 |
+
|
| 1434 |
Type conversions of class objects can be specified by constructors and
|
| 1435 |
by conversion functions. These conversions are called *user-defined
|
| 1436 |
conversions* and are used for implicit type conversions [[conv]], for
|
| 1437 |
+
initialization [[dcl.init]], and for explicit type conversions
|
| 1438 |
+
[[expr.type.conv]], [[expr.cast]], [[expr.static.cast]].
|
| 1439 |
|
| 1440 |
+
User-defined conversions are applied only where they are unambiguous
|
| 1441 |
+
[[class.member.lookup]], [[class.conv.fct]]. Conversions obey the access
|
| 1442 |
+
control rules [[class.access]]. Access control is applied after
|
| 1443 |
ambiguity resolution [[basic.lookup]].
|
| 1444 |
|
| 1445 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 1446 |
conversions in function calls as well as examples below. — *end note*]
|
| 1447 |
|
|
|
|
| 1459 |
operator X();
|
| 1460 |
};
|
| 1461 |
|
| 1462 |
Y a;
|
| 1463 |
int b = a; // error: no viable conversion (a.operator X().operator int() not considered)
|
| 1464 |
+
int c = X(a); // OK, a.operator X().operator int()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1465 |
```
|
| 1466 |
|
| 1467 |
— *end example*]
|
| 1468 |
|
| 1469 |
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
|
|
|
| 1494 |
|
| 1495 |
[*Note 1*:
|
| 1496 |
|
| 1497 |
An explicit constructor constructs objects just like non-explicit
|
| 1498 |
constructors, but does so only where the direct-initialization syntax
|
| 1499 |
+
[[dcl.init]] or where casts [[expr.static.cast]], [[expr.cast]] are
|
| 1500 |
explicitly used; see also [[over.match.copy]]. A default constructor
|
| 1501 |
+
can be an explicit constructor; such a constructor will be used to
|
| 1502 |
perform default-initialization or value-initialization [[dcl.init]].
|
| 1503 |
|
| 1504 |
[*Example 2*:
|
| 1505 |
|
| 1506 |
``` cpp
|
|
|
|
| 1508 |
explicit Z();
|
| 1509 |
explicit Z(int);
|
| 1510 |
explicit Z(int, int);
|
| 1511 |
};
|
| 1512 |
|
| 1513 |
+
Z a; // OK, default-initialization performed
|
| 1514 |
+
Z b{}; // OK, direct initialization syntax used
|
| 1515 |
Z c = {}; // error: copy-list-initialization
|
| 1516 |
Z a1 = 1; // error: no implicit conversion
|
| 1517 |
+
Z a3 = Z(1); // OK, direct initialization syntax used
|
| 1518 |
+
Z a2(1); // OK, direct initialization syntax used
|
| 1519 |
+
Z* p = new Z(1); // OK, direct initialization syntax used
|
| 1520 |
+
Z a4 = (Z)1; // OK, explicit cast used
|
| 1521 |
+
Z a5 = static_cast<Z>(1); // OK, explicit cast used
|
| 1522 |
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 1523 |
```
|
| 1524 |
|
| 1525 |
— *end example*]
|
| 1526 |
|
|
|
|
| 1528 |
|
| 1529 |
A non-explicit copy/move constructor [[class.copy.ctor]] is a converting
|
| 1530 |
constructor.
|
| 1531 |
|
| 1532 |
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 1533 |
+
explicit constructor; it can be called for implicit type
|
| 1534 |
conversions. — *end note*]
|
| 1535 |
|
| 1536 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 1537 |
|
|
|
|
|
|
|
|
|
|
| 1538 |
``` bnf
|
| 1539 |
conversion-function-id:
|
| 1540 |
operator conversion-type-id
|
| 1541 |
```
|
| 1542 |
|
|
|
|
| 1548 |
``` bnf
|
| 1549 |
conversion-declarator:
|
| 1550 |
ptr-operator conversion-declaratorₒₚₜ
|
| 1551 |
```
|
| 1552 |
|
| 1553 |
+
A declaration whose *declarator-id* has an *unqualified-id* that is a
|
| 1554 |
+
*conversion-function-id* declares a *conversion function*; its
|
| 1555 |
+
*declarator* shall be a function declarator [[dcl.fct]] of the form
|
| 1556 |
+
|
| 1557 |
+
``` bnf
|
| 1558 |
+
ptr-declarator '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 1559 |
+
ref-qualifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1560 |
+
```
|
| 1561 |
+
|
| 1562 |
+
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 1563 |
+
optional *attribute-specifier-seq*, and optional surrounding
|
| 1564 |
+
parentheses, and the *id-expression* has one of the following forms:
|
| 1565 |
+
|
| 1566 |
+
- in a *member-declaration* that belongs to the *member-specification*
|
| 1567 |
+
of a class or class template but is not a friend declaration
|
| 1568 |
+
[[class.friend]], the *id-expression* is a *conversion-function-id*;
|
| 1569 |
+
- otherwise, the *id-expression* is a *qualified-id* whose
|
| 1570 |
+
*unqualified-id* is a *conversion-function-id*.
|
| 1571 |
+
|
| 1572 |
+
A conversion function shall have no non-object parameters and shall be a
|
| 1573 |
+
non-static member function of a class or class template `X`; it
|
| 1574 |
specifies a conversion from `X` to the type specified by the
|
| 1575 |
+
*conversion-type-id*, interpreted as a *type-id* [[dcl.name]]. A
|
| 1576 |
+
*decl-specifier* in the *decl-specifier-seq* of a conversion function
|
| 1577 |
+
(if any) shall not be a *defining-type-specifier*.
|
| 1578 |
+
|
| 1579 |
+
The type of the conversion function is “`noexcept`ₒₚₜ function taking
|
| 1580 |
+
no parameter *cv-qualifier-seq*ₒₚₜ *ref-qualifier*ₒₚₜ returning
|
| 1581 |
+
*conversion-type-id*”.
|
| 1582 |
+
|
| 1583 |
+
A conversion function is never used to convert a (possibly cv-qualified)
|
| 1584 |
+
object to the (possibly cv-qualified) same object type (or a reference
|
| 1585 |
+
to it), to a (possibly cv-qualified) base class of that type (or a
|
| 1586 |
+
reference to it), or to cv `void`.[^6]
|
| 1587 |
|
| 1588 |
[*Example 1*:
|
| 1589 |
|
| 1590 |
``` cpp
|
| 1591 |
struct X {
|
|
|
|
| 1617 |
struct Z {
|
| 1618 |
explicit operator Y() const;
|
| 1619 |
};
|
| 1620 |
|
| 1621 |
void h(Z z) {
|
| 1622 |
+
Y y1(z); // OK, direct-initialization
|
| 1623 |
Y y2 = z; // error: no conversion function candidate for copy-initialization
|
| 1624 |
+
Y y3 = (Y)z; // OK, cast notation
|
| 1625 |
}
|
| 1626 |
|
| 1627 |
void g(X a, X b) {
|
| 1628 |
int i = (a) ? 1+a : 0;
|
| 1629 |
int j = (a&&b) ? a+b : i;
|
|
|
|
| 1666 |
|
| 1667 |
— *end example*]
|
| 1668 |
|
| 1669 |
— *end note*]
|
| 1670 |
|
| 1671 |
+
[*Note 2*:
|
| 1672 |
+
|
| 1673 |
+
A conversion function in a derived class hides only conversion functions
|
| 1674 |
+
in base classes that convert to the same type. A conversion function
|
| 1675 |
+
template with a dependent return type hides only templates in base
|
| 1676 |
+
classes that correspond to it [[class.member.lookup]]; otherwise, it
|
| 1677 |
+
hides and is hidden as a non-template function. Function overload
|
| 1678 |
+
resolution [[over.match.best]] selects the best conversion function to
|
| 1679 |
+
perform the conversion.
|
| 1680 |
+
|
| 1681 |
+
[*Example 5*:
|
| 1682 |
+
|
| 1683 |
+
``` cpp
|
| 1684 |
+
struct X {
|
| 1685 |
+
operator int();
|
| 1686 |
+
};
|
| 1687 |
+
|
| 1688 |
+
struct Y : X {
|
| 1689 |
+
operator char();
|
| 1690 |
+
};
|
| 1691 |
+
|
| 1692 |
+
void f(Y& a) {
|
| 1693 |
+
if (a) { // error: ambiguous between X::operator int() and Y::operator char()
|
| 1694 |
+
}
|
| 1695 |
+
}
|
| 1696 |
+
```
|
| 1697 |
+
|
| 1698 |
+
— *end example*]
|
| 1699 |
+
|
| 1700 |
+
— *end note*]
|
| 1701 |
|
| 1702 |
Conversion functions can be virtual.
|
| 1703 |
|
| 1704 |
A conversion function template shall not have a deduced return type
|
| 1705 |
[[dcl.spec.auto]].
|
| 1706 |
|
| 1707 |
+
[*Example 6*:
|
| 1708 |
|
| 1709 |
``` cpp
|
| 1710 |
struct S {
|
| 1711 |
operator auto() const { return 10; } // OK
|
| 1712 |
template<class T>
|
|
|
|
| 1716 |
|
| 1717 |
— *end example*]
|
| 1718 |
|
| 1719 |
### Static members <a id="class.static">[[class.static]]</a>
|
| 1720 |
|
| 1721 |
+
#### General <a id="class.static.general">[[class.static.general]]</a>
|
| 1722 |
+
|
| 1723 |
A static member `s` of class `X` may be referred to using the
|
| 1724 |
*qualified-id* expression `X::s`; it is not necessary to use the class
|
| 1725 |
member access syntax [[expr.ref]] to refer to a static member. A static
|
| 1726 |
member may be referred to using the class member access syntax, in which
|
| 1727 |
case the object expression is evaluated.
|
|
|
|
| 1733 |
static void reschedule();
|
| 1734 |
};
|
| 1735 |
process& g();
|
| 1736 |
|
| 1737 |
void f() {
|
| 1738 |
+
process::reschedule(); // OK, no object necessary
|
| 1739 |
g().reschedule(); // g() is called
|
| 1740 |
}
|
| 1741 |
```
|
| 1742 |
|
| 1743 |
— *end example*]
|
| 1744 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1745 |
Static members obey the usual class member access rules
|
| 1746 |
[[class.access]]. When used in the declaration of a class member, the
|
| 1747 |
`static` specifier shall only be used in the member declarations that
|
| 1748 |
appear within the *member-specification* of the class definition.
|
| 1749 |
|
|
|
|
| 1754 |
|
| 1755 |
[*Note 1*: The rules described in [[class.mfct]] apply to static
|
| 1756 |
member functions. — *end note*]
|
| 1757 |
|
| 1758 |
[*Note 2*: A static member function does not have a `this` pointer
|
| 1759 |
+
[[expr.prim.this]]. A static member function cannot be qualified with
|
| 1760 |
+
`const`, `volatile`, or `virtual` [[dcl.fct]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1761 |
|
| 1762 |
#### Static data members <a id="class.static.data">[[class.static.data]]</a>
|
| 1763 |
|
| 1764 |
A static data member is not part of the subobjects of a class. If a
|
| 1765 |
static data member is declared `thread_local` there is one copy of the
|
|
|
|
| 1772 |
[[class.pre]] or local [[class.local]] class or of a (possibly
|
| 1773 |
indirectly) nested class [[class.nest]] thereof.
|
| 1774 |
|
| 1775 |
The declaration of a non-inline static data member in its class
|
| 1776 |
definition is not a definition and may be of an incomplete type other
|
| 1777 |
+
than cv `void`.
|
| 1778 |
+
|
| 1779 |
+
[*Note 1*: The *initializer* in the definition of a static data member
|
| 1780 |
+
is in the scope of its class [[basic.scope.class]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 1781 |
|
| 1782 |
[*Example 1*:
|
| 1783 |
|
| 1784 |
``` cpp
|
| 1785 |
class process {
|
|
|
|
| 1789 |
|
| 1790 |
process* process::running = get_main();
|
| 1791 |
process* process::run_chain = running;
|
| 1792 |
```
|
| 1793 |
|
| 1794 |
+
The definition of the static data member `run_chain` of class `process`
|
| 1795 |
+
inhabits the global scope; the notation `process::run_chain` indicates
|
| 1796 |
+
that the member `run_chain` is a member of class `process` and in the
|
| 1797 |
+
scope of class `process`. In the static data member definition, the
|
| 1798 |
+
*initializer* expression refers to the static data member `running` of
|
| 1799 |
+
class `process`.
|
| 1800 |
|
| 1801 |
— *end example*]
|
| 1802 |
|
| 1803 |
+
[*Note 2*:
|
| 1804 |
|
| 1805 |
Once the static data member has been defined, it exists even if no
|
| 1806 |
objects of its class have been created.
|
| 1807 |
|
| 1808 |
[*Example 2*:
|
|
|
|
| 1810 |
In the example above, `run_chain` and `running` exist even if no objects
|
| 1811 |
of class `process` are created by the program.
|
| 1812 |
|
| 1813 |
— *end example*]
|
| 1814 |
|
| 1815 |
+
The initialization and destruction of static data members is described
|
| 1816 |
+
in [[basic.start.static]], [[basic.start.dynamic]], and
|
| 1817 |
+
[[basic.start.term]].
|
| 1818 |
+
|
| 1819 |
— *end note*]
|
| 1820 |
|
| 1821 |
If a non-volatile non-inline `const` static data member is of integral
|
| 1822 |
or enumeration type, its declaration in the class definition can specify
|
| 1823 |
a *brace-or-equal-initializer* in which every *initializer-clause* that
|
| 1824 |
is an *assignment-expression* is a constant expression [[expr.const]].
|
| 1825 |
The member shall still be defined in a namespace scope if it is odr-used
|
| 1826 |
+
[[term.odr.use]] in the program and the namespace scope definition shall
|
| 1827 |
+
not contain an *initializer*. The declaration of an inline static data
|
| 1828 |
+
member (which is a definition) may specify a
|
| 1829 |
*brace-or-equal-initializer*. If the member is declared with the
|
| 1830 |
`constexpr` specifier, it may be redeclared in namespace scope with no
|
| 1831 |
initializer (this usage is deprecated; see [[depr.static.constexpr]]).
|
| 1832 |
Declarations of other static data members shall not specify a
|
| 1833 |
*brace-or-equal-initializer*.
|
| 1834 |
|
| 1835 |
+
[*Note 3*: There is exactly one definition of a static data member that
|
| 1836 |
+
is odr-used [[term.odr.use]] in a valid program. — *end note*]
|
| 1837 |
|
| 1838 |
+
[*Note 4*: Static data members of a class in namespace scope have the
|
| 1839 |
linkage of the name of the class [[basic.link]]. — *end note*]
|
| 1840 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1841 |
### Bit-fields <a id="class.bit">[[class.bit]]</a>
|
| 1842 |
|
| 1843 |
A *member-declarator* of the form
|
| 1844 |
|
| 1845 |
``` bnf
|
| 1846 |
identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
|
| 1847 |
```
|
| 1848 |
|
| 1849 |
specifies a bit-field. The optional *attribute-specifier-seq* appertains
|
| 1850 |
to the entity being declared. A bit-field shall not be a static member.
|
| 1851 |
+
A bit-field shall have integral or (possibly cv-qualified) enumeration
|
| 1852 |
+
type; the bit-field semantic property is not part of the type of the
|
| 1853 |
+
class member. The *constant-expression* shall be an integral constant
|
| 1854 |
+
expression with a value greater than or equal to zero and is called the
|
| 1855 |
+
*width* of the bit-field. If the width of a bit-field is larger than the
|
| 1856 |
+
width of the bit-field’s type (or, in case of an enumeration type, of
|
| 1857 |
+
its underlying type), the extra bits are padding bits
|
| 1858 |
+
[[term.padding.bits]]. Allocation of bit-fields within a class object is
|
| 1859 |
+
*implementation-defined*. Alignment of bit-fields is
|
| 1860 |
+
*implementation-defined*. Bit-fields are packed into some addressable
|
| 1861 |
+
allocation unit.
|
| 1862 |
|
| 1863 |
[*Note 1*: Bit-fields straddle allocation units on some machines and
|
| 1864 |
not on others. Bit-fields are assigned right-to-left on some machines,
|
| 1865 |
left-to-right on others. — *end note*]
|
| 1866 |
|
|
|
|
| 1875 |
As a special case, an unnamed bit-field with a width of zero specifies
|
| 1876 |
alignment of the next bit-field at an allocation unit boundary. Only
|
| 1877 |
when declaring an unnamed bit-field may the width be zero.
|
| 1878 |
|
| 1879 |
The address-of operator `&` shall not be applied to a bit-field, so
|
| 1880 |
+
there are no pointers to bit-fields. A non-const reference shall not
|
| 1881 |
+
bind to a bit-field [[dcl.init.ref]].
|
| 1882 |
|
| 1883 |
[*Note 3*: If the initializer for a reference of type `const` `T&` is
|
| 1884 |
an lvalue that refers to a bit-field, the reference is bound to a
|
| 1885 |
temporary initialized to hold the value of the bit-field; the reference
|
| 1886 |
is not bound to the bit-field directly. See
|
|
|
|
| 1913 |
}
|
| 1914 |
```
|
| 1915 |
|
| 1916 |
— *end example*]
|
| 1917 |
|
| 1918 |
+
### Allocation and deallocation functions <a id="class.free">[[class.free]]</a>
|
| 1919 |
+
|
| 1920 |
+
Any allocation function for a class `T` is a static member (even if not
|
| 1921 |
+
explicitly declared `static`).
|
| 1922 |
+
|
| 1923 |
+
[*Example 1*:
|
| 1924 |
+
|
| 1925 |
+
``` cpp
|
| 1926 |
+
class Arena;
|
| 1927 |
+
struct B {
|
| 1928 |
+
void* operator new(std::size_t, Arena*);
|
| 1929 |
+
};
|
| 1930 |
+
struct D1 : B {
|
| 1931 |
+
};
|
| 1932 |
+
|
| 1933 |
+
Arena* ap;
|
| 1934 |
+
void foo(int i) {
|
| 1935 |
+
new (ap) D1; // calls B::operator new(std::size_t, Arena*)
|
| 1936 |
+
new D1[i]; // calls ::operator new[](std::size_t)
|
| 1937 |
+
new D1; // error: ::operator new(std::size_t) hidden
|
| 1938 |
+
}
|
| 1939 |
+
```
|
| 1940 |
+
|
| 1941 |
+
— *end example*]
|
| 1942 |
+
|
| 1943 |
+
Any deallocation function for a class `X` is a static member (even if
|
| 1944 |
+
not explicitly declared `static`).
|
| 1945 |
+
|
| 1946 |
+
[*Example 2*:
|
| 1947 |
+
|
| 1948 |
+
``` cpp
|
| 1949 |
+
class X {
|
| 1950 |
+
void operator delete(void*);
|
| 1951 |
+
void operator delete[](void*, std::size_t);
|
| 1952 |
+
};
|
| 1953 |
+
|
| 1954 |
+
class Y {
|
| 1955 |
+
void operator delete(void*, std::size_t);
|
| 1956 |
+
void operator delete[](void*);
|
| 1957 |
+
};
|
| 1958 |
+
```
|
| 1959 |
+
|
| 1960 |
+
— *end example*]
|
| 1961 |
+
|
| 1962 |
+
Since member allocation and deallocation functions are `static` they
|
| 1963 |
+
cannot be virtual.
|
| 1964 |
+
|
| 1965 |
+
[*Note 1*:
|
| 1966 |
+
|
| 1967 |
+
However, when the *cast-expression* of a *delete-expression* refers to
|
| 1968 |
+
an object of class type with a virtual destructor, because the
|
| 1969 |
+
deallocation function is chosen by the destructor of the dynamic type of
|
| 1970 |
+
the object, the effect is the same in that case. For example,
|
| 1971 |
+
|
| 1972 |
+
``` cpp
|
| 1973 |
+
struct B {
|
| 1974 |
+
virtual ~B();
|
| 1975 |
+
void operator delete(void*, std::size_t);
|
| 1976 |
+
};
|
| 1977 |
+
|
| 1978 |
+
struct D : B {
|
| 1979 |
+
void operator delete(void*);
|
| 1980 |
+
};
|
| 1981 |
+
|
| 1982 |
+
struct E : B {
|
| 1983 |
+
void log_deletion();
|
| 1984 |
+
void operator delete(E *p, std::destroying_delete_t) {
|
| 1985 |
+
p->log_deletion();
|
| 1986 |
+
p->~E();
|
| 1987 |
+
::operator delete(p);
|
| 1988 |
+
}
|
| 1989 |
+
};
|
| 1990 |
+
|
| 1991 |
+
void f() {
|
| 1992 |
+
B* bp = new D;
|
| 1993 |
+
delete bp; // 1: uses D::operator delete(void*)
|
| 1994 |
+
bp = new E;
|
| 1995 |
+
delete bp; // 2: uses E::operator delete(E*, std::destroying_delete_t)
|
| 1996 |
+
}
|
| 1997 |
+
```
|
| 1998 |
+
|
| 1999 |
+
Here, storage for the object of class `D` is deallocated by
|
| 2000 |
+
`D::operator delete()`, and the object of class `E` is destroyed and its
|
| 2001 |
+
storage is deallocated by `E::operator delete()`, due to the virtual
|
| 2002 |
+
destructor.
|
| 2003 |
+
|
| 2004 |
+
— *end note*]
|
| 2005 |
+
|
| 2006 |
+
[*Note 2*:
|
| 2007 |
+
|
| 2008 |
+
Virtual destructors have no effect on the deallocation function actually
|
| 2009 |
+
called when the *cast-expression* of a *delete-expression* refers to an
|
| 2010 |
+
array of objects of class type. For example,
|
| 2011 |
+
|
| 2012 |
+
``` cpp
|
| 2013 |
+
struct B {
|
| 2014 |
+
virtual ~B();
|
| 2015 |
+
void operator delete[](void*, std::size_t);
|
| 2016 |
+
};
|
| 2017 |
+
|
| 2018 |
+
struct D : B {
|
| 2019 |
+
void operator delete[](void*, std::size_t);
|
| 2020 |
+
};
|
| 2021 |
+
|
| 2022 |
+
void f(int i) {
|
| 2023 |
+
D* dp = new D[i];
|
| 2024 |
+
delete [] dp; // uses D::operator delete[](void*, std::size_t)
|
| 2025 |
+
B* bp = new D[i];
|
| 2026 |
+
delete[] bp; // undefined behavior
|
| 2027 |
+
}
|
| 2028 |
+
```
|
| 2029 |
+
|
| 2030 |
+
— *end note*]
|
| 2031 |
+
|
| 2032 |
+
Access to the deallocation function is checked statically, even if a
|
| 2033 |
+
different one is actually executed.
|
| 2034 |
+
|
| 2035 |
+
[*Example 3*: For the call on line “// 1” above, if
|
| 2036 |
+
`B::operator delete()` had been private, the delete expression would
|
| 2037 |
+
have been ill-formed. — *end example*]
|
| 2038 |
+
|
| 2039 |
+
[*Note 3*: If a deallocation function has no explicit
|
| 2040 |
+
*noexcept-specifier*, it has a non-throwing exception specification
|
| 2041 |
+
[[except.spec]]. — *end note*]
|
| 2042 |
+
|
| 2043 |
### Nested class declarations <a id="class.nest">[[class.nest]]</a>
|
| 2044 |
|
| 2045 |
A class can be declared within another class. A class declared within
|
| 2046 |
+
another is called a *nested class*.
|
|
|
|
|
|
|
| 2047 |
|
| 2048 |
[*Note 1*: See [[expr.prim.id]] for restrictions on the use of
|
| 2049 |
non-static data members and non-static member functions. — *end note*]
|
| 2050 |
|
| 2051 |
[*Example 1*:
|
|
|
|
| 2058 |
int x;
|
| 2059 |
static int s;
|
| 2060 |
|
| 2061 |
struct inner {
|
| 2062 |
void f(int i) {
|
| 2063 |
+
int a = sizeof(x); // OK, operand of sizeof is an unevaluated operand
|
| 2064 |
x = i; // error: assign to enclose::x
|
| 2065 |
+
s = i; // OK, assign to enclose::s
|
| 2066 |
+
::x = i; // OK, assign to global x
|
| 2067 |
+
y = i; // OK, assign to global y
|
| 2068 |
}
|
| 2069 |
void g(enclose* p, int i) {
|
| 2070 |
+
p->x = i; // OK, assign to enclose::x
|
| 2071 |
}
|
| 2072 |
};
|
| 2073 |
};
|
| 2074 |
|
| 2075 |
+
inner* p = 0; // error: inner not found
|
| 2076 |
```
|
| 2077 |
|
| 2078 |
— *end example*]
|
| 2079 |
|
| 2080 |
+
[*Note 2*:
|
| 2081 |
+
|
| 2082 |
+
Nested classes can be defined either in the enclosing class or in an
|
| 2083 |
+
enclosing namespace; member functions and static data members of a
|
| 2084 |
+
nested class can be defined either in the nested class or in an
|
| 2085 |
+
enclosing namespace scope.
|
| 2086 |
|
| 2087 |
[*Example 2*:
|
| 2088 |
|
| 2089 |
``` cpp
|
| 2090 |
struct enclose {
|
|
|
|
| 2095 |
};
|
| 2096 |
|
| 2097 |
int enclose::inner::x = 1;
|
| 2098 |
|
| 2099 |
void enclose::inner::f(int i) { ... }
|
|
|
|
| 2100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2101 |
class E {
|
| 2102 |
class I1; // forward declaration of nested class
|
| 2103 |
class I2;
|
| 2104 |
class I1 { }; // definition of nested class
|
| 2105 |
};
|
| 2106 |
class E::I2 { }; // definition of nested class
|
| 2107 |
```
|
| 2108 |
|
| 2109 |
— *end example*]
|
| 2110 |
|
| 2111 |
+
— *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2112 |
|
| 2113 |
+
A friend function [[class.friend]] defined within a nested class has no
|
| 2114 |
+
special access rights to members of an enclosing class.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2115 |
|