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