- tmp/tmpba7gq7h0/{from.md → to.md} +977 -1375
tmp/tmpba7gq7h0/{from.md → to.md}
RENAMED
|
@@ -43,37 +43,53 @@ class-key:
|
|
| 43 |
union
|
| 44 |
```
|
| 45 |
|
| 46 |
A class declaration where the *class-name* in the *class-head-name* is a
|
| 47 |
*simple-template-id* shall be an explicit specialization
|
| 48 |
-
[[temp.expl.spec]] or a partial specialization [[temp.
|
| 49 |
*class-specifier* whose *class-head* omits the *class-head-name* defines
|
| 50 |
an unnamed class.
|
| 51 |
|
| 52 |
[*Note 1*: An unnamed class thus can’t be `final`. — *end note*]
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
*
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
*class
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
If a *class-head-name* contains a *nested-name-specifier*, the
|
| 67 |
-
*class-specifier* shall
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
[*Note 2*: The *class-key* determines whether the class is a union
|
| 77 |
[[class.union]] and whether access is public or private by default
|
| 78 |
[[class.access]]. A union holds the value of at most one data member at
|
| 79 |
a time. — *end note*]
|
|
@@ -82,45 +98,45 @@ If a class is marked with the *class-virt-specifier* `final` and it
|
|
| 82 |
appears as a *class-or-decltype* in a *base-clause* [[class.derived]],
|
| 83 |
the program is ill-formed. Whenever a *class-key* is followed by a
|
| 84 |
*class-head-name*, the *identifier* `final`, and a colon or left brace,
|
| 85 |
`final` is interpreted as a *class-virt-specifier*.
|
| 86 |
|
| 87 |
-
[*Example
|
| 88 |
|
| 89 |
``` cpp
|
| 90 |
struct A;
|
| 91 |
-
struct A final {}; // OK
|
| 92 |
// not value-initialization of variable final
|
| 93 |
|
| 94 |
struct X {
|
| 95 |
struct C { constexpr operator int() { return 5; } };
|
| 96 |
-
struct B final : C{}; // OK
|
| 97 |
// not declaration of a bit-field member final
|
| 98 |
};
|
| 99 |
```
|
| 100 |
|
| 101 |
— *end example*]
|
| 102 |
|
| 103 |
[*Note 3*: Complete objects of class type have nonzero size. Base class
|
| 104 |
subobjects and members declared with the `no_unique_address` attribute
|
| 105 |
[[dcl.attr.nouniqueaddr]] are not so constrained. — *end note*]
|
| 106 |
|
| 107 |
-
[*Note 4*: Class objects can be assigned
|
| 108 |
-
[[class.copy.assign]]
|
| 109 |
-
[[class.copy.ctor]]
|
| 110 |
-
classes for which copying or moving has been restricted; see
|
| 111 |
[[dcl.fct.def.delete]] and [[class.access]]). Other plausible operators,
|
| 112 |
such as equality comparison, can be defined by the user; see
|
| 113 |
[[over.oper]]. — *end note*]
|
| 114 |
|
| 115 |
## Properties of classes <a id="class.prop">[[class.prop]]</a>
|
| 116 |
|
| 117 |
A *trivially copyable class* is a class:
|
| 118 |
|
| 119 |
- that has at least one eligible copy constructor, move constructor,
|
| 120 |
-
copy assignment operator, or move assignment operator
|
| 121 |
-
[[class.copy.ctor]], [[class.copy.assign]]
|
| 122 |
- where each eligible copy constructor, move constructor, copy
|
| 123 |
assignment operator, and move assignment operator is trivial, and
|
| 124 |
- that has a trivial, non-deleted destructor [[class.dtor]].
|
| 125 |
|
| 126 |
A *trivial class* is a class that is trivially copyable and has one or
|
|
@@ -143,13 +159,13 @@ A class `S` is a *standard-layout class* if it:
|
|
| 143 |
- has all non-static data members and bit-fields in the class and its
|
| 144 |
base classes first declared in the same class, and
|
| 145 |
- has no element of the set M(S) of types as a base class, where for any
|
| 146 |
type `X`, M(X) is defined as follows.[^1]
|
| 147 |
\[*Note 2*: M(X) is the set of the types of all non-base-class
|
| 148 |
-
subobjects that
|
| 149 |
-
- If `X` is a non-union class type with no
|
| 150 |
-
|
| 151 |
- If `X` is a non-union class type with a non-static data member of
|
| 152 |
type X₀ that is either of zero size or is the first non-static data
|
| 153 |
member of `X` (where said member may be an anonymous union), the set
|
| 154 |
M(X) consists of X₀ and the elements of M(X₀).
|
| 155 |
- If `X` is a union type, the set M(X) is the union of all M(Uᵢ) and
|
|
@@ -214,13 +230,15 @@ struct POD { // both trivial and standard-layout
|
|
| 214 |
— *end example*]
|
| 215 |
|
| 216 |
[*Note 4*: Aggregates of class type are described in
|
| 217 |
[[dcl.init.aggr]]. — *end note*]
|
| 218 |
|
| 219 |
-
A class `S` is an *implicit-lifetime class* if
|
| 220 |
-
|
| 221 |
-
destructor
|
|
|
|
|
|
|
| 222 |
|
| 223 |
## Class names <a id="class.name">[[class.name]]</a>
|
| 224 |
|
| 225 |
A class definition introduces a new type.
|
| 226 |
|
|
@@ -246,53 +264,49 @@ are type mismatches, and that
|
|
| 246 |
``` cpp
|
| 247 |
int f(X);
|
| 248 |
int f(Y);
|
| 249 |
```
|
| 250 |
|
| 251 |
-
declare
|
| 252 |
-
|
| 253 |
|
| 254 |
``` cpp
|
| 255 |
struct S { int a; };
|
| 256 |
struct S { int a; }; // error: double definition
|
| 257 |
```
|
| 258 |
|
| 259 |
is ill-formed because it defines `S` twice.
|
| 260 |
|
| 261 |
— *end example*]
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
the class can be referred to only using an *elaborated-type-specifier*
|
| 269 |
-
[[basic.lookup.elab]].
|
| 270 |
|
| 271 |
[*Example 2*:
|
| 272 |
|
| 273 |
``` cpp
|
| 274 |
struct stat {
|
| 275 |
// ...
|
| 276 |
};
|
| 277 |
|
| 278 |
stat gstat; // use plain stat to define variable
|
| 279 |
|
| 280 |
-
int stat(struct stat*); //
|
| 281 |
|
| 282 |
void f() {
|
| 283 |
struct stat* ps; // struct prefix needed to name struct stat
|
| 284 |
-
stat(ps); // call stat
|
| 285 |
}
|
| 286 |
```
|
| 287 |
|
| 288 |
— *end example*]
|
| 289 |
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
declaration of the identifier as a class name. It introduces the class
|
| 293 |
-
name into the current scope.
|
| 294 |
|
| 295 |
[*Example 3*:
|
| 296 |
|
| 297 |
``` cpp
|
| 298 |
struct s { int a; };
|
|
@@ -305,12 +319,10 @@ void g() {
|
|
| 305 |
}
|
| 306 |
```
|
| 307 |
|
| 308 |
— *end example*]
|
| 309 |
|
| 310 |
-
[*Note 1*:
|
| 311 |
-
|
| 312 |
Such declarations allow definition of classes that refer to each other.
|
| 313 |
|
| 314 |
[*Example 4*:
|
| 315 |
|
| 316 |
``` cpp
|
|
@@ -334,12 +346,12 @@ functions in [[over.oper]].
|
|
| 334 |
|
| 335 |
— *end note*]
|
| 336 |
|
| 337 |
[*Note 2*: An *elaborated-type-specifier* [[dcl.type.elab]] can also be
|
| 338 |
used as a *type-specifier* as part of a declaration. It differs from a
|
| 339 |
-
class declaration in that
|
| 340 |
-
|
| 341 |
|
| 342 |
[*Example 5*:
|
| 343 |
|
| 344 |
``` cpp
|
| 345 |
struct s { int a; };
|
|
@@ -372,10 +384,12 @@ artistry with names can be confusing and is best avoided.
|
|
| 372 |
A *simple-template-id* is only a *class-name* if its *template-name*
|
| 373 |
names a class template.
|
| 374 |
|
| 375 |
## Class members <a id="class.mem">[[class.mem]]</a>
|
| 376 |
|
|
|
|
|
|
|
| 377 |
``` bnf
|
| 378 |
member-specification:
|
| 379 |
member-declaration member-specificationₒₚₜ
|
| 380 |
access-specifier ':' member-specificationₒₚₜ
|
| 381 |
```
|
|
@@ -425,27 +439,29 @@ virt-specifier:
|
|
| 425 |
pure-specifier:
|
| 426 |
'=' '0'
|
| 427 |
```
|
| 428 |
|
| 429 |
The *member-specification* in a class definition declares the full set
|
| 430 |
-
of members of the class; no member can be added elsewhere. A
|
| 431 |
-
member* of a class `X` is a member of `X` that was first
|
| 432 |
-
the *member-specification* of `X`, including anonymous
|
| 433 |
-
[[class.union.anon]] and direct members thereof. Members
|
| 434 |
-
data members, member functions [[class.mfct]], nested
|
| 435 |
-
enumerators, and member templates [[temp.mem]] and
|
| 436 |
-
thereof.
|
| 437 |
|
| 438 |
[*Note 1*: A specialization of a static data member template is a
|
| 439 |
static data member. A specialization of a member function template is a
|
| 440 |
member function. A specialization of a member class template is a nested
|
| 441 |
class. — *end note*]
|
| 442 |
|
| 443 |
A *member-declaration* does not declare new members of the class if it
|
| 444 |
is
|
| 445 |
|
| 446 |
- a friend declaration [[class.friend]],
|
|
|
|
|
|
|
| 447 |
- a *static_assert-declaration*,
|
| 448 |
- a *using-declaration* [[namespace.udecl]], or
|
| 449 |
- an *empty-declaration*.
|
| 450 |
|
| 451 |
For any other *member-declaration*, each declared entity that is not an
|
|
@@ -453,23 +469,23 @@ unnamed bit-field [[class.bit]] is a member of the class, and each such
|
|
| 453 |
*member-declaration* shall either declare at least one member name of
|
| 454 |
the class or declare at least one unnamed bit-field.
|
| 455 |
|
| 456 |
A *data member* is a non-function member introduced by a
|
| 457 |
*member-declarator*. A *member function* is a member that is a function.
|
| 458 |
-
Nested types are classes
|
| 459 |
-
|
| 460 |
-
|
| 461 |
*alias-declaration*. The enumerators of an unscoped enumeration
|
| 462 |
[[dcl.enum]] defined in the class are members of the class.
|
| 463 |
|
| 464 |
A data member or member function may be declared `static` in its
|
| 465 |
*member-declaration*, in which case it is a *static member* (see
|
| 466 |
[[class.static]]) (a *static data member* [[class.static.data]] or
|
| 467 |
*static member function* [[class.static.mfct]], respectively) of the
|
| 468 |
class. Any other data member or member function is a *non-static member*
|
| 469 |
-
(a *non-static data member* or *non-static member function*
|
| 470 |
-
[[class.mfct.non
|
| 471 |
|
| 472 |
[*Note 2*: A non-static data member of non-reference type is a member
|
| 473 |
subobject of a class object [[intro.object]]. — *end note*]
|
| 474 |
|
| 475 |
A member shall not be declared twice in the *member-specification*,
|
|
@@ -479,31 +495,36 @@ except that
|
|
| 479 |
defined, and
|
| 480 |
- an enumeration can be introduced with an *opaque-enum-declaration* and
|
| 481 |
later redeclared with an *enum-specifier*.
|
| 482 |
|
| 483 |
[*Note 3*: A single name can denote several member functions provided
|
| 484 |
-
their types are sufficiently different
|
|
|
|
| 485 |
|
| 486 |
-
A
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
- function body [[dcl.fct.def.general]],
|
| 489 |
- default argument [[dcl.fct.default]],
|
|
|
|
| 490 |
- *noexcept-specifier* [[except.spec]], or
|
| 491 |
- default member initializer
|
| 492 |
|
| 493 |
-
within the *member-specification* of the class.
|
| 494 |
|
| 495 |
[*Note 4*: A complete-class context of a nested class is also a
|
| 496 |
complete-class context of any enclosing class, if the nested class is
|
| 497 |
defined within the *member-specification* of the enclosing
|
| 498 |
class. — *end note*]
|
| 499 |
|
| 500 |
-
A class is
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
otherwise it is regarded as incomplete within its own class
|
| 504 |
-
*member-specification*.
|
| 505 |
|
| 506 |
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 507 |
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 508 |
has function type, otherwise it is interpreted as introducing a
|
| 509 |
*brace-or-equal-initializer*.
|
|
@@ -511,12 +532,12 @@ has function type, otherwise it is interpreted as introducing a
|
|
| 511 |
[*Example 1*:
|
| 512 |
|
| 513 |
``` cpp
|
| 514 |
struct S {
|
| 515 |
using T = void();
|
| 516 |
-
T * p = 0; // OK
|
| 517 |
-
virtual T f = 0; // OK
|
| 518 |
};
|
| 519 |
```
|
| 520 |
|
| 521 |
— *end example*]
|
| 522 |
|
|
@@ -546,11 +567,15 @@ data member. (For static data members, see [[class.static.data]]; for
|
|
| 546 |
non-static data members, see [[class.base.init]] and
|
| 547 |
[[dcl.init.aggr]]). A *brace-or-equal-initializer* for a non-static data
|
| 548 |
member specifies a *default member initializer* for the member, and
|
| 549 |
shall not directly or indirectly cause the implicit definition of a
|
| 550 |
defaulted default constructor for the enclosing class or the exception
|
| 551 |
-
specification of that constructor.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
|
| 553 |
A member shall not be declared with the `extern`
|
| 554 |
*storage-class-specifier*. Within a class definition, a member shall not
|
| 555 |
be declared with the `thread_local` *storage-class-specifier* unless
|
| 556 |
also declared `static`.
|
|
@@ -571,12 +596,12 @@ it shall not appear if the optional *member-declarator-list* is omitted.
|
|
| 571 |
A *virt-specifier-seq* shall contain at most one of each
|
| 572 |
*virt-specifier*. A *virt-specifier-seq* shall appear only in the first
|
| 573 |
declaration of a virtual member function [[class.virtual]].
|
| 574 |
|
| 575 |
The type of a non-static data member shall not be an incomplete type
|
| 576 |
-
[[
|
| 577 |
-
(possibly
|
| 578 |
|
| 579 |
[*Note 5*: In particular, a class `C` cannot contain a non-static
|
| 580 |
member of class `C`, but it can contain a pointer or reference to an
|
| 581 |
object of class `C`. — *end note*]
|
| 582 |
|
|
@@ -615,45 +640,47 @@ object to which `sp` points; `s.left` refers to the `left` subtree
|
|
| 615 |
pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
| 616 |
character of the `tword` member of the `right` subtree of `s`.
|
| 617 |
|
| 618 |
— *end example*]
|
| 619 |
|
| 620 |
-
[*Note 8*: Non-static data members of
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
immediately after each other; so might requirements for space for
|
| 627 |
-
managing virtual functions [[class.virtual]] and virtual base classes
|
| 628 |
[[class.mi]]. — *end note*]
|
| 629 |
|
| 630 |
If `T` is the name of a class, then each of the following shall have a
|
| 631 |
name different from `T`:
|
| 632 |
|
| 633 |
- every static data member of class `T`;
|
| 634 |
-
- every member function of class `T` \[*Note 9*: This restriction does
|
| 635 |
not apply to constructors, which do not have names
|
| 636 |
-
[[class.ctor]] — *end note*]
|
| 637 |
- every member of class `T` that is itself a type;
|
| 638 |
- every member template of class `T`;
|
| 639 |
- every enumerator of every member of class `T` that is an unscoped
|
| 640 |
-
|
| 641 |
- every member of every anonymous union that is a member of class `T`.
|
| 642 |
|
| 643 |
In addition, if class `T` has a user-declared constructor
|
| 644 |
[[class.ctor]], every non-static data member of class `T` shall have a
|
| 645 |
name different from `T`.
|
| 646 |
|
| 647 |
The *common initial sequence* of two standard-layout struct
|
| 648 |
[[class.prop]] types is the longest sequence of non-static data members
|
| 649 |
and bit-fields in declaration order, starting with the first such entity
|
| 650 |
-
in each of the structs, such that
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 655 |
|
| 656 |
[*Example 4*:
|
| 657 |
|
| 658 |
``` cpp
|
| 659 |
struct A { int a; char b; };
|
|
@@ -674,11 +701,12 @@ Two standard-layout struct [[class.prop]] types are *layout-compatible
|
|
| 674 |
classes* if their common initial sequence comprises all members and
|
| 675 |
bit-fields of both classes [[basic.types]].
|
| 676 |
|
| 677 |
Two standard-layout unions are layout-compatible if they have the same
|
| 678 |
number of non-static data members and corresponding non-static data
|
| 679 |
-
members (in any order) have layout-compatible types
|
|
|
|
| 680 |
|
| 681 |
In a standard-layout union with an active member [[class.union]] of
|
| 682 |
struct type `T1`, it is permitted to read a non-static data member `m`
|
| 683 |
of another union member of struct type `T2` provided `m` is part of the
|
| 684 |
common initial sequence of `T1` and `T2`; the behavior is as if the
|
|
@@ -704,55 +732,27 @@ type has undefined behavior [[dcl.type.cv]]. — *end note*]
|
|
| 704 |
If a standard-layout class object has any non-static data members, its
|
| 705 |
address is the same as the address of its first non-static data member
|
| 706 |
if that member is not a bit-field. Its address is also the same as the
|
| 707 |
address of each of its base class subobjects.
|
| 708 |
|
| 709 |
-
[*Note 11*: There
|
| 710 |
standard-layout struct object inserted by an implementation, but not at
|
| 711 |
its beginning, as necessary to achieve appropriate
|
| 712 |
alignment. — *end note*]
|
| 713 |
|
| 714 |
[*Note 12*: The object and its first subobject are
|
| 715 |
-
pointer-interconvertible
|
| 716 |
-
[[expr.static.cast]]
|
| 717 |
|
| 718 |
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 719 |
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
if it is attached to the global module, or it may be defined outside of
|
| 723 |
-
its class definition if it has already been declared but not defined in
|
| 724 |
-
its class definition.
|
| 725 |
|
| 726 |
[*Note 1*: A member function is also inline if it is declared `inline`,
|
| 727 |
`constexpr`, or `consteval`. — *end note*]
|
| 728 |
|
| 729 |
-
A member function definition that appears outside of the class
|
| 730 |
-
definition shall appear in a namespace scope enclosing the class
|
| 731 |
-
definition. Except for member function definitions that appear outside
|
| 732 |
-
of a class definition, and except for explicit specializations of member
|
| 733 |
-
functions of class templates and member function templates [[temp.spec]]
|
| 734 |
-
appearing outside of the class definition, a member function shall not
|
| 735 |
-
be redeclared.
|
| 736 |
-
|
| 737 |
-
[*Note 2*: There can be at most one definition of a non-inline member
|
| 738 |
-
function in a program. There may be more than one inline member function
|
| 739 |
-
definition in a program. See [[basic.def.odr]] and
|
| 740 |
-
[[dcl.inline]]. — *end note*]
|
| 741 |
-
|
| 742 |
-
[*Note 3*: Member functions of a class have the linkage of the name of
|
| 743 |
-
the class. See [[basic.link]]. — *end note*]
|
| 744 |
-
|
| 745 |
-
If the definition of a member function is lexically outside its class
|
| 746 |
-
definition, the member function name shall be qualified by its class
|
| 747 |
-
name using the `::` operator.
|
| 748 |
-
|
| 749 |
-
[*Note 4*: A name used in a member function definition (that is, in the
|
| 750 |
-
*parameter-declaration-clause* including the default arguments
|
| 751 |
-
[[dcl.fct.default]] or in the member function body) is looked up as
|
| 752 |
-
described in [[basic.lookup]]. — *end note*]
|
| 753 |
-
|
| 754 |
[*Example 1*:
|
| 755 |
|
| 756 |
``` cpp
|
| 757 |
struct X {
|
| 758 |
typedef int T;
|
|
@@ -760,30 +760,23 @@ struct X {
|
|
| 760 |
void f(T);
|
| 761 |
};
|
| 762 |
void X::f(T t = count) { }
|
| 763 |
```
|
| 764 |
|
| 765 |
-
The member function `f` of class `X`
|
| 766 |
-
notation `X::f`
|
| 767 |
-
and in the scope of class `X`. In the function
|
| 768 |
-
type `T` refers to the typedef member `T`
|
| 769 |
-
|
| 770 |
-
declared in class `X`.
|
| 771 |
|
| 772 |
— *end example*]
|
| 773 |
|
| 774 |
-
[*Note 5*: A `static` local variable or local type in a member function
|
| 775 |
-
always refers to the same entity, whether or not the member function is
|
| 776 |
-
inline. — *end note*]
|
| 777 |
-
|
| 778 |
-
Previously declared member functions may be mentioned in friend
|
| 779 |
-
declarations.
|
| 780 |
-
|
| 781 |
Member functions of a local class shall be defined inline in their class
|
| 782 |
definition, if they are defined at all.
|
| 783 |
|
| 784 |
-
[*Note
|
| 785 |
|
| 786 |
A member function can be declared (but not defined) using a typedef for
|
| 787 |
a function type. The resulting member function has exactly the same type
|
| 788 |
as it would have if the function declarator were provided explicitly,
|
| 789 |
see [[dcl.fct]]. For example,
|
|
@@ -803,34 +796,29 @@ fvc S::* pmfv3 = &S::memfunc3;
|
|
| 803 |
|
| 804 |
Also see [[temp.arg]].
|
| 805 |
|
| 806 |
— *end note*]
|
| 807 |
|
| 808 |
-
### Non-static member functions <a id="class.mfct.non
|
| 809 |
|
| 810 |
A non-static member function may be called for an object of its class
|
| 811 |
type, or for an object of a class derived [[class.derived]] from its
|
| 812 |
-
class type, using the class member access syntax
|
| 813 |
-
[[over.match.call]]
|
| 814 |
-
directly using the function call syntax
|
| 815 |
-
[[over.match.call]]
|
| 816 |
-
class, or a member thereof, as described below.
|
| 817 |
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
non-static non-type member of some class `C`, and if either the
|
| 828 |
-
*id-expression* is potentially evaluated or `C` is `X` or a base class
|
| 829 |
-
of `X`, the *id-expression* is transformed into a class member access
|
| 830 |
-
expression [[expr.ref]] using `(*this)` [[class.this]] as the
|
| 831 |
-
*postfix-expression* to the left of the `.` operator.
|
| 832 |
|
| 833 |
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 834 |
access expression is ill-formed. — *end note*]
|
| 835 |
|
| 836 |
This transformation does not apply in the template definition context
|
|
@@ -869,113 +857,29 @@ which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
|
| 869 |
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 870 |
not members of the class `tnode` and should be declared elsewhere.[^2]
|
| 871 |
|
| 872 |
— *end example*]
|
| 873 |
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
member function*, a member function declared `volatile` is a *volatile
|
| 879 |
-
member function* and a member function declared `const` `volatile` is a
|
| 880 |
-
*const volatile member function*.
|
| 881 |
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
``` cpp
|
| 885 |
-
struct X {
|
| 886 |
-
void g() const;
|
| 887 |
-
void h() const volatile;
|
| 888 |
-
};
|
| 889 |
-
```
|
| 890 |
-
|
| 891 |
-
`X::g` is a const member function and `X::h` is a const volatile member
|
| 892 |
-
function.
|
| 893 |
-
|
| 894 |
-
— *end example*]
|
| 895 |
-
|
| 896 |
-
A non-static member function may be declared with a *ref-qualifier*
|
| 897 |
-
[[dcl.fct]]; see [[over.match.funcs]].
|
| 898 |
-
|
| 899 |
-
A non-static member function may be declared virtual [[class.virtual]]
|
| 900 |
-
or pure virtual [[class.abstract]].
|
| 901 |
-
|
| 902 |
-
#### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 903 |
-
|
| 904 |
-
In the body of a non-static [[class.mfct]] member function, the keyword
|
| 905 |
-
`this` is a prvalue whose value is a pointer to the object for which the
|
| 906 |
-
function is called. The type of `this` in a member function whose type
|
| 907 |
-
has a *cv-qualifier-seq* cv and whose class is `X` is “pointer to cv
|
| 908 |
-
`X`”.
|
| 909 |
-
|
| 910 |
-
[*Note 1*: Thus in a const member function, the object for which the
|
| 911 |
-
function is called is accessed through a const access
|
| 912 |
-
path. — *end note*]
|
| 913 |
-
|
| 914 |
-
[*Example 1*:
|
| 915 |
-
|
| 916 |
-
``` cpp
|
| 917 |
-
struct s {
|
| 918 |
-
int a;
|
| 919 |
-
int f() const;
|
| 920 |
-
int g() { return a++; }
|
| 921 |
-
int h() const { return a++; } // error
|
| 922 |
-
};
|
| 923 |
-
|
| 924 |
-
int s::f() const { return a; }
|
| 925 |
-
```
|
| 926 |
-
|
| 927 |
-
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 928 |
-
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 929 |
-
in a const member function because `this` is a pointer to `const`; that
|
| 930 |
-
is, `*this` has `const` type.
|
| 931 |
-
|
| 932 |
-
— *end example*]
|
| 933 |
-
|
| 934 |
-
[*Note 2*: Similarly, `volatile` semantics [[dcl.type.cv]] apply in
|
| 935 |
-
volatile member functions when accessing the object and its non-static
|
| 936 |
-
data members. — *end note*]
|
| 937 |
-
|
| 938 |
-
A member function whose type has a *cv-qualifier-seq* *cv1* can be
|
| 939 |
-
called on an object expression [[expr.ref]] of type *cv2* `T` only if
|
| 940 |
-
*cv1* is the same as or more cv-qualified than *cv2*
|
| 941 |
-
[[basic.type.qualifier]].
|
| 942 |
-
|
| 943 |
-
[*Example 2*:
|
| 944 |
-
|
| 945 |
-
``` cpp
|
| 946 |
-
void k(s& x, const s& y) {
|
| 947 |
-
x.f();
|
| 948 |
-
x.g();
|
| 949 |
-
y.f();
|
| 950 |
-
y.g(); // error
|
| 951 |
-
}
|
| 952 |
-
```
|
| 953 |
-
|
| 954 |
-
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 955 |
-
non-const member function, that is, `s::g()` is less-qualified than the
|
| 956 |
-
object expression `y`.
|
| 957 |
-
|
| 958 |
-
— *end example*]
|
| 959 |
-
|
| 960 |
-
[*Note 3*: Constructors and destructors cannot be declared `const`,
|
| 961 |
-
`volatile`, or `const` `volatile`. However, these functions can be
|
| 962 |
-
invoked to create and destroy objects with cv-qualified types; see
|
| 963 |
-
[[class.ctor]] and [[class.dtor]]. — *end note*]
|
| 964 |
|
| 965 |
### Special member functions <a id="special">[[special]]</a>
|
| 966 |
|
| 967 |
Default constructors [[class.default.ctor]], copy constructors, move
|
| 968 |
constructors [[class.copy.ctor]], copy assignment operators, move
|
| 969 |
assignment operators [[class.copy.assign]], and prospective destructors
|
| 970 |
[[class.dtor]] are *special member functions*.
|
| 971 |
|
| 972 |
[*Note 1*: The implementation will implicitly declare these member
|
| 973 |
functions for some class types when the program does not explicitly
|
| 974 |
-
declare them. The implementation will implicitly define them
|
| 975 |
-
|
| 976 |
-
[[expr.const]]. — *end note*]
|
| 977 |
|
| 978 |
An implicitly-declared special member function is declared at the
|
| 979 |
closing `}` of the *class-specifier*. Programs shall not define
|
| 980 |
implicitly-declared special member functions.
|
| 981 |
|
|
@@ -1031,38 +935,37 @@ which:
|
|
| 1031 |
For a class, its non-static data members, its non-virtual direct base
|
| 1032 |
classes, and, if the class is not abstract [[class.abstract]], its
|
| 1033 |
virtual base classes are called its *potentially constructed
|
| 1034 |
subobjects*.
|
| 1035 |
|
| 1036 |
-
A defaulted special member function is *constexpr-compatible* if the
|
| 1037 |
-
corresponding implicitly-declared special member function would be a
|
| 1038 |
-
constexpr function.
|
| 1039 |
-
|
| 1040 |
### Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 1041 |
|
| 1042 |
-
|
| 1043 |
-
|
|
|
|
|
|
|
| 1044 |
|
| 1045 |
``` bnf
|
| 1046 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1047 |
```
|
| 1048 |
|
| 1049 |
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 1050 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 1051 |
parentheses, and the *id-expression* has one of the following forms:
|
| 1052 |
|
| 1053 |
-
- in a
|
| 1054 |
-
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
-
|
| 1058 |
-
|
| 1059 |
-
|
|
|
|
| 1060 |
|
| 1061 |
Constructors do not have names. In a constructor declaration, each
|
| 1062 |
*decl-specifier* in the optional *decl-specifier-seq* shall be `friend`,
|
| 1063 |
-
`inline`, `constexpr`, or an *explicit-specifier*.
|
| 1064 |
|
| 1065 |
[*Example 1*:
|
| 1066 |
|
| 1067 |
``` cpp
|
| 1068 |
struct S {
|
|
@@ -1072,18 +975,17 @@ struct S {
|
|
| 1072 |
S::S() { } // defines the constructor
|
| 1073 |
```
|
| 1074 |
|
| 1075 |
— *end example*]
|
| 1076 |
|
| 1077 |
-
A constructor is used to initialize objects of its class type.
|
| 1078 |
-
constructors do not have names, they are never found during name lookup;
|
| 1079 |
-
however an explicit type conversion using the functional notation
|
| 1080 |
-
[[expr.type.conv]] will cause a constructor to be called to initialize
|
| 1081 |
-
an object.
|
| 1082 |
|
| 1083 |
-
[*Note 1*:
|
| 1084 |
-
|
|
|
|
|
|
|
|
|
|
| 1085 |
|
| 1086 |
[*Example 2*:
|
| 1087 |
|
| 1088 |
``` cpp
|
| 1089 |
complex zz = complex(1,2.3);
|
|
@@ -1110,15 +1012,19 @@ during construction; see [[class.base.init]] and
|
|
| 1110 |
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 1111 |
`volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
|
| 1112 |
not applied on an object under construction. They come into effect when
|
| 1113 |
the constructor for the most derived object [[intro.object]] ends.
|
| 1114 |
|
| 1115 |
-
|
| 1116 |
-
|
|
|
|
|
|
|
| 1117 |
|
| 1118 |
A constructor shall not be a coroutine.
|
| 1119 |
|
|
|
|
|
|
|
| 1120 |
#### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
|
| 1121 |
|
| 1122 |
A *default constructor* for a class `X` is a constructor of class `X`
|
| 1123 |
for which each parameter that is not a function parameter pack has a
|
| 1124 |
default argument (including the case of a constructor with no
|
|
@@ -1166,42 +1072,33 @@ A default constructor is *trivial* if it is not user-provided and if:
|
|
| 1166 |
type (or array thereof), each such class has a trivial default
|
| 1167 |
constructor.
|
| 1168 |
|
| 1169 |
Otherwise, the default constructor is *non-trivial*.
|
| 1170 |
|
| 1171 |
-
|
| 1172 |
-
*implicitly defined* when it is odr-used [[basic.def.odr]] to create an
|
| 1173 |
-
object of its class type [[intro.object]], when it is needed for
|
| 1174 |
-
constant evaluation [[expr.const]], or when it is explicitly defaulted
|
| 1175 |
-
after its first declaration. The implicitly-defined default constructor
|
| 1176 |
performs the set of initializations of the class that would be performed
|
| 1177 |
by a user-written default constructor for that class with no
|
| 1178 |
*ctor-initializer* [[class.base.init]] and an empty
|
| 1179 |
*compound-statement*. If that user-written default constructor would be
|
| 1180 |
ill-formed, the program is ill-formed. If that user-written default
|
| 1181 |
-
constructor would
|
| 1182 |
-
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
|
| 1187 |
[*Note 1*: An implicitly-declared default constructor has an exception
|
| 1188 |
specification [[except.spec]]. An explicitly-defaulted definition might
|
| 1189 |
have an implicit exception specification, see
|
| 1190 |
[[dcl.fct.def]]. — *end note*]
|
| 1191 |
|
| 1192 |
-
|
| 1193 |
-
|
| 1194 |
-
|
| 1195 |
-
[[
|
| 1196 |
-
duration [[basic.stc.dynamic]] created by a *new-expression* in which
|
| 1197 |
-
the *new-initializer* is omitted [[expr.new]], or are called when the
|
| 1198 |
-
explicit type conversion syntax [[expr.type.conv]] is used. A program is
|
| 1199 |
-
ill-formed if the default constructor for an object is implicitly used
|
| 1200 |
-
and the constructor is not accessible [[class.access]].
|
| 1201 |
|
| 1202 |
-
[*Note
|
| 1203 |
constructors for base classes and non-static data members are called and
|
| 1204 |
describes how arguments can be specified for the calls to these
|
| 1205 |
constructors. — *end note*]
|
| 1206 |
|
| 1207 |
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
|
@@ -1250,11 +1147,11 @@ Y e = d; // calls Y(const Y&)
|
|
| 1250 |
|
| 1251 |
— *end example*]
|
| 1252 |
|
| 1253 |
[*Note 1*:
|
| 1254 |
|
| 1255 |
-
All forms of copy/move constructor
|
| 1256 |
|
| 1257 |
[*Example 3*:
|
| 1258 |
|
| 1259 |
``` cpp
|
| 1260 |
struct X {
|
|
@@ -1316,25 +1213,26 @@ void h() {
|
|
| 1316 |
|
| 1317 |
If the class definition does not explicitly declare a copy constructor,
|
| 1318 |
a non-explicit one is declared *implicitly*. If the class definition
|
| 1319 |
declares a move constructor or move assignment operator, the implicitly
|
| 1320 |
declared copy constructor is defined as deleted; otherwise, it is
|
| 1321 |
-
|
| 1322 |
-
|
| 1323 |
-
|
| 1324 |
|
| 1325 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 1326 |
form
|
| 1327 |
|
| 1328 |
``` cpp
|
| 1329 |
X::X(const X&)
|
| 1330 |
```
|
| 1331 |
|
| 1332 |
if each potentially constructed subobject of a class type `M` (or array
|
| 1333 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 1334 |
-
`M&` or `const` `volatile` `M&`.[^3]
|
| 1335 |
-
|
|
|
|
| 1336 |
|
| 1337 |
``` cpp
|
| 1338 |
X::X(X&)
|
| 1339 |
```
|
| 1340 |
|
|
@@ -1347,11 +1245,11 @@ if and only if
|
|
| 1347 |
- `X` does not have a user-declared move assignment operator, and
|
| 1348 |
- `X` does not have a user-declared destructor.
|
| 1349 |
|
| 1350 |
[*Note 3*: When the move constructor is not implicitly declared or
|
| 1351 |
explicitly supplied, expressions that otherwise would have invoked the
|
| 1352 |
-
move constructor
|
| 1353 |
|
| 1354 |
The implicitly-declared move constructor for class `X` will have the
|
| 1355 |
form
|
| 1356 |
|
| 1357 |
``` cpp
|
|
@@ -1360,24 +1258,24 @@ X::X(X&&)
|
|
| 1360 |
|
| 1361 |
An implicitly-declared copy/move constructor is an inline public member
|
| 1362 |
of its class. A defaulted copy/move constructor for a class `X` is
|
| 1363 |
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 1364 |
|
| 1365 |
-
- a potentially constructed subobject type `M` (or array thereof)
|
| 1366 |
-
cannot be copied/moved because overload resolution
|
| 1367 |
-
applied to find `M`’s corresponding constructor,
|
| 1368 |
-
ambiguity or a function that is deleted or inaccessible
|
| 1369 |
-
defaulted constructor,
|
| 1370 |
- a variant member whose corresponding constructor as selected by
|
| 1371 |
overload resolution is non-trivial,
|
| 1372 |
- any potentially constructed subobject of a type with a destructor that
|
| 1373 |
is deleted or inaccessible from the defaulted constructor, or,
|
| 1374 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 1375 |
type.
|
| 1376 |
|
| 1377 |
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 1378 |
-
ignored by overload resolution
|
| 1379 |
constructor would otherwise interfere with initialization from an rvalue
|
| 1380 |
which can use the copy constructor instead. — *end note*]
|
| 1381 |
|
| 1382 |
A copy/move constructor for class `X` is trivial if it is not
|
| 1383 |
user-provided and if:
|
|
@@ -1390,22 +1288,17 @@ user-provided and if:
|
|
| 1390 |
thereof), the constructor selected to copy/move that member is
|
| 1391 |
trivial;
|
| 1392 |
|
| 1393 |
otherwise the copy/move constructor is *non-trivial*.
|
| 1394 |
|
| 1395 |
-
A copy/move constructor that is defaulted and not defined as deleted is
|
| 1396 |
-
*implicitly defined* when it is odr-used [[basic.def.odr]], when it is
|
| 1397 |
-
needed for constant evaluation [[expr.const]], or when it is explicitly
|
| 1398 |
-
defaulted after its first declaration.
|
| 1399 |
-
|
| 1400 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 1401 |
-
implementation elided its odr-use
|
| 1402 |
-
[[class.temporary]]
|
| 1403 |
|
| 1404 |
-
If
|
| 1405 |
-
|
| 1406 |
-
|
| 1407 |
|
| 1408 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 1409 |
defined, all non-user-provided copy/move constructors for its
|
| 1410 |
potentially constructed subobjects are implicitly defined.
|
| 1411 |
|
|
@@ -1434,27 +1327,27 @@ to its type:
|
|
| 1434 |
|
| 1435 |
Virtual base class subobjects shall be initialized only once by the
|
| 1436 |
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 1437 |
|
| 1438 |
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 1439 |
-
object representation [[
|
| 1440 |
-
within [[intro.object]] the object that is the source of
|
| 1441 |
-
corresponding object o nested within the destination is
|
| 1442 |
-
the object is a subobject) or created (otherwise), and
|
| 1443 |
-
begins before the copy is performed.
|
| 1444 |
|
| 1445 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 1446 |
|
| 1447 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 1448 |
non-static non-template member function of class `X` with exactly one
|
| 1449 |
-
parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
|
| 1450 |
`const volatile X&`.[^4]
|
| 1451 |
|
| 1452 |
[*Note 1*: An overloaded assignment operator must be declared to have
|
| 1453 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 1454 |
|
| 1455 |
-
[*Note 2*: More than one form of copy assignment operator
|
| 1456 |
declared for a class. — *end note*]
|
| 1457 |
|
| 1458 |
[*Note 3*:
|
| 1459 |
|
| 1460 |
If a class `X` only has a copy assignment operator with a parameter of
|
|
@@ -1480,12 +1373,12 @@ void f() {
|
|
| 1480 |
— *end note*]
|
| 1481 |
|
| 1482 |
If the class definition does not explicitly declare a copy assignment
|
| 1483 |
operator, one is declared *implicitly*. If the class definition declares
|
| 1484 |
a move constructor or move assignment operator, the implicitly declared
|
| 1485 |
-
copy assignment operator is defined as deleted; otherwise, it is
|
| 1486 |
-
|
| 1487 |
has a user-declared copy constructor or a user-declared destructor
|
| 1488 |
[[depr.impldec]]. The implicitly-declared copy assignment operator for a
|
| 1489 |
class `X` will have the form
|
| 1490 |
|
| 1491 |
``` cpp
|
|
@@ -1507,17 +1400,18 @@ the form
|
|
| 1507 |
``` cpp
|
| 1508 |
X& X::operator=(X&)
|
| 1509 |
```
|
| 1510 |
|
| 1511 |
A user-declared move assignment operator `X::operator=` is a non-static
|
| 1512 |
-
non-template member function of class `X` with exactly one
|
| 1513 |
-
type `X&&`, `const X&&`, `volatile X&&`, or
|
|
|
|
| 1514 |
|
| 1515 |
[*Note 4*: An overloaded assignment operator must be declared to have
|
| 1516 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 1517 |
|
| 1518 |
-
[*Note 5*: More than one form of move assignment operator
|
| 1519 |
declared for a class. — *end note*]
|
| 1520 |
|
| 1521 |
If the definition of a class `X` does not explicitly declare a move
|
| 1522 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1523 |
only if
|
|
@@ -1558,14 +1452,12 @@ have the form
|
|
| 1558 |
``` cpp
|
| 1559 |
X& X::operator=(X&&)
|
| 1560 |
```
|
| 1561 |
|
| 1562 |
The implicitly-declared copy/move assignment operator for class `X` has
|
| 1563 |
-
the return type `X&`
|
| 1564 |
-
operator is
|
| 1565 |
-
implicitly-declared copy/move assignment operator is an inline public
|
| 1566 |
-
member of its class.
|
| 1567 |
|
| 1568 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 1569 |
deleted if `X` has:
|
| 1570 |
|
| 1571 |
- a variant member with a non-trivial corresponding assignment operator
|
|
@@ -1579,23 +1471,23 @@ deleted if `X` has:
|
|
| 1579 |
corresponding assignment operator, results in an ambiguity or a
|
| 1580 |
function that is deleted or inaccessible from the defaulted assignment
|
| 1581 |
operator.
|
| 1582 |
|
| 1583 |
[*Note 6*: A defaulted move assignment operator that is defined as
|
| 1584 |
-
deleted is ignored by overload resolution
|
| 1585 |
-
[[over.over]]
|
| 1586 |
|
| 1587 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1588 |
class if not declared by the user, a base class copy/move assignment
|
| 1589 |
operator is always hidden by the corresponding assignment operator of a
|
| 1590 |
-
derived class [[over.ass]].
|
| 1591 |
-
|
| 1592 |
-
|
| 1593 |
-
|
| 1594 |
-
|
| 1595 |
-
|
| 1596 |
-
|
| 1597 |
|
| 1598 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1599 |
user-provided and if:
|
| 1600 |
|
| 1601 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
|
@@ -1606,31 +1498,19 @@ user-provided and if:
|
|
| 1606 |
thereof), the assignment operator selected to copy/move that member is
|
| 1607 |
trivial;
|
| 1608 |
|
| 1609 |
otherwise the copy/move assignment operator is *non-trivial*.
|
| 1610 |
|
| 1611 |
-
|
| 1612 |
-
|
| 1613 |
-
[[basic.def.odr]] (e.g., when it is selected by overload resolution to
|
| 1614 |
-
assign to an object of its class type), when it is needed for constant
|
| 1615 |
-
evaluation [[expr.const]], or when it is explicitly defaulted after its
|
| 1616 |
-
first declaration. The implicitly-defined copy/move assignment operator
|
| 1617 |
-
is `constexpr` if
|
| 1618 |
-
|
| 1619 |
-
- `X` is a literal type, and
|
| 1620 |
-
- the assignment operator selected to copy/move each direct base class
|
| 1621 |
-
subobject is a constexpr function, and
|
| 1622 |
-
- for each non-static data member of `X` that is of class type (or array
|
| 1623 |
-
thereof), the assignment operator selected to copy/move that member is
|
| 1624 |
-
a constexpr function.
|
| 1625 |
|
| 1626 |
Before the defaulted copy/move assignment operator for a class is
|
| 1627 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1628 |
for its direct base classes and its non-static data members are
|
| 1629 |
implicitly defined.
|
| 1630 |
|
| 1631 |
-
[*Note
|
| 1632 |
implied exception specification [[except.spec]]. — *end note*]
|
| 1633 |
|
| 1634 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 1635 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 1636 |
The direct base classes of `X` are assigned first, in the order of their
|
|
@@ -1668,21 +1548,27 @@ It is unspecified whether the virtual base class subobject `V` is
|
|
| 1668 |
assigned twice by the implicitly-defined copy/move assignment operator
|
| 1669 |
for `C`.
|
| 1670 |
|
| 1671 |
— *end example*]
|
| 1672 |
|
| 1673 |
-
The implicitly-defined copy assignment operator for a union `X`
|
| 1674 |
-
the object representation [[
|
| 1675 |
-
destination of the assignment are not the same object,
|
| 1676 |
-
object nested within [[intro.object]] the object that is
|
| 1677 |
-
the copy, a corresponding object o nested within the
|
| 1678 |
-
created, and the lifetime of o begins before the copy is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1679 |
|
| 1680 |
### Destructors <a id="class.dtor">[[class.dtor]]</a>
|
| 1681 |
|
| 1682 |
-
A
|
| 1683 |
-
|
|
|
|
| 1684 |
|
| 1685 |
``` bnf
|
| 1686 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1687 |
```
|
| 1688 |
|
|
@@ -1693,13 +1579,13 @@ parentheses, and the *id-expression* has one of the following forms:
|
|
| 1693 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 1694 |
of a class or class template but is not a friend declaration
|
| 1695 |
[[class.friend]], the *id-expression* is `~`*class-name* and the
|
| 1696 |
*class-name* is the injected-class-name [[class.pre]] of the
|
| 1697 |
immediately-enclosing entity or
|
| 1698 |
-
-
|
| 1699 |
-
*
|
| 1700 |
-
|
| 1701 |
|
| 1702 |
A prospective destructor shall take no arguments [[dcl.fct]]. Each
|
| 1703 |
*decl-specifier* of the *decl-specifier-seq* of a prospective destructor
|
| 1704 |
declaration (if any) shall be `friend`, `inline`, `virtual`,
|
| 1705 |
`constexpr`, or `consteval`.
|
|
@@ -1719,21 +1605,25 @@ the form
|
|
| 1719 |
At the end of the definition of a class, overload resolution is
|
| 1720 |
performed among the prospective destructors declared in that class with
|
| 1721 |
an empty argument list to select the *destructor* for the class, also
|
| 1722 |
known as the *selected destructor*. The program is ill-formed if
|
| 1723 |
overload resolution fails. Destructor selection does not constitute a
|
| 1724 |
-
reference to, or odr-use [[
|
| 1725 |
and in particular, the selected destructor may be deleted
|
| 1726 |
[[dcl.fct.def.delete]].
|
| 1727 |
|
| 1728 |
-
The address of a destructor shall not be taken.
|
| 1729 |
-
invoked for a `const`, `volatile` or `const` `volatile` object. `const`
|
| 1730 |
-
and `volatile` semantics [[dcl.type.cv]] are not applied on an object
|
| 1731 |
-
under destruction. They stop being in effect when the destructor for the
|
| 1732 |
-
most derived object [[intro.object]] starts.
|
| 1733 |
|
| 1734 |
-
[*Note 1*: A
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1735 |
*noexcept-specifier* has the same exception specification as if it had
|
| 1736 |
been implicitly declared [[except.spec]]. — *end note*]
|
| 1737 |
|
| 1738 |
A defaulted destructor for a class `X` is defined as deleted if:
|
| 1739 |
|
|
@@ -1746,37 +1636,31 @@ A defaulted destructor for a class `X` is defined as deleted if:
|
|
| 1746 |
function results in an ambiguity or in a function that is deleted or
|
| 1747 |
inaccessible from the defaulted destructor.
|
| 1748 |
|
| 1749 |
A destructor is trivial if it is not user-provided and if:
|
| 1750 |
|
| 1751 |
-
- the destructor is not
|
| 1752 |
- all of the direct base classes of its class have trivial destructors,
|
| 1753 |
and
|
| 1754 |
- for all of the non-static data members of its class that are of class
|
| 1755 |
type (or array thereof), each such class has a trivial destructor.
|
| 1756 |
|
| 1757 |
Otherwise, the destructor is *non-trivial*.
|
| 1758 |
|
| 1759 |
-
A defaulted destructor is a constexpr destructor if it
|
| 1760 |
-
|
| 1761 |
-
|
| 1762 |
-
A destructor that is defaulted and not defined as deleted is *implicitly
|
| 1763 |
-
defined* when it is odr-used [[basic.def.odr]] or when it is explicitly
|
| 1764 |
-
defaulted after its first declaration.
|
| 1765 |
|
| 1766 |
Before a defaulted destructor for a class is implicitly defined, all the
|
| 1767 |
non-user-provided destructors for its base classes and its non-static
|
| 1768 |
data members are implicitly defined.
|
| 1769 |
|
| 1770 |
-
A prospective destructor can be declared `virtual` [[class.virtual]]
|
| 1771 |
-
|
| 1772 |
-
virtual and any objects of that class or any derived class are
|
| 1773 |
-
in the program, the destructor shall be defined.
|
| 1774 |
-
class with a virtual destructor, its destructor (whether user- or
|
| 1775 |
-
implicitly-declared) is virtual.
|
| 1776 |
|
| 1777 |
-
[*Note
|
| 1778 |
during destruction; see [[class.cdtor]]. — *end note*]
|
| 1779 |
|
| 1780 |
After executing the body of the destructor and destroying any objects
|
| 1781 |
with automatic storage duration allocated within the body, a destructor
|
| 1782 |
for class `X` calls the destructors for `X`’s direct non-variant
|
|
@@ -1784,53 +1668,56 @@ non-static data members, the destructors for `X`’s non-virtual direct
|
|
| 1784 |
base classes and, if `X` is the most derived class [[class.base.init]],
|
| 1785 |
its destructor calls the destructors for `X`’s virtual base classes. All
|
| 1786 |
destructors are called as if they were referenced with a qualified name,
|
| 1787 |
that is, ignoring any possible virtual overriding destructors in more
|
| 1788 |
derived classes. Bases and members are destroyed in the reverse order of
|
| 1789 |
-
the completion of their constructor (see [[class.base.init]]).
|
| 1790 |
-
|
| 1791 |
-
|
| 1792 |
-
|
| 1793 |
-
|
| 1794 |
-
|
|
|
|
|
|
|
|
|
|
| 1795 |
|
| 1796 |
A destructor is invoked implicitly
|
| 1797 |
|
| 1798 |
- for a constructed object with static storage duration
|
| 1799 |
[[basic.stc.static]] at program termination [[basic.start.term]],
|
| 1800 |
- for a constructed object with thread storage duration
|
| 1801 |
[[basic.stc.thread]] at thread exit,
|
| 1802 |
- for a constructed object with automatic storage duration
|
| 1803 |
[[basic.stc.auto]] when the block in which an object is created exits
|
| 1804 |
[[stmt.dcl]],
|
| 1805 |
-
- for a constructed temporary object when its lifetime ends
|
| 1806 |
-
[[conv.rval]], [[class.temporary]]
|
| 1807 |
|
| 1808 |
In each case, the context of the invocation is the context of the
|
| 1809 |
construction of the object. A destructor may also be invoked implicitly
|
| 1810 |
through use of a *delete-expression* [[expr.delete]] for a constructed
|
| 1811 |
object allocated by a *new-expression* [[expr.new]]; the context of the
|
| 1812 |
invocation is the *delete-expression*.
|
| 1813 |
|
| 1814 |
-
[*Note
|
| 1815 |
of which the destructor is invoked. — *end note*]
|
| 1816 |
|
| 1817 |
A destructor can also be invoked explicitly. A destructor is
|
| 1818 |
*potentially invoked* if it is invoked or as specified in [[expr.new]],
|
| 1819 |
[[stmt.return]], [[dcl.init.aggr]], [[class.base.init]], and
|
| 1820 |
[[except.throw]]. A program is ill-formed if a destructor that is
|
| 1821 |
potentially invoked is deleted or not accessible from the context of the
|
| 1822 |
invocation.
|
| 1823 |
|
| 1824 |
At the point of definition of a virtual destructor (including an
|
| 1825 |
-
implicit definition
|
| 1826 |
-
|
| 1827 |
-
|
| 1828 |
-
|
| 1829 |
-
|
| 1830 |
|
| 1831 |
-
[*Note
|
| 1832 |
the dynamic type of an object is available for the *delete-expression*
|
| 1833 |
[[class.free]]. — *end note*]
|
| 1834 |
|
| 1835 |
In an explicit destructor call, the destructor is specified by a `~`
|
| 1836 |
followed by a *type-name* or *decltype-specifier* that denotes the
|
|
@@ -1838,11 +1725,11 @@ destructor’s class type. The invocation of a destructor is subject to
|
|
| 1838 |
the usual rules for member functions [[class.mfct]]; that is, if the
|
| 1839 |
object is not of the destructor’s class type and not of a class derived
|
| 1840 |
from the destructor’s class type (including when the destructor is
|
| 1841 |
invoked via a null pointer value), the program has undefined behavior.
|
| 1842 |
|
| 1843 |
-
[*Note
|
| 1844 |
destructor; see [[expr.delete]]. — *end note*]
|
| 1845 |
|
| 1846 |
[*Example 1*:
|
| 1847 |
|
| 1848 |
``` cpp
|
|
@@ -1866,17 +1753,17 @@ void f() {
|
|
| 1866 |
}
|
| 1867 |
```
|
| 1868 |
|
| 1869 |
— *end example*]
|
| 1870 |
|
| 1871 |
-
[*Note
|
| 1872 |
member access operator [[expr.ref]] or a *qualified-id*
|
| 1873 |
[[expr.prim.id.qual]]; in particular, the *unary-expression* `~X()` in a
|
| 1874 |
member function is not an explicit destructor call
|
| 1875 |
[[expr.unary.op]]. — *end note*]
|
| 1876 |
|
| 1877 |
-
[*Note
|
| 1878 |
|
| 1879 |
Explicit calls of destructors are rarely needed. One use of such calls
|
| 1880 |
is for objects placed at specific addresses using a placement
|
| 1881 |
*new-expression*. Such use of explicit placement and destruction of
|
| 1882 |
objects can be necessary to cope with dedicated hardware resources and
|
|
@@ -1898,20 +1785,20 @@ void g() { // rare, specialized use:
|
|
| 1898 |
}
|
| 1899 |
```
|
| 1900 |
|
| 1901 |
— *end note*]
|
| 1902 |
|
| 1903 |
-
Once a destructor is invoked for an object, the object
|
| 1904 |
the behavior is undefined if the destructor is invoked for an object
|
| 1905 |
whose lifetime has ended [[basic.life]].
|
| 1906 |
|
| 1907 |
[*Example 2*: If the destructor for an object with automatic storage
|
| 1908 |
duration is explicitly invoked, and the block is subsequently left in a
|
| 1909 |
manner that would ordinarily invoke implicit destruction of the object,
|
| 1910 |
the behavior is undefined. — *end example*]
|
| 1911 |
|
| 1912 |
-
[*Note
|
| 1913 |
|
| 1914 |
The notation for explicit call of a destructor can be used for any
|
| 1915 |
scalar type name [[expr.prim.id.dtor]]. Allowing this makes it possible
|
| 1916 |
to write code without having to know if a destructor exists for a given
|
| 1917 |
type. For example:
|
|
@@ -1926,19 +1813,21 @@ p->I::~I();
|
|
| 1926 |
|
| 1927 |
A destructor shall not be a coroutine.
|
| 1928 |
|
| 1929 |
### Conversions <a id="class.conv">[[class.conv]]</a>
|
| 1930 |
|
|
|
|
|
|
|
| 1931 |
Type conversions of class objects can be specified by constructors and
|
| 1932 |
by conversion functions. These conversions are called *user-defined
|
| 1933 |
conversions* and are used for implicit type conversions [[conv]], for
|
| 1934 |
-
initialization [[dcl.init]], and for explicit type conversions
|
| 1935 |
-
[[expr.type.conv]], [[expr.cast]], [[expr.static.cast]]
|
| 1936 |
|
| 1937 |
-
User-defined conversions are applied only where they are unambiguous
|
| 1938 |
-
[[class.member.lookup]], [[class.conv.fct]]
|
| 1939 |
-
|
| 1940 |
ambiguity resolution [[basic.lookup]].
|
| 1941 |
|
| 1942 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 1943 |
conversions in function calls as well as examples below. — *end note*]
|
| 1944 |
|
|
@@ -1956,36 +1845,11 @@ struct Y {
|
|
| 1956 |
operator X();
|
| 1957 |
};
|
| 1958 |
|
| 1959 |
Y a;
|
| 1960 |
int b = a; // error: no viable conversion (a.operator X().operator int() not considered)
|
| 1961 |
-
int c = X(a); // OK
|
| 1962 |
-
```
|
| 1963 |
-
|
| 1964 |
-
— *end example*]
|
| 1965 |
-
|
| 1966 |
-
User-defined conversions are used implicitly only if they are
|
| 1967 |
-
unambiguous. A conversion function in a derived class does not hide a
|
| 1968 |
-
conversion function in a base class unless the two functions convert to
|
| 1969 |
-
the same type. Function overload resolution [[over.match.best]] selects
|
| 1970 |
-
the best conversion function to perform the conversion.
|
| 1971 |
-
|
| 1972 |
-
[*Example 2*:
|
| 1973 |
-
|
| 1974 |
-
``` cpp
|
| 1975 |
-
struct X {
|
| 1976 |
-
operator int();
|
| 1977 |
-
};
|
| 1978 |
-
|
| 1979 |
-
struct Y : X {
|
| 1980 |
-
operator char();
|
| 1981 |
-
};
|
| 1982 |
-
|
| 1983 |
-
void f(Y& a) {
|
| 1984 |
-
if (a) { // error: ambiguous between X::operator int() and Y::operator char()
|
| 1985 |
-
}
|
| 1986 |
-
}
|
| 1987 |
```
|
| 1988 |
|
| 1989 |
— *end example*]
|
| 1990 |
|
| 1991 |
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
|
@@ -2016,13 +1880,13 @@ void f(X arg) {
|
|
| 2016 |
|
| 2017 |
[*Note 1*:
|
| 2018 |
|
| 2019 |
An explicit constructor constructs objects just like non-explicit
|
| 2020 |
constructors, but does so only where the direct-initialization syntax
|
| 2021 |
-
[[dcl.init]] or where casts
|
| 2022 |
explicitly used; see also [[over.match.copy]]. A default constructor
|
| 2023 |
-
|
| 2024 |
perform default-initialization or value-initialization [[dcl.init]].
|
| 2025 |
|
| 2026 |
[*Example 2*:
|
| 2027 |
|
| 2028 |
``` cpp
|
|
@@ -2030,19 +1894,19 @@ struct Z {
|
|
| 2030 |
explicit Z();
|
| 2031 |
explicit Z(int);
|
| 2032 |
explicit Z(int, int);
|
| 2033 |
};
|
| 2034 |
|
| 2035 |
-
Z a; // OK
|
| 2036 |
-
Z b{}; // OK
|
| 2037 |
Z c = {}; // error: copy-list-initialization
|
| 2038 |
Z a1 = 1; // error: no implicit conversion
|
| 2039 |
-
Z a3 = Z(1); // OK
|
| 2040 |
-
Z a2(1); // OK
|
| 2041 |
-
Z* p = new Z(1); // OK
|
| 2042 |
-
Z a4 = (Z)1; // OK
|
| 2043 |
-
Z a5 = static_cast<Z>(1); // OK
|
| 2044 |
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 2045 |
```
|
| 2046 |
|
| 2047 |
— *end example*]
|
| 2048 |
|
|
@@ -2050,18 +1914,15 @@ Z a6 = { 3, 4 }; // error: no implicit conversion
|
|
| 2050 |
|
| 2051 |
A non-explicit copy/move constructor [[class.copy.ctor]] is a converting
|
| 2052 |
constructor.
|
| 2053 |
|
| 2054 |
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 2055 |
-
explicit constructor; it
|
| 2056 |
conversions. — *end note*]
|
| 2057 |
|
| 2058 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 2059 |
|
| 2060 |
-
A member function of a class `X` having no parameters with a name of the
|
| 2061 |
-
form
|
| 2062 |
-
|
| 2063 |
``` bnf
|
| 2064 |
conversion-function-id:
|
| 2065 |
operator conversion-type-id
|
| 2066 |
```
|
| 2067 |
|
|
@@ -2073,20 +1934,44 @@ conversion-type-id:
|
|
| 2073 |
``` bnf
|
| 2074 |
conversion-declarator:
|
| 2075 |
ptr-operator conversion-declaratorₒₚₜ
|
| 2076 |
```
|
| 2077 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2078 |
specifies a conversion from `X` to the type specified by the
|
| 2079 |
-
*conversion-type-id*
|
| 2080 |
-
|
| 2081 |
-
(if any) shall be
|
| 2082 |
-
|
| 2083 |
-
|
| 2084 |
-
|
| 2085 |
-
|
| 2086 |
-
|
| 2087 |
-
cv
|
|
|
|
|
|
|
|
|
|
| 2088 |
|
| 2089 |
[*Example 1*:
|
| 2090 |
|
| 2091 |
``` cpp
|
| 2092 |
struct X {
|
|
@@ -2118,13 +2003,13 @@ class Y { };
|
|
| 2118 |
struct Z {
|
| 2119 |
explicit operator Y() const;
|
| 2120 |
};
|
| 2121 |
|
| 2122 |
void h(Z z) {
|
| 2123 |
-
Y y1(z); // OK
|
| 2124 |
Y y2 = z; // error: no conversion function candidate for copy-initialization
|
| 2125 |
-
Y y3 = (Y)z; // OK
|
| 2126 |
}
|
| 2127 |
|
| 2128 |
void g(X a, X b) {
|
| 2129 |
int i = (a) ? 1+a : 0;
|
| 2130 |
int j = (a&&b) ? a+b : i;
|
|
@@ -2167,18 +2052,47 @@ operator int [[noreturn]] (); // error: noreturn attribute applied to a type
|
|
| 2167 |
|
| 2168 |
— *end example*]
|
| 2169 |
|
| 2170 |
— *end note*]
|
| 2171 |
|
| 2172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2173 |
|
| 2174 |
Conversion functions can be virtual.
|
| 2175 |
|
| 2176 |
A conversion function template shall not have a deduced return type
|
| 2177 |
[[dcl.spec.auto]].
|
| 2178 |
|
| 2179 |
-
[*Example
|
| 2180 |
|
| 2181 |
``` cpp
|
| 2182 |
struct S {
|
| 2183 |
operator auto() const { return 10; } // OK
|
| 2184 |
template<class T>
|
|
@@ -2188,10 +2102,12 @@ struct S {
|
|
| 2188 |
|
| 2189 |
— *end example*]
|
| 2190 |
|
| 2191 |
### Static members <a id="class.static">[[class.static]]</a>
|
| 2192 |
|
|
|
|
|
|
|
| 2193 |
A static member `s` of class `X` may be referred to using the
|
| 2194 |
*qualified-id* expression `X::s`; it is not necessary to use the class
|
| 2195 |
member access syntax [[expr.ref]] to refer to a static member. A static
|
| 2196 |
member may be referred to using the class member access syntax, in which
|
| 2197 |
case the object expression is evaluated.
|
|
@@ -2203,39 +2119,17 @@ struct process {
|
|
| 2203 |
static void reschedule();
|
| 2204 |
};
|
| 2205 |
process& g();
|
| 2206 |
|
| 2207 |
void f() {
|
| 2208 |
-
process::reschedule(); // OK
|
| 2209 |
g().reschedule(); // g() is called
|
| 2210 |
}
|
| 2211 |
```
|
| 2212 |
|
| 2213 |
— *end example*]
|
| 2214 |
|
| 2215 |
-
A static member may be referred to directly in the scope of its class or
|
| 2216 |
-
in the scope of a class derived [[class.derived]] from its class; in
|
| 2217 |
-
this case, the static member is referred to as if a *qualified-id*
|
| 2218 |
-
expression was used, with the *nested-name-specifier* of the
|
| 2219 |
-
*qualified-id* naming the class scope from which the static member is
|
| 2220 |
-
referenced.
|
| 2221 |
-
|
| 2222 |
-
[*Example 2*:
|
| 2223 |
-
|
| 2224 |
-
``` cpp
|
| 2225 |
-
int g();
|
| 2226 |
-
struct X {
|
| 2227 |
-
static int g();
|
| 2228 |
-
};
|
| 2229 |
-
struct Y : X {
|
| 2230 |
-
static int i;
|
| 2231 |
-
};
|
| 2232 |
-
int Y::i = g(); // equivalent to Y::g();
|
| 2233 |
-
```
|
| 2234 |
-
|
| 2235 |
-
— *end example*]
|
| 2236 |
-
|
| 2237 |
Static members obey the usual class member access rules
|
| 2238 |
[[class.access]]. When used in the declaration of a class member, the
|
| 2239 |
`static` specifier shall only be used in the member declarations that
|
| 2240 |
appear within the *member-specification* of the class definition.
|
| 2241 |
|
|
@@ -2246,16 +2140,12 @@ namespace scope. — *end note*]
|
|
| 2246 |
|
| 2247 |
[*Note 1*: The rules described in [[class.mfct]] apply to static
|
| 2248 |
member functions. — *end note*]
|
| 2249 |
|
| 2250 |
[*Note 2*: A static member function does not have a `this` pointer
|
| 2251 |
-
[[
|
| 2252 |
-
|
| 2253 |
-
A static member function shall not be `virtual`. There shall not be a
|
| 2254 |
-
static and a non-static member function with the same name and the same
|
| 2255 |
-
parameter types [[over.load]]. A static member function shall not be
|
| 2256 |
-
declared `const`, `volatile`, or `const volatile`.
|
| 2257 |
|
| 2258 |
#### Static data members <a id="class.static.data">[[class.static.data]]</a>
|
| 2259 |
|
| 2260 |
A static data member is not part of the subobjects of a class. If a
|
| 2261 |
static data member is declared `thread_local` there is one copy of the
|
|
@@ -2268,17 +2158,14 @@ member shall not be a direct member [[class.mem]] of an unnamed
|
|
| 2268 |
[[class.pre]] or local [[class.local]] class or of a (possibly
|
| 2269 |
indirectly) nested class [[class.nest]] thereof.
|
| 2270 |
|
| 2271 |
The declaration of a non-inline static data member in its class
|
| 2272 |
definition is not a definition and may be of an incomplete type other
|
| 2273 |
-
than cv `void`.
|
| 2274 |
-
|
| 2275 |
-
|
| 2276 |
-
|
| 2277 |
-
class name using the `::` operator. The *initializer* expression in the
|
| 2278 |
-
definition of a static data member is in the scope of its class
|
| 2279 |
-
[[basic.scope.class]].
|
| 2280 |
|
| 2281 |
[*Example 1*:
|
| 2282 |
|
| 2283 |
``` cpp
|
| 2284 |
class process {
|
|
@@ -2288,20 +2175,20 @@ class process {
|
|
| 2288 |
|
| 2289 |
process* process::running = get_main();
|
| 2290 |
process* process::run_chain = running;
|
| 2291 |
```
|
| 2292 |
|
| 2293 |
-
The static data member `run_chain` of class `process`
|
| 2294 |
-
global scope; the notation `process::run_chain`
|
| 2295 |
-
member `run_chain` is a member of class `process` and in the
|
| 2296 |
-
class `process`. In the static data member definition, the
|
| 2297 |
-
expression refers to the static data member `running` of
|
| 2298 |
-
`process`.
|
| 2299 |
|
| 2300 |
— *end example*]
|
| 2301 |
|
| 2302 |
-
[*Note
|
| 2303 |
|
| 2304 |
Once the static data member has been defined, it exists even if no
|
| 2305 |
objects of its class have been created.
|
| 2306 |
|
| 2307 |
[*Example 2*:
|
|
@@ -2309,56 +2196,57 @@ objects of its class have been created.
|
|
| 2309 |
In the example above, `run_chain` and `running` exist even if no objects
|
| 2310 |
of class `process` are created by the program.
|
| 2311 |
|
| 2312 |
— *end example*]
|
| 2313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2314 |
— *end note*]
|
| 2315 |
|
| 2316 |
If a non-volatile non-inline `const` static data member is of integral
|
| 2317 |
or enumeration type, its declaration in the class definition can specify
|
| 2318 |
a *brace-or-equal-initializer* in which every *initializer-clause* that
|
| 2319 |
is an *assignment-expression* is a constant expression [[expr.const]].
|
| 2320 |
The member shall still be defined in a namespace scope if it is odr-used
|
| 2321 |
-
[[
|
| 2322 |
-
|
| 2323 |
-
|
| 2324 |
*brace-or-equal-initializer*. If the member is declared with the
|
| 2325 |
`constexpr` specifier, it may be redeclared in namespace scope with no
|
| 2326 |
initializer (this usage is deprecated; see [[depr.static.constexpr]]).
|
| 2327 |
Declarations of other static data members shall not specify a
|
| 2328 |
*brace-or-equal-initializer*.
|
| 2329 |
|
| 2330 |
-
[*Note
|
| 2331 |
-
is odr-used [[
|
| 2332 |
|
| 2333 |
-
[*Note
|
| 2334 |
linkage of the name of the class [[basic.link]]. — *end note*]
|
| 2335 |
|
| 2336 |
-
Static data members are initialized and destroyed exactly like non-local
|
| 2337 |
-
variables ([[basic.start.static]], [[basic.start.dynamic]],
|
| 2338 |
-
[[basic.start.term]]).
|
| 2339 |
-
|
| 2340 |
### Bit-fields <a id="class.bit">[[class.bit]]</a>
|
| 2341 |
|
| 2342 |
A *member-declarator* of the form
|
| 2343 |
|
| 2344 |
``` bnf
|
| 2345 |
identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
|
| 2346 |
```
|
| 2347 |
|
| 2348 |
specifies a bit-field. The optional *attribute-specifier-seq* appertains
|
| 2349 |
to the entity being declared. A bit-field shall not be a static member.
|
| 2350 |
-
A bit-field shall have integral or
|
| 2351 |
-
semantic property is not part of the type of the
|
| 2352 |
-
*constant-expression* shall be an integral constant
|
| 2353 |
-
value greater than or equal to zero and is called the
|
| 2354 |
-
bit-field. If the width of a bit-field is larger than the
|
| 2355 |
-
bit-field’s type (or, in case of an enumeration type, of
|
| 2356 |
-
type), the extra bits are padding bits
|
| 2357 |
-
bit-fields within a class object is
|
| 2358 |
-
|
| 2359 |
-
|
|
|
|
| 2360 |
|
| 2361 |
[*Note 1*: Bit-fields straddle allocation units on some machines and
|
| 2362 |
not on others. Bit-fields are assigned right-to-left on some machines,
|
| 2363 |
left-to-right on others. — *end note*]
|
| 2364 |
|
|
@@ -2373,12 +2261,12 @@ externally-imposed layouts. — *end note*]
|
|
| 2373 |
As a special case, an unnamed bit-field with a width of zero specifies
|
| 2374 |
alignment of the next bit-field at an allocation unit boundary. Only
|
| 2375 |
when declaring an unnamed bit-field may the width be zero.
|
| 2376 |
|
| 2377 |
The address-of operator `&` shall not be applied to a bit-field, so
|
| 2378 |
-
there are no pointers to bit-fields. A non-const reference shall not
|
| 2379 |
-
|
| 2380 |
|
| 2381 |
[*Note 3*: If the initializer for a reference of type `const` `T&` is
|
| 2382 |
an lvalue that refers to a bit-field, the reference is bound to a
|
| 2383 |
temporary initialized to hold the value of the bit-field; the reference
|
| 2384 |
is not bound to the bit-field directly. See
|
|
@@ -2411,16 +2299,139 @@ void f() {
|
|
| 2411 |
}
|
| 2412 |
```
|
| 2413 |
|
| 2414 |
— *end example*]
|
| 2415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2416 |
### Nested class declarations <a id="class.nest">[[class.nest]]</a>
|
| 2417 |
|
| 2418 |
A class can be declared within another class. A class declared within
|
| 2419 |
-
another is called a *nested class*.
|
| 2420 |
-
to its enclosing class. The nested class is in the scope of its
|
| 2421 |
-
enclosing class.
|
| 2422 |
|
| 2423 |
[*Note 1*: See [[expr.prim.id]] for restrictions on the use of
|
| 2424 |
non-static data members and non-static member functions. — *end note*]
|
| 2425 |
|
| 2426 |
[*Example 1*:
|
|
@@ -2433,29 +2444,33 @@ struct enclose {
|
|
| 2433 |
int x;
|
| 2434 |
static int s;
|
| 2435 |
|
| 2436 |
struct inner {
|
| 2437 |
void f(int i) {
|
| 2438 |
-
int a = sizeof(x); // OK
|
| 2439 |
x = i; // error: assign to enclose::x
|
| 2440 |
-
s = i; // OK
|
| 2441 |
-
::x = i; // OK
|
| 2442 |
-
y = i; // OK
|
| 2443 |
}
|
| 2444 |
void g(enclose* p, int i) {
|
| 2445 |
-
p->x = i; // OK
|
| 2446 |
}
|
| 2447 |
};
|
| 2448 |
};
|
| 2449 |
|
| 2450 |
-
inner* p = 0; // error: inner not
|
| 2451 |
```
|
| 2452 |
|
| 2453 |
— *end example*]
|
| 2454 |
|
| 2455 |
-
|
| 2456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2457 |
|
| 2458 |
[*Example 2*:
|
| 2459 |
|
| 2460 |
``` cpp
|
| 2461 |
struct enclose {
|
|
@@ -2466,63 +2481,30 @@ struct enclose {
|
|
| 2466 |
};
|
| 2467 |
|
| 2468 |
int enclose::inner::x = 1;
|
| 2469 |
|
| 2470 |
void enclose::inner::f(int i) { ... }
|
| 2471 |
-
```
|
| 2472 |
|
| 2473 |
-
— *end example*]
|
| 2474 |
-
|
| 2475 |
-
If class `X` is defined in a namespace scope, a nested class `Y` may be
|
| 2476 |
-
declared in class `X` and later defined in the definition of class `X`
|
| 2477 |
-
or be later defined in a namespace scope enclosing the definition of
|
| 2478 |
-
class `X`.
|
| 2479 |
-
|
| 2480 |
-
[*Example 3*:
|
| 2481 |
-
|
| 2482 |
-
``` cpp
|
| 2483 |
class E {
|
| 2484 |
class I1; // forward declaration of nested class
|
| 2485 |
class I2;
|
| 2486 |
class I1 { }; // definition of nested class
|
| 2487 |
};
|
| 2488 |
class E::I2 { }; // definition of nested class
|
| 2489 |
```
|
| 2490 |
|
| 2491 |
— *end example*]
|
| 2492 |
|
| 2493 |
-
|
| 2494 |
-
within a nested class is in the lexical scope of that class; it obeys
|
| 2495 |
-
the same rules for name binding as a static member function of that
|
| 2496 |
-
class [[class.static]], but it has no special access rights to members
|
| 2497 |
-
of an enclosing class.
|
| 2498 |
|
| 2499 |
-
|
| 2500 |
-
|
| 2501 |
-
Type names obey exactly the same scope rules as other names. In
|
| 2502 |
-
particular, type names defined within a class definition cannot be used
|
| 2503 |
-
outside their class without qualification.
|
| 2504 |
-
|
| 2505 |
-
[*Example 1*:
|
| 2506 |
-
|
| 2507 |
-
``` cpp
|
| 2508 |
-
struct X {
|
| 2509 |
-
typedef int I;
|
| 2510 |
-
class Y { ... };
|
| 2511 |
-
I a;
|
| 2512 |
-
};
|
| 2513 |
-
|
| 2514 |
-
I b; // error
|
| 2515 |
-
Y c; // error
|
| 2516 |
-
X::Y d; // OK
|
| 2517 |
-
X::I e; // OK
|
| 2518 |
-
```
|
| 2519 |
-
|
| 2520 |
-
— *end example*]
|
| 2521 |
|
| 2522 |
## Unions <a id="class.union">[[class.union]]</a>
|
| 2523 |
|
|
|
|
|
|
|
| 2524 |
A *union* is a class defined with the *class-key* `union`.
|
| 2525 |
|
| 2526 |
In a union, a non-static data member is *active* if its name refers to
|
| 2527 |
an object whose lifetime has begun and has not ended [[basic.life]]. At
|
| 2528 |
most one of the non-static data members of an object of union type can
|
|
@@ -2540,27 +2522,29 @@ members; see [[class.mem]]. — *end note*]
|
|
| 2540 |
The size of a union is sufficient to contain the largest of its
|
| 2541 |
non-static data members. Each non-static data member is allocated as if
|
| 2542 |
it were the sole member of a non-union class.
|
| 2543 |
|
| 2544 |
[*Note 2*: A union object and its non-static data members are
|
| 2545 |
-
pointer-interconvertible
|
| 2546 |
-
|
| 2547 |
-
|
| 2548 |
|
| 2549 |
A union can have member functions (including constructors and
|
| 2550 |
destructors), but it shall not have virtual [[class.virtual]] functions.
|
| 2551 |
A union shall not have base classes. A union shall not be used as a base
|
| 2552 |
class. If a union contains a non-static data member of reference type
|
| 2553 |
the program is ill-formed.
|
| 2554 |
|
| 2555 |
-
[*Note 3*:
|
| 2556 |
-
|
|
|
|
|
|
|
| 2557 |
[[class.default.ctor]], copy constructor, move constructor
|
| 2558 |
[[class.copy.ctor]], copy assignment operator, move assignment operator
|
| 2559 |
[[class.copy.assign]], or destructor [[class.dtor]], the corresponding
|
| 2560 |
member function of the union must be user-provided or it will be
|
| 2561 |
-
implicitly deleted [[dcl.fct.def.delete]] for the union.
|
| 2562 |
|
| 2563 |
[*Example 1*:
|
| 2564 |
|
| 2565 |
Consider the following union:
|
| 2566 |
|
|
@@ -2578,10 +2562,12 @@ default constructor, copy/move constructor, copy/move assignment
|
|
| 2578 |
operator, and destructor. To use `U`, some or all of these member
|
| 2579 |
functions must be user-provided.
|
| 2580 |
|
| 2581 |
— *end example*]
|
| 2582 |
|
|
|
|
|
|
|
| 2583 |
When the left operand of an assignment operator involves a member access
|
| 2584 |
expression [[expr.ref]] that nominates a union member, it may begin the
|
| 2585 |
lifetime of that union member, as described below. For an expression
|
| 2586 |
`E`, define the set S(E) of subexpressions of `E` as follows:
|
| 2587 |
|
|
@@ -2612,32 +2598,32 @@ the union, if any [[basic.life]]. — *end note*]
|
|
| 2612 |
union A { int x; int y[4]; };
|
| 2613 |
struct B { A a; };
|
| 2614 |
union C { B b; int k; };
|
| 2615 |
int f() {
|
| 2616 |
C c; // does not start lifetime of any union member
|
| 2617 |
-
c.b.a.y[3] = 4; // OK
|
| 2618 |
// creates objects to hold union members c.b and c.b.a.y
|
| 2619 |
-
return c.b.a.y[3]; // OK
|
| 2620 |
}
|
| 2621 |
|
| 2622 |
struct X { const int a; int b; };
|
| 2623 |
union Y { X x; int k; };
|
| 2624 |
void g() {
|
| 2625 |
Y y = { { 1, 2 } }; // OK, y.x is active union member[class.mem]
|
| 2626 |
int n = y.x.a;
|
| 2627 |
-
y.k = 4; // OK
|
| 2628 |
y.x.b = n; // undefined behavior: y.x.b modified outside its lifetime,
|
| 2629 |
// S(y.x.b) is empty because X's default constructor is deleted,
|
| 2630 |
// so union member y.x's lifetime does not implicitly start
|
| 2631 |
}
|
| 2632 |
```
|
| 2633 |
|
| 2634 |
— *end example*]
|
| 2635 |
|
| 2636 |
-
[*Note 5*: In
|
| 2637 |
-
|
| 2638 |
-
|
| 2639 |
|
| 2640 |
[*Example 3*:
|
| 2641 |
|
| 2642 |
Consider an object `u` of a `union` type `U` having non-static data
|
| 2643 |
members `m` of type `M` and `n` of type `N`. If `M` has a non-trivial
|
|
@@ -2660,20 +2646,18 @@ A union of the form
|
|
| 2660 |
``` bnf
|
| 2661 |
union '{' member-specification '}' ';'
|
| 2662 |
```
|
| 2663 |
|
| 2664 |
is called an *anonymous union*; it defines an unnamed type and an
|
| 2665 |
-
unnamed object of that type called an *anonymous union
|
| 2666 |
-
|
| 2667 |
-
|
| 2668 |
-
|
|
|
|
| 2669 |
functions shall not be declared within an anonymous union. The names of
|
| 2670 |
-
the members of an anonymous union
|
| 2671 |
-
|
| 2672 |
-
For the purpose of name lookup, after the anonymous union definition,
|
| 2673 |
-
the members of the anonymous union are considered to have been defined
|
| 2674 |
-
in the scope in which the anonymous union is declared.
|
| 2675 |
|
| 2676 |
[*Example 1*:
|
| 2677 |
|
| 2678 |
``` cpp
|
| 2679 |
void f() {
|
|
@@ -2686,17 +2670,17 @@ void f() {
|
|
| 2686 |
Here `a` and `p` are used like ordinary (non-member) variables, but
|
| 2687 |
since they are union members they have the same address.
|
| 2688 |
|
| 2689 |
— *end example*]
|
| 2690 |
|
| 2691 |
-
Anonymous unions declared in
|
| 2692 |
-
|
| 2693 |
-
scope shall be declared with any storage class allowed for a block
|
| 2694 |
variable, or with no storage class. A storage class is not allowed in a
|
| 2695 |
-
declaration of an anonymous union in a class scope.
|
| 2696 |
-
|
| 2697 |
-
|
| 2698 |
|
| 2699 |
A union for which objects, pointers, or references are declared is not
|
| 2700 |
an anonymous union.
|
| 2701 |
|
| 2702 |
[*Example 2*:
|
|
@@ -2713,11 +2697,13 @@ The assignment to plain `aa` is ill-formed since the member name is not
|
|
| 2713 |
visible outside the union, and even if it were visible, it is not
|
| 2714 |
associated with any particular object.
|
| 2715 |
|
| 2716 |
— *end example*]
|
| 2717 |
|
| 2718 |
-
|
|
|
|
|
|
|
| 2719 |
is described in [[dcl.init.aggr]]. — *end note*]
|
| 2720 |
|
| 2721 |
A *union-like class* is a union or a class that has an anonymous union
|
| 2722 |
as a direct member. A union-like class `X` has a set of *variant
|
| 2723 |
members*. If `X` is a union, a non-static data member of `X` that is not
|
|
@@ -2744,17 +2730,14 @@ union U {
|
|
| 2744 |
— *end example*]
|
| 2745 |
|
| 2746 |
## Local class declarations <a id="class.local">[[class.local]]</a>
|
| 2747 |
|
| 2748 |
A class can be declared within a function definition; such a class is
|
| 2749 |
-
called a *local class*.
|
| 2750 |
-
enclosing scope. The local class is in the scope of the enclosing scope,
|
| 2751 |
-
and has the same access to names outside the function as does the
|
| 2752 |
-
enclosing function.
|
| 2753 |
|
| 2754 |
[*Note 1*: A declaration in a local class cannot odr-use
|
| 2755 |
-
[[
|
| 2756 |
|
| 2757 |
[*Example 1*:
|
| 2758 |
|
| 2759 |
``` cpp
|
| 2760 |
int x;
|
|
@@ -2769,17 +2752,17 @@ void f() {
|
|
| 2769 |
struct local {
|
| 2770 |
int g() { return x; } // error: odr-use of non-odr-usable variable x
|
| 2771 |
int h() { return s; } // OK
|
| 2772 |
int k() { return ::x; } // OK
|
| 2773 |
int l() { return q(); } // OK
|
| 2774 |
-
int m() { return N; } // OK
|
| 2775 |
int* n() { return &N; } // error: odr-use of non-odr-usable variable N
|
| 2776 |
int p() { return y; } // error: odr-use of non-odr-usable structured binding y
|
| 2777 |
};
|
| 2778 |
}
|
| 2779 |
|
| 2780 |
-
local* p = 0; // error: local not
|
| 2781 |
```
|
| 2782 |
|
| 2783 |
— *end example*]
|
| 2784 |
|
| 2785 |
An enclosing function has no special access to members of the local
|
|
@@ -2795,10 +2778,12 @@ within a local class is a local class.
|
|
| 2795 |
[*Note 2*: A local class cannot have static data members
|
| 2796 |
[[class.static.data]]. — *end note*]
|
| 2797 |
|
| 2798 |
## Derived classes <a id="class.derived">[[class.derived]]</a>
|
| 2799 |
|
|
|
|
|
|
|
| 2800 |
A list of base classes can be specified in a class definition using the
|
| 2801 |
notation:
|
| 2802 |
|
| 2803 |
``` bnf
|
| 2804 |
base-clause:
|
|
@@ -2833,42 +2818,41 @@ access-specifier:
|
|
| 2833 |
```
|
| 2834 |
|
| 2835 |
The optional *attribute-specifier-seq* appertains to the
|
| 2836 |
*base-specifier*.
|
| 2837 |
|
| 2838 |
-
|
|
|
|
|
|
|
| 2839 |
that is not an incompletely defined class [[class.mem]]; any
|
| 2840 |
cv-qualifiers are ignored. The class denoted by the *class-or-decltype*
|
| 2841 |
of a *base-specifier* is called a *direct base class* for the class
|
| 2842 |
-
being defined.
|
| 2843 |
-
|
| 2844 |
-
class `D` if it is a direct base class of `D` or a
|
| 2845 |
-
one of `D`’s base classes. A class is an
|
| 2846 |
-
another if it is a base class but not a direct
|
| 2847 |
-
said to be (directly or indirectly) *derived*
|
| 2848 |
-
indirect) base classes.
|
| 2849 |
|
| 2850 |
[*Note 1*: See [[class.access]] for the meaning of
|
| 2851 |
*access-specifier*. — *end note*]
|
| 2852 |
|
| 2853 |
-
|
| 2854 |
-
considered to be members of the derived class. Members of a base class
|
| 2855 |
-
other than constructors are said to be *inherited* by the derived class.
|
| 2856 |
-
Constructors of a base class can also be inherited as described in
|
| 2857 |
-
[[namespace.udecl]]. Inherited members can be referred to in expressions
|
| 2858 |
-
in the same manner as other members of the derived class, unless their
|
| 2859 |
-
names are hidden or ambiguous [[class.member.lookup]].
|
| 2860 |
|
| 2861 |
-
[*Note 2*:
|
| 2862 |
-
|
| 2863 |
-
|
| 2864 |
-
|
| 2865 |
-
|
| 2866 |
-
|
| 2867 |
-
|
| 2868 |
-
|
| 2869 |
-
|
|
|
|
|
|
|
|
|
|
| 2870 |
|
| 2871 |
The *base-specifier-list* specifies the type of the *base class
|
| 2872 |
subobjects* contained in an object of the derived class type.
|
| 2873 |
|
| 2874 |
[*Example 1*:
|
|
@@ -2904,29 +2888,26 @@ derived object [[intro.object]] is unspecified.
|
|
| 2904 |
|
| 2905 |
[*Note 3*: A derived class and its base class subobjects can be
|
| 2906 |
represented by a directed acyclic graph (DAG) where an arrow means
|
| 2907 |
“directly derived from” (see Figure [[fig:class.dag]]). An arrow
|
| 2908 |
need not have a physical representation in memory. A DAG of subobjects
|
| 2909 |
-
is often referred to as a “subobject lattice”.
|
| 2910 |
|
| 2911 |
<a id="fig:class.dag"></a>
|
| 2912 |
|
| 2913 |
![Directed acyclic graph \[fig:class.dag\]](images/figdag.svg)
|
| 2914 |
|
| 2915 |
-
— *end note*]
|
| 2916 |
-
|
| 2917 |
[*Note 4*: Initialization of objects representing base classes can be
|
| 2918 |
specified in constructors; see [[class.base.init]]. — *end note*]
|
| 2919 |
|
| 2920 |
-
[*Note 5*: A base class subobject
|
| 2921 |
-
|
| 2922 |
-
|
| 2923 |
-
|
| 2924 |
-
|
| 2925 |
-
|
| 2926 |
-
|
| 2927 |
-
[[expr.eq]]. — *end note*]
|
| 2928 |
|
| 2929 |
### Multiple base classes <a id="class.mi">[[class.mi]]</a>
|
| 2930 |
|
| 2931 |
A class can be derived from any number of base classes.
|
| 2932 |
|
|
@@ -2944,22 +2925,22 @@ class D : public A, public B, public C { ... };
|
|
| 2944 |
|
| 2945 |
— *end example*]
|
| 2946 |
|
| 2947 |
[*Note 2*: The order of derivation is not significant except as
|
| 2948 |
specified by the semantics of initialization by constructor
|
| 2949 |
-
[[class.base.init]], cleanup [[class.dtor]], and storage layout
|
| 2950 |
-
[[class.mem]], [[class.access.spec]]
|
| 2951 |
|
| 2952 |
A class shall not be specified as a direct base class of a derived class
|
| 2953 |
more than once.
|
| 2954 |
|
| 2955 |
[*Note 3*: A class can be an indirect base class more than once and can
|
| 2956 |
be a direct and an indirect base class. There are limited things that
|
| 2957 |
-
can be done with such a class
|
| 2958 |
-
|
| 2959 |
-
|
| 2960 |
-
|
| 2961 |
|
| 2962 |
[*Example 2*:
|
| 2963 |
|
| 2964 |
``` cpp
|
| 2965 |
class X { ... };
|
|
@@ -2997,12 +2978,12 @@ subobjects of class `L` as shown in Figure [[fig:class.nonvirt]].
|
|
| 2997 |
<a id="fig:class.nonvirt"></a>
|
| 2998 |
|
| 2999 |
![Non-virtual base \[fig:class.nonvirt\]](images/fignonvirt.svg)
|
| 3000 |
|
| 3001 |
In such lattices, explicit qualification can be used to specify which
|
| 3002 |
-
subobject is meant. The body of function `C::f`
|
| 3003 |
-
|
| 3004 |
|
| 3005 |
``` cpp
|
| 3006 |
void C::f() { A::next = B::next; } // well-formed
|
| 3007 |
```
|
| 3008 |
|
|
@@ -3069,25 +3050,24 @@ declared with the keyword `virtual` or if it overrides a virtual member
|
|
| 3069 |
function declared in a base class (see below).[^7]
|
| 3070 |
|
| 3071 |
[*Note 1*: Virtual functions support dynamic binding and
|
| 3072 |
object-oriented programming. — *end note*]
|
| 3073 |
|
| 3074 |
-
A class
|
| 3075 |
*polymorphic class*.[^8]
|
| 3076 |
|
| 3077 |
-
If a virtual member function
|
| 3078 |
-
|
| 3079 |
-
function
|
| 3080 |
-
|
| 3081 |
-
|
| 3082 |
-
convenience we say that any virtual function overrides itself. A
|
| 3083 |
-
member function
|
| 3084 |
-
unless the most derived class [[intro.object]] of which
|
| 3085 |
-
class subobject (if any)
|
| 3086 |
-
|
| 3087 |
-
|
| 3088 |
-
ill-formed.
|
| 3089 |
|
| 3090 |
[*Example 1*:
|
| 3091 |
|
| 3092 |
``` cpp
|
| 3093 |
struct A {
|
|
@@ -3113,11 +3093,11 @@ void foo() {
|
|
| 3113 |
|
| 3114 |
``` cpp
|
| 3115 |
struct A { virtual void f(); };
|
| 3116 |
struct B : A { };
|
| 3117 |
struct C : A { void f(); };
|
| 3118 |
-
struct D : B, C { }; // OK
|
| 3119 |
// for the B and C subobjects, respectively
|
| 3120 |
```
|
| 3121 |
|
| 3122 |
— *end example*]
|
| 3123 |
|
|
@@ -3187,20 +3167,20 @@ A virtual function shall not have a trailing *requires-clause*
|
|
| 3187 |
[[dcl.decl]].
|
| 3188 |
|
| 3189 |
[*Example 5*:
|
| 3190 |
|
| 3191 |
``` cpp
|
|
|
|
| 3192 |
struct A {
|
| 3193 |
virtual void f() requires true; // error: virtual function cannot be constrained[temp.constr.decl]
|
| 3194 |
};
|
| 3195 |
```
|
| 3196 |
|
| 3197 |
— *end example*]
|
| 3198 |
|
| 3199 |
-
|
| 3200 |
-
|
| 3201 |
-
[[class.dtor]] and [[class.free]].
|
| 3202 |
|
| 3203 |
The return type of an overriding function shall be either identical to
|
| 3204 |
the return type of the overridden function or *covariant* with the
|
| 3205 |
classes of the functions. If a function `D::f` overrides a function
|
| 3206 |
`B::f`, the return types of the functions are covariant if they satisfy
|
|
@@ -3217,14 +3197,15 @@ the following criteria:
|
|
| 3217 |
as or less cv-qualification than the class type in the return type of
|
| 3218 |
`B::f`.
|
| 3219 |
|
| 3220 |
If the class type in the covariant return type of `D::f` differs from
|
| 3221 |
that of `B::f`, the class type in the return type of `D::f` shall be
|
| 3222 |
-
complete at the
|
| 3223 |
-
type `D`. When the overriding function
|
| 3224 |
-
of the overridden function, its result
|
| 3225 |
-
by the (statically chosen) overridden
|
|
|
|
| 3226 |
|
| 3227 |
[*Example 6*:
|
| 3228 |
|
| 3229 |
``` cpp
|
| 3230 |
class B { };
|
|
@@ -3245,11 +3226,11 @@ struct No_good : public Base {
|
|
| 3245 |
class A;
|
| 3246 |
struct Derived : public Base {
|
| 3247 |
void vf1(); // virtual and overrides Base::vf1()
|
| 3248 |
void vf2(int); // not virtual, hides Base::vf2()
|
| 3249 |
char vf3(); // error: invalid difference in return type only
|
| 3250 |
-
D* vf4(); // OK
|
| 3251 |
A* vf5(); // error: returns pointer to incomplete class
|
| 3252 |
void f();
|
| 3253 |
};
|
| 3254 |
|
| 3255 |
void g() {
|
|
@@ -3278,12 +3259,12 @@ object (the static type) [[expr.call]]. — *end note*]
|
|
| 3278 |
|
| 3279 |
[*Note 4*: The `virtual` specifier implies membership, so a virtual
|
| 3280 |
function cannot be a non-member [[dcl.fct.spec]] function. Nor can a
|
| 3281 |
virtual function be a static member, since a virtual function call
|
| 3282 |
relies on a specific object for determining which function to invoke. A
|
| 3283 |
-
virtual function declared in one class can be declared a friend
|
| 3284 |
-
[[class.friend]]
|
| 3285 |
|
| 3286 |
A virtual function declared in a class shall be defined, or declared
|
| 3287 |
pure [[class.abstract]] in that class, or both; no diagnostic is
|
| 3288 |
required [[basic.def.odr]].
|
| 3289 |
|
|
@@ -3392,14 +3373,13 @@ void D::f() { ... B::f(); }
|
|
| 3392 |
Here, the function call in `D::f` really does call `B::f` and not
|
| 3393 |
`D::f`.
|
| 3394 |
|
| 3395 |
— *end example*]
|
| 3396 |
|
| 3397 |
-
A
|
| 3398 |
-
a function that
|
| 3399 |
-
|
| 3400 |
-
with a deleted definition.
|
| 3401 |
|
| 3402 |
A `consteval` virtual function shall not override a virtual function
|
| 3403 |
that is not `consteval`. A `consteval` virtual function shall not be
|
| 3404 |
overridden by a virtual function that is not `consteval`.
|
| 3405 |
|
|
@@ -3421,12 +3401,12 @@ below. — *end note*]
|
|
| 3421 |
A class is an *abstract class* if it has at least one pure virtual
|
| 3422 |
function.
|
| 3423 |
|
| 3424 |
[*Note 3*: An abstract class can be used only as a base class of some
|
| 3425 |
other class; no objects of an abstract class can be created except as
|
| 3426 |
-
subobjects of a class derived from it
|
| 3427 |
-
[[class.mem]]
|
| 3428 |
|
| 3429 |
A pure virtual function need be defined only if called with, or as if
|
| 3430 |
with [[class.dtor]], the *qualified-id* syntax [[expr.prim.id.qual]].
|
| 3431 |
|
| 3432 |
[*Example 1*:
|
|
@@ -3460,17 +3440,18 @@ struct C {
|
|
| 3460 |
|
| 3461 |
[*Note 5*: An abstract class type cannot be used as a parameter or
|
| 3462 |
return type of a function being defined [[dcl.fct]] or called
|
| 3463 |
[[expr.call]], except as specified in [[dcl.type.simple]]. Further, an
|
| 3464 |
abstract class type cannot be used as the type of an explicit type
|
| 3465 |
-
conversion
|
| 3466 |
-
[[expr.
|
| 3467 |
-
|
| 3468 |
-
|
|
|
|
| 3469 |
|
| 3470 |
-
A class is abstract if it
|
| 3471 |
-
|
| 3472 |
|
| 3473 |
[*Example 3*:
|
| 3474 |
|
| 3475 |
``` cpp
|
| 3476 |
class ab_circle : public shape {
|
|
@@ -3497,276 +3478,38 @@ would make class `circle` non-abstract and a definition of
|
|
| 3497 |
`circle::draw()` must be provided.
|
| 3498 |
|
| 3499 |
— *end example*]
|
| 3500 |
|
| 3501 |
[*Note 6*: An abstract class can be derived from a class that is not
|
| 3502 |
-
abstract, and a pure virtual function
|
| 3503 |
which is not pure. — *end note*]
|
| 3504 |
|
| 3505 |
Member functions can be called from a constructor (or destructor) of an
|
| 3506 |
abstract class; the effect of making a virtual call [[class.virtual]] to
|
| 3507 |
a pure virtual function directly or indirectly for the object being
|
| 3508 |
created (or destroyed) from such a constructor (or destructor) is
|
| 3509 |
undefined.
|
| 3510 |
|
| 3511 |
-
## Member name lookup <a id="class.member.lookup">[[class.member.lookup]]</a>
|
| 3512 |
-
|
| 3513 |
-
Member name lookup determines the meaning of a name (*id-expression*) in
|
| 3514 |
-
a class scope [[basic.scope.class]]. Name lookup can result in an
|
| 3515 |
-
ambiguity, in which case the program is ill-formed. For an
|
| 3516 |
-
*unqualified-id*, name lookup begins in the class scope of `this`; for a
|
| 3517 |
-
*qualified-id*, name lookup begins in the scope of the
|
| 3518 |
-
*nested-name-specifier*. Name lookup takes place before access control (
|
| 3519 |
-
[[basic.lookup]], [[class.access]]).
|
| 3520 |
-
|
| 3521 |
-
The following steps define the result of name lookup for a member name
|
| 3522 |
-
`f` in a class scope `C`.
|
| 3523 |
-
|
| 3524 |
-
The *lookup set* for `f` in `C`, called S(f,C), consists of two
|
| 3525 |
-
component sets: the *declaration set*, a set of members named `f`; and
|
| 3526 |
-
the *subobject set*, a set of subobjects where declarations of these
|
| 3527 |
-
members (possibly including *using-declaration*s) were found. In the
|
| 3528 |
-
declaration set, *using-declaration*s are replaced by the set of
|
| 3529 |
-
designated members that are not hidden or overridden by members of the
|
| 3530 |
-
derived class [[namespace.udecl]], and type declarations (including
|
| 3531 |
-
injected-class-names) are replaced by the types they designate. S(f,C)
|
| 3532 |
-
is calculated as follows:
|
| 3533 |
-
|
| 3534 |
-
If `C` contains a declaration of the name `f`, the declaration set
|
| 3535 |
-
contains every declaration of `f` declared in `C` that satisfies the
|
| 3536 |
-
requirements of the language construct in which the lookup occurs.
|
| 3537 |
-
|
| 3538 |
-
[*Note 1*: Looking up a name in an *elaborated-type-specifier*
|
| 3539 |
-
[[basic.lookup.elab]] or *base-specifier* [[class.derived]], for
|
| 3540 |
-
instance, ignores all non-type declarations, while looking up a name in
|
| 3541 |
-
a *nested-name-specifier* [[basic.lookup.qual]] ignores function,
|
| 3542 |
-
variable, and enumerator declarations. As another example, looking up a
|
| 3543 |
-
name in a *using-declaration* [[namespace.udecl]] includes the
|
| 3544 |
-
declaration of a class or enumeration that would ordinarily be hidden by
|
| 3545 |
-
another declaration of that name in the same scope. — *end note*]
|
| 3546 |
-
|
| 3547 |
-
If the resulting declaration set is not empty, the subobject set
|
| 3548 |
-
contains `C` itself, and calculation is complete.
|
| 3549 |
-
|
| 3550 |
-
Otherwise (i.e., `C` does not contain a declaration of `f` or the
|
| 3551 |
-
resulting declaration set is empty), S(f,C) is initially empty. If `C`
|
| 3552 |
-
has base classes, calculate the lookup set for `f` in each direct base
|
| 3553 |
-
class subobject Bᵢ, and merge each such lookup set S(f,Bᵢ) in turn into
|
| 3554 |
-
S(f,C).
|
| 3555 |
-
|
| 3556 |
-
The following steps define the result of merging lookup set S(f,Bᵢ) into
|
| 3557 |
-
the intermediate S(f,C):
|
| 3558 |
-
|
| 3559 |
-
- If each of the subobject members of S(f,Bᵢ) is a base class subobject
|
| 3560 |
-
of at least one of the subobject members of S(f,C), or if S(f,Bᵢ) is
|
| 3561 |
-
empty, S(f,C) is unchanged and the merge is complete. Conversely, if
|
| 3562 |
-
each of the subobject members of S(f,C) is a base class subobject of
|
| 3563 |
-
at least one of the subobject members of S(f,Bᵢ), or if S(f,C) is
|
| 3564 |
-
empty, the new S(f,C) is a copy of S(f,Bᵢ).
|
| 3565 |
-
- Otherwise, if the declaration sets of S(f,Bᵢ) and S(f,C) differ, the
|
| 3566 |
-
merge is ambiguous: the new S(f,C) is a lookup set with an invalid
|
| 3567 |
-
declaration set and the union of the subobject sets. In subsequent
|
| 3568 |
-
merges, an invalid declaration set is considered different from any
|
| 3569 |
-
other.
|
| 3570 |
-
- Otherwise, the new S(f,C) is a lookup set with the shared set of
|
| 3571 |
-
declarations and the union of the subobject sets.
|
| 3572 |
-
|
| 3573 |
-
The result of name lookup for `f` in `C` is the declaration set of
|
| 3574 |
-
S(f,C). If it is an invalid set, the program is ill-formed.
|
| 3575 |
-
|
| 3576 |
-
[*Example 1*:
|
| 3577 |
-
|
| 3578 |
-
``` cpp
|
| 3579 |
-
struct A { int x; }; // S(x,A) = { { A::x }, { A } }
|
| 3580 |
-
struct B { float x; }; // S(x,B) = { { B::x }, { B } }
|
| 3581 |
-
struct C: public A, public B { }; // S(x,C) = { invalid, { A in C, B in C } }
|
| 3582 |
-
struct D: public virtual C { }; // S(x,D) = S(x,C)
|
| 3583 |
-
struct E: public virtual C { char x; }; // S(x,E) = { { E::x }, { E } }
|
| 3584 |
-
struct F: public D, public E { }; // S(x,F) = S(x,E)
|
| 3585 |
-
int main() {
|
| 3586 |
-
F f;
|
| 3587 |
-
f.x = 0; // OK, lookup finds E::x
|
| 3588 |
-
}
|
| 3589 |
-
```
|
| 3590 |
-
|
| 3591 |
-
S(x,F) is unambiguous because the `A` and `B` base class subobjects of
|
| 3592 |
-
`D` are also base class subobjects of `E`, so S(x,D) is discarded in the
|
| 3593 |
-
first merge step.
|
| 3594 |
-
|
| 3595 |
-
— *end example*]
|
| 3596 |
-
|
| 3597 |
-
If the name of an overloaded function is unambiguously found, overload
|
| 3598 |
-
resolution [[over.match]] also takes place before access control.
|
| 3599 |
-
Ambiguities can often be resolved by qualifying a name with its class
|
| 3600 |
-
name.
|
| 3601 |
-
|
| 3602 |
-
[*Example 2*:
|
| 3603 |
-
|
| 3604 |
-
``` cpp
|
| 3605 |
-
struct A {
|
| 3606 |
-
int f();
|
| 3607 |
-
};
|
| 3608 |
-
```
|
| 3609 |
-
|
| 3610 |
-
``` cpp
|
| 3611 |
-
struct B {
|
| 3612 |
-
int f();
|
| 3613 |
-
};
|
| 3614 |
-
```
|
| 3615 |
-
|
| 3616 |
-
``` cpp
|
| 3617 |
-
struct C : A, B {
|
| 3618 |
-
int f() { return A::f() + B::f(); }
|
| 3619 |
-
};
|
| 3620 |
-
```
|
| 3621 |
-
|
| 3622 |
-
— *end example*]
|
| 3623 |
-
|
| 3624 |
-
[*Note 2*: A static member, a nested type or an enumerator defined in a
|
| 3625 |
-
base class `T` can unambiguously be found even if an object has more
|
| 3626 |
-
than one base class subobject of type `T`. Two base class subobjects
|
| 3627 |
-
share the non-static member subobjects of their common virtual base
|
| 3628 |
-
classes. — *end note*]
|
| 3629 |
-
|
| 3630 |
-
[*Example 3*:
|
| 3631 |
-
|
| 3632 |
-
``` cpp
|
| 3633 |
-
struct V {
|
| 3634 |
-
int v;
|
| 3635 |
-
};
|
| 3636 |
-
struct A {
|
| 3637 |
-
int a;
|
| 3638 |
-
static int s;
|
| 3639 |
-
enum { e };
|
| 3640 |
-
};
|
| 3641 |
-
struct B : A, virtual V { };
|
| 3642 |
-
struct C : A, virtual V { };
|
| 3643 |
-
struct D : B, C { };
|
| 3644 |
-
|
| 3645 |
-
void f(D* pd) {
|
| 3646 |
-
pd->v++; // OK: only one v (virtual)
|
| 3647 |
-
pd->s++; // OK: only one s (static)
|
| 3648 |
-
int i = pd->e; // OK: only one e (enumerator)
|
| 3649 |
-
pd->a++; // error: ambiguous: two a{s} in D
|
| 3650 |
-
}
|
| 3651 |
-
```
|
| 3652 |
-
|
| 3653 |
-
— *end example*]
|
| 3654 |
-
|
| 3655 |
-
[*Note 3*: When virtual base classes are used, a hidden declaration
|
| 3656 |
-
can be reached along a path through the subobject lattice that does not
|
| 3657 |
-
pass through the hiding declaration. This is not an ambiguity. The
|
| 3658 |
-
identical use with non-virtual base classes is an ambiguity; in that
|
| 3659 |
-
case there is no unique instance of the name that hides all the
|
| 3660 |
-
others. — *end note*]
|
| 3661 |
-
|
| 3662 |
-
[*Example 4*:
|
| 3663 |
-
|
| 3664 |
-
``` cpp
|
| 3665 |
-
struct V { int f(); int x; };
|
| 3666 |
-
struct W { int g(); int y; };
|
| 3667 |
-
struct B : virtual V, W {
|
| 3668 |
-
int f(); int x;
|
| 3669 |
-
int g(); int y;
|
| 3670 |
-
};
|
| 3671 |
-
struct C : virtual V, W { };
|
| 3672 |
-
|
| 3673 |
-
struct D : B, C { void glorp(); };
|
| 3674 |
-
```
|
| 3675 |
-
|
| 3676 |
-
<a id="fig:class.lookup"></a>
|
| 3677 |
-
|
| 3678 |
-
![Name lookup \[fig:class.lookup\]](images/figname.svg)
|
| 3679 |
-
|
| 3680 |
-
As illustrated in Figure [[fig:class.lookup]], the names declared in
|
| 3681 |
-
`V` and the left-hand instance of `W` are hidden by those in `B`, but
|
| 3682 |
-
the names declared in the right-hand instance of `W` are not hidden at
|
| 3683 |
-
all.
|
| 3684 |
-
|
| 3685 |
-
``` cpp
|
| 3686 |
-
void D::glorp() {
|
| 3687 |
-
x++; // OK: B::x hides V::x
|
| 3688 |
-
f(); // OK: B::f() hides V::f()
|
| 3689 |
-
y++; // error: B::y and C's W::y
|
| 3690 |
-
g(); // error: B::g() and C's W::g()
|
| 3691 |
-
}
|
| 3692 |
-
```
|
| 3693 |
-
|
| 3694 |
-
— *end example*]
|
| 3695 |
-
|
| 3696 |
-
An explicit or implicit conversion from a pointer to or an expression
|
| 3697 |
-
designating an object of a derived class to a pointer or reference to
|
| 3698 |
-
one of its base classes shall unambiguously refer to a unique object
|
| 3699 |
-
representing the base class.
|
| 3700 |
-
|
| 3701 |
-
[*Example 5*:
|
| 3702 |
-
|
| 3703 |
-
``` cpp
|
| 3704 |
-
struct V { };
|
| 3705 |
-
struct A { };
|
| 3706 |
-
struct B : A, virtual V { };
|
| 3707 |
-
struct C : A, virtual V { };
|
| 3708 |
-
struct D : B, C { };
|
| 3709 |
-
|
| 3710 |
-
void g() {
|
| 3711 |
-
D d;
|
| 3712 |
-
B* pb = &d;
|
| 3713 |
-
A* pa = &d; // error: ambiguous: C's A or B's A?
|
| 3714 |
-
V* pv = &d; // OK: only one V subobject
|
| 3715 |
-
}
|
| 3716 |
-
```
|
| 3717 |
-
|
| 3718 |
-
— *end example*]
|
| 3719 |
-
|
| 3720 |
-
[*Note 4*: Even if the result of name lookup is unambiguous, use of a
|
| 3721 |
-
name found in multiple subobjects might still be ambiguous (
|
| 3722 |
-
[[conv.mem]], [[expr.ref]], [[class.access.base]]). — *end note*]
|
| 3723 |
-
|
| 3724 |
-
[*Example 6*:
|
| 3725 |
-
|
| 3726 |
-
``` cpp
|
| 3727 |
-
struct B1 {
|
| 3728 |
-
void f();
|
| 3729 |
-
static void f(int);
|
| 3730 |
-
int i;
|
| 3731 |
-
};
|
| 3732 |
-
struct B2 {
|
| 3733 |
-
void f(double);
|
| 3734 |
-
};
|
| 3735 |
-
struct I1: B1 { };
|
| 3736 |
-
struct I2: B1 { };
|
| 3737 |
-
|
| 3738 |
-
struct D: I1, I2, B2 {
|
| 3739 |
-
using B1::f;
|
| 3740 |
-
using B2::f;
|
| 3741 |
-
void g() {
|
| 3742 |
-
f(); // Ambiguous conversion of this
|
| 3743 |
-
f(0); // Unambiguous (static)
|
| 3744 |
-
f(0.0); // Unambiguous (only one B2)
|
| 3745 |
-
int B1::* mpB1 = &D::i; // Unambiguous
|
| 3746 |
-
int D::* mpD = &D::i; // Ambiguous conversion
|
| 3747 |
-
}
|
| 3748 |
-
};
|
| 3749 |
-
```
|
| 3750 |
-
|
| 3751 |
-
— *end example*]
|
| 3752 |
-
|
| 3753 |
## Member access control <a id="class.access">[[class.access]]</a>
|
| 3754 |
|
|
|
|
|
|
|
| 3755 |
A member of a class can be
|
| 3756 |
|
| 3757 |
-
- private
|
| 3758 |
-
|
| 3759 |
-
- protected
|
| 3760 |
-
|
| 3761 |
-
|
| 3762 |
-
- public
|
| 3763 |
-
restriction.
|
| 3764 |
|
| 3765 |
-
|
| 3766 |
-
|
| 3767 |
-
|
|
|
|
|
|
|
|
|
|
| 3768 |
|
| 3769 |
Members of a class defined with the keyword `class` are `private` by
|
| 3770 |
default. Members of a class defined with the keywords `struct` or
|
| 3771 |
`union` are public by default.
|
| 3772 |
|
|
@@ -3782,61 +3525,77 @@ struct S {
|
|
| 3782 |
};
|
| 3783 |
```
|
| 3784 |
|
| 3785 |
— *end example*]
|
| 3786 |
|
| 3787 |
-
Access control is applied uniformly to
|
| 3788 |
-
referred to from declarations or expressions.
|
| 3789 |
|
| 3790 |
-
[*Note
|
| 3791 |
declarations [[class.friend]] and *using-declaration*s
|
| 3792 |
[[namespace.udecl]]. — *end note*]
|
| 3793 |
|
| 3794 |
-
|
| 3795 |
-
the
|
|
|
|
| 3796 |
|
| 3797 |
-
[*
|
| 3798 |
|
| 3799 |
-
|
| 3800 |
-
|
| 3801 |
-
|
| 3802 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3803 |
|
| 3804 |
``` cpp
|
| 3805 |
class A {
|
| 3806 |
class B { };
|
| 3807 |
public:
|
| 3808 |
typedef B BB;
|
| 3809 |
};
|
| 3810 |
|
| 3811 |
void f() {
|
| 3812 |
-
A::BB x; // OK, typedef
|
| 3813 |
A::B y; // access error, A::B is private
|
| 3814 |
}
|
| 3815 |
```
|
| 3816 |
|
| 3817 |
— *end note*]
|
| 3818 |
|
| 3819 |
-
[*Note
|
| 3820 |
-
|
| 3821 |
-
|
| 3822 |
-
those members and base classes are inaccessible. — *end note*]
|
| 3823 |
|
| 3824 |
The interpretation of a given construct is established without regard to
|
| 3825 |
access control. If the interpretation established makes use of
|
| 3826 |
-
inaccessible
|
| 3827 |
|
| 3828 |
-
All access controls in [[class.access]] affect the ability to
|
| 3829 |
-
class member
|
| 3830 |
parts of the declaration preceding the name of the entity being declared
|
| 3831 |
and, if the entity is a class, the definitions of members of the class
|
| 3832 |
appearing outside the class’s *member-specification*.
|
| 3833 |
|
| 3834 |
-
[*Note
|
| 3835 |
constructors, conversion functions, and destructors. — *end note*]
|
| 3836 |
|
| 3837 |
-
[*Example
|
| 3838 |
|
| 3839 |
``` cpp
|
| 3840 |
class A {
|
| 3841 |
typedef int I; // private member
|
| 3842 |
I f();
|
|
@@ -3867,21 +3626,20 @@ is as the return type of a member of class `A`. Similarly, the use of
|
|
| 3867 |
`A`, so checking of *base-specifier*s must be deferred until the entire
|
| 3868 |
*base-specifier-list* has been seen.
|
| 3869 |
|
| 3870 |
— *end example*]
|
| 3871 |
|
| 3872 |
-
|
| 3873 |
-
point of declaration,
|
| 3874 |
-
|
| 3875 |
-
|
| 3876 |
-
|
| 3877 |
|
| 3878 |
-
|
| 3879 |
-
|
| 3880 |
-
points of use of the default *template-argument*.
|
| 3881 |
|
| 3882 |
-
[*Example
|
| 3883 |
|
| 3884 |
``` cpp
|
| 3885 |
class B { };
|
| 3886 |
template <class T> class C {
|
| 3887 |
protected:
|
|
@@ -3896,12 +3654,12 @@ D <C<B> >* d; // access error, C::TT is protected
|
|
| 3896 |
|
| 3897 |
— *end example*]
|
| 3898 |
|
| 3899 |
### Access specifiers <a id="class.access.spec">[[class.access.spec]]</a>
|
| 3900 |
|
| 3901 |
-
Member declarations can be labeled by an *access-specifier*
|
| 3902 |
-
[[class.derived]]
|
| 3903 |
|
| 3904 |
``` bnf
|
| 3905 |
access-specifier ':' member-specificationₒₚₜ
|
| 3906 |
```
|
| 3907 |
|
|
@@ -3939,13 +3697,10 @@ public:
|
|
| 3939 |
};
|
| 3940 |
```
|
| 3941 |
|
| 3942 |
— *end example*]
|
| 3943 |
|
| 3944 |
-
[*Note 1*: The effect of access control on the order of allocation of
|
| 3945 |
-
data members is specified in [[expr.rel]]. — *end note*]
|
| 3946 |
-
|
| 3947 |
When a member is redeclared within its class definition, the access
|
| 3948 |
specified at its redeclaration shall be the same as at its initial
|
| 3949 |
declaration.
|
| 3950 |
|
| 3951 |
[*Example 3*:
|
|
@@ -3960,11 +3715,11 @@ private:
|
|
| 3960 |
};
|
| 3961 |
```
|
| 3962 |
|
| 3963 |
— *end example*]
|
| 3964 |
|
| 3965 |
-
[*Note
|
| 3966 |
find the injected-class-name instead of the name of the base class in
|
| 3967 |
the scope in which it was declared. The injected-class-name might be
|
| 3968 |
less accessible than the name of the base class in the scope in which it
|
| 3969 |
was declared. — *end note*]
|
| 3970 |
|
|
@@ -4019,17 +3774,17 @@ Here `B` is a public base of `D2`, `D4`, and `D6`, a private base of
|
|
| 4019 |
|
| 4020 |
— *end example*]
|
| 4021 |
|
| 4022 |
[*Note 1*:
|
| 4023 |
|
| 4024 |
-
A member of a private base class
|
| 4025 |
-
|
| 4026 |
-
|
| 4027 |
-
[[expr.static.cast]], [[expr.cast]]
|
| 4028 |
-
derived class to a pointer to an inaccessible base
|
| 4029 |
-
ill-formed if an implicit conversion is used, but
|
| 4030 |
-
explicit cast is used. For example,
|
| 4031 |
|
| 4032 |
``` cpp
|
| 4033 |
class B {
|
| 4034 |
public:
|
| 4035 |
int mi; // non-static member
|
|
@@ -4048,24 +3803,24 @@ void DD::f() {
|
|
| 4048 |
b.mi = 3; // OK (b.mi is different from this->mi)
|
| 4049 |
b.si = 3; // OK (b.si is different from this->si)
|
| 4050 |
::B::si = 3; // OK
|
| 4051 |
::B* bp1 = this; // error: B is a private base class
|
| 4052 |
::B* bp2 = (::B*)this; // OK with cast
|
| 4053 |
-
bp2->mi = 3; // OK
|
| 4054 |
}
|
| 4055 |
```
|
| 4056 |
|
| 4057 |
— *end note*]
|
| 4058 |
|
| 4059 |
A base class `B` of `N` is *accessible* at *R*, if
|
| 4060 |
|
| 4061 |
- an invented public member of `B` would be a public member of `N`, or
|
| 4062 |
-
- *R* occurs in a member or friend of class `N`, and an invented
|
| 4063 |
-
member of `B` would be a private or protected member of `N`, or
|
| 4064 |
-
- *R* occurs in a member or friend of a class `P` derived from
|
| 4065 |
-
an invented public member of `B` would be a private or
|
| 4066 |
-
member of `P`, or
|
| 4067 |
- there exists a class `S` such that `B` is a base class of `S`
|
| 4068 |
accessible at *R* and `S` is a base class of `N` accessible at *R*.
|
| 4069 |
|
| 4070 |
[*Example 2*:
|
| 4071 |
|
|
@@ -4089,20 +3844,20 @@ class N: private S {
|
|
| 4089 |
```
|
| 4090 |
|
| 4091 |
— *end example*]
|
| 4092 |
|
| 4093 |
If a base class is accessible, one can implicitly convert a pointer to a
|
| 4094 |
-
derived class to a pointer to that base class
|
| 4095 |
-
[[conv.mem]]
|
| 4096 |
|
| 4097 |
[*Note 2*: It follows that members and friends of a class `X` can
|
| 4098 |
implicitly convert an `X*` to a pointer to a private or protected
|
| 4099 |
immediate base class of `X`. — *end note*]
|
| 4100 |
|
| 4101 |
The access to a member is affected by the class in which the member is
|
| 4102 |
-
named. This naming class is the class in
|
| 4103 |
-
|
| 4104 |
|
| 4105 |
[*Note 3*: This class can be explicit, e.g., when a *qualified-id* is
|
| 4106 |
used, or implicit, e.g., when a class member access operator
|
| 4107 |
[[expr.ref]] is used (including cases where an implicit “`this->`” is
|
| 4108 |
added). If both a class member access operator and a *qualified-id* are
|
|
@@ -4111,15 +3866,15 @@ is the class denoted by the *nested-name-specifier* of the
|
|
| 4111 |
*qualified-id* (that is, `T`). — *end note*]
|
| 4112 |
|
| 4113 |
A member `m` is accessible at the point *R* when named in class `N` if
|
| 4114 |
|
| 4115 |
- `m` as a member of `N` is public, or
|
| 4116 |
-
- `m` as a member of `N` is private, and *R* occurs in a
|
| 4117 |
-
friend of class `N`, or
|
| 4118 |
-
- `m` as a member of `N` is protected, and *R* occurs in a
|
| 4119 |
-
friend of class `N`, or in a member of a class `P` derived from
|
| 4120 |
-
where `m` as a member of `P` is public, private, or protected, or
|
| 4121 |
- there exists a base class `B` of `N` that is accessible at *R*, and
|
| 4122 |
`m` is accessible at *R* when named in class `B`.
|
| 4123 |
\[*Example 3*:
|
| 4124 |
``` cpp
|
| 4125 |
class B;
|
|
@@ -4128,11 +3883,11 @@ A member `m` is accessible at the point *R* when named in class `N` if
|
|
| 4128 |
int i;
|
| 4129 |
friend void f(B*);
|
| 4130 |
};
|
| 4131 |
class B : public A { };
|
| 4132 |
void f(B* p) {
|
| 4133 |
-
p->i = 1; // OK
|
| 4134 |
}
|
| 4135 |
```
|
| 4136 |
|
| 4137 |
— *end example*]
|
| 4138 |
|
|
@@ -4146,14 +3901,14 @@ to the naming class of the right operand.
|
|
| 4146 |
member be accessible as named. — *end note*]
|
| 4147 |
|
| 4148 |
### Friends <a id="class.friend">[[class.friend]]</a>
|
| 4149 |
|
| 4150 |
A friend of a class is a function or class that is given permission to
|
| 4151 |
-
|
| 4152 |
-
|
| 4153 |
-
|
| 4154 |
-
|
| 4155 |
|
| 4156 |
[*Example 1*:
|
| 4157 |
|
| 4158 |
The following example illustrates the differences between members and
|
| 4159 |
friends:
|
|
@@ -4176,26 +3931,26 @@ void f() {
|
|
| 4176 |
}
|
| 4177 |
```
|
| 4178 |
|
| 4179 |
— *end example*]
|
| 4180 |
|
| 4181 |
-
Declaring a class to be a friend implies that
|
| 4182 |
-
|
| 4183 |
-
|
| 4184 |
|
| 4185 |
[*Example 2*:
|
| 4186 |
|
| 4187 |
``` cpp
|
| 4188 |
class A {
|
| 4189 |
class B { };
|
| 4190 |
friend class X;
|
| 4191 |
};
|
| 4192 |
|
| 4193 |
-
struct X : A::B { // OK
|
| 4194 |
-
A::B mx; // OK
|
| 4195 |
class Y {
|
| 4196 |
-
A::B my; // OK
|
| 4197 |
};
|
| 4198 |
};
|
| 4199 |
```
|
| 4200 |
|
| 4201 |
— *end example*]
|
|
@@ -4217,74 +3972,62 @@ class Z {
|
|
| 4217 |
};
|
| 4218 |
```
|
| 4219 |
|
| 4220 |
— *end example*]
|
| 4221 |
|
| 4222 |
-
A class shall not be defined in a friend declaration.
|
| 4223 |
-
|
| 4224 |
-
[*Example 4*:
|
| 4225 |
-
|
| 4226 |
-
``` cpp
|
| 4227 |
-
class A {
|
| 4228 |
-
friend class B { }; // error: cannot define class in friend declaration
|
| 4229 |
-
};
|
| 4230 |
-
```
|
| 4231 |
-
|
| 4232 |
-
— *end example*]
|
| 4233 |
-
|
| 4234 |
A friend declaration that does not declare a function shall have one of
|
| 4235 |
the following forms:
|
| 4236 |
|
| 4237 |
``` bnf
|
| 4238 |
friend elaborated-type-specifier ';'
|
| 4239 |
friend simple-type-specifier ';'
|
| 4240 |
friend typename-specifier ';'
|
| 4241 |
```
|
| 4242 |
|
| 4243 |
-
[*Note 1*: A friend declaration
|
| 4244 |
-
*template-declaration*
|
| 4245 |
|
| 4246 |
If the type specifier in a `friend` declaration designates a (possibly
|
| 4247 |
cv-qualified) class type, that class is declared as a friend; otherwise,
|
| 4248 |
the friend declaration is ignored.
|
| 4249 |
|
| 4250 |
-
[*Example
|
| 4251 |
|
| 4252 |
``` cpp
|
| 4253 |
class C;
|
| 4254 |
typedef C Ct;
|
| 4255 |
|
| 4256 |
class X1 {
|
| 4257 |
-
friend C; // OK
|
| 4258 |
};
|
| 4259 |
|
| 4260 |
class X2 {
|
| 4261 |
-
friend Ct; // OK
|
| 4262 |
-
friend D; // error:
|
| 4263 |
-
friend class D; // OK
|
| 4264 |
};
|
| 4265 |
|
| 4266 |
template <typename T> class R {
|
| 4267 |
friend T;
|
| 4268 |
};
|
| 4269 |
|
| 4270 |
R<C> rc; // class C is a friend of R<C>
|
| 4271 |
-
R<int> Ri; // OK
|
| 4272 |
```
|
| 4273 |
|
| 4274 |
— *end example*]
|
| 4275 |
|
| 4276 |
A function first declared in a friend declaration has the linkage of the
|
| 4277 |
-
namespace of which it is a member
|
| 4278 |
-
|
| 4279 |
-
linkage [[dcl.stc]].
|
| 4280 |
|
| 4281 |
-
|
| 4282 |
-
the function specified by the parameter types becomes a friend. A member
|
| 4283 |
-
function of a class `X` can be a friend of a class `Y`.
|
| 4284 |
|
| 4285 |
-
|
|
|
|
|
|
|
|
|
|
| 4286 |
|
| 4287 |
``` cpp
|
| 4288 |
class Y {
|
| 4289 |
friend char* X::foo(int);
|
| 4290 |
friend X::X(char); // constructors can be friends
|
|
@@ -4292,15 +4035,17 @@ class Y {
|
|
| 4292 |
};
|
| 4293 |
```
|
| 4294 |
|
| 4295 |
— *end example*]
|
| 4296 |
|
| 4297 |
-
|
| 4298 |
-
if the class is a non-local class [[class.local]], the function name is
|
| 4299 |
-
unqualified, and the function has namespace scope.
|
| 4300 |
|
| 4301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4302 |
|
| 4303 |
``` cpp
|
| 4304 |
class M {
|
| 4305 |
friend void f() { } // definition of global f, a friend of M,
|
| 4306 |
// not the definition of a member function
|
|
@@ -4308,26 +4053,27 @@ class M {
|
|
| 4308 |
```
|
| 4309 |
|
| 4310 |
— *end example*]
|
| 4311 |
|
| 4312 |
Such a function is implicitly an inline [[dcl.inline]] function if it is
|
| 4313 |
-
attached to the global module.
|
| 4314 |
-
|
| 4315 |
-
function defined outside
|
|
|
|
| 4316 |
|
| 4317 |
No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
|
| 4318 |
a friend declaration.
|
| 4319 |
|
| 4320 |
-
A
|
| 4321 |
-
|
| 4322 |
-
|
| 4323 |
-
|
| 4324 |
*member-specification*.
|
| 4325 |
|
| 4326 |
Friendship is neither inherited nor transitive.
|
| 4327 |
|
| 4328 |
-
[*Example
|
| 4329 |
|
| 4330 |
``` cpp
|
| 4331 |
class A {
|
| 4332 |
friend class B;
|
| 4333 |
int a;
|
|
@@ -4350,19 +4096,48 @@ class D : public B {
|
|
| 4350 |
};
|
| 4351 |
```
|
| 4352 |
|
| 4353 |
— *end example*]
|
| 4354 |
|
| 4355 |
-
|
| 4356 |
-
|
| 4357 |
-
|
| 4358 |
-
|
| 4359 |
-
|
| 4360 |
-
|
| 4361 |
-
|
| 4362 |
-
|
| 4363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4364 |
|
| 4365 |
[*Example 9*:
|
| 4366 |
|
| 4367 |
``` cpp
|
| 4368 |
class X;
|
|
@@ -4388,17 +4163,19 @@ void f() {
|
|
| 4388 |
### Protected member access <a id="class.protected">[[class.protected]]</a>
|
| 4389 |
|
| 4390 |
An additional access check beyond those described earlier in
|
| 4391 |
[[class.access]] is applied when a non-static data member or non-static
|
| 4392 |
member function is a protected member of its naming class
|
| 4393 |
-
[[class.access.base]].[^13]
|
| 4394 |
-
|
| 4395 |
-
|
| 4396 |
-
|
| 4397 |
-
|
| 4398 |
-
|
| 4399 |
-
|
|
|
|
|
|
|
| 4400 |
|
| 4401 |
[*Example 1*:
|
| 4402 |
|
| 4403 |
``` cpp
|
| 4404 |
class B {
|
|
@@ -4468,11 +4245,11 @@ private:
|
|
| 4468 |
void f() {
|
| 4469 |
D d;
|
| 4470 |
B* pb = &d;
|
| 4471 |
D* pd = &d;
|
| 4472 |
|
| 4473 |
-
pb->f(); // OK
|
| 4474 |
pd->f(); // error: D::f() is private
|
| 4475 |
}
|
| 4476 |
```
|
| 4477 |
|
| 4478 |
— *end example*]
|
|
@@ -4482,12 +4259,13 @@ used to denote the object for which the member function is called (`B*`
|
|
| 4482 |
in the example above). The access of the member function in the class in
|
| 4483 |
which it was defined (`D` in the example above) is in general not known.
|
| 4484 |
|
| 4485 |
### Multiple access <a id="class.paths">[[class.paths]]</a>
|
| 4486 |
|
| 4487 |
-
If a
|
| 4488 |
-
graph, the access is that of the path that gives most
|
|
|
|
| 4489 |
|
| 4490 |
[*Example 1*:
|
| 4491 |
|
| 4492 |
``` cpp
|
| 4493 |
class W { public: void f(); };
|
|
@@ -4516,14 +4294,14 @@ shall be obeyed.
|
|
| 4516 |
class E {
|
| 4517 |
int x;
|
| 4518 |
class B { };
|
| 4519 |
|
| 4520 |
class I {
|
| 4521 |
-
B b; // OK
|
| 4522 |
int y;
|
| 4523 |
void f(E* p, int i) {
|
| 4524 |
-
p->x = i; // OK
|
| 4525 |
}
|
| 4526 |
};
|
| 4527 |
|
| 4528 |
int g(I* p) {
|
| 4529 |
return p->y; // error: I::y is private
|
|
@@ -4533,10 +4311,12 @@ class E {
|
|
| 4533 |
|
| 4534 |
— *end example*]
|
| 4535 |
|
| 4536 |
## Initialization <a id="class.init">[[class.init]]</a>
|
| 4537 |
|
|
|
|
|
|
|
| 4538 |
When no initializer is specified for an object of (possibly
|
| 4539 |
cv-qualified) class type (or array thereof), or the initializer has the
|
| 4540 |
form `()`, the object is initialized as specified in [[dcl.init]].
|
| 4541 |
|
| 4542 |
An object of class type (or array thereof) can be explicitly
|
|
@@ -4596,11 +4376,11 @@ complex v[6] = { 1, complex(1,2), complex(), 2 };
|
|
| 4596 |
```
|
| 4597 |
|
| 4598 |
Here, `complex::complex(double)` is called for the initialization of
|
| 4599 |
`v[0]` and `v[3]`, `complex::complex({}double, double)` is called for
|
| 4600 |
the initialization of `v[1]`, `complex::complex()` is called for the
|
| 4601 |
-
initialization `v[2]`, `v[4]`, and `v[5]`. For another example,
|
| 4602 |
|
| 4603 |
``` cpp
|
| 4604 |
struct X {
|
| 4605 |
int i;
|
| 4606 |
float f;
|
|
@@ -4653,20 +4433,18 @@ mem-initializer:
|
|
| 4653 |
mem-initializer-id:
|
| 4654 |
class-or-decltype
|
| 4655 |
identifier
|
| 4656 |
```
|
| 4657 |
|
| 4658 |
-
|
| 4659 |
-
|
| 4660 |
-
scope, it is looked up in the scope containing the constructor’s
|
| 4661 |
-
definition.
|
| 4662 |
|
| 4663 |
[*Note 1*: If the constructor’s class contains a member with the same
|
| 4664 |
name as a direct or virtual base class of the class, a
|
| 4665 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 4666 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 4667 |
-
the hidden base class
|
| 4668 |
name. — *end note*]
|
| 4669 |
|
| 4670 |
Unless the *mem-initializer-id* names the constructor’s class, a
|
| 4671 |
non-static data member of the constructor’s class, or a direct or
|
| 4672 |
virtual base of that class, the *mem-initializer* is ill-formed.
|
|
@@ -4685,11 +4463,11 @@ C::C(): global_A() { } // mem-initializer for base A
|
|
| 4685 |
```
|
| 4686 |
|
| 4687 |
— *end example*]
|
| 4688 |
|
| 4689 |
If a *mem-initializer-id* is ambiguous because it designates both a
|
| 4690 |
-
direct non-virtual base class and an
|
| 4691 |
*mem-initializer* is ill-formed.
|
| 4692 |
|
| 4693 |
[*Example 2*:
|
| 4694 |
|
| 4695 |
``` cpp
|
|
@@ -4773,14 +4551,15 @@ struct A {
|
|
| 4773 |
};
|
| 4774 |
```
|
| 4775 |
|
| 4776 |
— *end example*]
|
| 4777 |
|
| 4778 |
-
In a non-delegating constructor
|
| 4779 |
-
|
| 4780 |
-
|
| 4781 |
-
|
|
|
|
| 4782 |
|
| 4783 |
- if the entity is a non-static data member that has a default member
|
| 4784 |
initializer [[class.mem]] and either
|
| 4785 |
- the constructor’s class is a union [[class.union]], and no other
|
| 4786 |
variant member of that union is designated by a *mem-initializer-id*
|
|
@@ -4795,11 +4574,11 @@ has no *ctor-initializer*), then
|
|
| 4795 |
[[class.union.anon]], no initialization is performed;
|
| 4796 |
- otherwise, the entity is default-initialized [[dcl.init]].
|
| 4797 |
|
| 4798 |
[*Note 3*: An abstract class [[class.abstract]] is never a most derived
|
| 4799 |
class, thus its constructors never initialize virtual base classes,
|
| 4800 |
-
therefore the corresponding *mem-initializer*s
|
| 4801 |
omitted. — *end note*]
|
| 4802 |
|
| 4803 |
An attempt to initialize more than one non-static data member of a union
|
| 4804 |
renders the program ill-formed.
|
| 4805 |
|
|
@@ -4821,14 +4600,14 @@ struct B {
|
|
| 4821 |
B(int);
|
| 4822 |
};
|
| 4823 |
|
| 4824 |
struct C {
|
| 4825 |
C() { } // initializes members as follows:
|
| 4826 |
-
A a; // OK
|
| 4827 |
const B b; // error: B has no default constructor
|
| 4828 |
-
int i; // OK
|
| 4829 |
-
int j = 5; // OK
|
| 4830 |
};
|
| 4831 |
```
|
| 4832 |
|
| 4833 |
— *end example*]
|
| 4834 |
|
|
@@ -4934,13 +4713,14 @@ B b(3); // use V()
|
|
| 4934 |
C c(4); // use V()
|
| 4935 |
```
|
| 4936 |
|
| 4937 |
— *end example*]
|
| 4938 |
|
| 4939 |
-
|
| 4940 |
-
*mem-initializer*
|
| 4941 |
-
|
|
|
|
| 4942 |
|
| 4943 |
[*Example 10*:
|
| 4944 |
|
| 4945 |
``` cpp
|
| 4946 |
class X {
|
|
@@ -4959,15 +4739,10 @@ of the constructor parameter `i`, initializes `X::i` with the value of
|
|
| 4959 |
the constructor parameter `i`, and initializes `X::j` with the value of
|
| 4960 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 4961 |
|
| 4962 |
— *end example*]
|
| 4963 |
|
| 4964 |
-
[*Note 7*: Because the *mem-initializer* are evaluated in the scope of
|
| 4965 |
-
the constructor, the `this` pointer can be used in the *expression-list*
|
| 4966 |
-
of a *mem-initializer* to refer to the object being
|
| 4967 |
-
initialized. — *end note*]
|
| 4968 |
-
|
| 4969 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 4970 |
can be called for an object under construction. Similarly, an object
|
| 4971 |
under construction can be the operand of the `typeid` operator
|
| 4972 |
[[expr.typeid]] or of a `dynamic_cast` [[expr.dynamic.cast]]. However,
|
| 4973 |
if these operations are performed in a *ctor-initializer* (or in a
|
|
@@ -5004,11 +4779,11 @@ public:
|
|
| 5004 |
};
|
| 5005 |
```
|
| 5006 |
|
| 5007 |
— *end example*]
|
| 5008 |
|
| 5009 |
-
[*Note 8*: [[class.cdtor]] describes the
|
| 5010 |
calls, `typeid` and `dynamic_cast`s during construction for the
|
| 5011 |
well-defined cases; that is, describes the polymorphic behavior of an
|
| 5012 |
object under construction. — *end note*]
|
| 5013 |
|
| 5014 |
A *mem-initializer* followed by an ellipsis is a pack expansion
|
|
@@ -5058,28 +4833,28 @@ struct D1 : B1 {
|
|
| 5058 |
int x;
|
| 5059 |
int y = get();
|
| 5060 |
};
|
| 5061 |
|
| 5062 |
void test() {
|
| 5063 |
-
D1 d(2, 3, 4); // OK
|
| 5064 |
// then d.x is default-initialized (no initialization is performed),
|
| 5065 |
// then d.y is initialized by calling get()
|
| 5066 |
-
D1 e; // error: D1 has
|
| 5067 |
}
|
| 5068 |
|
| 5069 |
struct D2 : B2 {
|
| 5070 |
using B2::B2;
|
| 5071 |
B1 b;
|
| 5072 |
};
|
| 5073 |
|
| 5074 |
-
D2 f(1.0); // error: B1 has
|
| 5075 |
|
| 5076 |
struct W { W(int); };
|
| 5077 |
struct X : virtual W { using W::W; X() = delete; };
|
| 5078 |
struct Y : X { using X::X; };
|
| 5079 |
struct Z : Y, virtual W { using Y::Y; };
|
| 5080 |
-
Z z(0); // OK
|
| 5081 |
|
| 5082 |
template<class T> struct Log : T {
|
| 5083 |
using T::T; // inherits all constructors from class T
|
| 5084 |
~Log() { std::clog << "Destroying wrapper" << std::endl; }
|
| 5085 |
};
|
|
@@ -5115,18 +4890,18 @@ struct D2 : V1, V2 {
|
|
| 5115 |
using V1::V1;
|
| 5116 |
using V2::V2;
|
| 5117 |
};
|
| 5118 |
|
| 5119 |
D1 d1(0); // error: ambiguous
|
| 5120 |
-
D2 d2(0); // OK
|
| 5121 |
// then initializes the V1 and V2 base classes as if by a defaulted default constructor
|
| 5122 |
|
| 5123 |
struct M { M(); M(int); };
|
| 5124 |
struct N : M { using M::M; };
|
| 5125 |
struct O : M {};
|
| 5126 |
struct P : N, O { using N::N; using O::O; };
|
| 5127 |
-
P p(0); // OK
|
| 5128 |
// use M() to initialize O's base class
|
| 5129 |
```
|
| 5130 |
|
| 5131 |
— *end example*]
|
| 5132 |
|
|
@@ -5357,13 +5132,15 @@ source and target of the omitted copy/move operation as simply two
|
|
| 5357 |
different ways of referring to the same object. If the first parameter
|
| 5358 |
of the selected constructor is an rvalue reference to the object’s type,
|
| 5359 |
the destruction of that object occurs when the target would have been
|
| 5360 |
destroyed; otherwise, the destruction occurs at the later of the times
|
| 5361 |
when the two objects would have been destroyed without the
|
| 5362 |
-
optimization.[^14]
|
| 5363 |
-
|
| 5364 |
-
|
|
|
|
|
|
|
| 5365 |
|
| 5366 |
- in a `return` statement in a function with a class return type, when
|
| 5367 |
the *expression* is the name of a non-volatile object with automatic
|
| 5368 |
storage duration (other than a function parameter or a variable
|
| 5369 |
introduced by the *exception-declaration* of a *handler*
|
|
@@ -5371,14 +5148,14 @@ combined to eliminate multiple copies):
|
|
| 5371 |
the function return type, the copy/move operation can be omitted by
|
| 5372 |
constructing the object directly into the function call’s return
|
| 5373 |
object
|
| 5374 |
- in a *throw-expression* [[expr.throw]], when the operand is the name
|
| 5375 |
of a non-volatile object with automatic storage duration (other than a
|
| 5376 |
-
function or catch-clause parameter)
|
| 5377 |
-
|
| 5378 |
-
|
| 5379 |
-
into the exception object
|
| 5380 |
- in a coroutine [[dcl.fct.def.coroutine]], a copy of a coroutine
|
| 5381 |
parameter can be omitted and references to that copy replaced with
|
| 5382 |
references to the corresponding parameter if the meaning of the
|
| 5383 |
program will be unchanged except for the execution of a constructor
|
| 5384 |
and destructor for the parameter copy object
|
|
@@ -5394,12 +5171,12 @@ combined to eliminate multiple copies):
|
|
| 5394 |
|
| 5395 |
Copy elision is not permitted where an expression is evaluated in a
|
| 5396 |
context requiring a constant expression [[expr.const]] and in constant
|
| 5397 |
initialization [[basic.start.static]].
|
| 5398 |
|
| 5399 |
-
[*Note 2*:
|
| 5400 |
-
evaluated in another context. — *end note*]
|
| 5401 |
|
| 5402 |
[*Example 1*:
|
| 5403 |
|
| 5404 |
``` cpp
|
| 5405 |
class Thing {
|
|
@@ -5428,54 +5205,25 @@ constexpr A g() {
|
|
| 5428 |
|
| 5429 |
constexpr A a; // well-formed, a.p points to a
|
| 5430 |
constexpr A b = g(); // error: b.p would be dangling[expr.const]
|
| 5431 |
|
| 5432 |
void h() {
|
| 5433 |
-
A c = g(); // well-formed, c.p
|
| 5434 |
}
|
| 5435 |
```
|
| 5436 |
|
| 5437 |
Here the criteria for elision can eliminate the copying of the object
|
| 5438 |
`t` with automatic storage duration into the result object for the
|
| 5439 |
-
function call `f()`, which is the
|
| 5440 |
-
construction of
|
| 5441 |
-
|
| 5442 |
-
|
| 5443 |
-
|
| 5444 |
-
|
| 5445 |
|
| 5446 |
— *end example*]
|
| 5447 |
|
| 5448 |
-
An *implicitly movable entity* is a variable of automatic storage
|
| 5449 |
-
duration that is either a non-volatile object or an rvalue reference to
|
| 5450 |
-
a non-volatile object type. In the following copy-initialization
|
| 5451 |
-
contexts, a move operation might be used instead of a copy operation:
|
| 5452 |
-
|
| 5453 |
-
- If the *expression* in a `return` [[stmt.return]] or `co_return`
|
| 5454 |
-
[[stmt.return.coroutine]] statement is a (possibly parenthesized)
|
| 5455 |
-
*id-expression* that names an implicitly movable entity declared in
|
| 5456 |
-
the body or *parameter-declaration-clause* of the innermost enclosing
|
| 5457 |
-
function or *lambda-expression*, or
|
| 5458 |
-
- if the operand of a *throw-expression* [[expr.throw]] is a (possibly
|
| 5459 |
-
parenthesized) *id-expression* that names an implicitly movable entity
|
| 5460 |
-
whose scope does not extend beyond the *compound-statement* of the
|
| 5461 |
-
innermost *try-block* or *function-try-block* (if any) whose
|
| 5462 |
-
*compound-statement* or *ctor-initializer* encloses the
|
| 5463 |
-
*throw-expression*,
|
| 5464 |
-
|
| 5465 |
-
overload resolution to select the constructor for the copy or the
|
| 5466 |
-
`return_value` overload to call is first performed as if the expression
|
| 5467 |
-
or operand were an rvalue. If the first overload resolution fails or was
|
| 5468 |
-
not performed, overload resolution is performed again, considering the
|
| 5469 |
-
expression or operand as an lvalue.
|
| 5470 |
-
|
| 5471 |
-
[*Note 3*: This two-stage overload resolution must be performed
|
| 5472 |
-
regardless of whether copy elision will occur. It determines the
|
| 5473 |
-
constructor or the `return_value` overload to be called if elision is
|
| 5474 |
-
not performed, and the selected constructor or `return_value` overload
|
| 5475 |
-
must be accessible even if the call is elided. — *end note*]
|
| 5476 |
-
|
| 5477 |
[*Example 2*:
|
| 5478 |
|
| 5479 |
``` cpp
|
| 5480 |
class Thing {
|
| 5481 |
public:
|
|
@@ -5487,24 +5235,44 @@ private:
|
|
| 5487 |
};
|
| 5488 |
|
| 5489 |
Thing f(bool b) {
|
| 5490 |
Thing t;
|
| 5491 |
if (b)
|
| 5492 |
-
throw t; // OK
|
| 5493 |
-
return t; // OK
|
| 5494 |
}
|
| 5495 |
|
| 5496 |
-
Thing t2 = f(false); // OK
|
| 5497 |
|
| 5498 |
struct Weird {
|
| 5499 |
Weird();
|
| 5500 |
Weird(Weird&);
|
| 5501 |
};
|
| 5502 |
|
| 5503 |
-
Weird g() {
|
| 5504 |
-
Weird
|
| 5505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5506 |
}
|
| 5507 |
```
|
| 5508 |
|
| 5509 |
— *end example*]
|
| 5510 |
|
|
@@ -5538,23 +5306,19 @@ template<class T> void f() {
|
|
| 5538 |
### Defaulted comparison operator functions <a id="class.compare.default">[[class.compare.default]]</a>
|
| 5539 |
|
| 5540 |
A defaulted comparison operator function [[over.binary]] for some class
|
| 5541 |
`C` shall be a non-template function that is
|
| 5542 |
|
| 5543 |
-
- a non-static
|
| 5544 |
-
|
| 5545 |
-
`
|
| 5546 |
-
|
| 5547 |
-
parameters of type `C`.
|
| 5548 |
|
| 5549 |
-
|
| 5550 |
-
|
| 5551 |
-
|
| 5552 |
-
|
| 5553 |
-
from a context equivalent to its *function-body*. A definition of a
|
| 5554 |
-
comparison operator as defaulted that appears in a class shall be the
|
| 5555 |
-
first declaration of that function.
|
| 5556 |
|
| 5557 |
A defaulted `<=>` or `==` operator function for class `C` is defined as
|
| 5558 |
deleted if any non-static data member of `C` is of reference type or `C`
|
| 5559 |
has variant members [[class.union.anon]].
|
| 5560 |
|
|
@@ -5563,47 +5327,28 @@ A binary operator expression `a @ b` is *usable* if either
|
|
| 5563 |
- `a` or `b` is of class or enumeration type and overload resolution
|
| 5564 |
[[over.match]] as applied to `a @ b` results in a usable candidate, or
|
| 5565 |
- neither `a` nor `b` is of class or enumeration type and `a @ b` is a
|
| 5566 |
valid expression.
|
| 5567 |
|
| 5568 |
-
A defaulted comparison function is *constexpr-compatible* if it
|
| 5569 |
-
satisfies the requirements for a constexpr function [[dcl.constexpr]]
|
| 5570 |
-
and no overload resolution performed when determining whether to delete
|
| 5571 |
-
the function results in a usable candidate that is a non-constexpr
|
| 5572 |
-
function.
|
| 5573 |
-
|
| 5574 |
-
[*Note 1*:
|
| 5575 |
-
|
| 5576 |
-
This includes the overload resolutions performed:
|
| 5577 |
-
|
| 5578 |
-
- for an `operator<=>` whose return type is not `auto`, when determining
|
| 5579 |
-
whether a synthesized three-way comparison is defined,
|
| 5580 |
-
- for an `operator<=>` whose return type is `auto` or for an
|
| 5581 |
-
`operator==`, for a comparison between an element of the expanded list
|
| 5582 |
-
of subobjects and itself, or
|
| 5583 |
-
- for a secondary comparison operator `@`, for the expression `x @ y`.
|
| 5584 |
-
|
| 5585 |
-
— *end note*]
|
| 5586 |
-
|
| 5587 |
If the *member-specification* does not explicitly declare any member or
|
| 5588 |
friend named `operator==`, an `==` operator function is declared
|
| 5589 |
implicitly for each three-way comparison operator function defined as
|
| 5590 |
defaulted in the *member-specification*, with the same access and
|
| 5591 |
*function-definition* and in the same class scope as the respective
|
| 5592 |
three-way comparison operator function, except that the return type is
|
| 5593 |
replaced with `bool` and the *declarator-id* is replaced with
|
| 5594 |
`operator==`.
|
| 5595 |
|
| 5596 |
-
[*Note
|
| 5597 |
defined as defaulted in the definition of `X` and has the same
|
| 5598 |
*parameter-declaration-clause* and trailing *requires-clause* as the
|
| 5599 |
respective three-way comparison operator. It is declared with `friend`,
|
| 5600 |
`virtual`, `constexpr`, or `consteval` if the three-way comparison
|
| 5601 |
operator function is so declared. If the three-way comparison operator
|
| 5602 |
function has no *noexcept-specifier*, the implicitly-declared `==`
|
| 5603 |
operator function has an implicit exception specification
|
| 5604 |
-
[[except.spec]] that
|
| 5605 |
specification of the three-way comparison operator
|
| 5606 |
function. — *end note*]
|
| 5607 |
|
| 5608 |
[*Example 1*:
|
| 5609 |
|
|
@@ -5617,11 +5362,11 @@ template<typename T> struct X {
|
|
| 5617 |
};
|
| 5618 |
```
|
| 5619 |
|
| 5620 |
— *end example*]
|
| 5621 |
|
| 5622 |
-
[*Note
|
| 5623 |
the defaulted three-way comparison operator function is defined as
|
| 5624 |
deleted. — *end note*]
|
| 5625 |
|
| 5626 |
The direct base class subobjects of `C`, in the order of their
|
| 5627 |
declaration in the *base-specifier-list* of `C`, followed by the
|
|
@@ -5667,12 +5412,12 @@ struct D {
|
|
| 5667 |
### Three-way comparison <a id="class.spaceship">[[class.spaceship]]</a>
|
| 5668 |
|
| 5669 |
The *synthesized three-way comparison* of type `R` [[cmp.categories]] of
|
| 5670 |
glvalues `a` and `b` of the same type is defined as follows:
|
| 5671 |
|
| 5672 |
-
- If `a <=> b` is usable [[class.compare.default]]
|
| 5673 |
-
`static_cast<R>(a <=> b)`.
|
| 5674 |
- Otherwise, if overload resolution for `a <=> b` is performed and finds
|
| 5675 |
at least one viable candidate, the synthesized three-way comparison is
|
| 5676 |
not defined.
|
| 5677 |
- Otherwise, if `R` is not a comparison category type, or either the
|
| 5678 |
expression `a == b` or the expression `a < b` is not usable, the
|
|
@@ -5695,13 +5440,13 @@ glvalues `a` and `b` of the same type is defined as follows:
|
|
| 5695 |
a < b ? partial_ordering::less :
|
| 5696 |
b < a ? partial_ordering::greater :
|
| 5697 |
partial_ordering::unordered
|
| 5698 |
```
|
| 5699 |
|
| 5700 |
-
[*Note 1*: A synthesized three-way comparison
|
| 5701 |
-
|
| 5702 |
-
|
| 5703 |
|
| 5704 |
Let `R` be the declared return type of a defaulted three-way comparison
|
| 5705 |
operator function, and let `xᵢ` be the elements of the expanded list of
|
| 5706 |
subobjects for an object `x` of type `C`.
|
| 5707 |
|
|
@@ -5765,184 +5510,35 @@ struct C {
|
|
| 5765 |
};
|
| 5766 |
```
|
| 5767 |
|
| 5768 |
— *end example*]
|
| 5769 |
|
| 5770 |
-
## Free store <a id="class.free">[[class.free]]</a>
|
| 5771 |
-
|
| 5772 |
-
Any allocation function for a class `T` is a static member (even if not
|
| 5773 |
-
explicitly declared `static`).
|
| 5774 |
-
|
| 5775 |
-
[*Example 1*:
|
| 5776 |
-
|
| 5777 |
-
``` cpp
|
| 5778 |
-
class Arena;
|
| 5779 |
-
struct B {
|
| 5780 |
-
void* operator new(std::size_t, Arena*);
|
| 5781 |
-
};
|
| 5782 |
-
struct D1 : B {
|
| 5783 |
-
};
|
| 5784 |
-
|
| 5785 |
-
Arena* ap;
|
| 5786 |
-
void foo(int i) {
|
| 5787 |
-
new (ap) D1; // calls B::operator new(std::size_t, Arena*)
|
| 5788 |
-
new D1[i]; // calls ::operator new[](std::size_t)
|
| 5789 |
-
new D1; // error: ::operator new(std::size_t) hidden
|
| 5790 |
-
}
|
| 5791 |
-
```
|
| 5792 |
-
|
| 5793 |
-
— *end example*]
|
| 5794 |
-
|
| 5795 |
-
When an object is deleted with a *delete-expression* [[expr.delete]], a
|
| 5796 |
-
deallocation function (`operator delete()` for non-array objects or
|
| 5797 |
-
`operator delete[]()` for arrays) is (implicitly) called to reclaim the
|
| 5798 |
-
storage occupied by the object [[basic.stc.dynamic.deallocation]].
|
| 5799 |
-
|
| 5800 |
-
Class-specific deallocation function lookup is a part of general
|
| 5801 |
-
deallocation function lookup [[expr.delete]] and occurs as follows. If
|
| 5802 |
-
the *delete-expression* is used to deallocate a class object whose
|
| 5803 |
-
static type has a virtual destructor, the deallocation function is the
|
| 5804 |
-
one selected at the point of definition of the dynamic type’s virtual
|
| 5805 |
-
destructor [[class.dtor]].[^15] Otherwise, if the *delete-expression* is
|
| 5806 |
-
used to deallocate an object of class `T` or array thereof, the
|
| 5807 |
-
deallocation function’s name is looked up in the scope of `T`. If this
|
| 5808 |
-
lookup fails to find the name, general deallocation function lookup
|
| 5809 |
-
[[expr.delete]] continues. If the result of the lookup is ambiguous or
|
| 5810 |
-
inaccessible, or if the lookup selects a placement deallocation
|
| 5811 |
-
function, the program is ill-formed.
|
| 5812 |
-
|
| 5813 |
-
Any deallocation function for a class `X` is a static member (even if
|
| 5814 |
-
not explicitly declared `static`).
|
| 5815 |
-
|
| 5816 |
-
[*Example 2*:
|
| 5817 |
-
|
| 5818 |
-
``` cpp
|
| 5819 |
-
class X {
|
| 5820 |
-
void operator delete(void*);
|
| 5821 |
-
void operator delete[](void*, std::size_t);
|
| 5822 |
-
};
|
| 5823 |
-
|
| 5824 |
-
class Y {
|
| 5825 |
-
void operator delete(void*, std::size_t);
|
| 5826 |
-
void operator delete[](void*);
|
| 5827 |
-
};
|
| 5828 |
-
```
|
| 5829 |
-
|
| 5830 |
-
— *end example*]
|
| 5831 |
-
|
| 5832 |
-
Since member allocation and deallocation functions are `static` they
|
| 5833 |
-
cannot be virtual.
|
| 5834 |
-
|
| 5835 |
-
[*Note 1*:
|
| 5836 |
-
|
| 5837 |
-
However, when the *cast-expression* of a *delete-expression* refers to
|
| 5838 |
-
an object of class type, because the deallocation function actually
|
| 5839 |
-
called is looked up in the scope of the class that is the dynamic type
|
| 5840 |
-
of the object if the destructor is virtual, the effect is the same in
|
| 5841 |
-
that case. For example,
|
| 5842 |
-
|
| 5843 |
-
``` cpp
|
| 5844 |
-
struct B {
|
| 5845 |
-
virtual ~B();
|
| 5846 |
-
void operator delete(void*, std::size_t);
|
| 5847 |
-
};
|
| 5848 |
-
|
| 5849 |
-
struct D : B {
|
| 5850 |
-
void operator delete(void*);
|
| 5851 |
-
};
|
| 5852 |
-
|
| 5853 |
-
struct E : B {
|
| 5854 |
-
void log_deletion();
|
| 5855 |
-
void operator delete(E *p, std::destroying_delete_t) {
|
| 5856 |
-
p->log_deletion();
|
| 5857 |
-
p->~E();
|
| 5858 |
-
::operator delete(p);
|
| 5859 |
-
}
|
| 5860 |
-
};
|
| 5861 |
-
|
| 5862 |
-
void f() {
|
| 5863 |
-
B* bp = new D;
|
| 5864 |
-
delete bp; // 1: uses D::operator delete(void*)
|
| 5865 |
-
bp = new E;
|
| 5866 |
-
delete bp; // 2: uses E::operator delete(E*, std::destroying_delete_t)
|
| 5867 |
-
}
|
| 5868 |
-
```
|
| 5869 |
-
|
| 5870 |
-
Here, storage for the object of class `D` is deallocated by
|
| 5871 |
-
`D::operator delete()`, and the object of class `E` is destroyed and its
|
| 5872 |
-
storage is deallocated by `E::operator delete()`, due to the virtual
|
| 5873 |
-
destructor.
|
| 5874 |
-
|
| 5875 |
-
— *end note*]
|
| 5876 |
-
|
| 5877 |
-
[*Note 2*:
|
| 5878 |
-
|
| 5879 |
-
Virtual destructors have no effect on the deallocation function actually
|
| 5880 |
-
called when the *cast-expression* of a *delete-expression* refers to an
|
| 5881 |
-
array of objects of class type. For example,
|
| 5882 |
-
|
| 5883 |
-
``` cpp
|
| 5884 |
-
struct B {
|
| 5885 |
-
virtual ~B();
|
| 5886 |
-
void operator delete[](void*, std::size_t);
|
| 5887 |
-
};
|
| 5888 |
-
|
| 5889 |
-
struct D : B {
|
| 5890 |
-
void operator delete[](void*, std::size_t);
|
| 5891 |
-
};
|
| 5892 |
-
|
| 5893 |
-
void f(int i) {
|
| 5894 |
-
D* dp = new D[i];
|
| 5895 |
-
delete [] dp; // uses D::operator delete[](void*, std::size_t)
|
| 5896 |
-
B* bp = new D[i];
|
| 5897 |
-
delete[] bp; // undefined behavior
|
| 5898 |
-
}
|
| 5899 |
-
```
|
| 5900 |
-
|
| 5901 |
-
— *end note*]
|
| 5902 |
-
|
| 5903 |
-
Access to the deallocation function is checked statically. Hence, even
|
| 5904 |
-
though a different one might actually be executed, the statically
|
| 5905 |
-
visible deallocation function is required to be accessible.
|
| 5906 |
-
|
| 5907 |
-
[*Example 3*: For the call on line “// 1” above, if
|
| 5908 |
-
`B::operator delete()` had been private, the delete expression would
|
| 5909 |
-
have been ill-formed. — *end example*]
|
| 5910 |
-
|
| 5911 |
-
[*Note 3*: If a deallocation function has no explicit
|
| 5912 |
-
*noexcept-specifier*, it has a non-throwing exception specification
|
| 5913 |
-
[[except.spec]]. — *end note*]
|
| 5914 |
-
|
| 5915 |
<!-- Link reference definitions -->
|
|
|
|
| 5916 |
[basic.compound]: basic.md#basic.compound
|
| 5917 |
[basic.def]: basic.md#basic.def
|
| 5918 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 5919 |
[basic.life]: basic.md#basic.life
|
| 5920 |
[basic.link]: basic.md#basic.link
|
| 5921 |
[basic.lookup]: basic.md#basic.lookup
|
| 5922 |
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
| 5923 |
-
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 5924 |
-
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 5925 |
[basic.lval]: expr.md#basic.lval
|
| 5926 |
-
[basic.scope]: basic.md#basic.scope
|
| 5927 |
[basic.scope.class]: basic.md#basic.scope.class
|
| 5928 |
-
[basic.scope.
|
|
|
|
| 5929 |
[basic.start.dynamic]: basic.md#basic.start.dynamic
|
| 5930 |
[basic.start.static]: basic.md#basic.start.static
|
| 5931 |
[basic.start.term]: basic.md#basic.start.term
|
| 5932 |
-
[basic.stc]: basic.md#basic.stc
|
| 5933 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 5934 |
-
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 5935 |
-
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 5936 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 5937 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 5938 |
-
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 5939 |
[basic.types]: basic.md#basic.types
|
| 5940 |
[class]: #class
|
| 5941 |
[class.abstract]: #class.abstract
|
| 5942 |
[class.access]: #class.access
|
| 5943 |
[class.access.base]: #class.access.base
|
|
|
|
| 5944 |
[class.access.nest]: #class.access.nest
|
| 5945 |
[class.access.spec]: #class.access.spec
|
| 5946 |
[class.access.virt]: #class.access.virt
|
| 5947 |
[class.base.init]: #class.base.init
|
| 5948 |
[class.bit]: #class.bit
|
|
@@ -5951,45 +5547,50 @@ have been ill-formed. — *end example*]
|
|
| 5951 |
[class.compare.default]: #class.compare.default
|
| 5952 |
[class.compare.secondary]: #class.compare.secondary
|
| 5953 |
[class.conv]: #class.conv
|
| 5954 |
[class.conv.ctor]: #class.conv.ctor
|
| 5955 |
[class.conv.fct]: #class.conv.fct
|
|
|
|
| 5956 |
[class.copy.assign]: #class.copy.assign
|
| 5957 |
[class.copy.ctor]: #class.copy.ctor
|
| 5958 |
[class.copy.elision]: #class.copy.elision
|
| 5959 |
[class.ctor]: #class.ctor
|
|
|
|
| 5960 |
[class.default.ctor]: #class.default.ctor
|
| 5961 |
[class.derived]: #class.derived
|
|
|
|
| 5962 |
[class.dtor]: #class.dtor
|
| 5963 |
[class.eq]: #class.eq
|
| 5964 |
[class.expl.init]: #class.expl.init
|
| 5965 |
[class.free]: #class.free
|
| 5966 |
[class.friend]: #class.friend
|
| 5967 |
[class.inhctor.init]: #class.inhctor.init
|
| 5968 |
[class.init]: #class.init
|
|
|
|
| 5969 |
[class.local]: #class.local
|
| 5970 |
[class.mem]: #class.mem
|
| 5971 |
-
[class.
|
|
|
|
| 5972 |
[class.mfct]: #class.mfct
|
| 5973 |
-
[class.mfct.non
|
| 5974 |
[class.mi]: #class.mi
|
| 5975 |
[class.name]: #class.name
|
| 5976 |
[class.nest]: #class.nest
|
| 5977 |
-
[class.nested.type]: #class.nested.type
|
| 5978 |
[class.paths]: #class.paths
|
| 5979 |
[class.pre]: #class.pre
|
| 5980 |
[class.prop]: #class.prop
|
| 5981 |
[class.protected]: #class.protected
|
| 5982 |
[class.qual]: basic.md#class.qual
|
| 5983 |
[class.spaceship]: #class.spaceship
|
| 5984 |
[class.static]: #class.static
|
| 5985 |
[class.static.data]: #class.static.data
|
|
|
|
| 5986 |
[class.static.mfct]: #class.static.mfct
|
| 5987 |
[class.temporary]: basic.md#class.temporary
|
| 5988 |
-
[class.this]: #class.this
|
| 5989 |
[class.union]: #class.union
|
| 5990 |
[class.union.anon]: #class.union.anon
|
|
|
|
| 5991 |
[class.virtual]: #class.virtual
|
| 5992 |
[cmp.categories]: support.md#cmp.categories
|
| 5993 |
[cmp.categories.pre]: support.md#cmp.categories.pre
|
| 5994 |
[cmp.partialord]: support.md#cmp.partialord
|
| 5995 |
[cmp.strongord]: support.md#cmp.strongord
|
|
@@ -6004,19 +5605,23 @@ have been ill-formed. — *end example*]
|
|
| 6004 |
[dcl.decl]: dcl.md#dcl.decl
|
| 6005 |
[dcl.enum]: dcl.md#dcl.enum
|
| 6006 |
[dcl.fct]: dcl.md#dcl.fct
|
| 6007 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 6008 |
[dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
|
|
|
|
| 6009 |
[dcl.fct.def.delete]: dcl.md#dcl.fct.def.delete
|
| 6010 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 6011 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 6012 |
[dcl.fct.spec]: dcl.md#dcl.fct.spec
|
| 6013 |
[dcl.init]: dcl.md#dcl.init
|
| 6014 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
|
|
|
| 6015 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 6016 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 6017 |
[dcl.inline]: dcl.md#dcl.inline
|
|
|
|
|
|
|
| 6018 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 6019 |
[dcl.stc]: dcl.md#dcl.stc
|
| 6020 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 6021 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 6022 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
|
@@ -6051,44 +5656,46 @@ have been ill-formed. — *end example*]
|
|
| 6051 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 6052 |
[expr.typeid]: expr.md#expr.typeid
|
| 6053 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 6054 |
[intro.execution]: basic.md#intro.execution
|
| 6055 |
[intro.object]: basic.md#intro.object
|
| 6056 |
-
[namespace.def]: dcl.md#namespace.def
|
| 6057 |
-
[namespace.memdef]: dcl.md#namespace.memdef
|
| 6058 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 6059 |
[over]: over.md#over
|
| 6060 |
[over.ass]: over.md#over.ass
|
| 6061 |
[over.best.ics]: over.md#over.best.ics
|
| 6062 |
[over.binary]: over.md#over.binary
|
| 6063 |
[over.ics.ref]: over.md#over.ics.ref
|
| 6064 |
-
[over.load]: over.md#over.load
|
| 6065 |
[over.match]: over.md#over.match
|
| 6066 |
[over.match.best]: over.md#over.match.best
|
| 6067 |
[over.match.call]: over.md#over.match.call
|
| 6068 |
[over.match.copy]: over.md#over.match.copy
|
| 6069 |
[over.match.funcs]: over.md#over.match.funcs
|
| 6070 |
[over.oper]: over.md#over.oper
|
| 6071 |
[over.over]: over.md#over.over
|
| 6072 |
[special]: #special
|
| 6073 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 6074 |
[stmt.return]: stmt.md#stmt.return
|
| 6075 |
-
[stmt.return.coroutine]: stmt.md#stmt.return.coroutine
|
| 6076 |
[string.classes]: strings.md#string.classes
|
| 6077 |
[temp.arg]: temp.md#temp.arg
|
| 6078 |
-
[temp.class.spec]: temp.md#temp.class.spec
|
| 6079 |
[temp.constr]: temp.md#temp.constr
|
| 6080 |
[temp.constr.order]: temp.md#temp.constr.order
|
|
|
|
| 6081 |
[temp.dep.type]: temp.md#temp.dep.type
|
| 6082 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
|
|
|
| 6083 |
[temp.friend]: temp.md#temp.friend
|
| 6084 |
[temp.inst]: temp.md#temp.inst
|
| 6085 |
[temp.mem]: temp.md#temp.mem
|
| 6086 |
[temp.param]: temp.md#temp.param
|
| 6087 |
[temp.pre]: temp.md#temp.pre
|
| 6088 |
-
[temp.spec]: temp.md#temp.spec
|
| 6089 |
[temp.variadic]: temp.md#temp.variadic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6090 |
|
| 6091 |
[^1]: This ensures that two subobjects that have the same class type and
|
| 6092 |
that belong to the same most derived object are not allocated at the
|
| 6093 |
same address [[expr.eq]].
|
| 6094 |
|
|
@@ -6109,23 +5716,23 @@ have been ill-formed. — *end example*]
|
|
| 6109 |
[^5]: This implies that the reference parameter of the
|
| 6110 |
implicitly-declared copy assignment operator cannot bind to a
|
| 6111 |
`volatile` lvalue; see [[diff.class]].
|
| 6112 |
|
| 6113 |
[^6]: These conversions are considered as standard conversions for the
|
| 6114 |
-
purposes of overload resolution
|
| 6115 |
-
|
| 6116 |
-
|
| 6117 |
-
|
| 6118 |
-
|
| 6119 |
-
|
| 6120 |
-
|
| 6121 |
|
| 6122 |
[^7]: The use of the `virtual` specifier in the declaration of an
|
| 6123 |
overriding function is valid but redundant (has empty semantics).
|
| 6124 |
|
| 6125 |
[^8]: If all virtual functions are immediate functions, the class is
|
| 6126 |
-
still polymorphic even
|
| 6127 |
otherwise require any additions for that polymorphic behavior.
|
| 6128 |
|
| 6129 |
[^9]: A function with the same name but a different parameter list
|
| 6130 |
[[over]] as a virtual function is not necessarily virtual and does
|
| 6131 |
not override. Access control [[class.access]] is not considered in
|
|
@@ -6146,10 +5753,5 @@ have been ill-formed. — *end example*]
|
|
| 6146 |
static data members or enumerator member constants.
|
| 6147 |
|
| 6148 |
[^14]: Because only one object is destroyed instead of two, and one
|
| 6149 |
copy/move constructor is not executed, there is still one object
|
| 6150 |
destroyed for each one constructed.
|
| 6151 |
-
|
| 6152 |
-
[^15]: A similar provision is not needed for the array version of
|
| 6153 |
-
`operator` `delete` because [[expr.delete]] requires that in this
|
| 6154 |
-
situation, the static type of the object to be deleted be the same
|
| 6155 |
-
as its dynamic type.
|
|
|
|
| 43 |
union
|
| 44 |
```
|
| 45 |
|
| 46 |
A class declaration where the *class-name* in the *class-head-name* is a
|
| 47 |
*simple-template-id* shall be an explicit specialization
|
| 48 |
+
[[temp.expl.spec]] or a partial specialization [[temp.spec.partial]]. A
|
| 49 |
*class-specifier* whose *class-head* omits the *class-head-name* defines
|
| 50 |
an unnamed class.
|
| 51 |
|
| 52 |
[*Note 1*: An unnamed class thus can’t be `final`. — *end note*]
|
| 53 |
|
| 54 |
+
Otherwise, the *class-name* is an *identifier*; it is not looked up, and
|
| 55 |
+
the *class-specifier* introduces it.
|
| 56 |
+
|
| 57 |
+
The *class-name* is also bound in the scope of the class (template)
|
| 58 |
+
itself; this is known as the *injected-class-name*. For purposes of
|
| 59 |
+
access checking, the injected-class-name is treated as if it were a
|
| 60 |
+
public member name. A *class-specifier* is commonly referred to as a
|
| 61 |
+
*class definition*. A class is considered defined after the closing
|
| 62 |
+
brace of its *class-specifier* has been seen even though its member
|
| 63 |
+
functions are in general not yet defined. The optional
|
| 64 |
+
*attribute-specifier-seq* appertains to the class; the attributes in the
|
| 65 |
+
*attribute-specifier-seq* are thereafter considered attributes of the
|
| 66 |
+
class whenever it is named.
|
| 67 |
|
| 68 |
If a *class-head-name* contains a *nested-name-specifier*, the
|
| 69 |
+
*class-specifier* shall not inhabit a class scope. If its *class-name*
|
| 70 |
+
is an *identifier*, the *class-specifier* shall correspond to one or
|
| 71 |
+
more declarations nominable in the class, class template, or namespace
|
| 72 |
+
to which the *nested-name-specifier* refers; they shall all have the
|
| 73 |
+
same target scope, and the target scope of the *class-specifier* is that
|
| 74 |
+
scope.
|
| 75 |
+
|
| 76 |
+
[*Example 1*:
|
| 77 |
+
|
| 78 |
+
``` cpp
|
| 79 |
+
namespace N {
|
| 80 |
+
template<class>
|
| 81 |
+
struct A {
|
| 82 |
+
struct B;
|
| 83 |
+
};
|
| 84 |
+
}
|
| 85 |
+
using N::A;
|
| 86 |
+
template<class T> struct A<T>::B {}; // OK
|
| 87 |
+
template<> struct A<void> {}; // OK
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
— *end example*]
|
| 91 |
|
| 92 |
[*Note 2*: The *class-key* determines whether the class is a union
|
| 93 |
[[class.union]] and whether access is public or private by default
|
| 94 |
[[class.access]]. A union holds the value of at most one data member at
|
| 95 |
a time. — *end note*]
|
|
|
|
| 98 |
appears as a *class-or-decltype* in a *base-clause* [[class.derived]],
|
| 99 |
the program is ill-formed. Whenever a *class-key* is followed by a
|
| 100 |
*class-head-name*, the *identifier* `final`, and a colon or left brace,
|
| 101 |
`final` is interpreted as a *class-virt-specifier*.
|
| 102 |
|
| 103 |
+
[*Example 2*:
|
| 104 |
|
| 105 |
``` cpp
|
| 106 |
struct A;
|
| 107 |
+
struct A final {}; // OK, definition of struct A,
|
| 108 |
// not value-initialization of variable final
|
| 109 |
|
| 110 |
struct X {
|
| 111 |
struct C { constexpr operator int() { return 5; } };
|
| 112 |
+
struct B final : C{}; // OK, definition of nested class B,
|
| 113 |
// not declaration of a bit-field member final
|
| 114 |
};
|
| 115 |
```
|
| 116 |
|
| 117 |
— *end example*]
|
| 118 |
|
| 119 |
[*Note 3*: Complete objects of class type have nonzero size. Base class
|
| 120 |
subobjects and members declared with the `no_unique_address` attribute
|
| 121 |
[[dcl.attr.nouniqueaddr]] are not so constrained. — *end note*]
|
| 122 |
|
| 123 |
+
[*Note 4*: Class objects can be assigned
|
| 124 |
+
[[over.ass]], [[class.copy.assign]], passed as arguments to functions
|
| 125 |
+
[[dcl.init]], [[class.copy.ctor]], and returned by functions (except
|
| 126 |
+
objects of classes for which copying or moving has been restricted; see
|
| 127 |
[[dcl.fct.def.delete]] and [[class.access]]). Other plausible operators,
|
| 128 |
such as equality comparison, can be defined by the user; see
|
| 129 |
[[over.oper]]. — *end note*]
|
| 130 |
|
| 131 |
## Properties of classes <a id="class.prop">[[class.prop]]</a>
|
| 132 |
|
| 133 |
A *trivially copyable class* is a class:
|
| 134 |
|
| 135 |
- that has at least one eligible copy constructor, move constructor,
|
| 136 |
+
copy assignment operator, or move assignment operator
|
| 137 |
+
[[special]], [[class.copy.ctor]], [[class.copy.assign]],
|
| 138 |
- where each eligible copy constructor, move constructor, copy
|
| 139 |
assignment operator, and move assignment operator is trivial, and
|
| 140 |
- that has a trivial, non-deleted destructor [[class.dtor]].
|
| 141 |
|
| 142 |
A *trivial class* is a class that is trivially copyable and has one or
|
|
|
|
| 159 |
- has all non-static data members and bit-fields in the class and its
|
| 160 |
base classes first declared in the same class, and
|
| 161 |
- has no element of the set M(S) of types as a base class, where for any
|
| 162 |
type `X`, M(X) is defined as follows.[^1]
|
| 163 |
\[*Note 2*: M(X) is the set of the types of all non-base-class
|
| 164 |
+
subobjects that can be at a zero offset in `X`. — *end note*]
|
| 165 |
+
- If `X` is a non-union class type with no non-static data members,
|
| 166 |
+
the set M(X) is empty.
|
| 167 |
- If `X` is a non-union class type with a non-static data member of
|
| 168 |
type X₀ that is either of zero size or is the first non-static data
|
| 169 |
member of `X` (where said member may be an anonymous union), the set
|
| 170 |
M(X) consists of X₀ and the elements of M(X₀).
|
| 171 |
- If `X` is a union type, the set M(X) is the union of all M(Uᵢ) and
|
|
|
|
| 230 |
— *end example*]
|
| 231 |
|
| 232 |
[*Note 4*: Aggregates of class type are described in
|
| 233 |
[[dcl.init.aggr]]. — *end note*]
|
| 234 |
|
| 235 |
+
A class `S` is an *implicit-lifetime class* if
|
| 236 |
+
|
| 237 |
+
- it is an aggregate whose destructor is not user-provided or
|
| 238 |
+
- it has at least one trivial eligible constructor and a trivial,
|
| 239 |
+
non-deleted destructor.
|
| 240 |
|
| 241 |
## Class names <a id="class.name">[[class.name]]</a>
|
| 242 |
|
| 243 |
A class definition introduces a new type.
|
| 244 |
|
|
|
|
| 264 |
``` cpp
|
| 265 |
int f(X);
|
| 266 |
int f(Y);
|
| 267 |
```
|
| 268 |
|
| 269 |
+
declare overloads [[over]] named `f` and not simply a single function
|
| 270 |
+
`f` twice. For the same reason,
|
| 271 |
|
| 272 |
``` cpp
|
| 273 |
struct S { int a; };
|
| 274 |
struct S { int a; }; // error: double definition
|
| 275 |
```
|
| 276 |
|
| 277 |
is ill-formed because it defines `S` twice.
|
| 278 |
|
| 279 |
— *end example*]
|
| 280 |
|
| 281 |
+
[*Note 1*:
|
| 282 |
+
|
| 283 |
+
It can be necessary to use an *elaborated-type-specifier* to refer to a
|
| 284 |
+
class that belongs to a scope in which its name is also bound to a
|
| 285 |
+
variable, function, or enumerator [[basic.lookup.elab]].
|
|
|
|
|
|
|
| 286 |
|
| 287 |
[*Example 2*:
|
| 288 |
|
| 289 |
``` cpp
|
| 290 |
struct stat {
|
| 291 |
// ...
|
| 292 |
};
|
| 293 |
|
| 294 |
stat gstat; // use plain stat to define variable
|
| 295 |
|
| 296 |
+
int stat(struct stat*); // stat now also names a function
|
| 297 |
|
| 298 |
void f() {
|
| 299 |
struct stat* ps; // struct prefix needed to name struct stat
|
| 300 |
+
stat(ps); // call stat function
|
| 301 |
}
|
| 302 |
```
|
| 303 |
|
| 304 |
— *end example*]
|
| 305 |
|
| 306 |
+
An *elaborated-type-specifier* can also be used to declare an
|
| 307 |
+
*identifier* as a *class-name*.
|
|
|
|
|
|
|
| 308 |
|
| 309 |
[*Example 3*:
|
| 310 |
|
| 311 |
``` cpp
|
| 312 |
struct s { int a; };
|
|
|
|
| 319 |
}
|
| 320 |
```
|
| 321 |
|
| 322 |
— *end example*]
|
| 323 |
|
|
|
|
|
|
|
| 324 |
Such declarations allow definition of classes that refer to each other.
|
| 325 |
|
| 326 |
[*Example 4*:
|
| 327 |
|
| 328 |
``` cpp
|
|
|
|
| 346 |
|
| 347 |
— *end note*]
|
| 348 |
|
| 349 |
[*Note 2*: An *elaborated-type-specifier* [[dcl.type.elab]] can also be
|
| 350 |
used as a *type-specifier* as part of a declaration. It differs from a
|
| 351 |
+
class declaration in that it can refer to an existing class of the given
|
| 352 |
+
name. — *end note*]
|
| 353 |
|
| 354 |
[*Example 5*:
|
| 355 |
|
| 356 |
``` cpp
|
| 357 |
struct s { int a; };
|
|
|
|
| 384 |
A *simple-template-id* is only a *class-name* if its *template-name*
|
| 385 |
names a class template.
|
| 386 |
|
| 387 |
## Class members <a id="class.mem">[[class.mem]]</a>
|
| 388 |
|
| 389 |
+
### General <a id="class.mem.general">[[class.mem.general]]</a>
|
| 390 |
+
|
| 391 |
``` bnf
|
| 392 |
member-specification:
|
| 393 |
member-declaration member-specificationₒₚₜ
|
| 394 |
access-specifier ':' member-specificationₒₚₜ
|
| 395 |
```
|
|
|
|
| 439 |
pure-specifier:
|
| 440 |
'=' '0'
|
| 441 |
```
|
| 442 |
|
| 443 |
The *member-specification* in a class definition declares the full set
|
| 444 |
+
of members of the class; no member can be added elsewhere. A
|
| 445 |
+
*direct member* of a class `X` is a member of `X` that was first
|
| 446 |
+
declared within the *member-specification* of `X`, including anonymous
|
| 447 |
+
union members [[class.union.anon]] and direct members thereof. Members
|
| 448 |
+
of a class are data members, member functions [[class.mfct]], nested
|
| 449 |
+
types, enumerators, and member templates [[temp.mem]] and
|
| 450 |
+
specializations thereof.
|
| 451 |
|
| 452 |
[*Note 1*: A specialization of a static data member template is a
|
| 453 |
static data member. A specialization of a member function template is a
|
| 454 |
member function. A specialization of a member class template is a nested
|
| 455 |
class. — *end note*]
|
| 456 |
|
| 457 |
A *member-declaration* does not declare new members of the class if it
|
| 458 |
is
|
| 459 |
|
| 460 |
- a friend declaration [[class.friend]],
|
| 461 |
+
- a *deduction-guide* [[temp.deduct.guide]],
|
| 462 |
+
- a *template-declaration* whose *declaration* is one of the above,
|
| 463 |
- a *static_assert-declaration*,
|
| 464 |
- a *using-declaration* [[namespace.udecl]], or
|
| 465 |
- an *empty-declaration*.
|
| 466 |
|
| 467 |
For any other *member-declaration*, each declared entity that is not an
|
|
|
|
| 469 |
*member-declaration* shall either declare at least one member name of
|
| 470 |
the class or declare at least one unnamed bit-field.
|
| 471 |
|
| 472 |
A *data member* is a non-function member introduced by a
|
| 473 |
*member-declarator*. A *member function* is a member that is a function.
|
| 474 |
+
Nested types are classes [[class.name]], [[class.nest]] and enumerations
|
| 475 |
+
[[dcl.enum]] declared in the class and arbitrary types declared as
|
| 476 |
+
members by use of a typedef declaration [[dcl.typedef]] or
|
| 477 |
*alias-declaration*. The enumerators of an unscoped enumeration
|
| 478 |
[[dcl.enum]] defined in the class are members of the class.
|
| 479 |
|
| 480 |
A data member or member function may be declared `static` in its
|
| 481 |
*member-declaration*, in which case it is a *static member* (see
|
| 482 |
[[class.static]]) (a *static data member* [[class.static.data]] or
|
| 483 |
*static member function* [[class.static.mfct]], respectively) of the
|
| 484 |
class. Any other data member or member function is a *non-static member*
|
| 485 |
+
(a *non-static data member* or *non-static member function*
|
| 486 |
+
[[class.mfct.non.static]], respectively).
|
| 487 |
|
| 488 |
[*Note 2*: A non-static data member of non-reference type is a member
|
| 489 |
subobject of a class object [[intro.object]]. — *end note*]
|
| 490 |
|
| 491 |
A member shall not be declared twice in the *member-specification*,
|
|
|
|
| 495 |
defined, and
|
| 496 |
- an enumeration can be introduced with an *opaque-enum-declaration* and
|
| 497 |
later redeclared with an *enum-specifier*.
|
| 498 |
|
| 499 |
[*Note 3*: A single name can denote several member functions provided
|
| 500 |
+
their types are sufficiently different
|
| 501 |
+
[[basic.scope.scope]]. — *end note*]
|
| 502 |
|
| 503 |
+
A redeclaration of a class member outside its class definition shall be
|
| 504 |
+
a definition, an explicit specialization, or an explicit instantiation
|
| 505 |
+
[[temp.expl.spec]], [[temp.explicit]]. The member shall not be a
|
| 506 |
+
non-static data member.
|
| 507 |
+
|
| 508 |
+
A *complete-class context* of a class (template) is a
|
| 509 |
|
| 510 |
- function body [[dcl.fct.def.general]],
|
| 511 |
- default argument [[dcl.fct.default]],
|
| 512 |
+
- default template argument [[temp.param]],
|
| 513 |
- *noexcept-specifier* [[except.spec]], or
|
| 514 |
- default member initializer
|
| 515 |
|
| 516 |
+
within the *member-specification* of the class or class template.
|
| 517 |
|
| 518 |
[*Note 4*: A complete-class context of a nested class is also a
|
| 519 |
complete-class context of any enclosing class, if the nested class is
|
| 520 |
defined within the *member-specification* of the enclosing
|
| 521 |
class. — *end note*]
|
| 522 |
|
| 523 |
+
A class is regarded as complete where its definition is reachable and
|
| 524 |
+
within its complete-class contexts; otherwise it is regarded as
|
| 525 |
+
incomplete within its own class *member-specification*.
|
|
|
|
|
|
|
| 526 |
|
| 527 |
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 528 |
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 529 |
has function type, otherwise it is interpreted as introducing a
|
| 530 |
*brace-or-equal-initializer*.
|
|
|
|
| 532 |
[*Example 1*:
|
| 533 |
|
| 534 |
``` cpp
|
| 535 |
struct S {
|
| 536 |
using T = void();
|
| 537 |
+
T * p = 0; // OK, brace-or-equal-initializer
|
| 538 |
+
virtual T f = 0; // OK, pure-specifier
|
| 539 |
};
|
| 540 |
```
|
| 541 |
|
| 542 |
— *end example*]
|
| 543 |
|
|
|
|
| 567 |
non-static data members, see [[class.base.init]] and
|
| 568 |
[[dcl.init.aggr]]). A *brace-or-equal-initializer* for a non-static data
|
| 569 |
member specifies a *default member initializer* for the member, and
|
| 570 |
shall not directly or indirectly cause the implicit definition of a
|
| 571 |
defaulted default constructor for the enclosing class or the exception
|
| 572 |
+
specification of that constructor. An immediate invocation
|
| 573 |
+
[[expr.const]] that is a potentially-evaluated subexpression
|
| 574 |
+
[[intro.execution]] of a default member initializer is neither evaluated
|
| 575 |
+
nor checked for whether it is a constant expression at the point where
|
| 576 |
+
the subexpression appears.
|
| 577 |
|
| 578 |
A member shall not be declared with the `extern`
|
| 579 |
*storage-class-specifier*. Within a class definition, a member shall not
|
| 580 |
be declared with the `thread_local` *storage-class-specifier* unless
|
| 581 |
also declared `static`.
|
|
|
|
| 596 |
A *virt-specifier-seq* shall contain at most one of each
|
| 597 |
*virt-specifier*. A *virt-specifier-seq* shall appear only in the first
|
| 598 |
declaration of a virtual member function [[class.virtual]].
|
| 599 |
|
| 600 |
The type of a non-static data member shall not be an incomplete type
|
| 601 |
+
[[term.incomplete.type]], an abstract class type [[class.abstract]], or
|
| 602 |
+
a (possibly multidimensional) array thereof.
|
| 603 |
|
| 604 |
[*Note 5*: In particular, a class `C` cannot contain a non-static
|
| 605 |
member of class `C`, but it can contain a pointer or reference to an
|
| 606 |
object of class `C`. — *end note*]
|
| 607 |
|
|
|
|
| 640 |
pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
| 641 |
character of the `tword` member of the `right` subtree of `s`.
|
| 642 |
|
| 643 |
— *end example*]
|
| 644 |
|
| 645 |
+
[*Note 8*: Non-variant non-static data members of non-zero size
|
| 646 |
+
[[intro.object]] are allocated so that later members have higher
|
| 647 |
+
addresses within a class object [[expr.rel]]. Implementation alignment
|
| 648 |
+
requirements can cause two adjacent members not to be allocated
|
| 649 |
+
immediately after each other; so can requirements for space for managing
|
| 650 |
+
virtual functions [[class.virtual]] and virtual base classes
|
|
|
|
|
|
|
| 651 |
[[class.mi]]. — *end note*]
|
| 652 |
|
| 653 |
If `T` is the name of a class, then each of the following shall have a
|
| 654 |
name different from `T`:
|
| 655 |
|
| 656 |
- every static data member of class `T`;
|
| 657 |
+
- every member function of class `T`; \[*Note 9*: This restriction does
|
| 658 |
not apply to constructors, which do not have names
|
| 659 |
+
[[class.ctor]] — *end note*]
|
| 660 |
- every member of class `T` that is itself a type;
|
| 661 |
- every member template of class `T`;
|
| 662 |
- every enumerator of every member of class `T` that is an unscoped
|
| 663 |
+
enumeration type; and
|
| 664 |
- every member of every anonymous union that is a member of class `T`.
|
| 665 |
|
| 666 |
In addition, if class `T` has a user-declared constructor
|
| 667 |
[[class.ctor]], every non-static data member of class `T` shall have a
|
| 668 |
name different from `T`.
|
| 669 |
|
| 670 |
The *common initial sequence* of two standard-layout struct
|
| 671 |
[[class.prop]] types is the longest sequence of non-static data members
|
| 672 |
and bit-fields in declaration order, starting with the first such entity
|
| 673 |
+
in each of the structs, such that
|
| 674 |
+
|
| 675 |
+
- corresponding entities have layout-compatible types [[basic.types]],
|
| 676 |
+
- corresponding entities have the same alignment requirements
|
| 677 |
+
[[basic.align]],
|
| 678 |
+
- either both entities are declared with the `no_unique_address`
|
| 679 |
+
attribute [[dcl.attr.nouniqueaddr]] or neither is, and
|
| 680 |
+
- either both entities are bit-fields with the same width or neither is
|
| 681 |
+
a bit-field.
|
| 682 |
|
| 683 |
[*Example 4*:
|
| 684 |
|
| 685 |
``` cpp
|
| 686 |
struct A { int a; char b; };
|
|
|
|
| 701 |
classes* if their common initial sequence comprises all members and
|
| 702 |
bit-fields of both classes [[basic.types]].
|
| 703 |
|
| 704 |
Two standard-layout unions are layout-compatible if they have the same
|
| 705 |
number of non-static data members and corresponding non-static data
|
| 706 |
+
members (in any order) have layout-compatible types
|
| 707 |
+
[[term.layout.compatible.type]].
|
| 708 |
|
| 709 |
In a standard-layout union with an active member [[class.union]] of
|
| 710 |
struct type `T1`, it is permitted to read a non-static data member `m`
|
| 711 |
of another union member of struct type `T2` provided `m` is part of the
|
| 712 |
common initial sequence of `T1` and `T2`; the behavior is as if the
|
|
|
|
| 732 |
If a standard-layout class object has any non-static data members, its
|
| 733 |
address is the same as the address of its first non-static data member
|
| 734 |
if that member is not a bit-field. Its address is also the same as the
|
| 735 |
address of each of its base class subobjects.
|
| 736 |
|
| 737 |
+
[*Note 11*: There can therefore be unnamed padding within a
|
| 738 |
standard-layout struct object inserted by an implementation, but not at
|
| 739 |
its beginning, as necessary to achieve appropriate
|
| 740 |
alignment. — *end note*]
|
| 741 |
|
| 742 |
[*Note 12*: The object and its first subobject are
|
| 743 |
+
pointer-interconvertible
|
| 744 |
+
[[basic.compound]], [[expr.static.cast]]. — *end note*]
|
| 745 |
|
| 746 |
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 747 |
|
| 748 |
+
If a member function is attached to the global module and is defined
|
| 749 |
+
[[dcl.fct.def]] in its class definition, it is inline [[dcl.inline]].
|
|
|
|
|
|
|
|
|
|
| 750 |
|
| 751 |
[*Note 1*: A member function is also inline if it is declared `inline`,
|
| 752 |
`constexpr`, or `consteval`. — *end note*]
|
| 753 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
[*Example 1*:
|
| 755 |
|
| 756 |
``` cpp
|
| 757 |
struct X {
|
| 758 |
typedef int T;
|
|
|
|
| 760 |
void f(T);
|
| 761 |
};
|
| 762 |
void X::f(T t = count) { }
|
| 763 |
```
|
| 764 |
|
| 765 |
+
The definition of the member function `f` of class `X` inhabits the
|
| 766 |
+
global scope; the notation `X::f` indicates that the function `f` is a
|
| 767 |
+
member of class `X` and in the scope of class `X`. In the function
|
| 768 |
+
definition, the parameter type `T` refers to the typedef member `T`
|
| 769 |
+
declared in class `X` and the default argument `count` refers to the
|
| 770 |
+
static data member `count` declared in class `X`.
|
| 771 |
|
| 772 |
— *end example*]
|
| 773 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 774 |
Member functions of a local class shall be defined inline in their class
|
| 775 |
definition, if they are defined at all.
|
| 776 |
|
| 777 |
+
[*Note 2*:
|
| 778 |
|
| 779 |
A member function can be declared (but not defined) using a typedef for
|
| 780 |
a function type. The resulting member function has exactly the same type
|
| 781 |
as it would have if the function declarator were provided explicitly,
|
| 782 |
see [[dcl.fct]]. For example,
|
|
|
|
| 796 |
|
| 797 |
Also see [[temp.arg]].
|
| 798 |
|
| 799 |
— *end note*]
|
| 800 |
|
| 801 |
+
### Non-static member functions <a id="class.mfct.non.static">[[class.mfct.non.static]]</a>
|
| 802 |
|
| 803 |
A non-static member function may be called for an object of its class
|
| 804 |
type, or for an object of a class derived [[class.derived]] from its
|
| 805 |
+
class type, using the class member access syntax
|
| 806 |
+
[[expr.ref]], [[over.match.call]]. A non-static member function may also
|
| 807 |
+
be called directly using the function call syntax
|
| 808 |
+
[[expr.call]], [[over.match.call]] from within its class or a class
|
| 809 |
+
derived from its class, or a member thereof, as described below.
|
| 810 |
|
| 811 |
+
When an *id-expression* [[expr.prim.id]] that is neither part of a class
|
| 812 |
+
member access syntax [[expr.ref]] nor the unparenthesized operand of the
|
| 813 |
+
unary `&` operator [[expr.unary.op]] is used where the current class is
|
| 814 |
+
`X` [[expr.prim.this]], if name lookup [[basic.lookup]] resolves the
|
| 815 |
+
name in the *id-expression* to a non-static non-type member of some
|
| 816 |
+
class `C`, and if either the *id-expression* is potentially evaluated or
|
| 817 |
+
`C` is `X` or a base class of `X`, the *id-expression* is transformed
|
| 818 |
+
into a class member access expression [[expr.ref]] using `(*this)` as
|
| 819 |
+
the *postfix-expression* to the left of the `.` operator.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 820 |
|
| 821 |
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 822 |
access expression is ill-formed. — *end note*]
|
| 823 |
|
| 824 |
This transformation does not apply in the template definition context
|
|
|
|
| 857 |
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 858 |
not members of the class `tnode` and should be declared elsewhere.[^2]
|
| 859 |
|
| 860 |
— *end example*]
|
| 861 |
|
| 862 |
+
[*Note 2*: An implicit object member function can be declared with
|
| 863 |
+
*cv-qualifier*s, which affect the type of the `this` pointer
|
| 864 |
+
[[expr.prim.this]], and/or a *ref-qualifier* [[dcl.fct]]; both affect
|
| 865 |
+
overload resolution [[over.match.funcs]] — *end note*]
|
|
|
|
|
|
|
|
|
|
| 866 |
|
| 867 |
+
An implicit object member function may be declared virtual
|
| 868 |
+
[[class.virtual]] or pure virtual [[class.abstract]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 869 |
|
| 870 |
### Special member functions <a id="special">[[special]]</a>
|
| 871 |
|
| 872 |
Default constructors [[class.default.ctor]], copy constructors, move
|
| 873 |
constructors [[class.copy.ctor]], copy assignment operators, move
|
| 874 |
assignment operators [[class.copy.assign]], and prospective destructors
|
| 875 |
[[class.dtor]] are *special member functions*.
|
| 876 |
|
| 877 |
[*Note 1*: The implementation will implicitly declare these member
|
| 878 |
functions for some class types when the program does not explicitly
|
| 879 |
+
declare them. The implementation will implicitly define them as needed
|
| 880 |
+
[[dcl.fct.def.default]]. — *end note*]
|
|
|
|
| 881 |
|
| 882 |
An implicitly-declared special member function is declared at the
|
| 883 |
closing `}` of the *class-specifier*. Programs shall not define
|
| 884 |
implicitly-declared special member functions.
|
| 885 |
|
|
|
|
| 935 |
For a class, its non-static data members, its non-virtual direct base
|
| 936 |
classes, and, if the class is not abstract [[class.abstract]], its
|
| 937 |
virtual base classes are called its *potentially constructed
|
| 938 |
subobjects*.
|
| 939 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 940 |
### Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 941 |
|
| 942 |
+
#### General <a id="class.ctor.general">[[class.ctor.general]]</a>
|
| 943 |
+
|
| 944 |
+
A *declarator* declares a *constructor* if it is a function declarator
|
| 945 |
+
[[dcl.fct]] of the form
|
| 946 |
|
| 947 |
``` bnf
|
| 948 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 949 |
```
|
| 950 |
|
| 951 |
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 952 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 953 |
parentheses, and the *id-expression* has one of the following forms:
|
| 954 |
|
| 955 |
+
- in a friend declaration [[class.friend]], the *id-expression* is a
|
| 956 |
+
*qualified-id* that names a constructor [[class.qual]];
|
| 957 |
+
- otherwise, in a *member-declaration* that belongs to the
|
| 958 |
+
*member-specification* of a class or class template, the
|
| 959 |
+
*id-expression* is the injected-class-name [[class.pre]] of the
|
| 960 |
+
immediately-enclosing entity;
|
| 961 |
+
- otherwise, the *id-expression* is a *qualified-id* whose
|
| 962 |
+
*unqualified-id* is the injected-class-name of its lookup context.
|
| 963 |
|
| 964 |
Constructors do not have names. In a constructor declaration, each
|
| 965 |
*decl-specifier* in the optional *decl-specifier-seq* shall be `friend`,
|
| 966 |
+
`inline`, `constexpr`, `consteval`, or an *explicit-specifier*.
|
| 967 |
|
| 968 |
[*Example 1*:
|
| 969 |
|
| 970 |
``` cpp
|
| 971 |
struct S {
|
|
|
|
| 975 |
S::S() { } // defines the constructor
|
| 976 |
```
|
| 977 |
|
| 978 |
— *end example*]
|
| 979 |
|
| 980 |
+
A constructor is used to initialize objects of its class type.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 981 |
|
| 982 |
+
[*Note 1*: Because constructors do not have names, they are never found
|
| 983 |
+
during unqualified name lookup; however an explicit type conversion
|
| 984 |
+
using the functional notation [[expr.type.conv]] will cause a
|
| 985 |
+
constructor to be called to initialize an object. The syntax looks like
|
| 986 |
+
an explicit call of the constructor. — *end note*]
|
| 987 |
|
| 988 |
[*Example 2*:
|
| 989 |
|
| 990 |
``` cpp
|
| 991 |
complex zz = complex(1,2.3);
|
|
|
|
| 1012 |
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 1013 |
`volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
|
| 1014 |
not applied on an object under construction. They come into effect when
|
| 1015 |
the constructor for the most derived object [[intro.object]] ends.
|
| 1016 |
|
| 1017 |
+
The address of a constructor shall not be taken.
|
| 1018 |
+
|
| 1019 |
+
[*Note 6*: A `return` statement in the body of a constructor cannot
|
| 1020 |
+
specify a return value [[stmt.return]]. — *end note*]
|
| 1021 |
|
| 1022 |
A constructor shall not be a coroutine.
|
| 1023 |
|
| 1024 |
+
A constructor shall not have an explicit object parameter [[dcl.fct]].
|
| 1025 |
+
|
| 1026 |
#### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
|
| 1027 |
|
| 1028 |
A *default constructor* for a class `X` is a constructor of class `X`
|
| 1029 |
for which each parameter that is not a function parameter pack has a
|
| 1030 |
default argument (including the case of a constructor with no
|
|
|
|
| 1072 |
type (or array thereof), each such class has a trivial default
|
| 1073 |
constructor.
|
| 1074 |
|
| 1075 |
Otherwise, the default constructor is *non-trivial*.
|
| 1076 |
|
| 1077 |
+
An implicitly-defined [[dcl.fct.def.default]] default constructor
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1078 |
performs the set of initializations of the class that would be performed
|
| 1079 |
by a user-written default constructor for that class with no
|
| 1080 |
*ctor-initializer* [[class.base.init]] and an empty
|
| 1081 |
*compound-statement*. If that user-written default constructor would be
|
| 1082 |
ill-formed, the program is ill-formed. If that user-written default
|
| 1083 |
+
constructor would be constexpr-suitable [[dcl.constexpr]], the
|
| 1084 |
+
implicitly-defined default constructor is `constexpr`. Before the
|
| 1085 |
+
defaulted default constructor for a class is implicitly defined, all the
|
| 1086 |
+
non-user-provided default constructors for its base classes and its
|
| 1087 |
+
non-static data members are implicitly defined.
|
| 1088 |
|
| 1089 |
[*Note 1*: An implicitly-declared default constructor has an exception
|
| 1090 |
specification [[except.spec]]. An explicitly-defaulted definition might
|
| 1091 |
have an implicit exception specification, see
|
| 1092 |
[[dcl.fct.def]]. — *end note*]
|
| 1093 |
|
| 1094 |
+
[*Note 2*: A default constructor is implicitly invoked to initialize a
|
| 1095 |
+
class object when no initializer is specified [[dcl.init.general]]. Such
|
| 1096 |
+
a default constructor is required to be accessible
|
| 1097 |
+
[[class.access]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1098 |
|
| 1099 |
+
[*Note 3*: [[class.base.init]] describes the order in which
|
| 1100 |
constructors for base classes and non-static data members are called and
|
| 1101 |
describes how arguments can be specified for the calls to these
|
| 1102 |
constructors. — *end note*]
|
| 1103 |
|
| 1104 |
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
|
|
|
| 1147 |
|
| 1148 |
— *end example*]
|
| 1149 |
|
| 1150 |
[*Note 1*:
|
| 1151 |
|
| 1152 |
+
All forms of copy/move constructor can be declared for a class.
|
| 1153 |
|
| 1154 |
[*Example 3*:
|
| 1155 |
|
| 1156 |
``` cpp
|
| 1157 |
struct X {
|
|
|
|
| 1213 |
|
| 1214 |
If the class definition does not explicitly declare a copy constructor,
|
| 1215 |
a non-explicit one is declared *implicitly*. If the class definition
|
| 1216 |
declares a move constructor or move assignment operator, the implicitly
|
| 1217 |
declared copy constructor is defined as deleted; otherwise, it is
|
| 1218 |
+
defaulted [[dcl.fct.def]]. The latter case is deprecated if the class
|
| 1219 |
+
has a user-declared copy assignment operator or a user-declared
|
| 1220 |
+
destructor [[depr.impldec]].
|
| 1221 |
|
| 1222 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 1223 |
form
|
| 1224 |
|
| 1225 |
``` cpp
|
| 1226 |
X::X(const X&)
|
| 1227 |
```
|
| 1228 |
|
| 1229 |
if each potentially constructed subobject of a class type `M` (or array
|
| 1230 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 1231 |
+
`M&` or `const` `volatile` `M&`.[^3]
|
| 1232 |
+
|
| 1233 |
+
Otherwise, the implicitly-declared copy constructor will have the form
|
| 1234 |
|
| 1235 |
``` cpp
|
| 1236 |
X::X(X&)
|
| 1237 |
```
|
| 1238 |
|
|
|
|
| 1245 |
- `X` does not have a user-declared move assignment operator, and
|
| 1246 |
- `X` does not have a user-declared destructor.
|
| 1247 |
|
| 1248 |
[*Note 3*: When the move constructor is not implicitly declared or
|
| 1249 |
explicitly supplied, expressions that otherwise would have invoked the
|
| 1250 |
+
move constructor might instead invoke a copy constructor. — *end note*]
|
| 1251 |
|
| 1252 |
The implicitly-declared move constructor for class `X` will have the
|
| 1253 |
form
|
| 1254 |
|
| 1255 |
``` cpp
|
|
|
|
| 1258 |
|
| 1259 |
An implicitly-declared copy/move constructor is an inline public member
|
| 1260 |
of its class. A defaulted copy/move constructor for a class `X` is
|
| 1261 |
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 1262 |
|
| 1263 |
+
- a potentially constructed subobject of type `M` (or array thereof)
|
| 1264 |
+
that cannot be copied/moved because overload resolution
|
| 1265 |
+
[[over.match]], as applied to find `M`’s corresponding constructor,
|
| 1266 |
+
results in an ambiguity or a function that is deleted or inaccessible
|
| 1267 |
+
from the defaulted constructor,
|
| 1268 |
- a variant member whose corresponding constructor as selected by
|
| 1269 |
overload resolution is non-trivial,
|
| 1270 |
- any potentially constructed subobject of a type with a destructor that
|
| 1271 |
is deleted or inaccessible from the defaulted constructor, or,
|
| 1272 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 1273 |
type.
|
| 1274 |
|
| 1275 |
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 1276 |
+
ignored by overload resolution [[over.match]], [[over.over]]. Such a
|
| 1277 |
constructor would otherwise interfere with initialization from an rvalue
|
| 1278 |
which can use the copy constructor instead. — *end note*]
|
| 1279 |
|
| 1280 |
A copy/move constructor for class `X` is trivial if it is not
|
| 1281 |
user-provided and if:
|
|
|
|
| 1288 |
thereof), the constructor selected to copy/move that member is
|
| 1289 |
trivial;
|
| 1290 |
|
| 1291 |
otherwise the copy/move constructor is *non-trivial*.
|
| 1292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1293 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 1294 |
+
implementation elided its odr-use
|
| 1295 |
+
[[term.odr.use]], [[class.temporary]]. — *end note*]
|
| 1296 |
|
| 1297 |
+
If an implicitly-defined [[dcl.fct.def.default]] constructor would be
|
| 1298 |
+
constexpr-suitable [[dcl.constexpr]], the implicitly-defined constructor
|
| 1299 |
+
is `constexpr`.
|
| 1300 |
|
| 1301 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 1302 |
defined, all non-user-provided copy/move constructors for its
|
| 1303 |
potentially constructed subobjects are implicitly defined.
|
| 1304 |
|
|
|
|
| 1327 |
|
| 1328 |
Virtual base class subobjects shall be initialized only once by the
|
| 1329 |
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 1330 |
|
| 1331 |
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 1332 |
+
object representation [[term.object.representation]] of `X`. For each
|
| 1333 |
+
object nested within [[intro.object]] the object that is the source of
|
| 1334 |
+
the copy, a corresponding object o nested within the destination is
|
| 1335 |
+
identified (if the object is a subobject) or created (otherwise), and
|
| 1336 |
+
the lifetime of o begins before the copy is performed.
|
| 1337 |
|
| 1338 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 1339 |
|
| 1340 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 1341 |
non-static non-template member function of class `X` with exactly one
|
| 1342 |
+
non-object parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
|
| 1343 |
`const volatile X&`.[^4]
|
| 1344 |
|
| 1345 |
[*Note 1*: An overloaded assignment operator must be declared to have
|
| 1346 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 1347 |
|
| 1348 |
+
[*Note 2*: More than one form of copy assignment operator can be
|
| 1349 |
declared for a class. — *end note*]
|
| 1350 |
|
| 1351 |
[*Note 3*:
|
| 1352 |
|
| 1353 |
If a class `X` only has a copy assignment operator with a parameter of
|
|
|
|
| 1373 |
— *end note*]
|
| 1374 |
|
| 1375 |
If the class definition does not explicitly declare a copy assignment
|
| 1376 |
operator, one is declared *implicitly*. If the class definition declares
|
| 1377 |
a move constructor or move assignment operator, the implicitly declared
|
| 1378 |
+
copy assignment operator is defined as deleted; otherwise, it is
|
| 1379 |
+
defaulted [[dcl.fct.def]]. The latter case is deprecated if the class
|
| 1380 |
has a user-declared copy constructor or a user-declared destructor
|
| 1381 |
[[depr.impldec]]. The implicitly-declared copy assignment operator for a
|
| 1382 |
class `X` will have the form
|
| 1383 |
|
| 1384 |
``` cpp
|
|
|
|
| 1400 |
``` cpp
|
| 1401 |
X& X::operator=(X&)
|
| 1402 |
```
|
| 1403 |
|
| 1404 |
A user-declared move assignment operator `X::operator=` is a non-static
|
| 1405 |
+
non-template member function of class `X` with exactly one non-object
|
| 1406 |
+
parameter of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 1407 |
+
`const volatile X&&`.
|
| 1408 |
|
| 1409 |
[*Note 4*: An overloaded assignment operator must be declared to have
|
| 1410 |
only one parameter; see [[over.ass]]. — *end note*]
|
| 1411 |
|
| 1412 |
+
[*Note 5*: More than one form of move assignment operator can be
|
| 1413 |
declared for a class. — *end note*]
|
| 1414 |
|
| 1415 |
If the definition of a class `X` does not explicitly declare a move
|
| 1416 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1417 |
only if
|
|
|
|
| 1452 |
``` cpp
|
| 1453 |
X& X::operator=(X&&)
|
| 1454 |
```
|
| 1455 |
|
| 1456 |
The implicitly-declared copy/move assignment operator for class `X` has
|
| 1457 |
+
the return type `X&`. An implicitly-declared copy/move assignment
|
| 1458 |
+
operator is an inline public member of its class.
|
|
|
|
|
|
|
| 1459 |
|
| 1460 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 1461 |
deleted if `X` has:
|
| 1462 |
|
| 1463 |
- a variant member with a non-trivial corresponding assignment operator
|
|
|
|
| 1471 |
corresponding assignment operator, results in an ambiguity or a
|
| 1472 |
function that is deleted or inaccessible from the defaulted assignment
|
| 1473 |
operator.
|
| 1474 |
|
| 1475 |
[*Note 6*: A defaulted move assignment operator that is defined as
|
| 1476 |
+
deleted is ignored by overload resolution
|
| 1477 |
+
[[over.match]], [[over.over]]. — *end note*]
|
| 1478 |
|
| 1479 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1480 |
class if not declared by the user, a base class copy/move assignment
|
| 1481 |
operator is always hidden by the corresponding assignment operator of a
|
| 1482 |
+
derived class [[over.ass]].
|
| 1483 |
+
|
| 1484 |
+
[*Note 7*: A *using-declaration* in a derived class `C` that names an
|
| 1485 |
+
assignment operator from a base class never suppresses the implicit
|
| 1486 |
+
declaration of an assignment operator of `C`, even if the base class
|
| 1487 |
+
assignment operator would be a copy or move assignment operator if
|
| 1488 |
+
declared as a member of `C`. — *end note*]
|
| 1489 |
|
| 1490 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1491 |
user-provided and if:
|
| 1492 |
|
| 1493 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
|
|
|
| 1498 |
thereof), the assignment operator selected to copy/move that member is
|
| 1499 |
trivial;
|
| 1500 |
|
| 1501 |
otherwise the copy/move assignment operator is *non-trivial*.
|
| 1502 |
|
| 1503 |
+
An implicitly-defined [[dcl.fct.def.default]] copy/move assignment
|
| 1504 |
+
operator is `constexpr`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1505 |
|
| 1506 |
Before the defaulted copy/move assignment operator for a class is
|
| 1507 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1508 |
for its direct base classes and its non-static data members are
|
| 1509 |
implicitly defined.
|
| 1510 |
|
| 1511 |
+
[*Note 8*: An implicitly-declared copy/move assignment operator has an
|
| 1512 |
implied exception specification [[except.spec]]. — *end note*]
|
| 1513 |
|
| 1514 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 1515 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 1516 |
The direct base classes of `X` are assigned first, in the order of their
|
|
|
|
| 1548 |
assigned twice by the implicitly-defined copy/move assignment operator
|
| 1549 |
for `C`.
|
| 1550 |
|
| 1551 |
— *end example*]
|
| 1552 |
|
| 1553 |
+
The implicitly-defined copy/move assignment operator for a union `X`
|
| 1554 |
+
copies the object representation [[term.object.representation]] of `X`.
|
| 1555 |
+
If the source and destination of the assignment are not the same object,
|
| 1556 |
+
then for each object nested within [[intro.object]] the object that is
|
| 1557 |
+
the source of the copy, a corresponding object o nested within the
|
| 1558 |
+
destination is created, and the lifetime of o begins before the copy is
|
| 1559 |
+
performed.
|
| 1560 |
+
|
| 1561 |
+
The implicitly-defined copy/move assignment operator for a class returns
|
| 1562 |
+
the object for which the assignment operator is invoked, that is, the
|
| 1563 |
+
object assigned to.
|
| 1564 |
|
| 1565 |
### Destructors <a id="class.dtor">[[class.dtor]]</a>
|
| 1566 |
|
| 1567 |
+
A declaration whose *declarator-id* has an *unqualified-id* that begins
|
| 1568 |
+
with a `~` declares a *prospective destructor*; its *declarator* shall
|
| 1569 |
+
be a function declarator [[dcl.fct]] of the form
|
| 1570 |
|
| 1571 |
``` bnf
|
| 1572 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1573 |
```
|
| 1574 |
|
|
|
|
| 1579 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 1580 |
of a class or class template but is not a friend declaration
|
| 1581 |
[[class.friend]], the *id-expression* is `~`*class-name* and the
|
| 1582 |
*class-name* is the injected-class-name [[class.pre]] of the
|
| 1583 |
immediately-enclosing entity or
|
| 1584 |
+
- otherwise, the *id-expression* is *nested-name-specifier*
|
| 1585 |
+
`~`*class-name* and the *class-name* is the injected-class-name of the
|
| 1586 |
+
class nominated by the *nested-name-specifier*.
|
| 1587 |
|
| 1588 |
A prospective destructor shall take no arguments [[dcl.fct]]. Each
|
| 1589 |
*decl-specifier* of the *decl-specifier-seq* of a prospective destructor
|
| 1590 |
declaration (if any) shall be `friend`, `inline`, `virtual`,
|
| 1591 |
`constexpr`, or `consteval`.
|
|
|
|
| 1605 |
At the end of the definition of a class, overload resolution is
|
| 1606 |
performed among the prospective destructors declared in that class with
|
| 1607 |
an empty argument list to select the *destructor* for the class, also
|
| 1608 |
known as the *selected destructor*. The program is ill-formed if
|
| 1609 |
overload resolution fails. Destructor selection does not constitute a
|
| 1610 |
+
reference to, or odr-use [[term.odr.use]] of, the selected destructor,
|
| 1611 |
and in particular, the selected destructor may be deleted
|
| 1612 |
[[dcl.fct.def.delete]].
|
| 1613 |
|
| 1614 |
+
The address of a destructor shall not be taken.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1615 |
|
| 1616 |
+
[*Note 1*: A `return` statement in the body of a destructor cannot
|
| 1617 |
+
specify a return value [[stmt.return]]. — *end note*]
|
| 1618 |
+
|
| 1619 |
+
A destructor can be invoked for a `const`, `volatile` or `const`
|
| 1620 |
+
`volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
|
| 1621 |
+
not applied on an object under destruction. They stop being in effect
|
| 1622 |
+
when the destructor for the most derived object [[intro.object]] starts.
|
| 1623 |
+
|
| 1624 |
+
[*Note 2*: A declaration of a destructor that does not have a
|
| 1625 |
*noexcept-specifier* has the same exception specification as if it had
|
| 1626 |
been implicitly declared [[except.spec]]. — *end note*]
|
| 1627 |
|
| 1628 |
A defaulted destructor for a class `X` is defined as deleted if:
|
| 1629 |
|
|
|
|
| 1636 |
function results in an ambiguity or in a function that is deleted or
|
| 1637 |
inaccessible from the defaulted destructor.
|
| 1638 |
|
| 1639 |
A destructor is trivial if it is not user-provided and if:
|
| 1640 |
|
| 1641 |
+
- the destructor is not virtual,
|
| 1642 |
- all of the direct base classes of its class have trivial destructors,
|
| 1643 |
and
|
| 1644 |
- for all of the non-static data members of its class that are of class
|
| 1645 |
type (or array thereof), each such class has a trivial destructor.
|
| 1646 |
|
| 1647 |
Otherwise, the destructor is *non-trivial*.
|
| 1648 |
|
| 1649 |
+
A defaulted destructor is a constexpr destructor if it is
|
| 1650 |
+
constexpr-suitable [[dcl.constexpr]].
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1651 |
|
| 1652 |
Before a defaulted destructor for a class is implicitly defined, all the
|
| 1653 |
non-user-provided destructors for its base classes and its non-static
|
| 1654 |
data members are implicitly defined.
|
| 1655 |
|
| 1656 |
+
A prospective destructor can be declared `virtual` [[class.virtual]] and
|
| 1657 |
+
with a *pure-specifier* [[class.abstract]]. If the destructor of a class
|
| 1658 |
+
is virtual and any objects of that class or any derived class are
|
| 1659 |
+
created in the program, the destructor shall be defined.
|
|
|
|
|
|
|
| 1660 |
|
| 1661 |
+
[*Note 3*: Some language constructs have special semantics when used
|
| 1662 |
during destruction; see [[class.cdtor]]. — *end note*]
|
| 1663 |
|
| 1664 |
After executing the body of the destructor and destroying any objects
|
| 1665 |
with automatic storage duration allocated within the body, a destructor
|
| 1666 |
for class `X` calls the destructors for `X`’s direct non-variant
|
|
|
|
| 1668 |
base classes and, if `X` is the most derived class [[class.base.init]],
|
| 1669 |
its destructor calls the destructors for `X`’s virtual base classes. All
|
| 1670 |
destructors are called as if they were referenced with a qualified name,
|
| 1671 |
that is, ignoring any possible virtual overriding destructors in more
|
| 1672 |
derived classes. Bases and members are destroyed in the reverse order of
|
| 1673 |
+
the completion of their constructor (see [[class.base.init]]).
|
| 1674 |
+
|
| 1675 |
+
[*Note 4*: A `return` statement [[stmt.return]] in a destructor might
|
| 1676 |
+
not directly return to the caller; before transferring control to the
|
| 1677 |
+
caller, the destructors for the members and bases are
|
| 1678 |
+
called. — *end note*]
|
| 1679 |
+
|
| 1680 |
+
Destructors for elements of an array are called in reverse order of
|
| 1681 |
+
their construction (see [[class.init]]).
|
| 1682 |
|
| 1683 |
A destructor is invoked implicitly
|
| 1684 |
|
| 1685 |
- for a constructed object with static storage duration
|
| 1686 |
[[basic.stc.static]] at program termination [[basic.start.term]],
|
| 1687 |
- for a constructed object with thread storage duration
|
| 1688 |
[[basic.stc.thread]] at thread exit,
|
| 1689 |
- for a constructed object with automatic storage duration
|
| 1690 |
[[basic.stc.auto]] when the block in which an object is created exits
|
| 1691 |
[[stmt.dcl]],
|
| 1692 |
+
- for a constructed temporary object when its lifetime ends
|
| 1693 |
+
[[conv.rval]], [[class.temporary]].
|
| 1694 |
|
| 1695 |
In each case, the context of the invocation is the context of the
|
| 1696 |
construction of the object. A destructor may also be invoked implicitly
|
| 1697 |
through use of a *delete-expression* [[expr.delete]] for a constructed
|
| 1698 |
object allocated by a *new-expression* [[expr.new]]; the context of the
|
| 1699 |
invocation is the *delete-expression*.
|
| 1700 |
|
| 1701 |
+
[*Note 5*: An array of class type contains several subobjects for each
|
| 1702 |
of which the destructor is invoked. — *end note*]
|
| 1703 |
|
| 1704 |
A destructor can also be invoked explicitly. A destructor is
|
| 1705 |
*potentially invoked* if it is invoked or as specified in [[expr.new]],
|
| 1706 |
[[stmt.return]], [[dcl.init.aggr]], [[class.base.init]], and
|
| 1707 |
[[except.throw]]. A program is ill-formed if a destructor that is
|
| 1708 |
potentially invoked is deleted or not accessible from the context of the
|
| 1709 |
invocation.
|
| 1710 |
|
| 1711 |
At the point of definition of a virtual destructor (including an
|
| 1712 |
+
implicit definition), the non-array deallocation function is determined
|
| 1713 |
+
as if for the expression `delete this` appearing in a non-virtual
|
| 1714 |
+
destructor of the destructor’s class (see [[expr.delete]]). If the
|
| 1715 |
+
lookup fails or if the deallocation function has a deleted definition
|
| 1716 |
+
[[dcl.fct.def]], the program is ill-formed.
|
| 1717 |
|
| 1718 |
+
[*Note 6*: This assures that a deallocation function corresponding to
|
| 1719 |
the dynamic type of an object is available for the *delete-expression*
|
| 1720 |
[[class.free]]. — *end note*]
|
| 1721 |
|
| 1722 |
In an explicit destructor call, the destructor is specified by a `~`
|
| 1723 |
followed by a *type-name* or *decltype-specifier* that denotes the
|
|
|
|
| 1725 |
the usual rules for member functions [[class.mfct]]; that is, if the
|
| 1726 |
object is not of the destructor’s class type and not of a class derived
|
| 1727 |
from the destructor’s class type (including when the destructor is
|
| 1728 |
invoked via a null pointer value), the program has undefined behavior.
|
| 1729 |
|
| 1730 |
+
[*Note 7*: Invoking `delete` on a null pointer does not call the
|
| 1731 |
destructor; see [[expr.delete]]. — *end note*]
|
| 1732 |
|
| 1733 |
[*Example 1*:
|
| 1734 |
|
| 1735 |
``` cpp
|
|
|
|
| 1753 |
}
|
| 1754 |
```
|
| 1755 |
|
| 1756 |
— *end example*]
|
| 1757 |
|
| 1758 |
+
[*Note 8*: An explicit destructor call must always be written using a
|
| 1759 |
member access operator [[expr.ref]] or a *qualified-id*
|
| 1760 |
[[expr.prim.id.qual]]; in particular, the *unary-expression* `~X()` in a
|
| 1761 |
member function is not an explicit destructor call
|
| 1762 |
[[expr.unary.op]]. — *end note*]
|
| 1763 |
|
| 1764 |
+
[*Note 9*:
|
| 1765 |
|
| 1766 |
Explicit calls of destructors are rarely needed. One use of such calls
|
| 1767 |
is for objects placed at specific addresses using a placement
|
| 1768 |
*new-expression*. Such use of explicit placement and destruction of
|
| 1769 |
objects can be necessary to cope with dedicated hardware resources and
|
|
|
|
| 1785 |
}
|
| 1786 |
```
|
| 1787 |
|
| 1788 |
— *end note*]
|
| 1789 |
|
| 1790 |
+
Once a destructor is invoked for an object, the object’s lifetime ends;
|
| 1791 |
the behavior is undefined if the destructor is invoked for an object
|
| 1792 |
whose lifetime has ended [[basic.life]].
|
| 1793 |
|
| 1794 |
[*Example 2*: If the destructor for an object with automatic storage
|
| 1795 |
duration is explicitly invoked, and the block is subsequently left in a
|
| 1796 |
manner that would ordinarily invoke implicit destruction of the object,
|
| 1797 |
the behavior is undefined. — *end example*]
|
| 1798 |
|
| 1799 |
+
[*Note 10*:
|
| 1800 |
|
| 1801 |
The notation for explicit call of a destructor can be used for any
|
| 1802 |
scalar type name [[expr.prim.id.dtor]]. Allowing this makes it possible
|
| 1803 |
to write code without having to know if a destructor exists for a given
|
| 1804 |
type. For example:
|
|
|
|
| 1813 |
|
| 1814 |
A destructor shall not be a coroutine.
|
| 1815 |
|
| 1816 |
### Conversions <a id="class.conv">[[class.conv]]</a>
|
| 1817 |
|
| 1818 |
+
#### General <a id="class.conv.general">[[class.conv.general]]</a>
|
| 1819 |
+
|
| 1820 |
Type conversions of class objects can be specified by constructors and
|
| 1821 |
by conversion functions. These conversions are called *user-defined
|
| 1822 |
conversions* and are used for implicit type conversions [[conv]], for
|
| 1823 |
+
initialization [[dcl.init]], and for explicit type conversions
|
| 1824 |
+
[[expr.type.conv]], [[expr.cast]], [[expr.static.cast]].
|
| 1825 |
|
| 1826 |
+
User-defined conversions are applied only where they are unambiguous
|
| 1827 |
+
[[class.member.lookup]], [[class.conv.fct]]. Conversions obey the access
|
| 1828 |
+
control rules [[class.access]]. Access control is applied after
|
| 1829 |
ambiguity resolution [[basic.lookup]].
|
| 1830 |
|
| 1831 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 1832 |
conversions in function calls as well as examples below. — *end note*]
|
| 1833 |
|
|
|
|
| 1845 |
operator X();
|
| 1846 |
};
|
| 1847 |
|
| 1848 |
Y a;
|
| 1849 |
int b = a; // error: no viable conversion (a.operator X().operator int() not considered)
|
| 1850 |
+
int c = X(a); // OK, a.operator X().operator int()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1851 |
```
|
| 1852 |
|
| 1853 |
— *end example*]
|
| 1854 |
|
| 1855 |
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
|
|
|
| 1880 |
|
| 1881 |
[*Note 1*:
|
| 1882 |
|
| 1883 |
An explicit constructor constructs objects just like non-explicit
|
| 1884 |
constructors, but does so only where the direct-initialization syntax
|
| 1885 |
+
[[dcl.init]] or where casts [[expr.static.cast]], [[expr.cast]] are
|
| 1886 |
explicitly used; see also [[over.match.copy]]. A default constructor
|
| 1887 |
+
can be an explicit constructor; such a constructor will be used to
|
| 1888 |
perform default-initialization or value-initialization [[dcl.init]].
|
| 1889 |
|
| 1890 |
[*Example 2*:
|
| 1891 |
|
| 1892 |
``` cpp
|
|
|
|
| 1894 |
explicit Z();
|
| 1895 |
explicit Z(int);
|
| 1896 |
explicit Z(int, int);
|
| 1897 |
};
|
| 1898 |
|
| 1899 |
+
Z a; // OK, default-initialization performed
|
| 1900 |
+
Z b{}; // OK, direct initialization syntax used
|
| 1901 |
Z c = {}; // error: copy-list-initialization
|
| 1902 |
Z a1 = 1; // error: no implicit conversion
|
| 1903 |
+
Z a3 = Z(1); // OK, direct initialization syntax used
|
| 1904 |
+
Z a2(1); // OK, direct initialization syntax used
|
| 1905 |
+
Z* p = new Z(1); // OK, direct initialization syntax used
|
| 1906 |
+
Z a4 = (Z)1; // OK, explicit cast used
|
| 1907 |
+
Z a5 = static_cast<Z>(1); // OK, explicit cast used
|
| 1908 |
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 1909 |
```
|
| 1910 |
|
| 1911 |
— *end example*]
|
| 1912 |
|
|
|
|
| 1914 |
|
| 1915 |
A non-explicit copy/move constructor [[class.copy.ctor]] is a converting
|
| 1916 |
constructor.
|
| 1917 |
|
| 1918 |
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 1919 |
+
explicit constructor; it can be called for implicit type
|
| 1920 |
conversions. — *end note*]
|
| 1921 |
|
| 1922 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 1923 |
|
|
|
|
|
|
|
|
|
|
| 1924 |
``` bnf
|
| 1925 |
conversion-function-id:
|
| 1926 |
operator conversion-type-id
|
| 1927 |
```
|
| 1928 |
|
|
|
|
| 1934 |
``` bnf
|
| 1935 |
conversion-declarator:
|
| 1936 |
ptr-operator conversion-declaratorₒₚₜ
|
| 1937 |
```
|
| 1938 |
|
| 1939 |
+
A declaration whose *declarator-id* has an *unqualified-id* that is a
|
| 1940 |
+
*conversion-function-id* declares a *conversion function*; its
|
| 1941 |
+
*declarator* shall be a function declarator [[dcl.fct]] of the form
|
| 1942 |
+
|
| 1943 |
+
``` bnf
|
| 1944 |
+
ptr-declarator '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 1945 |
+
ref-qualifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1946 |
+
```
|
| 1947 |
+
|
| 1948 |
+
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 1949 |
+
optional *attribute-specifier-seq*, and optional surrounding
|
| 1950 |
+
parentheses, and the *id-expression* has one of the following forms:
|
| 1951 |
+
|
| 1952 |
+
- in a *member-declaration* that belongs to the *member-specification*
|
| 1953 |
+
of a class or class template but is not a friend declaration
|
| 1954 |
+
[[class.friend]], the *id-expression* is a *conversion-function-id*;
|
| 1955 |
+
- otherwise, the *id-expression* is a *qualified-id* whose
|
| 1956 |
+
*unqualified-id* is a *conversion-function-id*.
|
| 1957 |
+
|
| 1958 |
+
A conversion function shall have no non-object parameters and shall be a
|
| 1959 |
+
non-static member function of a class or class template `X`; it
|
| 1960 |
specifies a conversion from `X` to the type specified by the
|
| 1961 |
+
*conversion-type-id*, interpreted as a *type-id* [[dcl.name]]. A
|
| 1962 |
+
*decl-specifier* in the *decl-specifier-seq* of a conversion function
|
| 1963 |
+
(if any) shall not be a *defining-type-specifier*.
|
| 1964 |
+
|
| 1965 |
+
The type of the conversion function is “`noexcept`ₒₚₜ function taking
|
| 1966 |
+
no parameter *cv-qualifier-seq*ₒₚₜ *ref-qualifier*ₒₚₜ returning
|
| 1967 |
+
*conversion-type-id*”.
|
| 1968 |
+
|
| 1969 |
+
A conversion function is never used to convert a (possibly cv-qualified)
|
| 1970 |
+
object to the (possibly cv-qualified) same object type (or a reference
|
| 1971 |
+
to it), to a (possibly cv-qualified) base class of that type (or a
|
| 1972 |
+
reference to it), or to cv `void`.[^6]
|
| 1973 |
|
| 1974 |
[*Example 1*:
|
| 1975 |
|
| 1976 |
``` cpp
|
| 1977 |
struct X {
|
|
|
|
| 2003 |
struct Z {
|
| 2004 |
explicit operator Y() const;
|
| 2005 |
};
|
| 2006 |
|
| 2007 |
void h(Z z) {
|
| 2008 |
+
Y y1(z); // OK, direct-initialization
|
| 2009 |
Y y2 = z; // error: no conversion function candidate for copy-initialization
|
| 2010 |
+
Y y3 = (Y)z; // OK, cast notation
|
| 2011 |
}
|
| 2012 |
|
| 2013 |
void g(X a, X b) {
|
| 2014 |
int i = (a) ? 1+a : 0;
|
| 2015 |
int j = (a&&b) ? a+b : i;
|
|
|
|
| 2052 |
|
| 2053 |
— *end example*]
|
| 2054 |
|
| 2055 |
— *end note*]
|
| 2056 |
|
| 2057 |
+
[*Note 2*:
|
| 2058 |
+
|
| 2059 |
+
A conversion function in a derived class hides only conversion functions
|
| 2060 |
+
in base classes that convert to the same type. A conversion function
|
| 2061 |
+
template with a dependent return type hides only templates in base
|
| 2062 |
+
classes that correspond to it [[class.member.lookup]]; otherwise, it
|
| 2063 |
+
hides and is hidden as a non-template function. Function overload
|
| 2064 |
+
resolution [[over.match.best]] selects the best conversion function to
|
| 2065 |
+
perform the conversion.
|
| 2066 |
+
|
| 2067 |
+
[*Example 5*:
|
| 2068 |
+
|
| 2069 |
+
``` cpp
|
| 2070 |
+
struct X {
|
| 2071 |
+
operator int();
|
| 2072 |
+
};
|
| 2073 |
+
|
| 2074 |
+
struct Y : X {
|
| 2075 |
+
operator char();
|
| 2076 |
+
};
|
| 2077 |
+
|
| 2078 |
+
void f(Y& a) {
|
| 2079 |
+
if (a) { // error: ambiguous between X::operator int() and Y::operator char()
|
| 2080 |
+
}
|
| 2081 |
+
}
|
| 2082 |
+
```
|
| 2083 |
+
|
| 2084 |
+
— *end example*]
|
| 2085 |
+
|
| 2086 |
+
— *end note*]
|
| 2087 |
|
| 2088 |
Conversion functions can be virtual.
|
| 2089 |
|
| 2090 |
A conversion function template shall not have a deduced return type
|
| 2091 |
[[dcl.spec.auto]].
|
| 2092 |
|
| 2093 |
+
[*Example 6*:
|
| 2094 |
|
| 2095 |
``` cpp
|
| 2096 |
struct S {
|
| 2097 |
operator auto() const { return 10; } // OK
|
| 2098 |
template<class T>
|
|
|
|
| 2102 |
|
| 2103 |
— *end example*]
|
| 2104 |
|
| 2105 |
### Static members <a id="class.static">[[class.static]]</a>
|
| 2106 |
|
| 2107 |
+
#### General <a id="class.static.general">[[class.static.general]]</a>
|
| 2108 |
+
|
| 2109 |
A static member `s` of class `X` may be referred to using the
|
| 2110 |
*qualified-id* expression `X::s`; it is not necessary to use the class
|
| 2111 |
member access syntax [[expr.ref]] to refer to a static member. A static
|
| 2112 |
member may be referred to using the class member access syntax, in which
|
| 2113 |
case the object expression is evaluated.
|
|
|
|
| 2119 |
static void reschedule();
|
| 2120 |
};
|
| 2121 |
process& g();
|
| 2122 |
|
| 2123 |
void f() {
|
| 2124 |
+
process::reschedule(); // OK, no object necessary
|
| 2125 |
g().reschedule(); // g() is called
|
| 2126 |
}
|
| 2127 |
```
|
| 2128 |
|
| 2129 |
— *end example*]
|
| 2130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2131 |
Static members obey the usual class member access rules
|
| 2132 |
[[class.access]]. When used in the declaration of a class member, the
|
| 2133 |
`static` specifier shall only be used in the member declarations that
|
| 2134 |
appear within the *member-specification* of the class definition.
|
| 2135 |
|
|
|
|
| 2140 |
|
| 2141 |
[*Note 1*: The rules described in [[class.mfct]] apply to static
|
| 2142 |
member functions. — *end note*]
|
| 2143 |
|
| 2144 |
[*Note 2*: A static member function does not have a `this` pointer
|
| 2145 |
+
[[expr.prim.this]]. A static member function cannot be qualified with
|
| 2146 |
+
`const`, `volatile`, or `virtual` [[dcl.fct]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2147 |
|
| 2148 |
#### Static data members <a id="class.static.data">[[class.static.data]]</a>
|
| 2149 |
|
| 2150 |
A static data member is not part of the subobjects of a class. If a
|
| 2151 |
static data member is declared `thread_local` there is one copy of the
|
|
|
|
| 2158 |
[[class.pre]] or local [[class.local]] class or of a (possibly
|
| 2159 |
indirectly) nested class [[class.nest]] thereof.
|
| 2160 |
|
| 2161 |
The declaration of a non-inline static data member in its class
|
| 2162 |
definition is not a definition and may be of an incomplete type other
|
| 2163 |
+
than cv `void`.
|
| 2164 |
+
|
| 2165 |
+
[*Note 1*: The *initializer* in the definition of a static data member
|
| 2166 |
+
is in the scope of its class [[basic.scope.class]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 2167 |
|
| 2168 |
[*Example 1*:
|
| 2169 |
|
| 2170 |
``` cpp
|
| 2171 |
class process {
|
|
|
|
| 2175 |
|
| 2176 |
process* process::running = get_main();
|
| 2177 |
process* process::run_chain = running;
|
| 2178 |
```
|
| 2179 |
|
| 2180 |
+
The definition of the static data member `run_chain` of class `process`
|
| 2181 |
+
inhabits the global scope; the notation `process::run_chain` indicates
|
| 2182 |
+
that the member `run_chain` is a member of class `process` and in the
|
| 2183 |
+
scope of class `process`. In the static data member definition, the
|
| 2184 |
+
*initializer* expression refers to the static data member `running` of
|
| 2185 |
+
class `process`.
|
| 2186 |
|
| 2187 |
— *end example*]
|
| 2188 |
|
| 2189 |
+
[*Note 2*:
|
| 2190 |
|
| 2191 |
Once the static data member has been defined, it exists even if no
|
| 2192 |
objects of its class have been created.
|
| 2193 |
|
| 2194 |
[*Example 2*:
|
|
|
|
| 2196 |
In the example above, `run_chain` and `running` exist even if no objects
|
| 2197 |
of class `process` are created by the program.
|
| 2198 |
|
| 2199 |
— *end example*]
|
| 2200 |
|
| 2201 |
+
The initialization and destruction of static data members is described
|
| 2202 |
+
in [[basic.start.static]], [[basic.start.dynamic]], and
|
| 2203 |
+
[[basic.start.term]].
|
| 2204 |
+
|
| 2205 |
— *end note*]
|
| 2206 |
|
| 2207 |
If a non-volatile non-inline `const` static data member is of integral
|
| 2208 |
or enumeration type, its declaration in the class definition can specify
|
| 2209 |
a *brace-or-equal-initializer* in which every *initializer-clause* that
|
| 2210 |
is an *assignment-expression* is a constant expression [[expr.const]].
|
| 2211 |
The member shall still be defined in a namespace scope if it is odr-used
|
| 2212 |
+
[[term.odr.use]] in the program and the namespace scope definition shall
|
| 2213 |
+
not contain an *initializer*. The declaration of an inline static data
|
| 2214 |
+
member (which is a definition) may specify a
|
| 2215 |
*brace-or-equal-initializer*. If the member is declared with the
|
| 2216 |
`constexpr` specifier, it may be redeclared in namespace scope with no
|
| 2217 |
initializer (this usage is deprecated; see [[depr.static.constexpr]]).
|
| 2218 |
Declarations of other static data members shall not specify a
|
| 2219 |
*brace-or-equal-initializer*.
|
| 2220 |
|
| 2221 |
+
[*Note 3*: There is exactly one definition of a static data member that
|
| 2222 |
+
is odr-used [[term.odr.use]] in a valid program. — *end note*]
|
| 2223 |
|
| 2224 |
+
[*Note 4*: Static data members of a class in namespace scope have the
|
| 2225 |
linkage of the name of the class [[basic.link]]. — *end note*]
|
| 2226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2227 |
### Bit-fields <a id="class.bit">[[class.bit]]</a>
|
| 2228 |
|
| 2229 |
A *member-declarator* of the form
|
| 2230 |
|
| 2231 |
``` bnf
|
| 2232 |
identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
|
| 2233 |
```
|
| 2234 |
|
| 2235 |
specifies a bit-field. The optional *attribute-specifier-seq* appertains
|
| 2236 |
to the entity being declared. A bit-field shall not be a static member.
|
| 2237 |
+
A bit-field shall have integral or (possibly cv-qualified) enumeration
|
| 2238 |
+
type; the bit-field semantic property is not part of the type of the
|
| 2239 |
+
class member. The *constant-expression* shall be an integral constant
|
| 2240 |
+
expression with a value greater than or equal to zero and is called the
|
| 2241 |
+
*width* of the bit-field. If the width of a bit-field is larger than the
|
| 2242 |
+
width of the bit-field’s type (or, in case of an enumeration type, of
|
| 2243 |
+
its underlying type), the extra bits are padding bits
|
| 2244 |
+
[[term.padding.bits]]. Allocation of bit-fields within a class object is
|
| 2245 |
+
*implementation-defined*. Alignment of bit-fields is
|
| 2246 |
+
*implementation-defined*. Bit-fields are packed into some addressable
|
| 2247 |
+
allocation unit.
|
| 2248 |
|
| 2249 |
[*Note 1*: Bit-fields straddle allocation units on some machines and
|
| 2250 |
not on others. Bit-fields are assigned right-to-left on some machines,
|
| 2251 |
left-to-right on others. — *end note*]
|
| 2252 |
|
|
|
|
| 2261 |
As a special case, an unnamed bit-field with a width of zero specifies
|
| 2262 |
alignment of the next bit-field at an allocation unit boundary. Only
|
| 2263 |
when declaring an unnamed bit-field may the width be zero.
|
| 2264 |
|
| 2265 |
The address-of operator `&` shall not be applied to a bit-field, so
|
| 2266 |
+
there are no pointers to bit-fields. A non-const reference shall not
|
| 2267 |
+
bind to a bit-field [[dcl.init.ref]].
|
| 2268 |
|
| 2269 |
[*Note 3*: If the initializer for a reference of type `const` `T&` is
|
| 2270 |
an lvalue that refers to a bit-field, the reference is bound to a
|
| 2271 |
temporary initialized to hold the value of the bit-field; the reference
|
| 2272 |
is not bound to the bit-field directly. See
|
|
|
|
| 2299 |
}
|
| 2300 |
```
|
| 2301 |
|
| 2302 |
— *end example*]
|
| 2303 |
|
| 2304 |
+
### Allocation and deallocation functions <a id="class.free">[[class.free]]</a>
|
| 2305 |
+
|
| 2306 |
+
Any allocation function for a class `T` is a static member (even if not
|
| 2307 |
+
explicitly declared `static`).
|
| 2308 |
+
|
| 2309 |
+
[*Example 1*:
|
| 2310 |
+
|
| 2311 |
+
``` cpp
|
| 2312 |
+
class Arena;
|
| 2313 |
+
struct B {
|
| 2314 |
+
void* operator new(std::size_t, Arena*);
|
| 2315 |
+
};
|
| 2316 |
+
struct D1 : B {
|
| 2317 |
+
};
|
| 2318 |
+
|
| 2319 |
+
Arena* ap;
|
| 2320 |
+
void foo(int i) {
|
| 2321 |
+
new (ap) D1; // calls B::operator new(std::size_t, Arena*)
|
| 2322 |
+
new D1[i]; // calls ::operator new[](std::size_t)
|
| 2323 |
+
new D1; // error: ::operator new(std::size_t) hidden
|
| 2324 |
+
}
|
| 2325 |
+
```
|
| 2326 |
+
|
| 2327 |
+
— *end example*]
|
| 2328 |
+
|
| 2329 |
+
Any deallocation function for a class `X` is a static member (even if
|
| 2330 |
+
not explicitly declared `static`).
|
| 2331 |
+
|
| 2332 |
+
[*Example 2*:
|
| 2333 |
+
|
| 2334 |
+
``` cpp
|
| 2335 |
+
class X {
|
| 2336 |
+
void operator delete(void*);
|
| 2337 |
+
void operator delete[](void*, std::size_t);
|
| 2338 |
+
};
|
| 2339 |
+
|
| 2340 |
+
class Y {
|
| 2341 |
+
void operator delete(void*, std::size_t);
|
| 2342 |
+
void operator delete[](void*);
|
| 2343 |
+
};
|
| 2344 |
+
```
|
| 2345 |
+
|
| 2346 |
+
— *end example*]
|
| 2347 |
+
|
| 2348 |
+
Since member allocation and deallocation functions are `static` they
|
| 2349 |
+
cannot be virtual.
|
| 2350 |
+
|
| 2351 |
+
[*Note 1*:
|
| 2352 |
+
|
| 2353 |
+
However, when the *cast-expression* of a *delete-expression* refers to
|
| 2354 |
+
an object of class type with a virtual destructor, because the
|
| 2355 |
+
deallocation function is chosen by the destructor of the dynamic type of
|
| 2356 |
+
the object, the effect is the same in that case. For example,
|
| 2357 |
+
|
| 2358 |
+
``` cpp
|
| 2359 |
+
struct B {
|
| 2360 |
+
virtual ~B();
|
| 2361 |
+
void operator delete(void*, std::size_t);
|
| 2362 |
+
};
|
| 2363 |
+
|
| 2364 |
+
struct D : B {
|
| 2365 |
+
void operator delete(void*);
|
| 2366 |
+
};
|
| 2367 |
+
|
| 2368 |
+
struct E : B {
|
| 2369 |
+
void log_deletion();
|
| 2370 |
+
void operator delete(E *p, std::destroying_delete_t) {
|
| 2371 |
+
p->log_deletion();
|
| 2372 |
+
p->~E();
|
| 2373 |
+
::operator delete(p);
|
| 2374 |
+
}
|
| 2375 |
+
};
|
| 2376 |
+
|
| 2377 |
+
void f() {
|
| 2378 |
+
B* bp = new D;
|
| 2379 |
+
delete bp; // 1: uses D::operator delete(void*)
|
| 2380 |
+
bp = new E;
|
| 2381 |
+
delete bp; // 2: uses E::operator delete(E*, std::destroying_delete_t)
|
| 2382 |
+
}
|
| 2383 |
+
```
|
| 2384 |
+
|
| 2385 |
+
Here, storage for the object of class `D` is deallocated by
|
| 2386 |
+
`D::operator delete()`, and the object of class `E` is destroyed and its
|
| 2387 |
+
storage is deallocated by `E::operator delete()`, due to the virtual
|
| 2388 |
+
destructor.
|
| 2389 |
+
|
| 2390 |
+
— *end note*]
|
| 2391 |
+
|
| 2392 |
+
[*Note 2*:
|
| 2393 |
+
|
| 2394 |
+
Virtual destructors have no effect on the deallocation function actually
|
| 2395 |
+
called when the *cast-expression* of a *delete-expression* refers to an
|
| 2396 |
+
array of objects of class type. For example,
|
| 2397 |
+
|
| 2398 |
+
``` cpp
|
| 2399 |
+
struct B {
|
| 2400 |
+
virtual ~B();
|
| 2401 |
+
void operator delete[](void*, std::size_t);
|
| 2402 |
+
};
|
| 2403 |
+
|
| 2404 |
+
struct D : B {
|
| 2405 |
+
void operator delete[](void*, std::size_t);
|
| 2406 |
+
};
|
| 2407 |
+
|
| 2408 |
+
void f(int i) {
|
| 2409 |
+
D* dp = new D[i];
|
| 2410 |
+
delete [] dp; // uses D::operator delete[](void*, std::size_t)
|
| 2411 |
+
B* bp = new D[i];
|
| 2412 |
+
delete[] bp; // undefined behavior
|
| 2413 |
+
}
|
| 2414 |
+
```
|
| 2415 |
+
|
| 2416 |
+
— *end note*]
|
| 2417 |
+
|
| 2418 |
+
Access to the deallocation function is checked statically, even if a
|
| 2419 |
+
different one is actually executed.
|
| 2420 |
+
|
| 2421 |
+
[*Example 3*: For the call on line “// 1” above, if
|
| 2422 |
+
`B::operator delete()` had been private, the delete expression would
|
| 2423 |
+
have been ill-formed. — *end example*]
|
| 2424 |
+
|
| 2425 |
+
[*Note 3*: If a deallocation function has no explicit
|
| 2426 |
+
*noexcept-specifier*, it has a non-throwing exception specification
|
| 2427 |
+
[[except.spec]]. — *end note*]
|
| 2428 |
+
|
| 2429 |
### Nested class declarations <a id="class.nest">[[class.nest]]</a>
|
| 2430 |
|
| 2431 |
A class can be declared within another class. A class declared within
|
| 2432 |
+
another is called a *nested class*.
|
|
|
|
|
|
|
| 2433 |
|
| 2434 |
[*Note 1*: See [[expr.prim.id]] for restrictions on the use of
|
| 2435 |
non-static data members and non-static member functions. — *end note*]
|
| 2436 |
|
| 2437 |
[*Example 1*:
|
|
|
|
| 2444 |
int x;
|
| 2445 |
static int s;
|
| 2446 |
|
| 2447 |
struct inner {
|
| 2448 |
void f(int i) {
|
| 2449 |
+
int a = sizeof(x); // OK, operand of sizeof is an unevaluated operand
|
| 2450 |
x = i; // error: assign to enclose::x
|
| 2451 |
+
s = i; // OK, assign to enclose::s
|
| 2452 |
+
::x = i; // OK, assign to global x
|
| 2453 |
+
y = i; // OK, assign to global y
|
| 2454 |
}
|
| 2455 |
void g(enclose* p, int i) {
|
| 2456 |
+
p->x = i; // OK, assign to enclose::x
|
| 2457 |
}
|
| 2458 |
};
|
| 2459 |
};
|
| 2460 |
|
| 2461 |
+
inner* p = 0; // error: inner not found
|
| 2462 |
```
|
| 2463 |
|
| 2464 |
— *end example*]
|
| 2465 |
|
| 2466 |
+
[*Note 2*:
|
| 2467 |
+
|
| 2468 |
+
Nested classes can be defined either in the enclosing class or in an
|
| 2469 |
+
enclosing namespace; member functions and static data members of a
|
| 2470 |
+
nested class can be defined either in the nested class or in an
|
| 2471 |
+
enclosing namespace scope.
|
| 2472 |
|
| 2473 |
[*Example 2*:
|
| 2474 |
|
| 2475 |
``` cpp
|
| 2476 |
struct enclose {
|
|
|
|
| 2481 |
};
|
| 2482 |
|
| 2483 |
int enclose::inner::x = 1;
|
| 2484 |
|
| 2485 |
void enclose::inner::f(int i) { ... }
|
|
|
|
| 2486 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2487 |
class E {
|
| 2488 |
class I1; // forward declaration of nested class
|
| 2489 |
class I2;
|
| 2490 |
class I1 { }; // definition of nested class
|
| 2491 |
};
|
| 2492 |
class E::I2 { }; // definition of nested class
|
| 2493 |
```
|
| 2494 |
|
| 2495 |
— *end example*]
|
| 2496 |
|
| 2497 |
+
— *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2498 |
|
| 2499 |
+
A friend function [[class.friend]] defined within a nested class has no
|
| 2500 |
+
special access rights to members of an enclosing class.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2501 |
|
| 2502 |
## Unions <a id="class.union">[[class.union]]</a>
|
| 2503 |
|
| 2504 |
+
### General <a id="class.union.general">[[class.union.general]]</a>
|
| 2505 |
+
|
| 2506 |
A *union* is a class defined with the *class-key* `union`.
|
| 2507 |
|
| 2508 |
In a union, a non-static data member is *active* if its name refers to
|
| 2509 |
an object whose lifetime has begun and has not ended [[basic.life]]. At
|
| 2510 |
most one of the non-static data members of an object of union type can
|
|
|
|
| 2522 |
The size of a union is sufficient to contain the largest of its
|
| 2523 |
non-static data members. Each non-static data member is allocated as if
|
| 2524 |
it were the sole member of a non-union class.
|
| 2525 |
|
| 2526 |
[*Note 2*: A union object and its non-static data members are
|
| 2527 |
+
pointer-interconvertible [[basic.compound]], [[expr.static.cast]]. As a
|
| 2528 |
+
consequence, all non-static data members of a union object have the same
|
| 2529 |
+
address. — *end note*]
|
| 2530 |
|
| 2531 |
A union can have member functions (including constructors and
|
| 2532 |
destructors), but it shall not have virtual [[class.virtual]] functions.
|
| 2533 |
A union shall not have base classes. A union shall not be used as a base
|
| 2534 |
class. If a union contains a non-static data member of reference type
|
| 2535 |
the program is ill-formed.
|
| 2536 |
|
| 2537 |
+
[*Note 3*:
|
| 2538 |
+
|
| 2539 |
+
Absent default member initializers [[class.mem]], if any non-static data
|
| 2540 |
+
member of a union has a non-trivial default constructor
|
| 2541 |
[[class.default.ctor]], copy constructor, move constructor
|
| 2542 |
[[class.copy.ctor]], copy assignment operator, move assignment operator
|
| 2543 |
[[class.copy.assign]], or destructor [[class.dtor]], the corresponding
|
| 2544 |
member function of the union must be user-provided or it will be
|
| 2545 |
+
implicitly deleted [[dcl.fct.def.delete]] for the union.
|
| 2546 |
|
| 2547 |
[*Example 1*:
|
| 2548 |
|
| 2549 |
Consider the following union:
|
| 2550 |
|
|
|
|
| 2562 |
operator, and destructor. To use `U`, some or all of these member
|
| 2563 |
functions must be user-provided.
|
| 2564 |
|
| 2565 |
— *end example*]
|
| 2566 |
|
| 2567 |
+
— *end note*]
|
| 2568 |
+
|
| 2569 |
When the left operand of an assignment operator involves a member access
|
| 2570 |
expression [[expr.ref]] that nominates a union member, it may begin the
|
| 2571 |
lifetime of that union member, as described below. For an expression
|
| 2572 |
`E`, define the set S(E) of subexpressions of `E` as follows:
|
| 2573 |
|
|
|
|
| 2598 |
union A { int x; int y[4]; };
|
| 2599 |
struct B { A a; };
|
| 2600 |
union C { B b; int k; };
|
| 2601 |
int f() {
|
| 2602 |
C c; // does not start lifetime of any union member
|
| 2603 |
+
c.b.a.y[3] = 4; // OK, S(c.b.a.y[3]) contains c.b and c.b.a.y;
|
| 2604 |
// creates objects to hold union members c.b and c.b.a.y
|
| 2605 |
+
return c.b.a.y[3]; // OK, c.b.a.y refers to newly created object (see [basic.life])
|
| 2606 |
}
|
| 2607 |
|
| 2608 |
struct X { const int a; int b; };
|
| 2609 |
union Y { X x; int k; };
|
| 2610 |
void g() {
|
| 2611 |
Y y = { { 1, 2 } }; // OK, y.x is active union member[class.mem]
|
| 2612 |
int n = y.x.a;
|
| 2613 |
+
y.k = 4; // OK, ends lifetime of y.x, y.k is active member of union
|
| 2614 |
y.x.b = n; // undefined behavior: y.x.b modified outside its lifetime,
|
| 2615 |
// S(y.x.b) is empty because X's default constructor is deleted,
|
| 2616 |
// so union member y.x's lifetime does not implicitly start
|
| 2617 |
}
|
| 2618 |
```
|
| 2619 |
|
| 2620 |
— *end example*]
|
| 2621 |
|
| 2622 |
+
[*Note 5*: In cases where the above rule does not apply, the active
|
| 2623 |
+
member of a union can only be changed by the use of a placement
|
| 2624 |
+
*new-expression*. — *end note*]
|
| 2625 |
|
| 2626 |
[*Example 3*:
|
| 2627 |
|
| 2628 |
Consider an object `u` of a `union` type `U` having non-static data
|
| 2629 |
members `m` of type `M` and `n` of type `N`. If `M` has a non-trivial
|
|
|
|
| 2646 |
``` bnf
|
| 2647 |
union '{' member-specification '}' ';'
|
| 2648 |
```
|
| 2649 |
|
| 2650 |
is called an *anonymous union*; it defines an unnamed type and an
|
| 2651 |
+
unnamed object of that type called an *anonymous union member* if it is
|
| 2652 |
+
a non-static data member or an *anonymous union variable* otherwise.
|
| 2653 |
+
Each *member-declaration* in the *member-specification* of an anonymous
|
| 2654 |
+
union shall either define one or more public non-static data members or
|
| 2655 |
+
be a *static_assert-declaration*. Nested types, anonymous unions, and
|
| 2656 |
functions shall not be declared within an anonymous union. The names of
|
| 2657 |
+
the members of an anonymous union are bound in the scope inhabited by
|
| 2658 |
+
the union declaration.
|
|
|
|
|
|
|
|
|
|
| 2659 |
|
| 2660 |
[*Example 1*:
|
| 2661 |
|
| 2662 |
``` cpp
|
| 2663 |
void f() {
|
|
|
|
| 2670 |
Here `a` and `p` are used like ordinary (non-member) variables, but
|
| 2671 |
since they are union members they have the same address.
|
| 2672 |
|
| 2673 |
— *end example*]
|
| 2674 |
|
| 2675 |
+
Anonymous unions declared in the scope of a namespace with external
|
| 2676 |
+
linkage shall be declared `static`. Anonymous unions declared at block
|
| 2677 |
+
scope shall be declared with any storage class allowed for a block
|
| 2678 |
variable, or with no storage class. A storage class is not allowed in a
|
| 2679 |
+
declaration of an anonymous union in a class scope.
|
| 2680 |
+
|
| 2681 |
+
[*Note 1*:
|
| 2682 |
|
| 2683 |
A union for which objects, pointers, or references are declared is not
|
| 2684 |
an anonymous union.
|
| 2685 |
|
| 2686 |
[*Example 2*:
|
|
|
|
| 2697 |
visible outside the union, and even if it were visible, it is not
|
| 2698 |
associated with any particular object.
|
| 2699 |
|
| 2700 |
— *end example*]
|
| 2701 |
|
| 2702 |
+
— *end note*]
|
| 2703 |
+
|
| 2704 |
+
[*Note 2*: Initialization of unions with no user-declared constructors
|
| 2705 |
is described in [[dcl.init.aggr]]. — *end note*]
|
| 2706 |
|
| 2707 |
A *union-like class* is a union or a class that has an anonymous union
|
| 2708 |
as a direct member. A union-like class `X` has a set of *variant
|
| 2709 |
members*. If `X` is a union, a non-static data member of `X` that is not
|
|
|
|
| 2730 |
— *end example*]
|
| 2731 |
|
| 2732 |
## Local class declarations <a id="class.local">[[class.local]]</a>
|
| 2733 |
|
| 2734 |
A class can be declared within a function definition; such a class is
|
| 2735 |
+
called a *local class*.
|
|
|
|
|
|
|
|
|
|
| 2736 |
|
| 2737 |
[*Note 1*: A declaration in a local class cannot odr-use
|
| 2738 |
+
[[term.odr.use]] a local entity from an enclosing scope. — *end note*]
|
| 2739 |
|
| 2740 |
[*Example 1*:
|
| 2741 |
|
| 2742 |
``` cpp
|
| 2743 |
int x;
|
|
|
|
| 2752 |
struct local {
|
| 2753 |
int g() { return x; } // error: odr-use of non-odr-usable variable x
|
| 2754 |
int h() { return s; } // OK
|
| 2755 |
int k() { return ::x; } // OK
|
| 2756 |
int l() { return q(); } // OK
|
| 2757 |
+
int m() { return N; } // OK, not an odr-use
|
| 2758 |
int* n() { return &N; } // error: odr-use of non-odr-usable variable N
|
| 2759 |
int p() { return y; } // error: odr-use of non-odr-usable structured binding y
|
| 2760 |
};
|
| 2761 |
}
|
| 2762 |
|
| 2763 |
+
local* p = 0; // error: local not found
|
| 2764 |
```
|
| 2765 |
|
| 2766 |
— *end example*]
|
| 2767 |
|
| 2768 |
An enclosing function has no special access to members of the local
|
|
|
|
| 2778 |
[*Note 2*: A local class cannot have static data members
|
| 2779 |
[[class.static.data]]. — *end note*]
|
| 2780 |
|
| 2781 |
## Derived classes <a id="class.derived">[[class.derived]]</a>
|
| 2782 |
|
| 2783 |
+
### General <a id="class.derived.general">[[class.derived.general]]</a>
|
| 2784 |
+
|
| 2785 |
A list of base classes can be specified in a class definition using the
|
| 2786 |
notation:
|
| 2787 |
|
| 2788 |
``` bnf
|
| 2789 |
base-clause:
|
|
|
|
| 2818 |
```
|
| 2819 |
|
| 2820 |
The optional *attribute-specifier-seq* appertains to the
|
| 2821 |
*base-specifier*.
|
| 2822 |
|
| 2823 |
+
The component names of a *class-or-decltype* are those of its
|
| 2824 |
+
*nested-name-specifier*, *type-name*, and/or *simple-template-id*. A
|
| 2825 |
+
*class-or-decltype* shall denote a (possibly cv-qualified) class type
|
| 2826 |
that is not an incompletely defined class [[class.mem]]; any
|
| 2827 |
cv-qualifiers are ignored. The class denoted by the *class-or-decltype*
|
| 2828 |
of a *base-specifier* is called a *direct base class* for the class
|
| 2829 |
+
being defined. The lookup for the component name of the *type-name* or
|
| 2830 |
+
*simple-template-id* is type-only [[basic.lookup]]. A class `B` is a
|
| 2831 |
+
base class of a class `D` if it is a direct base class of `D` or a
|
| 2832 |
+
direct base class of one of `D`’s base classes. A class is an
|
| 2833 |
+
*indirect base class* of another if it is a base class but not a direct
|
| 2834 |
+
base class. A class is said to be (directly or indirectly) *derived*
|
| 2835 |
+
from its (direct or indirect) base classes.
|
| 2836 |
|
| 2837 |
[*Note 1*: See [[class.access]] for the meaning of
|
| 2838 |
*access-specifier*. — *end note*]
|
| 2839 |
|
| 2840 |
+
Members of a base class are also members of the derived class.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2841 |
|
| 2842 |
+
[*Note 2*: Constructors of a base class can be explicitly inherited
|
| 2843 |
+
[[namespace.udecl]]. Base class members can be referred to in
|
| 2844 |
+
expressions in the same manner as other members of the derived class,
|
| 2845 |
+
unless their names are hidden or ambiguous [[class.member.lookup]]. The
|
| 2846 |
+
scope resolution operator `::` [[expr.prim.id.qual]] can be used to
|
| 2847 |
+
refer to a direct or indirect base member explicitly, even if it is
|
| 2848 |
+
hidden in the derived class. A derived class can itself serve as a base
|
| 2849 |
+
class subject to access control; see [[class.access.base]]. A pointer
|
| 2850 |
+
to a derived class can be implicitly converted to a pointer to an
|
| 2851 |
+
accessible unambiguous base class [[conv.ptr]]. An lvalue of a derived
|
| 2852 |
+
class type can be bound to a reference to an accessible unambiguous base
|
| 2853 |
+
class [[dcl.init.ref]]. — *end note*]
|
| 2854 |
|
| 2855 |
The *base-specifier-list* specifies the type of the *base class
|
| 2856 |
subobjects* contained in an object of the derived class type.
|
| 2857 |
|
| 2858 |
[*Example 1*:
|
|
|
|
| 2888 |
|
| 2889 |
[*Note 3*: A derived class and its base class subobjects can be
|
| 2890 |
represented by a directed acyclic graph (DAG) where an arrow means
|
| 2891 |
“directly derived from” (see Figure [[fig:class.dag]]). An arrow
|
| 2892 |
need not have a physical representation in memory. A DAG of subobjects
|
| 2893 |
+
is often referred to as a “subobject lattice”. — *end note*]
|
| 2894 |
|
| 2895 |
<a id="fig:class.dag"></a>
|
| 2896 |
|
| 2897 |
![Directed acyclic graph \[fig:class.dag\]](images/figdag.svg)
|
| 2898 |
|
|
|
|
|
|
|
| 2899 |
[*Note 4*: Initialization of objects representing base classes can be
|
| 2900 |
specified in constructors; see [[class.base.init]]. — *end note*]
|
| 2901 |
|
| 2902 |
+
[*Note 5*: A base class subobject can have a layout different from the
|
| 2903 |
+
layout of a most derived object of the same type. A base class subobject
|
| 2904 |
+
can have a polymorphic behavior [[class.cdtor]] different from the
|
| 2905 |
+
polymorphic behavior of a most derived object of the same type. A base
|
| 2906 |
+
class subobject can be of zero size; however, two subobjects that have
|
| 2907 |
+
the same class type and that belong to the same most derived object
|
| 2908 |
+
cannot be allocated at the same address [[intro.object]]. — *end note*]
|
|
|
|
| 2909 |
|
| 2910 |
### Multiple base classes <a id="class.mi">[[class.mi]]</a>
|
| 2911 |
|
| 2912 |
A class can be derived from any number of base classes.
|
| 2913 |
|
|
|
|
| 2925 |
|
| 2926 |
— *end example*]
|
| 2927 |
|
| 2928 |
[*Note 2*: The order of derivation is not significant except as
|
| 2929 |
specified by the semantics of initialization by constructor
|
| 2930 |
+
[[class.base.init]], cleanup [[class.dtor]], and storage layout
|
| 2931 |
+
[[class.mem]], [[class.access.spec]]. — *end note*]
|
| 2932 |
|
| 2933 |
A class shall not be specified as a direct base class of a derived class
|
| 2934 |
more than once.
|
| 2935 |
|
| 2936 |
[*Note 3*: A class can be an indirect base class more than once and can
|
| 2937 |
be a direct and an indirect base class. There are limited things that
|
| 2938 |
+
can be done with such a class; lookup that finds its non-static data
|
| 2939 |
+
members and member functions in the scope of the derived class will be
|
| 2940 |
+
ambiguous. However, the static members, enumerations and types can be
|
| 2941 |
+
unambiguously referred to. — *end note*]
|
| 2942 |
|
| 2943 |
[*Example 2*:
|
| 2944 |
|
| 2945 |
``` cpp
|
| 2946 |
class X { ... };
|
|
|
|
| 2978 |
<a id="fig:class.nonvirt"></a>
|
| 2979 |
|
| 2980 |
![Non-virtual base \[fig:class.nonvirt\]](images/fignonvirt.svg)
|
| 2981 |
|
| 2982 |
In such lattices, explicit qualification can be used to specify which
|
| 2983 |
+
subobject is meant. The body of function `C::f` can refer to the member
|
| 2984 |
+
`next` of each `L` subobject:
|
| 2985 |
|
| 2986 |
``` cpp
|
| 2987 |
void C::f() { A::next = B::next; } // well-formed
|
| 2988 |
```
|
| 2989 |
|
|
|
|
| 3050 |
function declared in a base class (see below).[^7]
|
| 3051 |
|
| 3052 |
[*Note 1*: Virtual functions support dynamic binding and
|
| 3053 |
object-oriented programming. — *end note*]
|
| 3054 |
|
| 3055 |
+
A class with a virtual member function is called a
|
| 3056 |
*polymorphic class*.[^8]
|
| 3057 |
|
| 3058 |
+
If a virtual member function F is declared in a class B, and, in a class
|
| 3059 |
+
D derived (directly or indirectly) from B, a declaration of a member
|
| 3060 |
+
function G corresponds [[basic.scope.scope]] to a declaration of F,
|
| 3061 |
+
ignoring trailing *requires-clause*s, then G *overrides*[^9]
|
| 3062 |
+
|
| 3063 |
+
F. For convenience we say that any virtual function overrides itself. A
|
| 3064 |
+
virtual member function V of a class object S is a *final overrider*
|
| 3065 |
+
unless the most derived class [[intro.object]] of which S is a base
|
| 3066 |
+
class subobject (if any) has another member function that overrides V.
|
| 3067 |
+
In a derived class, if a virtual member function of a base class
|
| 3068 |
+
subobject has more than one final overrider the program is ill-formed.
|
|
|
|
| 3069 |
|
| 3070 |
[*Example 1*:
|
| 3071 |
|
| 3072 |
``` cpp
|
| 3073 |
struct A {
|
|
|
|
| 3093 |
|
| 3094 |
``` cpp
|
| 3095 |
struct A { virtual void f(); };
|
| 3096 |
struct B : A { };
|
| 3097 |
struct C : A { void f(); };
|
| 3098 |
+
struct D : B, C { }; // OK, A::f and C::f are the final overriders
|
| 3099 |
// for the B and C subobjects, respectively
|
| 3100 |
```
|
| 3101 |
|
| 3102 |
— *end example*]
|
| 3103 |
|
|
|
|
| 3167 |
[[dcl.decl]].
|
| 3168 |
|
| 3169 |
[*Example 5*:
|
| 3170 |
|
| 3171 |
``` cpp
|
| 3172 |
+
template<typename T>
|
| 3173 |
struct A {
|
| 3174 |
virtual void f() requires true; // error: virtual function cannot be constrained[temp.constr.decl]
|
| 3175 |
};
|
| 3176 |
```
|
| 3177 |
|
| 3178 |
— *end example*]
|
| 3179 |
|
| 3180 |
+
The *ref-qualifier*, or lack thereof, of an overriding function shall be
|
| 3181 |
+
the same as that of the overridden function.
|
|
|
|
| 3182 |
|
| 3183 |
The return type of an overriding function shall be either identical to
|
| 3184 |
the return type of the overridden function or *covariant* with the
|
| 3185 |
classes of the functions. If a function `D::f` overrides a function
|
| 3186 |
`B::f`, the return types of the functions are covariant if they satisfy
|
|
|
|
| 3197 |
as or less cv-qualification than the class type in the return type of
|
| 3198 |
`B::f`.
|
| 3199 |
|
| 3200 |
If the class type in the covariant return type of `D::f` differs from
|
| 3201 |
that of `B::f`, the class type in the return type of `D::f` shall be
|
| 3202 |
+
complete at the locus [[basic.scope.pdecl]] of the overriding
|
| 3203 |
+
declaration or shall be the class type `D`. When the overriding function
|
| 3204 |
+
is called as the final overrider of the overridden function, its result
|
| 3205 |
+
is converted to the type returned by the (statically chosen) overridden
|
| 3206 |
+
function [[expr.call]].
|
| 3207 |
|
| 3208 |
[*Example 6*:
|
| 3209 |
|
| 3210 |
``` cpp
|
| 3211 |
class B { };
|
|
|
|
| 3226 |
class A;
|
| 3227 |
struct Derived : public Base {
|
| 3228 |
void vf1(); // virtual and overrides Base::vf1()
|
| 3229 |
void vf2(int); // not virtual, hides Base::vf2()
|
| 3230 |
char vf3(); // error: invalid difference in return type only
|
| 3231 |
+
D* vf4(); // OK, returns pointer to derived class
|
| 3232 |
A* vf5(); // error: returns pointer to incomplete class
|
| 3233 |
void f();
|
| 3234 |
};
|
| 3235 |
|
| 3236 |
void g() {
|
|
|
|
| 3259 |
|
| 3260 |
[*Note 4*: The `virtual` specifier implies membership, so a virtual
|
| 3261 |
function cannot be a non-member [[dcl.fct.spec]] function. Nor can a
|
| 3262 |
virtual function be a static member, since a virtual function call
|
| 3263 |
relies on a specific object for determining which function to invoke. A
|
| 3264 |
+
virtual function declared in one class can be declared a friend
|
| 3265 |
+
[[class.friend]] in another class. — *end note*]
|
| 3266 |
|
| 3267 |
A virtual function declared in a class shall be defined, or declared
|
| 3268 |
pure [[class.abstract]] in that class, or both; no diagnostic is
|
| 3269 |
required [[basic.def.odr]].
|
| 3270 |
|
|
|
|
| 3373 |
Here, the function call in `D::f` really does call `B::f` and not
|
| 3374 |
`D::f`.
|
| 3375 |
|
| 3376 |
— *end example*]
|
| 3377 |
|
| 3378 |
+
A deleted function [[dcl.fct.def]] shall not override a function that is
|
| 3379 |
+
not deleted. Likewise, a function that is not deleted shall not override
|
| 3380 |
+
a deleted function.
|
|
|
|
| 3381 |
|
| 3382 |
A `consteval` virtual function shall not override a virtual function
|
| 3383 |
that is not `consteval`. A `consteval` virtual function shall not be
|
| 3384 |
overridden by a virtual function that is not `consteval`.
|
| 3385 |
|
|
|
|
| 3401 |
A class is an *abstract class* if it has at least one pure virtual
|
| 3402 |
function.
|
| 3403 |
|
| 3404 |
[*Note 3*: An abstract class can be used only as a base class of some
|
| 3405 |
other class; no objects of an abstract class can be created except as
|
| 3406 |
+
subobjects of a class derived from it
|
| 3407 |
+
[[basic.def]], [[class.mem]]. — *end note*]
|
| 3408 |
|
| 3409 |
A pure virtual function need be defined only if called with, or as if
|
| 3410 |
with [[class.dtor]], the *qualified-id* syntax [[expr.prim.id.qual]].
|
| 3411 |
|
| 3412 |
[*Example 1*:
|
|
|
|
| 3440 |
|
| 3441 |
[*Note 5*: An abstract class type cannot be used as a parameter or
|
| 3442 |
return type of a function being defined [[dcl.fct]] or called
|
| 3443 |
[[expr.call]], except as specified in [[dcl.type.simple]]. Further, an
|
| 3444 |
abstract class type cannot be used as the type of an explicit type
|
| 3445 |
+
conversion
|
| 3446 |
+
[[expr.static.cast]], [[expr.reinterpret.cast]], [[expr.const.cast]],
|
| 3447 |
+
because the resulting prvalue would be of abstract class type
|
| 3448 |
+
[[basic.lval]]. However, pointers and references to abstract class types
|
| 3449 |
+
can appear in such contexts. — *end note*]
|
| 3450 |
|
| 3451 |
+
A class is abstract if it has at least one pure virtual function for
|
| 3452 |
+
which the final overrider is pure virtual.
|
| 3453 |
|
| 3454 |
[*Example 3*:
|
| 3455 |
|
| 3456 |
``` cpp
|
| 3457 |
class ab_circle : public shape {
|
|
|
|
| 3478 |
`circle::draw()` must be provided.
|
| 3479 |
|
| 3480 |
— *end example*]
|
| 3481 |
|
| 3482 |
[*Note 6*: An abstract class can be derived from a class that is not
|
| 3483 |
+
abstract, and a pure virtual function can override a virtual function
|
| 3484 |
which is not pure. — *end note*]
|
| 3485 |
|
| 3486 |
Member functions can be called from a constructor (or destructor) of an
|
| 3487 |
abstract class; the effect of making a virtual call [[class.virtual]] to
|
| 3488 |
a pure virtual function directly or indirectly for the object being
|
| 3489 |
created (or destroyed) from such a constructor (or destructor) is
|
| 3490 |
undefined.
|
| 3491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3492 |
## Member access control <a id="class.access">[[class.access]]</a>
|
| 3493 |
|
| 3494 |
+
### General <a id="class.access.general">[[class.access.general]]</a>
|
| 3495 |
+
|
| 3496 |
A member of a class can be
|
| 3497 |
|
| 3498 |
+
- private, that is, it can be named only by members and friends of the
|
| 3499 |
+
class in which it is declared;
|
| 3500 |
+
- protected, that is, it can be named only by members and friends of the
|
| 3501 |
+
class in which it is declared, by classes derived from that class, and
|
| 3502 |
+
by their friends (see [[class.protected]]); or
|
| 3503 |
+
- public, that is, it can be named anywhere without access restriction.
|
|
|
|
| 3504 |
|
| 3505 |
+
[*Note 1*: A constructor or destructor can be named by an expression
|
| 3506 |
+
[[basic.def.odr]] even though it has no name. — *end note*]
|
| 3507 |
+
|
| 3508 |
+
A member of a class can also access all the members to which the class
|
| 3509 |
+
has access. A local class of a member function may access the same
|
| 3510 |
+
members that the member function itself may access.[^11]
|
| 3511 |
|
| 3512 |
Members of a class defined with the keyword `class` are `private` by
|
| 3513 |
default. Members of a class defined with the keywords `struct` or
|
| 3514 |
`union` are public by default.
|
| 3515 |
|
|
|
|
| 3525 |
};
|
| 3526 |
```
|
| 3527 |
|
| 3528 |
— *end example*]
|
| 3529 |
|
| 3530 |
+
Access control is applied uniformly to declarations and expressions.
|
|
|
|
| 3531 |
|
| 3532 |
+
[*Note 2*: Access control applies to members nominated by friend
|
| 3533 |
declarations [[class.friend]] and *using-declaration*s
|
| 3534 |
[[namespace.udecl]]. — *end note*]
|
| 3535 |
|
| 3536 |
+
When a *using-declarator* is named, access control is applied to it, not
|
| 3537 |
+
to the declarations that replace it. For an overload set, access control
|
| 3538 |
+
is applied only to the function selected by overload resolution.
|
| 3539 |
|
| 3540 |
+
[*Example 2*:
|
| 3541 |
|
| 3542 |
+
``` cpp
|
| 3543 |
+
struct S {
|
| 3544 |
+
void f(int);
|
| 3545 |
+
private:
|
| 3546 |
+
void f(double);
|
| 3547 |
+
};
|
| 3548 |
+
|
| 3549 |
+
void g(S* sp) {
|
| 3550 |
+
sp->f(2); // OK, access control applied after overload resolution
|
| 3551 |
+
}
|
| 3552 |
+
```
|
| 3553 |
+
|
| 3554 |
+
— *end example*]
|
| 3555 |
+
|
| 3556 |
+
[*Note 3*:
|
| 3557 |
+
|
| 3558 |
+
Because access control applies to the declarations named, if access
|
| 3559 |
+
control is applied to a *typedef-name*, only the accessibility of the
|
| 3560 |
+
typedef or alias declaration itself is considered. The accessibility of
|
| 3561 |
+
the entity referred to by the *typedef-name* is not considered. For
|
| 3562 |
+
example,
|
| 3563 |
|
| 3564 |
``` cpp
|
| 3565 |
class A {
|
| 3566 |
class B { };
|
| 3567 |
public:
|
| 3568 |
typedef B BB;
|
| 3569 |
};
|
| 3570 |
|
| 3571 |
void f() {
|
| 3572 |
+
A::BB x; // OK, typedef A::BB is public
|
| 3573 |
A::B y; // access error, A::B is private
|
| 3574 |
}
|
| 3575 |
```
|
| 3576 |
|
| 3577 |
— *end note*]
|
| 3578 |
|
| 3579 |
+
[*Note 4*: Access control does not prevent members from being found by
|
| 3580 |
+
name lookup or implicit conversions to base classes from being
|
| 3581 |
+
considered. — *end note*]
|
|
|
|
| 3582 |
|
| 3583 |
The interpretation of a given construct is established without regard to
|
| 3584 |
access control. If the interpretation established makes use of
|
| 3585 |
+
inaccessible members or base classes, the construct is ill-formed.
|
| 3586 |
|
| 3587 |
+
All access controls in [[class.access]] affect the ability to name a
|
| 3588 |
+
class member from the declaration of a particular entity, including
|
| 3589 |
parts of the declaration preceding the name of the entity being declared
|
| 3590 |
and, if the entity is a class, the definitions of members of the class
|
| 3591 |
appearing outside the class’s *member-specification*.
|
| 3592 |
|
| 3593 |
+
[*Note 5*: This access also applies to implicit references to
|
| 3594 |
constructors, conversion functions, and destructors. — *end note*]
|
| 3595 |
|
| 3596 |
+
[*Example 3*:
|
| 3597 |
|
| 3598 |
``` cpp
|
| 3599 |
class A {
|
| 3600 |
typedef int I; // private member
|
| 3601 |
I f();
|
|
|
|
| 3626 |
`A`, so checking of *base-specifier*s must be deferred until the entire
|
| 3627 |
*base-specifier-list* has been seen.
|
| 3628 |
|
| 3629 |
— *end example*]
|
| 3630 |
|
| 3631 |
+
Access is checked for a default argument [[dcl.fct.default]] at the
|
| 3632 |
+
point of declaration, rather than at any points of use of the default
|
| 3633 |
+
argument. Access checking for default arguments in function templates
|
| 3634 |
+
and in member functions of class templates is performed as described in
|
| 3635 |
+
[[temp.inst]].
|
| 3636 |
|
| 3637 |
+
Access for a default *template-argument* [[temp.param]] is checked in
|
| 3638 |
+
the context in which it appears rather than at any points of use of it.
|
|
|
|
| 3639 |
|
| 3640 |
+
[*Example 4*:
|
| 3641 |
|
| 3642 |
``` cpp
|
| 3643 |
class B { };
|
| 3644 |
template <class T> class C {
|
| 3645 |
protected:
|
|
|
|
| 3654 |
|
| 3655 |
— *end example*]
|
| 3656 |
|
| 3657 |
### Access specifiers <a id="class.access.spec">[[class.access.spec]]</a>
|
| 3658 |
|
| 3659 |
+
Member declarations can be labeled by an *access-specifier*
|
| 3660 |
+
[[class.derived]]:
|
| 3661 |
|
| 3662 |
``` bnf
|
| 3663 |
access-specifier ':' member-specificationₒₚₜ
|
| 3664 |
```
|
| 3665 |
|
|
|
|
| 3697 |
};
|
| 3698 |
```
|
| 3699 |
|
| 3700 |
— *end example*]
|
| 3701 |
|
|
|
|
|
|
|
|
|
|
| 3702 |
When a member is redeclared within its class definition, the access
|
| 3703 |
specified at its redeclaration shall be the same as at its initial
|
| 3704 |
declaration.
|
| 3705 |
|
| 3706 |
[*Example 3*:
|
|
|
|
| 3715 |
};
|
| 3716 |
```
|
| 3717 |
|
| 3718 |
— *end example*]
|
| 3719 |
|
| 3720 |
+
[*Note 1*: In a derived class, the lookup of a base class name will
|
| 3721 |
find the injected-class-name instead of the name of the base class in
|
| 3722 |
the scope in which it was declared. The injected-class-name might be
|
| 3723 |
less accessible than the name of the base class in the scope in which it
|
| 3724 |
was declared. — *end note*]
|
| 3725 |
|
|
|
|
| 3774 |
|
| 3775 |
— *end example*]
|
| 3776 |
|
| 3777 |
[*Note 1*:
|
| 3778 |
|
| 3779 |
+
A member of a private base class can be inaccessible as inherited, but
|
| 3780 |
+
accessible directly. Because of the rules on pointer conversions
|
| 3781 |
+
[[conv.ptr]] and explicit casts
|
| 3782 |
+
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]], a conversion
|
| 3783 |
+
from a pointer to a derived class to a pointer to an inaccessible base
|
| 3784 |
+
class can be ill-formed if an implicit conversion is used, but
|
| 3785 |
+
well-formed if an explicit cast is used. For example,
|
| 3786 |
|
| 3787 |
``` cpp
|
| 3788 |
class B {
|
| 3789 |
public:
|
| 3790 |
int mi; // non-static member
|
|
|
|
| 3803 |
b.mi = 3; // OK (b.mi is different from this->mi)
|
| 3804 |
b.si = 3; // OK (b.si is different from this->si)
|
| 3805 |
::B::si = 3; // OK
|
| 3806 |
::B* bp1 = this; // error: B is a private base class
|
| 3807 |
::B* bp2 = (::B*)this; // OK with cast
|
| 3808 |
+
bp2->mi = 3; // OK, access through a pointer to B.
|
| 3809 |
}
|
| 3810 |
```
|
| 3811 |
|
| 3812 |
— *end note*]
|
| 3813 |
|
| 3814 |
A base class `B` of `N` is *accessible* at *R*, if
|
| 3815 |
|
| 3816 |
- an invented public member of `B` would be a public member of `N`, or
|
| 3817 |
+
- *R* occurs in a direct member or friend of class `N`, and an invented
|
| 3818 |
+
public member of `B` would be a private or protected member of `N`, or
|
| 3819 |
+
- *R* occurs in a direct member or friend of a class `P` derived from
|
| 3820 |
+
`N`, and an invented public member of `B` would be a private or
|
| 3821 |
+
protected member of `P`, or
|
| 3822 |
- there exists a class `S` such that `B` is a base class of `S`
|
| 3823 |
accessible at *R* and `S` is a base class of `N` accessible at *R*.
|
| 3824 |
|
| 3825 |
[*Example 2*:
|
| 3826 |
|
|
|
|
| 3844 |
```
|
| 3845 |
|
| 3846 |
— *end example*]
|
| 3847 |
|
| 3848 |
If a base class is accessible, one can implicitly convert a pointer to a
|
| 3849 |
+
derived class to a pointer to that base class
|
| 3850 |
+
[[conv.ptr]], [[conv.mem]].
|
| 3851 |
|
| 3852 |
[*Note 2*: It follows that members and friends of a class `X` can
|
| 3853 |
implicitly convert an `X*` to a pointer to a private or protected
|
| 3854 |
immediate base class of `X`. — *end note*]
|
| 3855 |
|
| 3856 |
The access to a member is affected by the class in which the member is
|
| 3857 |
+
named. This naming class is the class in whose scope name lookup
|
| 3858 |
+
performed a search that found the member.
|
| 3859 |
|
| 3860 |
[*Note 3*: This class can be explicit, e.g., when a *qualified-id* is
|
| 3861 |
used, or implicit, e.g., when a class member access operator
|
| 3862 |
[[expr.ref]] is used (including cases where an implicit “`this->`” is
|
| 3863 |
added). If both a class member access operator and a *qualified-id* are
|
|
|
|
| 3866 |
*qualified-id* (that is, `T`). — *end note*]
|
| 3867 |
|
| 3868 |
A member `m` is accessible at the point *R* when named in class `N` if
|
| 3869 |
|
| 3870 |
- `m` as a member of `N` is public, or
|
| 3871 |
+
- `m` as a member of `N` is private, and *R* occurs in a direct member
|
| 3872 |
+
or friend of class `N`, or
|
| 3873 |
+
- `m` as a member of `N` is protected, and *R* occurs in a direct member
|
| 3874 |
+
or friend of class `N`, or in a member of a class `P` derived from
|
| 3875 |
+
`N`, where `m` as a member of `P` is public, private, or protected, or
|
| 3876 |
- there exists a base class `B` of `N` that is accessible at *R*, and
|
| 3877 |
`m` is accessible at *R* when named in class `B`.
|
| 3878 |
\[*Example 3*:
|
| 3879 |
``` cpp
|
| 3880 |
class B;
|
|
|
|
| 3883 |
int i;
|
| 3884 |
friend void f(B*);
|
| 3885 |
};
|
| 3886 |
class B : public A { };
|
| 3887 |
void f(B* p) {
|
| 3888 |
+
p->i = 1; // OK, B* can be implicitly converted to A*, and f has access to i in A
|
| 3889 |
}
|
| 3890 |
```
|
| 3891 |
|
| 3892 |
— *end example*]
|
| 3893 |
|
|
|
|
| 3901 |
member be accessible as named. — *end note*]
|
| 3902 |
|
| 3903 |
### Friends <a id="class.friend">[[class.friend]]</a>
|
| 3904 |
|
| 3905 |
A friend of a class is a function or class that is given permission to
|
| 3906 |
+
name the private and protected members of the class. A class specifies
|
| 3907 |
+
its friends, if any, by way of friend declarations. Such declarations
|
| 3908 |
+
give special access rights to the friends, but they do not make the
|
| 3909 |
+
nominated friends members of the befriending class.
|
| 3910 |
|
| 3911 |
[*Example 1*:
|
| 3912 |
|
| 3913 |
The following example illustrates the differences between members and
|
| 3914 |
friends:
|
|
|
|
| 3931 |
}
|
| 3932 |
```
|
| 3933 |
|
| 3934 |
— *end example*]
|
| 3935 |
|
| 3936 |
+
Declaring a class to be a friend implies that private and protected
|
| 3937 |
+
members of the class granting friendship can be named in the
|
| 3938 |
+
*base-specifier*s and member declarations of the befriended class.
|
| 3939 |
|
| 3940 |
[*Example 2*:
|
| 3941 |
|
| 3942 |
``` cpp
|
| 3943 |
class A {
|
| 3944 |
class B { };
|
| 3945 |
friend class X;
|
| 3946 |
};
|
| 3947 |
|
| 3948 |
+
struct X : A::B { // OK, A::B accessible to friend
|
| 3949 |
+
A::B mx; // OK, A::B accessible to member of friend
|
| 3950 |
class Y {
|
| 3951 |
+
A::B my; // OK, A::B accessible to nested member of friend
|
| 3952 |
};
|
| 3953 |
};
|
| 3954 |
```
|
| 3955 |
|
| 3956 |
— *end example*]
|
|
|
|
| 3972 |
};
|
| 3973 |
```
|
| 3974 |
|
| 3975 |
— *end example*]
|
| 3976 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3977 |
A friend declaration that does not declare a function shall have one of
|
| 3978 |
the following forms:
|
| 3979 |
|
| 3980 |
``` bnf
|
| 3981 |
friend elaborated-type-specifier ';'
|
| 3982 |
friend simple-type-specifier ';'
|
| 3983 |
friend typename-specifier ';'
|
| 3984 |
```
|
| 3985 |
|
| 3986 |
+
[*Note 1*: A friend declaration can be the *declaration* in a
|
| 3987 |
+
*template-declaration* [[temp.pre]], [[temp.friend]]. — *end note*]
|
| 3988 |
|
| 3989 |
If the type specifier in a `friend` declaration designates a (possibly
|
| 3990 |
cv-qualified) class type, that class is declared as a friend; otherwise,
|
| 3991 |
the friend declaration is ignored.
|
| 3992 |
|
| 3993 |
+
[*Example 4*:
|
| 3994 |
|
| 3995 |
``` cpp
|
| 3996 |
class C;
|
| 3997 |
typedef C Ct;
|
| 3998 |
|
| 3999 |
class X1 {
|
| 4000 |
+
friend C; // OK, class C is a friend
|
| 4001 |
};
|
| 4002 |
|
| 4003 |
class X2 {
|
| 4004 |
+
friend Ct; // OK, class C is a friend
|
| 4005 |
+
friend D; // error: D not found
|
| 4006 |
+
friend class D; // OK, elaborated-type-specifier declares new class
|
| 4007 |
};
|
| 4008 |
|
| 4009 |
template <typename T> class R {
|
| 4010 |
friend T;
|
| 4011 |
};
|
| 4012 |
|
| 4013 |
R<C> rc; // class C is a friend of R<C>
|
| 4014 |
+
R<int> Ri; // OK, "friend int;" is ignored
|
| 4015 |
```
|
| 4016 |
|
| 4017 |
— *end example*]
|
| 4018 |
|
| 4019 |
A function first declared in a friend declaration has the linkage of the
|
| 4020 |
+
namespace of which it is a member [[basic.link]]. Otherwise, the
|
| 4021 |
+
function retains its previous linkage [[dcl.stc]].
|
|
|
|
| 4022 |
|
| 4023 |
+
[*Note 2*:
|
|
|
|
|
|
|
| 4024 |
|
| 4025 |
+
A friend declaration refers to an entity, not (all overloads of) a name.
|
| 4026 |
+
A member function of a class `X` can be a friend of a class `Y`.
|
| 4027 |
+
|
| 4028 |
+
[*Example 5*:
|
| 4029 |
|
| 4030 |
``` cpp
|
| 4031 |
class Y {
|
| 4032 |
friend char* X::foo(int);
|
| 4033 |
friend X::X(char); // constructors can be friends
|
|
|
|
| 4035 |
};
|
| 4036 |
```
|
| 4037 |
|
| 4038 |
— *end example*]
|
| 4039 |
|
| 4040 |
+
— *end note*]
|
|
|
|
|
|
|
| 4041 |
|
| 4042 |
+
A function may be defined in a friend declaration of a class if and only
|
| 4043 |
+
if the class is a non-local class [[class.local]] and the function name
|
| 4044 |
+
is unqualified.
|
| 4045 |
+
|
| 4046 |
+
[*Example 6*:
|
| 4047 |
|
| 4048 |
``` cpp
|
| 4049 |
class M {
|
| 4050 |
friend void f() { } // definition of global f, a friend of M,
|
| 4051 |
// not the definition of a member function
|
|
|
|
| 4053 |
```
|
| 4054 |
|
| 4055 |
— *end example*]
|
| 4056 |
|
| 4057 |
Such a function is implicitly an inline [[dcl.inline]] function if it is
|
| 4058 |
+
attached to the global module.
|
| 4059 |
+
|
| 4060 |
+
[*Note 3*: If a friend function is defined outside a class, it is not
|
| 4061 |
+
in the scope of the class. — *end note*]
|
| 4062 |
|
| 4063 |
No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
|
| 4064 |
a friend declaration.
|
| 4065 |
|
| 4066 |
+
A member nominated by a friend declaration shall be accessible in the
|
| 4067 |
+
class containing the friend declaration. The meaning of the friend
|
| 4068 |
+
declaration is the same whether the friend declaration appears in the
|
| 4069 |
+
private, protected, or public [[class.mem]] portion of the class
|
| 4070 |
*member-specification*.
|
| 4071 |
|
| 4072 |
Friendship is neither inherited nor transitive.
|
| 4073 |
|
| 4074 |
+
[*Example 7*:
|
| 4075 |
|
| 4076 |
``` cpp
|
| 4077 |
class A {
|
| 4078 |
friend class B;
|
| 4079 |
int a;
|
|
|
|
| 4096 |
};
|
| 4097 |
```
|
| 4098 |
|
| 4099 |
— *end example*]
|
| 4100 |
|
| 4101 |
+
[*Note 4*: A friend declaration never binds any names
|
| 4102 |
+
[[dcl.meaning]], [[dcl.type.elab]]. — *end note*]
|
| 4103 |
+
|
| 4104 |
+
[*Example 8*:
|
| 4105 |
+
|
| 4106 |
+
``` cpp
|
| 4107 |
+
// Assume f and g have not yet been declared.
|
| 4108 |
+
void h(int);
|
| 4109 |
+
template <class T> void f2(T);
|
| 4110 |
+
namespace A {
|
| 4111 |
+
class X {
|
| 4112 |
+
friend void f(X); // A::f(X) is a friend
|
| 4113 |
+
class Y {
|
| 4114 |
+
friend void g(); // A::g is a friend
|
| 4115 |
+
friend void h(int); // A::h is a friend
|
| 4116 |
+
// ::h not considered
|
| 4117 |
+
friend void f2<>(int); // ::f2<>(int) is a friend
|
| 4118 |
+
};
|
| 4119 |
+
};
|
| 4120 |
+
|
| 4121 |
+
// A::f, A::g and A::h are not visible here
|
| 4122 |
+
X x;
|
| 4123 |
+
void g() { f(x); } // definition of A::g
|
| 4124 |
+
void f(X) { ... } // definition of A::f
|
| 4125 |
+
void h(int) { ... } // definition of A::h
|
| 4126 |
+
// A::f, A::g and A::h are visible here and known to be friends
|
| 4127 |
+
}
|
| 4128 |
+
|
| 4129 |
+
using A::x;
|
| 4130 |
+
|
| 4131 |
+
void h() {
|
| 4132 |
+
A::f(x);
|
| 4133 |
+
A::X::f(x); // error: f is not a member of A::X
|
| 4134 |
+
A::X::Y::g(); // error: g is not a member of A::X::Y
|
| 4135 |
+
}
|
| 4136 |
+
```
|
| 4137 |
+
|
| 4138 |
+
— *end example*]
|
| 4139 |
|
| 4140 |
[*Example 9*:
|
| 4141 |
|
| 4142 |
``` cpp
|
| 4143 |
class X;
|
|
|
|
| 4163 |
### Protected member access <a id="class.protected">[[class.protected]]</a>
|
| 4164 |
|
| 4165 |
An additional access check beyond those described earlier in
|
| 4166 |
[[class.access]] is applied when a non-static data member or non-static
|
| 4167 |
member function is a protected member of its naming class
|
| 4168 |
+
[[class.access.base]].[^13]
|
| 4169 |
+
|
| 4170 |
+
As described earlier, access to a protected member is granted because
|
| 4171 |
+
the reference occurs in a friend or direct member of some class `C`. If
|
| 4172 |
+
the access is to form a pointer to member [[expr.unary.op]], the
|
| 4173 |
+
*nested-name-specifier* shall denote `C` or a class derived from `C`.
|
| 4174 |
+
All other accesses involve a (possibly implicit) object expression
|
| 4175 |
+
[[expr.ref]]. In this case, the class of the object expression shall be
|
| 4176 |
+
`C` or a class derived from `C`.
|
| 4177 |
|
| 4178 |
[*Example 1*:
|
| 4179 |
|
| 4180 |
``` cpp
|
| 4181 |
class B {
|
|
|
|
| 4245 |
void f() {
|
| 4246 |
D d;
|
| 4247 |
B* pb = &d;
|
| 4248 |
D* pd = &d;
|
| 4249 |
|
| 4250 |
+
pb->f(); // OK, B::f() is public, D::f() is invoked
|
| 4251 |
pd->f(); // error: D::f() is private
|
| 4252 |
}
|
| 4253 |
```
|
| 4254 |
|
| 4255 |
— *end example*]
|
|
|
|
| 4259 |
in the example above). The access of the member function in the class in
|
| 4260 |
which it was defined (`D` in the example above) is in general not known.
|
| 4261 |
|
| 4262 |
### Multiple access <a id="class.paths">[[class.paths]]</a>
|
| 4263 |
|
| 4264 |
+
If a declaration can be reached by several paths through a multiple
|
| 4265 |
+
inheritance graph, the access is that of the path that gives most
|
| 4266 |
+
access.
|
| 4267 |
|
| 4268 |
[*Example 1*:
|
| 4269 |
|
| 4270 |
``` cpp
|
| 4271 |
class W { public: void f(); };
|
|
|
|
| 4294 |
class E {
|
| 4295 |
int x;
|
| 4296 |
class B { };
|
| 4297 |
|
| 4298 |
class I {
|
| 4299 |
+
B b; // OK, E::I can access E::B
|
| 4300 |
int y;
|
| 4301 |
void f(E* p, int i) {
|
| 4302 |
+
p->x = i; // OK, E::I can access E::x
|
| 4303 |
}
|
| 4304 |
};
|
| 4305 |
|
| 4306 |
int g(I* p) {
|
| 4307 |
return p->y; // error: I::y is private
|
|
|
|
| 4311 |
|
| 4312 |
— *end example*]
|
| 4313 |
|
| 4314 |
## Initialization <a id="class.init">[[class.init]]</a>
|
| 4315 |
|
| 4316 |
+
### General <a id="class.init.general">[[class.init.general]]</a>
|
| 4317 |
+
|
| 4318 |
When no initializer is specified for an object of (possibly
|
| 4319 |
cv-qualified) class type (or array thereof), or the initializer has the
|
| 4320 |
form `()`, the object is initialized as specified in [[dcl.init]].
|
| 4321 |
|
| 4322 |
An object of class type (or array thereof) can be explicitly
|
|
|
|
| 4376 |
```
|
| 4377 |
|
| 4378 |
Here, `complex::complex(double)` is called for the initialization of
|
| 4379 |
`v[0]` and `v[3]`, `complex::complex({}double, double)` is called for
|
| 4380 |
the initialization of `v[1]`, `complex::complex()` is called for the
|
| 4381 |
+
initialization of `v[2]`, `v[4]`, and `v[5]`. For another example,
|
| 4382 |
|
| 4383 |
``` cpp
|
| 4384 |
struct X {
|
| 4385 |
int i;
|
| 4386 |
float f;
|
|
|
|
| 4433 |
mem-initializer-id:
|
| 4434 |
class-or-decltype
|
| 4435 |
identifier
|
| 4436 |
```
|
| 4437 |
|
| 4438 |
+
Lookup for an unqualified name in a *mem-initializer-id* ignores the
|
| 4439 |
+
constructor’s function parameter scope.
|
|
|
|
|
|
|
| 4440 |
|
| 4441 |
[*Note 1*: If the constructor’s class contains a member with the same
|
| 4442 |
name as a direct or virtual base class of the class, a
|
| 4443 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 4444 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 4445 |
+
the hidden base class can be specified using a qualified
|
| 4446 |
name. — *end note*]
|
| 4447 |
|
| 4448 |
Unless the *mem-initializer-id* names the constructor’s class, a
|
| 4449 |
non-static data member of the constructor’s class, or a direct or
|
| 4450 |
virtual base of that class, the *mem-initializer* is ill-formed.
|
|
|
|
| 4463 |
```
|
| 4464 |
|
| 4465 |
— *end example*]
|
| 4466 |
|
| 4467 |
If a *mem-initializer-id* is ambiguous because it designates both a
|
| 4468 |
+
direct non-virtual base class and an indirect virtual base class, the
|
| 4469 |
*mem-initializer* is ill-formed.
|
| 4470 |
|
| 4471 |
[*Example 2*:
|
| 4472 |
|
| 4473 |
``` cpp
|
|
|
|
| 4551 |
};
|
| 4552 |
```
|
| 4553 |
|
| 4554 |
— *end example*]
|
| 4555 |
|
| 4556 |
+
In a non-delegating constructor other than an implicitly-defined
|
| 4557 |
+
copy/move constructor [[class.copy.ctor]], if a given potentially
|
| 4558 |
+
constructed subobject is not designated by a *mem-initializer-id*
|
| 4559 |
+
(including the case where there is no *mem-initializer-list* because the
|
| 4560 |
+
constructor has no *ctor-initializer*), then
|
| 4561 |
|
| 4562 |
- if the entity is a non-static data member that has a default member
|
| 4563 |
initializer [[class.mem]] and either
|
| 4564 |
- the constructor’s class is a union [[class.union]], and no other
|
| 4565 |
variant member of that union is designated by a *mem-initializer-id*
|
|
|
|
| 4574 |
[[class.union.anon]], no initialization is performed;
|
| 4575 |
- otherwise, the entity is default-initialized [[dcl.init]].
|
| 4576 |
|
| 4577 |
[*Note 3*: An abstract class [[class.abstract]] is never a most derived
|
| 4578 |
class, thus its constructors never initialize virtual base classes,
|
| 4579 |
+
therefore the corresponding *mem-initializer*s can be
|
| 4580 |
omitted. — *end note*]
|
| 4581 |
|
| 4582 |
An attempt to initialize more than one non-static data member of a union
|
| 4583 |
renders the program ill-formed.
|
| 4584 |
|
|
|
|
| 4600 |
B(int);
|
| 4601 |
};
|
| 4602 |
|
| 4603 |
struct C {
|
| 4604 |
C() { } // initializes members as follows:
|
| 4605 |
+
A a; // OK, calls A::A()
|
| 4606 |
const B b; // error: B has no default constructor
|
| 4607 |
+
int i; // OK, i has indeterminate value
|
| 4608 |
+
int j = 5; // OK, j has the value 5
|
| 4609 |
};
|
| 4610 |
```
|
| 4611 |
|
| 4612 |
— *end example*]
|
| 4613 |
|
|
|
|
| 4713 |
C c(4); // use V()
|
| 4714 |
```
|
| 4715 |
|
| 4716 |
— *end example*]
|
| 4717 |
|
| 4718 |
+
[*Note 7*: The *expression-list* or *braced-init-list* of a
|
| 4719 |
+
*mem-initializer* is in the function parameter scope of the constructor
|
| 4720 |
+
and can use `this` to refer to the object being
|
| 4721 |
+
initialized. — *end note*]
|
| 4722 |
|
| 4723 |
[*Example 10*:
|
| 4724 |
|
| 4725 |
``` cpp
|
| 4726 |
class X {
|
|
|
|
| 4739 |
the constructor parameter `i`, and initializes `X::j` with the value of
|
| 4740 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 4741 |
|
| 4742 |
— *end example*]
|
| 4743 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4744 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 4745 |
can be called for an object under construction. Similarly, an object
|
| 4746 |
under construction can be the operand of the `typeid` operator
|
| 4747 |
[[expr.typeid]] or of a `dynamic_cast` [[expr.dynamic.cast]]. However,
|
| 4748 |
if these operations are performed in a *ctor-initializer* (or in a
|
|
|
|
| 4779 |
};
|
| 4780 |
```
|
| 4781 |
|
| 4782 |
— *end example*]
|
| 4783 |
|
| 4784 |
+
[*Note 8*: [[class.cdtor]] describes the results of virtual function
|
| 4785 |
calls, `typeid` and `dynamic_cast`s during construction for the
|
| 4786 |
well-defined cases; that is, describes the polymorphic behavior of an
|
| 4787 |
object under construction. — *end note*]
|
| 4788 |
|
| 4789 |
A *mem-initializer* followed by an ellipsis is a pack expansion
|
|
|
|
| 4833 |
int x;
|
| 4834 |
int y = get();
|
| 4835 |
};
|
| 4836 |
|
| 4837 |
void test() {
|
| 4838 |
+
D1 d(2, 3, 4); // OK, B1 is initialized by calling B1(2, 3, 4),
|
| 4839 |
// then d.x is default-initialized (no initialization is performed),
|
| 4840 |
// then d.y is initialized by calling get()
|
| 4841 |
+
D1 e; // error: D1 has no default constructor
|
| 4842 |
}
|
| 4843 |
|
| 4844 |
struct D2 : B2 {
|
| 4845 |
using B2::B2;
|
| 4846 |
B1 b;
|
| 4847 |
};
|
| 4848 |
|
| 4849 |
+
D2 f(1.0); // error: B1 has no default constructor
|
| 4850 |
|
| 4851 |
struct W { W(int); };
|
| 4852 |
struct X : virtual W { using W::W; X() = delete; };
|
| 4853 |
struct Y : X { using X::X; };
|
| 4854 |
struct Z : Y, virtual W { using Y::Y; };
|
| 4855 |
+
Z z(0); // OK, initialization of Y does not invoke default constructor of X
|
| 4856 |
|
| 4857 |
template<class T> struct Log : T {
|
| 4858 |
using T::T; // inherits all constructors from class T
|
| 4859 |
~Log() { std::clog << "Destroying wrapper" << std::endl; }
|
| 4860 |
};
|
|
|
|
| 4890 |
using V1::V1;
|
| 4891 |
using V2::V2;
|
| 4892 |
};
|
| 4893 |
|
| 4894 |
D1 d1(0); // error: ambiguous
|
| 4895 |
+
D2 d2(0); // OK, initializes virtual B base class, which initializes the A base class
|
| 4896 |
// then initializes the V1 and V2 base classes as if by a defaulted default constructor
|
| 4897 |
|
| 4898 |
struct M { M(); M(int); };
|
| 4899 |
struct N : M { using M::M; };
|
| 4900 |
struct O : M {};
|
| 4901 |
struct P : N, O { using N::N; using O::O; };
|
| 4902 |
+
P p(0); // OK, use M(0) to initialize N's base class,
|
| 4903 |
// use M() to initialize O's base class
|
| 4904 |
```
|
| 4905 |
|
| 4906 |
— *end example*]
|
| 4907 |
|
|
|
|
| 5132 |
different ways of referring to the same object. If the first parameter
|
| 5133 |
of the selected constructor is an rvalue reference to the object’s type,
|
| 5134 |
the destruction of that object occurs when the target would have been
|
| 5135 |
destroyed; otherwise, the destruction occurs at the later of the times
|
| 5136 |
when the two objects would have been destroyed without the
|
| 5137 |
+
optimization.[^14]
|
| 5138 |
+
|
| 5139 |
+
This elision of copy/move operations, called *copy elision*, is
|
| 5140 |
+
permitted in the following circumstances (which may be combined to
|
| 5141 |
+
eliminate multiple copies):
|
| 5142 |
|
| 5143 |
- in a `return` statement in a function with a class return type, when
|
| 5144 |
the *expression* is the name of a non-volatile object with automatic
|
| 5145 |
storage duration (other than a function parameter or a variable
|
| 5146 |
introduced by the *exception-declaration* of a *handler*
|
|
|
|
| 5148 |
the function return type, the copy/move operation can be omitted by
|
| 5149 |
constructing the object directly into the function call’s return
|
| 5150 |
object
|
| 5151 |
- in a *throw-expression* [[expr.throw]], when the operand is the name
|
| 5152 |
of a non-volatile object with automatic storage duration (other than a
|
| 5153 |
+
function or catch-clause parameter) that belongs to a scope that does
|
| 5154 |
+
not contain the innermost enclosing *compound-statement* associated
|
| 5155 |
+
with a *try-block* (if there is one), the copy/move operation can be
|
| 5156 |
+
omitted by constructing the object directly into the exception object
|
| 5157 |
- in a coroutine [[dcl.fct.def.coroutine]], a copy of a coroutine
|
| 5158 |
parameter can be omitted and references to that copy replaced with
|
| 5159 |
references to the corresponding parameter if the meaning of the
|
| 5160 |
program will be unchanged except for the execution of a constructor
|
| 5161 |
and destructor for the parameter copy object
|
|
|
|
| 5171 |
|
| 5172 |
Copy elision is not permitted where an expression is evaluated in a
|
| 5173 |
context requiring a constant expression [[expr.const]] and in constant
|
| 5174 |
initialization [[basic.start.static]].
|
| 5175 |
|
| 5176 |
+
[*Note 2*: It is possible that copy elision is performed if the same
|
| 5177 |
+
expression is evaluated in another context. — *end note*]
|
| 5178 |
|
| 5179 |
[*Example 1*:
|
| 5180 |
|
| 5181 |
``` cpp
|
| 5182 |
class Thing {
|
|
|
|
| 5205 |
|
| 5206 |
constexpr A a; // well-formed, a.p points to a
|
| 5207 |
constexpr A b = g(); // error: b.p would be dangling[expr.const]
|
| 5208 |
|
| 5209 |
void h() {
|
| 5210 |
+
A c = g(); // well-formed, c.p can point to c or be dangling
|
| 5211 |
}
|
| 5212 |
```
|
| 5213 |
|
| 5214 |
Here the criteria for elision can eliminate the copying of the object
|
| 5215 |
`t` with automatic storage duration into the result object for the
|
| 5216 |
+
function call `f()`, which is the non-local object `t2`. Effectively,
|
| 5217 |
+
the construction of `t` can be viewed as directly initializing `t2`, and
|
| 5218 |
+
that object’s destruction will occur at program exit. Adding a move
|
| 5219 |
+
constructor to `Thing` has the same effect, but it is the move
|
| 5220 |
+
construction from the object with automatic storage duration to `t2`
|
| 5221 |
+
that is elided.
|
| 5222 |
|
| 5223 |
— *end example*]
|
| 5224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5225 |
[*Example 2*:
|
| 5226 |
|
| 5227 |
``` cpp
|
| 5228 |
class Thing {
|
| 5229 |
public:
|
|
|
|
| 5235 |
};
|
| 5236 |
|
| 5237 |
Thing f(bool b) {
|
| 5238 |
Thing t;
|
| 5239 |
if (b)
|
| 5240 |
+
throw t; // OK, Thing(Thing&&) used (or elided) to throw t
|
| 5241 |
+
return t; // OK, Thing(Thing&&) used (or elided) to return t
|
| 5242 |
}
|
| 5243 |
|
| 5244 |
+
Thing t2 = f(false); // OK, no extra copy/move performed, t2 constructed by call to f
|
| 5245 |
|
| 5246 |
struct Weird {
|
| 5247 |
Weird();
|
| 5248 |
Weird(Weird&);
|
| 5249 |
};
|
| 5250 |
|
| 5251 |
+
Weird g(bool b) {
|
| 5252 |
+
static Weird w1;
|
| 5253 |
+
Weird w2;
|
| 5254 |
+
if (b)
|
| 5255 |
+
return w1; // OK, uses Weird(Weird&)
|
| 5256 |
+
else
|
| 5257 |
+
return w2; // error: w2 in this context is an xvalue
|
| 5258 |
+
}
|
| 5259 |
+
|
| 5260 |
+
int& h(bool b, int i) {
|
| 5261 |
+
static int s;
|
| 5262 |
+
if (b)
|
| 5263 |
+
return s; // OK
|
| 5264 |
+
else
|
| 5265 |
+
return i; // error: i is an xvalue
|
| 5266 |
+
}
|
| 5267 |
+
|
| 5268 |
+
decltype(auto) h2(Thing t) {
|
| 5269 |
+
return t; // OK, t is an xvalue and h2's return type is Thing
|
| 5270 |
+
}
|
| 5271 |
+
|
| 5272 |
+
decltype(auto) h3(Thing t) {
|
| 5273 |
+
return (t); // OK, (t) is an xvalue and h3's return type is Thing&&
|
| 5274 |
}
|
| 5275 |
```
|
| 5276 |
|
| 5277 |
— *end example*]
|
| 5278 |
|
|
|
|
| 5306 |
### Defaulted comparison operator functions <a id="class.compare.default">[[class.compare.default]]</a>
|
| 5307 |
|
| 5308 |
A defaulted comparison operator function [[over.binary]] for some class
|
| 5309 |
`C` shall be a non-template function that is
|
| 5310 |
|
| 5311 |
+
- a non-static member or friend of `C` and
|
| 5312 |
+
- either has two parameters of type `const C&` or two parameters of type
|
| 5313 |
+
`C`, where the implicit object parameter (if any) is considered to be
|
| 5314 |
+
the first parameter.
|
|
|
|
| 5315 |
|
| 5316 |
+
Name lookups in the implicit definition [[dcl.fct.def.default]] of a
|
| 5317 |
+
comparison operator function are performed from a context equivalent to
|
| 5318 |
+
its *function-body*. A definition of a comparison operator as defaulted
|
| 5319 |
+
that appears in a class shall be the first declaration of that function.
|
|
|
|
|
|
|
|
|
|
| 5320 |
|
| 5321 |
A defaulted `<=>` or `==` operator function for class `C` is defined as
|
| 5322 |
deleted if any non-static data member of `C` is of reference type or `C`
|
| 5323 |
has variant members [[class.union.anon]].
|
| 5324 |
|
|
|
|
| 5327 |
- `a` or `b` is of class or enumeration type and overload resolution
|
| 5328 |
[[over.match]] as applied to `a @ b` results in a usable candidate, or
|
| 5329 |
- neither `a` nor `b` is of class or enumeration type and `a @ b` is a
|
| 5330 |
valid expression.
|
| 5331 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5332 |
If the *member-specification* does not explicitly declare any member or
|
| 5333 |
friend named `operator==`, an `==` operator function is declared
|
| 5334 |
implicitly for each three-way comparison operator function defined as
|
| 5335 |
defaulted in the *member-specification*, with the same access and
|
| 5336 |
*function-definition* and in the same class scope as the respective
|
| 5337 |
three-way comparison operator function, except that the return type is
|
| 5338 |
replaced with `bool` and the *declarator-id* is replaced with
|
| 5339 |
`operator==`.
|
| 5340 |
|
| 5341 |
+
[*Note 1*: Such an implicitly-declared `==` operator for a class `X` is
|
| 5342 |
defined as defaulted in the definition of `X` and has the same
|
| 5343 |
*parameter-declaration-clause* and trailing *requires-clause* as the
|
| 5344 |
respective three-way comparison operator. It is declared with `friend`,
|
| 5345 |
`virtual`, `constexpr`, or `consteval` if the three-way comparison
|
| 5346 |
operator function is so declared. If the three-way comparison operator
|
| 5347 |
function has no *noexcept-specifier*, the implicitly-declared `==`
|
| 5348 |
operator function has an implicit exception specification
|
| 5349 |
+
[[except.spec]] that can differ from the implicit exception
|
| 5350 |
specification of the three-way comparison operator
|
| 5351 |
function. — *end note*]
|
| 5352 |
|
| 5353 |
[*Example 1*:
|
| 5354 |
|
|
|
|
| 5362 |
};
|
| 5363 |
```
|
| 5364 |
|
| 5365 |
— *end example*]
|
| 5366 |
|
| 5367 |
+
[*Note 2*: The `==` operator function is declared implicitly even if
|
| 5368 |
the defaulted three-way comparison operator function is defined as
|
| 5369 |
deleted. — *end note*]
|
| 5370 |
|
| 5371 |
The direct base class subobjects of `C`, in the order of their
|
| 5372 |
declaration in the *base-specifier-list* of `C`, followed by the
|
|
|
|
| 5412 |
### Three-way comparison <a id="class.spaceship">[[class.spaceship]]</a>
|
| 5413 |
|
| 5414 |
The *synthesized three-way comparison* of type `R` [[cmp.categories]] of
|
| 5415 |
glvalues `a` and `b` of the same type is defined as follows:
|
| 5416 |
|
| 5417 |
+
- If `a <=> b` is usable [[class.compare.default]] and can be explicitly
|
| 5418 |
+
converted to `R` using `static_cast`, `static_cast<R>(a <=> b)`.
|
| 5419 |
- Otherwise, if overload resolution for `a <=> b` is performed and finds
|
| 5420 |
at least one viable candidate, the synthesized three-way comparison is
|
| 5421 |
not defined.
|
| 5422 |
- Otherwise, if `R` is not a comparison category type, or either the
|
| 5423 |
expression `a == b` or the expression `a < b` is not usable, the
|
|
|
|
| 5440 |
a < b ? partial_ordering::less :
|
| 5441 |
b < a ? partial_ordering::greater :
|
| 5442 |
partial_ordering::unordered
|
| 5443 |
```
|
| 5444 |
|
| 5445 |
+
[*Note 1*: A synthesized three-way comparison is ill-formed if overload
|
| 5446 |
+
resolution finds usable candidates that do not otherwise meet the
|
| 5447 |
+
requirements implied by the defined expression. — *end note*]
|
| 5448 |
|
| 5449 |
Let `R` be the declared return type of a defaulted three-way comparison
|
| 5450 |
operator function, and let `xᵢ` be the elements of the expanded list of
|
| 5451 |
subobjects for an object `x` of type `C`.
|
| 5452 |
|
|
|
|
| 5510 |
};
|
| 5511 |
```
|
| 5512 |
|
| 5513 |
— *end example*]
|
| 5514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5515 |
<!-- Link reference definitions -->
|
| 5516 |
+
[basic.align]: basic.md#basic.align
|
| 5517 |
[basic.compound]: basic.md#basic.compound
|
| 5518 |
[basic.def]: basic.md#basic.def
|
| 5519 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 5520 |
[basic.life]: basic.md#basic.life
|
| 5521 |
[basic.link]: basic.md#basic.link
|
| 5522 |
[basic.lookup]: basic.md#basic.lookup
|
| 5523 |
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
|
|
|
|
|
|
| 5524 |
[basic.lval]: expr.md#basic.lval
|
|
|
|
| 5525 |
[basic.scope.class]: basic.md#basic.scope.class
|
| 5526 |
+
[basic.scope.pdecl]: basic.md#basic.scope.pdecl
|
| 5527 |
+
[basic.scope.scope]: basic.md#basic.scope.scope
|
| 5528 |
[basic.start.dynamic]: basic.md#basic.start.dynamic
|
| 5529 |
[basic.start.static]: basic.md#basic.start.static
|
| 5530 |
[basic.start.term]: basic.md#basic.start.term
|
|
|
|
| 5531 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
|
|
|
|
|
|
| 5532 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 5533 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
|
|
|
| 5534 |
[basic.types]: basic.md#basic.types
|
| 5535 |
[class]: #class
|
| 5536 |
[class.abstract]: #class.abstract
|
| 5537 |
[class.access]: #class.access
|
| 5538 |
[class.access.base]: #class.access.base
|
| 5539 |
+
[class.access.general]: #class.access.general
|
| 5540 |
[class.access.nest]: #class.access.nest
|
| 5541 |
[class.access.spec]: #class.access.spec
|
| 5542 |
[class.access.virt]: #class.access.virt
|
| 5543 |
[class.base.init]: #class.base.init
|
| 5544 |
[class.bit]: #class.bit
|
|
|
|
| 5547 |
[class.compare.default]: #class.compare.default
|
| 5548 |
[class.compare.secondary]: #class.compare.secondary
|
| 5549 |
[class.conv]: #class.conv
|
| 5550 |
[class.conv.ctor]: #class.conv.ctor
|
| 5551 |
[class.conv.fct]: #class.conv.fct
|
| 5552 |
+
[class.conv.general]: #class.conv.general
|
| 5553 |
[class.copy.assign]: #class.copy.assign
|
| 5554 |
[class.copy.ctor]: #class.copy.ctor
|
| 5555 |
[class.copy.elision]: #class.copy.elision
|
| 5556 |
[class.ctor]: #class.ctor
|
| 5557 |
+
[class.ctor.general]: #class.ctor.general
|
| 5558 |
[class.default.ctor]: #class.default.ctor
|
| 5559 |
[class.derived]: #class.derived
|
| 5560 |
+
[class.derived.general]: #class.derived.general
|
| 5561 |
[class.dtor]: #class.dtor
|
| 5562 |
[class.eq]: #class.eq
|
| 5563 |
[class.expl.init]: #class.expl.init
|
| 5564 |
[class.free]: #class.free
|
| 5565 |
[class.friend]: #class.friend
|
| 5566 |
[class.inhctor.init]: #class.inhctor.init
|
| 5567 |
[class.init]: #class.init
|
| 5568 |
+
[class.init.general]: #class.init.general
|
| 5569 |
[class.local]: #class.local
|
| 5570 |
[class.mem]: #class.mem
|
| 5571 |
+
[class.mem.general]: #class.mem.general
|
| 5572 |
+
[class.member.lookup]: basic.md#class.member.lookup
|
| 5573 |
[class.mfct]: #class.mfct
|
| 5574 |
+
[class.mfct.non.static]: #class.mfct.non.static
|
| 5575 |
[class.mi]: #class.mi
|
| 5576 |
[class.name]: #class.name
|
| 5577 |
[class.nest]: #class.nest
|
|
|
|
| 5578 |
[class.paths]: #class.paths
|
| 5579 |
[class.pre]: #class.pre
|
| 5580 |
[class.prop]: #class.prop
|
| 5581 |
[class.protected]: #class.protected
|
| 5582 |
[class.qual]: basic.md#class.qual
|
| 5583 |
[class.spaceship]: #class.spaceship
|
| 5584 |
[class.static]: #class.static
|
| 5585 |
[class.static.data]: #class.static.data
|
| 5586 |
+
[class.static.general]: #class.static.general
|
| 5587 |
[class.static.mfct]: #class.static.mfct
|
| 5588 |
[class.temporary]: basic.md#class.temporary
|
|
|
|
| 5589 |
[class.union]: #class.union
|
| 5590 |
[class.union.anon]: #class.union.anon
|
| 5591 |
+
[class.union.general]: #class.union.general
|
| 5592 |
[class.virtual]: #class.virtual
|
| 5593 |
[cmp.categories]: support.md#cmp.categories
|
| 5594 |
[cmp.categories.pre]: support.md#cmp.categories.pre
|
| 5595 |
[cmp.partialord]: support.md#cmp.partialord
|
| 5596 |
[cmp.strongord]: support.md#cmp.strongord
|
|
|
|
| 5605 |
[dcl.decl]: dcl.md#dcl.decl
|
| 5606 |
[dcl.enum]: dcl.md#dcl.enum
|
| 5607 |
[dcl.fct]: dcl.md#dcl.fct
|
| 5608 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 5609 |
[dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
|
| 5610 |
+
[dcl.fct.def.default]: dcl.md#dcl.fct.def.default
|
| 5611 |
[dcl.fct.def.delete]: dcl.md#dcl.fct.def.delete
|
| 5612 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 5613 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 5614 |
[dcl.fct.spec]: dcl.md#dcl.fct.spec
|
| 5615 |
[dcl.init]: dcl.md#dcl.init
|
| 5616 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 5617 |
+
[dcl.init.general]: dcl.md#dcl.init.general
|
| 5618 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 5619 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 5620 |
[dcl.inline]: dcl.md#dcl.inline
|
| 5621 |
+
[dcl.meaning]: dcl.md#dcl.meaning
|
| 5622 |
+
[dcl.name]: dcl.md#dcl.name
|
| 5623 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 5624 |
[dcl.stc]: dcl.md#dcl.stc
|
| 5625 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 5626 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 5627 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
|
|
|
| 5656 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 5657 |
[expr.typeid]: expr.md#expr.typeid
|
| 5658 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 5659 |
[intro.execution]: basic.md#intro.execution
|
| 5660 |
[intro.object]: basic.md#intro.object
|
|
|
|
|
|
|
| 5661 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 5662 |
[over]: over.md#over
|
| 5663 |
[over.ass]: over.md#over.ass
|
| 5664 |
[over.best.ics]: over.md#over.best.ics
|
| 5665 |
[over.binary]: over.md#over.binary
|
| 5666 |
[over.ics.ref]: over.md#over.ics.ref
|
|
|
|
| 5667 |
[over.match]: over.md#over.match
|
| 5668 |
[over.match.best]: over.md#over.match.best
|
| 5669 |
[over.match.call]: over.md#over.match.call
|
| 5670 |
[over.match.copy]: over.md#over.match.copy
|
| 5671 |
[over.match.funcs]: over.md#over.match.funcs
|
| 5672 |
[over.oper]: over.md#over.oper
|
| 5673 |
[over.over]: over.md#over.over
|
| 5674 |
[special]: #special
|
| 5675 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 5676 |
[stmt.return]: stmt.md#stmt.return
|
|
|
|
| 5677 |
[string.classes]: strings.md#string.classes
|
| 5678 |
[temp.arg]: temp.md#temp.arg
|
|
|
|
| 5679 |
[temp.constr]: temp.md#temp.constr
|
| 5680 |
[temp.constr.order]: temp.md#temp.constr.order
|
| 5681 |
+
[temp.deduct.guide]: temp.md#temp.deduct.guide
|
| 5682 |
[temp.dep.type]: temp.md#temp.dep.type
|
| 5683 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 5684 |
+
[temp.explicit]: temp.md#temp.explicit
|
| 5685 |
[temp.friend]: temp.md#temp.friend
|
| 5686 |
[temp.inst]: temp.md#temp.inst
|
| 5687 |
[temp.mem]: temp.md#temp.mem
|
| 5688 |
[temp.param]: temp.md#temp.param
|
| 5689 |
[temp.pre]: temp.md#temp.pre
|
| 5690 |
+
[temp.spec.partial]: temp.md#temp.spec.partial
|
| 5691 |
[temp.variadic]: temp.md#temp.variadic
|
| 5692 |
+
[term.incomplete.type]: basic.md#term.incomplete.type
|
| 5693 |
+
[term.layout.compatible.type]: basic.md#term.layout.compatible.type
|
| 5694 |
+
[term.object.representation]: basic.md#term.object.representation
|
| 5695 |
+
[term.odr.use]: basic.md#term.odr.use
|
| 5696 |
+
[term.padding.bits]: basic.md#term.padding.bits
|
| 5697 |
|
| 5698 |
[^1]: This ensures that two subobjects that have the same class type and
|
| 5699 |
that belong to the same most derived object are not allocated at the
|
| 5700 |
same address [[expr.eq]].
|
| 5701 |
|
|
|
|
| 5716 |
[^5]: This implies that the reference parameter of the
|
| 5717 |
implicitly-declared copy assignment operator cannot bind to a
|
| 5718 |
`volatile` lvalue; see [[diff.class]].
|
| 5719 |
|
| 5720 |
[^6]: These conversions are considered as standard conversions for the
|
| 5721 |
+
purposes of overload resolution [[over.best.ics]], [[over.ics.ref]]
|
| 5722 |
+
and therefore initialization [[dcl.init]] and explicit casts
|
| 5723 |
+
[[expr.static.cast]]. A conversion to `void` does not invoke any
|
| 5724 |
+
conversion function [[expr.static.cast]]. Even though never directly
|
| 5725 |
+
called to perform a conversion, such conversion functions can be
|
| 5726 |
+
declared and can potentially be reached through a call to a virtual
|
| 5727 |
+
conversion function in a base class.
|
| 5728 |
|
| 5729 |
[^7]: The use of the `virtual` specifier in the declaration of an
|
| 5730 |
overriding function is valid but redundant (has empty semantics).
|
| 5731 |
|
| 5732 |
[^8]: If all virtual functions are immediate functions, the class is
|
| 5733 |
+
still polymorphic even if its internal representation does not
|
| 5734 |
otherwise require any additions for that polymorphic behavior.
|
| 5735 |
|
| 5736 |
[^9]: A function with the same name but a different parameter list
|
| 5737 |
[[over]] as a virtual function is not necessarily virtual and does
|
| 5738 |
not override. Access control [[class.access]] is not considered in
|
|
|
|
| 5753 |
static data members or enumerator member constants.
|
| 5754 |
|
| 5755 |
[^14]: Because only one object is destroyed instead of two, and one
|
| 5756 |
copy/move constructor is not executed, there is still one object
|
| 5757 |
destroyed for each one constructed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|