- tmp/tmpw9rfxrlh/{from.md → to.md} +763 -394
tmp/tmpw9rfxrlh/{from.md → to.md}
RENAMED
|
@@ -40,11 +40,13 @@ class-key:
|
|
| 40 |
'struct'
|
| 41 |
'union'
|
| 42 |
```
|
| 43 |
|
| 44 |
A *class-specifier* whose *class-head* omits the *class-head-name*
|
| 45 |
-
defines an unnamed class.
|
|
|
|
|
|
|
| 46 |
|
| 47 |
A *class-name* is inserted into the scope in which it is declared
|
| 48 |
immediately after the *class-name* is seen. The *class-name* is also
|
| 49 |
inserted into the scope of the class itself; this is known as the
|
| 50 |
*injected-class-name*. For purposes of access checking, the
|
|
@@ -55,15 +57,17 @@ has been seen even though its member functions are in general not yet
|
|
| 55 |
defined. The optional *attribute-specifier-seq* appertains to the class;
|
| 56 |
the attributes in the *attribute-specifier-seq* are thereafter
|
| 57 |
considered attributes of the class whenever it is named.
|
| 58 |
|
| 59 |
If a class is marked with the *class-virt-specifier* `final` and it
|
| 60 |
-
appears as a *
|
| 61 |
[[class.derived]]), the program is ill-formed. Whenever a *class-key* is
|
| 62 |
followed by a *class-head-name*, the *identifier* `final`, and a colon
|
| 63 |
or left brace, `final` is interpreted as a *class-virt-specifier*.
|
| 64 |
|
|
|
|
|
|
|
| 65 |
``` cpp
|
| 66 |
struct A;
|
| 67 |
struct A final {}; // OK: definition of struct A,
|
| 68 |
// not value-initialization of variable final
|
| 69 |
|
|
@@ -72,68 +76,110 @@ struct X {
|
|
| 72 |
struct B final : C{}; // OK: definition of nested class B,
|
| 73 |
// not declaration of a bit-field member final
|
| 74 |
};
|
| 75 |
```
|
| 76 |
|
|
|
|
|
|
|
| 77 |
Complete objects and member subobjects of class type shall have nonzero
|
| 78 |
-
size.[^1]
|
|
|
|
|
|
|
| 79 |
functions, and returned by functions (except objects of classes for
|
| 80 |
which copying or moving has been restricted; see [[class.copy]]). Other
|
| 81 |
plausible operators, such as equality comparison, can be defined by the
|
| 82 |
-
user; see [[over.oper]].
|
| 83 |
|
| 84 |
A *union* is a class defined with the *class-key* `union`; it holds at
|
| 85 |
-
most one data member at a time ([[class.union]]).
|
| 86 |
-
type are described in [[dcl.init.aggr]].
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
- has no non-trivial move constructors ([[class.copy]]),
|
| 92 |
-
- has no non-trivial copy assignment operators ([[over.ass]],
|
| 93 |
-
[[class.copy]]),
|
| 94 |
-
- has no non-trivial move assignment operators ([[over.ass]],
|
| 95 |
-
[[class.copy]]), and
|
| 96 |
-
- has a trivial destructor ([[class.dtor]]).
|
| 97 |
|
| 98 |
-
|
| 99 |
-
[[class.
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
A *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
- has no non-static data members of type non-standard-layout class (or
|
| 106 |
array of such types) or reference,
|
| 107 |
- has no virtual functions ([[class.virtual]]) and no virtual base
|
| 108 |
classes ([[class.mi]]),
|
| 109 |
- has the same access control (Clause [[class.access]]) for all
|
| 110 |
non-static data members,
|
| 111 |
- has no non-standard-layout base classes,
|
| 112 |
-
-
|
| 113 |
-
|
| 114 |
-
classes
|
| 115 |
-
- has no
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
A *standard-layout struct* is a standard-layout class defined with the
|
| 119 |
-
*class-key* `struct` or the *class-key* `class`. A
|
| 120 |
-
|
| 121 |
-
*class-key* `union`.
|
| 122 |
|
| 123 |
-
Standard-layout classes are useful for communicating with
|
| 124 |
-
in other programming languages. Their layout is specified
|
| 125 |
-
[[class.mem]].
|
| 126 |
|
| 127 |
A *POD struct*[^3] is a non-union class that is both a trivial class and
|
| 128 |
a standard-layout class, and has no non-static data members of type
|
| 129 |
non-POD struct, non-POD union (or array of such types). Similarly, a
|
| 130 |
*POD union* is a union that is both a trivial class and a
|
| 131 |
standard-layout class, and has no non-static data members of type
|
| 132 |
non-POD struct, non-POD union (or array of such types). A *POD class* is
|
| 133 |
a class that is either a POD struct or a POD union.
|
| 134 |
|
|
|
|
|
|
|
| 135 |
``` cpp
|
| 136 |
struct N { // neither trivial nor standard-layout
|
| 137 |
int i;
|
| 138 |
int j;
|
| 139 |
virtual ~N();
|
|
@@ -155,10 +201,12 @@ struct POD { // both trivial and standard-layout
|
|
| 155 |
int i;
|
| 156 |
int j;
|
| 157 |
};
|
| 158 |
```
|
| 159 |
|
|
|
|
|
|
|
| 160 |
If a *class-head-name* contains a *nested-name-specifier*, the
|
| 161 |
*class-specifier* shall refer to a class that was previously declared
|
| 162 |
directly in the class or namespace to which the *nested-name-specifier*
|
| 163 |
refers, or in an element of the inline namespace set (
|
| 164 |
[[namespace.def]]) of that namespace (i.e., not merely inherited or
|
|
@@ -169,10 +217,12 @@ shall not begin with a *decltype-specifier*.
|
|
| 169 |
|
| 170 |
## Class names <a id="class.name">[[class.name]]</a>
|
| 171 |
|
| 172 |
A class definition introduces a new type.
|
| 173 |
|
|
|
|
|
|
|
| 174 |
``` cpp
|
| 175 |
struct X { int a; };
|
| 176 |
struct Y { int a; };
|
| 177 |
X a1;
|
| 178 |
Y a2;
|
|
@@ -201,54 +251,65 @@ struct S { int a; };
|
|
| 201 |
struct S { int a; }; // error, double definition
|
| 202 |
```
|
| 203 |
|
| 204 |
is ill-formed because it defines `S` twice.
|
| 205 |
|
|
|
|
|
|
|
| 206 |
A class declaration introduces the class name into the scope where it is
|
| 207 |
declared and hides any class, variable, function, or other declaration
|
| 208 |
of that name in an enclosing scope ([[basic.scope]]). If a class name
|
| 209 |
is declared in a scope where a variable, function, or enumerator of the
|
| 210 |
same name is also declared, then when both declarations are in scope,
|
| 211 |
the class can be referred to only using an *elaborated-type-specifier* (
|
| 212 |
[[basic.lookup.elab]]).
|
| 213 |
|
|
|
|
|
|
|
| 214 |
``` cpp
|
| 215 |
struct stat {
|
| 216 |
// ...
|
| 217 |
};
|
| 218 |
|
| 219 |
-
stat gstat; // use plain stat to
|
| 220 |
-
// define variable
|
| 221 |
|
| 222 |
int stat(struct stat*); // redeclare stat as function
|
| 223 |
|
| 224 |
void f() {
|
| 225 |
-
struct stat* ps; // struct prefix needed
|
| 226 |
-
// to name struct stat
|
| 227 |
stat(ps); // call stat()
|
| 228 |
}
|
| 229 |
```
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
``` cpp
|
| 237 |
struct s { int a; };
|
| 238 |
|
| 239 |
void g() {
|
| 240 |
-
struct s; // hide global struct s
|
| 241 |
-
// with a block-scope declaration
|
| 242 |
s* p; // refer to local struct s
|
| 243 |
struct s { char* p; }; // define local struct s
|
| 244 |
struct s; // redeclaration, has no effect
|
| 245 |
}
|
| 246 |
```
|
| 247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
Such declarations allow definition of classes that refer to each other.
|
| 249 |
|
|
|
|
|
|
|
| 250 |
``` cpp
|
| 251 |
class Vector;
|
| 252 |
|
| 253 |
class Matrix {
|
| 254 |
// ...
|
|
@@ -262,24 +323,34 @@ class Vector {
|
|
| 262 |
```
|
| 263 |
|
| 264 |
Declaration of `friend`s is described in [[class.friend]], operator
|
| 265 |
functions in [[over.oper]].
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
``` cpp
|
| 273 |
struct s { int a; };
|
| 274 |
|
| 275 |
void g(int s) {
|
| 276 |
struct s* p = new struct s; // global s
|
| 277 |
p->a = s; // parameter s
|
| 278 |
}
|
| 279 |
```
|
| 280 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
The declaration of a class name takes effect immediately after the
|
| 282 |
*identifier* is seen in the class definition or
|
| 283 |
*elaborated-type-specifier*. For example,
|
| 284 |
|
| 285 |
``` cpp
|
|
@@ -289,10 +360,12 @@ class A * A;
|
|
| 289 |
first specifies `A` to be the name of a class and then redefines it as
|
| 290 |
the name of a pointer to an object of that class. This means that the
|
| 291 |
elaborated form `class` `A` must be used to refer to the class. Such
|
| 292 |
artistry with names can be confusing and is best avoided.
|
| 293 |
|
|
|
|
|
|
|
| 294 |
A *typedef-name* ([[dcl.typedef]]) that names a class type, or a
|
| 295 |
cv-qualified version thereof, is also a *class-name*. If a
|
| 296 |
*typedef-name* that names a cv-qualified class type is used where a
|
| 297 |
*class-name* is required, the cv-qualifiers are ignored. A
|
| 298 |
*typedef-name* shall not be used as the *identifier* in a *class-head*.
|
|
@@ -310,10 +383,11 @@ member-declaration:
|
|
| 310 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 311 |
function-definition
|
| 312 |
using-declaration
|
| 313 |
static_assert-declaration
|
| 314 |
template-declaration
|
|
|
|
| 315 |
alias-declaration
|
| 316 |
empty-declaration
|
| 317 |
```
|
| 318 |
|
| 319 |
``` bnf
|
|
@@ -345,77 +419,136 @@ virt-specifier:
|
|
| 345 |
pure-specifier:
|
| 346 |
'= 0'
|
| 347 |
```
|
| 348 |
|
| 349 |
The *member-specification* in a class definition declares the full set
|
| 350 |
-
of members of the class; no member can be added elsewhere.
|
| 351 |
-
class
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
A class is considered a completely-defined object type (
|
| 370 |
[[basic.types]]) (or complete type) at the closing `}` of the
|
| 371 |
*class-specifier*. Within the class *member-specification*, the class is
|
| 372 |
regarded as complete within function bodies, default arguments,
|
| 373 |
-
*
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
A *brace-or-equal-initializer* shall appear only in the declaration of a
|
| 383 |
data member. (For static data members, see [[class.static.data]]; for
|
| 384 |
-
non-static data members, see [[class.base.init]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
-
A member shall not be declared with the `extern`
|
| 387 |
-
a class definition, a member shall not
|
| 388 |
-
|
|
|
|
| 389 |
|
| 390 |
The *decl-specifier-seq* may be omitted in constructor, destructor, and
|
| 391 |
conversion function declarations only; when declaring another kind of
|
| 392 |
member the *decl-specifier-seq* shall contain a *type-specifier* that is
|
| 393 |
not a *cv-qualifier*. The *member-declarator-list* can be omitted only
|
| 394 |
after a *class-specifier* or an *enum-specifier* or in a `friend`
|
| 395 |
declaration ([[class.friend]]). A *pure-specifier* shall be used only
|
| 396 |
-
in the declaration of a virtual function ([[class.virtual]])
|
|
|
|
| 397 |
|
| 398 |
The optional *attribute-specifier-seq* in a *member-declaration*
|
| 399 |
appertains to each of the entities declared by the *member-declarator*s;
|
| 400 |
it shall not appear if the optional *member-declarator-list* is omitted.
|
| 401 |
|
| 402 |
A *virt-specifier-seq* shall contain at most one of each
|
| 403 |
*virt-specifier*. A *virt-specifier-seq* shall appear only in the
|
| 404 |
declaration of a virtual member function ([[class.virtual]]).
|
| 405 |
|
| 406 |
-
Non-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
class `C`.
|
| 410 |
|
| 411 |
-
See [[expr.prim]] for restrictions on the use of non-static
|
| 412 |
-
members and non-static member functions.
|
| 413 |
|
| 414 |
-
The type of a non-static member function is an ordinary
|
| 415 |
-
and the type of a non-static data member is an ordinary
|
| 416 |
-
There are no special member function types or data member
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
A simple example of a class definition is
|
| 419 |
|
| 420 |
``` cpp
|
| 421 |
struct tnode {
|
|
@@ -438,11 +571,13 @@ declares `s` to be a `tnode` and `sp` to be a pointer to a `tnode`. With
|
|
| 438 |
these declarations, `sp->count` refers to the `count` member of the
|
| 439 |
object to which `sp` points; `s.left` refers to the `left` subtree
|
| 440 |
pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
| 441 |
character of the `tword` member of the `right` subtree of `s`.
|
| 442 |
|
| 443 |
-
|
|
|
|
|
|
|
| 444 |
control (Clause [[class.access]]) are allocated so that later members
|
| 445 |
have higher addresses within a class object. The order of allocation of
|
| 446 |
non-static data members with different access control is unspecified
|
| 447 |
(Clause [[class.access]]). Implementation alignment requirements might
|
| 448 |
cause two adjacent members not to be allocated immediately after each
|
|
@@ -451,86 +586,129 @@ other; so might requirements for space for managing virtual functions (
|
|
| 451 |
|
| 452 |
If `T` is the name of a class, then each of the following shall have a
|
| 453 |
name different from `T`:
|
| 454 |
|
| 455 |
- every static data member of class `T`;
|
| 456 |
-
- every member function of class `T` This restriction does
|
| 457 |
-
constructors, which do not have names (
|
|
|
|
| 458 |
- every member of class `T` that is itself a type;
|
|
|
|
| 459 |
- every enumerator of every member of class `T` that is an unscoped
|
| 460 |
enumerated type; and
|
| 461 |
- every member of every anonymous union that is a member of class `T`.
|
| 462 |
|
| 463 |
In addition, if class `T` has a user-declared constructor (
|
| 464 |
[[class.ctor]]), every non-static data member of class `T` shall have a
|
| 465 |
name different from `T`.
|
| 466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
Two standard-layout struct (Clause [[class]]) types are
|
| 468 |
-
*layout-compatible* if
|
| 469 |
-
members and
|
| 470 |
-
have layout-compatible types ([[basic.types]]).
|
| 471 |
|
| 472 |
-
Two standard-layout
|
| 473 |
-
|
| 474 |
-
members
|
| 475 |
-
layout-compatible types ([[basic.types]]).
|
| 476 |
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
|
| 486 |
If a standard-layout class object has any non-static data members, its
|
| 487 |
address is the same as the address of its first non-static data member.
|
| 488 |
Otherwise, its address is the same as the address of its first base
|
| 489 |
-
class subobject (if any).
|
| 490 |
-
within a standard-layout struct object, but not at its beginning, as
|
| 491 |
-
necessary to achieve appropriate alignment.
|
| 492 |
|
| 493 |
-
|
|
|
|
|
|
|
| 494 |
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
[[class.
|
| 500 |
-
class ([[class.mfct.non-static]], [[class.this]]).
|
| 501 |
|
| 502 |
A member function may be defined ([[dcl.fct.def]]) in its class
|
| 503 |
definition, in which case it is an *inline* member function (
|
| 504 |
-
[[dcl.
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
|
| 514 |
-
An
|
| 515 |
defined outside of its class definition provided either its declaration
|
| 516 |
in the class definition or its definition outside of the class
|
| 517 |
-
definition declares the function as `inline`
|
| 518 |
-
class in namespace scope have external linkage. Member functions of a
|
| 519 |
-
local class ([[class.local]]) have no linkage. See [[basic.link]].
|
| 520 |
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
|
| 526 |
If the definition of a member function is lexically outside its class
|
| 527 |
definition, the member function name shall be qualified by its class
|
| 528 |
-
name using the `::` operator.
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
``` cpp
|
| 534 |
struct X {
|
| 535 |
typedef int T;
|
| 536 |
static T count;
|
|
@@ -544,42 +722,49 @@ notation `X::f` specifies that the function `f` is a member of class `X`
|
|
| 544 |
and in the scope of class `X`. In the function definition, the parameter
|
| 545 |
type `T` refers to the typedef member `T` declared in class `X` and the
|
| 546 |
default argument `count` refers to the static data member `count`
|
| 547 |
declared in class `X`.
|
| 548 |
|
| 549 |
-
|
| 550 |
-
|
|
|
|
|
|
|
|
|
|
| 551 |
|
| 552 |
Previously declared member functions may be mentioned in `friend`
|
| 553 |
declarations.
|
| 554 |
|
| 555 |
Member functions of a local class shall be defined inline in their class
|
| 556 |
definition, if they are defined at all.
|
| 557 |
|
|
|
|
|
|
|
| 558 |
A member function can be declared (but not defined) using a typedef for
|
| 559 |
a function type. The resulting member function has exactly the same type
|
| 560 |
as it would have if the function declarator were provided explicitly,
|
| 561 |
see [[dcl.fct]]. For example,
|
| 562 |
|
| 563 |
``` cpp
|
| 564 |
-
typedef void fv(
|
| 565 |
-
typedef void fvc(
|
| 566 |
struct S {
|
| 567 |
-
fv memfunc1; // equivalent to: void memfunc1(
|
| 568 |
void memfunc2();
|
| 569 |
-
fvc memfunc3; // equivalent to: void memfunc3(
|
| 570 |
};
|
| 571 |
fv S::* pmfv1 = &S::memfunc1;
|
| 572 |
fv S::* pmfv2 = &S::memfunc2;
|
| 573 |
fvc S::* pmfv3 = &S::memfunc3;
|
| 574 |
```
|
| 575 |
|
| 576 |
Also see [[temp.arg]].
|
| 577 |
|
| 578 |
-
|
| 579 |
|
| 580 |
-
|
|
|
|
|
|
|
| 581 |
type, or for an object of a class derived (Clause [[class.derived]])
|
| 582 |
from its class type, using the class member access syntax (
|
| 583 |
[[expr.ref]], [[over.match.call]]). A non-static member function may
|
| 584 |
also be called directly using the function call syntax ([[expr.call]],
|
| 585 |
[[over.match.call]]) from within the body of a member function of its
|
|
@@ -590,24 +775,30 @@ that is not of type `X`, or of a type derived from `X`, the behavior is
|
|
| 590 |
undefined.
|
| 591 |
|
| 592 |
When an *id-expression* ([[expr.prim]]) that is not part of a class
|
| 593 |
member access syntax ([[expr.ref]]) and not used to form a pointer to
|
| 594 |
member ([[expr.unary.op]]) is used in a member of class `X` in a
|
| 595 |
-
context where `this` can be used ([[expr.prim.
|
| 596 |
-
|
| 597 |
non-static non-type member of some class `C`, and if either the
|
| 598 |
*id-expression* is potentially evaluated or `C` is `X` or a base class
|
| 599 |
of `X`, the *id-expression* is transformed into a class member access
|
| 600 |
expression ([[expr.ref]]) using `(*this)` ([[class.this]]) as the
|
| 601 |
-
*postfix-expression* to the left of the `.` operator.
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
a
|
| 608 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
|
| 610 |
``` cpp
|
| 611 |
struct tnode {
|
| 612 |
char tword[20];
|
| 613 |
int count;
|
|
@@ -636,46 +827,56 @@ In the body of the member function `tnode::set`, the member names
|
|
| 636 |
which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
| 637 |
`tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
|
| 638 |
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 639 |
not members of the class `tnode` and should be declared elsewhere.[^4]
|
| 640 |
|
|
|
|
|
|
|
| 641 |
A non-static member function may be declared `const`, `volatile`, or
|
| 642 |
-
`const` `volatile`. These *cv-
|
| 643 |
pointer ([[class.this]]). They also affect the function type (
|
| 644 |
[[dcl.fct]]) of the member function; a member function declared `const`
|
| 645 |
is a *const* member function, a member function declared `volatile` is a
|
| 646 |
*volatile* member function and a member function declared `const`
|
| 647 |
`volatile` is a *const volatile* member function.
|
| 648 |
|
|
|
|
|
|
|
| 649 |
``` cpp
|
| 650 |
struct X {
|
| 651 |
void g() const;
|
| 652 |
void h() const volatile;
|
| 653 |
};
|
| 654 |
```
|
| 655 |
|
| 656 |
`X::g` is a `const` member function and `X::h` is a `const` `volatile`
|
| 657 |
member function.
|
| 658 |
|
|
|
|
|
|
|
| 659 |
A non-static member function may be declared with a *ref-qualifier* (
|
| 660 |
[[dcl.fct]]); see [[over.match.funcs]].
|
| 661 |
|
| 662 |
A non-static member function may be declared *virtual* (
|
| 663 |
[[class.virtual]]) or *pure virtual* ([[class.abstract]]).
|
| 664 |
|
| 665 |
-
### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 666 |
|
| 667 |
In the body of a non-static ([[class.mfct]]) member function, the
|
| 668 |
keyword `this` is a prvalue expression whose value is the address of the
|
| 669 |
object for which the function is called. The type of `this` in a member
|
| 670 |
function of a class `X` is `X*`. If the member function is declared
|
| 671 |
`const`, the type of `this` is `const` `X*`, if the member function is
|
| 672 |
declared `volatile`, the type of `this` is `volatile` `X*`, and if the
|
| 673 |
member function is declared `const` `volatile`, the type of `this` is
|
| 674 |
-
`const` `volatile` `X*`.
|
| 675 |
-
|
| 676 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
|
| 678 |
``` cpp
|
| 679 |
struct s {
|
| 680 |
int a;
|
| 681 |
int f() const;
|
|
@@ -689,18 +890,22 @@ int s::f() const { return a; }
|
|
| 689 |
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 690 |
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 691 |
in a `const` member function because `this` is a pointer to `const`;
|
| 692 |
that is, `*this` has `const` type.
|
| 693 |
|
|
|
|
|
|
|
| 694 |
Similarly, `volatile` semantics ([[dcl.type.cv]]) apply in `volatile`
|
| 695 |
member functions when accessing the object and its non-static data
|
| 696 |
members.
|
| 697 |
|
| 698 |
-
A
|
| 699 |
[[expr.ref]]) only if the object-expression is as cv-qualified or
|
| 700 |
less-cv-qualified than the member function.
|
| 701 |
|
|
|
|
|
|
|
| 702 |
``` cpp
|
| 703 |
void k(s& x, const s& y) {
|
| 704 |
x.f();
|
| 705 |
x.g();
|
| 706 |
y.f();
|
|
@@ -710,26 +915,29 @@ void k(s& x, const s& y) {
|
|
| 710 |
|
| 711 |
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 712 |
non-`const` member function, that is, `s::g()` is less-qualified than
|
| 713 |
the object-expression `y`.
|
| 714 |
|
|
|
|
|
|
|
| 715 |
Constructors ([[class.ctor]]) and destructors ([[class.dtor]]) shall
|
| 716 |
-
not be declared `const`, `volatile` or `const` `volatile`.
|
| 717 |
-
these functions can be invoked to create and destroy objects with
|
| 718 |
-
cv-qualified types, see ([[class.ctor]]) and ([[class.dtor]]).
|
| 719 |
|
| 720 |
-
|
|
|
|
|
|
|
| 721 |
|
| 722 |
-
|
| 723 |
-
definition, in which case it is a *static member* of the class.
|
| 724 |
|
| 725 |
-
A
|
| 726 |
*qualified-id* expression `X::s`; it is not necessary to use the class
|
| 727 |
-
member access syntax ([[expr.ref]]) to refer to a
|
| 728 |
-
|
| 729 |
in which case the object expression is evaluated.
|
| 730 |
|
|
|
|
|
|
|
| 731 |
``` cpp
|
| 732 |
struct process {
|
| 733 |
static void reschedule();
|
| 734 |
};
|
| 735 |
process& g();
|
|
@@ -738,17 +946,21 @@ void f() {
|
|
| 738 |
process::reschedule(); // OK: no object necessary
|
| 739 |
g().reschedule(); // g() is called
|
| 740 |
}
|
| 741 |
```
|
| 742 |
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
|
|
|
|
|
|
| 746 |
*qualified-id* expression was used, with the *nested-name-specifier* of
|
| 747 |
the *qualified-id* naming the class scope from which the static member
|
| 748 |
is referenced.
|
| 749 |
|
|
|
|
|
|
|
| 750 |
``` cpp
|
| 751 |
int g();
|
| 752 |
struct X {
|
| 753 |
static int g();
|
| 754 |
};
|
|
@@ -756,55 +968,63 @@ struct Y : X {
|
|
| 756 |
static int i;
|
| 757 |
};
|
| 758 |
int Y::i = g(); // equivalent to Y::g();
|
| 759 |
```
|
| 760 |
|
|
|
|
|
|
|
| 761 |
If an *unqualified-id* ([[expr.prim]]) is used in the definition of a
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
|
|
|
| 770 |
|
| 771 |
Static members obey the usual class member access rules (Clause
|
| 772 |
[[class.access]]). When used in the declaration of a class member, the
|
| 773 |
`static` specifier shall only be used in the member declarations that
|
| 774 |
-
appear within the *member-specification* of the class definition.
|
| 775 |
-
cannot be specified in member declarations that appear in namespace
|
| 776 |
-
scope.
|
| 777 |
|
| 778 |
-
|
|
|
|
| 779 |
|
| 780 |
-
|
| 781 |
-
functions.
|
| 782 |
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
There shall not be a `static` and a non-static member function with the
|
| 786 |
-
same name and the same parameter types ([[over.load]]). A `static`
|
| 787 |
-
member function shall not be declared `const`, `volatile`, or
|
| 788 |
-
`const volatile`.
|
| 789 |
|
| 790 |
-
|
|
|
|
| 791 |
|
| 792 |
-
A
|
| 793 |
-
|
| 794 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
`thread_local` there is one copy of the data member that is shared by
|
| 796 |
all the objects of the class.
|
| 797 |
|
| 798 |
-
The declaration of a
|
| 799 |
-
a definition and may be of an incomplete type other
|
| 800 |
-
`void`. The definition for a
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
in the scope of its class (
|
|
|
|
|
|
|
|
|
|
| 806 |
|
| 807 |
``` cpp
|
| 808 |
class process {
|
| 809 |
static process* run_chain;
|
| 810 |
static process* running;
|
|
@@ -812,172 +1032,65 @@ class process {
|
|
| 812 |
|
| 813 |
process* process::running = get_main();
|
| 814 |
process* process::run_chain = running;
|
| 815 |
```
|
| 816 |
|
| 817 |
-
The
|
| 818 |
global scope; the notation `process::run_chain` specifies that the
|
| 819 |
member `run_chain` is a member of class `process` and in the scope of
|
| 820 |
-
class `process`. In the
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
*
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
Unnamed classes and classes contained directly or indirectly within
|
| 845 |
-
unnamed classes shall not contain
|
| 846 |
|
| 847 |
-
|
| 848 |
-
linkage ([[basic.link]]). A local class
|
| 849 |
-
members.
|
| 850 |
|
| 851 |
-
|
| 852 |
-
|
|
|
|
| 853 |
|
| 854 |
-
A
|
| 855 |
|
| 856 |
-
##
|
| 857 |
-
|
| 858 |
-
In a union, at most one of the non-static data members can be active at
|
| 859 |
-
any time, that is, the value of at most one of the non-static data
|
| 860 |
-
members can be stored in a union at any time. One special guarantee is
|
| 861 |
-
made in order to simplify the use of unions: If a standard-layout union
|
| 862 |
-
contains several standard-layout structs that share a common initial
|
| 863 |
-
sequence ([[class.mem]]), and if an object of this standard-layout
|
| 864 |
-
union type contains one of the standard-layout structs, it is permitted
|
| 865 |
-
to inspect the common initial sequence of any of standard-layout struct
|
| 866 |
-
members; see [[class.mem]]. The size of a union is sufficient to
|
| 867 |
-
contain the largest of its non-static data members. Each non-static data
|
| 868 |
-
member is allocated as if it were the sole member of a struct. All
|
| 869 |
-
non-static data members of a union object have the same address.
|
| 870 |
-
|
| 871 |
-
A union can have member functions (including constructors and
|
| 872 |
-
destructors), but not virtual ([[class.virtual]]) functions. A union
|
| 873 |
-
shall not have base classes. A union shall not be used as a base class.
|
| 874 |
-
If a union contains a non-static data member of reference type the
|
| 875 |
-
program is ill-formed. If any non-static data member of a union has a
|
| 876 |
-
non-trivial default constructor ([[class.ctor]]), copy constructor (
|
| 877 |
-
[[class.copy]]), move constructor ([[class.copy]]), copy assignment
|
| 878 |
-
operator ([[class.copy]]), move assignment operator ([[class.copy]]),
|
| 879 |
-
or destructor ([[class.dtor]]), the corresponding member function of
|
| 880 |
-
the union must be user-provided or it will be implicitly deleted (
|
| 881 |
-
[[dcl.fct.def.delete]]) for the union.
|
| 882 |
-
|
| 883 |
-
Consider the following union:
|
| 884 |
-
|
| 885 |
-
``` cpp
|
| 886 |
-
union U {
|
| 887 |
-
int i;
|
| 888 |
-
float f;
|
| 889 |
-
std::string s;
|
| 890 |
-
};
|
| 891 |
-
```
|
| 892 |
-
|
| 893 |
-
Since `std::string` ([[string.classes]]) declares non-trivial versions
|
| 894 |
-
of all of the special member functions, `U` will have an implicitly
|
| 895 |
-
deleted default constructor, copy/move constructor, copy/move assignment
|
| 896 |
-
operator, and destructor. To use `U`, some or all of these member
|
| 897 |
-
functions must be user-provided.
|
| 898 |
-
|
| 899 |
-
In general, one must use explicit destructor calls and placement new
|
| 900 |
-
operators to change the active member of a union. Consider an object `u`
|
| 901 |
-
of a `union` type `U` having non-static data members `m` of type `M` and
|
| 902 |
-
`n` of type `N`. If `M` has a non-trivial destructor and `N` has a
|
| 903 |
-
non-trivial constructor (for instance, if they declare or inherit
|
| 904 |
-
virtual functions), the active member of `u` can be safely switched from
|
| 905 |
-
`m` to `n` using the destructor and placement new operator as follows:
|
| 906 |
-
|
| 907 |
-
``` cpp
|
| 908 |
-
u.m.~M();
|
| 909 |
-
new (&u.n) N;
|
| 910 |
-
```
|
| 911 |
-
|
| 912 |
-
A union of the form
|
| 913 |
-
|
| 914 |
-
is called an *anonymous union*; it defines an unnamed object of unnamed
|
| 915 |
-
type. The *member-specification* of an anonymous union shall only define
|
| 916 |
-
non-static data members. Nested types, anonymous unions, and functions
|
| 917 |
-
cannot be declared within an anonymous union. The names of the members
|
| 918 |
-
of an anonymous union shall be distinct from the names of any other
|
| 919 |
-
entity in the scope in which the anonymous union is declared. For the
|
| 920 |
-
purpose of name lookup, after the anonymous union definition, the
|
| 921 |
-
members of the anonymous union are considered to have been defined in
|
| 922 |
-
the scope in which the anonymous union is declared.
|
| 923 |
-
|
| 924 |
-
``` cpp
|
| 925 |
-
void f() {
|
| 926 |
-
union { int a; const char* p; };
|
| 927 |
-
a = 1;
|
| 928 |
-
p = "Jennifer";
|
| 929 |
-
}
|
| 930 |
-
```
|
| 931 |
-
|
| 932 |
-
Here `a` and `p` are used like ordinary (nonmember) variables, but since
|
| 933 |
-
they are union members they have the same address.
|
| 934 |
-
|
| 935 |
-
Anonymous unions declared in a named namespace or in the global
|
| 936 |
-
namespace shall be declared `static`. Anonymous unions declared at block
|
| 937 |
-
scope shall be declared with any storage class allowed for a block-scope
|
| 938 |
-
variable, or with no storage class. A storage class is not allowed in a
|
| 939 |
-
declaration of an anonymous union in a class scope. An anonymous union
|
| 940 |
-
shall not have `private` or `protected` members (Clause
|
| 941 |
-
[[class.access]]). An anonymous union shall not have function members.
|
| 942 |
-
|
| 943 |
-
A union for which objects, pointers, or references are declared is not
|
| 944 |
-
an anonymous union.
|
| 945 |
-
|
| 946 |
-
``` cpp
|
| 947 |
-
void f() {
|
| 948 |
-
union { int aa; char* p; } obj, *ptr = &obj;
|
| 949 |
-
aa = 1; // error
|
| 950 |
-
ptr->aa = 1; // OK
|
| 951 |
-
}
|
| 952 |
-
```
|
| 953 |
-
|
| 954 |
-
The assignment to plain `aa` is ill-formed since the member name is not
|
| 955 |
-
visible outside the union, and even if it were visible, it is not
|
| 956 |
-
associated with any particular object. Initialization of unions with no
|
| 957 |
-
user-declared constructors is described in ([[dcl.init.aggr]]).
|
| 958 |
-
|
| 959 |
-
A *union-like class* is a union or a class that has an anonymous union
|
| 960 |
-
as a direct member. A union-like class `X` has a set of *variant
|
| 961 |
-
members*. If `X` is a union, a non-static data member of `X` that is not
|
| 962 |
-
an anonymous union is a variant member of `X`. In addition, a non-static
|
| 963 |
-
data member of an anonymous union that is a member of `X` is also a
|
| 964 |
-
variant member of `X`. At most one variant member of a union may have a
|
| 965 |
-
*brace-or-equal-initializer*.
|
| 966 |
-
|
| 967 |
-
``` cpp
|
| 968 |
-
union U {
|
| 969 |
-
int x = 0;
|
| 970 |
-
union { };
|
| 971 |
-
union {
|
| 972 |
-
int z;
|
| 973 |
-
int y = 1; // error: initialization for second variant member of U
|
| 974 |
-
};
|
| 975 |
-
};
|
| 976 |
-
```
|
| 977 |
-
|
| 978 |
-
## Bit-fields <a id="class.bit">[[class.bit]]</a>
|
| 979 |
|
| 980 |
A *member-declarator* of the form
|
| 981 |
|
| 982 |
specifies a bit-field; its length is set off from the bit-field name by
|
| 983 |
a colon. The optional *attribute-specifier-seq* appertains to the entity
|
|
@@ -989,61 +1102,77 @@ the object representation ([[basic.types]]) of the bit-field’s type; in
|
|
| 989 |
such cases the extra bits are used as padding bits and do not
|
| 990 |
participate in the value representation ([[basic.types]]) of the
|
| 991 |
bit-field. Allocation of bit-fields within a class object is
|
| 992 |
*implementation-defined*. Alignment of bit-fields is
|
| 993 |
*implementation-defined*. Bit-fields are packed into some addressable
|
| 994 |
-
allocation unit.
|
| 995 |
-
|
| 996 |
-
|
|
|
|
|
|
|
| 997 |
|
| 998 |
A declaration for a bit-field that omits the *identifier* declares an
|
| 999 |
-
*unnamed
|
| 1000 |
-
initialized.
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1005 |
|
| 1006 |
A bit-field shall not be a static member. A bit-field shall have
|
| 1007 |
integral or enumeration type ([[basic.fundamental]]). A `bool` value
|
| 1008 |
can successfully be stored in a bit-field of any nonzero size. The
|
| 1009 |
address-of operator `&` shall not be applied to a bit-field, so there
|
| 1010 |
are no pointers to bit-fields. A non-const reference shall not be bound
|
| 1011 |
-
to a bit-field ([[dcl.init.ref]]).
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
bit-field
|
| 1015 |
-
|
|
|
|
|
|
|
| 1016 |
|
| 1017 |
If the value `true` or `false` is stored into a bit-field of type `bool`
|
| 1018 |
of any size (including a one bit bit-field), the original `bool` value
|
| 1019 |
and the value of the bit-field shall compare equal. If the value of an
|
| 1020 |
enumerator is stored into a bit-field of the same enumeration type and
|
| 1021 |
the number of bits in the bit-field is large enough to hold all the
|
| 1022 |
values of that enumeration type ([[dcl.enum]]), the original enumerator
|
| 1023 |
value and the value of the bit-field shall compare equal.
|
| 1024 |
|
|
|
|
|
|
|
| 1025 |
``` cpp
|
| 1026 |
enum BOOL { FALSE=0, TRUE=1 };
|
| 1027 |
struct A {
|
| 1028 |
BOOL b:1;
|
| 1029 |
};
|
| 1030 |
A a;
|
| 1031 |
void f() {
|
| 1032 |
a.b = TRUE;
|
| 1033 |
if (a.b == TRUE) // yields true
|
| 1034 |
-
{
|
| 1035 |
}
|
| 1036 |
```
|
| 1037 |
|
| 1038 |
-
|
|
|
|
|
|
|
| 1039 |
|
| 1040 |
A class can be declared within another class. A class declared within
|
| 1041 |
another is called a *nested* class. The name of a nested class is local
|
| 1042 |
to its enclosing class. The nested class is in the scope of its
|
| 1043 |
-
enclosing class.
|
| 1044 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1045 |
|
| 1046 |
``` cpp
|
| 1047 |
int x;
|
| 1048 |
int y;
|
| 1049 |
|
|
@@ -1066,56 +1195,313 @@ struct enclose {
|
|
| 1066 |
};
|
| 1067 |
|
| 1068 |
inner* p = 0; // error: inner not in scope
|
| 1069 |
```
|
| 1070 |
|
|
|
|
|
|
|
| 1071 |
Member functions and static data members of a nested class can be
|
| 1072 |
defined in a namespace scope enclosing the definition of their class.
|
| 1073 |
|
|
|
|
|
|
|
| 1074 |
``` cpp
|
| 1075 |
struct enclose {
|
| 1076 |
struct inner {
|
| 1077 |
static int x;
|
| 1078 |
void f(int i);
|
| 1079 |
};
|
| 1080 |
};
|
| 1081 |
|
| 1082 |
int enclose::inner::x = 1;
|
| 1083 |
|
| 1084 |
-
void enclose::inner::f(int i) {
|
| 1085 |
```
|
| 1086 |
|
|
|
|
|
|
|
| 1087 |
If class `X` is defined in a namespace scope, a nested class `Y` may be
|
| 1088 |
declared in class `X` and later defined in the definition of class `X`
|
| 1089 |
or be later defined in a namespace scope enclosing the definition of
|
| 1090 |
class `X`.
|
| 1091 |
|
|
|
|
|
|
|
| 1092 |
``` cpp
|
| 1093 |
class E {
|
| 1094 |
class I1; // forward declaration of nested class
|
| 1095 |
class I2;
|
| 1096 |
class I1 { }; // definition of nested class
|
| 1097 |
};
|
| 1098 |
class E::I2 { }; // definition of nested class
|
| 1099 |
```
|
| 1100 |
|
|
|
|
|
|
|
| 1101 |
Like a member function, a friend function ([[class.friend]]) defined
|
| 1102 |
within a nested class is in the lexical scope of that class; it obeys
|
| 1103 |
the same rules for name binding as a static member function of that
|
| 1104 |
class ([[class.static]]), but it has no special access rights to
|
| 1105 |
members of an enclosing class.
|
| 1106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1107 |
## Local class declarations <a id="class.local">[[class.local]]</a>
|
| 1108 |
|
| 1109 |
A class can be declared within a function definition; such a class is
|
| 1110 |
called a *local* class. The name of a local class is local to its
|
| 1111 |
enclosing scope. The local class is in the scope of the enclosing scope,
|
| 1112 |
and has the same access to names outside the function as does the
|
| 1113 |
enclosing function. Declarations in a local class shall not odr-use (
|
| 1114 |
[[basic.def.odr]]) a variable with automatic storage duration from an
|
| 1115 |
enclosing scope.
|
| 1116 |
|
|
|
|
|
|
|
| 1117 |
``` cpp
|
| 1118 |
int x;
|
| 1119 |
void f() {
|
| 1120 |
static int s;
|
| 1121 |
int x;
|
|
@@ -1133,10 +1519,12 @@ void f() {
|
|
| 1133 |
}
|
| 1134 |
|
| 1135 |
local* p = 0; // error: local not in scope
|
| 1136 |
```
|
| 1137 |
|
|
|
|
|
|
|
| 1138 |
An enclosing function has no special access to members of the local
|
| 1139 |
class; it obeys the usual access rules (Clause [[class.access]]).
|
| 1140 |
Member functions of a local class shall be defined within their class
|
| 1141 |
definition, if they are defined at all.
|
| 1142 |
|
|
@@ -1145,24 +1533,5 @@ class `X` and later defined in the definition of class `X` or be later
|
|
| 1145 |
defined in the same scope as the definition of class `X`. A class nested
|
| 1146 |
within a local class is a local class.
|
| 1147 |
|
| 1148 |
A local class shall not have static data members.
|
| 1149 |
|
| 1150 |
-
## Nested type names <a id="class.nested.type">[[class.nested.type]]</a>
|
| 1151 |
-
|
| 1152 |
-
Type names obey exactly the same scope rules as other names. In
|
| 1153 |
-
particular, type names defined within a class definition cannot be used
|
| 1154 |
-
outside their class without qualification.
|
| 1155 |
-
|
| 1156 |
-
``` cpp
|
| 1157 |
-
struct X {
|
| 1158 |
-
typedef int I;
|
| 1159 |
-
class Y { /* ... */ };
|
| 1160 |
-
I a;
|
| 1161 |
-
};
|
| 1162 |
-
|
| 1163 |
-
I b; // error
|
| 1164 |
-
Y c; // error
|
| 1165 |
-
X::Y d; // OK
|
| 1166 |
-
X::I e; // OK
|
| 1167 |
-
```
|
| 1168 |
-
|
|
|
|
| 40 |
'struct'
|
| 41 |
'union'
|
| 42 |
```
|
| 43 |
|
| 44 |
A *class-specifier* whose *class-head* omits the *class-head-name*
|
| 45 |
+
defines an unnamed class.
|
| 46 |
+
|
| 47 |
+
[*Note 1*: An unnamed class thus can’t be `final`. — *end note*]
|
| 48 |
|
| 49 |
A *class-name* is inserted into the scope in which it is declared
|
| 50 |
immediately after the *class-name* is seen. The *class-name* is also
|
| 51 |
inserted into the scope of the class itself; this is known as the
|
| 52 |
*injected-class-name*. For purposes of access checking, the
|
|
|
|
| 57 |
defined. The optional *attribute-specifier-seq* appertains to the class;
|
| 58 |
the attributes in the *attribute-specifier-seq* are thereafter
|
| 59 |
considered attributes of the class whenever it is named.
|
| 60 |
|
| 61 |
If a class is marked with the *class-virt-specifier* `final` and it
|
| 62 |
+
appears as a *class-or-decltype* in a *base-clause* (Clause
|
| 63 |
[[class.derived]]), the program is ill-formed. Whenever a *class-key* is
|
| 64 |
followed by a *class-head-name*, the *identifier* `final`, and a colon
|
| 65 |
or left brace, `final` is interpreted as a *class-virt-specifier*.
|
| 66 |
|
| 67 |
+
[*Example 1*:
|
| 68 |
+
|
| 69 |
``` cpp
|
| 70 |
struct A;
|
| 71 |
struct A final {}; // OK: definition of struct A,
|
| 72 |
// not value-initialization of variable final
|
| 73 |
|
|
|
|
| 76 |
struct B final : C{}; // OK: definition of nested class B,
|
| 77 |
// not declaration of a bit-field member final
|
| 78 |
};
|
| 79 |
```
|
| 80 |
|
| 81 |
+
— *end example*]
|
| 82 |
+
|
| 83 |
Complete objects and member subobjects of class type shall have nonzero
|
| 84 |
+
size.[^1]
|
| 85 |
+
|
| 86 |
+
[*Note 2*: Class objects can be assigned, passed as arguments to
|
| 87 |
functions, and returned by functions (except objects of classes for
|
| 88 |
which copying or moving has been restricted; see [[class.copy]]). Other
|
| 89 |
plausible operators, such as equality comparison, can be defined by the
|
| 90 |
+
user; see [[over.oper]]. — *end note*]
|
| 91 |
|
| 92 |
A *union* is a class defined with the *class-key* `union`; it holds at
|
| 93 |
+
most one data member at a time ([[class.union]]).
|
|
|
|
| 94 |
|
| 95 |
+
[*Note 3*: Aggregates of class type are described in
|
| 96 |
+
[[dcl.init.aggr]]. — *end note*]
|
| 97 |
|
| 98 |
+
A *trivially copyable class* is a class:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
- where each copy constructor, move constructor, copy assignment
|
| 101 |
+
operator, and move assignment operator ([[class.copy]], [[over.ass]])
|
| 102 |
+
is either deleted or trivial,
|
| 103 |
+
- that has at least one non-deleted copy constructor, move constructor,
|
| 104 |
+
copy assignment operator, or move assignment operator, and
|
| 105 |
+
- that has a trivial, non-deleted destructor ([[class.dtor]]).
|
| 106 |
|
| 107 |
+
A *trivial class* is a class that is trivially copyable and has one or
|
| 108 |
+
more default constructors ([[class.ctor]]), all of which are either
|
| 109 |
+
trivial or deleted and at least one of which is not deleted.
|
| 110 |
+
|
| 111 |
+
[*Note 4*: In particular, a trivially copyable or trivial class does
|
| 112 |
+
not have virtual functions or virtual base classes. — *end note*]
|
| 113 |
+
|
| 114 |
+
A class `S` is a *standard-layout class* if it:
|
| 115 |
|
| 116 |
- has no non-static data members of type non-standard-layout class (or
|
| 117 |
array of such types) or reference,
|
| 118 |
- has no virtual functions ([[class.virtual]]) and no virtual base
|
| 119 |
classes ([[class.mi]]),
|
| 120 |
- has the same access control (Clause [[class.access]]) for all
|
| 121 |
non-static data members,
|
| 122 |
- has no non-standard-layout base classes,
|
| 123 |
+
- has at most one base class subobject of any given type,
|
| 124 |
+
- has all non-static data members and bit-fields in the class and its
|
| 125 |
+
base classes first declared in the same class, and
|
| 126 |
+
- has no element of the set M(S) of types (defined below) as a base
|
| 127 |
+
class.[^2]
|
| 128 |
+
|
| 129 |
+
M(X) is defined as follows:
|
| 130 |
+
|
| 131 |
+
- If `X` is a non-union class type with no (possibly inherited (Clause
|
| 132 |
+
[[class.derived]])) non-static data members, the set M(X) is empty.
|
| 133 |
+
- If `X` is a non-union class type whose first non-static data member
|
| 134 |
+
has type X₀ (where said member may be an anonymous union), the set
|
| 135 |
+
M(X) consists of X₀ and the elements of M(X₀).
|
| 136 |
+
- If `X` is a union type, the set M(X) is the union of all M(Uᵢ) and the
|
| 137 |
+
set containing all Uᵢ, where each Uᵢ is the type of the ith non-static
|
| 138 |
+
data member of `X`.
|
| 139 |
+
- If `X` is an array type with element type Xₑ, the set M(X) consists of
|
| 140 |
+
Xₑ and the elements of M(Xₑ).
|
| 141 |
+
- If `X` is a non-class, non-array type, the set M(X) is empty.
|
| 142 |
+
|
| 143 |
+
[*Note 5*: M(X) is the set of the types of all non-base-class
|
| 144 |
+
subobjects that are guaranteed in a standard-layout class to be at a
|
| 145 |
+
zero offset in `X`. — *end note*]
|
| 146 |
+
|
| 147 |
+
[*Example 2*:
|
| 148 |
+
|
| 149 |
+
``` cpp
|
| 150 |
+
struct B { int i; }; // standard-layout class
|
| 151 |
+
struct C : B { }; // standard-layout class
|
| 152 |
+
struct D : C { }; // standard-layout class
|
| 153 |
+
struct E : D { char : 4; }; // not a standard-layout class
|
| 154 |
+
|
| 155 |
+
struct Q {};
|
| 156 |
+
struct S : Q { };
|
| 157 |
+
struct T : Q { };
|
| 158 |
+
struct U : S, T { }; // not a standard-layout class
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
— *end example*]
|
| 162 |
|
| 163 |
A *standard-layout struct* is a standard-layout class defined with the
|
| 164 |
+
*class-key* `struct` or the *class-key* `class`. A *standard-layout
|
| 165 |
+
union* is a standard-layout class defined with the *class-key* `union`.
|
|
|
|
| 166 |
|
| 167 |
+
[*Note 6*: Standard-layout classes are useful for communicating with
|
| 168 |
+
code written in other programming languages. Their layout is specified
|
| 169 |
+
in [[class.mem]]. — *end note*]
|
| 170 |
|
| 171 |
A *POD struct*[^3] is a non-union class that is both a trivial class and
|
| 172 |
a standard-layout class, and has no non-static data members of type
|
| 173 |
non-POD struct, non-POD union (or array of such types). Similarly, a
|
| 174 |
*POD union* is a union that is both a trivial class and a
|
| 175 |
standard-layout class, and has no non-static data members of type
|
| 176 |
non-POD struct, non-POD union (or array of such types). A *POD class* is
|
| 177 |
a class that is either a POD struct or a POD union.
|
| 178 |
|
| 179 |
+
[*Example 3*:
|
| 180 |
+
|
| 181 |
``` cpp
|
| 182 |
struct N { // neither trivial nor standard-layout
|
| 183 |
int i;
|
| 184 |
int j;
|
| 185 |
virtual ~N();
|
|
|
|
| 201 |
int i;
|
| 202 |
int j;
|
| 203 |
};
|
| 204 |
```
|
| 205 |
|
| 206 |
+
— *end example*]
|
| 207 |
+
|
| 208 |
If a *class-head-name* contains a *nested-name-specifier*, the
|
| 209 |
*class-specifier* shall refer to a class that was previously declared
|
| 210 |
directly in the class or namespace to which the *nested-name-specifier*
|
| 211 |
refers, or in an element of the inline namespace set (
|
| 212 |
[[namespace.def]]) of that namespace (i.e., not merely inherited or
|
|
|
|
| 217 |
|
| 218 |
## Class names <a id="class.name">[[class.name]]</a>
|
| 219 |
|
| 220 |
A class definition introduces a new type.
|
| 221 |
|
| 222 |
+
[*Example 1*:
|
| 223 |
+
|
| 224 |
``` cpp
|
| 225 |
struct X { int a; };
|
| 226 |
struct Y { int a; };
|
| 227 |
X a1;
|
| 228 |
Y a2;
|
|
|
|
| 251 |
struct S { int a; }; // error, double definition
|
| 252 |
```
|
| 253 |
|
| 254 |
is ill-formed because it defines `S` twice.
|
| 255 |
|
| 256 |
+
— *end example*]
|
| 257 |
+
|
| 258 |
A class declaration introduces the class name into the scope where it is
|
| 259 |
declared and hides any class, variable, function, or other declaration
|
| 260 |
of that name in an enclosing scope ([[basic.scope]]). If a class name
|
| 261 |
is declared in a scope where a variable, function, or enumerator of the
|
| 262 |
same name is also declared, then when both declarations are in scope,
|
| 263 |
the class can be referred to only using an *elaborated-type-specifier* (
|
| 264 |
[[basic.lookup.elab]]).
|
| 265 |
|
| 266 |
+
[*Example 2*:
|
| 267 |
+
|
| 268 |
``` cpp
|
| 269 |
struct stat {
|
| 270 |
// ...
|
| 271 |
};
|
| 272 |
|
| 273 |
+
stat gstat; // use plain stat to define variable
|
|
|
|
| 274 |
|
| 275 |
int stat(struct stat*); // redeclare stat as function
|
| 276 |
|
| 277 |
void f() {
|
| 278 |
+
struct stat* ps; // struct prefix needed to name struct stat
|
|
|
|
| 279 |
stat(ps); // call stat()
|
| 280 |
}
|
| 281 |
```
|
| 282 |
|
| 283 |
+
— *end example*]
|
| 284 |
+
|
| 285 |
+
A *declaration* consisting solely of *class-key* *identifier*`;` is
|
| 286 |
+
either a redeclaration of the name in the current scope or a forward
|
| 287 |
+
declaration of the identifier as a class name. It introduces the class
|
| 288 |
+
name into the current scope.
|
| 289 |
+
|
| 290 |
+
[*Example 3*:
|
| 291 |
|
| 292 |
``` cpp
|
| 293 |
struct s { int a; };
|
| 294 |
|
| 295 |
void g() {
|
| 296 |
+
struct s; // hide global struct s with a block-scope declaration
|
|
|
|
| 297 |
s* p; // refer to local struct s
|
| 298 |
struct s { char* p; }; // define local struct s
|
| 299 |
struct s; // redeclaration, has no effect
|
| 300 |
}
|
| 301 |
```
|
| 302 |
|
| 303 |
+
— *end example*]
|
| 304 |
+
|
| 305 |
+
[*Note 1*:
|
| 306 |
+
|
| 307 |
Such declarations allow definition of classes that refer to each other.
|
| 308 |
|
| 309 |
+
[*Example 4*:
|
| 310 |
+
|
| 311 |
``` cpp
|
| 312 |
class Vector;
|
| 313 |
|
| 314 |
class Matrix {
|
| 315 |
// ...
|
|
|
|
| 323 |
```
|
| 324 |
|
| 325 |
Declaration of `friend`s is described in [[class.friend]], operator
|
| 326 |
functions in [[over.oper]].
|
| 327 |
|
| 328 |
+
— *end example*]
|
| 329 |
+
|
| 330 |
+
— *end note*]
|
| 331 |
+
|
| 332 |
+
[*Note 2*: An *elaborated-type-specifier* ([[dcl.type.elab]]) can also
|
| 333 |
+
be used as a *type-specifier* as part of a declaration. It differs from
|
| 334 |
+
a class declaration in that if a class of the elaborated name is in
|
| 335 |
+
scope the elaborated name will refer to it. — *end note*]
|
| 336 |
+
|
| 337 |
+
[*Example 5*:
|
| 338 |
|
| 339 |
``` cpp
|
| 340 |
struct s { int a; };
|
| 341 |
|
| 342 |
void g(int s) {
|
| 343 |
struct s* p = new struct s; // global s
|
| 344 |
p->a = s; // parameter s
|
| 345 |
}
|
| 346 |
```
|
| 347 |
|
| 348 |
+
— *end example*]
|
| 349 |
+
|
| 350 |
+
[*Note 3*:
|
| 351 |
+
|
| 352 |
The declaration of a class name takes effect immediately after the
|
| 353 |
*identifier* is seen in the class definition or
|
| 354 |
*elaborated-type-specifier*. For example,
|
| 355 |
|
| 356 |
``` cpp
|
|
|
|
| 360 |
first specifies `A` to be the name of a class and then redefines it as
|
| 361 |
the name of a pointer to an object of that class. This means that the
|
| 362 |
elaborated form `class` `A` must be used to refer to the class. Such
|
| 363 |
artistry with names can be confusing and is best avoided.
|
| 364 |
|
| 365 |
+
— *end note*]
|
| 366 |
+
|
| 367 |
A *typedef-name* ([[dcl.typedef]]) that names a class type, or a
|
| 368 |
cv-qualified version thereof, is also a *class-name*. If a
|
| 369 |
*typedef-name* that names a cv-qualified class type is used where a
|
| 370 |
*class-name* is required, the cv-qualifiers are ignored. A
|
| 371 |
*typedef-name* shall not be used as the *identifier* in a *class-head*.
|
|
|
|
| 383 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 384 |
function-definition
|
| 385 |
using-declaration
|
| 386 |
static_assert-declaration
|
| 387 |
template-declaration
|
| 388 |
+
deduction-guide
|
| 389 |
alias-declaration
|
| 390 |
empty-declaration
|
| 391 |
```
|
| 392 |
|
| 393 |
``` bnf
|
|
|
|
| 419 |
pure-specifier:
|
| 420 |
'= 0'
|
| 421 |
```
|
| 422 |
|
| 423 |
The *member-specification* in a class definition declares the full set
|
| 424 |
+
of members of the class; no member can be added elsewhere. A *direct
|
| 425 |
+
member* of a class `X` is a member of `X` that was first declared within
|
| 426 |
+
the *member-specification* of `X`, including anonymous union objects (
|
| 427 |
+
[[class.union.anon]]) and direct members thereof. Members of a class are
|
| 428 |
+
data members, member functions ([[class.mfct]]), nested types,
|
| 429 |
+
enumerators, and member templates ([[temp.mem]]) and specializations
|
| 430 |
+
thereof.
|
| 431 |
+
|
| 432 |
+
[*Note 1*: A specialization of a static data member template is a
|
| 433 |
+
static data member. A specialization of a member function template is a
|
| 434 |
+
member function. A specialization of a member class template is a nested
|
| 435 |
+
class. — *end note*]
|
| 436 |
+
|
| 437 |
+
A *member-declaration* does not declare new members of the class if it
|
| 438 |
+
is
|
| 439 |
+
|
| 440 |
+
- a friend declaration ([[class.friend]]),
|
| 441 |
+
- a *static_assert-declaration*,
|
| 442 |
+
- a *using-declaration* ([[namespace.udecl]]), or
|
| 443 |
+
- an *empty-declaration*.
|
| 444 |
+
|
| 445 |
+
For any other *member-declaration*, each declared entity that is not an
|
| 446 |
+
unnamed bit-field ([[class.bit]]) is a member of the class, and each
|
| 447 |
+
such *member-declaration* shall either declare at least one member name
|
| 448 |
+
of the class or declare at least one unnamed bit-field.
|
| 449 |
+
|
| 450 |
+
A *data member* is a non-function member introduced by a
|
| 451 |
+
*member-declarator*. A *member function* is a member that is a function.
|
| 452 |
+
Nested types are classes ([[class.name]], [[class.nest]]) and
|
| 453 |
+
enumerations ([[dcl.enum]]) declared in the class and arbitrary types
|
| 454 |
+
declared as members by use of a typedef declaration ([[dcl.typedef]])
|
| 455 |
+
or *alias-declaration*. The enumerators of an unscoped enumeration (
|
| 456 |
+
[[dcl.enum]]) defined in the class are members of the class.
|
| 457 |
+
|
| 458 |
+
A data member or member function may be declared `static` in its
|
| 459 |
+
*member-declaration*, in which case it is a *static member* (see
|
| 460 |
+
[[class.static]]) (a *static data member* ([[class.static.data]]) or
|
| 461 |
+
*static member function* ([[class.static.mfct]]), respectively) of the
|
| 462 |
+
class. Any other data member or member function is a *non-static member*
|
| 463 |
+
(a *non-static data member* or *non-static member function* (
|
| 464 |
+
[[class.mfct.non-static]]), respectively).
|
| 465 |
+
|
| 466 |
+
[*Note 2*: A non-static data member of non-reference type is a member
|
| 467 |
+
subobject of a class object ([[intro.object]]). — *end note*]
|
| 468 |
+
|
| 469 |
+
A member shall not be declared twice in the *member-specification*,
|
| 470 |
+
except that
|
| 471 |
+
|
| 472 |
+
- a nested class or member class template can be declared and then later
|
| 473 |
+
defined, and
|
| 474 |
+
- an enumeration can be introduced with an *opaque-enum-declaration* and
|
| 475 |
+
later redeclared with an *enum-specifier*.
|
| 476 |
+
|
| 477 |
+
[*Note 3*: A single name can denote several member functions provided
|
| 478 |
+
their types are sufficiently different (Clause
|
| 479 |
+
[[over]]). — *end note*]
|
| 480 |
|
| 481 |
A class is considered a completely-defined object type (
|
| 482 |
[[basic.types]]) (or complete type) at the closing `}` of the
|
| 483 |
*class-specifier*. Within the class *member-specification*, the class is
|
| 484 |
regarded as complete within function bodies, default arguments,
|
| 485 |
+
*noexcept-specifier*s, and default member initializers (including such
|
| 486 |
+
things in nested classes). Otherwise it is regarded as incomplete within
|
| 487 |
+
its own class *member-specification*.
|
| 488 |
+
|
| 489 |
+
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 490 |
+
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 491 |
+
has function type, otherwise it is interpreted as introducing a
|
| 492 |
+
*brace-or-equal-initializer*.
|
| 493 |
+
|
| 494 |
+
[*Example 1*:
|
| 495 |
+
|
| 496 |
+
``` cpp
|
| 497 |
+
struct S {
|
| 498 |
+
using T = void();
|
| 499 |
+
T * p = 0; // OK: brace-or-equal-initializer
|
| 500 |
+
virtual T f = 0; // OK: pure-specifier
|
| 501 |
+
};
|
| 502 |
+
```
|
| 503 |
+
|
| 504 |
+
— *end example*]
|
| 505 |
|
| 506 |
A *brace-or-equal-initializer* shall appear only in the declaration of a
|
| 507 |
data member. (For static data members, see [[class.static.data]]; for
|
| 508 |
+
non-static data members, see [[class.base.init]] and
|
| 509 |
+
[[dcl.init.aggr]]). A *brace-or-equal-initializer* for a non-static data
|
| 510 |
+
member specifies a *default member initializer* for the member, and
|
| 511 |
+
shall not directly or indirectly cause the implicit definition of a
|
| 512 |
+
defaulted default constructor for the enclosing class or the exception
|
| 513 |
+
specification of that constructor.
|
| 514 |
|
| 515 |
+
A member shall not be declared with the `extern`
|
| 516 |
+
*storage-class-specifier*. Within a class definition, a member shall not
|
| 517 |
+
be declared with the `thread_local` *storage-class-specifier* unless
|
| 518 |
+
also declared `static`.
|
| 519 |
|
| 520 |
The *decl-specifier-seq* may be omitted in constructor, destructor, and
|
| 521 |
conversion function declarations only; when declaring another kind of
|
| 522 |
member the *decl-specifier-seq* shall contain a *type-specifier* that is
|
| 523 |
not a *cv-qualifier*. The *member-declarator-list* can be omitted only
|
| 524 |
after a *class-specifier* or an *enum-specifier* or in a `friend`
|
| 525 |
declaration ([[class.friend]]). A *pure-specifier* shall be used only
|
| 526 |
+
in the declaration of a virtual function ([[class.virtual]]) that is
|
| 527 |
+
not a `friend` declaration.
|
| 528 |
|
| 529 |
The optional *attribute-specifier-seq* in a *member-declaration*
|
| 530 |
appertains to each of the entities declared by the *member-declarator*s;
|
| 531 |
it shall not appear if the optional *member-declarator-list* is omitted.
|
| 532 |
|
| 533 |
A *virt-specifier-seq* shall contain at most one of each
|
| 534 |
*virt-specifier*. A *virt-specifier-seq* shall appear only in the
|
| 535 |
declaration of a virtual member function ([[class.virtual]]).
|
| 536 |
|
| 537 |
+
Non-static data members shall not have incomplete types. In particular,
|
| 538 |
+
a class `C` shall not contain a non-static member of class `C`, but it
|
| 539 |
+
can contain a pointer or reference to an object of class `C`.
|
|
|
|
| 540 |
|
| 541 |
+
[*Note 4*: See [[expr.prim]] for restrictions on the use of non-static
|
| 542 |
+
data members and non-static member functions. — *end note*]
|
| 543 |
|
| 544 |
+
[*Note 5*: The type of a non-static member function is an ordinary
|
| 545 |
+
function type, and the type of a non-static data member is an ordinary
|
| 546 |
+
object type. There are no special member function types or data member
|
| 547 |
+
types. — *end note*]
|
| 548 |
+
|
| 549 |
+
[*Example 2*:
|
| 550 |
|
| 551 |
A simple example of a class definition is
|
| 552 |
|
| 553 |
``` cpp
|
| 554 |
struct tnode {
|
|
|
|
| 571 |
these declarations, `sp->count` refers to the `count` member of the
|
| 572 |
object to which `sp` points; `s.left` refers to the `left` subtree
|
| 573 |
pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
| 574 |
character of the `tword` member of the `right` subtree of `s`.
|
| 575 |
|
| 576 |
+
— *end example*]
|
| 577 |
+
|
| 578 |
+
Non-static data members of a (non-union) class with the same access
|
| 579 |
control (Clause [[class.access]]) are allocated so that later members
|
| 580 |
have higher addresses within a class object. The order of allocation of
|
| 581 |
non-static data members with different access control is unspecified
|
| 582 |
(Clause [[class.access]]). Implementation alignment requirements might
|
| 583 |
cause two adjacent members not to be allocated immediately after each
|
|
|
|
| 586 |
|
| 587 |
If `T` is the name of a class, then each of the following shall have a
|
| 588 |
name different from `T`:
|
| 589 |
|
| 590 |
- every static data member of class `T`;
|
| 591 |
+
- every member function of class `T` \[*Note 6*: This restriction does
|
| 592 |
+
not apply to constructors, which do not have names (
|
| 593 |
+
[[class.ctor]]) — *end note*] ;
|
| 594 |
- every member of class `T` that is itself a type;
|
| 595 |
+
- every member template of class `T`;
|
| 596 |
- every enumerator of every member of class `T` that is an unscoped
|
| 597 |
enumerated type; and
|
| 598 |
- every member of every anonymous union that is a member of class `T`.
|
| 599 |
|
| 600 |
In addition, if class `T` has a user-declared constructor (
|
| 601 |
[[class.ctor]]), every non-static data member of class `T` shall have a
|
| 602 |
name different from `T`.
|
| 603 |
|
| 604 |
+
The *common initial sequence* of two standard-layout struct (Clause
|
| 605 |
+
[[class]]) types is the longest sequence of non-static data members and
|
| 606 |
+
bit-fields in declaration order, starting with the first such entity in
|
| 607 |
+
each of the structs, such that corresponding entities have
|
| 608 |
+
layout-compatible types and either neither entity is a bit-field or both
|
| 609 |
+
are bit-fields with the same width.
|
| 610 |
+
|
| 611 |
+
[*Example 3*:
|
| 612 |
+
|
| 613 |
+
``` cpp
|
| 614 |
+
struct A { int a; char b; };
|
| 615 |
+
struct B { const int b1; volatile char b2; };
|
| 616 |
+
struct C { int c; unsigned : 0; char b; };
|
| 617 |
+
struct D { int d; char b : 4; };
|
| 618 |
+
struct E { unsigned int e; char b; };
|
| 619 |
+
```
|
| 620 |
+
|
| 621 |
+
The common initial sequence of `A` and `B` comprises all members of
|
| 622 |
+
either class. The common initial sequence of `A` and `C` and of `A` and
|
| 623 |
+
`D` comprises the first member in each case. The common initial sequence
|
| 624 |
+
of `A` and `E` is empty.
|
| 625 |
+
|
| 626 |
+
— *end example*]
|
| 627 |
+
|
| 628 |
Two standard-layout struct (Clause [[class]]) types are
|
| 629 |
+
*layout-compatible classes* if their common initial sequence comprises
|
| 630 |
+
all members and bit-fields of both classes ([[basic.types]]).
|
|
|
|
| 631 |
|
| 632 |
+
Two standard-layout unions are layout-compatible if they have the same
|
| 633 |
+
number of non-static data members and corresponding non-static data
|
| 634 |
+
members (in any order) have layout-compatible types ([[basic.types]]).
|
|
|
|
| 635 |
|
| 636 |
+
In a standard-layout union with an active member ([[class.union]]) of
|
| 637 |
+
struct type `T1`, it is permitted to read a non-static data member `m`
|
| 638 |
+
of another union member of struct type `T2` provided `m` is part of the
|
| 639 |
+
common initial sequence of `T1` and `T2`; the behavior is as if the
|
| 640 |
+
corresponding member of `T1` were nominated.
|
| 641 |
+
|
| 642 |
+
[*Example 4*:
|
| 643 |
+
|
| 644 |
+
``` cpp
|
| 645 |
+
struct T1 { int a, b; };
|
| 646 |
+
struct T2 { int c; double d; };
|
| 647 |
+
union U { T1 t1; T2 t2; };
|
| 648 |
+
int f() {
|
| 649 |
+
U u = { { 1, 2 } }; // active member is t1
|
| 650 |
+
return u.t2.c; // OK, as if u.t1.a were nominated
|
| 651 |
+
}
|
| 652 |
+
```
|
| 653 |
+
|
| 654 |
+
— *end example*]
|
| 655 |
+
|
| 656 |
+
[*Note 7*: Reading a volatile object through a non-volatile glvalue has
|
| 657 |
+
undefined behavior ([[dcl.type.cv]]). — *end note*]
|
| 658 |
|
| 659 |
If a standard-layout class object has any non-static data members, its
|
| 660 |
address is the same as the address of its first non-static data member.
|
| 661 |
Otherwise, its address is the same as the address of its first base
|
| 662 |
+
class subobject (if any).
|
|
|
|
|
|
|
| 663 |
|
| 664 |
+
[*Note 8*: There might therefore be unnamed padding within a
|
| 665 |
+
standard-layout struct object, but not at its beginning, as necessary to
|
| 666 |
+
achieve appropriate alignment. — *end note*]
|
| 667 |
|
| 668 |
+
[*Note 9*: The object and its first subobject are
|
| 669 |
+
pointer-interconvertible ([[basic.compound]],
|
| 670 |
+
[[expr.static.cast]]). — *end note*]
|
| 671 |
+
|
| 672 |
+
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
|
|
|
| 673 |
|
| 674 |
A member function may be defined ([[dcl.fct.def]]) in its class
|
| 675 |
definition, in which case it is an *inline* member function (
|
| 676 |
+
[[dcl.inline]]), or it may be defined outside of its class definition if
|
| 677 |
+
it has already been declared but not defined in its class definition. A
|
| 678 |
+
member function definition that appears outside of the class definition
|
| 679 |
+
shall appear in a namespace scope enclosing the class definition. Except
|
| 680 |
+
for member function definitions that appear outside of a class
|
| 681 |
+
definition, and except for explicit specializations of member functions
|
| 682 |
+
of class templates and member function templates ([[temp.spec]])
|
| 683 |
+
appearing outside of the class definition, a member function shall not
|
| 684 |
+
be redeclared.
|
| 685 |
|
| 686 |
+
An inline member function (whether static or non-static) may also be
|
| 687 |
defined outside of its class definition provided either its declaration
|
| 688 |
in the class definition or its definition outside of the class
|
| 689 |
+
definition declares the function as `inline` or `constexpr`.
|
|
|
|
|
|
|
| 690 |
|
| 691 |
+
[*Note 1*: Member functions of a class in namespace scope have the
|
| 692 |
+
linkage of that class. Member functions of a local class (
|
| 693 |
+
[[class.local]]) have no linkage. See [[basic.link]]. — *end note*]
|
| 694 |
+
|
| 695 |
+
[*Note 2*: There can be at most one definition of a non-inline member
|
| 696 |
+
function in a program. There may be more than one `inline` member
|
| 697 |
+
function definition in a program. See [[basic.def.odr]] and
|
| 698 |
+
[[dcl.inline]]. — *end note*]
|
| 699 |
|
| 700 |
If the definition of a member function is lexically outside its class
|
| 701 |
definition, the member function name shall be qualified by its class
|
| 702 |
+
name using the `::` operator.
|
| 703 |
+
|
| 704 |
+
[*Note 3*: A name used in a member function definition (that is, in the
|
| 705 |
+
*parameter-declaration-clause* including the default arguments (
|
| 706 |
+
[[dcl.fct.default]]) or in the member function body) is looked up as
|
| 707 |
+
described in [[basic.lookup]]. — *end note*]
|
| 708 |
+
|
| 709 |
+
[*Example 1*:
|
| 710 |
|
| 711 |
``` cpp
|
| 712 |
struct X {
|
| 713 |
typedef int T;
|
| 714 |
static T count;
|
|
|
|
| 722 |
and in the scope of class `X`. In the function definition, the parameter
|
| 723 |
type `T` refers to the typedef member `T` declared in class `X` and the
|
| 724 |
default argument `count` refers to the static data member `count`
|
| 725 |
declared in class `X`.
|
| 726 |
|
| 727 |
+
— *end example*]
|
| 728 |
+
|
| 729 |
+
[*Note 4*: A `static` local variable or local type in a member function
|
| 730 |
+
always refers to the same entity, whether or not the member function is
|
| 731 |
+
`inline`. — *end note*]
|
| 732 |
|
| 733 |
Previously declared member functions may be mentioned in `friend`
|
| 734 |
declarations.
|
| 735 |
|
| 736 |
Member functions of a local class shall be defined inline in their class
|
| 737 |
definition, if they are defined at all.
|
| 738 |
|
| 739 |
+
[*Note 5*:
|
| 740 |
+
|
| 741 |
A member function can be declared (but not defined) using a typedef for
|
| 742 |
a function type. The resulting member function has exactly the same type
|
| 743 |
as it would have if the function declarator were provided explicitly,
|
| 744 |
see [[dcl.fct]]. For example,
|
| 745 |
|
| 746 |
``` cpp
|
| 747 |
+
typedef void fv();
|
| 748 |
+
typedef void fvc() const;
|
| 749 |
struct S {
|
| 750 |
+
fv memfunc1; // equivalent to: void memfunc1();
|
| 751 |
void memfunc2();
|
| 752 |
+
fvc memfunc3; // equivalent to: void memfunc3() const;
|
| 753 |
};
|
| 754 |
fv S::* pmfv1 = &S::memfunc1;
|
| 755 |
fv S::* pmfv2 = &S::memfunc2;
|
| 756 |
fvc S::* pmfv3 = &S::memfunc3;
|
| 757 |
```
|
| 758 |
|
| 759 |
Also see [[temp.arg]].
|
| 760 |
|
| 761 |
+
— *end note*]
|
| 762 |
|
| 763 |
+
### Non-static member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
|
| 764 |
+
|
| 765 |
+
A non-static member function may be called for an object of its class
|
| 766 |
type, or for an object of a class derived (Clause [[class.derived]])
|
| 767 |
from its class type, using the class member access syntax (
|
| 768 |
[[expr.ref]], [[over.match.call]]). A non-static member function may
|
| 769 |
also be called directly using the function call syntax ([[expr.call]],
|
| 770 |
[[over.match.call]]) from within the body of a member function of its
|
|
|
|
| 775 |
undefined.
|
| 776 |
|
| 777 |
When an *id-expression* ([[expr.prim]]) that is not part of a class
|
| 778 |
member access syntax ([[expr.ref]]) and not used to form a pointer to
|
| 779 |
member ([[expr.unary.op]]) is used in a member of class `X` in a
|
| 780 |
+
context where `this` can be used ([[expr.prim.this]]), if name lookup (
|
| 781 |
+
[[basic.lookup]]) resolves the name in the *id-expression* to a
|
| 782 |
non-static non-type member of some class `C`, and if either the
|
| 783 |
*id-expression* is potentially evaluated or `C` is `X` or a base class
|
| 784 |
of `X`, the *id-expression* is transformed into a class member access
|
| 785 |
expression ([[expr.ref]]) using `(*this)` ([[class.this]]) as the
|
| 786 |
+
*postfix-expression* to the left of the `.` operator.
|
| 787 |
+
|
| 788 |
+
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 789 |
+
access expression is ill-formed. — *end note*]
|
| 790 |
+
|
| 791 |
+
Similarly during name lookup, when an *unqualified-id* ([[expr.prim]])
|
| 792 |
+
used in the definition of a member function for class `X` resolves to a
|
| 793 |
+
static member, an enumerator or a nested type of class `X` or of a base
|
| 794 |
+
class of `X`, the *unqualified-id* is transformed into a
|
| 795 |
+
*qualified-id* ([[expr.prim]]) in which the *nested-name-specifier*
|
| 796 |
+
names the class of the member function. These transformations do not
|
| 797 |
+
apply in the template definition context ([[temp.dep.type]]).
|
| 798 |
+
|
| 799 |
+
[*Example 1*:
|
| 800 |
|
| 801 |
``` cpp
|
| 802 |
struct tnode {
|
| 803 |
char tword[20];
|
| 804 |
int count;
|
|
|
|
| 827 |
which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
| 828 |
`tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
|
| 829 |
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 830 |
not members of the class `tnode` and should be declared elsewhere.[^4]
|
| 831 |
|
| 832 |
+
— *end example*]
|
| 833 |
+
|
| 834 |
A non-static member function may be declared `const`, `volatile`, or
|
| 835 |
+
`const` `volatile`. These *cv-qualifier*s affect the type of the `this`
|
| 836 |
pointer ([[class.this]]). They also affect the function type (
|
| 837 |
[[dcl.fct]]) of the member function; a member function declared `const`
|
| 838 |
is a *const* member function, a member function declared `volatile` is a
|
| 839 |
*volatile* member function and a member function declared `const`
|
| 840 |
`volatile` is a *const volatile* member function.
|
| 841 |
|
| 842 |
+
[*Example 2*:
|
| 843 |
+
|
| 844 |
``` cpp
|
| 845 |
struct X {
|
| 846 |
void g() const;
|
| 847 |
void h() const volatile;
|
| 848 |
};
|
| 849 |
```
|
| 850 |
|
| 851 |
`X::g` is a `const` member function and `X::h` is a `const` `volatile`
|
| 852 |
member function.
|
| 853 |
|
| 854 |
+
— *end example*]
|
| 855 |
+
|
| 856 |
A non-static member function may be declared with a *ref-qualifier* (
|
| 857 |
[[dcl.fct]]); see [[over.match.funcs]].
|
| 858 |
|
| 859 |
A non-static member function may be declared *virtual* (
|
| 860 |
[[class.virtual]]) or *pure virtual* ([[class.abstract]]).
|
| 861 |
|
| 862 |
+
#### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 863 |
|
| 864 |
In the body of a non-static ([[class.mfct]]) member function, the
|
| 865 |
keyword `this` is a prvalue expression whose value is the address of the
|
| 866 |
object for which the function is called. The type of `this` in a member
|
| 867 |
function of a class `X` is `X*`. If the member function is declared
|
| 868 |
`const`, the type of `this` is `const` `X*`, if the member function is
|
| 869 |
declared `volatile`, the type of `this` is `volatile` `X*`, and if the
|
| 870 |
member function is declared `const` `volatile`, the type of `this` is
|
| 871 |
+
`const` `volatile` `X*`.
|
| 872 |
+
|
| 873 |
+
[*Note 1*: Thus in a `const` member function, the object for which the
|
| 874 |
+
function is called is accessed through a `const` access
|
| 875 |
+
path. — *end note*]
|
| 876 |
+
|
| 877 |
+
[*Example 1*:
|
| 878 |
|
| 879 |
``` cpp
|
| 880 |
struct s {
|
| 881 |
int a;
|
| 882 |
int f() const;
|
|
|
|
| 890 |
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 891 |
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 892 |
in a `const` member function because `this` is a pointer to `const`;
|
| 893 |
that is, `*this` has `const` type.
|
| 894 |
|
| 895 |
+
— *end example*]
|
| 896 |
+
|
| 897 |
Similarly, `volatile` semantics ([[dcl.type.cv]]) apply in `volatile`
|
| 898 |
member functions when accessing the object and its non-static data
|
| 899 |
members.
|
| 900 |
|
| 901 |
+
A cv-qualified member function can be called on an object-expression (
|
| 902 |
[[expr.ref]]) only if the object-expression is as cv-qualified or
|
| 903 |
less-cv-qualified than the member function.
|
| 904 |
|
| 905 |
+
[*Example 2*:
|
| 906 |
+
|
| 907 |
``` cpp
|
| 908 |
void k(s& x, const s& y) {
|
| 909 |
x.f();
|
| 910 |
x.g();
|
| 911 |
y.f();
|
|
|
|
| 915 |
|
| 916 |
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 917 |
non-`const` member function, that is, `s::g()` is less-qualified than
|
| 918 |
the object-expression `y`.
|
| 919 |
|
| 920 |
+
— *end example*]
|
| 921 |
+
|
| 922 |
Constructors ([[class.ctor]]) and destructors ([[class.dtor]]) shall
|
| 923 |
+
not be declared `const`, `volatile` or `const` `volatile`.
|
|
|
|
|
|
|
| 924 |
|
| 925 |
+
[*Note 2*: However, these functions can be invoked to create and
|
| 926 |
+
destroy objects with cv-qualified types, see [[class.ctor]] and
|
| 927 |
+
[[class.dtor]]. — *end note*]
|
| 928 |
|
| 929 |
+
### Static members <a id="class.static">[[class.static]]</a>
|
|
|
|
| 930 |
|
| 931 |
+
A static member `s` of class `X` may be referred to using the
|
| 932 |
*qualified-id* expression `X::s`; it is not necessary to use the class
|
| 933 |
+
member access syntax ([[expr.ref]]) to refer to a static member. A
|
| 934 |
+
static member may be referred to using the class member access syntax,
|
| 935 |
in which case the object expression is evaluated.
|
| 936 |
|
| 937 |
+
[*Example 1*:
|
| 938 |
+
|
| 939 |
``` cpp
|
| 940 |
struct process {
|
| 941 |
static void reschedule();
|
| 942 |
};
|
| 943 |
process& g();
|
|
|
|
| 946 |
process::reschedule(); // OK: no object necessary
|
| 947 |
g().reschedule(); // g() is called
|
| 948 |
}
|
| 949 |
```
|
| 950 |
|
| 951 |
+
— *end example*]
|
| 952 |
+
|
| 953 |
+
A static member may be referred to directly in the scope of its class or
|
| 954 |
+
in the scope of a class derived (Clause [[class.derived]]) from its
|
| 955 |
+
class; in this case, the static member is referred to as if a
|
| 956 |
*qualified-id* expression was used, with the *nested-name-specifier* of
|
| 957 |
the *qualified-id* naming the class scope from which the static member
|
| 958 |
is referenced.
|
| 959 |
|
| 960 |
+
[*Example 2*:
|
| 961 |
+
|
| 962 |
``` cpp
|
| 963 |
int g();
|
| 964 |
struct X {
|
| 965 |
static int g();
|
| 966 |
};
|
|
|
|
| 968 |
static int i;
|
| 969 |
};
|
| 970 |
int Y::i = g(); // equivalent to Y::g();
|
| 971 |
```
|
| 972 |
|
| 973 |
+
— *end example*]
|
| 974 |
+
|
| 975 |
If an *unqualified-id* ([[expr.prim]]) is used in the definition of a
|
| 976 |
+
static member following the member’s *declarator-id*, and name lookup (
|
| 977 |
+
[[basic.lookup.unqual]]) finds that the *unqualified-id* refers to a
|
| 978 |
+
static member, enumerator, or nested type of the member’s class (or of a
|
| 979 |
+
base class of the member’s class), the *unqualified-id* is transformed
|
| 980 |
+
into a *qualified-id* expression in which the *nested-name-specifier*
|
| 981 |
+
names the class scope from which the member is referenced.
|
| 982 |
+
|
| 983 |
+
[*Note 1*: See [[expr.prim]] for restrictions on the use of non-static
|
| 984 |
+
data members and non-static member functions. — *end note*]
|
| 985 |
|
| 986 |
Static members obey the usual class member access rules (Clause
|
| 987 |
[[class.access]]). When used in the declaration of a class member, the
|
| 988 |
`static` specifier shall only be used in the member declarations that
|
| 989 |
+
appear within the *member-specification* of the class definition.
|
|
|
|
|
|
|
| 990 |
|
| 991 |
+
[*Note 2*: It cannot be specified in member declarations that appear in
|
| 992 |
+
namespace scope. — *end note*]
|
| 993 |
|
| 994 |
+
#### Static member functions <a id="class.static.mfct">[[class.static.mfct]]</a>
|
|
|
|
| 995 |
|
| 996 |
+
[*Note 1*: The rules described in [[class.mfct]] apply to static
|
| 997 |
+
member functions. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 998 |
|
| 999 |
+
[*Note 2*: A static member function does not have a `this` pointer (
|
| 1000 |
+
[[class.this]]). — *end note*]
|
| 1001 |
|
| 1002 |
+
A static member function shall not be `virtual`. There shall not be a
|
| 1003 |
+
static and a non-static member function with the same name and the same
|
| 1004 |
+
parameter types ([[over.load]]). A static member function shall not be
|
| 1005 |
+
declared `const`, `volatile`, or `const volatile`.
|
| 1006 |
+
|
| 1007 |
+
#### Static data members <a id="class.static.data">[[class.static.data]]</a>
|
| 1008 |
+
|
| 1009 |
+
A static data member is not part of the subobjects of a class. If a
|
| 1010 |
+
static data member is declared `thread_local` there is one copy of the
|
| 1011 |
+
member per thread. If a static data member is not declared
|
| 1012 |
`thread_local` there is one copy of the data member that is shared by
|
| 1013 |
all the objects of the class.
|
| 1014 |
|
| 1015 |
+
The declaration of a non-inline static data member in its class
|
| 1016 |
+
definition is not a definition and may be of an incomplete type other
|
| 1017 |
+
than cv `void`. The definition for a static data member that is not
|
| 1018 |
+
defined inline in the class definition shall appear in a namespace scope
|
| 1019 |
+
enclosing the member’s class definition. In the definition at namespace
|
| 1020 |
+
scope, the name of the static data member shall be qualified by its
|
| 1021 |
+
class name using the `::` operator. The *initializer* expression in the
|
| 1022 |
+
definition of a static data member is in the scope of its class (
|
| 1023 |
+
[[basic.scope.class]]).
|
| 1024 |
+
|
| 1025 |
+
[*Example 1*:
|
| 1026 |
|
| 1027 |
``` cpp
|
| 1028 |
class process {
|
| 1029 |
static process* run_chain;
|
| 1030 |
static process* running;
|
|
|
|
| 1032 |
|
| 1033 |
process* process::running = get_main();
|
| 1034 |
process* process::run_chain = running;
|
| 1035 |
```
|
| 1036 |
|
| 1037 |
+
The static data member `run_chain` of class `process` is defined in
|
| 1038 |
global scope; the notation `process::run_chain` specifies that the
|
| 1039 |
member `run_chain` is a member of class `process` and in the scope of
|
| 1040 |
+
class `process`. In the static data member definition, the *initializer*
|
| 1041 |
+
expression refers to the static data member `running` of class
|
| 1042 |
+
`process`.
|
| 1043 |
+
|
| 1044 |
+
— *end example*]
|
| 1045 |
+
|
| 1046 |
+
[*Note 1*:
|
| 1047 |
+
|
| 1048 |
+
Once the static data member has been defined, it exists even if no
|
| 1049 |
+
objects of its class have been created.
|
| 1050 |
+
|
| 1051 |
+
[*Example 2*:
|
| 1052 |
+
|
| 1053 |
+
In the example above, `run_chain` and `running` exist even if no objects
|
| 1054 |
+
of class `process` are created by the program.
|
| 1055 |
+
|
| 1056 |
+
— *end example*]
|
| 1057 |
+
|
| 1058 |
+
— *end note*]
|
| 1059 |
+
|
| 1060 |
+
If a non-volatile non-inline `const` static data member is of integral
|
| 1061 |
+
or enumeration type, its declaration in the class definition can specify
|
| 1062 |
+
a *brace-or-equal-initializer* in which every *initializer-clause* that
|
| 1063 |
+
is an *assignment-expression* is a constant expression (
|
| 1064 |
+
[[expr.const]]). The member shall still be defined in a namespace scope
|
| 1065 |
+
if it is odr-used ([[basic.def.odr]]) in the program and the namespace
|
| 1066 |
+
scope definition shall not contain an *initializer*. An inline static
|
| 1067 |
+
data member may be defined in the class definition and may specify a
|
| 1068 |
+
*brace-or-equal-initializer*. If the member is declared with the
|
| 1069 |
+
`constexpr` specifier, it may be redeclared in namespace scope with no
|
| 1070 |
+
initializer (this usage is deprecated; see [[depr.static_constexpr]]).
|
| 1071 |
+
Declarations of other static data members shall not specify a
|
| 1072 |
+
*brace-or-equal-initializer*.
|
| 1073 |
+
|
| 1074 |
+
[*Note 2*: There shall be exactly one definition of a static data
|
| 1075 |
+
member that is odr-used ([[basic.def.odr]]) in a program; no diagnostic
|
| 1076 |
+
is required. — *end note*]
|
| 1077 |
+
|
| 1078 |
Unnamed classes and classes contained directly or indirectly within
|
| 1079 |
+
unnamed classes shall not contain static data members.
|
| 1080 |
|
| 1081 |
+
[*Note 3*: Static data members of a class in namespace scope have the
|
| 1082 |
+
linkage of that class ([[basic.link]]). A local class cannot have
|
| 1083 |
+
static data members ([[class.local]]). — *end note*]
|
| 1084 |
|
| 1085 |
+
Static data members are initialized and destroyed exactly like non-local
|
| 1086 |
+
variables ([[basic.start.static]], [[basic.start.dynamic]],
|
| 1087 |
+
[[basic.start.term]]).
|
| 1088 |
|
| 1089 |
+
A static data member shall not be `mutable` ([[dcl.stc]]).
|
| 1090 |
|
| 1091 |
+
### Bit-fields <a id="class.bit">[[class.bit]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1092 |
|
| 1093 |
A *member-declarator* of the form
|
| 1094 |
|
| 1095 |
specifies a bit-field; its length is set off from the bit-field name by
|
| 1096 |
a colon. The optional *attribute-specifier-seq* appertains to the entity
|
|
|
|
| 1102 |
such cases the extra bits are used as padding bits and do not
|
| 1103 |
participate in the value representation ([[basic.types]]) of the
|
| 1104 |
bit-field. Allocation of bit-fields within a class object is
|
| 1105 |
*implementation-defined*. Alignment of bit-fields is
|
| 1106 |
*implementation-defined*. Bit-fields are packed into some addressable
|
| 1107 |
+
allocation unit.
|
| 1108 |
+
|
| 1109 |
+
[*Note 1*: Bit-fields straddle allocation units on some machines and
|
| 1110 |
+
not on others. Bit-fields are assigned right-to-left on some machines,
|
| 1111 |
+
left-to-right on others. — *end note*]
|
| 1112 |
|
| 1113 |
A declaration for a bit-field that omits the *identifier* declares an
|
| 1114 |
+
*unnamed bit-field*. Unnamed bit-fields are not members and cannot be
|
| 1115 |
+
initialized.
|
| 1116 |
+
|
| 1117 |
+
[*Note 2*: An unnamed bit-field is useful for padding to conform to
|
| 1118 |
+
externally-imposed layouts. — *end note*]
|
| 1119 |
+
|
| 1120 |
+
As a special case, an unnamed bit-field with a width of zero specifies
|
| 1121 |
+
alignment of the next bit-field at an allocation unit boundary. Only
|
| 1122 |
+
when declaring an unnamed bit-field may the value of the
|
| 1123 |
+
*constant-expression* be equal to zero.
|
| 1124 |
|
| 1125 |
A bit-field shall not be a static member. A bit-field shall have
|
| 1126 |
integral or enumeration type ([[basic.fundamental]]). A `bool` value
|
| 1127 |
can successfully be stored in a bit-field of any nonzero size. The
|
| 1128 |
address-of operator `&` shall not be applied to a bit-field, so there
|
| 1129 |
are no pointers to bit-fields. A non-const reference shall not be bound
|
| 1130 |
+
to a bit-field ([[dcl.init.ref]]).
|
| 1131 |
+
|
| 1132 |
+
[*Note 3*: If the initializer for a reference of type `const` `T&` is
|
| 1133 |
+
an lvalue that refers to a bit-field, the reference is bound to a
|
| 1134 |
+
temporary initialized to hold the value of the bit-field; the reference
|
| 1135 |
+
is not bound to the bit-field directly. See
|
| 1136 |
+
[[dcl.init.ref]]. — *end note*]
|
| 1137 |
|
| 1138 |
If the value `true` or `false` is stored into a bit-field of type `bool`
|
| 1139 |
of any size (including a one bit bit-field), the original `bool` value
|
| 1140 |
and the value of the bit-field shall compare equal. If the value of an
|
| 1141 |
enumerator is stored into a bit-field of the same enumeration type and
|
| 1142 |
the number of bits in the bit-field is large enough to hold all the
|
| 1143 |
values of that enumeration type ([[dcl.enum]]), the original enumerator
|
| 1144 |
value and the value of the bit-field shall compare equal.
|
| 1145 |
|
| 1146 |
+
[*Example 1*:
|
| 1147 |
+
|
| 1148 |
``` cpp
|
| 1149 |
enum BOOL { FALSE=0, TRUE=1 };
|
| 1150 |
struct A {
|
| 1151 |
BOOL b:1;
|
| 1152 |
};
|
| 1153 |
A a;
|
| 1154 |
void f() {
|
| 1155 |
a.b = TRUE;
|
| 1156 |
if (a.b == TRUE) // yields true
|
| 1157 |
+
{ ... }
|
| 1158 |
}
|
| 1159 |
```
|
| 1160 |
|
| 1161 |
+
— *end example*]
|
| 1162 |
+
|
| 1163 |
+
### Nested class declarations <a id="class.nest">[[class.nest]]</a>
|
| 1164 |
|
| 1165 |
A class can be declared within another class. A class declared within
|
| 1166 |
another is called a *nested* class. The name of a nested class is local
|
| 1167 |
to its enclosing class. The nested class is in the scope of its
|
| 1168 |
+
enclosing class.
|
| 1169 |
+
|
| 1170 |
+
[*Note 1*: See [[expr.prim]] for restrictions on the use of non-static
|
| 1171 |
+
data members and non-static member functions. — *end note*]
|
| 1172 |
+
|
| 1173 |
+
[*Example 1*:
|
| 1174 |
|
| 1175 |
``` cpp
|
| 1176 |
int x;
|
| 1177 |
int y;
|
| 1178 |
|
|
|
|
| 1195 |
};
|
| 1196 |
|
| 1197 |
inner* p = 0; // error: inner not in scope
|
| 1198 |
```
|
| 1199 |
|
| 1200 |
+
— *end example*]
|
| 1201 |
+
|
| 1202 |
Member functions and static data members of a nested class can be
|
| 1203 |
defined in a namespace scope enclosing the definition of their class.
|
| 1204 |
|
| 1205 |
+
[*Example 2*:
|
| 1206 |
+
|
| 1207 |
``` cpp
|
| 1208 |
struct enclose {
|
| 1209 |
struct inner {
|
| 1210 |
static int x;
|
| 1211 |
void f(int i);
|
| 1212 |
};
|
| 1213 |
};
|
| 1214 |
|
| 1215 |
int enclose::inner::x = 1;
|
| 1216 |
|
| 1217 |
+
void enclose::inner::f(int i) { ... }
|
| 1218 |
```
|
| 1219 |
|
| 1220 |
+
— *end example*]
|
| 1221 |
+
|
| 1222 |
If class `X` is defined in a namespace scope, a nested class `Y` may be
|
| 1223 |
declared in class `X` and later defined in the definition of class `X`
|
| 1224 |
or be later defined in a namespace scope enclosing the definition of
|
| 1225 |
class `X`.
|
| 1226 |
|
| 1227 |
+
[*Example 3*:
|
| 1228 |
+
|
| 1229 |
``` cpp
|
| 1230 |
class E {
|
| 1231 |
class I1; // forward declaration of nested class
|
| 1232 |
class I2;
|
| 1233 |
class I1 { }; // definition of nested class
|
| 1234 |
};
|
| 1235 |
class E::I2 { }; // definition of nested class
|
| 1236 |
```
|
| 1237 |
|
| 1238 |
+
— *end example*]
|
| 1239 |
+
|
| 1240 |
Like a member function, a friend function ([[class.friend]]) defined
|
| 1241 |
within a nested class is in the lexical scope of that class; it obeys
|
| 1242 |
the same rules for name binding as a static member function of that
|
| 1243 |
class ([[class.static]]), but it has no special access rights to
|
| 1244 |
members of an enclosing class.
|
| 1245 |
|
| 1246 |
+
### Nested type names <a id="class.nested.type">[[class.nested.type]]</a>
|
| 1247 |
+
|
| 1248 |
+
Type names obey exactly the same scope rules as other names. In
|
| 1249 |
+
particular, type names defined within a class definition cannot be used
|
| 1250 |
+
outside their class without qualification.
|
| 1251 |
+
|
| 1252 |
+
[*Example 1*:
|
| 1253 |
+
|
| 1254 |
+
``` cpp
|
| 1255 |
+
struct X {
|
| 1256 |
+
typedef int I;
|
| 1257 |
+
class Y { ... };
|
| 1258 |
+
I a;
|
| 1259 |
+
};
|
| 1260 |
+
|
| 1261 |
+
I b; // error
|
| 1262 |
+
Y c; // error
|
| 1263 |
+
X::Y d; // OK
|
| 1264 |
+
X::I e; // OK
|
| 1265 |
+
```
|
| 1266 |
+
|
| 1267 |
+
— *end example*]
|
| 1268 |
+
|
| 1269 |
+
## Unions <a id="class.union">[[class.union]]</a>
|
| 1270 |
+
|
| 1271 |
+
In a union, a non-static data member is *active* if its name refers to
|
| 1272 |
+
an object whose lifetime has begun and has not ended ([[basic.life]]).
|
| 1273 |
+
At most one of the non-static data members of an object of union type
|
| 1274 |
+
can be active at any time, that is, the value of at most one of the
|
| 1275 |
+
non-static data members can be stored in a union at any time.
|
| 1276 |
+
|
| 1277 |
+
[*Note 1*: One special guarantee is made in order to simplify the use
|
| 1278 |
+
of unions: If a standard-layout union contains several standard-layout
|
| 1279 |
+
structs that share a common initial sequence ([[class.mem]]), and if a
|
| 1280 |
+
non-static data member of an object of this standard-layout union type
|
| 1281 |
+
is active and is one of the standard-layout structs, it is permitted to
|
| 1282 |
+
inspect the common initial sequence of any of the standard-layout struct
|
| 1283 |
+
members; see [[class.mem]]. — *end note*]
|
| 1284 |
+
|
| 1285 |
+
The size of a union is sufficient to contain the largest of its
|
| 1286 |
+
non-static data members. Each non-static data member is allocated as if
|
| 1287 |
+
it were the sole member of a struct.
|
| 1288 |
+
|
| 1289 |
+
[*Note 2*: A union object and its non-static data members are
|
| 1290 |
+
pointer-interconvertible ([[basic.compound]], [[expr.static.cast]]). As
|
| 1291 |
+
a consequence, all non-static data members of a union object have the
|
| 1292 |
+
same address. — *end note*]
|
| 1293 |
+
|
| 1294 |
+
A union can have member functions (including constructors and
|
| 1295 |
+
destructors), but it shall not have virtual ([[class.virtual]])
|
| 1296 |
+
functions. A union shall not have base classes. A union shall not be
|
| 1297 |
+
used as a base class. If a union contains a non-static data member of
|
| 1298 |
+
reference type the program is ill-formed.
|
| 1299 |
+
|
| 1300 |
+
[*Note 3*: Absent default member initializers ([[class.mem]]), if any
|
| 1301 |
+
non-static data member of a union has a non-trivial default
|
| 1302 |
+
constructor ([[class.ctor]]), copy constructor ([[class.copy]]), move
|
| 1303 |
+
constructor ([[class.copy]]), copy assignment operator (
|
| 1304 |
+
[[class.copy]]), move assignment operator ([[class.copy]]), or
|
| 1305 |
+
destructor ([[class.dtor]]), the corresponding member function of the
|
| 1306 |
+
union must be user-provided or it will be implicitly deleted (
|
| 1307 |
+
[[dcl.fct.def.delete]]) for the union. — *end note*]
|
| 1308 |
+
|
| 1309 |
+
[*Example 1*:
|
| 1310 |
+
|
| 1311 |
+
Consider the following union:
|
| 1312 |
+
|
| 1313 |
+
``` cpp
|
| 1314 |
+
union U {
|
| 1315 |
+
int i;
|
| 1316 |
+
float f;
|
| 1317 |
+
std::string s;
|
| 1318 |
+
};
|
| 1319 |
+
```
|
| 1320 |
+
|
| 1321 |
+
Since `std::string` ([[string.classes]]) declares non-trivial versions
|
| 1322 |
+
of all of the special member functions, `U` will have an implicitly
|
| 1323 |
+
deleted default constructor, copy/move constructor, copy/move assignment
|
| 1324 |
+
operator, and destructor. To use `U`, some or all of these member
|
| 1325 |
+
functions must be user-provided.
|
| 1326 |
+
|
| 1327 |
+
— *end example*]
|
| 1328 |
+
|
| 1329 |
+
When the left operand of an assignment operator involves a member access
|
| 1330 |
+
expression ([[expr.ref]]) that nominates a union member, it may begin
|
| 1331 |
+
the lifetime of that union member, as described below. For an expression
|
| 1332 |
+
`E`, define the set S(E) of subexpressions of `E` as follows:
|
| 1333 |
+
|
| 1334 |
+
- If `E` is of the form `A.B`, S(E) contains the elements of S(A), and
|
| 1335 |
+
also contains `A.B` if `B` names a union member of a non-class,
|
| 1336 |
+
non-array type, or of a class type with a trivial default constructor
|
| 1337 |
+
that is not deleted, or an array of such types.
|
| 1338 |
+
- If `E` is of the form `A[B]` and is interpreted as a built-in array
|
| 1339 |
+
subscripting operator, S(E) is S(A) if `A` is of array type, S(B) if
|
| 1340 |
+
`B` is of array type, and empty otherwise.
|
| 1341 |
+
- Otherwise, S(E) is empty.
|
| 1342 |
+
|
| 1343 |
+
In an assignment expression of the form `E1 = E2` that uses either the
|
| 1344 |
+
built-in assignment operator ([[expr.ass]]) or a trivial assignment
|
| 1345 |
+
operator ([[class.copy]]), for each element `X` of S(`E1`), if
|
| 1346 |
+
modification of `X` would have undefined behavior under [[basic.life]],
|
| 1347 |
+
an object of the type of `X` is implicitly created in the nominated
|
| 1348 |
+
storage; no initialization is performed and the beginning of its
|
| 1349 |
+
lifetime is sequenced after the value computation of the left and right
|
| 1350 |
+
operands and before the assignment.
|
| 1351 |
+
|
| 1352 |
+
[*Note 4*: This ends the lifetime of the previously-active member of
|
| 1353 |
+
the union, if any ([[basic.life]]). — *end note*]
|
| 1354 |
+
|
| 1355 |
+
[*Example 2*:
|
| 1356 |
+
|
| 1357 |
+
``` cpp
|
| 1358 |
+
union A { int x; int y[4]; };
|
| 1359 |
+
struct B { A a; };
|
| 1360 |
+
union C { B b; int k; };
|
| 1361 |
+
int f() {
|
| 1362 |
+
C c; // does not start lifetime of any union member
|
| 1363 |
+
c.b.a.y[3] = 4; // OK: S(c.b.a.y[3]) contains c.b and c.b.a.y;
|
| 1364 |
+
// creates objects to hold union members c.b and c.b.a.y
|
| 1365 |
+
return c.b.a.y[3]; // OK: c.b.a.y refers to newly created object (see [basic.life])
|
| 1366 |
+
}
|
| 1367 |
+
|
| 1368 |
+
struct X { const int a; int b; };
|
| 1369 |
+
union Y { X x; int k; };
|
| 1370 |
+
void g() {
|
| 1371 |
+
Y y = { { 1, 2 } }; // OK, y.x is active union member ([class.mem])
|
| 1372 |
+
int n = y.x.a;
|
| 1373 |
+
y.k = 4; // OK: ends lifetime of y.x, y.k is active member of union
|
| 1374 |
+
y.x.b = n; // undefined behavior: y.x.b modified outside its lifetime,
|
| 1375 |
+
// S(y.x.b) is empty because X's default constructor is deleted,
|
| 1376 |
+
// so union member y.x's lifetime does not implicitly start
|
| 1377 |
+
}
|
| 1378 |
+
```
|
| 1379 |
+
|
| 1380 |
+
— *end example*]
|
| 1381 |
+
|
| 1382 |
+
[*Note 5*: In general, one must use explicit destructor calls and
|
| 1383 |
+
placement *new-expression* to change the active member of a
|
| 1384 |
+
union. — *end note*]
|
| 1385 |
+
|
| 1386 |
+
[*Example 3*:
|
| 1387 |
+
|
| 1388 |
+
Consider an object `u` of a `union` type `U` having non-static data
|
| 1389 |
+
members `m` of type `M` and `n` of type `N`. If `M` has a non-trivial
|
| 1390 |
+
destructor and `N` has a non-trivial constructor (for instance, if they
|
| 1391 |
+
declare or inherit virtual functions), the active member of `u` can be
|
| 1392 |
+
safely switched from `m` to `n` using the destructor and placement
|
| 1393 |
+
*new-expression* as follows:
|
| 1394 |
+
|
| 1395 |
+
``` cpp
|
| 1396 |
+
u.m.~M();
|
| 1397 |
+
new (&u.n) N;
|
| 1398 |
+
```
|
| 1399 |
+
|
| 1400 |
+
— *end example*]
|
| 1401 |
+
|
| 1402 |
+
### Anonymous unions <a id="class.union.anon">[[class.union.anon]]</a>
|
| 1403 |
+
|
| 1404 |
+
A union of the form
|
| 1405 |
+
|
| 1406 |
+
is called an *anonymous union*; it defines an unnamed type and an
|
| 1407 |
+
unnamed object of that type called an *anonymous union object*. Each
|
| 1408 |
+
*member-declaration* in the *member-specification* of an anonymous union
|
| 1409 |
+
shall either define a non-static data member or be a
|
| 1410 |
+
*static_assert-declaration*.
|
| 1411 |
+
|
| 1412 |
+
[*Note 1*: Nested types, anonymous unions, and functions cannot be
|
| 1413 |
+
declared within an anonymous union. — *end note*]
|
| 1414 |
+
|
| 1415 |
+
The names of the members of an anonymous union shall be distinct from
|
| 1416 |
+
the names of any other entity in the scope in which the anonymous union
|
| 1417 |
+
is declared. For the purpose of name lookup, after the anonymous union
|
| 1418 |
+
definition, the members of the anonymous union are considered to have
|
| 1419 |
+
been defined in the scope in which the anonymous union is declared.
|
| 1420 |
+
|
| 1421 |
+
[*Example 1*:
|
| 1422 |
+
|
| 1423 |
+
``` cpp
|
| 1424 |
+
void f() {
|
| 1425 |
+
union { int a; const char* p; };
|
| 1426 |
+
a = 1;
|
| 1427 |
+
p = "Jennifer";
|
| 1428 |
+
}
|
| 1429 |
+
```
|
| 1430 |
+
|
| 1431 |
+
Here `a` and `p` are used like ordinary (non-member) variables, but
|
| 1432 |
+
since they are union members they have the same address.
|
| 1433 |
+
|
| 1434 |
+
— *end example*]
|
| 1435 |
+
|
| 1436 |
+
Anonymous unions declared in a named namespace or in the global
|
| 1437 |
+
namespace shall be declared `static`. Anonymous unions declared at block
|
| 1438 |
+
scope shall be declared with any storage class allowed for a block-scope
|
| 1439 |
+
variable, or with no storage class. A storage class is not allowed in a
|
| 1440 |
+
declaration of an anonymous union in a class scope. An anonymous union
|
| 1441 |
+
shall not have `private` or `protected` members (Clause
|
| 1442 |
+
[[class.access]]). An anonymous union shall not have member functions.
|
| 1443 |
+
|
| 1444 |
+
A union for which objects, pointers, or references are declared is not
|
| 1445 |
+
an anonymous union.
|
| 1446 |
+
|
| 1447 |
+
[*Example 2*:
|
| 1448 |
+
|
| 1449 |
+
``` cpp
|
| 1450 |
+
void f() {
|
| 1451 |
+
union { int aa; char* p; } obj, *ptr = &obj;
|
| 1452 |
+
aa = 1; // error
|
| 1453 |
+
ptr->aa = 1; // OK
|
| 1454 |
+
}
|
| 1455 |
+
```
|
| 1456 |
+
|
| 1457 |
+
The assignment to plain `aa` is ill-formed since the member name is not
|
| 1458 |
+
visible outside the union, and even if it were visible, it is not
|
| 1459 |
+
associated with any particular object.
|
| 1460 |
+
|
| 1461 |
+
— *end example*]
|
| 1462 |
+
|
| 1463 |
+
[*Note 2*: Initialization of unions with no user-declared constructors
|
| 1464 |
+
is described in [[dcl.init.aggr]]. — *end note*]
|
| 1465 |
+
|
| 1466 |
+
A *union-like class* is a union or a class that has an anonymous union
|
| 1467 |
+
as a direct member. A union-like class `X` has a set of *variant
|
| 1468 |
+
members*. If `X` is a union, a non-static data member of `X` that is not
|
| 1469 |
+
an anonymous union is a variant member of `X`. In addition, a non-static
|
| 1470 |
+
data member of an anonymous union that is a member of `X` is also a
|
| 1471 |
+
variant member of `X`. At most one variant member of a union may have a
|
| 1472 |
+
default member initializer.
|
| 1473 |
+
|
| 1474 |
+
[*Example 3*:
|
| 1475 |
+
|
| 1476 |
+
``` cpp
|
| 1477 |
+
union U {
|
| 1478 |
+
int x = 0;
|
| 1479 |
+
union {
|
| 1480 |
+
int k;
|
| 1481 |
+
};
|
| 1482 |
+
union {
|
| 1483 |
+
int z;
|
| 1484 |
+
int y = 1; // error: initialization for second variant member of U
|
| 1485 |
+
};
|
| 1486 |
+
};
|
| 1487 |
+
```
|
| 1488 |
+
|
| 1489 |
+
— *end example*]
|
| 1490 |
+
|
| 1491 |
## Local class declarations <a id="class.local">[[class.local]]</a>
|
| 1492 |
|
| 1493 |
A class can be declared within a function definition; such a class is
|
| 1494 |
called a *local* class. The name of a local class is local to its
|
| 1495 |
enclosing scope. The local class is in the scope of the enclosing scope,
|
| 1496 |
and has the same access to names outside the function as does the
|
| 1497 |
enclosing function. Declarations in a local class shall not odr-use (
|
| 1498 |
[[basic.def.odr]]) a variable with automatic storage duration from an
|
| 1499 |
enclosing scope.
|
| 1500 |
|
| 1501 |
+
[*Example 1*:
|
| 1502 |
+
|
| 1503 |
``` cpp
|
| 1504 |
int x;
|
| 1505 |
void f() {
|
| 1506 |
static int s;
|
| 1507 |
int x;
|
|
|
|
| 1519 |
}
|
| 1520 |
|
| 1521 |
local* p = 0; // error: local not in scope
|
| 1522 |
```
|
| 1523 |
|
| 1524 |
+
— *end example*]
|
| 1525 |
+
|
| 1526 |
An enclosing function has no special access to members of the local
|
| 1527 |
class; it obeys the usual access rules (Clause [[class.access]]).
|
| 1528 |
Member functions of a local class shall be defined within their class
|
| 1529 |
definition, if they are defined at all.
|
| 1530 |
|
|
|
|
| 1533 |
defined in the same scope as the definition of class `X`. A class nested
|
| 1534 |
within a local class is a local class.
|
| 1535 |
|
| 1536 |
A local class shall not have static data members.
|
| 1537 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|