- tmp/tmpgxtvb7h4/{from.md → to.md} +107 -85
tmp/tmpgxtvb7h4/{from.md → to.md}
RENAMED
|
@@ -56,22 +56,36 @@ 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 *base-type-specifier* in a *base-clause* (Clause
|
| 61 |
-
[[class.derived]]), the program is ill-formed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
Complete objects and member subobjects of class type shall have nonzero
|
| 64 |
size.[^1] Class objects can be assigned, passed as arguments to
|
| 65 |
functions, and returned by functions (except objects of classes for
|
| 66 |
which copying or moving has been restricted; see [[class.copy]]). Other
|
| 67 |
plausible operators, such as equality comparison, can be defined by the
|
| 68 |
user; see [[over.oper]].
|
| 69 |
|
| 70 |
-
A *union* is a class defined with the *class-key* `union`; it holds
|
| 71 |
-
one data member at a time ([[class.union]]). Aggregates of class
|
| 72 |
-
are described in [[dcl.init.aggr]].
|
| 73 |
|
| 74 |
A *trivially copyable class* is a class that:
|
| 75 |
|
| 76 |
- has no non-trivial copy constructors ([[class.copy]]),
|
| 77 |
- has no non-trivial move constructors ([[class.copy]]),
|
|
@@ -79,15 +93,14 @@ A *trivially copyable class* is a class that:
|
|
| 79 |
[[class.copy]]),
|
| 80 |
- has no non-trivial move assignment operators ([[over.ass]],
|
| 81 |
[[class.copy]]), and
|
| 82 |
- has a trivial destructor ([[class.dtor]]).
|
| 83 |
|
| 84 |
-
A *trivial class* is a class that has a
|
| 85 |
-
[[class.ctor]])
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
virtual functions or virtual base classes.
|
| 89 |
|
| 90 |
A *standard-layout class* is a class that:
|
| 91 |
|
| 92 |
- has no non-static data members of type non-standard-layout class (or
|
| 93 |
array of such types) or reference,
|
|
@@ -112,14 +125,14 @@ in other programming languages. Their layout is specified in
|
|
| 112 |
[[class.mem]].
|
| 113 |
|
| 114 |
A *POD struct*[^3] is a non-union class that is both a trivial class and
|
| 115 |
a standard-layout class, and has no non-static data members of type
|
| 116 |
non-POD struct, non-POD union (or array of such types). Similarly, a
|
| 117 |
-
*POD union* is a union that is both a trivial class and a
|
| 118 |
-
layout class, and has no non-static data members of type
|
| 119 |
-
non-POD union (or array of such types). A *POD class* is
|
| 120 |
-
either a POD struct or a POD union.
|
| 121 |
|
| 122 |
``` cpp
|
| 123 |
struct N { // neither trivial nor standard-layout
|
| 124 |
int i;
|
| 125 |
int j;
|
|
@@ -293,15 +306,16 @@ member-specification:
|
|
| 293 |
```
|
| 294 |
|
| 295 |
``` bnf
|
| 296 |
member-declaration:
|
| 297 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 298 |
-
function-definition
|
| 299 |
using-declaration
|
| 300 |
static_assert-declaration
|
| 301 |
template-declaration
|
| 302 |
alias-declaration
|
|
|
|
| 303 |
```
|
| 304 |
|
| 305 |
``` bnf
|
| 306 |
member-declarator-list:
|
| 307 |
member-declarator
|
|
@@ -339,39 +353,37 @@ types, and enumerators. Data members and member functions are static or
|
|
| 339 |
non-static; see [[class.static]]. Nested types are classes (
|
| 340 |
[[class.name]], [[class.nest]]) and enumerations ([[dcl.enum]])
|
| 341 |
defined in the class, and arbitrary types declared as members by use of
|
| 342 |
a typedef declaration ([[dcl.typedef]]). The enumerators of an unscoped
|
| 343 |
enumeration ([[dcl.enum]]) defined in the class are members of the
|
| 344 |
-
class. Except when used to declare friends ([[class.friend]])
|
| 345 |
-
|
| 346 |
-
[[namespace.udecl]]),
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
|
|
|
| 353 |
|
| 354 |
A class is considered a completely-defined object type (
|
| 355 |
[[basic.types]]) (or complete type) at the closing `}` of the
|
| 356 |
*class-specifier*. Within the class *member-specification*, the class is
|
| 357 |
regarded as complete within function bodies, default arguments,
|
| 358 |
-
*
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
|
|
|
| 362 |
|
| 363 |
A single name can denote several function members provided their types
|
| 364 |
are sufficiently different (Clause [[over]]).
|
| 365 |
|
| 366 |
-
A
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
A member can be initialized using a *brace-or-equal-initializer*. (For
|
| 371 |
-
static data members, see [[class.static.data]]; for non-static data
|
| 372 |
-
members, see [[class.base.init]]).
|
| 373 |
|
| 374 |
A member shall not be declared with the `extern` or `register` . Within
|
| 375 |
a class definition, a member shall not be declared with the
|
| 376 |
`thread_local` unless also declared `static`.
|
| 377 |
|
|
@@ -429,14 +441,14 @@ pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
|
| 429 |
character of the `tword` member of the `right` subtree of `s`.
|
| 430 |
|
| 431 |
Nonstatic data members of a (non-union) class with the same access
|
| 432 |
control (Clause [[class.access]]) are allocated so that later members
|
| 433 |
have higher addresses within a class object. The order of allocation of
|
| 434 |
-
non-static data members with different access control is unspecified
|
| 435 |
-
[[class.access]]). Implementation alignment requirements might
|
| 436 |
-
adjacent members not to be allocated immediately after each
|
| 437 |
-
might requirements for space for managing virtual functions (
|
| 438 |
[[class.virtual]]) and virtual base classes ([[class.mi]]).
|
| 439 |
|
| 440 |
If `T` is the name of a class, then each of the following shall have a
|
| 441 |
name different from `T`:
|
| 442 |
|
|
@@ -451,16 +463,16 @@ name different from `T`:
|
|
| 451 |
In addition, if class `T` has a user-declared constructor (
|
| 452 |
[[class.ctor]]), every non-static data member of class `T` shall have a
|
| 453 |
name different from `T`.
|
| 454 |
|
| 455 |
Two standard-layout struct (Clause [[class]]) types are
|
| 456 |
-
layout-compatible if they have the same number of non-static data
|
| 457 |
members and corresponding non-static data members (in declaration order)
|
| 458 |
have layout-compatible types ([[basic.types]]).
|
| 459 |
|
| 460 |
Two standard-layout union (Clause [[class]]) types are
|
| 461 |
-
layout-compatible if they have the same number of non-static data
|
| 462 |
members and corresponding non-static data members (in any order) have
|
| 463 |
layout-compatible types ([[basic.types]]).
|
| 464 |
|
| 465 |
If a standard-layout union contains two or more standard-layout structs
|
| 466 |
that share a common initial sequence, and if the standard-layout union
|
|
@@ -469,16 +481,16 @@ permitted to inspect the common initial part of any of them. Two
|
|
| 469 |
standard-layout structs share a common initial sequence if corresponding
|
| 470 |
members have layout-compatible types and either neither member is a
|
| 471 |
bit-field or both are bit-fields with the same width for a sequence of
|
| 472 |
one or more initial members.
|
| 473 |
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
might therefore be unnamed padding
|
| 478 |
-
object, but not at its beginning, as
|
| 479 |
-
alignment.
|
| 480 |
|
| 481 |
## Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 482 |
|
| 483 |
Functions declared in the definition of a class, excluding those
|
| 484 |
declared with a `friend` specifier ([[class.friend]]), are called
|
|
@@ -657,16 +669,13 @@ keyword `this` is a prvalue expression whose value is the address of the
|
|
| 657 |
object for which the function is called. The type of `this` in a member
|
| 658 |
function of a class `X` is `X*`. If the member function is declared
|
| 659 |
`const`, the type of `this` is `const` `X*`, if the member function is
|
| 660 |
declared `volatile`, the type of `this` is `volatile` `X*`, and if the
|
| 661 |
member function is declared `const` `volatile`, the type of `this` is
|
| 662 |
-
`const` `volatile` `X*`.
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
called is accessed through a `const` access path; therefore, a `const`
|
| 666 |
-
member function shall not modify the object and its non-static data
|
| 667 |
-
members.
|
| 668 |
|
| 669 |
``` cpp
|
| 670 |
struct s {
|
| 671 |
int a;
|
| 672 |
int f() const;
|
|
@@ -854,24 +863,24 @@ contains several standard-layout structs that share a common initial
|
|
| 854 |
sequence ([[class.mem]]), and if an object of this standard-layout
|
| 855 |
union type contains one of the standard-layout structs, it is permitted
|
| 856 |
to inspect the common initial sequence of any of standard-layout struct
|
| 857 |
members; see [[class.mem]]. The size of a union is sufficient to
|
| 858 |
contain the largest of its non-static data members. Each non-static data
|
| 859 |
-
member is allocated as if it were the sole member of a struct.
|
|
|
|
| 860 |
|
| 861 |
A union can have member functions (including constructors and
|
| 862 |
destructors), but not virtual ([[class.virtual]]) functions. A union
|
| 863 |
shall not have base classes. A union shall not be used as a base class.
|
| 864 |
If a union contains a non-static data member of reference type the
|
| 865 |
-
program is ill-formed.
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
implicitly deleted ([[dcl.fct.def.delete]]) for the union.
|
| 873 |
|
| 874 |
Consider the following union:
|
| 875 |
|
| 876 |
``` cpp
|
| 877 |
union U {
|
|
@@ -900,19 +909,19 @@ u.m.~M();
|
|
| 900 |
new (&u.n) N;
|
| 901 |
```
|
| 902 |
|
| 903 |
A union of the form
|
| 904 |
|
| 905 |
-
is called an anonymous union; it defines an unnamed object of unnamed
|
| 906 |
type. The *member-specification* of an anonymous union shall only define
|
| 907 |
-
non-static data members. Nested types
|
| 908 |
-
within an anonymous union. The names of the members
|
| 909 |
-
union shall be distinct from the names of any other
|
| 910 |
-
in which the anonymous union is declared. For the
|
| 911 |
-
lookup, after the anonymous union definition, the
|
| 912 |
-
anonymous union are considered to have been defined in
|
| 913 |
-
which the anonymous union is declared.
|
| 914 |
|
| 915 |
``` cpp
|
| 916 |
void f() {
|
| 917 |
union { int a; const char* p; };
|
| 918 |
a = 1;
|
|
@@ -929,29 +938,44 @@ scope shall be declared with any storage class allowed for a block-scope
|
|
| 929 |
variable, or with no storage class. A storage class is not allowed in a
|
| 930 |
declaration of an anonymous union in a class scope. An anonymous union
|
| 931 |
shall not have `private` or `protected` members (Clause
|
| 932 |
[[class.access]]). An anonymous union shall not have function members.
|
| 933 |
|
| 934 |
-
A union for which objects or
|
| 935 |
-
union.
|
| 936 |
|
| 937 |
``` cpp
|
|
|
|
| 938 |
union { int aa; char* p; } obj, *ptr = &obj;
|
| 939 |
aa = 1; // error
|
| 940 |
ptr->aa = 1; // OK
|
|
|
|
| 941 |
```
|
| 942 |
|
| 943 |
The assignment to plain `aa` is ill-formed since the member name is not
|
| 944 |
visible outside the union, and even if it were visible, it is not
|
| 945 |
associated with any particular object. Initialization of unions with no
|
| 946 |
user-declared constructors is described in ([[dcl.init.aggr]]).
|
| 947 |
|
| 948 |
A *union-like class* is a union or a class that has an anonymous union
|
| 949 |
as a direct member. A union-like class `X` has a set of *variant
|
| 950 |
-
members*. If `X` is a union
|
| 951 |
-
|
| 952 |
-
of
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 953 |
|
| 954 |
## Bit-fields <a id="class.bit">[[class.bit]]</a>
|
| 955 |
|
| 956 |
A *member-declarator* of the form
|
| 957 |
|
|
@@ -978,21 +1002,19 @@ externally-imposed layouts. As a special case, an unnamed bit-field with
|
|
| 978 |
a width of zero specifies alignment of the next bit-field at an
|
| 979 |
allocation unit boundary. Only when declaring an unnamed bit-field may
|
| 980 |
the value of the *constant-expression* be equal to zero.
|
| 981 |
|
| 982 |
A bit-field shall not be a static member. A bit-field shall have
|
| 983 |
-
integral or enumeration type ([[basic.fundamental]]).
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
bit-field
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
|
| 992 |
-
a temporary initialized to hold the value of the bit-field; the
|
| 993 |
-
reference is not bound to the bit-field directly. See [[dcl.init.ref]].
|
| 994 |
|
| 995 |
If the value `true` or `false` is stored into a bit-field of type `bool`
|
| 996 |
of any size (including a one bit bit-field), the original `bool` value
|
| 997 |
and the value of the bit-field shall compare equal. If the value of an
|
| 998 |
enumerator is stored into a bit-field of the same enumeration type and
|
|
|
|
| 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 *base-type-specifier* in a *base-clause* (Clause
|
| 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 |
+
|
| 70 |
+
struct X {
|
| 71 |
+
struct C { constexpr operator int() { return 5; } };
|
| 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] Class objects can be assigned, passed as arguments to
|
| 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]]). Aggregates of class
|
| 86 |
+
type are described in [[dcl.init.aggr]].
|
| 87 |
|
| 88 |
A *trivially copyable class* is a class that:
|
| 89 |
|
| 90 |
- has no non-trivial copy constructors ([[class.copy]]),
|
| 91 |
- has no non-trivial move constructors ([[class.copy]]),
|
|
|
|
| 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 |
+
A *trivial class* is a class that has a default constructor (
|
| 99 |
+
[[class.ctor]]), has no non-trivial default constructors, and is
|
| 100 |
+
trivially copyable. In particular, a trivially copyable or trivial class
|
| 101 |
+
does not have virtual functions or virtual base classes.
|
|
|
|
| 102 |
|
| 103 |
A *standard-layout class* is a class that:
|
| 104 |
|
| 105 |
- has no non-static data members of type non-standard-layout class (or
|
| 106 |
array of such types) or reference,
|
|
|
|
| 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;
|
|
|
|
| 306 |
```
|
| 307 |
|
| 308 |
``` bnf
|
| 309 |
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
|
| 320 |
member-declarator-list:
|
| 321 |
member-declarator
|
|
|
|
| 353 |
non-static; see [[class.static]]. Nested types are classes (
|
| 354 |
[[class.name]], [[class.nest]]) and enumerations ([[dcl.enum]])
|
| 355 |
defined in the class, and arbitrary types declared as members by use of
|
| 356 |
a typedef declaration ([[dcl.typedef]]). The enumerators of an unscoped
|
| 357 |
enumeration ([[dcl.enum]]) defined in the class are members of the
|
| 358 |
+
class. Except when used to declare friends ([[class.friend]]), to
|
| 359 |
+
declare an unnamed bit-field ([[class.bit]]), or to introduce the name
|
| 360 |
+
of a member of a base class into a derived class ([[namespace.udecl]]),
|
| 361 |
+
or when the declaration is an *empty-declaration*, *member-declaration*s
|
| 362 |
+
declare members of the class, and each such *member-declaration* shall
|
| 363 |
+
declare at least one member name of the class. A member shall not be
|
| 364 |
+
declared twice in the *member-specification*, except that a nested class
|
| 365 |
+
or member class template can be declared and then later defined, and
|
| 366 |
+
except that an enumeration can be introduced with an
|
| 367 |
+
*opaque-enum-declaration* and later redeclared with an *enum-specifier*.
|
| 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 |
+
*using-declaration*s introducing inheriting constructors (
|
| 374 |
+
[[class.inhctor]]), *exception-specification*s, and
|
| 375 |
+
*brace-or-equal-initializer*s for non-static data members (including
|
| 376 |
+
such things in nested classes). Otherwise it is regarded as incomplete
|
| 377 |
+
within its own class *member-specification*.
|
| 378 |
|
| 379 |
A single name can denote several function members provided their types
|
| 380 |
are sufficiently different (Clause [[over]]).
|
| 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` or `register` . Within
|
| 387 |
a class definition, a member shall not be declared with the
|
| 388 |
`thread_local` unless also declared `static`.
|
| 389 |
|
|
|
|
| 441 |
character of the `tword` member of the `right` subtree of `s`.
|
| 442 |
|
| 443 |
Nonstatic data members of a (non-union) class with the same access
|
| 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
|
| 449 |
+
other; so might requirements for space for managing virtual functions (
|
| 450 |
[[class.virtual]]) and virtual base classes ([[class.mi]]).
|
| 451 |
|
| 452 |
If `T` is the name of a class, then each of the following shall have a
|
| 453 |
name different from `T`:
|
| 454 |
|
|
|
|
| 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 they have the same number of non-static data
|
| 469 |
members and corresponding non-static data members (in declaration order)
|
| 470 |
have layout-compatible types ([[basic.types]]).
|
| 471 |
|
| 472 |
Two standard-layout union (Clause [[class]]) types are
|
| 473 |
+
*layout-compatible* if they have the same number of non-static data
|
| 474 |
members and corresponding non-static data members (in any order) have
|
| 475 |
layout-compatible types ([[basic.types]]).
|
| 476 |
|
| 477 |
If a standard-layout union contains two or more standard-layout structs
|
| 478 |
that share a common initial sequence, and if the standard-layout union
|
|
|
|
| 481 |
standard-layout structs share a common initial sequence if corresponding
|
| 482 |
members have layout-compatible types and either neither member is a
|
| 483 |
bit-field or both are bit-fields with the same width for a sequence of
|
| 484 |
one or more initial members.
|
| 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). There might therefore be unnamed padding
|
| 490 |
+
within a standard-layout struct object, but not at its beginning, as
|
| 491 |
+
necessary to achieve appropriate alignment.
|
| 492 |
|
| 493 |
## Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 494 |
|
| 495 |
Functions declared in the definition of a class, excluding those
|
| 496 |
declared with a `friend` specifier ([[class.friend]]), are called
|
|
|
|
| 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*`. thus in a `const` member function, the object
|
| 675 |
+
for which the function is called is accessed through a `const` access
|
| 676 |
+
path.
|
|
|
|
|
|
|
|
|
|
| 677 |
|
| 678 |
``` cpp
|
| 679 |
struct s {
|
| 680 |
int a;
|
| 681 |
int f() const;
|
|
|
|
| 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 {
|
|
|
|
| 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;
|
|
|
|
| 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 |
|
|
|
|
| 1002 |
a width of zero specifies alignment of the next bit-field at an
|
| 1003 |
allocation unit boundary. Only when declaring an unnamed bit-field may
|
| 1004 |
the value of the *constant-expression* be equal to zero.
|
| 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]]). If the initializer for a reference
|
| 1012 |
+
of type `const` `T&` is an lvalue that refers to a bit-field, the
|
| 1013 |
+
reference is bound to a temporary initialized to hold the value of the
|
| 1014 |
+
bit-field; the reference is not bound to the bit-field directly. See
|
| 1015 |
+
[[dcl.init.ref]].
|
|
|
|
|
|
|
| 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
|