- tmp/tmpx2an48b_/{from.md → to.md} +273 -236
tmp/tmpx2an48b_/{from.md → to.md}
RENAMED
|
@@ -10,13 +10,15 @@ member-specification:
|
|
| 10 |
|
| 11 |
``` bnf
|
| 12 |
member-declaration:
|
| 13 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 14 |
function-definition
|
|
|
|
| 15 |
using-declaration
|
| 16 |
using-enum-declaration
|
| 17 |
static_assert-declaration
|
|
|
|
| 18 |
template-declaration
|
| 19 |
explicit-specialization
|
| 20 |
deduction-guide
|
| 21 |
alias-declaration
|
| 22 |
opaque-enum-declaration
|
|
@@ -29,20 +31,19 @@ member-declarator-list:
|
|
| 29 |
member-declarator-list ',' member-declarator
|
| 30 |
```
|
| 31 |
|
| 32 |
``` bnf
|
| 33 |
member-declarator:
|
| 34 |
-
declarator virt-specifier-seqₒₚₜ pure-specifierₒₚₜ
|
| 35 |
-
declarator requires-clause
|
| 36 |
-
declarator brace-or-equal-initializer
|
| 37 |
identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
|
| 38 |
```
|
| 39 |
|
| 40 |
``` bnf
|
| 41 |
virt-specifier-seq:
|
| 42 |
-
virt-specifier
|
| 43 |
-
virt-specifier-seq virt-specifier
|
| 44 |
```
|
| 45 |
|
| 46 |
``` bnf
|
| 47 |
virt-specifier:
|
| 48 |
override
|
|
@@ -52,57 +53,97 @@ virt-specifier:
|
|
| 52 |
``` bnf
|
| 53 |
pure-specifier:
|
| 54 |
'=' '0'
|
| 55 |
```
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
The *member-specification* in a class definition declares the full set
|
| 58 |
of members of the class; no member can be added elsewhere. A
|
| 59 |
*direct member* of a class `X` is a member of `X` that was first
|
| 60 |
declared within the *member-specification* of `X`, including anonymous
|
| 61 |
union members [[class.union.anon]] and direct members thereof. Members
|
| 62 |
of a class are data members, member functions [[class.mfct]], nested
|
| 63 |
types, enumerators, and member templates [[temp.mem]] and
|
| 64 |
specializations thereof.
|
| 65 |
|
| 66 |
-
[*Note
|
| 67 |
static data member. A specialization of a member function template is a
|
| 68 |
member function. A specialization of a member class template is a nested
|
| 69 |
class. — *end note*]
|
| 70 |
|
| 71 |
-
A *member-declaration* does not declare new members of the class
|
| 72 |
-
is
|
| 73 |
|
| 74 |
- a friend declaration [[class.friend]],
|
| 75 |
- a *deduction-guide* [[temp.deduct.guide]],
|
| 76 |
- a *template-declaration* whose *declaration* is one of the above,
|
| 77 |
- a *static_assert-declaration*,
|
|
|
|
| 78 |
- a *using-declaration* [[namespace.udecl]], or
|
| 79 |
- an *empty-declaration*.
|
| 80 |
|
| 81 |
For any other *member-declaration*, each declared entity that is not an
|
| 82 |
unnamed bit-field [[class.bit]] is a member of the class, and each such
|
| 83 |
*member-declaration* shall either declare at least one member name of
|
| 84 |
the class or declare at least one unnamed bit-field.
|
| 85 |
|
| 86 |
-
A *data member* is a non-function member introduced by a
|
| 87 |
-
*member-declarator*. A *member function* is
|
| 88 |
-
|
| 89 |
-
[[
|
| 90 |
-
members by use of a typedef
|
| 91 |
-
*alias-declaration*. The enumerators of
|
| 92 |
-
[[dcl.enum]] defined in the class are members of
|
|
|
|
| 93 |
|
| 94 |
A data member or member function may be declared `static` in its
|
| 95 |
*member-declaration*, in which case it is a *static member* (see
|
| 96 |
[[class.static]]) (a *static data member* [[class.static.data]] or
|
| 97 |
*static member function* [[class.static.mfct]], respectively) of the
|
| 98 |
class. Any other data member or member function is a *non-static member*
|
| 99 |
(a *non-static data member* or *non-static member function*
|
| 100 |
[[class.mfct.non.static]], respectively).
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
A member shall not be declared twice in the *member-specification*,
|
| 106 |
except that
|
| 107 |
|
| 108 |
- a nested class or member class template can be declared and then later
|
|
@@ -122,23 +163,27 @@ non-static data member.
|
|
| 122 |
A *complete-class context* of a class (template) is a
|
| 123 |
|
| 124 |
- function body [[dcl.fct.def.general]],
|
| 125 |
- default argument [[dcl.fct.default]],
|
| 126 |
- default template argument [[temp.param]],
|
| 127 |
-
- *noexcept-specifier* [[except.spec]],
|
|
|
|
| 128 |
- default member initializer
|
| 129 |
|
| 130 |
within the *member-specification* of the class or class template.
|
| 131 |
|
| 132 |
[*Note 4*: A complete-class context of a nested class is also a
|
| 133 |
complete-class context of any enclosing class, if the nested class is
|
| 134 |
defined within the *member-specification* of the enclosing
|
| 135 |
class. — *end note*]
|
| 136 |
|
| 137 |
-
A class is
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 142 |
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 143 |
has function type, otherwise it is interpreted as introducing a
|
| 144 |
*brace-or-equal-initializer*.
|
|
@@ -268,11 +313,11 @@ If `T` is the name of a class, then each of the following shall have a
|
|
| 268 |
name different from `T`:
|
| 269 |
|
| 270 |
- every static data member of class `T`;
|
| 271 |
- every member function of class `T`; \[*Note 9*: This restriction does
|
| 272 |
not apply to constructors, which do not have names
|
| 273 |
-
[[class.ctor]] — *end note*]
|
| 274 |
- every member of class `T` that is itself a type;
|
| 275 |
- every member template of class `T`;
|
| 276 |
- every enumerator of every member of class `T` that is an unscoped
|
| 277 |
enumeration type; and
|
| 278 |
- every member of every anonymous union that is a member of class `T`.
|
|
@@ -287,12 +332,13 @@ and bit-fields in declaration order, starting with the first such entity
|
|
| 287 |
in each of the structs, such that
|
| 288 |
|
| 289 |
- corresponding entities have layout-compatible types [[basic.types]],
|
| 290 |
- corresponding entities have the same alignment requirements
|
| 291 |
[[basic.align]],
|
| 292 |
-
-
|
| 293 |
-
attribute
|
|
|
|
| 294 |
- either both entities are bit-fields with the same width or neither is
|
| 295 |
a bit-field.
|
| 296 |
|
| 297 |
[*Example 4*:
|
| 298 |
|
|
@@ -355,10 +401,47 @@ alignment. — *end note*]
|
|
| 355 |
|
| 356 |
[*Note 12*: The object and its first subobject are
|
| 357 |
pointer-interconvertible
|
| 358 |
[[basic.compound]], [[expr.static.cast]]. — *end note*]
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 361 |
|
| 362 |
If a member function is attached to the global module and is defined
|
| 363 |
[[dcl.fct.def]] in its class definition, it is inline [[dcl.inline]].
|
| 364 |
|
|
@@ -391,11 +474,13 @@ definition, if they are defined at all.
|
|
| 391 |
[*Note 2*:
|
| 392 |
|
| 393 |
A member function can be declared (but not defined) using a typedef for
|
| 394 |
a function type. The resulting member function has exactly the same type
|
| 395 |
as it would have if the function declarator were provided explicitly,
|
| 396 |
-
see [[dcl.fct]]
|
|
|
|
|
|
|
| 397 |
|
| 398 |
``` cpp
|
| 399 |
typedef void fv();
|
| 400 |
typedef void fvc() const;
|
| 401 |
struct S {
|
|
@@ -406,11 +491,11 @@ struct S {
|
|
| 406 |
fv S::* pmfv1 = &S::memfunc1;
|
| 407 |
fv S::* pmfv2 = &S::memfunc2;
|
| 408 |
fvc S::* pmfv3 = &S::memfunc3;
|
| 409 |
```
|
| 410 |
|
| 411 |
-
|
| 412 |
|
| 413 |
— *end note*]
|
| 414 |
|
| 415 |
### Non-static member functions <a id="class.mfct.non.static">[[class.mfct.non.static]]</a>
|
| 416 |
|
|
@@ -420,65 +505,14 @@ class type, using the class member access syntax
|
|
| 420 |
[[expr.ref]], [[over.match.call]]. A non-static member function may also
|
| 421 |
be called directly using the function call syntax
|
| 422 |
[[expr.call]], [[over.match.call]] from within its class or a class
|
| 423 |
derived from its class, or a member thereof, as described below.
|
| 424 |
|
| 425 |
-
|
| 426 |
-
member access syntax [[expr.ref]] nor the unparenthesized operand of the
|
| 427 |
-
unary `&` operator [[expr.unary.op]] is used where the current class is
|
| 428 |
-
`X` [[expr.prim.this]], if name lookup [[basic.lookup]] resolves the
|
| 429 |
-
name in the *id-expression* to a non-static non-type member of some
|
| 430 |
-
class `C`, and if either the *id-expression* is potentially evaluated or
|
| 431 |
-
`C` is `X` or a base class of `X`, the *id-expression* is transformed
|
| 432 |
-
into a class member access expression [[expr.ref]] using `(*this)` as
|
| 433 |
-
the *postfix-expression* to the left of the `.` operator.
|
| 434 |
-
|
| 435 |
-
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 436 |
-
access expression is ill-formed. — *end note*]
|
| 437 |
-
|
| 438 |
-
This transformation does not apply in the template definition context
|
| 439 |
-
[[temp.dep.type]].
|
| 440 |
-
|
| 441 |
-
[*Example 1*:
|
| 442 |
-
|
| 443 |
-
``` cpp
|
| 444 |
-
struct tnode {
|
| 445 |
-
char tword[20];
|
| 446 |
-
int count;
|
| 447 |
-
tnode* left;
|
| 448 |
-
tnode* right;
|
| 449 |
-
void set(const char*, tnode* l, tnode* r);
|
| 450 |
-
};
|
| 451 |
-
|
| 452 |
-
void tnode::set(const char* w, tnode* l, tnode* r) {
|
| 453 |
-
count = strlen(w)+1;
|
| 454 |
-
if (sizeof(tword)<=count)
|
| 455 |
-
perror("tnode string too long");
|
| 456 |
-
strcpy(tword,w);
|
| 457 |
-
left = l;
|
| 458 |
-
right = r;
|
| 459 |
-
}
|
| 460 |
-
|
| 461 |
-
void f(tnode n1, tnode n2) {
|
| 462 |
-
n1.set("abc",&n2,0);
|
| 463 |
-
n2.set("def",0,0);
|
| 464 |
-
}
|
| 465 |
-
```
|
| 466 |
-
|
| 467 |
-
In the body of the member function `tnode::set`, the member names
|
| 468 |
-
`tword`, `count`, `left`, and `right` refer to members of the object for
|
| 469 |
-
which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
| 470 |
-
`tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
|
| 471 |
-
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 472 |
-
not members of the class `tnode` and should be declared elsewhere.[^2]
|
| 473 |
-
|
| 474 |
-
— *end example*]
|
| 475 |
-
|
| 476 |
-
[*Note 2*: An implicit object member function can be declared with
|
| 477 |
*cv-qualifier*s, which affect the type of the `this` pointer
|
| 478 |
[[expr.prim.this]], and/or a *ref-qualifier* [[dcl.fct]]; both affect
|
| 479 |
-
overload resolution [[over.match.funcs]] — *end note*]
|
| 480 |
|
| 481 |
An implicit object member function may be declared virtual
|
| 482 |
[[class.virtual]] or pure virtual [[class.abstract]].
|
| 483 |
|
| 484 |
### Special member functions <a id="special">[[special]]</a>
|
|
@@ -527,11 +561,11 @@ Special member functions obey the usual access rules [[class.access]].
|
|
| 527 |
|
| 528 |
[*Example 2*: Declaring a constructor protected ensures that only
|
| 529 |
derived classes and friends can create objects using
|
| 530 |
it. — *end example*]
|
| 531 |
|
| 532 |
-
Two special member functions are of the same kind if
|
| 533 |
|
| 534 |
- they are both default constructors,
|
| 535 |
- they are both copy or move constructors with the same first parameter
|
| 536 |
type, or
|
| 537 |
- they are both copy or move assignment operators with the same first
|
|
@@ -541,15 +575,16 @@ Two special member functions are of the same kind if:
|
|
| 541 |
An *eligible special member function* is a special member function for
|
| 542 |
which:
|
| 543 |
|
| 544 |
- the function is not deleted,
|
| 545 |
- the associated constraints [[temp.constr]], if any, are satisfied, and
|
| 546 |
-
- no special member function of the same kind
|
|
|
|
| 547 |
[[temp.constr.order]].
|
| 548 |
|
| 549 |
-
For a class, its non-static data members, its non-virtual direct
|
| 550 |
-
classes, and, if the class is not abstract [[class.abstract]], its
|
| 551 |
virtual base classes are called its *potentially constructed
|
| 552 |
subobjects*.
|
| 553 |
|
| 554 |
### Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 555 |
|
|
@@ -609,12 +644,10 @@ cprint( complex(7.8,1.2) );
|
|
| 609 |
— *end example*]
|
| 610 |
|
| 611 |
[*Note 2*: For initialization of objects of class type see
|
| 612 |
[[class.init]]. — *end note*]
|
| 613 |
|
| 614 |
-
An object created in this way is unnamed.
|
| 615 |
-
|
| 616 |
[*Note 3*: [[class.temporary]] describes the lifetime of temporary
|
| 617 |
objects. — *end note*]
|
| 618 |
|
| 619 |
[*Note 4*: Explicit constructor calls do not yield lvalues, see
|
| 620 |
[[basic.lval]]. — *end note*]
|
|
@@ -640,79 +673,81 @@ A constructor shall not have an explicit object parameter [[dcl.fct]].
|
|
| 640 |
#### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
|
| 641 |
|
| 642 |
A *default constructor* for a class `X` is a constructor of class `X`
|
| 643 |
for which each parameter that is not a function parameter pack has a
|
| 644 |
default argument (including the case of a constructor with no
|
| 645 |
-
parameters). If there is no user-declared constructor
|
| 646 |
-
non-explicit constructor having no parameters
|
| 647 |
-
defaulted [[dcl.fct.def]]. An
|
| 648 |
-
an inline public member of
|
|
|
|
| 649 |
|
| 650 |
-
A defaulted default constructor for class `X` is defined as deleted if
|
| 651 |
|
| 652 |
-
- `X` is a union that has a variant member with a non-trivial default
|
| 653 |
-
constructor and no variant member of `X` has a default member
|
| 654 |
-
initializer,
|
| 655 |
-
- `X` is a non-union class that has a variant member `M` with a
|
| 656 |
-
non-trivial default constructor and no variant member of the anonymous
|
| 657 |
-
union containing `M` has a default member initializer,
|
| 658 |
- any non-static data member with no default member initializer
|
| 659 |
[[class.mem]] is of reference type,
|
| 660 |
-
- any non-variant non-static data member of
|
| 661 |
-
|
| 662 |
-
const-default-constructible
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
-
|
| 666 |
-
|
| 667 |
-
- any potentially constructed subobject, except for a non-static data
|
| 668 |
-
member with a *brace-or-equal-initializer*, has class type `M` (or
|
| 669 |
-
array thereof) and either `M` has no default constructor or overload
|
| 670 |
resolution [[over.match]] as applied to find `M`’s corresponding
|
| 671 |
-
constructor
|
| 672 |
-
|
| 673 |
-
- any potentially constructed subobject has
|
| 674 |
-
|
| 675 |
-
constructor
|
|
|
|
| 676 |
|
| 677 |
-
A default constructor is *trivial* if it is not
|
|
|
|
| 678 |
|
| 679 |
-
-
|
| 680 |
-
|
| 681 |
-
- no non-static data member of
|
| 682 |
-
|
| 683 |
-
- all the direct base classes of
|
| 684 |
-
|
| 685 |
-
-
|
| 686 |
-
type (or array thereof), each such
|
| 687 |
-
constructor.
|
| 688 |
|
| 689 |
Otherwise, the default constructor is *non-trivial*.
|
| 690 |
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
*compound-statement*. If that user-written default constructor would be
|
| 696 |
-
ill-formed, the program is ill-formed.
|
| 697 |
-
constructor
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
non-static data members are implicitly defined.
|
| 702 |
|
| 703 |
-
[*Note
|
| 704 |
specification [[except.spec]]. An explicitly-defaulted definition might
|
| 705 |
have an implicit exception specification, see
|
| 706 |
[[dcl.fct.def]]. — *end note*]
|
| 707 |
|
| 708 |
-
[*Note
|
| 709 |
class object when no initializer is specified [[dcl.init.general]]. Such
|
| 710 |
-
a default constructor
|
| 711 |
[[class.access]]. — *end note*]
|
| 712 |
|
| 713 |
-
[*Note
|
| 714 |
constructors for base classes and non-static data members are called and
|
| 715 |
describes how arguments can be specified for the calls to these
|
| 716 |
constructors. — *end note*]
|
| 717 |
|
| 718 |
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
|
@@ -840,11 +875,11 @@ form
|
|
| 840 |
X::X(const X&)
|
| 841 |
```
|
| 842 |
|
| 843 |
if each potentially constructed subobject of a class type `M` (or array
|
| 844 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 845 |
-
`M&` or `const` `volatile` `M&`.[^
|
| 846 |
|
| 847 |
Otherwise, the implicitly-declared copy constructor will have the form
|
| 848 |
|
| 849 |
``` cpp
|
| 850 |
X::X(X&)
|
|
@@ -872,29 +907,28 @@ X::X(X&&)
|
|
| 872 |
|
| 873 |
An implicitly-declared copy/move constructor is an inline public member
|
| 874 |
of its class. A defaulted copy/move constructor for a class `X` is
|
| 875 |
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 876 |
|
| 877 |
-
- a potentially constructed subobject of type `M` (or
|
| 878 |
-
|
| 879 |
[[over.match]], as applied to find `M`’s corresponding constructor,
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
is deleted or inaccessible from the defaulted constructor, or,
|
| 886 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 887 |
type.
|
| 888 |
|
| 889 |
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 890 |
ignored by overload resolution [[over.match]], [[over.over]]. Such a
|
| 891 |
constructor would otherwise interfere with initialization from an rvalue
|
| 892 |
which can use the copy constructor instead. — *end note*]
|
| 893 |
|
| 894 |
A copy/move constructor for class `X` is trivial if it is not
|
| 895 |
-
user-provided and if
|
| 896 |
|
| 897 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 898 |
base classes [[class.mi]], and
|
| 899 |
- the constructor selected to copy/move each direct base class subobject
|
| 900 |
is trivial, and
|
|
@@ -906,13 +940,12 @@ otherwise the copy/move constructor is *non-trivial*.
|
|
| 906 |
|
| 907 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 908 |
implementation elided its odr-use
|
| 909 |
[[term.odr.use]], [[class.temporary]]. — *end note*]
|
| 910 |
|
| 911 |
-
|
| 912 |
-
constexpr
|
| 913 |
-
is `constexpr`.
|
| 914 |
|
| 915 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 916 |
defined, all non-user-provided copy/move constructors for its
|
| 917 |
potentially constructed subobjects are implicitly defined.
|
| 918 |
|
|
@@ -921,11 +954,11 @@ exception specification [[except.spec]]. — *end note*]
|
|
| 921 |
|
| 922 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 923 |
performs a memberwise copy/move of its bases and members.
|
| 924 |
|
| 925 |
[*Note 7*: Default member initializers of non-static data members are
|
| 926 |
-
ignored.
|
| 927 |
|
| 928 |
The order of initialization is the same as the order of initialization
|
| 929 |
of bases and members in a user-defined constructor (see
|
| 930 |
[[class.base.init]]). Let `x` be either the parameter of the constructor
|
| 931 |
or, for the move constructor, an xvalue referring to the parameter. Each
|
|
@@ -952,23 +985,20 @@ the lifetime of o begins before the copy is performed.
|
|
| 952 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 953 |
|
| 954 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 955 |
non-static non-template member function of class `X` with exactly one
|
| 956 |
non-object parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
|
| 957 |
-
`const volatile X&`.[^
|
| 958 |
|
| 959 |
-
[*Note 1*:
|
| 960 |
-
only one parameter; see [[over.ass]]. — *end note*]
|
| 961 |
-
|
| 962 |
-
[*Note 2*: More than one form of copy assignment operator can be
|
| 963 |
declared for a class. — *end note*]
|
| 964 |
|
| 965 |
-
[*Note
|
| 966 |
|
| 967 |
-
If a class `X` only has a copy assignment operator with a
|
| 968 |
-
type `X&`, an expression of type const `X` cannot be
|
| 969 |
-
object of type `X`.
|
| 970 |
|
| 971 |
[*Example 1*:
|
| 972 |
|
| 973 |
``` cpp
|
| 974 |
struct X {
|
|
@@ -1000,15 +1030,16 @@ X& X::operator=(const X&)
|
|
| 1000 |
```
|
| 1001 |
|
| 1002 |
if
|
| 1003 |
|
| 1004 |
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 1005 |
-
parameter is of type `const B&`, `const volatile B&`, or
|
|
|
|
| 1006 |
- for all the non-static data members of `X` that are of a class type
|
| 1007 |
`M` (or array thereof), each such class type has a copy assignment
|
| 1008 |
-
operator whose parameter is of type `const M&`,
|
| 1009 |
-
or `M`.[^
|
| 1010 |
|
| 1011 |
Otherwise, the implicitly-declared copy assignment operator will have
|
| 1012 |
the form
|
| 1013 |
|
| 1014 |
``` cpp
|
|
@@ -1018,14 +1049,11 @@ X& X::operator=(X&)
|
|
| 1018 |
A user-declared move assignment operator `X::operator=` is a non-static
|
| 1019 |
non-template member function of class `X` with exactly one non-object
|
| 1020 |
parameter of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 1021 |
`const volatile X&&`.
|
| 1022 |
|
| 1023 |
-
[*Note
|
| 1024 |
-
only one parameter; see [[over.ass]]. — *end note*]
|
| 1025 |
-
|
| 1026 |
-
[*Note 5*: More than one form of move assignment operator can be
|
| 1027 |
declared for a class. — *end note*]
|
| 1028 |
|
| 1029 |
If the definition of a class `X` does not explicitly declare a move
|
| 1030 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1031 |
only if
|
|
@@ -1072,39 +1100,37 @@ the return type `X&`. An implicitly-declared copy/move assignment
|
|
| 1072 |
operator is an inline public member of its class.
|
| 1073 |
|
| 1074 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 1075 |
deleted if `X` has:
|
| 1076 |
|
| 1077 |
-
- a
|
| 1078 |
-
|
| 1079 |
-
- a non-static data member of `const` non-class type (or array thereof),
|
| 1080 |
-
or
|
| 1081 |
- a non-static data member of reference type, or
|
| 1082 |
-
- a direct non-static data member of class type `M` (or
|
| 1083 |
-
or a direct base class `M` that cannot
|
| 1084 |
-
overload resolution [[over.match]], as applied
|
| 1085 |
-
corresponding assignment operator,
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
|
| 1089 |
-
[*Note
|
| 1090 |
deleted is ignored by overload resolution
|
| 1091 |
[[over.match]], [[over.over]]. — *end note*]
|
| 1092 |
|
| 1093 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1094 |
class if not declared by the user, a base class copy/move assignment
|
| 1095 |
operator is always hidden by the corresponding assignment operator of a
|
| 1096 |
-
derived class [[over.
|
| 1097 |
|
| 1098 |
-
[*Note
|
| 1099 |
assignment operator from a base class never suppresses the implicit
|
| 1100 |
declaration of an assignment operator of `C`, even if the base class
|
| 1101 |
assignment operator would be a copy or move assignment operator if
|
| 1102 |
declared as a member of `C`. — *end note*]
|
| 1103 |
|
| 1104 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1105 |
-
user-provided and if
|
| 1106 |
|
| 1107 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 1108 |
base classes [[class.mi]], and
|
| 1109 |
- the assignment operator selected to copy/move each direct base class
|
| 1110 |
subobject is trivial, and
|
|
@@ -1120,11 +1146,11 @@ operator is `constexpr`.
|
|
| 1120 |
Before the defaulted copy/move assignment operator for a class is
|
| 1121 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1122 |
for its direct base classes and its non-static data members are
|
| 1123 |
implicitly defined.
|
| 1124 |
|
| 1125 |
-
[*Note
|
| 1126 |
implied exception specification [[except.spec]]. — *end note*]
|
| 1127 |
|
| 1128 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 1129 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 1130 |
The direct base classes of `X` are assigned first, in the order of their
|
|
@@ -1199,12 +1225,12 @@ parentheses, and the *id-expression* has one of the following forms:
|
|
| 1199 |
`~`*class-name* and the *class-name* is the injected-class-name of the
|
| 1200 |
class nominated by the *nested-name-specifier*.
|
| 1201 |
|
| 1202 |
A prospective destructor shall take no arguments [[dcl.fct]]. Each
|
| 1203 |
*decl-specifier* of the *decl-specifier-seq* of a prospective destructor
|
| 1204 |
-
declaration (if any) shall be `friend`, `inline`, `virtual`,
|
| 1205 |
-
`constexpr`
|
| 1206 |
|
| 1207 |
If a class has no user-declared prospective destructor, a prospective
|
| 1208 |
destructor is implicitly declared as defaulted [[dcl.fct.def]]. An
|
| 1209 |
implicitly-declared prospective destructor is an inline public member of
|
| 1210 |
its class.
|
|
@@ -1237,33 +1263,39 @@ when the destructor for the most derived object [[intro.object]] starts.
|
|
| 1237 |
|
| 1238 |
[*Note 2*: A declaration of a destructor that does not have a
|
| 1239 |
*noexcept-specifier* has the same exception specification as if it had
|
| 1240 |
been implicitly declared [[except.spec]]. — *end note*]
|
| 1241 |
|
| 1242 |
-
A defaulted destructor for a class `X` is defined as deleted if
|
| 1243 |
|
| 1244 |
-
- `X` is a
|
| 1245 |
-
|
| 1246 |
-
|
| 1247 |
-
|
| 1248 |
-
|
| 1249 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1250 |
function results in an ambiguity or in a function that is deleted or
|
| 1251 |
inaccessible from the defaulted destructor.
|
| 1252 |
|
| 1253 |
-
A destructor is trivial if it is not user-provided and
|
|
|
|
| 1254 |
|
| 1255 |
- the destructor is not virtual,
|
| 1256 |
-
- all of the direct base classes of
|
| 1257 |
-
|
| 1258 |
-
|
| 1259 |
-
|
| 1260 |
|
| 1261 |
Otherwise, the destructor is *non-trivial*.
|
| 1262 |
|
| 1263 |
-
A defaulted destructor is a constexpr destructor
|
| 1264 |
-
constexpr-suitable [[dcl.constexpr]].
|
| 1265 |
|
| 1266 |
Before a defaulted destructor for a class is implicitly defined, all the
|
| 1267 |
non-user-provided destructors for its base classes and its non-static
|
| 1268 |
data members are implicitly defined.
|
| 1269 |
|
|
@@ -1276,17 +1308,18 @@ created in the program, the destructor shall be defined.
|
|
| 1276 |
during destruction; see [[class.cdtor]]. — *end note*]
|
| 1277 |
|
| 1278 |
After executing the body of the destructor and destroying any objects
|
| 1279 |
with automatic storage duration allocated within the body, a destructor
|
| 1280 |
for class `X` calls the destructors for `X`’s direct non-variant
|
| 1281 |
-
non-static data members, the destructors for
|
| 1282 |
-
base classes and, if `X` is the most derived
|
| 1283 |
-
its destructor calls the destructors for
|
| 1284 |
-
destructors are called as if they were
|
| 1285 |
-
that is, ignoring any possible virtual
|
| 1286 |
-
derived classes. Bases and members are
|
| 1287 |
-
the completion of their constructor
|
|
|
|
| 1288 |
|
| 1289 |
[*Note 4*: A `return` statement [[stmt.return]] in a destructor might
|
| 1290 |
not directly return to the caller; before transferring control to the
|
| 1291 |
caller, the destructors for the members and bases are
|
| 1292 |
called. — *end note*]
|
|
@@ -1332,11 +1365,11 @@ lookup fails or if the deallocation function has a deleted definition
|
|
| 1332 |
[*Note 6*: This assures that a deallocation function corresponding to
|
| 1333 |
the dynamic type of an object is available for the *delete-expression*
|
| 1334 |
[[class.free]]. — *end note*]
|
| 1335 |
|
| 1336 |
In an explicit destructor call, the destructor is specified by a `~`
|
| 1337 |
-
followed by a *type-name* or *
|
| 1338 |
destructor’s class type. The invocation of a destructor is subject to
|
| 1339 |
the usual rules for member functions [[class.mfct]]; that is, if the
|
| 1340 |
object is not of the destructor’s class type and not of a class derived
|
| 1341 |
from the destructor’s class type (including when the destructor is
|
| 1342 |
invoked via a null pointer value), the program has undefined behavior.
|
|
@@ -1379,11 +1412,13 @@ member function is not an explicit destructor call
|
|
| 1379 |
|
| 1380 |
Explicit calls of destructors are rarely needed. One use of such calls
|
| 1381 |
is for objects placed at specific addresses using a placement
|
| 1382 |
*new-expression*. Such use of explicit placement and destruction of
|
| 1383 |
objects can be necessary to cope with dedicated hardware resources and
|
| 1384 |
-
for writing memory management facilities.
|
|
|
|
|
|
|
| 1385 |
|
| 1386 |
``` cpp
|
| 1387 |
void* operator new(std::size_t, void* p) { return p; }
|
| 1388 |
struct X {
|
| 1389 |
X(int);
|
|
@@ -1397,17 +1432,19 @@ void g() { // rare, specialized use:
|
|
| 1397 |
f(p);
|
| 1398 |
p->X::~X(); // cleanup
|
| 1399 |
}
|
| 1400 |
```
|
| 1401 |
|
|
|
|
|
|
|
| 1402 |
— *end note*]
|
| 1403 |
|
| 1404 |
Once a destructor is invoked for an object, the object’s lifetime ends;
|
| 1405 |
the behavior is undefined if the destructor is invoked for an object
|
| 1406 |
whose lifetime has ended [[basic.life]].
|
| 1407 |
|
| 1408 |
-
[*Example
|
| 1409 |
duration is explicitly invoked, and the block is subsequently left in a
|
| 1410 |
manner that would ordinarily invoke implicit destruction of the object,
|
| 1411 |
the behavior is undefined. — *end example*]
|
| 1412 |
|
| 1413 |
[*Note 10*:
|
|
@@ -1441,11 +1478,11 @@ User-defined conversions are applied only where they are unambiguous
|
|
| 1441 |
[[class.member.lookup]], [[class.conv.fct]]. Conversions obey the access
|
| 1442 |
control rules [[class.access]]. Access control is applied after
|
| 1443 |
ambiguity resolution [[basic.lookup]].
|
| 1444 |
|
| 1445 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 1446 |
-
conversions in function calls
|
| 1447 |
|
| 1448 |
At most one user-defined conversion (constructor or conversion function)
|
| 1449 |
is implicitly applied to a single value.
|
| 1450 |
|
| 1451 |
[*Example 1*:
|
|
@@ -1468,11 +1505,11 @@ int c = X(a); // OK, a.operator X().operator int()
|
|
| 1468 |
|
| 1469 |
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 1470 |
|
| 1471 |
A constructor that is not explicit [[dcl.fct.spec]] specifies a
|
| 1472 |
conversion from the types of its parameters (if any) to the type of its
|
| 1473 |
-
class.
|
| 1474 |
|
| 1475 |
[*Example 1*:
|
| 1476 |
|
| 1477 |
``` cpp
|
| 1478 |
struct X {
|
|
@@ -1524,17 +1561,10 @@ Z a6 = { 3, 4 }; // error: no implicit conversion
|
|
| 1524 |
|
| 1525 |
— *end example*]
|
| 1526 |
|
| 1527 |
— *end note*]
|
| 1528 |
|
| 1529 |
-
A non-explicit copy/move constructor [[class.copy.ctor]] is a converting
|
| 1530 |
-
constructor.
|
| 1531 |
-
|
| 1532 |
-
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 1533 |
-
explicit constructor; it can be called for implicit type
|
| 1534 |
-
conversions. — *end note*]
|
| 1535 |
-
|
| 1536 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 1537 |
|
| 1538 |
``` bnf
|
| 1539 |
conversion-function-id:
|
| 1540 |
operator conversion-type-id
|
|
@@ -1553,39 +1583,38 @@ conversion-declarator:
|
|
| 1553 |
A declaration whose *declarator-id* has an *unqualified-id* that is a
|
| 1554 |
*conversion-function-id* declares a *conversion function*; its
|
| 1555 |
*declarator* shall be a function declarator [[dcl.fct]] of the form
|
| 1556 |
|
| 1557 |
``` bnf
|
| 1558 |
-
|
| 1559 |
-
ref-qualifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1560 |
```
|
| 1561 |
|
| 1562 |
-
where the *
|
| 1563 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 1564 |
parentheses, and the *id-expression* has one of the following forms:
|
| 1565 |
|
| 1566 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 1567 |
of a class or class template but is not a friend declaration
|
| 1568 |
[[class.friend]], the *id-expression* is a *conversion-function-id*;
|
| 1569 |
- otherwise, the *id-expression* is a *qualified-id* whose
|
| 1570 |
*unqualified-id* is a *conversion-function-id*.
|
| 1571 |
|
| 1572 |
A conversion function shall have no non-object parameters and shall be a
|
| 1573 |
-
non-static member function of a class or class template `X`;
|
| 1574 |
-
|
| 1575 |
-
|
| 1576 |
-
*
|
| 1577 |
-
(if any) shall not be a
|
|
|
|
| 1578 |
|
| 1579 |
-
|
| 1580 |
-
|
| 1581 |
-
|
| 1582 |
-
|
| 1583 |
-
|
| 1584 |
-
|
| 1585 |
-
|
| 1586 |
-
reference to it), or to cv `void`.[^6]
|
| 1587 |
|
| 1588 |
[*Example 1*:
|
| 1589 |
|
| 1590 |
``` cpp
|
| 1591 |
struct X {
|
|
@@ -1637,11 +1666,11 @@ void g(X a, X b) {
|
|
| 1637 |
The *conversion-type-id* shall not represent a function type nor an
|
| 1638 |
array type. The *conversion-type-id* in a *conversion-function-id* is
|
| 1639 |
the longest sequence of tokens that could possibly form a
|
| 1640 |
*conversion-type-id*.
|
| 1641 |
|
| 1642 |
-
[*Note
|
| 1643 |
|
| 1644 |
This prevents ambiguities between the declarator operator `*` and its
|
| 1645 |
expression counterparts.
|
| 1646 |
|
| 1647 |
[*Example 3*:
|
|
@@ -1666,11 +1695,11 @@ operator int [[noreturn]] (); // error: noreturn attribute applied to a type
|
|
| 1666 |
|
| 1667 |
— *end example*]
|
| 1668 |
|
| 1669 |
— *end note*]
|
| 1670 |
|
| 1671 |
-
[*Note
|
| 1672 |
|
| 1673 |
A conversion function in a derived class hides only conversion functions
|
| 1674 |
in base classes that convert to the same type. A conversion function
|
| 1675 |
template with a dependent return type hides only templates in base
|
| 1676 |
classes that correspond to it [[class.member.lookup]]; otherwise, it
|
|
@@ -1965,11 +1994,13 @@ cannot be virtual.
|
|
| 1965 |
[*Note 1*:
|
| 1966 |
|
| 1967 |
However, when the *cast-expression* of a *delete-expression* refers to
|
| 1968 |
an object of class type with a virtual destructor, because the
|
| 1969 |
deallocation function is chosen by the destructor of the dynamic type of
|
| 1970 |
-
the object, the effect is the same in that case.
|
|
|
|
|
|
|
| 1971 |
|
| 1972 |
``` cpp
|
| 1973 |
struct B {
|
| 1974 |
virtual ~B();
|
| 1975 |
void operator delete(void*, std::size_t);
|
|
@@ -1999,17 +2030,21 @@ void f() {
|
|
| 1999 |
Here, storage for the object of class `D` is deallocated by
|
| 2000 |
`D::operator delete()`, and the object of class `E` is destroyed and its
|
| 2001 |
storage is deallocated by `E::operator delete()`, due to the virtual
|
| 2002 |
destructor.
|
| 2003 |
|
|
|
|
|
|
|
| 2004 |
— *end note*]
|
| 2005 |
|
| 2006 |
[*Note 2*:
|
| 2007 |
|
| 2008 |
Virtual destructors have no effect on the deallocation function actually
|
| 2009 |
called when the *cast-expression* of a *delete-expression* refers to an
|
| 2010 |
-
array of objects of class type.
|
|
|
|
|
|
|
| 2011 |
|
| 2012 |
``` cpp
|
| 2013 |
struct B {
|
| 2014 |
virtual ~B();
|
| 2015 |
void operator delete[](void*, std::size_t);
|
|
@@ -2025,16 +2060,18 @@ void f(int i) {
|
|
| 2025 |
B* bp = new D[i];
|
| 2026 |
delete[] bp; // undefined behavior
|
| 2027 |
}
|
| 2028 |
```
|
| 2029 |
|
|
|
|
|
|
|
| 2030 |
— *end note*]
|
| 2031 |
|
| 2032 |
Access to the deallocation function is checked statically, even if a
|
| 2033 |
different one is actually executed.
|
| 2034 |
|
| 2035 |
-
[*Example
|
| 2036 |
`B::operator delete()` had been private, the delete expression would
|
| 2037 |
have been ill-formed. — *end example*]
|
| 2038 |
|
| 2039 |
[*Note 3*: If a deallocation function has no explicit
|
| 2040 |
*noexcept-specifier*, it has a non-throwing exception specification
|
|
|
|
| 10 |
|
| 11 |
``` bnf
|
| 12 |
member-declaration:
|
| 13 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 14 |
function-definition
|
| 15 |
+
friend-type-declaration
|
| 16 |
using-declaration
|
| 17 |
using-enum-declaration
|
| 18 |
static_assert-declaration
|
| 19 |
+
consteval-block-declaration
|
| 20 |
template-declaration
|
| 21 |
explicit-specialization
|
| 22 |
deduction-guide
|
| 23 |
alias-declaration
|
| 24 |
opaque-enum-declaration
|
|
|
|
| 31 |
member-declarator-list ',' member-declarator
|
| 32 |
```
|
| 33 |
|
| 34 |
``` bnf
|
| 35 |
member-declarator:
|
| 36 |
+
declarator virt-specifier-seqₒₚₜ function-contract-specifier-seqₒₚₜ pure-specifierₒₚₜ
|
| 37 |
+
declarator requires-clause function-contract-specifier-seqₒₚₜ
|
| 38 |
+
declarator brace-or-equal-initializer
|
| 39 |
identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
|
| 40 |
```
|
| 41 |
|
| 42 |
``` bnf
|
| 43 |
virt-specifier-seq:
|
| 44 |
+
virt-specifier virt-specifier-seqₒₚₜ
|
|
|
|
| 45 |
```
|
| 46 |
|
| 47 |
``` bnf
|
| 48 |
virt-specifier:
|
| 49 |
override
|
|
|
|
| 53 |
``` bnf
|
| 54 |
pure-specifier:
|
| 55 |
'=' '0'
|
| 56 |
```
|
| 57 |
|
| 58 |
+
``` bnf
|
| 59 |
+
friend-type-declaration:
|
| 60 |
+
friend friend-type-specifier-list ';'
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
``` bnf
|
| 64 |
+
friend-type-specifier-list:
|
| 65 |
+
friend-type-specifier '...'ₒₚₜ
|
| 66 |
+
friend-type-specifier-list ',' friend-type-specifier '...'ₒₚₜ
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
``` bnf
|
| 70 |
+
friend-type-specifier:
|
| 71 |
+
simple-type-specifier
|
| 72 |
+
elaborated-type-specifier
|
| 73 |
+
typename-specifier
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
In the absence of a *virt-specifier-seq*, the token sequence `= 0` is
|
| 77 |
+
treated as a *pure-specifier* if the type of the *declarator-id*
|
| 78 |
+
[[dcl.meaning.general]] is a function type, and is otherwise treated as
|
| 79 |
+
a *brace-or-equal-initializer*.
|
| 80 |
+
|
| 81 |
+
[*Note 1*: If the member declaration acquires a function type through
|
| 82 |
+
template instantiation, the program is ill-formed; see
|
| 83 |
+
[[temp.spec.general]]. — *end note*]
|
| 84 |
+
|
| 85 |
+
The optional *function-contract-specifier-seq* [[dcl.contract.func]] in
|
| 86 |
+
a *member-declarator* shall be present only if the *declarator* declares
|
| 87 |
+
a function.
|
| 88 |
+
|
| 89 |
The *member-specification* in a class definition declares the full set
|
| 90 |
of members of the class; no member can be added elsewhere. A
|
| 91 |
*direct member* of a class `X` is a member of `X` that was first
|
| 92 |
declared within the *member-specification* of `X`, including anonymous
|
| 93 |
union members [[class.union.anon]] and direct members thereof. Members
|
| 94 |
of a class are data members, member functions [[class.mfct]], nested
|
| 95 |
types, enumerators, and member templates [[temp.mem]] and
|
| 96 |
specializations thereof.
|
| 97 |
|
| 98 |
+
[*Note 2*: A specialization of a static data member template is a
|
| 99 |
static data member. A specialization of a member function template is a
|
| 100 |
member function. A specialization of a member class template is a nested
|
| 101 |
class. — *end note*]
|
| 102 |
|
| 103 |
+
A *member-declaration* does not itself declare new members of the class
|
| 104 |
+
if it is
|
| 105 |
|
| 106 |
- a friend declaration [[class.friend]],
|
| 107 |
- a *deduction-guide* [[temp.deduct.guide]],
|
| 108 |
- a *template-declaration* whose *declaration* is one of the above,
|
| 109 |
- a *static_assert-declaration*,
|
| 110 |
+
- a *consteval-block-declaration*,
|
| 111 |
- a *using-declaration* [[namespace.udecl]], or
|
| 112 |
- an *empty-declaration*.
|
| 113 |
|
| 114 |
For any other *member-declaration*, each declared entity that is not an
|
| 115 |
unnamed bit-field [[class.bit]] is a member of the class, and each such
|
| 116 |
*member-declaration* shall either declare at least one member name of
|
| 117 |
the class or declare at least one unnamed bit-field.
|
| 118 |
|
| 119 |
+
A *data member* is either a non-function member introduced by a
|
| 120 |
+
*member-declarator* or an anonymous union member. A *member function* is
|
| 121 |
+
a member that is a function. Nested types are classes
|
| 122 |
+
[[class.name]], [[class.nest]] and enumerations [[dcl.enum]] declared in
|
| 123 |
+
the class and arbitrary types declared as members by use of a typedef
|
| 124 |
+
declaration [[dcl.typedef]] or *alias-declaration*. The enumerators of
|
| 125 |
+
an unscoped enumeration [[dcl.enum]] defined in the class are members of
|
| 126 |
+
the class.
|
| 127 |
|
| 128 |
A data member or member function may be declared `static` in its
|
| 129 |
*member-declaration*, in which case it is a *static member* (see
|
| 130 |
[[class.static]]) (a *static data member* [[class.static.data]] or
|
| 131 |
*static member function* [[class.static.mfct]], respectively) of the
|
| 132 |
class. Any other data member or member function is a *non-static member*
|
| 133 |
(a *non-static data member* or *non-static member function*
|
| 134 |
[[class.mfct.non.static]], respectively).
|
| 135 |
|
| 136 |
+
Every object of class type has a unique member subobject corresponding
|
| 137 |
+
to each of its direct non-static data members. If any non-static data
|
| 138 |
+
member of a class `C` is of reference type, then let `D` be an invented
|
| 139 |
+
class that is identical to `C` except that each non-static member of `D`
|
| 140 |
+
corresponding to a member of `C` of type “reference to `T`” instead has
|
| 141 |
+
type “pointer to `T`”. Every member subobject of a complete object of
|
| 142 |
+
type `C` has the same size, alignment, and offset as that of the
|
| 143 |
+
corresponding subobject of a complete object of type `D`. The size and
|
| 144 |
+
alignment of `C` are the same as the size and alignment of `D`.
|
| 145 |
|
| 146 |
A member shall not be declared twice in the *member-specification*,
|
| 147 |
except that
|
| 148 |
|
| 149 |
- a nested class or member class template can be declared and then later
|
|
|
|
| 163 |
A *complete-class context* of a class (template) is a
|
| 164 |
|
| 165 |
- function body [[dcl.fct.def.general]],
|
| 166 |
- default argument [[dcl.fct.default]],
|
| 167 |
- default template argument [[temp.param]],
|
| 168 |
+
- *noexcept-specifier* [[except.spec]],
|
| 169 |
+
- *function-contract-specifier* [[dcl.contract.func]], or
|
| 170 |
- default member initializer
|
| 171 |
|
| 172 |
within the *member-specification* of the class or class template.
|
| 173 |
|
| 174 |
[*Note 4*: A complete-class context of a nested class is also a
|
| 175 |
complete-class context of any enclosing class, if the nested class is
|
| 176 |
defined within the *member-specification* of the enclosing
|
| 177 |
class. — *end note*]
|
| 178 |
|
| 179 |
+
A class `C` is complete at a program point P if the definition of `C` is
|
| 180 |
+
reachable from P [[module.reach]] or if P is in a complete-class context
|
| 181 |
+
of `C`. Otherwise, `C` is incomplete at P.
|
| 182 |
+
|
| 183 |
+
If a *member-declaration* matches the syntactic requirements of
|
| 184 |
+
*friend-type-declaration*, it is a *friend-type-declaration*.
|
| 185 |
|
| 186 |
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 187 |
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 188 |
has function type, otherwise it is interpreted as introducing a
|
| 189 |
*brace-or-equal-initializer*.
|
|
|
|
| 313 |
name different from `T`:
|
| 314 |
|
| 315 |
- every static data member of class `T`;
|
| 316 |
- every member function of class `T`; \[*Note 9*: This restriction does
|
| 317 |
not apply to constructors, which do not have names
|
| 318 |
+
[[class.ctor]]. — *end note*]
|
| 319 |
- every member of class `T` that is itself a type;
|
| 320 |
- every member template of class `T`;
|
| 321 |
- every enumerator of every member of class `T` that is an unscoped
|
| 322 |
enumeration type; and
|
| 323 |
- every member of every anonymous union that is a member of class `T`.
|
|
|
|
| 332 |
in each of the structs, such that
|
| 333 |
|
| 334 |
- corresponding entities have layout-compatible types [[basic.types]],
|
| 335 |
- corresponding entities have the same alignment requirements
|
| 336 |
[[basic.align]],
|
| 337 |
+
- if a *has-attribute-expression* [[cpp.cond]] is not `0` for the
|
| 338 |
+
`no_unique_address` attribute, then neither entity is declared with
|
| 339 |
+
the `no_unique_address` attribute [[dcl.attr.nouniqueaddr]], and
|
| 340 |
- either both entities are bit-fields with the same width or neither is
|
| 341 |
a bit-field.
|
| 342 |
|
| 343 |
[*Example 4*:
|
| 344 |
|
|
|
|
| 401 |
|
| 402 |
[*Note 12*: The object and its first subobject are
|
| 403 |
pointer-interconvertible
|
| 404 |
[[basic.compound]], [[expr.static.cast]]. — *end note*]
|
| 405 |
|
| 406 |
+
A *data member description* is a quintuple (T, N, A, W, NUA) describing
|
| 407 |
+
the potential declaration of a non-static data member where
|
| 408 |
+
|
| 409 |
+
- T is a type,
|
| 410 |
+
- N is an *identifier* or $\bot$,
|
| 411 |
+
- A is an alignment or $\bot$,
|
| 412 |
+
- W is a bit-field width or $\bot$, and
|
| 413 |
+
- NUA is a boolean value.
|
| 414 |
+
|
| 415 |
+
Two data member descriptions are equal if each of their respective
|
| 416 |
+
components are the same entities, are the same identifiers, have equal
|
| 417 |
+
values, or are both $\bot$.
|
| 418 |
+
|
| 419 |
+
[*Note 13*:
|
| 420 |
+
|
| 421 |
+
The components of a data member description describe a data member such
|
| 422 |
+
that
|
| 423 |
+
|
| 424 |
+
- its type is specified using the type given by T,
|
| 425 |
+
- it is declared with the name given by N if N is not $\bot$ and is
|
| 426 |
+
otherwise unnamed,
|
| 427 |
+
- it is declared with the *alignment-specifier* [[dcl.align]] given by
|
| 428 |
+
`alignas(A)` if A is not $\bot$ and is otherwise declared without an
|
| 429 |
+
*alignment-specifier*,
|
| 430 |
+
- it is a bit-field [[class.bit]] with the width given by W if W is not
|
| 431 |
+
$\bot$ and is otherwise not a bit-field, and
|
| 432 |
+
- it is declared with the attribute `[[no_unique_address]]`
|
| 433 |
+
[[dcl.attr.nouniqueaddr]] if NUA is true and is otherwise declared
|
| 434 |
+
without that attribute.
|
| 435 |
+
|
| 436 |
+
Data member descriptions are represented by reflections
|
| 437 |
+
[[basic.fundamental]] returned by `std::meta::data_member_spec`
|
| 438 |
+
[[meta.reflection.define.aggregate]] and can be reified as data members
|
| 439 |
+
of a class using `std::meta::define_aggregate`.
|
| 440 |
+
|
| 441 |
+
— *end note*]
|
| 442 |
+
|
| 443 |
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 444 |
|
| 445 |
If a member function is attached to the global module and is defined
|
| 446 |
[[dcl.fct.def]] in its class definition, it is inline [[dcl.inline]].
|
| 447 |
|
|
|
|
| 474 |
[*Note 2*:
|
| 475 |
|
| 476 |
A member function can be declared (but not defined) using a typedef for
|
| 477 |
a function type. The resulting member function has exactly the same type
|
| 478 |
as it would have if the function declarator were provided explicitly,
|
| 479 |
+
see [[dcl.fct]] and [[temp.arg]].
|
| 480 |
+
|
| 481 |
+
[*Example 2*:
|
| 482 |
|
| 483 |
``` cpp
|
| 484 |
typedef void fv();
|
| 485 |
typedef void fvc() const;
|
| 486 |
struct S {
|
|
|
|
| 491 |
fv S::* pmfv1 = &S::memfunc1;
|
| 492 |
fv S::* pmfv2 = &S::memfunc2;
|
| 493 |
fvc S::* pmfv3 = &S::memfunc3;
|
| 494 |
```
|
| 495 |
|
| 496 |
+
— *end example*]
|
| 497 |
|
| 498 |
— *end note*]
|
| 499 |
|
| 500 |
### Non-static member functions <a id="class.mfct.non.static">[[class.mfct.non.static]]</a>
|
| 501 |
|
|
|
|
| 505 |
[[expr.ref]], [[over.match.call]]. A non-static member function may also
|
| 506 |
be called directly using the function call syntax
|
| 507 |
[[expr.call]], [[over.match.call]] from within its class or a class
|
| 508 |
derived from its class, or a member thereof, as described below.
|
| 509 |
|
| 510 |
+
[*Note 1*: An implicit object member function can be declared with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
*cv-qualifier*s, which affect the type of the `this` pointer
|
| 512 |
[[expr.prim.this]], and/or a *ref-qualifier* [[dcl.fct]]; both affect
|
| 513 |
+
overload resolution [[over.match.funcs]]. — *end note*]
|
| 514 |
|
| 515 |
An implicit object member function may be declared virtual
|
| 516 |
[[class.virtual]] or pure virtual [[class.abstract]].
|
| 517 |
|
| 518 |
### Special member functions <a id="special">[[special]]</a>
|
|
|
|
| 561 |
|
| 562 |
[*Example 2*: Declaring a constructor protected ensures that only
|
| 563 |
derived classes and friends can create objects using
|
| 564 |
it. — *end example*]
|
| 565 |
|
| 566 |
+
Two special member functions are of the same kind if
|
| 567 |
|
| 568 |
- they are both default constructors,
|
| 569 |
- they are both copy or move constructors with the same first parameter
|
| 570 |
type, or
|
| 571 |
- they are both copy or move assignment operators with the same first
|
|
|
|
| 575 |
An *eligible special member function* is a special member function for
|
| 576 |
which:
|
| 577 |
|
| 578 |
- the function is not deleted,
|
| 579 |
- the associated constraints [[temp.constr]], if any, are satisfied, and
|
| 580 |
+
- no special member function of the same kind whose associated
|
| 581 |
+
constraints, if any, are satisfied is more constrained
|
| 582 |
[[temp.constr.order]].
|
| 583 |
|
| 584 |
+
For a class, its direct non-static data members, its non-virtual direct
|
| 585 |
+
base classes, and, if the class is not abstract [[class.abstract]], its
|
| 586 |
virtual base classes are called its *potentially constructed
|
| 587 |
subobjects*.
|
| 588 |
|
| 589 |
### Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 590 |
|
|
|
|
| 644 |
— *end example*]
|
| 645 |
|
| 646 |
[*Note 2*: For initialization of objects of class type see
|
| 647 |
[[class.init]]. — *end note*]
|
| 648 |
|
|
|
|
|
|
|
| 649 |
[*Note 3*: [[class.temporary]] describes the lifetime of temporary
|
| 650 |
objects. — *end note*]
|
| 651 |
|
| 652 |
[*Note 4*: Explicit constructor calls do not yield lvalues, see
|
| 653 |
[[basic.lval]]. — *end note*]
|
|
|
|
| 673 |
#### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
|
| 674 |
|
| 675 |
A *default constructor* for a class `X` is a constructor of class `X`
|
| 676 |
for which each parameter that is not a function parameter pack has a
|
| 677 |
default argument (including the case of a constructor with no
|
| 678 |
+
parameters). If there is no user-declared constructor or constructor
|
| 679 |
+
template for class `X`, a non-explicit constructor having no parameters
|
| 680 |
+
is implicitly declared as defaulted [[dcl.fct.def]]. An
|
| 681 |
+
implicitly-declared default constructor is an inline public member of
|
| 682 |
+
its class.
|
| 683 |
|
| 684 |
+
A defaulted default constructor for class `X` is defined as deleted if
|
| 685 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
- any non-static data member with no default member initializer
|
| 687 |
[[class.mem]] is of reference type,
|
| 688 |
+
- `X` is a non-union class and any non-variant non-static data member of
|
| 689 |
+
const-qualified type (or possibly multidimensional array thereof) with
|
| 690 |
+
no *brace-or-equal-initializer* is not const-default-constructible
|
| 691 |
+
[[dcl.init]],
|
| 692 |
+
- any non-variant potentially constructed subobject, except for a
|
| 693 |
+
non-static data member with a *brace-or-equal-initializer*, has class
|
| 694 |
+
type `M` (or possibly multidimensional array thereof) and overload
|
|
|
|
|
|
|
|
|
|
| 695 |
resolution [[over.match]] as applied to find `M`’s corresponding
|
| 696 |
+
constructor does not result in a usable candidate
|
| 697 |
+
[[over.match.general]], or
|
| 698 |
+
- any potentially constructed subobject S has class type `M` (or
|
| 699 |
+
possibly multidimensional array thereof), `M` has a destructor that is
|
| 700 |
+
deleted or inaccessible from the defaulted default constructor, and
|
| 701 |
+
either S is non-variant or S has a default member initializer.
|
| 702 |
|
| 703 |
+
A default constructor for a class `X` is *trivial* if it is not
|
| 704 |
+
user-provided and if
|
| 705 |
|
| 706 |
+
- `X` has no virtual functions [[class.virtual]] and no virtual base
|
| 707 |
+
classes [[class.mi]], and
|
| 708 |
+
- no non-static data member of `X` has a default member initializer
|
| 709 |
+
[[class.mem]], and
|
| 710 |
+
- all the direct base classes of `X` have trivial default constructors,
|
| 711 |
+
and
|
| 712 |
+
- either `X` is a union or for all the non-variant non-static data
|
| 713 |
+
members of `X` that are of class type (or array thereof), each such
|
| 714 |
+
class has a trivial default constructor.
|
| 715 |
|
| 716 |
Otherwise, the default constructor is *non-trivial*.
|
| 717 |
|
| 718 |
+
If a default constructor of a union-like class `X` is trivial, then for
|
| 719 |
+
each union `U` that is either `X` or an anonymous union member of `X`,
|
| 720 |
+
if the first variant member, if any, of `U` has implicit-lifetime type
|
| 721 |
+
[[basic.types.general]], the default constructor of `X` begins the
|
| 722 |
+
lifetime of that member if it is not the active member of its union.
|
| 723 |
+
|
| 724 |
+
[*Note 1*: It is already the active member if `U` was
|
| 725 |
+
value-initialized. — *end note*]
|
| 726 |
+
|
| 727 |
+
Otherwise, an implicitly-defined [[dcl.fct.def.default]] default
|
| 728 |
+
constructor performs the set of initializations of the class that would
|
| 729 |
+
be performed by a user-written default constructor for that class with
|
| 730 |
+
no *ctor-initializer* [[class.base.init]] and an empty
|
| 731 |
*compound-statement*. If that user-written default constructor would be
|
| 732 |
+
ill-formed, the program is ill-formed. The implicitly-defined default
|
| 733 |
+
constructor is `constexpr`. Before the defaulted default constructor for
|
| 734 |
+
a class is implicitly defined, all the non-user-provided default
|
| 735 |
+
constructors for its base classes and its non-static data members are
|
| 736 |
+
implicitly defined.
|
|
|
|
| 737 |
|
| 738 |
+
[*Note 2*: An implicitly-declared default constructor has an exception
|
| 739 |
specification [[except.spec]]. An explicitly-defaulted definition might
|
| 740 |
have an implicit exception specification, see
|
| 741 |
[[dcl.fct.def]]. — *end note*]
|
| 742 |
|
| 743 |
+
[*Note 3*: A default constructor is implicitly invoked to initialize a
|
| 744 |
class object when no initializer is specified [[dcl.init.general]]. Such
|
| 745 |
+
a default constructor needs to be accessible
|
| 746 |
[[class.access]]. — *end note*]
|
| 747 |
|
| 748 |
+
[*Note 4*: [[class.base.init]] describes the order in which
|
| 749 |
constructors for base classes and non-static data members are called and
|
| 750 |
describes how arguments can be specified for the calls to these
|
| 751 |
constructors. — *end note*]
|
| 752 |
|
| 753 |
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
|
|
|
| 875 |
X::X(const X&)
|
| 876 |
```
|
| 877 |
|
| 878 |
if each potentially constructed subobject of a class type `M` (or array
|
| 879 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 880 |
+
`M&` or `const` `volatile` `M&`.[^2]
|
| 881 |
|
| 882 |
Otherwise, the implicitly-declared copy constructor will have the form
|
| 883 |
|
| 884 |
``` cpp
|
| 885 |
X::X(X&)
|
|
|
|
| 907 |
|
| 908 |
An implicitly-declared copy/move constructor is an inline public member
|
| 909 |
of its class. A defaulted copy/move constructor for a class `X` is
|
| 910 |
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 911 |
|
| 912 |
+
- a potentially constructed subobject of type `M` (or possibly
|
| 913 |
+
multidimensional array thereof) for which overload resolution
|
| 914 |
[[over.match]], as applied to find `M`’s corresponding constructor,
|
| 915 |
+
either does not result in a usable candidate [[over.match.general]]
|
| 916 |
+
or, in the case of a variant member, selects a non-trivial function,
|
| 917 |
+
- any potentially constructed subobject of class type `M` (or possibly
|
| 918 |
+
multidimensional array thereof) where `M` has a destructor that is
|
| 919 |
+
deleted or inaccessible from the defaulted constructor, or,
|
|
|
|
| 920 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 921 |
type.
|
| 922 |
|
| 923 |
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 924 |
ignored by overload resolution [[over.match]], [[over.over]]. Such a
|
| 925 |
constructor would otherwise interfere with initialization from an rvalue
|
| 926 |
which can use the copy constructor instead. — *end note*]
|
| 927 |
|
| 928 |
A copy/move constructor for class `X` is trivial if it is not
|
| 929 |
+
user-provided and if
|
| 930 |
|
| 931 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 932 |
base classes [[class.mi]], and
|
| 933 |
- the constructor selected to copy/move each direct base class subobject
|
| 934 |
is trivial, and
|
|
|
|
| 940 |
|
| 941 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 942 |
implementation elided its odr-use
|
| 943 |
[[term.odr.use]], [[class.temporary]]. — *end note*]
|
| 944 |
|
| 945 |
+
The implicitly-defined [[dcl.fct.def.default]] constructor is
|
| 946 |
+
`constexpr`.
|
|
|
|
| 947 |
|
| 948 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 949 |
defined, all non-user-provided copy/move constructors for its
|
| 950 |
potentially constructed subobjects are implicitly defined.
|
| 951 |
|
|
|
|
| 954 |
|
| 955 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 956 |
performs a memberwise copy/move of its bases and members.
|
| 957 |
|
| 958 |
[*Note 7*: Default member initializers of non-static data members are
|
| 959 |
+
ignored. — *end note*]
|
| 960 |
|
| 961 |
The order of initialization is the same as the order of initialization
|
| 962 |
of bases and members in a user-defined constructor (see
|
| 963 |
[[class.base.init]]). Let `x` be either the parameter of the constructor
|
| 964 |
or, for the move constructor, an xvalue referring to the parameter. Each
|
|
|
|
| 985 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 986 |
|
| 987 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 988 |
non-static non-template member function of class `X` with exactly one
|
| 989 |
non-object parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
|
| 990 |
+
`const volatile X&`.[^3]
|
| 991 |
|
| 992 |
+
[*Note 1*: More than one form of copy assignment operator can be
|
|
|
|
|
|
|
|
|
|
| 993 |
declared for a class. — *end note*]
|
| 994 |
|
| 995 |
+
[*Note 2*:
|
| 996 |
|
| 997 |
+
If a class `X` only has a copy assignment operator with a non-object
|
| 998 |
+
parameter of type `X&`, an expression of type const `X` cannot be
|
| 999 |
+
assigned to an object of type `X`.
|
| 1000 |
|
| 1001 |
[*Example 1*:
|
| 1002 |
|
| 1003 |
``` cpp
|
| 1004 |
struct X {
|
|
|
|
| 1030 |
```
|
| 1031 |
|
| 1032 |
if
|
| 1033 |
|
| 1034 |
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 1035 |
+
non-object parameter is of type `const B&`, `const volatile B&`, or
|
| 1036 |
+
`B`, and
|
| 1037 |
- for all the non-static data members of `X` that are of a class type
|
| 1038 |
`M` (or array thereof), each such class type has a copy assignment
|
| 1039 |
+
operator whose non-object parameter is of type `const M&`,
|
| 1040 |
+
`const volatile M&`, or `M`.[^4]
|
| 1041 |
|
| 1042 |
Otherwise, the implicitly-declared copy assignment operator will have
|
| 1043 |
the form
|
| 1044 |
|
| 1045 |
``` cpp
|
|
|
|
| 1049 |
A user-declared move assignment operator `X::operator=` is a non-static
|
| 1050 |
non-template member function of class `X` with exactly one non-object
|
| 1051 |
parameter of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 1052 |
`const volatile X&&`.
|
| 1053 |
|
| 1054 |
+
[*Note 3*: More than one form of move assignment operator can be
|
|
|
|
|
|
|
|
|
|
| 1055 |
declared for a class. — *end note*]
|
| 1056 |
|
| 1057 |
If the definition of a class `X` does not explicitly declare a move
|
| 1058 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1059 |
only if
|
|
|
|
| 1100 |
operator is an inline public member of its class.
|
| 1101 |
|
| 1102 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 1103 |
deleted if `X` has:
|
| 1104 |
|
| 1105 |
+
- a non-static data member of `const` non-class type (or possibly
|
| 1106 |
+
multidimensional array thereof), or
|
|
|
|
|
|
|
| 1107 |
- a non-static data member of reference type, or
|
| 1108 |
+
- a direct non-static data member of class type `M` (or possibly
|
| 1109 |
+
multidimensional array thereof) or a direct base class `M` that cannot
|
| 1110 |
+
be copied/moved because overload resolution [[over.match]], as applied
|
| 1111 |
+
to find `M`’s corresponding assignment operator, either does not
|
| 1112 |
+
result in a usable candidate [[over.match.general]] or, in the case of
|
| 1113 |
+
a variant member, selects a non-trivial function.
|
| 1114 |
|
| 1115 |
+
[*Note 4*: A defaulted move assignment operator that is defined as
|
| 1116 |
deleted is ignored by overload resolution
|
| 1117 |
[[over.match]], [[over.over]]. — *end note*]
|
| 1118 |
|
| 1119 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1120 |
class if not declared by the user, a base class copy/move assignment
|
| 1121 |
operator is always hidden by the corresponding assignment operator of a
|
| 1122 |
+
derived class [[over.assign]].
|
| 1123 |
|
| 1124 |
+
[*Note 5*: A *using-declaration* in a derived class `C` that names an
|
| 1125 |
assignment operator from a base class never suppresses the implicit
|
| 1126 |
declaration of an assignment operator of `C`, even if the base class
|
| 1127 |
assignment operator would be a copy or move assignment operator if
|
| 1128 |
declared as a member of `C`. — *end note*]
|
| 1129 |
|
| 1130 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1131 |
+
user-provided and if
|
| 1132 |
|
| 1133 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 1134 |
base classes [[class.mi]], and
|
| 1135 |
- the assignment operator selected to copy/move each direct base class
|
| 1136 |
subobject is trivial, and
|
|
|
|
| 1146 |
Before the defaulted copy/move assignment operator for a class is
|
| 1147 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1148 |
for its direct base classes and its non-static data members are
|
| 1149 |
implicitly defined.
|
| 1150 |
|
| 1151 |
+
[*Note 6*: An implicitly-declared copy/move assignment operator has an
|
| 1152 |
implied exception specification [[except.spec]]. — *end note*]
|
| 1153 |
|
| 1154 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 1155 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 1156 |
The direct base classes of `X` are assigned first, in the order of their
|
|
|
|
| 1225 |
`~`*class-name* and the *class-name* is the injected-class-name of the
|
| 1226 |
class nominated by the *nested-name-specifier*.
|
| 1227 |
|
| 1228 |
A prospective destructor shall take no arguments [[dcl.fct]]. Each
|
| 1229 |
*decl-specifier* of the *decl-specifier-seq* of a prospective destructor
|
| 1230 |
+
declaration (if any) shall be `friend`, `inline`, `virtual`, or
|
| 1231 |
+
`constexpr`.
|
| 1232 |
|
| 1233 |
If a class has no user-declared prospective destructor, a prospective
|
| 1234 |
destructor is implicitly declared as defaulted [[dcl.fct.def]]. An
|
| 1235 |
implicitly-declared prospective destructor is an inline public member of
|
| 1236 |
its class.
|
|
|
|
| 1263 |
|
| 1264 |
[*Note 2*: A declaration of a destructor that does not have a
|
| 1265 |
*noexcept-specifier* has the same exception specification as if it had
|
| 1266 |
been implicitly declared [[except.spec]]. — *end note*]
|
| 1267 |
|
| 1268 |
+
A defaulted destructor for a class `X` is defined as deleted if
|
| 1269 |
|
| 1270 |
+
- `X` is a non-union class and any non-variant potentially constructed
|
| 1271 |
+
subobject has class type `M` (or possibly multidimensional array
|
| 1272 |
+
thereof) where `M` has a destructor that is deleted or is inaccessible
|
| 1273 |
+
from the defaulted destructor,
|
| 1274 |
+
- `X` is a union and
|
| 1275 |
+
- overload resolution to select a constructor to default-initialize an
|
| 1276 |
+
object of type `X` either fails or selects a constructor that is
|
| 1277 |
+
either deleted or not trivial, or
|
| 1278 |
+
- `X` has a variant member `V` of class type `M` (or possibly
|
| 1279 |
+
multi-dimensional array thereof) where `V` has a default member
|
| 1280 |
+
initializer and `M` has a destructor that is non-trivial, or,
|
| 1281 |
+
- for a virtual destructor, lookup of the non-array deallocation
|
| 1282 |
function results in an ambiguity or in a function that is deleted or
|
| 1283 |
inaccessible from the defaulted destructor.
|
| 1284 |
|
| 1285 |
+
A destructor for a class `X` is trivial if it is not user-provided and
|
| 1286 |
+
if
|
| 1287 |
|
| 1288 |
- the destructor is not virtual,
|
| 1289 |
+
- all of the direct base classes of `X` have trivial destructors, and
|
| 1290 |
+
- either `X` is a union or for all of the non-variant non-static data
|
| 1291 |
+
members of `X` that are of class type (or array thereof), each such
|
| 1292 |
+
class has a trivial destructor.
|
| 1293 |
|
| 1294 |
Otherwise, the destructor is *non-trivial*.
|
| 1295 |
|
| 1296 |
+
A defaulted destructor is a constexpr destructor.
|
|
|
|
| 1297 |
|
| 1298 |
Before a defaulted destructor for a class is implicitly defined, all the
|
| 1299 |
non-user-provided destructors for its base classes and its non-static
|
| 1300 |
data members are implicitly defined.
|
| 1301 |
|
|
|
|
| 1308 |
during destruction; see [[class.cdtor]]. — *end note*]
|
| 1309 |
|
| 1310 |
After executing the body of the destructor and destroying any objects
|
| 1311 |
with automatic storage duration allocated within the body, a destructor
|
| 1312 |
for class `X` calls the destructors for `X`’s direct non-variant
|
| 1313 |
+
non-static data members other than anonymous unions, the destructors for
|
| 1314 |
+
`X`’s non-virtual direct base classes and, if `X` is the most derived
|
| 1315 |
+
class [[class.base.init]], its destructor calls the destructors for
|
| 1316 |
+
`X`’s virtual base classes. All destructors are called as if they were
|
| 1317 |
+
referenced with a qualified name, that is, ignoring any possible virtual
|
| 1318 |
+
overriding destructors in more derived classes. Bases and members are
|
| 1319 |
+
destroyed in the reverse order of the completion of their constructor
|
| 1320 |
+
(see [[class.base.init]]).
|
| 1321 |
|
| 1322 |
[*Note 4*: A `return` statement [[stmt.return]] in a destructor might
|
| 1323 |
not directly return to the caller; before transferring control to the
|
| 1324 |
caller, the destructors for the members and bases are
|
| 1325 |
called. — *end note*]
|
|
|
|
| 1365 |
[*Note 6*: This assures that a deallocation function corresponding to
|
| 1366 |
the dynamic type of an object is available for the *delete-expression*
|
| 1367 |
[[class.free]]. — *end note*]
|
| 1368 |
|
| 1369 |
In an explicit destructor call, the destructor is specified by a `~`
|
| 1370 |
+
followed by a *type-name* or *computed-type-specifier* that denotes the
|
| 1371 |
destructor’s class type. The invocation of a destructor is subject to
|
| 1372 |
the usual rules for member functions [[class.mfct]]; that is, if the
|
| 1373 |
object is not of the destructor’s class type and not of a class derived
|
| 1374 |
from the destructor’s class type (including when the destructor is
|
| 1375 |
invoked via a null pointer value), the program has undefined behavior.
|
|
|
|
| 1412 |
|
| 1413 |
Explicit calls of destructors are rarely needed. One use of such calls
|
| 1414 |
is for objects placed at specific addresses using a placement
|
| 1415 |
*new-expression*. Such use of explicit placement and destruction of
|
| 1416 |
objects can be necessary to cope with dedicated hardware resources and
|
| 1417 |
+
for writing memory management facilities.
|
| 1418 |
+
|
| 1419 |
+
[*Example 2*:
|
| 1420 |
|
| 1421 |
``` cpp
|
| 1422 |
void* operator new(std::size_t, void* p) { return p; }
|
| 1423 |
struct X {
|
| 1424 |
X(int);
|
|
|
|
| 1432 |
f(p);
|
| 1433 |
p->X::~X(); // cleanup
|
| 1434 |
}
|
| 1435 |
```
|
| 1436 |
|
| 1437 |
+
— *end example*]
|
| 1438 |
+
|
| 1439 |
— *end note*]
|
| 1440 |
|
| 1441 |
Once a destructor is invoked for an object, the object’s lifetime ends;
|
| 1442 |
the behavior is undefined if the destructor is invoked for an object
|
| 1443 |
whose lifetime has ended [[basic.life]].
|
| 1444 |
|
| 1445 |
+
[*Example 3*: If the destructor for an object with automatic storage
|
| 1446 |
duration is explicitly invoked, and the block is subsequently left in a
|
| 1447 |
manner that would ordinarily invoke implicit destruction of the object,
|
| 1448 |
the behavior is undefined. — *end example*]
|
| 1449 |
|
| 1450 |
[*Note 10*:
|
|
|
|
| 1478 |
[[class.member.lookup]], [[class.conv.fct]]. Conversions obey the access
|
| 1479 |
control rules [[class.access]]. Access control is applied after
|
| 1480 |
ambiguity resolution [[basic.lookup]].
|
| 1481 |
|
| 1482 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 1483 |
+
conversions in function calls. — *end note*]
|
| 1484 |
|
| 1485 |
At most one user-defined conversion (constructor or conversion function)
|
| 1486 |
is implicitly applied to a single value.
|
| 1487 |
|
| 1488 |
[*Example 1*:
|
|
|
|
| 1505 |
|
| 1506 |
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 1507 |
|
| 1508 |
A constructor that is not explicit [[dcl.fct.spec]] specifies a
|
| 1509 |
conversion from the types of its parameters (if any) to the type of its
|
| 1510 |
+
class.
|
| 1511 |
|
| 1512 |
[*Example 1*:
|
| 1513 |
|
| 1514 |
``` cpp
|
| 1515 |
struct X {
|
|
|
|
| 1561 |
|
| 1562 |
— *end example*]
|
| 1563 |
|
| 1564 |
— *end note*]
|
| 1565 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1566 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 1567 |
|
| 1568 |
``` bnf
|
| 1569 |
conversion-function-id:
|
| 1570 |
operator conversion-type-id
|
|
|
|
| 1583 |
A declaration whose *declarator-id* has an *unqualified-id* that is a
|
| 1584 |
*conversion-function-id* declares a *conversion function*; its
|
| 1585 |
*declarator* shall be a function declarator [[dcl.fct]] of the form
|
| 1586 |
|
| 1587 |
``` bnf
|
| 1588 |
+
noptr-declarator parameters-and-qualifiers
|
|
|
|
| 1589 |
```
|
| 1590 |
|
| 1591 |
+
where the *noptr-declarator* consists solely of an *id-expression*, an
|
| 1592 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 1593 |
parentheses, and the *id-expression* has one of the following forms:
|
| 1594 |
|
| 1595 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 1596 |
of a class or class template but is not a friend declaration
|
| 1597 |
[[class.friend]], the *id-expression* is a *conversion-function-id*;
|
| 1598 |
- otherwise, the *id-expression* is a *qualified-id* whose
|
| 1599 |
*unqualified-id* is a *conversion-function-id*.
|
| 1600 |
|
| 1601 |
A conversion function shall have no non-object parameters and shall be a
|
| 1602 |
+
non-static member function of a class or class template `X`; its
|
| 1603 |
+
declared return type is the *conversion-type-id* and it specifies a
|
| 1604 |
+
conversion from `X` to the type specified by the *conversion-type-id*,
|
| 1605 |
+
interpreted as a *type-id* [[dcl.name]]. A *decl-specifier* in the
|
| 1606 |
+
*decl-specifier-seq* of a conversion function (if any) shall not be a
|
| 1607 |
+
*defining-type-specifier*.
|
| 1608 |
|
| 1609 |
+
[*Note 1*: A conversion function is never invoked for implicit or
|
| 1610 |
+
explicit conversions of an object to the same object type (or a
|
| 1611 |
+
reference to it), to a base class of that type (or a reference to it),
|
| 1612 |
+
or to cv `void`. Even though never directly called to perform a
|
| 1613 |
+
conversion, such conversion functions can be declared and can
|
| 1614 |
+
potentially be reached through a call to a virtual conversion function
|
| 1615 |
+
in a base class. — *end note*]
|
|
|
|
| 1616 |
|
| 1617 |
[*Example 1*:
|
| 1618 |
|
| 1619 |
``` cpp
|
| 1620 |
struct X {
|
|
|
|
| 1666 |
The *conversion-type-id* shall not represent a function type nor an
|
| 1667 |
array type. The *conversion-type-id* in a *conversion-function-id* is
|
| 1668 |
the longest sequence of tokens that could possibly form a
|
| 1669 |
*conversion-type-id*.
|
| 1670 |
|
| 1671 |
+
[*Note 2*:
|
| 1672 |
|
| 1673 |
This prevents ambiguities between the declarator operator `*` and its
|
| 1674 |
expression counterparts.
|
| 1675 |
|
| 1676 |
[*Example 3*:
|
|
|
|
| 1695 |
|
| 1696 |
— *end example*]
|
| 1697 |
|
| 1698 |
— *end note*]
|
| 1699 |
|
| 1700 |
+
[*Note 3*:
|
| 1701 |
|
| 1702 |
A conversion function in a derived class hides only conversion functions
|
| 1703 |
in base classes that convert to the same type. A conversion function
|
| 1704 |
template with a dependent return type hides only templates in base
|
| 1705 |
classes that correspond to it [[class.member.lookup]]; otherwise, it
|
|
|
|
| 1994 |
[*Note 1*:
|
| 1995 |
|
| 1996 |
However, when the *cast-expression* of a *delete-expression* refers to
|
| 1997 |
an object of class type with a virtual destructor, because the
|
| 1998 |
deallocation function is chosen by the destructor of the dynamic type of
|
| 1999 |
+
the object, the effect is the same in that case.
|
| 2000 |
+
|
| 2001 |
+
[*Example 3*:
|
| 2002 |
|
| 2003 |
``` cpp
|
| 2004 |
struct B {
|
| 2005 |
virtual ~B();
|
| 2006 |
void operator delete(void*, std::size_t);
|
|
|
|
| 2030 |
Here, storage for the object of class `D` is deallocated by
|
| 2031 |
`D::operator delete()`, and the object of class `E` is destroyed and its
|
| 2032 |
storage is deallocated by `E::operator delete()`, due to the virtual
|
| 2033 |
destructor.
|
| 2034 |
|
| 2035 |
+
— *end example*]
|
| 2036 |
+
|
| 2037 |
— *end note*]
|
| 2038 |
|
| 2039 |
[*Note 2*:
|
| 2040 |
|
| 2041 |
Virtual destructors have no effect on the deallocation function actually
|
| 2042 |
called when the *cast-expression* of a *delete-expression* refers to an
|
| 2043 |
+
array of objects of class type.
|
| 2044 |
+
|
| 2045 |
+
[*Example 4*:
|
| 2046 |
|
| 2047 |
``` cpp
|
| 2048 |
struct B {
|
| 2049 |
virtual ~B();
|
| 2050 |
void operator delete[](void*, std::size_t);
|
|
|
|
| 2060 |
B* bp = new D[i];
|
| 2061 |
delete[] bp; // undefined behavior
|
| 2062 |
}
|
| 2063 |
```
|
| 2064 |
|
| 2065 |
+
— *end example*]
|
| 2066 |
+
|
| 2067 |
— *end note*]
|
| 2068 |
|
| 2069 |
Access to the deallocation function is checked statically, even if a
|
| 2070 |
different one is actually executed.
|
| 2071 |
|
| 2072 |
+
[*Example 5*: For the call on line “// 1” above, if
|
| 2073 |
`B::operator delete()` had been private, the delete expression would
|
| 2074 |
have been ill-formed. — *end example*]
|
| 2075 |
|
| 2076 |
[*Note 3*: If a deallocation function has no explicit
|
| 2077 |
*noexcept-specifier*, it has a non-throwing exception specification
|