- tmp/tmp2twctpz_/{from.md → to.md} +655 -480
tmp/tmp2twctpz_/{from.md → to.md}
RENAMED
|
@@ -20,22 +20,29 @@ class-specifier:
|
|
| 20 |
class-head '{' member-specificationₒₚₜ '}'
|
| 21 |
```
|
| 22 |
|
| 23 |
``` bnf
|
| 24 |
class-head:
|
| 25 |
-
class-key attribute-specifier-seqₒₚₜ class-head-name class-
|
| 26 |
class-key attribute-specifier-seqₒₚₜ base-clauseₒₚₜ
|
| 27 |
```
|
| 28 |
|
| 29 |
``` bnf
|
| 30 |
class-head-name:
|
| 31 |
nested-name-specifierₒₚₜ class-name
|
| 32 |
```
|
| 33 |
|
| 34 |
``` bnf
|
| 35 |
-
class-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
final
|
|
|
|
|
|
|
| 37 |
```
|
| 38 |
|
| 39 |
``` bnf
|
| 40 |
class-key:
|
| 41 |
class
|
|
@@ -45,22 +52,22 @@ class-key:
|
|
| 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
|
| 58 |
-
itself; this is known as the *injected-class-name*. For
|
| 59 |
-
access checking, the injected-class-name is treated as if it
|
| 60 |
-
public member name. A *class-specifier* is commonly referred to
|
| 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.
|
|
@@ -92,38 +99,45 @@ template<> struct A<void> {}; // OK
|
|
| 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*]
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 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
|
| 113 |
-
//
|
|
|
|
|
|
|
| 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.
|
| 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*]
|
|
@@ -137,16 +151,68 @@ A *trivially copyable class* is a class:
|
|
| 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
|
| 143 |
-
more eligible default constructors [[class.default.ctor]], all of which
|
| 144 |
-
are trivial.
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
A class `S` is a *standard-layout class* if it:
|
| 150 |
|
| 151 |
- has no non-static data members of type non-standard-layout class (or
|
| 152 |
array of such types) or reference,
|
|
@@ -158,11 +224,11 @@ A class `S` is a *standard-layout class* if it:
|
|
| 158 |
- has at most one base class subobject of any given type,
|
| 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
|
| 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
|
|
@@ -194,44 +260,44 @@ struct U : S, T { }; // not a standard-layout class
|
|
| 194 |
A *standard-layout struct* is a standard-layout class defined with the
|
| 195 |
*class-key* `struct` or the *class-key* `class`. A
|
| 196 |
*standard-layout union* is a standard-layout class defined with the
|
| 197 |
*class-key* `union`.
|
| 198 |
|
| 199 |
-
[*Note
|
| 200 |
code written in other programming languages. Their layout is specified
|
| 201 |
-
in [[class.mem]]. — *end note*]
|
| 202 |
|
| 203 |
[*Example 2*:
|
| 204 |
|
| 205 |
``` cpp
|
| 206 |
-
struct N { // neither
|
| 207 |
int i;
|
| 208 |
int j;
|
| 209 |
virtual ~N();
|
| 210 |
};
|
| 211 |
|
| 212 |
-
struct T { //
|
| 213 |
int i;
|
| 214 |
private:
|
| 215 |
int j;
|
| 216 |
};
|
| 217 |
|
| 218 |
-
struct SL { // standard-layout but not
|
| 219 |
int i;
|
| 220 |
int j;
|
| 221 |
~SL();
|
| 222 |
};
|
| 223 |
|
| 224 |
-
struct POD { // both
|
| 225 |
int i;
|
| 226 |
int j;
|
| 227 |
};
|
| 228 |
```
|
| 229 |
|
| 230 |
— *end example*]
|
| 231 |
|
| 232 |
-
[*Note
|
| 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
|
|
@@ -366,21 +432,25 @@ void g(int s) {
|
|
| 366 |
|
| 367 |
[*Note 3*:
|
| 368 |
|
| 369 |
The declaration of a class name takes effect immediately after the
|
| 370 |
*identifier* is seen in the class definition or
|
| 371 |
-
*elaborated-type-specifier*.
|
|
|
|
|
|
|
| 372 |
|
| 373 |
``` cpp
|
| 374 |
class A * A;
|
| 375 |
```
|
| 376 |
|
| 377 |
first specifies `A` to be the name of a class and then redefines it as
|
| 378 |
the name of a pointer to an object of that class. This means that the
|
| 379 |
elaborated form `class` `A` must be used to refer to the class. Such
|
| 380 |
artistry with names can be confusing and is best avoided.
|
| 381 |
|
|
|
|
|
|
|
| 382 |
— *end note*]
|
| 383 |
|
| 384 |
A *simple-template-id* is only a *class-name* if its *template-name*
|
| 385 |
names a class template.
|
| 386 |
|
|
@@ -396,13 +466,15 @@ member-specification:
|
|
| 396 |
|
| 397 |
``` bnf
|
| 398 |
member-declaration:
|
| 399 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 400 |
function-definition
|
|
|
|
| 401 |
using-declaration
|
| 402 |
using-enum-declaration
|
| 403 |
static_assert-declaration
|
|
|
|
| 404 |
template-declaration
|
| 405 |
explicit-specialization
|
| 406 |
deduction-guide
|
| 407 |
alias-declaration
|
| 408 |
opaque-enum-declaration
|
|
@@ -415,20 +487,19 @@ member-declarator-list:
|
|
| 415 |
member-declarator-list ',' member-declarator
|
| 416 |
```
|
| 417 |
|
| 418 |
``` bnf
|
| 419 |
member-declarator:
|
| 420 |
-
declarator virt-specifier-seqₒₚₜ pure-specifierₒₚₜ
|
| 421 |
-
declarator requires-clause
|
| 422 |
-
declarator brace-or-equal-initializer
|
| 423 |
identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
|
| 424 |
```
|
| 425 |
|
| 426 |
``` bnf
|
| 427 |
virt-specifier-seq:
|
| 428 |
-
virt-specifier
|
| 429 |
-
virt-specifier-seq virt-specifier
|
| 430 |
```
|
| 431 |
|
| 432 |
``` bnf
|
| 433 |
virt-specifier:
|
| 434 |
override
|
|
@@ -438,57 +509,97 @@ virt-specifier:
|
|
| 438 |
``` bnf
|
| 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
|
| 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
|
| 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
|
| 468 |
unnamed bit-field [[class.bit]] is a member of the class, and each such
|
| 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
|
| 474 |
-
|
| 475 |
-
[[
|
| 476 |
-
members by use of a typedef
|
| 477 |
-
*alias-declaration*. The enumerators of
|
| 478 |
-
[[dcl.enum]] defined in the class are members of
|
|
|
|
| 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 |
-
|
| 489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
A member shall not be declared twice in the *member-specification*,
|
| 492 |
except that
|
| 493 |
|
| 494 |
- a nested class or member class template can be declared and then later
|
|
@@ -508,23 +619,27 @@ non-static data member.
|
|
| 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]],
|
|
|
|
| 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
|
| 524 |
-
|
| 525 |
-
|
|
|
|
|
|
|
|
|
|
| 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*.
|
|
@@ -654,11 +769,11 @@ 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`.
|
|
@@ -673,12 +788,13 @@ 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 |
-
-
|
| 679 |
-
attribute
|
|
|
|
| 680 |
- either both entities are bit-fields with the same width or neither is
|
| 681 |
a bit-field.
|
| 682 |
|
| 683 |
[*Example 4*:
|
| 684 |
|
|
@@ -741,10 +857,47 @@ 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 |
|
|
@@ -777,11 +930,13 @@ definition, if they are defined at all.
|
|
| 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]]
|
|
|
|
|
|
|
| 783 |
|
| 784 |
``` cpp
|
| 785 |
typedef void fv();
|
| 786 |
typedef void fvc() const;
|
| 787 |
struct S {
|
|
@@ -792,11 +947,11 @@ struct S {
|
|
| 792 |
fv S::* pmfv1 = &S::memfunc1;
|
| 793 |
fv S::* pmfv2 = &S::memfunc2;
|
| 794 |
fvc S::* pmfv3 = &S::memfunc3;
|
| 795 |
```
|
| 796 |
|
| 797 |
-
|
| 798 |
|
| 799 |
— *end note*]
|
| 800 |
|
| 801 |
### Non-static member functions <a id="class.mfct.non.static">[[class.mfct.non.static]]</a>
|
| 802 |
|
|
@@ -806,65 +961,14 @@ 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 |
-
|
| 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
|
| 825 |
-
[[temp.dep.type]].
|
| 826 |
-
|
| 827 |
-
[*Example 1*:
|
| 828 |
-
|
| 829 |
-
``` cpp
|
| 830 |
-
struct tnode {
|
| 831 |
-
char tword[20];
|
| 832 |
-
int count;
|
| 833 |
-
tnode* left;
|
| 834 |
-
tnode* right;
|
| 835 |
-
void set(const char*, tnode* l, tnode* r);
|
| 836 |
-
};
|
| 837 |
-
|
| 838 |
-
void tnode::set(const char* w, tnode* l, tnode* r) {
|
| 839 |
-
count = strlen(w)+1;
|
| 840 |
-
if (sizeof(tword)<=count)
|
| 841 |
-
perror("tnode string too long");
|
| 842 |
-
strcpy(tword,w);
|
| 843 |
-
left = l;
|
| 844 |
-
right = r;
|
| 845 |
-
}
|
| 846 |
-
|
| 847 |
-
void f(tnode n1, tnode n2) {
|
| 848 |
-
n1.set("abc",&n2,0);
|
| 849 |
-
n2.set("def",0,0);
|
| 850 |
-
}
|
| 851 |
-
```
|
| 852 |
-
|
| 853 |
-
In the body of the member function `tnode::set`, the member names
|
| 854 |
-
`tword`, `count`, `left`, and `right` refer to members of the object for
|
| 855 |
-
which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
| 856 |
-
`tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
|
| 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>
|
|
@@ -913,11 +1017,11 @@ Special member functions obey the usual access rules [[class.access]].
|
|
| 913 |
|
| 914 |
[*Example 2*: Declaring a constructor protected ensures that only
|
| 915 |
derived classes and friends can create objects using
|
| 916 |
it. — *end example*]
|
| 917 |
|
| 918 |
-
Two special member functions are of the same kind if
|
| 919 |
|
| 920 |
- they are both default constructors,
|
| 921 |
- they are both copy or move constructors with the same first parameter
|
| 922 |
type, or
|
| 923 |
- they are both copy or move assignment operators with the same first
|
|
@@ -927,15 +1031,16 @@ Two special member functions are of the same kind if:
|
|
| 927 |
An *eligible special member function* is a special member function for
|
| 928 |
which:
|
| 929 |
|
| 930 |
- the function is not deleted,
|
| 931 |
- the associated constraints [[temp.constr]], if any, are satisfied, and
|
| 932 |
-
- no special member function of the same kind
|
|
|
|
| 933 |
[[temp.constr.order]].
|
| 934 |
|
| 935 |
-
For a class, its non-static data members, its non-virtual direct
|
| 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 |
|
|
@@ -995,12 +1100,10 @@ cprint( complex(7.8,1.2) );
|
|
| 995 |
— *end example*]
|
| 996 |
|
| 997 |
[*Note 2*: For initialization of objects of class type see
|
| 998 |
[[class.init]]. — *end note*]
|
| 999 |
|
| 1000 |
-
An object created in this way is unnamed.
|
| 1001 |
-
|
| 1002 |
[*Note 3*: [[class.temporary]] describes the lifetime of temporary
|
| 1003 |
objects. — *end note*]
|
| 1004 |
|
| 1005 |
[*Note 4*: Explicit constructor calls do not yield lvalues, see
|
| 1006 |
[[basic.lval]]. — *end note*]
|
|
@@ -1026,79 +1129,81 @@ A constructor shall not have an explicit object parameter [[dcl.fct]].
|
|
| 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
|
| 1031 |
-
parameters). If there is no user-declared constructor
|
| 1032 |
-
non-explicit constructor having no parameters
|
| 1033 |
-
defaulted [[dcl.fct.def]]. An
|
| 1034 |
-
an inline public member of
|
|
|
|
| 1035 |
|
| 1036 |
-
A defaulted default constructor for class `X` is defined as deleted if
|
| 1037 |
|
| 1038 |
-
- `X` is a union that has a variant member with a non-trivial default
|
| 1039 |
-
constructor and no variant member of `X` has a default member
|
| 1040 |
-
initializer,
|
| 1041 |
-
- `X` is a non-union class that has a variant member `M` with a
|
| 1042 |
-
non-trivial default constructor and no variant member of the anonymous
|
| 1043 |
-
union containing `M` has a default member initializer,
|
| 1044 |
- any non-static data member with no default member initializer
|
| 1045 |
[[class.mem]] is of reference type,
|
| 1046 |
-
- any non-variant non-static data member of
|
| 1047 |
-
|
| 1048 |
-
const-default-constructible
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
-
|
| 1052 |
-
|
| 1053 |
-
- any potentially constructed subobject, except for a non-static data
|
| 1054 |
-
member with a *brace-or-equal-initializer*, has class type `M` (or
|
| 1055 |
-
array thereof) and either `M` has no default constructor or overload
|
| 1056 |
resolution [[over.match]] as applied to find `M`’s corresponding
|
| 1057 |
-
constructor
|
| 1058 |
-
|
| 1059 |
-
- any potentially constructed subobject has
|
| 1060 |
-
|
| 1061 |
-
constructor
|
|
|
|
| 1062 |
|
| 1063 |
-
A default constructor is *trivial* if it is not
|
|
|
|
| 1064 |
|
| 1065 |
-
-
|
| 1066 |
-
|
| 1067 |
-
- no non-static data member of
|
| 1068 |
-
|
| 1069 |
-
- all the direct base classes of
|
| 1070 |
-
|
| 1071 |
-
-
|
| 1072 |
-
type (or array thereof), each such
|
| 1073 |
-
constructor.
|
| 1074 |
|
| 1075 |
Otherwise, the default constructor is *non-trivial*.
|
| 1076 |
|
| 1077 |
-
|
| 1078 |
-
|
| 1079 |
-
|
| 1080 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1081 |
*compound-statement*. If that user-written default constructor would be
|
| 1082 |
-
ill-formed, the program is ill-formed.
|
| 1083 |
-
constructor
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
non-static data members are implicitly defined.
|
| 1088 |
|
| 1089 |
-
[*Note
|
| 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
|
| 1095 |
class object when no initializer is specified [[dcl.init.general]]. Such
|
| 1096 |
-
a default constructor
|
| 1097 |
[[class.access]]. — *end note*]
|
| 1098 |
|
| 1099 |
-
[*Note
|
| 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>
|
|
@@ -1226,11 +1331,11 @@ form
|
|
| 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&`.[^
|
| 1232 |
|
| 1233 |
Otherwise, the implicitly-declared copy constructor will have the form
|
| 1234 |
|
| 1235 |
``` cpp
|
| 1236 |
X::X(X&)
|
|
@@ -1258,29 +1363,28 @@ X::X(X&&)
|
|
| 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
|
| 1264 |
-
|
| 1265 |
[[over.match]], as applied to find `M`’s corresponding constructor,
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
-
|
| 1269 |
-
|
| 1270 |
-
|
| 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
|
| 1282 |
|
| 1283 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 1284 |
base classes [[class.mi]], and
|
| 1285 |
- the constructor selected to copy/move each direct base class subobject
|
| 1286 |
is trivial, and
|
|
@@ -1292,13 +1396,12 @@ 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 |
-
|
| 1298 |
-
constexpr
|
| 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 |
|
|
@@ -1307,11 +1410,11 @@ exception specification [[except.spec]]. — *end note*]
|
|
| 1307 |
|
| 1308 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 1309 |
performs a memberwise copy/move of its bases and members.
|
| 1310 |
|
| 1311 |
[*Note 7*: Default member initializers of non-static data members are
|
| 1312 |
-
ignored.
|
| 1313 |
|
| 1314 |
The order of initialization is the same as the order of initialization
|
| 1315 |
of bases and members in a user-defined constructor (see
|
| 1316 |
[[class.base.init]]). Let `x` be either the parameter of the constructor
|
| 1317 |
or, for the move constructor, an xvalue referring to the parameter. Each
|
|
@@ -1338,23 +1441,20 @@ the lifetime of o begins before the copy is performed.
|
|
| 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&`.[^
|
| 1344 |
|
| 1345 |
-
[*Note 1*:
|
| 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
|
| 1352 |
|
| 1353 |
-
If a class `X` only has a copy assignment operator with a
|
| 1354 |
-
type `X&`, an expression of type const `X` cannot be
|
| 1355 |
-
object of type `X`.
|
| 1356 |
|
| 1357 |
[*Example 1*:
|
| 1358 |
|
| 1359 |
``` cpp
|
| 1360 |
struct X {
|
|
@@ -1386,15 +1486,16 @@ X& X::operator=(const X&)
|
|
| 1386 |
```
|
| 1387 |
|
| 1388 |
if
|
| 1389 |
|
| 1390 |
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 1391 |
-
parameter is of type `const B&`, `const volatile B&`, or
|
|
|
|
| 1392 |
- for all the non-static data members of `X` that are of a class type
|
| 1393 |
`M` (or array thereof), each such class type has a copy assignment
|
| 1394 |
-
operator whose parameter is of type `const M&`,
|
| 1395 |
-
or `M`.[^
|
| 1396 |
|
| 1397 |
Otherwise, the implicitly-declared copy assignment operator will have
|
| 1398 |
the form
|
| 1399 |
|
| 1400 |
``` cpp
|
|
@@ -1404,14 +1505,11 @@ X& X::operator=(X&)
|
|
| 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
|
| 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
|
|
@@ -1458,39 +1556,37 @@ 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
|
| 1464 |
-
|
| 1465 |
-
- a non-static data member of `const` non-class type (or array thereof),
|
| 1466 |
-
or
|
| 1467 |
- a non-static data member of reference type, or
|
| 1468 |
-
- a direct non-static data member of class type `M` (or
|
| 1469 |
-
or a direct base class `M` that cannot
|
| 1470 |
-
overload resolution [[over.match]], as applied
|
| 1471 |
-
corresponding assignment operator,
|
| 1472 |
-
|
| 1473 |
-
|
| 1474 |
|
| 1475 |
-
[*Note
|
| 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.
|
| 1483 |
|
| 1484 |
-
[*Note
|
| 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
|
| 1494 |
base classes [[class.mi]], and
|
| 1495 |
- the assignment operator selected to copy/move each direct base class
|
| 1496 |
subobject is trivial, and
|
|
@@ -1506,11 +1602,11 @@ operator is `constexpr`.
|
|
| 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
|
| 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
|
|
@@ -1585,12 +1681,12 @@ parentheses, and the *id-expression* has one of the following forms:
|
|
| 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`
|
| 1592 |
|
| 1593 |
If a class has no user-declared prospective destructor, a prospective
|
| 1594 |
destructor is implicitly declared as defaulted [[dcl.fct.def]]. An
|
| 1595 |
implicitly-declared prospective destructor is an inline public member of
|
| 1596 |
its class.
|
|
@@ -1623,33 +1719,39 @@ 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 |
|
| 1630 |
-
- `X` is a
|
| 1631 |
-
|
| 1632 |
-
|
| 1633 |
-
|
| 1634 |
-
|
| 1635 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 1640 |
|
| 1641 |
- the destructor is not virtual,
|
| 1642 |
-
- all of the direct base classes of
|
| 1643 |
-
|
| 1644 |
-
|
| 1645 |
-
|
| 1646 |
|
| 1647 |
Otherwise, the destructor is *non-trivial*.
|
| 1648 |
|
| 1649 |
-
A defaulted destructor is a constexpr destructor
|
| 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 |
|
|
@@ -1662,17 +1764,18 @@ created in the program, the destructor shall be defined.
|
|
| 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
|
| 1667 |
-
non-static data members, the destructors for
|
| 1668 |
-
base classes and, if `X` is the most derived
|
| 1669 |
-
its destructor calls the destructors for
|
| 1670 |
-
destructors are called as if they were
|
| 1671 |
-
that is, ignoring any possible virtual
|
| 1672 |
-
derived classes. Bases and members are
|
| 1673 |
-
the completion of their constructor
|
|
|
|
| 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*]
|
|
@@ -1718,11 +1821,11 @@ lookup fails or if the deallocation function has a deleted definition
|
|
| 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 *
|
| 1724 |
destructor’s class type. The invocation of a destructor is subject to
|
| 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.
|
|
@@ -1765,11 +1868,13 @@ member function is not an explicit destructor call
|
|
| 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
|
| 1770 |
-
for writing memory management facilities.
|
|
|
|
|
|
|
| 1771 |
|
| 1772 |
``` cpp
|
| 1773 |
void* operator new(std::size_t, void* p) { return p; }
|
| 1774 |
struct X {
|
| 1775 |
X(int);
|
|
@@ -1783,17 +1888,19 @@ void g() { // rare, specialized use:
|
|
| 1783 |
f(p);
|
| 1784 |
p->X::~X(); // cleanup
|
| 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
|
| 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*:
|
|
@@ -1827,11 +1934,11 @@ 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
|
| 1833 |
|
| 1834 |
At most one user-defined conversion (constructor or conversion function)
|
| 1835 |
is implicitly applied to a single value.
|
| 1836 |
|
| 1837 |
[*Example 1*:
|
|
@@ -1854,11 +1961,11 @@ int c = X(a); // OK, a.operator X().operator int()
|
|
| 1854 |
|
| 1855 |
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 1856 |
|
| 1857 |
A constructor that is not explicit [[dcl.fct.spec]] specifies a
|
| 1858 |
conversion from the types of its parameters (if any) to the type of its
|
| 1859 |
-
class.
|
| 1860 |
|
| 1861 |
[*Example 1*:
|
| 1862 |
|
| 1863 |
``` cpp
|
| 1864 |
struct X {
|
|
@@ -1910,17 +2017,10 @@ Z a6 = { 3, 4 }; // error: no implicit conversion
|
|
| 1910 |
|
| 1911 |
— *end example*]
|
| 1912 |
|
| 1913 |
— *end note*]
|
| 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
|
|
@@ -1939,39 +2039,38 @@ conversion-declarator:
|
|
| 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 |
-
|
| 1945 |
-
ref-qualifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1946 |
```
|
| 1947 |
|
| 1948 |
-
where the *
|
| 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`;
|
| 1960 |
-
|
| 1961 |
-
|
| 1962 |
-
*
|
| 1963 |
-
(if any) shall not be a
|
|
|
|
| 1964 |
|
| 1965 |
-
|
| 1966 |
-
|
| 1967 |
-
|
| 1968 |
-
|
| 1969 |
-
|
| 1970 |
-
|
| 1971 |
-
|
| 1972 |
-
reference to it), or to cv `void`.[^6]
|
| 1973 |
|
| 1974 |
[*Example 1*:
|
| 1975 |
|
| 1976 |
``` cpp
|
| 1977 |
struct X {
|
|
@@ -2023,11 +2122,11 @@ void g(X a, X b) {
|
|
| 2023 |
The *conversion-type-id* shall not represent a function type nor an
|
| 2024 |
array type. The *conversion-type-id* in a *conversion-function-id* is
|
| 2025 |
the longest sequence of tokens that could possibly form a
|
| 2026 |
*conversion-type-id*.
|
| 2027 |
|
| 2028 |
-
[*Note
|
| 2029 |
|
| 2030 |
This prevents ambiguities between the declarator operator `*` and its
|
| 2031 |
expression counterparts.
|
| 2032 |
|
| 2033 |
[*Example 3*:
|
|
@@ -2052,11 +2151,11 @@ operator int [[noreturn]] (); // error: noreturn attribute applied to a type
|
|
| 2052 |
|
| 2053 |
— *end example*]
|
| 2054 |
|
| 2055 |
— *end note*]
|
| 2056 |
|
| 2057 |
-
[*Note
|
| 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
|
|
@@ -2351,11 +2450,13 @@ cannot be virtual.
|
|
| 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.
|
|
|
|
|
|
|
| 2357 |
|
| 2358 |
``` cpp
|
| 2359 |
struct B {
|
| 2360 |
virtual ~B();
|
| 2361 |
void operator delete(void*, std::size_t);
|
|
@@ -2385,17 +2486,21 @@ void f() {
|
|
| 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.
|
|
|
|
|
|
|
| 2397 |
|
| 2398 |
``` cpp
|
| 2399 |
struct B {
|
| 2400 |
virtual ~B();
|
| 2401 |
void operator delete[](void*, std::size_t);
|
|
@@ -2411,16 +2516,18 @@ void f(int i) {
|
|
| 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
|
| 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
|
|
@@ -2513,13 +2620,13 @@ non-static data members can be stored in a union at any time.
|
|
| 2513 |
|
| 2514 |
[*Note 1*: One special guarantee is made in order to simplify the use
|
| 2515 |
of unions: If a standard-layout union contains several standard-layout
|
| 2516 |
structs that share a common initial sequence [[class.mem]], and if a
|
| 2517 |
non-static data member of an object of this standard-layout union type
|
| 2518 |
-
is active and is one of the standard-layout structs,
|
| 2519 |
-
|
| 2520 |
-
|
| 2521 |
|
| 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 |
|
|
@@ -2529,22 +2636,20 @@ 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 |
-
|
| 2540 |
-
|
| 2541 |
-
[[class.
|
| 2542 |
-
|
| 2543 |
-
|
| 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 |
|
|
@@ -2556,13 +2661,14 @@ union U {
|
|
| 2556 |
};
|
| 2557 |
```
|
| 2558 |
|
| 2559 |
Since `std::string` [[string.classes]] declares non-trivial versions of
|
| 2560 |
all of the special member functions, `U` will have an implicitly deleted
|
| 2561 |
-
|
| 2562 |
-
|
| 2563 |
-
|
|
|
|
| 2564 |
|
| 2565 |
— *end example*]
|
| 2566 |
|
| 2567 |
— *end note*]
|
| 2568 |
|
|
@@ -2579,17 +2685,19 @@ lifetime of that union member, as described below. For an expression
|
|
| 2579 |
subscripting operator, S(E) is S(A) if `A` is of array type, S(B) if
|
| 2580 |
`B` is of array type, and empty otherwise.
|
| 2581 |
- Otherwise, S(E) is empty.
|
| 2582 |
|
| 2583 |
In an assignment expression of the form `E1 = E2` that uses either the
|
| 2584 |
-
built-in assignment operator [[expr.
|
| 2585 |
-
operator [[class.copy.assign]], for each element `X` of S(`E1`)
|
| 2586 |
-
|
| 2587 |
-
|
| 2588 |
-
|
| 2589 |
-
|
| 2590 |
-
|
|
|
|
|
|
|
| 2591 |
|
| 2592 |
[*Note 4*: This ends the lifetime of the previously-active member of
|
| 2593 |
the union, if any [[basic.life]]. — *end note*]
|
| 2594 |
|
| 2595 |
[*Example 2*:
|
|
@@ -2670,15 +2778,16 @@ 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 |
-
|
| 2676 |
-
linkage shall
|
| 2677 |
-
|
| 2678 |
-
|
| 2679 |
-
|
|
|
|
| 2680 |
|
| 2681 |
[*Note 1*:
|
| 2682 |
|
| 2683 |
A union for which objects, pointers, or references are declared is not
|
| 2684 |
an anonymous union.
|
|
@@ -2768,14 +2877,14 @@ local* p = 0; // error: local not found
|
|
| 2768 |
An enclosing function has no special access to members of the local
|
| 2769 |
class; it obeys the usual access rules [[class.access]]. Member
|
| 2770 |
functions of a local class shall be defined within their class
|
| 2771 |
definition, if they are defined at all.
|
| 2772 |
|
| 2773 |
-
|
| 2774 |
-
class `X`
|
| 2775 |
-
|
| 2776 |
-
|
| 2777 |
|
| 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>
|
|
@@ -2805,11 +2914,11 @@ base-specifier:
|
|
| 2805 |
|
| 2806 |
``` bnf
|
| 2807 |
class-or-decltype:
|
| 2808 |
nested-name-specifierₒₚₜ type-name
|
| 2809 |
nested-name-specifier template simple-template-id
|
| 2810 |
-
|
| 2811 |
```
|
| 2812 |
|
| 2813 |
``` bnf
|
| 2814 |
access-specifier:
|
| 2815 |
private
|
|
@@ -2824,17 +2933,20 @@ 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
|
| 2830 |
-
*
|
| 2831 |
-
|
| 2832 |
-
|
| 2833 |
-
|
| 2834 |
-
base class
|
| 2835 |
-
|
|
|
|
|
|
|
|
|
|
| 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.
|
|
@@ -3045,29 +3157,29 @@ a distinct `B` subobject within the object of type `AA`. Given the class
|
|
| 3045 |
|
| 3046 |
### Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
| 3047 |
|
| 3048 |
A non-static member function is a *virtual function* if it is first
|
| 3049 |
declared with the keyword `virtual` or if it overrides a virtual member
|
| 3050 |
-
function declared in a base class (see below).[^
|
| 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*.[^
|
| 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*[^
|
| 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 {
|
|
@@ -3185,11 +3297,11 @@ 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
|
| 3187 |
the following criteria:
|
| 3188 |
|
| 3189 |
- both are pointers to classes, both are lvalue references to classes,
|
| 3190 |
-
or both are rvalue references to classes[^
|
| 3191 |
- the class in the return type of `B::f` is the same class as the class
|
| 3192 |
in the return type of `D::f`, or is an unambiguous and accessible
|
| 3193 |
direct or indirect base class of the class in the return type of
|
| 3194 |
`D::f`
|
| 3195 |
- both pointers or references have the same cv-qualification and the
|
|
@@ -3505,13 +3617,13 @@ A member of a class can be
|
|
| 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.[^
|
| 3511 |
|
| 3512 |
-
Members of a class defined with the keyword `class` are
|
| 3513 |
default. Members of a class defined with the keywords `struct` or
|
| 3514 |
`union` are public by default.
|
| 3515 |
|
| 3516 |
[*Example 1*:
|
| 3517 |
|
|
@@ -3554,14 +3666,15 @@ void g(S* sp) {
|
|
| 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
|
| 3560 |
typedef or alias declaration itself is considered. The accessibility of
|
| 3561 |
-
the entity
|
| 3562 |
-
|
|
|
|
| 3563 |
|
| 3564 |
``` cpp
|
| 3565 |
class A {
|
| 3566 |
class B { };
|
| 3567 |
public:
|
|
@@ -3572,10 +3685,12 @@ 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*]
|
|
@@ -3591,26 +3706,26 @@ 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
|
| 3597 |
|
| 3598 |
``` cpp
|
| 3599 |
class A {
|
| 3600 |
typedef int I; // private member
|
| 3601 |
-
I f();
|
| 3602 |
-
friend I g(I);
|
| 3603 |
static I x;
|
| 3604 |
template<int> struct Q;
|
| 3605 |
template<int> friend struct R;
|
| 3606 |
protected:
|
| 3607 |
struct B { };
|
| 3608 |
};
|
| 3609 |
|
| 3610 |
-
A::I A::f() { return 0; }
|
| 3611 |
-
A::I g(A::I p = A::x);
|
| 3612 |
A::I g(A::I p) { return 0; }
|
| 3613 |
A::I A::x = 0;
|
| 3614 |
template<A::I> struct A::Q { };
|
| 3615 |
template<A::I> struct R { };
|
| 3616 |
|
|
@@ -3635,11 +3750,11 @@ 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
|
| 3641 |
|
| 3642 |
``` cpp
|
| 3643 |
class B { };
|
| 3644 |
template <class T> class C {
|
| 3645 |
protected:
|
|
@@ -3746,11 +3861,11 @@ of the derived class. If a class is declared to be a base class for
|
|
| 3746 |
another class using the `protected` access specifier, the public and
|
| 3747 |
protected members of the base class are accessible as protected members
|
| 3748 |
of the derived class. If a class is declared to be a base class for
|
| 3749 |
another class using the `private` access specifier, the public and
|
| 3750 |
protected members of the base class are accessible as private members of
|
| 3751 |
-
the derived class.[^
|
| 3752 |
|
| 3753 |
In the absence of an *access-specifier* for a base class, `public` is
|
| 3754 |
assumed when the derived class is defined with the *class-key* `struct`
|
| 3755 |
and `private` is assumed when the class is defined with the *class-key*
|
| 3756 |
`class`.
|
|
@@ -3780,11 +3895,13 @@ 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.
|
|
|
|
|
|
|
| 3786 |
|
| 3787 |
``` cpp
|
| 3788 |
class B {
|
| 3789 |
public:
|
| 3790 |
int mi; // non-static member
|
|
@@ -3807,10 +3924,12 @@ void DD::f() {
|
|
| 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
|
|
@@ -3820,11 +3939,11 @@ A base class `B` of `N` is *accessible* at *R*, if
|
|
| 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
|
| 3826 |
|
| 3827 |
``` cpp
|
| 3828 |
class B {
|
| 3829 |
public:
|
| 3830 |
int m;
|
|
@@ -3851,33 +3970,39 @@ derived class to a pointer to that base class
|
|
| 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 |
-
|
| 3857 |
-
|
| 3858 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 3864 |
-
used to name the member (as in `p->T::m`), the class
|
| 3865 |
-
is the class
|
| 3866 |
*qualified-id* (that is, `T`). — *end note*]
|
| 3867 |
|
| 3868 |
-
A member `m` is accessible at the point *R* when
|
|
|
|
| 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
|
| 3878 |
-
\[*Example
|
| 3879 |
``` cpp
|
| 3880 |
class B;
|
| 3881 |
class A {
|
| 3882 |
private:
|
| 3883 |
int i;
|
|
@@ -3893,14 +4018,14 @@ A member `m` is accessible at the point *R* when named in class `N` if
|
|
| 3893 |
|
| 3894 |
If a class member access operator, including an implicit “`this->`”, is
|
| 3895 |
used to access a non-static data member or non-static member function,
|
| 3896 |
the reference is ill-formed if the left operand (considered as a pointer
|
| 3897 |
in the “`.`” operator case) cannot be implicitly converted to a pointer
|
| 3898 |
-
to the
|
| 3899 |
|
| 3900 |
[*Note 4*: This requirement is in addition to the requirement that the
|
| 3901 |
-
member be accessible as
|
| 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
|
|
@@ -3972,31 +4097,26 @@ class Z {
|
|
| 3972 |
};
|
| 3973 |
```
|
| 3974 |
|
| 3975 |
— *end example*]
|
| 3976 |
|
| 3977 |
-
A friend declaration that does not declare a function shall
|
| 3978 |
-
|
| 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
|
| 3990 |
-
cv-qualified) class type, that class is declared as a friend;
|
| 3991 |
-
the friend
|
| 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 |
|
|
@@ -4004,24 +4124,30 @@ 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
|
| 4010 |
-
friend
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4011 |
};
|
| 4012 |
|
| 4013 |
R<C> rc; // class C is a friend of R<C>
|
| 4014 |
-
R<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -4162,12 +4288,12 @@ void f() {
|
|
| 4162 |
|
| 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
|
| 4168 |
-
[[class.access.base]].[^
|
| 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`.
|
|
@@ -4194,14 +4320,14 @@ class D2 : public B {
|
|
| 4194 |
|
| 4195 |
void fr(B* pb, D1* p1, D2* p2) {
|
| 4196 |
pb->i = 1; // error
|
| 4197 |
p1->i = 2; // error
|
| 4198 |
p2->i = 3; // OK (access through a D2)
|
| 4199 |
-
p2->B::i = 4; // OK (access through a D2, even though
|
| 4200 |
int B::* pmi_B = &B::i; // error
|
| 4201 |
int B::* pmi_B2 = &D2::i; // OK (type of &D2::i is int B::*)
|
| 4202 |
-
B::j = 5; // error: not a friend of
|
| 4203 |
D2::j = 6; // OK (because refers to static member)
|
| 4204 |
}
|
| 4205 |
|
| 4206 |
void D2::mem(B* pb, D1* p1) {
|
| 4207 |
pb->i = 1; // error
|
|
@@ -4360,12 +4486,12 @@ complex f = 3; // initialized by calling complex(double) with a
|
|
| 4360 |
complex g = { 1, 2 }; // initialized by calling complex(double, double) with arguments 1 and 2
|
| 4361 |
```
|
| 4362 |
|
| 4363 |
— *end example*]
|
| 4364 |
|
| 4365 |
-
[*Note 1*: Overloading of the assignment operator [[over.
|
| 4366 |
-
effect on initialization. — *end note*]
|
| 4367 |
|
| 4368 |
An object of class type can also be initialized by a *braced-init-list*.
|
| 4369 |
List-initialization semantics apply; see [[dcl.init]] and
|
| 4370 |
[[dcl.init.list]].
|
| 4371 |
|
|
@@ -4396,13 +4522,13 @@ Here, `x.i` is initialized with 99, `x.f` is initialized with 88.8, and
|
|
| 4396 |
[*Note 2*: Braces can be elided in the *initializer-list* for any
|
| 4397 |
aggregate, even if the aggregate has members of a class type with
|
| 4398 |
user-defined type conversions; see [[dcl.init.aggr]]. — *end note*]
|
| 4399 |
|
| 4400 |
[*Note 3*: If `T` is a class type with no default constructor, any
|
| 4401 |
-
declaration of an object of type `T` (or array thereof) is
|
| 4402 |
-
no *initializer* is explicitly specified (see
|
| 4403 |
-
[[dcl.init]]). — *end note*]
|
| 4404 |
|
| 4405 |
[*Note 4*: The order in which objects with static or thread storage
|
| 4406 |
duration are initialized is described in [[basic.start.dynamic]] and
|
| 4407 |
[[stmt.dcl]]. — *end note*]
|
| 4408 |
|
|
@@ -4443,11 +4569,11 @@ 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.
|
| 4451 |
|
| 4452 |
A *mem-initializer-list* can initialize a base class using any
|
| 4453 |
*class-or-decltype* that denotes that base class type.
|
|
@@ -4585,11 +4711,11 @@ renders the program ill-formed.
|
|
| 4585 |
[*Note 4*: After the call to a constructor for class `X` for an object
|
| 4586 |
with automatic or dynamic storage duration has completed, if the
|
| 4587 |
constructor was not invoked as part of value-initialization and a member
|
| 4588 |
of `X` is neither initialized nor given a value during execution of the
|
| 4589 |
*compound-statement* of the body of the constructor, the member has an
|
| 4590 |
-
indeterminate value. — *end note*]
|
| 4591 |
|
| 4592 |
[*Example 6*:
|
| 4593 |
|
| 4594 |
``` cpp
|
| 4595 |
struct A {
|
|
@@ -4602,11 +4728,11 @@ struct B {
|
|
| 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*]
|
|
@@ -4740,17 +4866,23 @@ 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
|
| 4746 |
-
|
| 4747 |
-
[[expr.typeid]] or of a `dynamic_cast`
|
| 4748 |
-
if these operations are performed
|
| 4749 |
-
|
| 4750 |
-
|
| 4751 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4752 |
|
| 4753 |
[*Example 11*:
|
| 4754 |
|
| 4755 |
``` cpp
|
| 4756 |
class A {
|
|
@@ -4807,15 +4939,19 @@ public:
|
|
| 4807 |
When a constructor for type `B` is invoked to initialize an object of a
|
| 4808 |
different type `D` (that is, when the constructor was inherited
|
| 4809 |
[[namespace.udecl]]), initialization proceeds as if a defaulted default
|
| 4810 |
constructor were used to initialize the `D` object and each base class
|
| 4811 |
subobject from which the constructor was inherited, except that the `B`
|
| 4812 |
-
subobject is initialized by the
|
| 4813 |
-
|
| 4814 |
-
|
| 4815 |
-
|
| 4816 |
-
object.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4817 |
|
| 4818 |
[*Example 1*:
|
| 4819 |
|
| 4820 |
``` cpp
|
| 4821 |
struct B1 {
|
|
@@ -4864,14 +5000,33 @@ Class template `Log` wraps any class and forwards all of its
|
|
| 4864 |
constructors, while writing a message to the standard log whenever an
|
| 4865 |
object of class `Log` is destroyed.
|
| 4866 |
|
| 4867 |
— *end example*]
|
| 4868 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4869 |
If the constructor was inherited from multiple base class subobjects of
|
| 4870 |
type `B`, the program is ill-formed.
|
| 4871 |
|
| 4872 |
-
[*Example
|
| 4873 |
|
| 4874 |
``` cpp
|
| 4875 |
struct A { A(int); };
|
| 4876 |
struct B : A { using A::A; };
|
| 4877 |
|
|
@@ -4933,11 +5088,11 @@ int* p2 = &bobj.y.i; // undefined behavior: refers to member'
|
|
| 4933 |
|
| 4934 |
A* pa = &bobj; // undefined behavior: upcast to a base class type
|
| 4935 |
B bobj; // definition of bobj
|
| 4936 |
|
| 4937 |
extern X xobj;
|
| 4938 |
-
int* p3 = &xobj.i; // OK,
|
| 4939 |
X xobj;
|
| 4940 |
```
|
| 4941 |
|
| 4942 |
For another example,
|
| 4943 |
|
|
@@ -4952,14 +5107,14 @@ struct Y {
|
|
| 4952 |
};
|
| 4953 |
```
|
| 4954 |
|
| 4955 |
— *end example*]
|
| 4956 |
|
| 4957 |
-
During the construction of an object, if the value of
|
| 4958 |
-
|
| 4959 |
-
|
| 4960 |
-
|
| 4961 |
|
| 4962 |
[*Example 2*:
|
| 4963 |
|
| 4964 |
``` cpp
|
| 4965 |
struct C;
|
|
@@ -5025,11 +5180,13 @@ struct E : C, D, X {
|
|
| 5025 |
|
| 5026 |
Member functions, including virtual functions [[class.virtual]], can be
|
| 5027 |
called during construction or destruction [[class.base.init]]. When a
|
| 5028 |
virtual function is called directly or indirectly from a constructor or
|
| 5029 |
from a destructor, including during the construction or destruction of
|
| 5030 |
-
the class’s non-static data members,
|
|
|
|
|
|
|
| 5031 |
applies is the object (call it `x`) under construction or destruction,
|
| 5032 |
the function called is the final overrider in the constructor’s or
|
| 5033 |
destructor’s class and not one overriding it in a more-derived class. If
|
| 5034 |
the virtual function call uses an explicit class member access
|
| 5035 |
[[expr.ref]] and the object expression refers to the complete object of
|
|
@@ -5123,59 +5280,62 @@ B::B(V* v, A* a) {
|
|
| 5123 |
— *end example*]
|
| 5124 |
|
| 5125 |
### Copy/move elision <a id="class.copy.elision">[[class.copy.elision]]</a>
|
| 5126 |
|
| 5127 |
When certain criteria are met, an implementation is allowed to omit the
|
| 5128 |
-
|
| 5129 |
-
|
| 5130 |
-
object have side effects. In such cases, the
|
| 5131 |
-
source and target of the omitted
|
| 5132 |
-
different ways of referring to the same
|
| 5133 |
-
of the selected constructor is an rvalue
|
| 5134 |
-
|
| 5135 |
-
|
| 5136 |
-
when the two objects would have been
|
| 5137 |
-
optimization.
|
| 5138 |
|
| 5139 |
-
|
| 5140 |
-
|
| 5141 |
-
|
| 5142 |
|
| 5143 |
-
|
| 5144 |
-
|
| 5145 |
-
|
| 5146 |
-
|
| 5147 |
-
|
| 5148 |
-
|
| 5149 |
-
|
| 5150 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 5153 |
-
function
|
| 5154 |
-
|
| 5155 |
-
|
| 5156 |
-
|
|
|
|
|
|
|
| 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
|
| 5162 |
-
- when the *exception-declaration* of
|
| 5163 |
-
|
| 5164 |
-
|
| 5165 |
-
|
| 5166 |
-
|
| 5167 |
-
|
| 5168 |
-
the object
|
| 5169 |
-
cannot be a move from the exception object because it is always an
|
| 5170 |
-
lvalue. — *end note*]
|
| 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
|
| 5177 |
expression is evaluated in another context. — *end note*]
|
| 5178 |
|
| 5179 |
[*Example 1*:
|
| 5180 |
|
| 5181 |
``` cpp
|
|
@@ -5303,22 +5463,40 @@ template<class T> void f() {
|
|
| 5303 |
|
| 5304 |
## Comparisons <a id="class.compare">[[class.compare]]</a>
|
| 5305 |
|
| 5306 |
### Defaulted comparison operator functions <a id="class.compare.default">[[class.compare.default]]</a>
|
| 5307 |
|
| 5308 |
-
A defaulted comparison operator function [[over.binary]]
|
| 5309 |
-
|
| 5310 |
|
| 5311 |
-
- a non-static member or friend of `C`
|
|
|
|
|
|
|
| 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 |
-
|
| 5317 |
-
|
| 5318 |
-
|
| 5319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -5348,11 +5526,11 @@ 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
|
| 5354 |
|
| 5355 |
``` cpp
|
| 5356 |
template<typename T> struct X {
|
| 5357 |
friend constexpr std::partial_ordering operator<=>(X, X) requires (sizeof(T) != 1) = default;
|
| 5358 |
// implicitly declares: friend constexpr bool operator==(X, X) requires (sizeof(T) != 1) = default;
|
|
@@ -5387,17 +5565,17 @@ declared return type `bool`.
|
|
| 5387 |
|
| 5388 |
A defaulted `==` operator function for a class `C` is defined as deleted
|
| 5389 |
unless, for each `xᵢ` in the expanded list of subobjects for an object
|
| 5390 |
`x` of type `C`, `xᵢ`` == ``xᵢ` is usable [[class.compare.default]].
|
| 5391 |
|
| 5392 |
-
The return value
|
| 5393 |
-
|
| 5394 |
-
`
|
| 5395 |
-
|
| 5396 |
-
|
| 5397 |
-
|
| 5398 |
-
|
| 5399 |
|
| 5400 |
[*Example 1*:
|
| 5401 |
|
| 5402 |
``` cpp
|
| 5403 |
struct D {
|
|
@@ -5414,13 +5592,13 @@ struct D {
|
|
| 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`
|
| 5420 |
-
at least one viable candidate, the synthesized
|
| 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
|
| 5424 |
synthesized three-way comparison is not defined.
|
| 5425 |
- Otherwise, if `R` is `strong_ordering`, then
|
| 5426 |
``` cpp
|
|
@@ -5457,19 +5635,19 @@ subobjects for an object `x` of type `C`.
|
|
| 5457 |
common comparison type (see below) of `R₀`, `R₁`, …, `R_n-1`.
|
| 5458 |
- Otherwise, `R` shall not contain a placeholder type. If the
|
| 5459 |
synthesized three-way comparison of type `R` between any objects `xᵢ`
|
| 5460 |
and `xᵢ` is not defined, the operator function is defined as deleted.
|
| 5461 |
|
| 5462 |
-
The return value
|
| 5463 |
operator function with parameters `x` and `y` of the same type is
|
| 5464 |
determined by comparing corresponding elements `xᵢ` and `yᵢ` in the
|
| 5465 |
expanded lists of subobjects for `x` and `y` (in increasing index order)
|
| 5466 |
until the first index i where the synthesized three-way comparison of
|
| 5467 |
type `R` between `xᵢ` and `yᵢ` yields a result value `vᵢ` where
|
| 5468 |
-
`vᵢ` `!=` 0, contextually converted to `bool`, yields `true`
|
| 5469 |
-
copy of `vᵢ`
|
| 5470 |
-
`static_cast<R>(std::strong_ordering::equal)`.
|
| 5471 |
|
| 5472 |
The *common comparison type* `U` of a possibly-empty list of n
|
| 5473 |
comparison category types `T₀`, `T₁`, …, `T_n-1` is defined as follows:
|
| 5474 |
|
| 5475 |
- If at least one `Tᵢ` is `std::partial_ordering`, `U` is
|
|
@@ -5488,18 +5666,22 @@ a secondary comparison operator `@` shall have a declared return type
|
|
| 5488 |
`bool`.
|
| 5489 |
|
| 5490 |
The operator function with parameters `x` and `y` is defined as deleted
|
| 5491 |
if
|
| 5492 |
|
| 5493 |
-
- overload resolution [[over.match]], as applied to `x @ y`,
|
| 5494 |
-
result in a usable candidate, or
|
| 5495 |
-
- the
|
| 5496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5497 |
|
| 5498 |
-
|
| 5499 |
-
function is not considered as a candidate
|
| 5500 |
-
the `@
|
| 5501 |
|
| 5502 |
[*Example 1*:
|
| 5503 |
|
| 5504 |
``` cpp
|
| 5505 |
struct HasNoLessThan { };
|
|
@@ -5515,10 +5697,12 @@ struct C {
|
|
| 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
|
|
@@ -5530,10 +5714,11 @@ struct C {
|
|
| 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
|
|
@@ -5597,13 +5782,15 @@ struct C {
|
|
| 5597 |
[cmp.weakord]: support.md#cmp.weakord
|
| 5598 |
[conv]: expr.md#conv
|
| 5599 |
[conv.mem]: expr.md#conv.mem
|
| 5600 |
[conv.ptr]: expr.md#conv.ptr
|
| 5601 |
[conv.rval]: expr.md#conv.rval
|
|
|
|
|
|
|
| 5602 |
[dcl.array]: dcl.md#dcl.array
|
| 5603 |
[dcl.attr.nouniqueaddr]: dcl.md#dcl.attr.nouniqueaddr
|
| 5604 |
-
[dcl.
|
| 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
|
|
@@ -5617,10 +5804,11 @@ struct C {
|
|
| 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
|
|
@@ -5629,14 +5817,13 @@ struct C {
|
|
| 5629 |
[depr.impldec]: future.md#depr.impldec
|
| 5630 |
[depr.static.constexpr]: future.md#depr.static.constexpr
|
| 5631 |
[diff.class]: compatibility.md#diff.class
|
| 5632 |
[except.ctor]: except.md#except.ctor
|
| 5633 |
[except.handle]: except.md#except.handle
|
| 5634 |
-
[except.pre]: except.md#except.pre
|
| 5635 |
[except.spec]: except.md#except.spec
|
| 5636 |
[except.throw]: except.md#except.throw
|
| 5637 |
-
[expr.
|
| 5638 |
[expr.call]: expr.md#expr.call
|
| 5639 |
[expr.cast]: expr.md#expr.cast
|
| 5640 |
[expr.const]: expr.md#expr.const
|
| 5641 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 5642 |
[expr.delete]: expr.md#expr.delete
|
|
@@ -5656,39 +5843,42 @@ struct C {
|
|
| 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.
|
| 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
|
|
@@ -5697,61 +5887,46 @@ struct C {
|
|
| 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 |
|
| 5702 |
-
[^2]:
|
| 5703 |
-
|
| 5704 |
-
[^3]: This implies that the reference parameter of the
|
| 5705 |
implicitly-declared copy constructor cannot bind to a `volatile`
|
| 5706 |
lvalue; see [[diff.class]].
|
| 5707 |
|
| 5708 |
-
[^
|
| 5709 |
taking an rvalue reference parameter is never a copy assignment
|
| 5710 |
operator, the presence of such an assignment operator does not
|
| 5711 |
suppress the implicit declaration of a copy assignment operator.
|
| 5712 |
Such assignment operators participate in overload resolution with
|
| 5713 |
other assignment operators, including copy assignment operators,
|
| 5714 |
and, if selected, will be used to assign an object.
|
| 5715 |
|
| 5716 |
-
[^
|
| 5717 |
implicitly-declared copy assignment operator cannot bind to a
|
| 5718 |
`volatile` lvalue; see [[diff.class]].
|
| 5719 |
|
| 5720 |
-
[^
|
| 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 |
-
[^
|
| 5733 |
still polymorphic even if its internal representation does not
|
| 5734 |
otherwise require any additions for that polymorphic behavior.
|
| 5735 |
|
| 5736 |
-
[^
|
| 5737 |
[[over]] as a virtual function is not necessarily virtual and does
|
| 5738 |
not override. Access control [[class.access]] is not considered in
|
| 5739 |
determining overriding.
|
| 5740 |
|
| 5741 |
-
[^
|
| 5742 |
pointers to classes are not allowed.
|
| 5743 |
|
| 5744 |
-
[^
|
| 5745 |
and local classes.
|
| 5746 |
|
| 5747 |
-
[^
|
| 5748 |
base class remain inaccessible even to derived classes unless friend
|
| 5749 |
declarations within the base class definition are used to grant
|
| 5750 |
access explicitly.
|
| 5751 |
|
| 5752 |
-
[^
|
| 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.
|
|
|
|
| 20 |
class-head '{' member-specificationₒₚₜ '}'
|
| 21 |
```
|
| 22 |
|
| 23 |
``` bnf
|
| 24 |
class-head:
|
| 25 |
+
class-key attribute-specifier-seqₒₚₜ class-head-name class-property-specifier-seqₒₚₜ base-clauseₒₚₜ
|
| 26 |
class-key attribute-specifier-seqₒₚₜ base-clauseₒₚₜ
|
| 27 |
```
|
| 28 |
|
| 29 |
``` bnf
|
| 30 |
class-head-name:
|
| 31 |
nested-name-specifierₒₚₜ class-name
|
| 32 |
```
|
| 33 |
|
| 34 |
``` bnf
|
| 35 |
+
class-property-specifier-seq:
|
| 36 |
+
class-property-specifier class-property-specifier-seqₒₚₜ
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
``` bnf
|
| 40 |
+
class-property-specifier:
|
| 41 |
final
|
| 42 |
+
trivially_relocatable_if_eligible
|
| 43 |
+
replaceable_if_eligible
|
| 44 |
```
|
| 45 |
|
| 46 |
``` bnf
|
| 47 |
class-key:
|
| 48 |
class
|
|
|
|
| 52 |
|
| 53 |
A class declaration where the *class-name* in the *class-head-name* is a
|
| 54 |
*simple-template-id* shall be an explicit specialization
|
| 55 |
[[temp.expl.spec]] or a partial specialization [[temp.spec.partial]]. A
|
| 56 |
*class-specifier* whose *class-head* omits the *class-head-name* defines
|
| 57 |
+
an *unnamed class*.
|
| 58 |
|
| 59 |
[*Note 1*: An unnamed class thus can’t be `final`. — *end note*]
|
| 60 |
|
| 61 |
Otherwise, the *class-name* is an *identifier*; it is not looked up, and
|
| 62 |
the *class-specifier* introduces it.
|
| 63 |
|
| 64 |
+
The component name of the *class-name* is also bound in the scope of the
|
| 65 |
+
class (template) itself; this is known as the *injected-class-name*. For
|
| 66 |
+
purposes of access checking, the injected-class-name is treated as if it
|
| 67 |
+
were a public member name. A *class-specifier* is commonly referred to
|
| 68 |
+
as a *class definition*. A class is considered defined after the closing
|
| 69 |
brace of its *class-specifier* has been seen even though its member
|
| 70 |
functions are in general not yet defined. The optional
|
| 71 |
*attribute-specifier-seq* appertains to the class; the attributes in the
|
| 72 |
*attribute-specifier-seq* are thereafter considered attributes of the
|
| 73 |
class whenever it is named.
|
|
|
|
| 99 |
[*Note 2*: The *class-key* determines whether the class is a union
|
| 100 |
[[class.union]] and whether access is public or private by default
|
| 101 |
[[class.access]]. A union holds the value of at most one data member at
|
| 102 |
a time. — *end note*]
|
| 103 |
|
| 104 |
+
Each *class-property-specifier* shall appear at most once within a
|
| 105 |
+
single *class-property-specifier-seq*. Whenever a *class-key* is
|
| 106 |
+
followed by a *class-head-name*, one of the identifiers `final`,
|
| 107 |
+
`trivially_relocatable_if_eligible`, or `replaceable_if_eligible`, and a
|
| 108 |
+
colon or left brace, the identifier is interpreted as a
|
| 109 |
+
*class-property-specifier*.
|
| 110 |
|
| 111 |
[*Example 2*:
|
| 112 |
|
| 113 |
``` cpp
|
| 114 |
struct A;
|
| 115 |
struct A final {}; // OK, definition of struct A,
|
| 116 |
// not value-initialization of variable final
|
| 117 |
|
| 118 |
struct X {
|
| 119 |
struct C { constexpr operator int() { return 5; } };
|
| 120 |
+
struct B trivially_relocatable_if_eligible : C{};
|
| 121 |
+
// OK, definition of nested class B,
|
| 122 |
+
// not declaration of a bit-field member
|
| 123 |
+
// trivially_relocatable_if_eligible
|
| 124 |
};
|
| 125 |
```
|
| 126 |
|
| 127 |
— *end example*]
|
| 128 |
|
| 129 |
+
If a class is marked with the *class-property-specifier* `final` and
|
| 130 |
+
that class appears as a *class-or-decltype* in a *base-clause*
|
| 131 |
+
[[class.derived]], the program is ill-formed.
|
| 132 |
+
|
| 133 |
[*Note 3*: Complete objects of class type have nonzero size. Base class
|
| 134 |
subobjects and members declared with the `no_unique_address` attribute
|
| 135 |
[[dcl.attr.nouniqueaddr]] are not so constrained. — *end note*]
|
| 136 |
|
| 137 |
[*Note 4*: Class objects can be assigned
|
| 138 |
+
[[over.assign]], [[class.copy.assign]], passed as arguments to functions
|
| 139 |
[[dcl.init]], [[class.copy.ctor]], and returned by functions (except
|
| 140 |
objects of classes for which copying or moving has been restricted; see
|
| 141 |
[[dcl.fct.def.delete]] and [[class.access]]). Other plausible operators,
|
| 142 |
such as equality comparison, can be defined by the user; see
|
| 143 |
[[over.oper]]. — *end note*]
|
|
|
|
| 151 |
[[special]], [[class.copy.ctor]], [[class.copy.assign]],
|
| 152 |
- where each eligible copy constructor, move constructor, copy
|
| 153 |
assignment operator, and move assignment operator is trivial, and
|
| 154 |
- that has a trivial, non-deleted destructor [[class.dtor]].
|
| 155 |
|
| 156 |
+
A class `C` is *default-movable* if
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
- overload resolution for direct-initializing an object of type `C` from
|
| 159 |
+
an xvalue of type `C` selects a constructor that is a direct member of
|
| 160 |
+
`C` and is neither user-provided nor deleted,
|
| 161 |
+
- overload resolution for assigning to an lvalue of type `C` from an
|
| 162 |
+
xvalue of type `C` selects an assignment operator function that is a
|
| 163 |
+
direct member of `C` and is neither user-provided nor deleted, and
|
| 164 |
+
- `C` has a destructor that is neither user-provided nor deleted.
|
| 165 |
+
|
| 166 |
+
A class is *eligible for trivial relocation* unless it
|
| 167 |
+
|
| 168 |
+
- has any virtual base classes,
|
| 169 |
+
- has a base class that is not a trivially relocatable class,
|
| 170 |
+
- has a non-static data member of an object type that is not of a
|
| 171 |
+
trivially relocatable type, or
|
| 172 |
+
- has a deleted destructor,
|
| 173 |
+
|
| 174 |
+
except that it is *implementation-defined* whether an otherwise-eligible
|
| 175 |
+
union having one or more subobjects of polymorphic class type is
|
| 176 |
+
eligible for trivial relocation.
|
| 177 |
+
|
| 178 |
+
A class `C` is a *trivially relocatable class* if it is eligible for
|
| 179 |
+
trivial relocation and
|
| 180 |
+
|
| 181 |
+
- has the `trivially_relocatable_if_eligible`
|
| 182 |
+
*class-property-specifier*,
|
| 183 |
+
- is a union with no user-declared special member functions, or
|
| 184 |
+
- is default-movable.
|
| 185 |
+
|
| 186 |
+
[*Note 1*: A class with const-qualified or reference non-static data
|
| 187 |
+
members can be trivially relocatable. — *end note*]
|
| 188 |
+
|
| 189 |
+
A class `C` is *eligible for replacement* unless
|
| 190 |
+
|
| 191 |
+
- it has a base class that is not a replaceable class,
|
| 192 |
+
- it has a non-static data member that is not of a replaceable type,
|
| 193 |
+
- overload resolution fails or selects a deleted constructor when
|
| 194 |
+
direct-initializing an object of type `C` from an xvalue of type `C`
|
| 195 |
+
[[dcl.init.general]],
|
| 196 |
+
- overload resolution fails or selects a deleted assignment operator
|
| 197 |
+
function when assigning to an lvalue of type `C` from an xvalue of
|
| 198 |
+
type `C` [[expr.assign]], [[over.assign]], or
|
| 199 |
+
- it has a deleted destructor.
|
| 200 |
+
|
| 201 |
+
A class `C` is a *replaceable class* if it is eligible for replacement
|
| 202 |
+
and
|
| 203 |
+
|
| 204 |
+
- has the `replaceable_if_eligible` *class-property-specifier*,
|
| 205 |
+
- is a union with no user-declared special member functions, or
|
| 206 |
+
- is default-movable.
|
| 207 |
+
|
| 208 |
+
[*Note 2*: Accessibility of the special member functions is not
|
| 209 |
+
considered when establishing trivial relocatability or
|
| 210 |
+
replaceability. — *end note*]
|
| 211 |
+
|
| 212 |
+
[*Note 3*: Not all trivially copyable classes are trivially relocatable
|
| 213 |
+
or replaceable. — *end note*]
|
| 214 |
|
| 215 |
A class `S` is a *standard-layout class* if it:
|
| 216 |
|
| 217 |
- has no non-static data members of type non-standard-layout class (or
|
| 218 |
array of such types) or reference,
|
|
|
|
| 224 |
- has at most one base class subobject of any given type,
|
| 225 |
- has all non-static data members and bit-fields in the class and its
|
| 226 |
base classes first declared in the same class, and
|
| 227 |
- has no element of the set M(S) of types as a base class, where for any
|
| 228 |
type `X`, M(X) is defined as follows.[^1]
|
| 229 |
+
\[*Note 4*: M(X) is the set of the types of all non-base-class
|
| 230 |
subobjects that can be at a zero offset in `X`. — *end note*]
|
| 231 |
- If `X` is a non-union class type with no non-static data members,
|
| 232 |
the set M(X) is empty.
|
| 233 |
- If `X` is a non-union class type with a non-static data member of
|
| 234 |
type X₀ that is either of zero size or is the first non-static data
|
|
|
|
| 260 |
A *standard-layout struct* is a standard-layout class defined with the
|
| 261 |
*class-key* `struct` or the *class-key* `class`. A
|
| 262 |
*standard-layout union* is a standard-layout class defined with the
|
| 263 |
*class-key* `union`.
|
| 264 |
|
| 265 |
+
[*Note 5*: Standard-layout classes are useful for communicating with
|
| 266 |
code written in other programming languages. Their layout is specified
|
| 267 |
+
in [[class.mem.general]] and [[expr.rel]]. — *end note*]
|
| 268 |
|
| 269 |
[*Example 2*:
|
| 270 |
|
| 271 |
``` cpp
|
| 272 |
+
struct N { // neither trivially copyable nor standard-layout
|
| 273 |
int i;
|
| 274 |
int j;
|
| 275 |
virtual ~N();
|
| 276 |
};
|
| 277 |
|
| 278 |
+
struct T { // trivially copyable but not standard-layout
|
| 279 |
int i;
|
| 280 |
private:
|
| 281 |
int j;
|
| 282 |
};
|
| 283 |
|
| 284 |
+
struct SL { // standard-layout but not trivially copyable
|
| 285 |
int i;
|
| 286 |
int j;
|
| 287 |
~SL();
|
| 288 |
};
|
| 289 |
|
| 290 |
+
struct POD { // both trivially copyable and standard-layout
|
| 291 |
int i;
|
| 292 |
int j;
|
| 293 |
};
|
| 294 |
```
|
| 295 |
|
| 296 |
— *end example*]
|
| 297 |
|
| 298 |
+
[*Note 6*: Aggregates of class type are described in
|
| 299 |
[[dcl.init.aggr]]. — *end note*]
|
| 300 |
|
| 301 |
A class `S` is an *implicit-lifetime class* if
|
| 302 |
|
| 303 |
- it is an aggregate whose destructor is not user-provided or
|
|
|
|
| 432 |
|
| 433 |
[*Note 3*:
|
| 434 |
|
| 435 |
The declaration of a class name takes effect immediately after the
|
| 436 |
*identifier* is seen in the class definition or
|
| 437 |
+
*elaborated-type-specifier*.
|
| 438 |
+
|
| 439 |
+
[*Example 6*:
|
| 440 |
|
| 441 |
``` cpp
|
| 442 |
class A * A;
|
| 443 |
```
|
| 444 |
|
| 445 |
first specifies `A` to be the name of a class and then redefines it as
|
| 446 |
the name of a pointer to an object of that class. This means that the
|
| 447 |
elaborated form `class` `A` must be used to refer to the class. Such
|
| 448 |
artistry with names can be confusing and is best avoided.
|
| 449 |
|
| 450 |
+
— *end example*]
|
| 451 |
+
|
| 452 |
— *end note*]
|
| 453 |
|
| 454 |
A *simple-template-id* is only a *class-name* if its *template-name*
|
| 455 |
names a class template.
|
| 456 |
|
|
|
|
| 466 |
|
| 467 |
``` bnf
|
| 468 |
member-declaration:
|
| 469 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 470 |
function-definition
|
| 471 |
+
friend-type-declaration
|
| 472 |
using-declaration
|
| 473 |
using-enum-declaration
|
| 474 |
static_assert-declaration
|
| 475 |
+
consteval-block-declaration
|
| 476 |
template-declaration
|
| 477 |
explicit-specialization
|
| 478 |
deduction-guide
|
| 479 |
alias-declaration
|
| 480 |
opaque-enum-declaration
|
|
|
|
| 487 |
member-declarator-list ',' member-declarator
|
| 488 |
```
|
| 489 |
|
| 490 |
``` bnf
|
| 491 |
member-declarator:
|
| 492 |
+
declarator virt-specifier-seqₒₚₜ function-contract-specifier-seqₒₚₜ pure-specifierₒₚₜ
|
| 493 |
+
declarator requires-clause function-contract-specifier-seqₒₚₜ
|
| 494 |
+
declarator brace-or-equal-initializer
|
| 495 |
identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
|
| 496 |
```
|
| 497 |
|
| 498 |
``` bnf
|
| 499 |
virt-specifier-seq:
|
| 500 |
+
virt-specifier virt-specifier-seqₒₚₜ
|
|
|
|
| 501 |
```
|
| 502 |
|
| 503 |
``` bnf
|
| 504 |
virt-specifier:
|
| 505 |
override
|
|
|
|
| 509 |
``` bnf
|
| 510 |
pure-specifier:
|
| 511 |
'=' '0'
|
| 512 |
```
|
| 513 |
|
| 514 |
+
``` bnf
|
| 515 |
+
friend-type-declaration:
|
| 516 |
+
friend friend-type-specifier-list ';'
|
| 517 |
+
```
|
| 518 |
+
|
| 519 |
+
``` bnf
|
| 520 |
+
friend-type-specifier-list:
|
| 521 |
+
friend-type-specifier '...'ₒₚₜ
|
| 522 |
+
friend-type-specifier-list ',' friend-type-specifier '...'ₒₚₜ
|
| 523 |
+
```
|
| 524 |
+
|
| 525 |
+
``` bnf
|
| 526 |
+
friend-type-specifier:
|
| 527 |
+
simple-type-specifier
|
| 528 |
+
elaborated-type-specifier
|
| 529 |
+
typename-specifier
|
| 530 |
+
```
|
| 531 |
+
|
| 532 |
+
In the absence of a *virt-specifier-seq*, the token sequence `= 0` is
|
| 533 |
+
treated as a *pure-specifier* if the type of the *declarator-id*
|
| 534 |
+
[[dcl.meaning.general]] is a function type, and is otherwise treated as
|
| 535 |
+
a *brace-or-equal-initializer*.
|
| 536 |
+
|
| 537 |
+
[*Note 1*: If the member declaration acquires a function type through
|
| 538 |
+
template instantiation, the program is ill-formed; see
|
| 539 |
+
[[temp.spec.general]]. — *end note*]
|
| 540 |
+
|
| 541 |
+
The optional *function-contract-specifier-seq* [[dcl.contract.func]] in
|
| 542 |
+
a *member-declarator* shall be present only if the *declarator* declares
|
| 543 |
+
a function.
|
| 544 |
+
|
| 545 |
The *member-specification* in a class definition declares the full set
|
| 546 |
of members of the class; no member can be added elsewhere. A
|
| 547 |
*direct member* of a class `X` is a member of `X` that was first
|
| 548 |
declared within the *member-specification* of `X`, including anonymous
|
| 549 |
union members [[class.union.anon]] and direct members thereof. Members
|
| 550 |
of a class are data members, member functions [[class.mfct]], nested
|
| 551 |
types, enumerators, and member templates [[temp.mem]] and
|
| 552 |
specializations thereof.
|
| 553 |
|
| 554 |
+
[*Note 2*: A specialization of a static data member template is a
|
| 555 |
static data member. A specialization of a member function template is a
|
| 556 |
member function. A specialization of a member class template is a nested
|
| 557 |
class. — *end note*]
|
| 558 |
|
| 559 |
+
A *member-declaration* does not itself declare new members of the class
|
| 560 |
+
if it is
|
| 561 |
|
| 562 |
- a friend declaration [[class.friend]],
|
| 563 |
- a *deduction-guide* [[temp.deduct.guide]],
|
| 564 |
- a *template-declaration* whose *declaration* is one of the above,
|
| 565 |
- a *static_assert-declaration*,
|
| 566 |
+
- a *consteval-block-declaration*,
|
| 567 |
- a *using-declaration* [[namespace.udecl]], or
|
| 568 |
- an *empty-declaration*.
|
| 569 |
|
| 570 |
For any other *member-declaration*, each declared entity that is not an
|
| 571 |
unnamed bit-field [[class.bit]] is a member of the class, and each such
|
| 572 |
*member-declaration* shall either declare at least one member name of
|
| 573 |
the class or declare at least one unnamed bit-field.
|
| 574 |
|
| 575 |
+
A *data member* is either a non-function member introduced by a
|
| 576 |
+
*member-declarator* or an anonymous union member. A *member function* is
|
| 577 |
+
a member that is a function. Nested types are classes
|
| 578 |
+
[[class.name]], [[class.nest]] and enumerations [[dcl.enum]] declared in
|
| 579 |
+
the class and arbitrary types declared as members by use of a typedef
|
| 580 |
+
declaration [[dcl.typedef]] or *alias-declaration*. The enumerators of
|
| 581 |
+
an unscoped enumeration [[dcl.enum]] defined in the class are members of
|
| 582 |
+
the class.
|
| 583 |
|
| 584 |
A data member or member function may be declared `static` in its
|
| 585 |
*member-declaration*, in which case it is a *static member* (see
|
| 586 |
[[class.static]]) (a *static data member* [[class.static.data]] or
|
| 587 |
*static member function* [[class.static.mfct]], respectively) of the
|
| 588 |
class. Any other data member or member function is a *non-static member*
|
| 589 |
(a *non-static data member* or *non-static member function*
|
| 590 |
[[class.mfct.non.static]], respectively).
|
| 591 |
|
| 592 |
+
Every object of class type has a unique member subobject corresponding
|
| 593 |
+
to each of its direct non-static data members. If any non-static data
|
| 594 |
+
member of a class `C` is of reference type, then let `D` be an invented
|
| 595 |
+
class that is identical to `C` except that each non-static member of `D`
|
| 596 |
+
corresponding to a member of `C` of type “reference to `T`” instead has
|
| 597 |
+
type “pointer to `T`”. Every member subobject of a complete object of
|
| 598 |
+
type `C` has the same size, alignment, and offset as that of the
|
| 599 |
+
corresponding subobject of a complete object of type `D`. The size and
|
| 600 |
+
alignment of `C` are the same as the size and alignment of `D`.
|
| 601 |
|
| 602 |
A member shall not be declared twice in the *member-specification*,
|
| 603 |
except that
|
| 604 |
|
| 605 |
- a nested class or member class template can be declared and then later
|
|
|
|
| 619 |
A *complete-class context* of a class (template) is a
|
| 620 |
|
| 621 |
- function body [[dcl.fct.def.general]],
|
| 622 |
- default argument [[dcl.fct.default]],
|
| 623 |
- default template argument [[temp.param]],
|
| 624 |
+
- *noexcept-specifier* [[except.spec]],
|
| 625 |
+
- *function-contract-specifier* [[dcl.contract.func]], or
|
| 626 |
- default member initializer
|
| 627 |
|
| 628 |
within the *member-specification* of the class or class template.
|
| 629 |
|
| 630 |
[*Note 4*: A complete-class context of a nested class is also a
|
| 631 |
complete-class context of any enclosing class, if the nested class is
|
| 632 |
defined within the *member-specification* of the enclosing
|
| 633 |
class. — *end note*]
|
| 634 |
|
| 635 |
+
A class `C` is complete at a program point P if the definition of `C` is
|
| 636 |
+
reachable from P [[module.reach]] or if P is in a complete-class context
|
| 637 |
+
of `C`. Otherwise, `C` is incomplete at P.
|
| 638 |
+
|
| 639 |
+
If a *member-declaration* matches the syntactic requirements of
|
| 640 |
+
*friend-type-declaration*, it is a *friend-type-declaration*.
|
| 641 |
|
| 642 |
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 643 |
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 644 |
has function type, otherwise it is interpreted as introducing a
|
| 645 |
*brace-or-equal-initializer*.
|
|
|
|
| 769 |
name different from `T`:
|
| 770 |
|
| 771 |
- every static data member of class `T`;
|
| 772 |
- every member function of class `T`; \[*Note 9*: This restriction does
|
| 773 |
not apply to constructors, which do not have names
|
| 774 |
+
[[class.ctor]]. — *end note*]
|
| 775 |
- every member of class `T` that is itself a type;
|
| 776 |
- every member template of class `T`;
|
| 777 |
- every enumerator of every member of class `T` that is an unscoped
|
| 778 |
enumeration type; and
|
| 779 |
- every member of every anonymous union that is a member of class `T`.
|
|
|
|
| 788 |
in each of the structs, such that
|
| 789 |
|
| 790 |
- corresponding entities have layout-compatible types [[basic.types]],
|
| 791 |
- corresponding entities have the same alignment requirements
|
| 792 |
[[basic.align]],
|
| 793 |
+
- if a *has-attribute-expression* [[cpp.cond]] is not `0` for the
|
| 794 |
+
`no_unique_address` attribute, then neither entity is declared with
|
| 795 |
+
the `no_unique_address` attribute [[dcl.attr.nouniqueaddr]], and
|
| 796 |
- either both entities are bit-fields with the same width or neither is
|
| 797 |
a bit-field.
|
| 798 |
|
| 799 |
[*Example 4*:
|
| 800 |
|
|
|
|
| 857 |
|
| 858 |
[*Note 12*: The object and its first subobject are
|
| 859 |
pointer-interconvertible
|
| 860 |
[[basic.compound]], [[expr.static.cast]]. — *end note*]
|
| 861 |
|
| 862 |
+
A *data member description* is a quintuple (T, N, A, W, NUA) describing
|
| 863 |
+
the potential declaration of a non-static data member where
|
| 864 |
+
|
| 865 |
+
- T is a type,
|
| 866 |
+
- N is an *identifier* or $\bot$,
|
| 867 |
+
- A is an alignment or $\bot$,
|
| 868 |
+
- W is a bit-field width or $\bot$, and
|
| 869 |
+
- NUA is a boolean value.
|
| 870 |
+
|
| 871 |
+
Two data member descriptions are equal if each of their respective
|
| 872 |
+
components are the same entities, are the same identifiers, have equal
|
| 873 |
+
values, or are both $\bot$.
|
| 874 |
+
|
| 875 |
+
[*Note 13*:
|
| 876 |
+
|
| 877 |
+
The components of a data member description describe a data member such
|
| 878 |
+
that
|
| 879 |
+
|
| 880 |
+
- its type is specified using the type given by T,
|
| 881 |
+
- it is declared with the name given by N if N is not $\bot$ and is
|
| 882 |
+
otherwise unnamed,
|
| 883 |
+
- it is declared with the *alignment-specifier* [[dcl.align]] given by
|
| 884 |
+
`alignas(A)` if A is not $\bot$ and is otherwise declared without an
|
| 885 |
+
*alignment-specifier*,
|
| 886 |
+
- it is a bit-field [[class.bit]] with the width given by W if W is not
|
| 887 |
+
$\bot$ and is otherwise not a bit-field, and
|
| 888 |
+
- it is declared with the attribute `[[no_unique_address]]`
|
| 889 |
+
[[dcl.attr.nouniqueaddr]] if NUA is true and is otherwise declared
|
| 890 |
+
without that attribute.
|
| 891 |
+
|
| 892 |
+
Data member descriptions are represented by reflections
|
| 893 |
+
[[basic.fundamental]] returned by `std::meta::data_member_spec`
|
| 894 |
+
[[meta.reflection.define.aggregate]] and can be reified as data members
|
| 895 |
+
of a class using `std::meta::define_aggregate`.
|
| 896 |
+
|
| 897 |
+
— *end note*]
|
| 898 |
+
|
| 899 |
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 900 |
|
| 901 |
If a member function is attached to the global module and is defined
|
| 902 |
[[dcl.fct.def]] in its class definition, it is inline [[dcl.inline]].
|
| 903 |
|
|
|
|
| 930 |
[*Note 2*:
|
| 931 |
|
| 932 |
A member function can be declared (but not defined) using a typedef for
|
| 933 |
a function type. The resulting member function has exactly the same type
|
| 934 |
as it would have if the function declarator were provided explicitly,
|
| 935 |
+
see [[dcl.fct]] and [[temp.arg]].
|
| 936 |
+
|
| 937 |
+
[*Example 2*:
|
| 938 |
|
| 939 |
``` cpp
|
| 940 |
typedef void fv();
|
| 941 |
typedef void fvc() const;
|
| 942 |
struct S {
|
|
|
|
| 947 |
fv S::* pmfv1 = &S::memfunc1;
|
| 948 |
fv S::* pmfv2 = &S::memfunc2;
|
| 949 |
fvc S::* pmfv3 = &S::memfunc3;
|
| 950 |
```
|
| 951 |
|
| 952 |
+
— *end example*]
|
| 953 |
|
| 954 |
— *end note*]
|
| 955 |
|
| 956 |
### Non-static member functions <a id="class.mfct.non.static">[[class.mfct.non.static]]</a>
|
| 957 |
|
|
|
|
| 961 |
[[expr.ref]], [[over.match.call]]. A non-static member function may also
|
| 962 |
be called directly using the function call syntax
|
| 963 |
[[expr.call]], [[over.match.call]] from within its class or a class
|
| 964 |
derived from its class, or a member thereof, as described below.
|
| 965 |
|
| 966 |
+
[*Note 1*: An implicit object member function can be declared with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 967 |
*cv-qualifier*s, which affect the type of the `this` pointer
|
| 968 |
[[expr.prim.this]], and/or a *ref-qualifier* [[dcl.fct]]; both affect
|
| 969 |
+
overload resolution [[over.match.funcs]]. — *end note*]
|
| 970 |
|
| 971 |
An implicit object member function may be declared virtual
|
| 972 |
[[class.virtual]] or pure virtual [[class.abstract]].
|
| 973 |
|
| 974 |
### Special member functions <a id="special">[[special]]</a>
|
|
|
|
| 1017 |
|
| 1018 |
[*Example 2*: Declaring a constructor protected ensures that only
|
| 1019 |
derived classes and friends can create objects using
|
| 1020 |
it. — *end example*]
|
| 1021 |
|
| 1022 |
+
Two special member functions are of the same kind if
|
| 1023 |
|
| 1024 |
- they are both default constructors,
|
| 1025 |
- they are both copy or move constructors with the same first parameter
|
| 1026 |
type, or
|
| 1027 |
- they are both copy or move assignment operators with the same first
|
|
|
|
| 1031 |
An *eligible special member function* is a special member function for
|
| 1032 |
which:
|
| 1033 |
|
| 1034 |
- the function is not deleted,
|
| 1035 |
- the associated constraints [[temp.constr]], if any, are satisfied, and
|
| 1036 |
+
- no special member function of the same kind whose associated
|
| 1037 |
+
constraints, if any, are satisfied is more constrained
|
| 1038 |
[[temp.constr.order]].
|
| 1039 |
|
| 1040 |
+
For a class, its direct non-static data members, its non-virtual direct
|
| 1041 |
+
base classes, and, if the class is not abstract [[class.abstract]], its
|
| 1042 |
virtual base classes are called its *potentially constructed
|
| 1043 |
subobjects*.
|
| 1044 |
|
| 1045 |
### Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 1046 |
|
|
|
|
| 1100 |
— *end example*]
|
| 1101 |
|
| 1102 |
[*Note 2*: For initialization of objects of class type see
|
| 1103 |
[[class.init]]. — *end note*]
|
| 1104 |
|
|
|
|
|
|
|
| 1105 |
[*Note 3*: [[class.temporary]] describes the lifetime of temporary
|
| 1106 |
objects. — *end note*]
|
| 1107 |
|
| 1108 |
[*Note 4*: Explicit constructor calls do not yield lvalues, see
|
| 1109 |
[[basic.lval]]. — *end note*]
|
|
|
|
| 1129 |
#### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
|
| 1130 |
|
| 1131 |
A *default constructor* for a class `X` is a constructor of class `X`
|
| 1132 |
for which each parameter that is not a function parameter pack has a
|
| 1133 |
default argument (including the case of a constructor with no
|
| 1134 |
+
parameters). If there is no user-declared constructor or constructor
|
| 1135 |
+
template for class `X`, a non-explicit constructor having no parameters
|
| 1136 |
+
is implicitly declared as defaulted [[dcl.fct.def]]. An
|
| 1137 |
+
implicitly-declared default constructor is an inline public member of
|
| 1138 |
+
its class.
|
| 1139 |
|
| 1140 |
+
A defaulted default constructor for class `X` is defined as deleted if
|
| 1141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1142 |
- any non-static data member with no default member initializer
|
| 1143 |
[[class.mem]] is of reference type,
|
| 1144 |
+
- `X` is a non-union class and any non-variant non-static data member of
|
| 1145 |
+
const-qualified type (or possibly multidimensional array thereof) with
|
| 1146 |
+
no *brace-or-equal-initializer* is not const-default-constructible
|
| 1147 |
+
[[dcl.init]],
|
| 1148 |
+
- any non-variant potentially constructed subobject, except for a
|
| 1149 |
+
non-static data member with a *brace-or-equal-initializer*, has class
|
| 1150 |
+
type `M` (or possibly multidimensional array thereof) and overload
|
|
|
|
|
|
|
|
|
|
| 1151 |
resolution [[over.match]] as applied to find `M`’s corresponding
|
| 1152 |
+
constructor does not result in a usable candidate
|
| 1153 |
+
[[over.match.general]], or
|
| 1154 |
+
- any potentially constructed subobject S has class type `M` (or
|
| 1155 |
+
possibly multidimensional array thereof), `M` has a destructor that is
|
| 1156 |
+
deleted or inaccessible from the defaulted default constructor, and
|
| 1157 |
+
either S is non-variant or S has a default member initializer.
|
| 1158 |
|
| 1159 |
+
A default constructor for a class `X` is *trivial* if it is not
|
| 1160 |
+
user-provided and if
|
| 1161 |
|
| 1162 |
+
- `X` has no virtual functions [[class.virtual]] and no virtual base
|
| 1163 |
+
classes [[class.mi]], and
|
| 1164 |
+
- no non-static data member of `X` has a default member initializer
|
| 1165 |
+
[[class.mem]], and
|
| 1166 |
+
- all the direct base classes of `X` have trivial default constructors,
|
| 1167 |
+
and
|
| 1168 |
+
- either `X` is a union or for all the non-variant non-static data
|
| 1169 |
+
members of `X` that are of class type (or array thereof), each such
|
| 1170 |
+
class has a trivial default constructor.
|
| 1171 |
|
| 1172 |
Otherwise, the default constructor is *non-trivial*.
|
| 1173 |
|
| 1174 |
+
If a default constructor of a union-like class `X` is trivial, then for
|
| 1175 |
+
each union `U` that is either `X` or an anonymous union member of `X`,
|
| 1176 |
+
if the first variant member, if any, of `U` has implicit-lifetime type
|
| 1177 |
+
[[basic.types.general]], the default constructor of `X` begins the
|
| 1178 |
+
lifetime of that member if it is not the active member of its union.
|
| 1179 |
+
|
| 1180 |
+
[*Note 1*: It is already the active member if `U` was
|
| 1181 |
+
value-initialized. — *end note*]
|
| 1182 |
+
|
| 1183 |
+
Otherwise, an implicitly-defined [[dcl.fct.def.default]] default
|
| 1184 |
+
constructor performs the set of initializations of the class that would
|
| 1185 |
+
be performed by a user-written default constructor for that class with
|
| 1186 |
+
no *ctor-initializer* [[class.base.init]] and an empty
|
| 1187 |
*compound-statement*. If that user-written default constructor would be
|
| 1188 |
+
ill-formed, the program is ill-formed. The implicitly-defined default
|
| 1189 |
+
constructor is `constexpr`. Before the defaulted default constructor for
|
| 1190 |
+
a class is implicitly defined, all the non-user-provided default
|
| 1191 |
+
constructors for its base classes and its non-static data members are
|
| 1192 |
+
implicitly defined.
|
|
|
|
| 1193 |
|
| 1194 |
+
[*Note 2*: An implicitly-declared default constructor has an exception
|
| 1195 |
specification [[except.spec]]. An explicitly-defaulted definition might
|
| 1196 |
have an implicit exception specification, see
|
| 1197 |
[[dcl.fct.def]]. — *end note*]
|
| 1198 |
|
| 1199 |
+
[*Note 3*: A default constructor is implicitly invoked to initialize a
|
| 1200 |
class object when no initializer is specified [[dcl.init.general]]. Such
|
| 1201 |
+
a default constructor needs to be accessible
|
| 1202 |
[[class.access]]. — *end note*]
|
| 1203 |
|
| 1204 |
+
[*Note 4*: [[class.base.init]] describes the order in which
|
| 1205 |
constructors for base classes and non-static data members are called and
|
| 1206 |
describes how arguments can be specified for the calls to these
|
| 1207 |
constructors. — *end note*]
|
| 1208 |
|
| 1209 |
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
|
|
|
| 1331 |
X::X(const X&)
|
| 1332 |
```
|
| 1333 |
|
| 1334 |
if each potentially constructed subobject of a class type `M` (or array
|
| 1335 |
thereof) has a copy constructor whose first parameter is of type `const`
|
| 1336 |
+
`M&` or `const` `volatile` `M&`.[^2]
|
| 1337 |
|
| 1338 |
Otherwise, the implicitly-declared copy constructor will have the form
|
| 1339 |
|
| 1340 |
``` cpp
|
| 1341 |
X::X(X&)
|
|
|
|
| 1363 |
|
| 1364 |
An implicitly-declared copy/move constructor is an inline public member
|
| 1365 |
of its class. A defaulted copy/move constructor for a class `X` is
|
| 1366 |
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 1367 |
|
| 1368 |
+
- a potentially constructed subobject of type `M` (or possibly
|
| 1369 |
+
multidimensional array thereof) for which overload resolution
|
| 1370 |
[[over.match]], as applied to find `M`’s corresponding constructor,
|
| 1371 |
+
either does not result in a usable candidate [[over.match.general]]
|
| 1372 |
+
or, in the case of a variant member, selects a non-trivial function,
|
| 1373 |
+
- any potentially constructed subobject of class type `M` (or possibly
|
| 1374 |
+
multidimensional array thereof) where `M` has a destructor that is
|
| 1375 |
+
deleted or inaccessible from the defaulted constructor, or,
|
|
|
|
| 1376 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 1377 |
type.
|
| 1378 |
|
| 1379 |
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 1380 |
ignored by overload resolution [[over.match]], [[over.over]]. Such a
|
| 1381 |
constructor would otherwise interfere with initialization from an rvalue
|
| 1382 |
which can use the copy constructor instead. — *end note*]
|
| 1383 |
|
| 1384 |
A copy/move constructor for class `X` is trivial if it is not
|
| 1385 |
+
user-provided and if
|
| 1386 |
|
| 1387 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 1388 |
base classes [[class.mi]], and
|
| 1389 |
- the constructor selected to copy/move each direct base class subobject
|
| 1390 |
is trivial, and
|
|
|
|
| 1396 |
|
| 1397 |
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 1398 |
implementation elided its odr-use
|
| 1399 |
[[term.odr.use]], [[class.temporary]]. — *end note*]
|
| 1400 |
|
| 1401 |
+
The implicitly-defined [[dcl.fct.def.default]] constructor is
|
| 1402 |
+
`constexpr`.
|
|
|
|
| 1403 |
|
| 1404 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 1405 |
defined, all non-user-provided copy/move constructors for its
|
| 1406 |
potentially constructed subobjects are implicitly defined.
|
| 1407 |
|
|
|
|
| 1410 |
|
| 1411 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 1412 |
performs a memberwise copy/move of its bases and members.
|
| 1413 |
|
| 1414 |
[*Note 7*: Default member initializers of non-static data members are
|
| 1415 |
+
ignored. — *end note*]
|
| 1416 |
|
| 1417 |
The order of initialization is the same as the order of initialization
|
| 1418 |
of bases and members in a user-defined constructor (see
|
| 1419 |
[[class.base.init]]). Let `x` be either the parameter of the constructor
|
| 1420 |
or, for the move constructor, an xvalue referring to the parameter. Each
|
|
|
|
| 1441 |
### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
|
| 1442 |
|
| 1443 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 1444 |
non-static non-template member function of class `X` with exactly one
|
| 1445 |
non-object parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
|
| 1446 |
+
`const volatile X&`.[^3]
|
| 1447 |
|
| 1448 |
+
[*Note 1*: More than one form of copy assignment operator can be
|
|
|
|
|
|
|
|
|
|
| 1449 |
declared for a class. — *end note*]
|
| 1450 |
|
| 1451 |
+
[*Note 2*:
|
| 1452 |
|
| 1453 |
+
If a class `X` only has a copy assignment operator with a non-object
|
| 1454 |
+
parameter of type `X&`, an expression of type const `X` cannot be
|
| 1455 |
+
assigned to an object of type `X`.
|
| 1456 |
|
| 1457 |
[*Example 1*:
|
| 1458 |
|
| 1459 |
``` cpp
|
| 1460 |
struct X {
|
|
|
|
| 1486 |
```
|
| 1487 |
|
| 1488 |
if
|
| 1489 |
|
| 1490 |
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 1491 |
+
non-object parameter is of type `const B&`, `const volatile B&`, or
|
| 1492 |
+
`B`, and
|
| 1493 |
- for all the non-static data members of `X` that are of a class type
|
| 1494 |
`M` (or array thereof), each such class type has a copy assignment
|
| 1495 |
+
operator whose non-object parameter is of type `const M&`,
|
| 1496 |
+
`const volatile M&`, or `M`.[^4]
|
| 1497 |
|
| 1498 |
Otherwise, the implicitly-declared copy assignment operator will have
|
| 1499 |
the form
|
| 1500 |
|
| 1501 |
``` cpp
|
|
|
|
| 1505 |
A user-declared move assignment operator `X::operator=` is a non-static
|
| 1506 |
non-template member function of class `X` with exactly one non-object
|
| 1507 |
parameter of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 1508 |
`const volatile X&&`.
|
| 1509 |
|
| 1510 |
+
[*Note 3*: More than one form of move assignment operator can be
|
|
|
|
|
|
|
|
|
|
| 1511 |
declared for a class. — *end note*]
|
| 1512 |
|
| 1513 |
If the definition of a class `X` does not explicitly declare a move
|
| 1514 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1515 |
only if
|
|
|
|
| 1556 |
operator is an inline public member of its class.
|
| 1557 |
|
| 1558 |
A defaulted copy/move assignment operator for class `X` is defined as
|
| 1559 |
deleted if `X` has:
|
| 1560 |
|
| 1561 |
+
- a non-static data member of `const` non-class type (or possibly
|
| 1562 |
+
multidimensional array thereof), or
|
|
|
|
|
|
|
| 1563 |
- a non-static data member of reference type, or
|
| 1564 |
+
- a direct non-static data member of class type `M` (or possibly
|
| 1565 |
+
multidimensional array thereof) or a direct base class `M` that cannot
|
| 1566 |
+
be copied/moved because overload resolution [[over.match]], as applied
|
| 1567 |
+
to find `M`’s corresponding assignment operator, either does not
|
| 1568 |
+
result in a usable candidate [[over.match.general]] or, in the case of
|
| 1569 |
+
a variant member, selects a non-trivial function.
|
| 1570 |
|
| 1571 |
+
[*Note 4*: A defaulted move assignment operator that is defined as
|
| 1572 |
deleted is ignored by overload resolution
|
| 1573 |
[[over.match]], [[over.over]]. — *end note*]
|
| 1574 |
|
| 1575 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1576 |
class if not declared by the user, a base class copy/move assignment
|
| 1577 |
operator is always hidden by the corresponding assignment operator of a
|
| 1578 |
+
derived class [[over.assign]].
|
| 1579 |
|
| 1580 |
+
[*Note 5*: A *using-declaration* in a derived class `C` that names an
|
| 1581 |
assignment operator from a base class never suppresses the implicit
|
| 1582 |
declaration of an assignment operator of `C`, even if the base class
|
| 1583 |
assignment operator would be a copy or move assignment operator if
|
| 1584 |
declared as a member of `C`. — *end note*]
|
| 1585 |
|
| 1586 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1587 |
+
user-provided and if
|
| 1588 |
|
| 1589 |
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 1590 |
base classes [[class.mi]], and
|
| 1591 |
- the assignment operator selected to copy/move each direct base class
|
| 1592 |
subobject is trivial, and
|
|
|
|
| 1602 |
Before the defaulted copy/move assignment operator for a class is
|
| 1603 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1604 |
for its direct base classes and its non-static data members are
|
| 1605 |
implicitly defined.
|
| 1606 |
|
| 1607 |
+
[*Note 6*: An implicitly-declared copy/move assignment operator has an
|
| 1608 |
implied exception specification [[except.spec]]. — *end note*]
|
| 1609 |
|
| 1610 |
The implicitly-defined copy/move assignment operator for a non-union
|
| 1611 |
class `X` performs memberwise copy/move assignment of its subobjects.
|
| 1612 |
The direct base classes of `X` are assigned first, in the order of their
|
|
|
|
| 1681 |
`~`*class-name* and the *class-name* is the injected-class-name of the
|
| 1682 |
class nominated by the *nested-name-specifier*.
|
| 1683 |
|
| 1684 |
A prospective destructor shall take no arguments [[dcl.fct]]. Each
|
| 1685 |
*decl-specifier* of the *decl-specifier-seq* of a prospective destructor
|
| 1686 |
+
declaration (if any) shall be `friend`, `inline`, `virtual`, or
|
| 1687 |
+
`constexpr`.
|
| 1688 |
|
| 1689 |
If a class has no user-declared prospective destructor, a prospective
|
| 1690 |
destructor is implicitly declared as defaulted [[dcl.fct.def]]. An
|
| 1691 |
implicitly-declared prospective destructor is an inline public member of
|
| 1692 |
its class.
|
|
|
|
| 1719 |
|
| 1720 |
[*Note 2*: A declaration of a destructor that does not have a
|
| 1721 |
*noexcept-specifier* has the same exception specification as if it had
|
| 1722 |
been implicitly declared [[except.spec]]. — *end note*]
|
| 1723 |
|
| 1724 |
+
A defaulted destructor for a class `X` is defined as deleted if
|
| 1725 |
|
| 1726 |
+
- `X` is a non-union class and any non-variant potentially constructed
|
| 1727 |
+
subobject has class type `M` (or possibly multidimensional array
|
| 1728 |
+
thereof) where `M` has a destructor that is deleted or is inaccessible
|
| 1729 |
+
from the defaulted destructor,
|
| 1730 |
+
- `X` is a union and
|
| 1731 |
+
- overload resolution to select a constructor to default-initialize an
|
| 1732 |
+
object of type `X` either fails or selects a constructor that is
|
| 1733 |
+
either deleted or not trivial, or
|
| 1734 |
+
- `X` has a variant member `V` of class type `M` (or possibly
|
| 1735 |
+
multi-dimensional array thereof) where `V` has a default member
|
| 1736 |
+
initializer and `M` has a destructor that is non-trivial, or,
|
| 1737 |
+
- for a virtual destructor, lookup of the non-array deallocation
|
| 1738 |
function results in an ambiguity or in a function that is deleted or
|
| 1739 |
inaccessible from the defaulted destructor.
|
| 1740 |
|
| 1741 |
+
A destructor for a class `X` is trivial if it is not user-provided and
|
| 1742 |
+
if
|
| 1743 |
|
| 1744 |
- the destructor is not virtual,
|
| 1745 |
+
- all of the direct base classes of `X` have trivial destructors, and
|
| 1746 |
+
- either `X` is a union or for all of the non-variant non-static data
|
| 1747 |
+
members of `X` that are of class type (or array thereof), each such
|
| 1748 |
+
class has a trivial destructor.
|
| 1749 |
|
| 1750 |
Otherwise, the destructor is *non-trivial*.
|
| 1751 |
|
| 1752 |
+
A defaulted destructor is a constexpr destructor.
|
|
|
|
| 1753 |
|
| 1754 |
Before a defaulted destructor for a class is implicitly defined, all the
|
| 1755 |
non-user-provided destructors for its base classes and its non-static
|
| 1756 |
data members are implicitly defined.
|
| 1757 |
|
|
|
|
| 1764 |
during destruction; see [[class.cdtor]]. — *end note*]
|
| 1765 |
|
| 1766 |
After executing the body of the destructor and destroying any objects
|
| 1767 |
with automatic storage duration allocated within the body, a destructor
|
| 1768 |
for class `X` calls the destructors for `X`’s direct non-variant
|
| 1769 |
+
non-static data members other than anonymous unions, the destructors for
|
| 1770 |
+
`X`’s non-virtual direct base classes and, if `X` is the most derived
|
| 1771 |
+
class [[class.base.init]], its destructor calls the destructors for
|
| 1772 |
+
`X`’s virtual base classes. All destructors are called as if they were
|
| 1773 |
+
referenced with a qualified name, that is, ignoring any possible virtual
|
| 1774 |
+
overriding destructors in more derived classes. Bases and members are
|
| 1775 |
+
destroyed in the reverse order of the completion of their constructor
|
| 1776 |
+
(see [[class.base.init]]).
|
| 1777 |
|
| 1778 |
[*Note 4*: A `return` statement [[stmt.return]] in a destructor might
|
| 1779 |
not directly return to the caller; before transferring control to the
|
| 1780 |
caller, the destructors for the members and bases are
|
| 1781 |
called. — *end note*]
|
|
|
|
| 1821 |
[*Note 6*: This assures that a deallocation function corresponding to
|
| 1822 |
the dynamic type of an object is available for the *delete-expression*
|
| 1823 |
[[class.free]]. — *end note*]
|
| 1824 |
|
| 1825 |
In an explicit destructor call, the destructor is specified by a `~`
|
| 1826 |
+
followed by a *type-name* or *computed-type-specifier* that denotes the
|
| 1827 |
destructor’s class type. The invocation of a destructor is subject to
|
| 1828 |
the usual rules for member functions [[class.mfct]]; that is, if the
|
| 1829 |
object is not of the destructor’s class type and not of a class derived
|
| 1830 |
from the destructor’s class type (including when the destructor is
|
| 1831 |
invoked via a null pointer value), the program has undefined behavior.
|
|
|
|
| 1868 |
|
| 1869 |
Explicit calls of destructors are rarely needed. One use of such calls
|
| 1870 |
is for objects placed at specific addresses using a placement
|
| 1871 |
*new-expression*. Such use of explicit placement and destruction of
|
| 1872 |
objects can be necessary to cope with dedicated hardware resources and
|
| 1873 |
+
for writing memory management facilities.
|
| 1874 |
+
|
| 1875 |
+
[*Example 2*:
|
| 1876 |
|
| 1877 |
``` cpp
|
| 1878 |
void* operator new(std::size_t, void* p) { return p; }
|
| 1879 |
struct X {
|
| 1880 |
X(int);
|
|
|
|
| 1888 |
f(p);
|
| 1889 |
p->X::~X(); // cleanup
|
| 1890 |
}
|
| 1891 |
```
|
| 1892 |
|
| 1893 |
+
— *end example*]
|
| 1894 |
+
|
| 1895 |
— *end note*]
|
| 1896 |
|
| 1897 |
Once a destructor is invoked for an object, the object’s lifetime ends;
|
| 1898 |
the behavior is undefined if the destructor is invoked for an object
|
| 1899 |
whose lifetime has ended [[basic.life]].
|
| 1900 |
|
| 1901 |
+
[*Example 3*: If the destructor for an object with automatic storage
|
| 1902 |
duration is explicitly invoked, and the block is subsequently left in a
|
| 1903 |
manner that would ordinarily invoke implicit destruction of the object,
|
| 1904 |
the behavior is undefined. — *end example*]
|
| 1905 |
|
| 1906 |
[*Note 10*:
|
|
|
|
| 1934 |
[[class.member.lookup]], [[class.conv.fct]]. Conversions obey the access
|
| 1935 |
control rules [[class.access]]. Access control is applied after
|
| 1936 |
ambiguity resolution [[basic.lookup]].
|
| 1937 |
|
| 1938 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 1939 |
+
conversions in function calls. — *end note*]
|
| 1940 |
|
| 1941 |
At most one user-defined conversion (constructor or conversion function)
|
| 1942 |
is implicitly applied to a single value.
|
| 1943 |
|
| 1944 |
[*Example 1*:
|
|
|
|
| 1961 |
|
| 1962 |
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 1963 |
|
| 1964 |
A constructor that is not explicit [[dcl.fct.spec]] specifies a
|
| 1965 |
conversion from the types of its parameters (if any) to the type of its
|
| 1966 |
+
class.
|
| 1967 |
|
| 1968 |
[*Example 1*:
|
| 1969 |
|
| 1970 |
``` cpp
|
| 1971 |
struct X {
|
|
|
|
| 2017 |
|
| 2018 |
— *end example*]
|
| 2019 |
|
| 2020 |
— *end note*]
|
| 2021 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2022 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 2023 |
|
| 2024 |
``` bnf
|
| 2025 |
conversion-function-id:
|
| 2026 |
operator conversion-type-id
|
|
|
|
| 2039 |
A declaration whose *declarator-id* has an *unqualified-id* that is a
|
| 2040 |
*conversion-function-id* declares a *conversion function*; its
|
| 2041 |
*declarator* shall be a function declarator [[dcl.fct]] of the form
|
| 2042 |
|
| 2043 |
``` bnf
|
| 2044 |
+
noptr-declarator parameters-and-qualifiers
|
|
|
|
| 2045 |
```
|
| 2046 |
|
| 2047 |
+
where the *noptr-declarator* consists solely of an *id-expression*, an
|
| 2048 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 2049 |
parentheses, and the *id-expression* has one of the following forms:
|
| 2050 |
|
| 2051 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 2052 |
of a class or class template but is not a friend declaration
|
| 2053 |
[[class.friend]], the *id-expression* is a *conversion-function-id*;
|
| 2054 |
- otherwise, the *id-expression* is a *qualified-id* whose
|
| 2055 |
*unqualified-id* is a *conversion-function-id*.
|
| 2056 |
|
| 2057 |
A conversion function shall have no non-object parameters and shall be a
|
| 2058 |
+
non-static member function of a class or class template `X`; its
|
| 2059 |
+
declared return type is the *conversion-type-id* and it specifies a
|
| 2060 |
+
conversion from `X` to the type specified by the *conversion-type-id*,
|
| 2061 |
+
interpreted as a *type-id* [[dcl.name]]. A *decl-specifier* in the
|
| 2062 |
+
*decl-specifier-seq* of a conversion function (if any) shall not be a
|
| 2063 |
+
*defining-type-specifier*.
|
| 2064 |
|
| 2065 |
+
[*Note 1*: A conversion function is never invoked for implicit or
|
| 2066 |
+
explicit conversions of an object to the same object type (or a
|
| 2067 |
+
reference to it), to a base class of that type (or a reference to it),
|
| 2068 |
+
or to cv `void`. Even though never directly called to perform a
|
| 2069 |
+
conversion, such conversion functions can be declared and can
|
| 2070 |
+
potentially be reached through a call to a virtual conversion function
|
| 2071 |
+
in a base class. — *end note*]
|
|
|
|
| 2072 |
|
| 2073 |
[*Example 1*:
|
| 2074 |
|
| 2075 |
``` cpp
|
| 2076 |
struct X {
|
|
|
|
| 2122 |
The *conversion-type-id* shall not represent a function type nor an
|
| 2123 |
array type. The *conversion-type-id* in a *conversion-function-id* is
|
| 2124 |
the longest sequence of tokens that could possibly form a
|
| 2125 |
*conversion-type-id*.
|
| 2126 |
|
| 2127 |
+
[*Note 2*:
|
| 2128 |
|
| 2129 |
This prevents ambiguities between the declarator operator `*` and its
|
| 2130 |
expression counterparts.
|
| 2131 |
|
| 2132 |
[*Example 3*:
|
|
|
|
| 2151 |
|
| 2152 |
— *end example*]
|
| 2153 |
|
| 2154 |
— *end note*]
|
| 2155 |
|
| 2156 |
+
[*Note 3*:
|
| 2157 |
|
| 2158 |
A conversion function in a derived class hides only conversion functions
|
| 2159 |
in base classes that convert to the same type. A conversion function
|
| 2160 |
template with a dependent return type hides only templates in base
|
| 2161 |
classes that correspond to it [[class.member.lookup]]; otherwise, it
|
|
|
|
| 2450 |
[*Note 1*:
|
| 2451 |
|
| 2452 |
However, when the *cast-expression* of a *delete-expression* refers to
|
| 2453 |
an object of class type with a virtual destructor, because the
|
| 2454 |
deallocation function is chosen by the destructor of the dynamic type of
|
| 2455 |
+
the object, the effect is the same in that case.
|
| 2456 |
+
|
| 2457 |
+
[*Example 3*:
|
| 2458 |
|
| 2459 |
``` cpp
|
| 2460 |
struct B {
|
| 2461 |
virtual ~B();
|
| 2462 |
void operator delete(void*, std::size_t);
|
|
|
|
| 2486 |
Here, storage for the object of class `D` is deallocated by
|
| 2487 |
`D::operator delete()`, and the object of class `E` is destroyed and its
|
| 2488 |
storage is deallocated by `E::operator delete()`, due to the virtual
|
| 2489 |
destructor.
|
| 2490 |
|
| 2491 |
+
— *end example*]
|
| 2492 |
+
|
| 2493 |
— *end note*]
|
| 2494 |
|
| 2495 |
[*Note 2*:
|
| 2496 |
|
| 2497 |
Virtual destructors have no effect on the deallocation function actually
|
| 2498 |
called when the *cast-expression* of a *delete-expression* refers to an
|
| 2499 |
+
array of objects of class type.
|
| 2500 |
+
|
| 2501 |
+
[*Example 4*:
|
| 2502 |
|
| 2503 |
``` cpp
|
| 2504 |
struct B {
|
| 2505 |
virtual ~B();
|
| 2506 |
void operator delete[](void*, std::size_t);
|
|
|
|
| 2516 |
B* bp = new D[i];
|
| 2517 |
delete[] bp; // undefined behavior
|
| 2518 |
}
|
| 2519 |
```
|
| 2520 |
|
| 2521 |
+
— *end example*]
|
| 2522 |
+
|
| 2523 |
— *end note*]
|
| 2524 |
|
| 2525 |
Access to the deallocation function is checked statically, even if a
|
| 2526 |
different one is actually executed.
|
| 2527 |
|
| 2528 |
+
[*Example 5*: For the call on line “// 1” above, if
|
| 2529 |
`B::operator delete()` had been private, the delete expression would
|
| 2530 |
have been ill-formed. — *end example*]
|
| 2531 |
|
| 2532 |
[*Note 3*: If a deallocation function has no explicit
|
| 2533 |
*noexcept-specifier*, it has a non-throwing exception specification
|
|
|
|
| 2620 |
|
| 2621 |
[*Note 1*: One special guarantee is made in order to simplify the use
|
| 2622 |
of unions: If a standard-layout union contains several standard-layout
|
| 2623 |
structs that share a common initial sequence [[class.mem]], and if a
|
| 2624 |
non-static data member of an object of this standard-layout union type
|
| 2625 |
+
is active and is one of the standard-layout structs, the common initial
|
| 2626 |
+
sequence of any of the standard-layout struct members can be inspected;
|
| 2627 |
+
see [[class.mem]]. — *end note*]
|
| 2628 |
|
| 2629 |
The size of a union is sufficient to contain the largest of its
|
| 2630 |
non-static data members. Each non-static data member is allocated as if
|
| 2631 |
it were the sole member of a non-union class.
|
| 2632 |
|
|
|
|
| 2636 |
address. — *end note*]
|
| 2637 |
|
| 2638 |
A union can have member functions (including constructors and
|
| 2639 |
destructors), but it shall not have virtual [[class.virtual]] functions.
|
| 2640 |
A union shall not have base classes. A union shall not be used as a base
|
| 2641 |
+
class. If a union contains a non-static data member of reference type,
|
| 2642 |
the program is ill-formed.
|
| 2643 |
|
| 2644 |
[*Note 3*:
|
| 2645 |
|
| 2646 |
+
If any non-static data member of a union has a non-trivial copy
|
| 2647 |
+
constructor, move constructor [[class.copy.ctor]], copy assignment
|
| 2648 |
+
operator, or move assignment operator [[class.copy.assign]], the
|
| 2649 |
+
corresponding member function of the union must be user-provided or it
|
| 2650 |
+
will be implicitly deleted [[dcl.fct.def.delete]] for the union.
|
|
|
|
|
|
|
| 2651 |
|
| 2652 |
[*Example 1*:
|
| 2653 |
|
| 2654 |
Consider the following union:
|
| 2655 |
|
|
|
|
| 2661 |
};
|
| 2662 |
```
|
| 2663 |
|
| 2664 |
Since `std::string` [[string.classes]] declares non-trivial versions of
|
| 2665 |
all of the special member functions, `U` will have an implicitly deleted
|
| 2666 |
+
copy/move constructor and copy/move assignment operator. The default
|
| 2667 |
+
constructor and destructor of `U` are both trivial even though
|
| 2668 |
+
`std::string` has a non-trivial default constructor and a non-trivial
|
| 2669 |
+
destructor.
|
| 2670 |
|
| 2671 |
— *end example*]
|
| 2672 |
|
| 2673 |
— *end note*]
|
| 2674 |
|
|
|
|
| 2685 |
subscripting operator, S(E) is S(A) if `A` is of array type, S(B) if
|
| 2686 |
`B` is of array type, and empty otherwise.
|
| 2687 |
- Otherwise, S(E) is empty.
|
| 2688 |
|
| 2689 |
In an assignment expression of the form `E1 = E2` that uses either the
|
| 2690 |
+
built-in assignment operator [[expr.assign]] or a trivial assignment
|
| 2691 |
+
operator [[class.copy.assign]], for each element `X` of S(`E1`) and each
|
| 2692 |
+
anonymous union member `X` [[class.union.anon]] that is a member of a
|
| 2693 |
+
union and has such an element as an immediate subobject (recursively),
|
| 2694 |
+
if modification of `X` would have undefined behavior under
|
| 2695 |
+
[[basic.life]], an object of the type of `X` is implicitly created in
|
| 2696 |
+
the nominated storage; no initialization is performed and the beginning
|
| 2697 |
+
of its lifetime is sequenced after the value computation of the left and
|
| 2698 |
+
right operands and before the assignment.
|
| 2699 |
|
| 2700 |
[*Note 4*: This ends the lifetime of the previously-active member of
|
| 2701 |
the union, if any [[basic.life]]. — *end note*]
|
| 2702 |
|
| 2703 |
[*Example 2*:
|
|
|
|
| 2778 |
Here `a` and `p` are used like ordinary (non-member) variables, but
|
| 2779 |
since they are union members they have the same address.
|
| 2780 |
|
| 2781 |
— *end example*]
|
| 2782 |
|
| 2783 |
+
An anonymous union declared in the scope of a namespace with external
|
| 2784 |
+
linkage shall use the *storage-class-specifier* `static`. Anonymous
|
| 2785 |
+
unions declared at block scope shall not use a *storage-class-specifier*
|
| 2786 |
+
that is not permitted in the declaration of a block variable. An
|
| 2787 |
+
anonymous union declaration at class scope shall not have a
|
| 2788 |
+
*storage-class-specifier*.
|
| 2789 |
|
| 2790 |
[*Note 1*:
|
| 2791 |
|
| 2792 |
A union for which objects, pointers, or references are declared is not
|
| 2793 |
an anonymous union.
|
|
|
|
| 2877 |
An enclosing function has no special access to members of the local
|
| 2878 |
class; it obeys the usual access rules [[class.access]]. Member
|
| 2879 |
functions of a local class shall be defined within their class
|
| 2880 |
definition, if they are defined at all.
|
| 2881 |
|
| 2882 |
+
A class nested within a local class is a local class. A member of a
|
| 2883 |
+
local class `X` shall be declared only in the definition of `X` or, if
|
| 2884 |
+
the member is a nested class, in the nearest enclosing block scope of
|
| 2885 |
+
`X`.
|
| 2886 |
|
| 2887 |
[*Note 2*: A local class cannot have static data members
|
| 2888 |
[[class.static.data]]. — *end note*]
|
| 2889 |
|
| 2890 |
## Derived classes <a id="class.derived">[[class.derived]]</a>
|
|
|
|
| 2914 |
|
| 2915 |
``` bnf
|
| 2916 |
class-or-decltype:
|
| 2917 |
nested-name-specifierₒₚₜ type-name
|
| 2918 |
nested-name-specifier template simple-template-id
|
| 2919 |
+
computed-type-specifier
|
| 2920 |
```
|
| 2921 |
|
| 2922 |
``` bnf
|
| 2923 |
access-specifier:
|
| 2924 |
private
|
|
|
|
| 2933 |
*nested-name-specifier*, *type-name*, and/or *simple-template-id*. A
|
| 2934 |
*class-or-decltype* shall denote a (possibly cv-qualified) class type
|
| 2935 |
that is not an incompletely defined class [[class.mem]]; any
|
| 2936 |
cv-qualifiers are ignored. The class denoted by the *class-or-decltype*
|
| 2937 |
of a *base-specifier* is called a *direct base class* for the class
|
| 2938 |
+
being defined; for each such *base-specifier*, the corresponding
|
| 2939 |
+
*direct base class relationship* is the ordered pair (`D`, `B`) where
|
| 2940 |
+
`D` is the class being defined and `B` is the direct base class. The
|
| 2941 |
+
lookup for the component name of the *type-name* or *simple-template-id*
|
| 2942 |
+
is type-only [[basic.lookup]]. A class `B` is a base class of a class
|
| 2943 |
+
`D` if it is a direct base class of `D` or a direct base class of one of
|
| 2944 |
+
`D`’s base classes. A class is an *indirect base class* of another if it
|
| 2945 |
+
is a base class but not a direct base class. A class is said to be
|
| 2946 |
+
(directly or indirectly) *derived* from its (direct or indirect) base
|
| 2947 |
+
classes.
|
| 2948 |
|
| 2949 |
[*Note 1*: See [[class.access]] for the meaning of
|
| 2950 |
*access-specifier*. — *end note*]
|
| 2951 |
|
| 2952 |
Members of a base class are also members of the derived class.
|
|
|
|
| 3157 |
|
| 3158 |
### Virtual functions <a id="class.virtual">[[class.virtual]]</a>
|
| 3159 |
|
| 3160 |
A non-static member function is a *virtual function* if it is first
|
| 3161 |
declared with the keyword `virtual` or if it overrides a virtual member
|
| 3162 |
+
function declared in a base class (see below).[^5]
|
| 3163 |
|
| 3164 |
[*Note 1*: Virtual functions support dynamic binding and
|
| 3165 |
object-oriented programming. — *end note*]
|
| 3166 |
|
| 3167 |
A class with a virtual member function is called a
|
| 3168 |
+
*polymorphic class*.[^6]
|
| 3169 |
|
| 3170 |
If a virtual member function F is declared in a class B, and, in a class
|
| 3171 |
D derived (directly or indirectly) from B, a declaration of a member
|
| 3172 |
function G corresponds [[basic.scope.scope]] to a declaration of F,
|
| 3173 |
+
ignoring trailing *requires-clause*s, then G *overrides*[^7]
|
| 3174 |
|
| 3175 |
+
F. For convenience, we say that any virtual function overrides itself. A
|
| 3176 |
virtual member function V of a class object S is a *final overrider*
|
| 3177 |
unless the most derived class [[intro.object]] of which S is a base
|
| 3178 |
class subobject (if any) has another member function that overrides V.
|
| 3179 |
In a derived class, if a virtual member function of a base class
|
| 3180 |
+
subobject has more than one final overrider, the program is ill-formed.
|
| 3181 |
|
| 3182 |
[*Example 1*:
|
| 3183 |
|
| 3184 |
``` cpp
|
| 3185 |
struct A {
|
|
|
|
| 3297 |
classes of the functions. If a function `D::f` overrides a function
|
| 3298 |
`B::f`, the return types of the functions are covariant if they satisfy
|
| 3299 |
the following criteria:
|
| 3300 |
|
| 3301 |
- both are pointers to classes, both are lvalue references to classes,
|
| 3302 |
+
or both are rvalue references to classes[^8]
|
| 3303 |
- the class in the return type of `B::f` is the same class as the class
|
| 3304 |
in the return type of `D::f`, or is an unambiguous and accessible
|
| 3305 |
direct or indirect base class of the class in the return type of
|
| 3306 |
`D::f`
|
| 3307 |
- both pointers or references have the same cv-qualification and the
|
|
|
|
| 3617 |
[*Note 1*: A constructor or destructor can be named by an expression
|
| 3618 |
[[basic.def.odr]] even though it has no name. — *end note*]
|
| 3619 |
|
| 3620 |
A member of a class can also access all the members to which the class
|
| 3621 |
has access. A local class of a member function may access the same
|
| 3622 |
+
members that the member function itself may access.[^9]
|
| 3623 |
|
| 3624 |
+
Members of a class defined with the keyword `class` are private by
|
| 3625 |
default. Members of a class defined with the keywords `struct` or
|
| 3626 |
`union` are public by default.
|
| 3627 |
|
| 3628 |
[*Example 1*:
|
| 3629 |
|
|
|
|
| 3666 |
— *end example*]
|
| 3667 |
|
| 3668 |
[*Note 3*:
|
| 3669 |
|
| 3670 |
Because access control applies to the declarations named, if access
|
| 3671 |
+
control is applied to a type alias, only the accessibility of the
|
| 3672 |
typedef or alias declaration itself is considered. The accessibility of
|
| 3673 |
+
the underlying entity is not considered.
|
| 3674 |
+
|
| 3675 |
+
[*Example 3*:
|
| 3676 |
|
| 3677 |
``` cpp
|
| 3678 |
class A {
|
| 3679 |
class B { };
|
| 3680 |
public:
|
|
|
|
| 3685 |
A::BB x; // OK, typedef A::BB is public
|
| 3686 |
A::B y; // access error, A::B is private
|
| 3687 |
}
|
| 3688 |
```
|
| 3689 |
|
| 3690 |
+
— *end example*]
|
| 3691 |
+
|
| 3692 |
— *end note*]
|
| 3693 |
|
| 3694 |
[*Note 4*: Access control does not prevent members from being found by
|
| 3695 |
name lookup or implicit conversions to base classes from being
|
| 3696 |
considered. — *end note*]
|
|
|
|
| 3706 |
appearing outside the class’s *member-specification*.
|
| 3707 |
|
| 3708 |
[*Note 5*: This access also applies to implicit references to
|
| 3709 |
constructors, conversion functions, and destructors. — *end note*]
|
| 3710 |
|
| 3711 |
+
[*Example 4*:
|
| 3712 |
|
| 3713 |
``` cpp
|
| 3714 |
class A {
|
| 3715 |
typedef int I; // private member
|
| 3716 |
+
I f() pre(A::x > 0);
|
| 3717 |
+
friend I g(I) post(A::x <= 0);
|
| 3718 |
static I x;
|
| 3719 |
template<int> struct Q;
|
| 3720 |
template<int> friend struct R;
|
| 3721 |
protected:
|
| 3722 |
struct B { };
|
| 3723 |
};
|
| 3724 |
|
| 3725 |
+
A::I A::f() pre(A::x > 0) { return 0; }
|
| 3726 |
+
A::I g(A::I p = A::x) post(A::x <= 0);
|
| 3727 |
A::I g(A::I p) { return 0; }
|
| 3728 |
A::I A::x = 0;
|
| 3729 |
template<A::I> struct A::Q { };
|
| 3730 |
template<A::I> struct R { };
|
| 3731 |
|
|
|
|
| 3750 |
[[temp.inst]].
|
| 3751 |
|
| 3752 |
Access for a default *template-argument* [[temp.param]] is checked in
|
| 3753 |
the context in which it appears rather than at any points of use of it.
|
| 3754 |
|
| 3755 |
+
[*Example 5*:
|
| 3756 |
|
| 3757 |
``` cpp
|
| 3758 |
class B { };
|
| 3759 |
template <class T> class C {
|
| 3760 |
protected:
|
|
|
|
| 3861 |
another class using the `protected` access specifier, the public and
|
| 3862 |
protected members of the base class are accessible as protected members
|
| 3863 |
of the derived class. If a class is declared to be a base class for
|
| 3864 |
another class using the `private` access specifier, the public and
|
| 3865 |
protected members of the base class are accessible as private members of
|
| 3866 |
+
the derived class.[^10]
|
| 3867 |
|
| 3868 |
In the absence of an *access-specifier* for a base class, `public` is
|
| 3869 |
assumed when the derived class is defined with the *class-key* `struct`
|
| 3870 |
and `private` is assumed when the class is defined with the *class-key*
|
| 3871 |
`class`.
|
|
|
|
| 3895 |
accessible directly. Because of the rules on pointer conversions
|
| 3896 |
[[conv.ptr]] and explicit casts
|
| 3897 |
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]], a conversion
|
| 3898 |
from a pointer to a derived class to a pointer to an inaccessible base
|
| 3899 |
class can be ill-formed if an implicit conversion is used, but
|
| 3900 |
+
well-formed if an explicit cast is used.
|
| 3901 |
+
|
| 3902 |
+
[*Example 2*:
|
| 3903 |
|
| 3904 |
``` cpp
|
| 3905 |
class B {
|
| 3906 |
public:
|
| 3907 |
int mi; // non-static member
|
|
|
|
| 3924 |
::B* bp2 = (::B*)this; // OK with cast
|
| 3925 |
bp2->mi = 3; // OK, access through a pointer to B.
|
| 3926 |
}
|
| 3927 |
```
|
| 3928 |
|
| 3929 |
+
— *end example*]
|
| 3930 |
+
|
| 3931 |
— *end note*]
|
| 3932 |
|
| 3933 |
A base class `B` of `N` is *accessible* at *R*, if
|
| 3934 |
|
| 3935 |
- an invented public member of `B` would be a public member of `N`, or
|
|
|
|
| 3939 |
`N`, and an invented public member of `B` would be a private or
|
| 3940 |
protected member of `P`, or
|
| 3941 |
- there exists a class `S` such that `B` is a base class of `S`
|
| 3942 |
accessible at *R* and `S` is a base class of `N` accessible at *R*.
|
| 3943 |
|
| 3944 |
+
[*Example 3*:
|
| 3945 |
|
| 3946 |
``` cpp
|
| 3947 |
class B {
|
| 3948 |
public:
|
| 3949 |
int m;
|
|
|
|
| 3970 |
|
| 3971 |
[*Note 2*: It follows that members and friends of a class `X` can
|
| 3972 |
implicitly convert an `X*` to a pointer to a private or protected
|
| 3973 |
immediate base class of `X`. — *end note*]
|
| 3974 |
|
| 3975 |
+
An expression E that designates a member `m` has a *designating class*
|
| 3976 |
+
that affects the access to `m`. This designating class is either
|
| 3977 |
+
|
| 3978 |
+
- the innermost class of which `m` is directly a member if E is a
|
| 3979 |
+
*splice-expression* or
|
| 3980 |
+
- the class in whose scope name lookup performed a search that found `m`
|
| 3981 |
+
otherwise.
|
| 3982 |
|
| 3983 |
[*Note 3*: This class can be explicit, e.g., when a *qualified-id* is
|
| 3984 |
used, or implicit, e.g., when a class member access operator
|
| 3985 |
[[expr.ref]] is used (including cases where an implicit “`this->`” is
|
| 3986 |
added). If both a class member access operator and a *qualified-id* are
|
| 3987 |
+
used to name the member (as in `p->T::m`), the class designating the
|
| 3988 |
+
member is the class designated by the *nested-name-specifier* of the
|
| 3989 |
*qualified-id* (that is, `T`). — *end note*]
|
| 3990 |
|
| 3991 |
+
A member `m` is accessible at the point *R* when designated in class `N`
|
| 3992 |
+
if
|
| 3993 |
|
| 3994 |
+
- `m` is designated by a *splice-expression*, or
|
| 3995 |
- `m` as a member of `N` is public, or
|
| 3996 |
- `m` as a member of `N` is private, and *R* occurs in a direct member
|
| 3997 |
or friend of class `N`, or
|
| 3998 |
- `m` as a member of `N` is protected, and *R* occurs in a direct member
|
| 3999 |
or friend of class `N`, or in a member of a class `P` derived from
|
| 4000 |
`N`, where `m` as a member of `P` is public, private, or protected, or
|
| 4001 |
- there exists a base class `B` of `N` that is accessible at *R*, and
|
| 4002 |
+
`m` is accessible at *R* when designated in class `B`.
|
| 4003 |
+
\[*Example 4*:
|
| 4004 |
``` cpp
|
| 4005 |
class B;
|
| 4006 |
class A {
|
| 4007 |
private:
|
| 4008 |
int i;
|
|
|
|
| 4018 |
|
| 4019 |
If a class member access operator, including an implicit “`this->`”, is
|
| 4020 |
used to access a non-static data member or non-static member function,
|
| 4021 |
the reference is ill-formed if the left operand (considered as a pointer
|
| 4022 |
in the “`.`” operator case) cannot be implicitly converted to a pointer
|
| 4023 |
+
to the designating class of the right operand.
|
| 4024 |
|
| 4025 |
[*Note 4*: This requirement is in addition to the requirement that the
|
| 4026 |
+
member be accessible as designated. — *end note*]
|
| 4027 |
|
| 4028 |
### Friends <a id="class.friend">[[class.friend]]</a>
|
| 4029 |
|
| 4030 |
A friend of a class is a function or class that is given permission to
|
| 4031 |
name the private and protected members of the class. A class specifies
|
|
|
|
| 4097 |
};
|
| 4098 |
```
|
| 4099 |
|
| 4100 |
— *end example*]
|
| 4101 |
|
| 4102 |
+
A friend declaration that does not declare a function shall be a
|
| 4103 |
+
*friend-type-declaration*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4104 |
|
| 4105 |
[*Note 1*: A friend declaration can be the *declaration* in a
|
| 4106 |
*template-declaration* [[temp.pre]], [[temp.friend]]. — *end note*]
|
| 4107 |
|
| 4108 |
+
If a *friend-type-specifier* in a friend declaration designates a
|
| 4109 |
+
(possibly cv-qualified) class type, that class is declared as a friend;
|
| 4110 |
+
otherwise, the *friend-type-specifier* is ignored.
|
| 4111 |
|
| 4112 |
[*Example 4*:
|
| 4113 |
|
| 4114 |
``` cpp
|
| 4115 |
class C;
|
| 4116 |
typedef C Ct;
|
| 4117 |
+
class E;
|
| 4118 |
|
| 4119 |
class X1 {
|
| 4120 |
friend C; // OK, class C is a friend
|
| 4121 |
};
|
| 4122 |
|
|
|
|
| 4124 |
friend Ct; // OK, class C is a friend
|
| 4125 |
friend D; // error: D not found
|
| 4126 |
friend class D; // OK, elaborated-type-specifier declares new class
|
| 4127 |
};
|
| 4128 |
|
| 4129 |
+
template <typename ... Ts> class R {
|
| 4130 |
+
friend Ts...;
|
| 4131 |
+
};
|
| 4132 |
+
|
| 4133 |
+
template <class... Ts, class... Us>
|
| 4134 |
+
class R<R<Ts...>, R<Us...>> {
|
| 4135 |
+
friend Ts::Nested..., Us...;
|
| 4136 |
};
|
| 4137 |
|
| 4138 |
R<C> rc; // class C is a friend of R<C>
|
| 4139 |
+
R<C, E> rce; // classes C and E are friends of R<C, E>
|
| 4140 |
+
R<int> Ri; // OK, ``friend int;'' is ignored
|
| 4141 |
+
|
| 4142 |
+
struct E { struct Nested; };
|
| 4143 |
+
|
| 4144 |
+
R<R<E>, R<C, int>> rr; // E::Nested and C are friends of R<R<E>, R<C, int>>
|
| 4145 |
```
|
| 4146 |
|
| 4147 |
— *end example*]
|
| 4148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4149 |
[*Note 2*:
|
| 4150 |
|
| 4151 |
A friend declaration refers to an entity, not (all overloads of) a name.
|
| 4152 |
A member function of a class `X` can be a friend of a class `Y`.
|
| 4153 |
|
|
|
|
| 4288 |
|
| 4289 |
### Protected member access <a id="class.protected">[[class.protected]]</a>
|
| 4290 |
|
| 4291 |
An additional access check beyond those described earlier in
|
| 4292 |
[[class.access]] is applied when a non-static data member or non-static
|
| 4293 |
+
member function is a protected member of its designating class
|
| 4294 |
+
[[class.access.base]].[^11]
|
| 4295 |
|
| 4296 |
As described earlier, access to a protected member is granted because
|
| 4297 |
the reference occurs in a friend or direct member of some class `C`. If
|
| 4298 |
the access is to form a pointer to member [[expr.unary.op]], the
|
| 4299 |
*nested-name-specifier* shall denote `C` or a class derived from `C`.
|
|
|
|
| 4320 |
|
| 4321 |
void fr(B* pb, D1* p1, D2* p2) {
|
| 4322 |
pb->i = 1; // error
|
| 4323 |
p1->i = 2; // error
|
| 4324 |
p2->i = 3; // OK (access through a D2)
|
| 4325 |
+
p2->B::i = 4; // OK (access through a D2, even though designating class is B)
|
| 4326 |
int B::* pmi_B = &B::i; // error
|
| 4327 |
int B::* pmi_B2 = &D2::i; // OK (type of &D2::i is int B::*)
|
| 4328 |
+
B::j = 5; // error: not a friend of designating class B
|
| 4329 |
D2::j = 6; // OK (because refers to static member)
|
| 4330 |
}
|
| 4331 |
|
| 4332 |
void D2::mem(B* pb, D1* p1) {
|
| 4333 |
pb->i = 1; // error
|
|
|
|
| 4486 |
complex g = { 1, 2 }; // initialized by calling complex(double, double) with arguments 1 and 2
|
| 4487 |
```
|
| 4488 |
|
| 4489 |
— *end example*]
|
| 4490 |
|
| 4491 |
+
[*Note 1*: Overloading of the assignment operator [[over.assign]] has
|
| 4492 |
+
no effect on initialization. — *end note*]
|
| 4493 |
|
| 4494 |
An object of class type can also be initialized by a *braced-init-list*.
|
| 4495 |
List-initialization semantics apply; see [[dcl.init]] and
|
| 4496 |
[[dcl.init.list]].
|
| 4497 |
|
|
|
|
| 4522 |
[*Note 2*: Braces can be elided in the *initializer-list* for any
|
| 4523 |
aggregate, even if the aggregate has members of a class type with
|
| 4524 |
user-defined type conversions; see [[dcl.init.aggr]]. — *end note*]
|
| 4525 |
|
| 4526 |
[*Note 3*: If `T` is a class type with no default constructor, any
|
| 4527 |
+
initializing declaration of an object of type `T` (or array thereof) is
|
| 4528 |
+
ill-formed if no *initializer* is explicitly specified (see
|
| 4529 |
+
[[class.init]] and [[dcl.init]]). — *end note*]
|
| 4530 |
|
| 4531 |
[*Note 4*: The order in which objects with static or thread storage
|
| 4532 |
duration are initialized is described in [[basic.start.dynamic]] and
|
| 4533 |
[[stmt.dcl]]. — *end note*]
|
| 4534 |
|
|
|
|
| 4569 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 4570 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 4571 |
the hidden base class can be specified using a qualified
|
| 4572 |
name. — *end note*]
|
| 4573 |
|
| 4574 |
+
Unless the *mem-initializer-id* names the constructor’s class, a direct
|
| 4575 |
non-static data member of the constructor’s class, or a direct or
|
| 4576 |
virtual base of that class, the *mem-initializer* is ill-formed.
|
| 4577 |
|
| 4578 |
A *mem-initializer-list* can initialize a base class using any
|
| 4579 |
*class-or-decltype* that denotes that base class type.
|
|
|
|
| 4711 |
[*Note 4*: After the call to a constructor for class `X` for an object
|
| 4712 |
with automatic or dynamic storage duration has completed, if the
|
| 4713 |
constructor was not invoked as part of value-initialization and a member
|
| 4714 |
of `X` is neither initialized nor given a value during execution of the
|
| 4715 |
*compound-statement* of the body of the constructor, the member has an
|
| 4716 |
+
indeterminate or erroneous value [[basic.indet]]. — *end note*]
|
| 4717 |
|
| 4718 |
[*Example 6*:
|
| 4719 |
|
| 4720 |
``` cpp
|
| 4721 |
struct A {
|
|
|
|
| 4728 |
|
| 4729 |
struct C {
|
| 4730 |
C() { } // initializes members as follows:
|
| 4731 |
A a; // OK, calls A::A()
|
| 4732 |
const B b; // error: B has no default constructor
|
| 4733 |
+
int i; // OK, i has indeterminate or erroneous value
|
| 4734 |
int j = 5; // OK, j has the value 5
|
| 4735 |
};
|
| 4736 |
```
|
| 4737 |
|
| 4738 |
— *end example*]
|
|
|
|
| 4866 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 4867 |
|
| 4868 |
— *end example*]
|
| 4869 |
|
| 4870 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 4871 |
+
can be called for an object under construction or destruction.
|
| 4872 |
+
Similarly, an object under construction or destruction can be the
|
| 4873 |
+
operand of the `typeid` operator [[expr.typeid]] or of a `dynamic_cast`
|
| 4874 |
+
[[expr.dynamic.cast]]. However, if these operations are performed during
|
| 4875 |
+
evaluation of
|
| 4876 |
+
|
| 4877 |
+
- a *ctor-initializer* (or in a function called directly or indirectly
|
| 4878 |
+
from a *ctor-initializer*) before all the *mem-initializer*s for base
|
| 4879 |
+
classes have completed,
|
| 4880 |
+
- a precondition assertion of a constructor, or
|
| 4881 |
+
- a postcondition assertion of a destructor [[dcl.contract.func]],
|
| 4882 |
+
|
| 4883 |
+
the program has undefined behavior.
|
| 4884 |
|
| 4885 |
[*Example 11*:
|
| 4886 |
|
| 4887 |
``` cpp
|
| 4888 |
class A {
|
|
|
|
| 4939 |
When a constructor for type `B` is invoked to initialize an object of a
|
| 4940 |
different type `D` (that is, when the constructor was inherited
|
| 4941 |
[[namespace.udecl]]), initialization proceeds as if a defaulted default
|
| 4942 |
constructor were used to initialize the `D` object and each base class
|
| 4943 |
subobject from which the constructor was inherited, except that the `B`
|
| 4944 |
+
subobject is initialized by the inherited constructor if the base class
|
| 4945 |
+
subobject were to be initialized as part of the `D` object
|
| 4946 |
+
[[class.base.init]]. The invocation of the inherited constructor,
|
| 4947 |
+
including the evaluation of any arguments, is omitted if the `B`
|
| 4948 |
+
subobject is not to be initialized as part of the `D` object. The
|
| 4949 |
+
complete initialization is considered to be a single function call; in
|
| 4950 |
+
particular, unless omitted, the initialization of the inherited
|
| 4951 |
+
constructor’s parameters is sequenced before the initialization of any
|
| 4952 |
+
part of the `D` object.
|
| 4953 |
|
| 4954 |
[*Example 1*:
|
| 4955 |
|
| 4956 |
``` cpp
|
| 4957 |
struct B1 {
|
|
|
|
| 5000 |
constructors, while writing a message to the standard log whenever an
|
| 5001 |
object of class `Log` is destroyed.
|
| 5002 |
|
| 5003 |
— *end example*]
|
| 5004 |
|
| 5005 |
+
[*Example 2*:
|
| 5006 |
+
|
| 5007 |
+
``` cpp
|
| 5008 |
+
struct V { V() = default; V(int); };
|
| 5009 |
+
struct Q { Q(); };
|
| 5010 |
+
struct A : virtual V, Q {
|
| 5011 |
+
using V::V;
|
| 5012 |
+
A() = delete;
|
| 5013 |
+
};
|
| 5014 |
+
int bar() { return 42; }
|
| 5015 |
+
struct B : A {
|
| 5016 |
+
B() : A(bar()) {} // OK
|
| 5017 |
+
};
|
| 5018 |
+
struct C : B {};
|
| 5019 |
+
void foo() { C c; } // bar is not invoked, because the V subobject is not initialized as part of B
|
| 5020 |
+
```
|
| 5021 |
+
|
| 5022 |
+
— *end example*]
|
| 5023 |
+
|
| 5024 |
If the constructor was inherited from multiple base class subobjects of
|
| 5025 |
type `B`, the program is ill-formed.
|
| 5026 |
|
| 5027 |
+
[*Example 3*:
|
| 5028 |
|
| 5029 |
``` cpp
|
| 5030 |
struct A { A(int); };
|
| 5031 |
struct B : A { using A::A; };
|
| 5032 |
|
|
|
|
| 5088 |
|
| 5089 |
A* pa = &bobj; // undefined behavior: upcast to a base class type
|
| 5090 |
B bobj; // definition of bobj
|
| 5091 |
|
| 5092 |
extern X xobj;
|
| 5093 |
+
int* p3 = &xobj.i; // OK, all constructors of X are trivial
|
| 5094 |
X xobj;
|
| 5095 |
```
|
| 5096 |
|
| 5097 |
For another example,
|
| 5098 |
|
|
|
|
| 5107 |
};
|
| 5108 |
```
|
| 5109 |
|
| 5110 |
— *end example*]
|
| 5111 |
|
| 5112 |
+
During the construction of an object, if the value of any of its
|
| 5113 |
+
subobjects or any element of its object representation is accessed
|
| 5114 |
+
through a glvalue that is not obtained, directly or indirectly, from the
|
| 5115 |
+
constructor’s `this` pointer, the value thus obtained is unspecified.
|
| 5116 |
|
| 5117 |
[*Example 2*:
|
| 5118 |
|
| 5119 |
``` cpp
|
| 5120 |
struct C;
|
|
|
|
| 5180 |
|
| 5181 |
Member functions, including virtual functions [[class.virtual]], can be
|
| 5182 |
called during construction or destruction [[class.base.init]]. When a
|
| 5183 |
virtual function is called directly or indirectly from a constructor or
|
| 5184 |
from a destructor, including during the construction or destruction of
|
| 5185 |
+
the class’s non-static data members, or during the evaluation of a
|
| 5186 |
+
postcondition assertion of a constructor or a precondition assertion of
|
| 5187 |
+
a destructor [[dcl.contract.func]], and the object to which the call
|
| 5188 |
applies is the object (call it `x`) under construction or destruction,
|
| 5189 |
the function called is the final overrider in the constructor’s or
|
| 5190 |
destructor’s class and not one overriding it in a more-derived class. If
|
| 5191 |
the virtual function call uses an explicit class member access
|
| 5192 |
[[expr.ref]] and the object expression refers to the complete object of
|
|
|
|
| 5280 |
— *end example*]
|
| 5281 |
|
| 5282 |
### Copy/move elision <a id="class.copy.elision">[[class.copy.elision]]</a>
|
| 5283 |
|
| 5284 |
When certain criteria are met, an implementation is allowed to omit the
|
| 5285 |
+
creation of a class object from a source object of the same type
|
| 5286 |
+
(ignoring cv-qualification), even if the selected constructor and/or the
|
| 5287 |
+
destructor for the object have side effects. In such cases, the
|
| 5288 |
+
implementation treats the source and target of the omitted
|
| 5289 |
+
initialization as simply two different ways of referring to the same
|
| 5290 |
+
object. If the first parameter of the selected constructor is an rvalue
|
| 5291 |
+
reference to the object’s type, the destruction of that object occurs
|
| 5292 |
+
when the target would have been destroyed; otherwise, the destruction
|
| 5293 |
+
occurs at the later of the times when the two objects would have been
|
| 5294 |
+
destroyed without the optimization.
|
| 5295 |
|
| 5296 |
+
[*Note 1*: Because only one object is destroyed instead of two, and the
|
| 5297 |
+
creation of one object is omitted, there is still one object destroyed
|
| 5298 |
+
for each one constructed. — *end note*]
|
| 5299 |
|
| 5300 |
+
This elision of object creation, called *copy elision*, is permitted in
|
| 5301 |
+
the following circumstances (which may be combined to eliminate multiple
|
| 5302 |
+
copies):
|
| 5303 |
+
|
| 5304 |
+
- in a `return` statement [[stmt.return]] in a function with a class
|
| 5305 |
+
return type, when the *expression* is the name of a non-volatile
|
| 5306 |
+
object o with automatic storage duration (other than a function
|
| 5307 |
+
parameter or a variable introduced by the *exception-declaration* of a
|
| 5308 |
+
*handler* [[except.handle]]), the copy-initialization of the result
|
| 5309 |
+
object can be omitted by constructing o directly into the function
|
| 5310 |
+
call’s result object;
|
| 5311 |
- in a *throw-expression* [[expr.throw]], when the operand is the name
|
| 5312 |
+
of a non-volatile object o with automatic storage duration (other than
|
| 5313 |
+
a function parameter or a variable introduced by the
|
| 5314 |
+
*exception-declaration* of a *handler*) that belongs to a scope that
|
| 5315 |
+
does not contain the innermost enclosing *compound-statement*
|
| 5316 |
+
associated with a *try-block* (if there is one), the
|
| 5317 |
+
copy-initialization of the exception object can be omitted by
|
| 5318 |
+
constructing o directly into the exception object;
|
| 5319 |
- in a coroutine [[dcl.fct.def.coroutine]], a copy of a coroutine
|
| 5320 |
parameter can be omitted and references to that copy replaced with
|
| 5321 |
references to the corresponding parameter if the meaning of the
|
| 5322 |
program will be unchanged except for the execution of a constructor
|
| 5323 |
+
and destructor for the parameter copy object;
|
| 5324 |
+
- when the *exception-declaration* of a *handler* [[except.handle]]
|
| 5325 |
+
declares an object o, the copy-initialization of o can be omitted by
|
| 5326 |
+
treating the *exception-declaration* as an alias for the exception
|
| 5327 |
+
object if the meaning of the program will be unchanged except for the
|
| 5328 |
+
execution of constructors and destructors for the object declared by
|
| 5329 |
+
the *exception-declaration*. \[*Note 2*: There cannot be a move from
|
| 5330 |
+
the exception object because it is always an lvalue. — *end note*]
|
|
|
|
|
|
|
| 5331 |
|
| 5332 |
Copy elision is not permitted where an expression is evaluated in a
|
| 5333 |
context requiring a constant expression [[expr.const]] and in constant
|
| 5334 |
initialization [[basic.start.static]].
|
| 5335 |
|
| 5336 |
+
[*Note 3*: It is possible that copy elision is performed if the same
|
| 5337 |
expression is evaluated in another context. — *end note*]
|
| 5338 |
|
| 5339 |
[*Example 1*:
|
| 5340 |
|
| 5341 |
``` cpp
|
|
|
|
| 5463 |
|
| 5464 |
## Comparisons <a id="class.compare">[[class.compare]]</a>
|
| 5465 |
|
| 5466 |
### Defaulted comparison operator functions <a id="class.compare.default">[[class.compare.default]]</a>
|
| 5467 |
|
| 5468 |
+
A defaulted comparison operator function [[over.binary]] shall be a
|
| 5469 |
+
non-template function that
|
| 5470 |
|
| 5471 |
+
- is a non-static member or friend of some class `C`,
|
| 5472 |
+
- is defined as defaulted in `C` or in a context where `C` is complete,
|
| 5473 |
+
and
|
| 5474 |
- either has two parameters of type `const C&` or two parameters of type
|
| 5475 |
`C`, where the implicit object parameter (if any) is considered to be
|
| 5476 |
the first parameter.
|
| 5477 |
|
| 5478 |
+
Such a comparison operator function is termed a defaulted comparison
|
| 5479 |
+
operator function for class `C`. Name lookups and access checks in the
|
| 5480 |
+
implicit definition [[dcl.fct.def.default]] of a comparison operator
|
| 5481 |
+
function are performed from a context equivalent to its *function-body*.
|
| 5482 |
+
A definition of a comparison operator as defaulted that appears in a
|
| 5483 |
+
class shall be the first declaration of that function.
|
| 5484 |
+
|
| 5485 |
+
[*Example 1*:
|
| 5486 |
+
|
| 5487 |
+
``` cpp
|
| 5488 |
+
struct S;
|
| 5489 |
+
bool operator==(S, S) = default; // error: S is not complete
|
| 5490 |
+
struct S {
|
| 5491 |
+
friend bool operator==(S, const S&) = default; // error: parameters of different types
|
| 5492 |
+
};
|
| 5493 |
+
enum E { };
|
| 5494 |
+
bool operator==(E, E) = default; // error: not a member or friend of a class
|
| 5495 |
+
```
|
| 5496 |
+
|
| 5497 |
+
— *end example*]
|
| 5498 |
|
| 5499 |
A defaulted `<=>` or `==` operator function for class `C` is defined as
|
| 5500 |
deleted if any non-static data member of `C` is of reference type or `C`
|
| 5501 |
has variant members [[class.union.anon]].
|
| 5502 |
|
|
|
|
| 5526 |
operator function has an implicit exception specification
|
| 5527 |
[[except.spec]] that can differ from the implicit exception
|
| 5528 |
specification of the three-way comparison operator
|
| 5529 |
function. — *end note*]
|
| 5530 |
|
| 5531 |
+
[*Example 2*:
|
| 5532 |
|
| 5533 |
``` cpp
|
| 5534 |
template<typename T> struct X {
|
| 5535 |
friend constexpr std::partial_ordering operator<=>(X, X) requires (sizeof(T) != 1) = default;
|
| 5536 |
// implicitly declares: friend constexpr bool operator==(X, X) requires (sizeof(T) != 1) = default;
|
|
|
|
| 5565 |
|
| 5566 |
A defaulted `==` operator function for a class `C` is defined as deleted
|
| 5567 |
unless, for each `xᵢ` in the expanded list of subobjects for an object
|
| 5568 |
`x` of type `C`, `xᵢ`` == ``xᵢ` is usable [[class.compare.default]].
|
| 5569 |
|
| 5570 |
+
The return value of a defaulted `==` operator function with parameters
|
| 5571 |
+
`x` and `y` is determined by comparing corresponding elements `xᵢ` and
|
| 5572 |
+
`yᵢ` in the expanded lists of subobjects for `x` and `y` (in increasing
|
| 5573 |
+
index order) until the first index i where `xᵢ`` == ``yᵢ` yields a
|
| 5574 |
+
result value which, when contextually converted to `bool`, yields
|
| 5575 |
+
`false`. The return value is `false` if such an index exists and `true`
|
| 5576 |
+
otherwise.
|
| 5577 |
|
| 5578 |
[*Example 1*:
|
| 5579 |
|
| 5580 |
``` cpp
|
| 5581 |
struct D {
|
|
|
|
| 5592 |
The *synthesized three-way comparison* of type `R` [[cmp.categories]] of
|
| 5593 |
glvalues `a` and `b` of the same type is defined as follows:
|
| 5594 |
|
| 5595 |
- If `a <=> b` is usable [[class.compare.default]] and can be explicitly
|
| 5596 |
converted to `R` using `static_cast`, `static_cast<R>(a <=> b)`.
|
| 5597 |
+
- Otherwise, if `a <=> b` is usable or overload resolution for `a <=> b`
|
| 5598 |
+
is performed and finds at least one viable candidate, the synthesized
|
| 5599 |
+
three-way comparison is not defined.
|
| 5600 |
- Otherwise, if `R` is not a comparison category type, or either the
|
| 5601 |
expression `a == b` or the expression `a < b` is not usable, the
|
| 5602 |
synthesized three-way comparison is not defined.
|
| 5603 |
- Otherwise, if `R` is `strong_ordering`, then
|
| 5604 |
``` cpp
|
|
|
|
| 5635 |
common comparison type (see below) of `R₀`, `R₁`, …, `R_n-1`.
|
| 5636 |
- Otherwise, `R` shall not contain a placeholder type. If the
|
| 5637 |
synthesized three-way comparison of type `R` between any objects `xᵢ`
|
| 5638 |
and `xᵢ` is not defined, the operator function is defined as deleted.
|
| 5639 |
|
| 5640 |
+
The return value of type `R` of the defaulted three-way comparison
|
| 5641 |
operator function with parameters `x` and `y` of the same type is
|
| 5642 |
determined by comparing corresponding elements `xᵢ` and `yᵢ` in the
|
| 5643 |
expanded lists of subobjects for `x` and `y` (in increasing index order)
|
| 5644 |
until the first index i where the synthesized three-way comparison of
|
| 5645 |
type `R` between `xᵢ` and `yᵢ` yields a result value `vᵢ` where
|
| 5646 |
+
`vᵢ` `!=` 0, contextually converted to `bool`, yields `true`. The return
|
| 5647 |
+
value is a copy of `vᵢ` if such an index exists and
|
| 5648 |
+
`static_cast<R>(std::strong_ordering::equal)` otherwise.
|
| 5649 |
|
| 5650 |
The *common comparison type* `U` of a possibly-empty list of n
|
| 5651 |
comparison category types `T₀`, `T₁`, …, `T_n-1` is defined as follows:
|
| 5652 |
|
| 5653 |
- If at least one `Tᵢ` is `std::partial_ordering`, `U` is
|
|
|
|
| 5666 |
`bool`.
|
| 5667 |
|
| 5668 |
The operator function with parameters `x` and `y` is defined as deleted
|
| 5669 |
if
|
| 5670 |
|
| 5671 |
+
- a first overload resolution [[over.match]], as applied to `x @ y`,
|
| 5672 |
+
- does not result in a usable candidate, or
|
| 5673 |
+
- the selected candidate is not a rewritten candidate, or
|
| 5674 |
+
- a second overload resolution for the expression resulting from the
|
| 5675 |
+
interpretation of `x @ y` using the selected rewritten candidate
|
| 5676 |
+
[[over.match.oper]] does not result in a usable candidate (for
|
| 5677 |
+
example, that expression might be `(x <=> y) @ 0`), or
|
| 5678 |
+
- `x @ y` cannot be implicitly converted to `bool`.
|
| 5679 |
|
| 5680 |
+
In any of the two overload resolutions above, the defaulted operator
|
| 5681 |
+
function is not considered as a candidate for the `@` operator.
|
| 5682 |
+
Otherwise, the operator function yields `x @ y`.
|
| 5683 |
|
| 5684 |
[*Example 1*:
|
| 5685 |
|
| 5686 |
``` cpp
|
| 5687 |
struct HasNoLessThan { };
|
|
|
|
| 5697 |
<!-- Link reference definitions -->
|
| 5698 |
[basic.align]: basic.md#basic.align
|
| 5699 |
[basic.compound]: basic.md#basic.compound
|
| 5700 |
[basic.def]: basic.md#basic.def
|
| 5701 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 5702 |
+
[basic.fundamental]: basic.md#basic.fundamental
|
| 5703 |
+
[basic.indet]: basic.md#basic.indet
|
| 5704 |
[basic.life]: basic.md#basic.life
|
| 5705 |
[basic.link]: basic.md#basic.link
|
| 5706 |
[basic.lookup]: basic.md#basic.lookup
|
| 5707 |
[basic.lookup.elab]: basic.md#basic.lookup.elab
|
| 5708 |
[basic.lval]: expr.md#basic.lval
|
|
|
|
| 5714 |
[basic.start.term]: basic.md#basic.start.term
|
| 5715 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 5716 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 5717 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 5718 |
[basic.types]: basic.md#basic.types
|
| 5719 |
+
[basic.types.general]: basic.md#basic.types.general
|
| 5720 |
[class]: #class
|
| 5721 |
[class.abstract]: #class.abstract
|
| 5722 |
[class.access]: #class.access
|
| 5723 |
[class.access.base]: #class.access.base
|
| 5724 |
[class.access.general]: #class.access.general
|
|
|
|
| 5782 |
[cmp.weakord]: support.md#cmp.weakord
|
| 5783 |
[conv]: expr.md#conv
|
| 5784 |
[conv.mem]: expr.md#conv.mem
|
| 5785 |
[conv.ptr]: expr.md#conv.ptr
|
| 5786 |
[conv.rval]: expr.md#conv.rval
|
| 5787 |
+
[cpp.cond]: cpp.md#cpp.cond
|
| 5788 |
+
[dcl.align]: dcl.md#dcl.align
|
| 5789 |
[dcl.array]: dcl.md#dcl.array
|
| 5790 |
[dcl.attr.nouniqueaddr]: dcl.md#dcl.attr.nouniqueaddr
|
| 5791 |
+
[dcl.contract.func]: dcl.md#dcl.contract.func
|
| 5792 |
[dcl.decl]: dcl.md#dcl.decl
|
| 5793 |
[dcl.enum]: dcl.md#dcl.enum
|
| 5794 |
[dcl.fct]: dcl.md#dcl.fct
|
| 5795 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 5796 |
[dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
|
|
|
|
| 5804 |
[dcl.init.general]: dcl.md#dcl.init.general
|
| 5805 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 5806 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 5807 |
[dcl.inline]: dcl.md#dcl.inline
|
| 5808 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 5809 |
+
[dcl.meaning.general]: dcl.md#dcl.meaning.general
|
| 5810 |
[dcl.name]: dcl.md#dcl.name
|
| 5811 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 5812 |
[dcl.stc]: dcl.md#dcl.stc
|
| 5813 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 5814 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
|
|
|
| 5817 |
[depr.impldec]: future.md#depr.impldec
|
| 5818 |
[depr.static.constexpr]: future.md#depr.static.constexpr
|
| 5819 |
[diff.class]: compatibility.md#diff.class
|
| 5820 |
[except.ctor]: except.md#except.ctor
|
| 5821 |
[except.handle]: except.md#except.handle
|
|
|
|
| 5822 |
[except.spec]: except.md#except.spec
|
| 5823 |
[except.throw]: except.md#except.throw
|
| 5824 |
+
[expr.assign]: expr.md#expr.assign
|
| 5825 |
[expr.call]: expr.md#expr.call
|
| 5826 |
[expr.cast]: expr.md#expr.cast
|
| 5827 |
[expr.const]: expr.md#expr.const
|
| 5828 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 5829 |
[expr.delete]: expr.md#expr.delete
|
|
|
|
| 5843 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 5844 |
[expr.typeid]: expr.md#expr.typeid
|
| 5845 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 5846 |
[intro.execution]: basic.md#intro.execution
|
| 5847 |
[intro.object]: basic.md#intro.object
|
| 5848 |
+
[meta.reflection.define.aggregate]: meta.md#meta.reflection.define.aggregate
|
| 5849 |
+
[module.reach]: module.md#module.reach
|
| 5850 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 5851 |
[over]: over.md#over
|
| 5852 |
+
[over.assign]: over.md#over.assign
|
| 5853 |
[over.best.ics]: over.md#over.best.ics
|
| 5854 |
[over.binary]: over.md#over.binary
|
|
|
|
| 5855 |
[over.match]: over.md#over.match
|
| 5856 |
[over.match.best]: over.md#over.match.best
|
| 5857 |
[over.match.call]: over.md#over.match.call
|
| 5858 |
[over.match.copy]: over.md#over.match.copy
|
| 5859 |
[over.match.funcs]: over.md#over.match.funcs
|
| 5860 |
+
[over.match.general]: over.md#over.match.general
|
| 5861 |
+
[over.match.oper]: over.md#over.match.oper
|
| 5862 |
[over.oper]: over.md#over.oper
|
| 5863 |
[over.over]: over.md#over.over
|
| 5864 |
[special]: #special
|
| 5865 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 5866 |
[stmt.return]: stmt.md#stmt.return
|
| 5867 |
[string.classes]: strings.md#string.classes
|
| 5868 |
[temp.arg]: temp.md#temp.arg
|
| 5869 |
[temp.constr]: temp.md#temp.constr
|
| 5870 |
[temp.constr.order]: temp.md#temp.constr.order
|
| 5871 |
[temp.deduct.guide]: temp.md#temp.deduct.guide
|
|
|
|
| 5872 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 5873 |
[temp.explicit]: temp.md#temp.explicit
|
| 5874 |
[temp.friend]: temp.md#temp.friend
|
| 5875 |
[temp.inst]: temp.md#temp.inst
|
| 5876 |
[temp.mem]: temp.md#temp.mem
|
| 5877 |
[temp.param]: temp.md#temp.param
|
| 5878 |
[temp.pre]: temp.md#temp.pre
|
| 5879 |
+
[temp.spec.general]: temp.md#temp.spec.general
|
| 5880 |
[temp.spec.partial]: temp.md#temp.spec.partial
|
| 5881 |
[temp.variadic]: temp.md#temp.variadic
|
| 5882 |
[term.incomplete.type]: basic.md#term.incomplete.type
|
| 5883 |
[term.layout.compatible.type]: basic.md#term.layout.compatible.type
|
| 5884 |
[term.object.representation]: basic.md#term.object.representation
|
|
|
|
| 5887 |
|
| 5888 |
[^1]: This ensures that two subobjects that have the same class type and
|
| 5889 |
that belong to the same most derived object are not allocated at the
|
| 5890 |
same address [[expr.eq]].
|
| 5891 |
|
| 5892 |
+
[^2]: This implies that the reference parameter of the
|
|
|
|
|
|
|
| 5893 |
implicitly-declared copy constructor cannot bind to a `volatile`
|
| 5894 |
lvalue; see [[diff.class]].
|
| 5895 |
|
| 5896 |
+
[^3]: Because a template assignment operator or an assignment operator
|
| 5897 |
taking an rvalue reference parameter is never a copy assignment
|
| 5898 |
operator, the presence of such an assignment operator does not
|
| 5899 |
suppress the implicit declaration of a copy assignment operator.
|
| 5900 |
Such assignment operators participate in overload resolution with
|
| 5901 |
other assignment operators, including copy assignment operators,
|
| 5902 |
and, if selected, will be used to assign an object.
|
| 5903 |
|
| 5904 |
+
[^4]: This implies that the reference parameter of the
|
| 5905 |
implicitly-declared copy assignment operator cannot bind to a
|
| 5906 |
`volatile` lvalue; see [[diff.class]].
|
| 5907 |
|
| 5908 |
+
[^5]: The use of the `virtual` specifier in the declaration of an
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5909 |
overriding function is valid but redundant (has empty semantics).
|
| 5910 |
|
| 5911 |
+
[^6]: If all virtual functions are immediate functions, the class is
|
| 5912 |
still polymorphic even if its internal representation does not
|
| 5913 |
otherwise require any additions for that polymorphic behavior.
|
| 5914 |
|
| 5915 |
+
[^7]: A function with the same name but a different parameter list
|
| 5916 |
[[over]] as a virtual function is not necessarily virtual and does
|
| 5917 |
not override. Access control [[class.access]] is not considered in
|
| 5918 |
determining overriding.
|
| 5919 |
|
| 5920 |
+
[^8]: Multi-level pointers to classes or references to multi-level
|
| 5921 |
pointers to classes are not allowed.
|
| 5922 |
|
| 5923 |
+
[^9]: Access permissions are thus transitive and cumulative to nested
|
| 5924 |
and local classes.
|
| 5925 |
|
| 5926 |
+
[^10]: As specified previously in [[class.access]], private members of a
|
| 5927 |
base class remain inaccessible even to derived classes unless friend
|
| 5928 |
declarations within the base class definition are used to grant
|
| 5929 |
access explicitly.
|
| 5930 |
|
| 5931 |
+
[^11]: This additional check does not apply to other members, e.g.,
|
| 5932 |
static data members or enumerator member constants.
|
|
|
|
|
|
|
|
|
|
|
|