- tmp/tmpj4_anx9y/{from.md → to.md} +764 -61
tmp/tmpj4_anx9y/{from.md → to.md}
RENAMED
|
@@ -11,10 +11,11 @@ member-declaration:
|
|
| 11 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 12 |
function-definition
|
| 13 |
using-declaration
|
| 14 |
static_assert-declaration
|
| 15 |
template-declaration
|
|
|
|
| 16 |
alias-declaration
|
| 17 |
empty-declaration
|
| 18 |
```
|
| 19 |
|
| 20 |
``` bnf
|
|
@@ -46,77 +47,136 @@ virt-specifier:
|
|
| 46 |
pure-specifier:
|
| 47 |
'= 0'
|
| 48 |
```
|
| 49 |
|
| 50 |
The *member-specification* in a class definition declares the full set
|
| 51 |
-
of members of the class; no member can be added elsewhere.
|
| 52 |
-
class
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
A class is considered a completely-defined object type (
|
| 71 |
[[basic.types]]) (or complete type) at the closing `}` of the
|
| 72 |
*class-specifier*. Within the class *member-specification*, the class is
|
| 73 |
regarded as complete within function bodies, default arguments,
|
| 74 |
-
*
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
A *brace-or-equal-initializer* shall appear only in the declaration of a
|
| 84 |
data member. (For static data members, see [[class.static.data]]; for
|
| 85 |
-
non-static data members, see [[class.base.init]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
A member shall not be declared with the `extern`
|
| 88 |
-
a class definition, a member shall not
|
| 89 |
-
|
|
|
|
| 90 |
|
| 91 |
The *decl-specifier-seq* may be omitted in constructor, destructor, and
|
| 92 |
conversion function declarations only; when declaring another kind of
|
| 93 |
member the *decl-specifier-seq* shall contain a *type-specifier* that is
|
| 94 |
not a *cv-qualifier*. The *member-declarator-list* can be omitted only
|
| 95 |
after a *class-specifier* or an *enum-specifier* or in a `friend`
|
| 96 |
declaration ([[class.friend]]). A *pure-specifier* shall be used only
|
| 97 |
-
in the declaration of a virtual function ([[class.virtual]])
|
|
|
|
| 98 |
|
| 99 |
The optional *attribute-specifier-seq* in a *member-declaration*
|
| 100 |
appertains to each of the entities declared by the *member-declarator*s;
|
| 101 |
it shall not appear if the optional *member-declarator-list* is omitted.
|
| 102 |
|
| 103 |
A *virt-specifier-seq* shall contain at most one of each
|
| 104 |
*virt-specifier*. A *virt-specifier-seq* shall appear only in the
|
| 105 |
declaration of a virtual member function ([[class.virtual]]).
|
| 106 |
|
| 107 |
-
Non-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
class `C`.
|
| 111 |
|
| 112 |
-
See [[expr.prim]] for restrictions on the use of non-static
|
| 113 |
-
members and non-static member functions.
|
| 114 |
|
| 115 |
-
The type of a non-static member function is an ordinary
|
| 116 |
-
and the type of a non-static data member is an ordinary
|
| 117 |
-
There are no special member function types or data member
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
A simple example of a class definition is
|
| 120 |
|
| 121 |
``` cpp
|
| 122 |
struct tnode {
|
|
@@ -139,11 +199,13 @@ declares `s` to be a `tnode` and `sp` to be a pointer to a `tnode`. With
|
|
| 139 |
these declarations, `sp->count` refers to the `count` member of the
|
| 140 |
object to which `sp` points; `s.left` refers to the `left` subtree
|
| 141 |
pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
| 142 |
character of the `tword` member of the `right` subtree of `s`.
|
| 143 |
|
| 144 |
-
|
|
|
|
|
|
|
| 145 |
control (Clause [[class.access]]) are allocated so that later members
|
| 146 |
have higher addresses within a class object. The order of allocation of
|
| 147 |
non-static data members with different access control is unspecified
|
| 148 |
(Clause [[class.access]]). Implementation alignment requirements might
|
| 149 |
cause two adjacent members not to be allocated immediately after each
|
|
@@ -152,42 +214,683 @@ other; so might requirements for space for managing virtual functions (
|
|
| 152 |
|
| 153 |
If `T` is the name of a class, then each of the following shall have a
|
| 154 |
name different from `T`:
|
| 155 |
|
| 156 |
- every static data member of class `T`;
|
| 157 |
-
- every member function of class `T` This restriction does
|
| 158 |
-
constructors, which do not have names (
|
|
|
|
| 159 |
- every member of class `T` that is itself a type;
|
|
|
|
| 160 |
- every enumerator of every member of class `T` that is an unscoped
|
| 161 |
enumerated type; and
|
| 162 |
- every member of every anonymous union that is a member of class `T`.
|
| 163 |
|
| 164 |
In addition, if class `T` has a user-declared constructor (
|
| 165 |
[[class.ctor]]), every non-static data member of class `T` shall have a
|
| 166 |
name different from `T`.
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
Two standard-layout struct (Clause [[class]]) types are
|
| 169 |
-
*layout-compatible* if
|
| 170 |
-
members and
|
| 171 |
-
have layout-compatible types ([[basic.types]]).
|
| 172 |
|
| 173 |
-
Two standard-layout
|
| 174 |
-
|
| 175 |
-
members
|
| 176 |
-
layout-compatible types ([[basic.types]]).
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
If a standard-layout class object has any non-static data members, its
|
| 188 |
address is the same as the address of its first non-static data member.
|
| 189 |
Otherwise, its address is the same as the address of its first base
|
| 190 |
-
class subobject (if any).
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
|
|
|
| 11 |
attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
|
| 12 |
function-definition
|
| 13 |
using-declaration
|
| 14 |
static_assert-declaration
|
| 15 |
template-declaration
|
| 16 |
+
deduction-guide
|
| 17 |
alias-declaration
|
| 18 |
empty-declaration
|
| 19 |
```
|
| 20 |
|
| 21 |
``` bnf
|
|
|
|
| 47 |
pure-specifier:
|
| 48 |
'= 0'
|
| 49 |
```
|
| 50 |
|
| 51 |
The *member-specification* in a class definition declares the full set
|
| 52 |
+
of members of the class; no member can be added elsewhere. A *direct
|
| 53 |
+
member* of a class `X` is a member of `X` that was first declared within
|
| 54 |
+
the *member-specification* of `X`, including anonymous union objects (
|
| 55 |
+
[[class.union.anon]]) and direct members thereof. Members of a class are
|
| 56 |
+
data members, member functions ([[class.mfct]]), nested types,
|
| 57 |
+
enumerators, and member templates ([[temp.mem]]) and specializations
|
| 58 |
+
thereof.
|
| 59 |
+
|
| 60 |
+
[*Note 1*: A specialization of a static data member template is a
|
| 61 |
+
static data member. A specialization of a member function template is a
|
| 62 |
+
member function. A specialization of a member class template is a nested
|
| 63 |
+
class. — *end note*]
|
| 64 |
+
|
| 65 |
+
A *member-declaration* does not declare new members of the class if it
|
| 66 |
+
is
|
| 67 |
+
|
| 68 |
+
- a friend declaration ([[class.friend]]),
|
| 69 |
+
- a *static_assert-declaration*,
|
| 70 |
+
- a *using-declaration* ([[namespace.udecl]]), or
|
| 71 |
+
- an *empty-declaration*.
|
| 72 |
+
|
| 73 |
+
For any other *member-declaration*, each declared entity that is not an
|
| 74 |
+
unnamed bit-field ([[class.bit]]) is a member of the class, and each
|
| 75 |
+
such *member-declaration* shall either declare at least one member name
|
| 76 |
+
of the class or declare at least one unnamed bit-field.
|
| 77 |
+
|
| 78 |
+
A *data member* is a non-function member introduced by a
|
| 79 |
+
*member-declarator*. A *member function* is a member that is a function.
|
| 80 |
+
Nested types are classes ([[class.name]], [[class.nest]]) and
|
| 81 |
+
enumerations ([[dcl.enum]]) declared in the class and arbitrary types
|
| 82 |
+
declared as members by use of a typedef declaration ([[dcl.typedef]])
|
| 83 |
+
or *alias-declaration*. The enumerators of an unscoped enumeration (
|
| 84 |
+
[[dcl.enum]]) defined in the class are members of the class.
|
| 85 |
+
|
| 86 |
+
A data member or member function may be declared `static` in its
|
| 87 |
+
*member-declaration*, in which case it is a *static member* (see
|
| 88 |
+
[[class.static]]) (a *static data member* ([[class.static.data]]) or
|
| 89 |
+
*static member function* ([[class.static.mfct]]), respectively) of the
|
| 90 |
+
class. Any other data member or member function is a *non-static member*
|
| 91 |
+
(a *non-static data member* or *non-static member function* (
|
| 92 |
+
[[class.mfct.non-static]]), respectively).
|
| 93 |
+
|
| 94 |
+
[*Note 2*: A non-static data member of non-reference type is a member
|
| 95 |
+
subobject of a class object ([[intro.object]]). — *end note*]
|
| 96 |
+
|
| 97 |
+
A member shall not be declared twice in the *member-specification*,
|
| 98 |
+
except that
|
| 99 |
+
|
| 100 |
+
- a nested class or member class template can be declared and then later
|
| 101 |
+
defined, and
|
| 102 |
+
- an enumeration can be introduced with an *opaque-enum-declaration* and
|
| 103 |
+
later redeclared with an *enum-specifier*.
|
| 104 |
+
|
| 105 |
+
[*Note 3*: A single name can denote several member functions provided
|
| 106 |
+
their types are sufficiently different (Clause
|
| 107 |
+
[[over]]). — *end note*]
|
| 108 |
|
| 109 |
A class is considered a completely-defined object type (
|
| 110 |
[[basic.types]]) (or complete type) at the closing `}` of the
|
| 111 |
*class-specifier*. Within the class *member-specification*, the class is
|
| 112 |
regarded as complete within function bodies, default arguments,
|
| 113 |
+
*noexcept-specifier*s, and default member initializers (including such
|
| 114 |
+
things in nested classes). Otherwise it is regarded as incomplete within
|
| 115 |
+
its own class *member-specification*.
|
| 116 |
+
|
| 117 |
+
In a *member-declarator*, an `=` immediately following the *declarator*
|
| 118 |
+
is interpreted as introducing a *pure-specifier* if the *declarator-id*
|
| 119 |
+
has function type, otherwise it is interpreted as introducing a
|
| 120 |
+
*brace-or-equal-initializer*.
|
| 121 |
+
|
| 122 |
+
[*Example 1*:
|
| 123 |
+
|
| 124 |
+
``` cpp
|
| 125 |
+
struct S {
|
| 126 |
+
using T = void();
|
| 127 |
+
T * p = 0; // OK: brace-or-equal-initializer
|
| 128 |
+
virtual T f = 0; // OK: pure-specifier
|
| 129 |
+
};
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
— *end example*]
|
| 133 |
|
| 134 |
A *brace-or-equal-initializer* shall appear only in the declaration of a
|
| 135 |
data member. (For static data members, see [[class.static.data]]; for
|
| 136 |
+
non-static data members, see [[class.base.init]] and
|
| 137 |
+
[[dcl.init.aggr]]). A *brace-or-equal-initializer* for a non-static data
|
| 138 |
+
member specifies a *default member initializer* for the member, and
|
| 139 |
+
shall not directly or indirectly cause the implicit definition of a
|
| 140 |
+
defaulted default constructor for the enclosing class or the exception
|
| 141 |
+
specification of that constructor.
|
| 142 |
|
| 143 |
+
A member shall not be declared with the `extern`
|
| 144 |
+
*storage-class-specifier*. Within a class definition, a member shall not
|
| 145 |
+
be declared with the `thread_local` *storage-class-specifier* unless
|
| 146 |
+
also declared `static`.
|
| 147 |
|
| 148 |
The *decl-specifier-seq* may be omitted in constructor, destructor, and
|
| 149 |
conversion function declarations only; when declaring another kind of
|
| 150 |
member the *decl-specifier-seq* shall contain a *type-specifier* that is
|
| 151 |
not a *cv-qualifier*. The *member-declarator-list* can be omitted only
|
| 152 |
after a *class-specifier* or an *enum-specifier* or in a `friend`
|
| 153 |
declaration ([[class.friend]]). A *pure-specifier* shall be used only
|
| 154 |
+
in the declaration of a virtual function ([[class.virtual]]) that is
|
| 155 |
+
not a `friend` declaration.
|
| 156 |
|
| 157 |
The optional *attribute-specifier-seq* in a *member-declaration*
|
| 158 |
appertains to each of the entities declared by the *member-declarator*s;
|
| 159 |
it shall not appear if the optional *member-declarator-list* is omitted.
|
| 160 |
|
| 161 |
A *virt-specifier-seq* shall contain at most one of each
|
| 162 |
*virt-specifier*. A *virt-specifier-seq* shall appear only in the
|
| 163 |
declaration of a virtual member function ([[class.virtual]]).
|
| 164 |
|
| 165 |
+
Non-static data members shall not have incomplete types. In particular,
|
| 166 |
+
a class `C` shall not contain a non-static member of class `C`, but it
|
| 167 |
+
can contain a pointer or reference to an object of class `C`.
|
|
|
|
| 168 |
|
| 169 |
+
[*Note 4*: See [[expr.prim]] for restrictions on the use of non-static
|
| 170 |
+
data members and non-static member functions. — *end note*]
|
| 171 |
|
| 172 |
+
[*Note 5*: The type of a non-static member function is an ordinary
|
| 173 |
+
function type, and the type of a non-static data member is an ordinary
|
| 174 |
+
object type. There are no special member function types or data member
|
| 175 |
+
types. — *end note*]
|
| 176 |
+
|
| 177 |
+
[*Example 2*:
|
| 178 |
|
| 179 |
A simple example of a class definition is
|
| 180 |
|
| 181 |
``` cpp
|
| 182 |
struct tnode {
|
|
|
|
| 199 |
these declarations, `sp->count` refers to the `count` member of the
|
| 200 |
object to which `sp` points; `s.left` refers to the `left` subtree
|
| 201 |
pointer of the object `s`; and `s.right->tword[0]` refers to the initial
|
| 202 |
character of the `tword` member of the `right` subtree of `s`.
|
| 203 |
|
| 204 |
+
— *end example*]
|
| 205 |
+
|
| 206 |
+
Non-static data members of a (non-union) class with the same access
|
| 207 |
control (Clause [[class.access]]) are allocated so that later members
|
| 208 |
have higher addresses within a class object. The order of allocation of
|
| 209 |
non-static data members with different access control is unspecified
|
| 210 |
(Clause [[class.access]]). Implementation alignment requirements might
|
| 211 |
cause two adjacent members not to be allocated immediately after each
|
|
|
|
| 214 |
|
| 215 |
If `T` is the name of a class, then each of the following shall have a
|
| 216 |
name different from `T`:
|
| 217 |
|
| 218 |
- every static data member of class `T`;
|
| 219 |
+
- every member function of class `T` \[*Note 6*: This restriction does
|
| 220 |
+
not apply to constructors, which do not have names (
|
| 221 |
+
[[class.ctor]]) — *end note*] ;
|
| 222 |
- every member of class `T` that is itself a type;
|
| 223 |
+
- every member template of class `T`;
|
| 224 |
- every enumerator of every member of class `T` that is an unscoped
|
| 225 |
enumerated type; and
|
| 226 |
- every member of every anonymous union that is a member of class `T`.
|
| 227 |
|
| 228 |
In addition, if class `T` has a user-declared constructor (
|
| 229 |
[[class.ctor]]), every non-static data member of class `T` shall have a
|
| 230 |
name different from `T`.
|
| 231 |
|
| 232 |
+
The *common initial sequence* of two standard-layout struct (Clause
|
| 233 |
+
[[class]]) types is the longest sequence of non-static data members and
|
| 234 |
+
bit-fields in declaration order, starting with the first such entity in
|
| 235 |
+
each of the structs, such that corresponding entities have
|
| 236 |
+
layout-compatible types and either neither entity is a bit-field or both
|
| 237 |
+
are bit-fields with the same width.
|
| 238 |
+
|
| 239 |
+
[*Example 3*:
|
| 240 |
+
|
| 241 |
+
``` cpp
|
| 242 |
+
struct A { int a; char b; };
|
| 243 |
+
struct B { const int b1; volatile char b2; };
|
| 244 |
+
struct C { int c; unsigned : 0; char b; };
|
| 245 |
+
struct D { int d; char b : 4; };
|
| 246 |
+
struct E { unsigned int e; char b; };
|
| 247 |
+
```
|
| 248 |
+
|
| 249 |
+
The common initial sequence of `A` and `B` comprises all members of
|
| 250 |
+
either class. The common initial sequence of `A` and `C` and of `A` and
|
| 251 |
+
`D` comprises the first member in each case. The common initial sequence
|
| 252 |
+
of `A` and `E` is empty.
|
| 253 |
+
|
| 254 |
+
— *end example*]
|
| 255 |
+
|
| 256 |
Two standard-layout struct (Clause [[class]]) types are
|
| 257 |
+
*layout-compatible classes* if their common initial sequence comprises
|
| 258 |
+
all members and bit-fields of both classes ([[basic.types]]).
|
|
|
|
| 259 |
|
| 260 |
+
Two standard-layout unions are layout-compatible if they have the same
|
| 261 |
+
number of non-static data members and corresponding non-static data
|
| 262 |
+
members (in any order) have layout-compatible types ([[basic.types]]).
|
|
|
|
| 263 |
|
| 264 |
+
In a standard-layout union with an active member ([[class.union]]) of
|
| 265 |
+
struct type `T1`, it is permitted to read a non-static data member `m`
|
| 266 |
+
of another union member of struct type `T2` provided `m` is part of the
|
| 267 |
+
common initial sequence of `T1` and `T2`; the behavior is as if the
|
| 268 |
+
corresponding member of `T1` were nominated.
|
| 269 |
+
|
| 270 |
+
[*Example 4*:
|
| 271 |
+
|
| 272 |
+
``` cpp
|
| 273 |
+
struct T1 { int a, b; };
|
| 274 |
+
struct T2 { int c; double d; };
|
| 275 |
+
union U { T1 t1; T2 t2; };
|
| 276 |
+
int f() {
|
| 277 |
+
U u = { { 1, 2 } }; // active member is t1
|
| 278 |
+
return u.t2.c; // OK, as if u.t1.a were nominated
|
| 279 |
+
}
|
| 280 |
+
```
|
| 281 |
+
|
| 282 |
+
— *end example*]
|
| 283 |
+
|
| 284 |
+
[*Note 7*: Reading a volatile object through a non-volatile glvalue has
|
| 285 |
+
undefined behavior ([[dcl.type.cv]]). — *end note*]
|
| 286 |
|
| 287 |
If a standard-layout class object has any non-static data members, its
|
| 288 |
address is the same as the address of its first non-static data member.
|
| 289 |
Otherwise, its address is the same as the address of its first base
|
| 290 |
+
class subobject (if any).
|
| 291 |
+
|
| 292 |
+
[*Note 8*: There might therefore be unnamed padding within a
|
| 293 |
+
standard-layout struct object, but not at its beginning, as necessary to
|
| 294 |
+
achieve appropriate alignment. — *end note*]
|
| 295 |
+
|
| 296 |
+
[*Note 9*: The object and its first subobject are
|
| 297 |
+
pointer-interconvertible ([[basic.compound]],
|
| 298 |
+
[[expr.static.cast]]). — *end note*]
|
| 299 |
+
|
| 300 |
+
### Member functions <a id="class.mfct">[[class.mfct]]</a>
|
| 301 |
+
|
| 302 |
+
A member function may be defined ([[dcl.fct.def]]) in its class
|
| 303 |
+
definition, in which case it is an *inline* member function (
|
| 304 |
+
[[dcl.inline]]), or it may be defined outside of its class definition if
|
| 305 |
+
it has already been declared but not defined in its class definition. A
|
| 306 |
+
member function definition that appears outside of the class definition
|
| 307 |
+
shall appear in a namespace scope enclosing the class definition. Except
|
| 308 |
+
for member function definitions that appear outside of a class
|
| 309 |
+
definition, and except for explicit specializations of member functions
|
| 310 |
+
of class templates and member function templates ([[temp.spec]])
|
| 311 |
+
appearing outside of the class definition, a member function shall not
|
| 312 |
+
be redeclared.
|
| 313 |
+
|
| 314 |
+
An inline member function (whether static or non-static) may also be
|
| 315 |
+
defined outside of its class definition provided either its declaration
|
| 316 |
+
in the class definition or its definition outside of the class
|
| 317 |
+
definition declares the function as `inline` or `constexpr`.
|
| 318 |
+
|
| 319 |
+
[*Note 1*: Member functions of a class in namespace scope have the
|
| 320 |
+
linkage of that class. Member functions of a local class (
|
| 321 |
+
[[class.local]]) have no linkage. See [[basic.link]]. — *end note*]
|
| 322 |
+
|
| 323 |
+
[*Note 2*: There can be at most one definition of a non-inline member
|
| 324 |
+
function in a program. There may be more than one `inline` member
|
| 325 |
+
function definition in a program. See [[basic.def.odr]] and
|
| 326 |
+
[[dcl.inline]]. — *end note*]
|
| 327 |
+
|
| 328 |
+
If the definition of a member function is lexically outside its class
|
| 329 |
+
definition, the member function name shall be qualified by its class
|
| 330 |
+
name using the `::` operator.
|
| 331 |
+
|
| 332 |
+
[*Note 3*: A name used in a member function definition (that is, in the
|
| 333 |
+
*parameter-declaration-clause* including the default arguments (
|
| 334 |
+
[[dcl.fct.default]]) or in the member function body) is looked up as
|
| 335 |
+
described in [[basic.lookup]]. — *end note*]
|
| 336 |
+
|
| 337 |
+
[*Example 1*:
|
| 338 |
+
|
| 339 |
+
``` cpp
|
| 340 |
+
struct X {
|
| 341 |
+
typedef int T;
|
| 342 |
+
static T count;
|
| 343 |
+
void f(T);
|
| 344 |
+
};
|
| 345 |
+
void X::f(T t = count) { }
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
The member function `f` of class `X` is defined in global scope; the
|
| 349 |
+
notation `X::f` specifies that the function `f` is a member of class `X`
|
| 350 |
+
and in the scope of class `X`. In the function definition, the parameter
|
| 351 |
+
type `T` refers to the typedef member `T` declared in class `X` and the
|
| 352 |
+
default argument `count` refers to the static data member `count`
|
| 353 |
+
declared in class `X`.
|
| 354 |
+
|
| 355 |
+
— *end example*]
|
| 356 |
+
|
| 357 |
+
[*Note 4*: A `static` local variable or local type in a member function
|
| 358 |
+
always refers to the same entity, whether or not the member function is
|
| 359 |
+
`inline`. — *end note*]
|
| 360 |
+
|
| 361 |
+
Previously declared member functions may be mentioned in `friend`
|
| 362 |
+
declarations.
|
| 363 |
+
|
| 364 |
+
Member functions of a local class shall be defined inline in their class
|
| 365 |
+
definition, if they are defined at all.
|
| 366 |
+
|
| 367 |
+
[*Note 5*:
|
| 368 |
+
|
| 369 |
+
A member function can be declared (but not defined) using a typedef for
|
| 370 |
+
a function type. The resulting member function has exactly the same type
|
| 371 |
+
as it would have if the function declarator were provided explicitly,
|
| 372 |
+
see [[dcl.fct]]. For example,
|
| 373 |
+
|
| 374 |
+
``` cpp
|
| 375 |
+
typedef void fv();
|
| 376 |
+
typedef void fvc() const;
|
| 377 |
+
struct S {
|
| 378 |
+
fv memfunc1; // equivalent to: void memfunc1();
|
| 379 |
+
void memfunc2();
|
| 380 |
+
fvc memfunc3; // equivalent to: void memfunc3() const;
|
| 381 |
+
};
|
| 382 |
+
fv S::* pmfv1 = &S::memfunc1;
|
| 383 |
+
fv S::* pmfv2 = &S::memfunc2;
|
| 384 |
+
fvc S::* pmfv3 = &S::memfunc3;
|
| 385 |
+
```
|
| 386 |
+
|
| 387 |
+
Also see [[temp.arg]].
|
| 388 |
+
|
| 389 |
+
— *end note*]
|
| 390 |
+
|
| 391 |
+
### Non-static member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
|
| 392 |
+
|
| 393 |
+
A non-static member function may be called for an object of its class
|
| 394 |
+
type, or for an object of a class derived (Clause [[class.derived]])
|
| 395 |
+
from its class type, using the class member access syntax (
|
| 396 |
+
[[expr.ref]], [[over.match.call]]). A non-static member function may
|
| 397 |
+
also be called directly using the function call syntax ([[expr.call]],
|
| 398 |
+
[[over.match.call]]) from within the body of a member function of its
|
| 399 |
+
class or of a class derived from its class.
|
| 400 |
+
|
| 401 |
+
If a non-static member function of a class `X` is called for an object
|
| 402 |
+
that is not of type `X`, or of a type derived from `X`, the behavior is
|
| 403 |
+
undefined.
|
| 404 |
+
|
| 405 |
+
When an *id-expression* ([[expr.prim]]) that is not part of a class
|
| 406 |
+
member access syntax ([[expr.ref]]) and not used to form a pointer to
|
| 407 |
+
member ([[expr.unary.op]]) is used in a member of class `X` in a
|
| 408 |
+
context where `this` can be used ([[expr.prim.this]]), if name lookup (
|
| 409 |
+
[[basic.lookup]]) resolves the name in the *id-expression* to a
|
| 410 |
+
non-static non-type member of some class `C`, and if either the
|
| 411 |
+
*id-expression* is potentially evaluated or `C` is `X` or a base class
|
| 412 |
+
of `X`, the *id-expression* is transformed into a class member access
|
| 413 |
+
expression ([[expr.ref]]) using `(*this)` ([[class.this]]) as the
|
| 414 |
+
*postfix-expression* to the left of the `.` operator.
|
| 415 |
+
|
| 416 |
+
[*Note 1*: If `C` is not `X` or a base class of `X`, the class member
|
| 417 |
+
access expression is ill-formed. — *end note*]
|
| 418 |
+
|
| 419 |
+
Similarly during name lookup, when an *unqualified-id* ([[expr.prim]])
|
| 420 |
+
used in the definition of a member function for class `X` resolves to a
|
| 421 |
+
static member, an enumerator or a nested type of class `X` or of a base
|
| 422 |
+
class of `X`, the *unqualified-id* is transformed into a
|
| 423 |
+
*qualified-id* ([[expr.prim]]) in which the *nested-name-specifier*
|
| 424 |
+
names the class of the member function. These transformations do not
|
| 425 |
+
apply in the template definition context ([[temp.dep.type]]).
|
| 426 |
+
|
| 427 |
+
[*Example 1*:
|
| 428 |
+
|
| 429 |
+
``` cpp
|
| 430 |
+
struct tnode {
|
| 431 |
+
char tword[20];
|
| 432 |
+
int count;
|
| 433 |
+
tnode* left;
|
| 434 |
+
tnode* right;
|
| 435 |
+
void set(const char*, tnode* l, tnode* r);
|
| 436 |
+
};
|
| 437 |
+
|
| 438 |
+
void tnode::set(const char* w, tnode* l, tnode* r) {
|
| 439 |
+
count = strlen(w)+1;
|
| 440 |
+
if (sizeof(tword)<=count)
|
| 441 |
+
perror("tnode string too long");
|
| 442 |
+
strcpy(tword,w);
|
| 443 |
+
left = l;
|
| 444 |
+
right = r;
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
void f(tnode n1, tnode n2) {
|
| 448 |
+
n1.set("abc",&n2,0);
|
| 449 |
+
n2.set("def",0,0);
|
| 450 |
+
}
|
| 451 |
+
```
|
| 452 |
+
|
| 453 |
+
In the body of the member function `tnode::set`, the member names
|
| 454 |
+
`tword`, `count`, `left`, and `right` refer to members of the object for
|
| 455 |
+
which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
|
| 456 |
+
`tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
|
| 457 |
+
refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
|
| 458 |
+
not members of the class `tnode` and should be declared elsewhere.[^4]
|
| 459 |
+
|
| 460 |
+
— *end example*]
|
| 461 |
+
|
| 462 |
+
A non-static member function may be declared `const`, `volatile`, or
|
| 463 |
+
`const` `volatile`. These *cv-qualifier*s affect the type of the `this`
|
| 464 |
+
pointer ([[class.this]]). They also affect the function type (
|
| 465 |
+
[[dcl.fct]]) of the member function; a member function declared `const`
|
| 466 |
+
is a *const* member function, a member function declared `volatile` is a
|
| 467 |
+
*volatile* member function and a member function declared `const`
|
| 468 |
+
`volatile` is a *const volatile* member function.
|
| 469 |
+
|
| 470 |
+
[*Example 2*:
|
| 471 |
+
|
| 472 |
+
``` cpp
|
| 473 |
+
struct X {
|
| 474 |
+
void g() const;
|
| 475 |
+
void h() const volatile;
|
| 476 |
+
};
|
| 477 |
+
```
|
| 478 |
+
|
| 479 |
+
`X::g` is a `const` member function and `X::h` is a `const` `volatile`
|
| 480 |
+
member function.
|
| 481 |
+
|
| 482 |
+
— *end example*]
|
| 483 |
+
|
| 484 |
+
A non-static member function may be declared with a *ref-qualifier* (
|
| 485 |
+
[[dcl.fct]]); see [[over.match.funcs]].
|
| 486 |
+
|
| 487 |
+
A non-static member function may be declared *virtual* (
|
| 488 |
+
[[class.virtual]]) or *pure virtual* ([[class.abstract]]).
|
| 489 |
+
|
| 490 |
+
#### The `this` pointer <a id="class.this">[[class.this]]</a>
|
| 491 |
+
|
| 492 |
+
In the body of a non-static ([[class.mfct]]) member function, the
|
| 493 |
+
keyword `this` is a prvalue expression whose value is the address of the
|
| 494 |
+
object for which the function is called. The type of `this` in a member
|
| 495 |
+
function of a class `X` is `X*`. If the member function is declared
|
| 496 |
+
`const`, the type of `this` is `const` `X*`, if the member function is
|
| 497 |
+
declared `volatile`, the type of `this` is `volatile` `X*`, and if the
|
| 498 |
+
member function is declared `const` `volatile`, the type of `this` is
|
| 499 |
+
`const` `volatile` `X*`.
|
| 500 |
+
|
| 501 |
+
[*Note 1*: Thus in a `const` member function, the object for which the
|
| 502 |
+
function is called is accessed through a `const` access
|
| 503 |
+
path. — *end note*]
|
| 504 |
+
|
| 505 |
+
[*Example 1*:
|
| 506 |
+
|
| 507 |
+
``` cpp
|
| 508 |
+
struct s {
|
| 509 |
+
int a;
|
| 510 |
+
int f() const;
|
| 511 |
+
int g() { return a++; }
|
| 512 |
+
int h() const { return a++; } // error
|
| 513 |
+
};
|
| 514 |
+
|
| 515 |
+
int s::f() const { return a; }
|
| 516 |
+
```
|
| 517 |
+
|
| 518 |
+
The `a++` in the body of `s::h` is ill-formed because it tries to modify
|
| 519 |
+
(a part of) the object for which `s::h()` is called. This is not allowed
|
| 520 |
+
in a `const` member function because `this` is a pointer to `const`;
|
| 521 |
+
that is, `*this` has `const` type.
|
| 522 |
+
|
| 523 |
+
— *end example*]
|
| 524 |
+
|
| 525 |
+
Similarly, `volatile` semantics ([[dcl.type.cv]]) apply in `volatile`
|
| 526 |
+
member functions when accessing the object and its non-static data
|
| 527 |
+
members.
|
| 528 |
+
|
| 529 |
+
A cv-qualified member function can be called on an object-expression (
|
| 530 |
+
[[expr.ref]]) only if the object-expression is as cv-qualified or
|
| 531 |
+
less-cv-qualified than the member function.
|
| 532 |
+
|
| 533 |
+
[*Example 2*:
|
| 534 |
+
|
| 535 |
+
``` cpp
|
| 536 |
+
void k(s& x, const s& y) {
|
| 537 |
+
x.f();
|
| 538 |
+
x.g();
|
| 539 |
+
y.f();
|
| 540 |
+
y.g(); // error
|
| 541 |
+
}
|
| 542 |
+
```
|
| 543 |
+
|
| 544 |
+
The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
|
| 545 |
+
non-`const` member function, that is, `s::g()` is less-qualified than
|
| 546 |
+
the object-expression `y`.
|
| 547 |
+
|
| 548 |
+
— *end example*]
|
| 549 |
+
|
| 550 |
+
Constructors ([[class.ctor]]) and destructors ([[class.dtor]]) shall
|
| 551 |
+
not be declared `const`, `volatile` or `const` `volatile`.
|
| 552 |
+
|
| 553 |
+
[*Note 2*: However, these functions can be invoked to create and
|
| 554 |
+
destroy objects with cv-qualified types, see [[class.ctor]] and
|
| 555 |
+
[[class.dtor]]. — *end note*]
|
| 556 |
+
|
| 557 |
+
### Static members <a id="class.static">[[class.static]]</a>
|
| 558 |
+
|
| 559 |
+
A static member `s` of class `X` may be referred to using the
|
| 560 |
+
*qualified-id* expression `X::s`; it is not necessary to use the class
|
| 561 |
+
member access syntax ([[expr.ref]]) to refer to a static member. A
|
| 562 |
+
static member may be referred to using the class member access syntax,
|
| 563 |
+
in which case the object expression is evaluated.
|
| 564 |
+
|
| 565 |
+
[*Example 1*:
|
| 566 |
+
|
| 567 |
+
``` cpp
|
| 568 |
+
struct process {
|
| 569 |
+
static void reschedule();
|
| 570 |
+
};
|
| 571 |
+
process& g();
|
| 572 |
+
|
| 573 |
+
void f() {
|
| 574 |
+
process::reschedule(); // OK: no object necessary
|
| 575 |
+
g().reschedule(); // g() is called
|
| 576 |
+
}
|
| 577 |
+
```
|
| 578 |
+
|
| 579 |
+
— *end example*]
|
| 580 |
+
|
| 581 |
+
A static member may be referred to directly in the scope of its class or
|
| 582 |
+
in the scope of a class derived (Clause [[class.derived]]) from its
|
| 583 |
+
class; in this case, the static member is referred to as if a
|
| 584 |
+
*qualified-id* expression was used, with the *nested-name-specifier* of
|
| 585 |
+
the *qualified-id* naming the class scope from which the static member
|
| 586 |
+
is referenced.
|
| 587 |
+
|
| 588 |
+
[*Example 2*:
|
| 589 |
+
|
| 590 |
+
``` cpp
|
| 591 |
+
int g();
|
| 592 |
+
struct X {
|
| 593 |
+
static int g();
|
| 594 |
+
};
|
| 595 |
+
struct Y : X {
|
| 596 |
+
static int i;
|
| 597 |
+
};
|
| 598 |
+
int Y::i = g(); // equivalent to Y::g();
|
| 599 |
+
```
|
| 600 |
+
|
| 601 |
+
— *end example*]
|
| 602 |
+
|
| 603 |
+
If an *unqualified-id* ([[expr.prim]]) is used in the definition of a
|
| 604 |
+
static member following the member’s *declarator-id*, and name lookup (
|
| 605 |
+
[[basic.lookup.unqual]]) finds that the *unqualified-id* refers to a
|
| 606 |
+
static member, enumerator, or nested type of the member’s class (or of a
|
| 607 |
+
base class of the member’s class), the *unqualified-id* is transformed
|
| 608 |
+
into a *qualified-id* expression in which the *nested-name-specifier*
|
| 609 |
+
names the class scope from which the member is referenced.
|
| 610 |
+
|
| 611 |
+
[*Note 1*: See [[expr.prim]] for restrictions on the use of non-static
|
| 612 |
+
data members and non-static member functions. — *end note*]
|
| 613 |
+
|
| 614 |
+
Static members obey the usual class member access rules (Clause
|
| 615 |
+
[[class.access]]). When used in the declaration of a class member, the
|
| 616 |
+
`static` specifier shall only be used in the member declarations that
|
| 617 |
+
appear within the *member-specification* of the class definition.
|
| 618 |
+
|
| 619 |
+
[*Note 2*: It cannot be specified in member declarations that appear in
|
| 620 |
+
namespace scope. — *end note*]
|
| 621 |
+
|
| 622 |
+
#### Static member functions <a id="class.static.mfct">[[class.static.mfct]]</a>
|
| 623 |
+
|
| 624 |
+
[*Note 1*: The rules described in [[class.mfct]] apply to static
|
| 625 |
+
member functions. — *end note*]
|
| 626 |
+
|
| 627 |
+
[*Note 2*: A static member function does not have a `this` pointer (
|
| 628 |
+
[[class.this]]). — *end note*]
|
| 629 |
+
|
| 630 |
+
A static member function shall not be `virtual`. There shall not be a
|
| 631 |
+
static and a non-static member function with the same name and the same
|
| 632 |
+
parameter types ([[over.load]]). A static member function shall not be
|
| 633 |
+
declared `const`, `volatile`, or `const volatile`.
|
| 634 |
+
|
| 635 |
+
#### Static data members <a id="class.static.data">[[class.static.data]]</a>
|
| 636 |
+
|
| 637 |
+
A static data member is not part of the subobjects of a class. If a
|
| 638 |
+
static data member is declared `thread_local` there is one copy of the
|
| 639 |
+
member per thread. If a static data member is not declared
|
| 640 |
+
`thread_local` there is one copy of the data member that is shared by
|
| 641 |
+
all the objects of the class.
|
| 642 |
+
|
| 643 |
+
The declaration of a non-inline static data member in its class
|
| 644 |
+
definition is not a definition and may be of an incomplete type other
|
| 645 |
+
than cv `void`. The definition for a static data member that is not
|
| 646 |
+
defined inline in the class definition shall appear in a namespace scope
|
| 647 |
+
enclosing the member’s class definition. In the definition at namespace
|
| 648 |
+
scope, the name of the static data member shall be qualified by its
|
| 649 |
+
class name using the `::` operator. The *initializer* expression in the
|
| 650 |
+
definition of a static data member is in the scope of its class (
|
| 651 |
+
[[basic.scope.class]]).
|
| 652 |
+
|
| 653 |
+
[*Example 1*:
|
| 654 |
+
|
| 655 |
+
``` cpp
|
| 656 |
+
class process {
|
| 657 |
+
static process* run_chain;
|
| 658 |
+
static process* running;
|
| 659 |
+
};
|
| 660 |
+
|
| 661 |
+
process* process::running = get_main();
|
| 662 |
+
process* process::run_chain = running;
|
| 663 |
+
```
|
| 664 |
+
|
| 665 |
+
The static data member `run_chain` of class `process` is defined in
|
| 666 |
+
global scope; the notation `process::run_chain` specifies that the
|
| 667 |
+
member `run_chain` is a member of class `process` and in the scope of
|
| 668 |
+
class `process`. In the static data member definition, the *initializer*
|
| 669 |
+
expression refers to the static data member `running` of class
|
| 670 |
+
`process`.
|
| 671 |
+
|
| 672 |
+
— *end example*]
|
| 673 |
+
|
| 674 |
+
[*Note 1*:
|
| 675 |
+
|
| 676 |
+
Once the static data member has been defined, it exists even if no
|
| 677 |
+
objects of its class have been created.
|
| 678 |
+
|
| 679 |
+
[*Example 2*:
|
| 680 |
+
|
| 681 |
+
In the example above, `run_chain` and `running` exist even if no objects
|
| 682 |
+
of class `process` are created by the program.
|
| 683 |
+
|
| 684 |
+
— *end example*]
|
| 685 |
+
|
| 686 |
+
— *end note*]
|
| 687 |
+
|
| 688 |
+
If a non-volatile non-inline `const` static data member is of integral
|
| 689 |
+
or enumeration type, its declaration in the class definition can specify
|
| 690 |
+
a *brace-or-equal-initializer* in which every *initializer-clause* that
|
| 691 |
+
is an *assignment-expression* is a constant expression (
|
| 692 |
+
[[expr.const]]). The member shall still be defined in a namespace scope
|
| 693 |
+
if it is odr-used ([[basic.def.odr]]) in the program and the namespace
|
| 694 |
+
scope definition shall not contain an *initializer*. An inline static
|
| 695 |
+
data member may be defined in the class definition and may specify a
|
| 696 |
+
*brace-or-equal-initializer*. If the member is declared with the
|
| 697 |
+
`constexpr` specifier, it may be redeclared in namespace scope with no
|
| 698 |
+
initializer (this usage is deprecated; see [[depr.static_constexpr]]).
|
| 699 |
+
Declarations of other static data members shall not specify a
|
| 700 |
+
*brace-or-equal-initializer*.
|
| 701 |
+
|
| 702 |
+
[*Note 2*: There shall be exactly one definition of a static data
|
| 703 |
+
member that is odr-used ([[basic.def.odr]]) in a program; no diagnostic
|
| 704 |
+
is required. — *end note*]
|
| 705 |
+
|
| 706 |
+
Unnamed classes and classes contained directly or indirectly within
|
| 707 |
+
unnamed classes shall not contain static data members.
|
| 708 |
+
|
| 709 |
+
[*Note 3*: Static data members of a class in namespace scope have the
|
| 710 |
+
linkage of that class ([[basic.link]]). A local class cannot have
|
| 711 |
+
static data members ([[class.local]]). — *end note*]
|
| 712 |
+
|
| 713 |
+
Static data members are initialized and destroyed exactly like non-local
|
| 714 |
+
variables ([[basic.start.static]], [[basic.start.dynamic]],
|
| 715 |
+
[[basic.start.term]]).
|
| 716 |
+
|
| 717 |
+
A static data member shall not be `mutable` ([[dcl.stc]]).
|
| 718 |
+
|
| 719 |
+
### Bit-fields <a id="class.bit">[[class.bit]]</a>
|
| 720 |
+
|
| 721 |
+
A *member-declarator* of the form
|
| 722 |
+
|
| 723 |
+
specifies a bit-field; its length is set off from the bit-field name by
|
| 724 |
+
a colon. The optional *attribute-specifier-seq* appertains to the entity
|
| 725 |
+
being declared. The bit-field attribute is not part of the type of the
|
| 726 |
+
class member. The *constant-expression* shall be an integral constant
|
| 727 |
+
expression with a value greater than or equal to zero. The value of the
|
| 728 |
+
integral constant expression may be larger than the number of bits in
|
| 729 |
+
the object representation ([[basic.types]]) of the bit-field’s type; in
|
| 730 |
+
such cases the extra bits are used as padding bits and do not
|
| 731 |
+
participate in the value representation ([[basic.types]]) of the
|
| 732 |
+
bit-field. Allocation of bit-fields within a class object is
|
| 733 |
+
*implementation-defined*. Alignment of bit-fields is
|
| 734 |
+
*implementation-defined*. Bit-fields are packed into some addressable
|
| 735 |
+
allocation unit.
|
| 736 |
+
|
| 737 |
+
[*Note 1*: Bit-fields straddle allocation units on some machines and
|
| 738 |
+
not on others. Bit-fields are assigned right-to-left on some machines,
|
| 739 |
+
left-to-right on others. — *end note*]
|
| 740 |
+
|
| 741 |
+
A declaration for a bit-field that omits the *identifier* declares an
|
| 742 |
+
*unnamed bit-field*. Unnamed bit-fields are not members and cannot be
|
| 743 |
+
initialized.
|
| 744 |
+
|
| 745 |
+
[*Note 2*: An unnamed bit-field is useful for padding to conform to
|
| 746 |
+
externally-imposed layouts. — *end note*]
|
| 747 |
+
|
| 748 |
+
As a special case, an unnamed bit-field with a width of zero specifies
|
| 749 |
+
alignment of the next bit-field at an allocation unit boundary. Only
|
| 750 |
+
when declaring an unnamed bit-field may the value of the
|
| 751 |
+
*constant-expression* be equal to zero.
|
| 752 |
+
|
| 753 |
+
A bit-field shall not be a static member. A bit-field shall have
|
| 754 |
+
integral or enumeration type ([[basic.fundamental]]). A `bool` value
|
| 755 |
+
can successfully be stored in a bit-field of any nonzero size. The
|
| 756 |
+
address-of operator `&` shall not be applied to a bit-field, so there
|
| 757 |
+
are no pointers to bit-fields. A non-const reference shall not be bound
|
| 758 |
+
to a bit-field ([[dcl.init.ref]]).
|
| 759 |
+
|
| 760 |
+
[*Note 3*: If the initializer for a reference of type `const` `T&` is
|
| 761 |
+
an lvalue that refers to a bit-field, the reference is bound to a
|
| 762 |
+
temporary initialized to hold the value of the bit-field; the reference
|
| 763 |
+
is not bound to the bit-field directly. See
|
| 764 |
+
[[dcl.init.ref]]. — *end note*]
|
| 765 |
+
|
| 766 |
+
If the value `true` or `false` is stored into a bit-field of type `bool`
|
| 767 |
+
of any size (including a one bit bit-field), the original `bool` value
|
| 768 |
+
and the value of the bit-field shall compare equal. If the value of an
|
| 769 |
+
enumerator is stored into a bit-field of the same enumeration type and
|
| 770 |
+
the number of bits in the bit-field is large enough to hold all the
|
| 771 |
+
values of that enumeration type ([[dcl.enum]]), the original enumerator
|
| 772 |
+
value and the value of the bit-field shall compare equal.
|
| 773 |
+
|
| 774 |
+
[*Example 1*:
|
| 775 |
+
|
| 776 |
+
``` cpp
|
| 777 |
+
enum BOOL { FALSE=0, TRUE=1 };
|
| 778 |
+
struct A {
|
| 779 |
+
BOOL b:1;
|
| 780 |
+
};
|
| 781 |
+
A a;
|
| 782 |
+
void f() {
|
| 783 |
+
a.b = TRUE;
|
| 784 |
+
if (a.b == TRUE) // yields true
|
| 785 |
+
{ ... }
|
| 786 |
+
}
|
| 787 |
+
```
|
| 788 |
+
|
| 789 |
+
— *end example*]
|
| 790 |
+
|
| 791 |
+
### Nested class declarations <a id="class.nest">[[class.nest]]</a>
|
| 792 |
+
|
| 793 |
+
A class can be declared within another class. A class declared within
|
| 794 |
+
another is called a *nested* class. The name of a nested class is local
|
| 795 |
+
to its enclosing class. The nested class is in the scope of its
|
| 796 |
+
enclosing class.
|
| 797 |
+
|
| 798 |
+
[*Note 1*: See [[expr.prim]] for restrictions on the use of non-static
|
| 799 |
+
data members and non-static member functions. — *end note*]
|
| 800 |
+
|
| 801 |
+
[*Example 1*:
|
| 802 |
+
|
| 803 |
+
``` cpp
|
| 804 |
+
int x;
|
| 805 |
+
int y;
|
| 806 |
+
|
| 807 |
+
struct enclose {
|
| 808 |
+
int x;
|
| 809 |
+
static int s;
|
| 810 |
+
|
| 811 |
+
struct inner {
|
| 812 |
+
void f(int i) {
|
| 813 |
+
int a = sizeof(x); // OK: operand of sizeof is an unevaluated operand
|
| 814 |
+
x = i; // error: assign to enclose::x
|
| 815 |
+
s = i; // OK: assign to enclose::s
|
| 816 |
+
::x = i; // OK: assign to global x
|
| 817 |
+
y = i; // OK: assign to global y
|
| 818 |
+
}
|
| 819 |
+
void g(enclose* p, int i) {
|
| 820 |
+
p->x = i; // OK: assign to enclose::x
|
| 821 |
+
}
|
| 822 |
+
};
|
| 823 |
+
};
|
| 824 |
+
|
| 825 |
+
inner* p = 0; // error: inner not in scope
|
| 826 |
+
```
|
| 827 |
+
|
| 828 |
+
— *end example*]
|
| 829 |
+
|
| 830 |
+
Member functions and static data members of a nested class can be
|
| 831 |
+
defined in a namespace scope enclosing the definition of their class.
|
| 832 |
+
|
| 833 |
+
[*Example 2*:
|
| 834 |
+
|
| 835 |
+
``` cpp
|
| 836 |
+
struct enclose {
|
| 837 |
+
struct inner {
|
| 838 |
+
static int x;
|
| 839 |
+
void f(int i);
|
| 840 |
+
};
|
| 841 |
+
};
|
| 842 |
+
|
| 843 |
+
int enclose::inner::x = 1;
|
| 844 |
+
|
| 845 |
+
void enclose::inner::f(int i) { ... }
|
| 846 |
+
```
|
| 847 |
+
|
| 848 |
+
— *end example*]
|
| 849 |
+
|
| 850 |
+
If class `X` is defined in a namespace scope, a nested class `Y` may be
|
| 851 |
+
declared in class `X` and later defined in the definition of class `X`
|
| 852 |
+
or be later defined in a namespace scope enclosing the definition of
|
| 853 |
+
class `X`.
|
| 854 |
+
|
| 855 |
+
[*Example 3*:
|
| 856 |
+
|
| 857 |
+
``` cpp
|
| 858 |
+
class E {
|
| 859 |
+
class I1; // forward declaration of nested class
|
| 860 |
+
class I2;
|
| 861 |
+
class I1 { }; // definition of nested class
|
| 862 |
+
};
|
| 863 |
+
class E::I2 { }; // definition of nested class
|
| 864 |
+
```
|
| 865 |
+
|
| 866 |
+
— *end example*]
|
| 867 |
+
|
| 868 |
+
Like a member function, a friend function ([[class.friend]]) defined
|
| 869 |
+
within a nested class is in the lexical scope of that class; it obeys
|
| 870 |
+
the same rules for name binding as a static member function of that
|
| 871 |
+
class ([[class.static]]), but it has no special access rights to
|
| 872 |
+
members of an enclosing class.
|
| 873 |
+
|
| 874 |
+
### Nested type names <a id="class.nested.type">[[class.nested.type]]</a>
|
| 875 |
+
|
| 876 |
+
Type names obey exactly the same scope rules as other names. In
|
| 877 |
+
particular, type names defined within a class definition cannot be used
|
| 878 |
+
outside their class without qualification.
|
| 879 |
+
|
| 880 |
+
[*Example 1*:
|
| 881 |
+
|
| 882 |
+
``` cpp
|
| 883 |
+
struct X {
|
| 884 |
+
typedef int I;
|
| 885 |
+
class Y { ... };
|
| 886 |
+
I a;
|
| 887 |
+
};
|
| 888 |
+
|
| 889 |
+
I b; // error
|
| 890 |
+
Y c; // error
|
| 891 |
+
X::Y d; // OK
|
| 892 |
+
X::I e; // OK
|
| 893 |
+
```
|
| 894 |
+
|
| 895 |
+
— *end example*]
|
| 896 |
|