From Jason Turner

[dcl.enum]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp9zl4h7fk/{from.md → to.md} +91 -46
tmp/tmp9zl4h7fk/{from.md → to.md} RENAMED
@@ -1,9 +1,9 @@
1
  ## Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
2
 
3
  An enumeration is a distinct type ([[basic.compound]]) with named
4
- constants. Its name becomes an *enum-name*, within its scope.
5
 
6
  ``` bnf
7
  enum-name:
8
  identifier
9
  ```
@@ -14,18 +14,21 @@ enum-specifier:
14
  enum-head '{' enumerator-list ', }'
15
  ```
16
 
17
  ``` bnf
18
  enum-head:
19
- enum-key attribute-specifier-seqₒₚₜ identifierₒₚₜ enum-baseₒₚₜ
20
- enum-key attribute-specifier-seqₒₚₜ nested-name-specifier identifier
21
- enum-baseₒₚₜ
 
 
 
22
  ```
23
 
24
  ``` bnf
25
  opaque-enum-declaration:
26
- enum-key attribute-specifier-seqₒₚₜ identifier enum-baseₒₚₜ ';'
27
  ```
28
 
29
  ``` bnf
30
  enum-key:
31
  'enum'
@@ -50,61 +53,85 @@ enumerator-definition:
50
  enumerator '=' constant-expression
51
  ```
52
 
53
  ``` bnf
54
  enumerator:
55
- identifier
56
  ```
57
 
58
  The optional *attribute-specifier-seq* in the *enum-head* and the
59
  *opaque-enum-declaration* appertains to the enumeration; the attributes
60
  in that *attribute-specifier-seq* are thereafter considered attributes
61
  of the enumeration whenever it is named. A `:` following “`enum`
62
- *identifier*” is parsed as part of an *enum-base*. This resolves a
63
- potential ambiguity between the declaration of an enumeration with an
64
- *enum-base* and the declaration of an unnamed bit-field of enumeration
65
- type.
 
 
 
 
 
 
 
66
 
67
  ``` cpp
68
  struct S {
69
  enum E : int {};
70
  enum E : int {}; // error: redeclaration of enumeration
71
  };
72
  ```
73
 
 
 
 
 
 
 
 
74
  The enumeration type declared with an *enum-key* of only `enum` is an
75
- unscoped enumeration, and its *enumerator*s are *unscoped enumerators*.
76
- The *enum-key*s `enum class` and `enum struct` are semantically
77
- equivalent; an enumeration type declared with one of these is a *scoped
78
- enumeration*, and its *enumerator*s are *scoped enumerators*. The
79
- optional *identifier* shall not be omitted in the declaration of a
80
- scoped enumeration. The *type-specifier-seq* of an *enum-base* shall
81
- name an integral type; any cv-qualification is ignored. An
82
- *opaque-enum-declaration* declaring an unscoped enumeration shall not
83
- omit the *enum-base*. The identifiers in an *enumerator-list* are
84
- declared as constants, and can appear wherever constants are required.
85
- An *enumerator-definition* with `=` gives the associated *enumerator*
86
- the value indicated by the *constant-expression*. If the first
87
- *enumerator* has no *initializer*, the value of the corresponding
88
  constant is zero. An *enumerator-definition* without an *initializer*
89
  gives the *enumerator* the value obtained by increasing the value of the
90
  previous *enumerator* by one.
91
 
 
 
92
  ``` cpp
93
  enum { a, b, c=0 };
94
  enum { d, e, f=e+2 };
95
  ```
96
 
97
  defines `a`, `c`, and `d` to be zero, `b` and `e` to be `1`, and `f` to
98
  be `3`.
99
 
 
 
 
 
 
100
  An *opaque-enum-declaration* is either a redeclaration of an enumeration
101
- in the current scope or a declaration of a new enumeration. An
102
- enumeration declared by an *opaque-enum-declaration* has fixed
103
- underlying type and is a complete type. The list of enumerators can be
104
- provided in a later redeclaration with an *enum-specifier*. A scoped
105
- enumeration shall not be later redeclared as unscoped or with a
 
 
 
106
  different underlying type. An unscoped enumeration shall not be later
107
  redeclared as scoped and each redeclaration shall include an *enum-base*
108
  specifying the same underlying type as in the original declaration.
109
 
110
  If the *enum-key* is followed by a *nested-name-specifier*, the
@@ -113,12 +140,12 @@ declared directly in the class or namespace to which the
113
  *nested-name-specifier* refers (i.e., neither inherited nor introduced
114
  by a *using-declaration*), and the *enum-specifier* shall appear in a
115
  namespace enclosing the previous declaration.
116
 
117
  Each enumeration defines a type that is different from all other types.
118
- Each enumeration also has an underlying type. The underlying type can be
119
- explicitly specified using an *enum-base*. For a scoped enumeration
120
  type, the underlying type is `int` if it is not explicitly specified. In
121
  both of these cases, the underlying type is said to be *fixed*.
122
  Following the closing brace of an *enum-specifier*, each enumerator has
123
  the type of its enumeration. If the underlying type is fixed, the type
124
  of each enumerator prior to the closing brace is the underlying type and
@@ -158,29 +185,31 @@ unless the value of an enumerator cannot fit in an `int` or
158
  is as if the enumeration had a single enumerator with value 0.
159
 
160
  For an enumeration whose underlying type is fixed, the values of the
161
  enumeration are the values of the underlying type. Otherwise, for an
162
  enumeration where eₘin is the smallest enumerator and eₘax is the
163
- largest, the values of the enumeration are the values in the range bₘᵢₙ
164
- to bₘₐₓ, defined as follows: Let K be 1 for a two’s complement
165
- representation and 0 for a ones complement or sign-magnitude
166
- representation. bₘₐₓ is the smallest value greater than or equal to
167
- max(|eₘᵢₙ| - K, |eₘₐₓ|) and equal to $2^M-1$, where M is a non-negative
168
- integer. bₘᵢₙ is zero if eₘᵢₙ is non-negative and -(bₘₐₓ+K) otherwise.
169
  The size of the smallest bit-field large enough to hold all the values
170
- of the enumeration type is max(M,1) if bₘᵢₙ is zero and M+1 otherwise.
171
  It is possible to define an enumeration that has values not defined by
172
  any of its enumerators. If the *enumerator-list* is empty, the values of
173
  the enumeration are as if the enumeration had a single enumerator with
174
  value 0.[^4]
175
 
176
- Two enumeration types are *layout-compatible* if they have the same
177
- *underlying type*.
178
 
179
  The value of an enumerator or an object of an unscoped enumeration type
180
  is converted to an integer by integral promotion ([[conv.prom]]).
181
 
 
 
182
  ``` cpp
183
  enum color { red, yellow, green=20, blue };
184
  color col = red;
185
  color* cp = &col;
186
  if (*cp == blue) // ...
@@ -192,15 +221,12 @@ type. The possible values of an object of type `color` are `red`,
192
  `yellow`, `green`, `blue`; these values can be converted to the integral
193
  values `0`, `1`, `20`, and `21`. Since enumerations are distinct types,
194
  objects of type `color` can be assigned only values of type `color`.
195
 
196
  ``` cpp
197
- color c = 1; // error: type mismatch,
198
- // no conversion from int to color
199
-
200
- int i = yellow; // OK: yellow converted to integral value 1
201
- // integral promotion
202
  ```
203
 
204
  Note that this implicit `enum` to `int` conversion is not provided for a
205
  scoped enumeration:
206
 
@@ -209,15 +235,18 @@ enum class Col { red, yellow, green };
209
  int x = Col::red; // error: no Col to int conversion
210
  Col y = Col::red;
211
  if (y) { } // error: no Col to bool conversion
212
  ```
213
 
 
 
214
  Each *enum-name* and each unscoped *enumerator* is declared in the scope
215
  that immediately contains the *enum-specifier*. Each scoped *enumerator*
216
  is declared in the scope of the enumeration. These names obey the scope
217
- rules defined for all names in ([[basic.scope]]) and (
218
- [[basic.lookup]]).
 
219
 
220
  ``` cpp
221
  enum direction { left='l', right='r' };
222
 
223
  void g() {
@@ -233,14 +262,18 @@ void h() {
233
  a = high; // error: high not in scope
234
  a = altitude::low; // OK
235
  }
236
  ```
237
 
 
 
238
  An enumerator declared in class scope can be referred to using the class
239
  member access operators (`::`, `.` (dot) and `->` (arrow)), see 
240
  [[expr.ref]].
241
 
 
 
242
  ``` cpp
243
  struct X {
244
  enum direction { left='l', right='r' };
245
  int f(int i) { return i==left ? 0 : i==right ? 1 : 2; }
246
  };
@@ -253,5 +286,17 @@ void g(X* p) {
253
  i = p->f(p->left); // OK
254
  // ...
255
  }
256
  ```
257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ## Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
2
 
3
  An enumeration is a distinct type ([[basic.compound]]) with named
4
+ constants. Its name becomes an *enum-name* within its scope.
5
 
6
  ``` bnf
7
  enum-name:
8
  identifier
9
  ```
 
14
  enum-head '{' enumerator-list ', }'
15
  ```
16
 
17
  ``` bnf
18
  enum-head:
19
+ enum-key attribute-specifier-seqₒₚₜ enum-head-nameₒₚₜ enum-baseₒₚₜ
20
+ ```
21
+
22
+ ``` bnf
23
+ enum-head-name:
24
+ nested-name-specifierₒₚₜ identifier
25
  ```
26
 
27
  ``` bnf
28
  opaque-enum-declaration:
29
+ enum-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier enum-baseₒₚₜ ';'
30
  ```
31
 
32
  ``` bnf
33
  enum-key:
34
  'enum'
 
53
  enumerator '=' constant-expression
54
  ```
55
 
56
  ``` bnf
57
  enumerator:
58
+ identifier attribute-specifier-seqₒₚₜ
59
  ```
60
 
61
  The optional *attribute-specifier-seq* in the *enum-head* and the
62
  *opaque-enum-declaration* appertains to the enumeration; the attributes
63
  in that *attribute-specifier-seq* are thereafter considered attributes
64
  of the enumeration whenever it is named. A `:` following “`enum`
65
+ *nested-name-specifier*ₒₚₜ *identifier*” within the
66
+ *decl-specifier-seq* of a *member-declaration* is parsed as part of an
67
+ *enum-base*.
68
+
69
+ [*Note 1*:
70
+
71
+ This resolves a potential ambiguity between the declaration of an
72
+ enumeration with an *enum-base* and the declaration of an unnamed
73
+ bit-field of enumeration type.
74
+
75
+ [*Example 1*:
76
 
77
  ``` cpp
78
  struct S {
79
  enum E : int {};
80
  enum E : int {}; // error: redeclaration of enumeration
81
  };
82
  ```
83
 
84
+ — *end example*]
85
+
86
+ — *end note*]
87
+
88
+ If an *opaque-enum-declaration* contains a *nested-name-specifier*, the
89
+ declaration shall be an explicit specialization ([[temp.expl.spec]]).
90
+
91
  The enumeration type declared with an *enum-key* of only `enum` is an
92
+ *unscoped enumeration*, and its *enumerator*s are *unscoped
93
+ enumerators*. The *enum-key*s `enum class` and `enum struct` are
94
+ semantically equivalent; an enumeration type declared with one of these
95
+ is a *scoped enumeration*, and its *enumerator*s are *scoped
96
+ enumerators*. The optional *identifier* shall not be omitted in the
97
+ declaration of a scoped enumeration. The *type-specifier-seq* of an
98
+ *enum-base* shall name an integral type; any cv-qualification is
99
+ ignored. An *opaque-enum-declaration* declaring an unscoped enumeration
100
+ shall not omit the *enum-base*. The identifiers in an *enumerator-list*
101
+ are declared as constants, and can appear wherever constants are
102
+ required. An *enumerator-definition* with `=` gives the associated
103
+ *enumerator* the value indicated by the *constant-expression*. If the
104
+ first *enumerator* has no *initializer*, the value of the corresponding
105
  constant is zero. An *enumerator-definition* without an *initializer*
106
  gives the *enumerator* the value obtained by increasing the value of the
107
  previous *enumerator* by one.
108
 
109
+ [*Example 2*:
110
+
111
  ``` cpp
112
  enum { a, b, c=0 };
113
  enum { d, e, f=e+2 };
114
  ```
115
 
116
  defines `a`, `c`, and `d` to be zero, `b` and `e` to be `1`, and `f` to
117
  be `3`.
118
 
119
+ — *end example*]
120
+
121
+ The optional *attribute-specifier-seq* in an *enumerator* appertains to
122
+ that enumerator.
123
+
124
  An *opaque-enum-declaration* is either a redeclaration of an enumeration
125
+ in the current scope or a declaration of a new enumeration.
126
+
127
+ [*Note 2*: An enumeration declared by an *opaque-enum-declaration* has
128
+ fixed underlying type and is a complete type. The list of enumerators
129
+ can be provided in a later redeclaration with an
130
+ *enum-specifier*. — *end note*]
131
+
132
+ A scoped enumeration shall not be later redeclared as unscoped or with a
133
  different underlying type. An unscoped enumeration shall not be later
134
  redeclared as scoped and each redeclaration shall include an *enum-base*
135
  specifying the same underlying type as in the original declaration.
136
 
137
  If the *enum-key* is followed by a *nested-name-specifier*, the
 
140
  *nested-name-specifier* refers (i.e., neither inherited nor introduced
141
  by a *using-declaration*), and the *enum-specifier* shall appear in a
142
  namespace enclosing the previous declaration.
143
 
144
  Each enumeration defines a type that is different from all other types.
145
+ Each enumeration also has an *underlying type*. The underlying type can
146
+ be explicitly specified using an *enum-base*. For a scoped enumeration
147
  type, the underlying type is `int` if it is not explicitly specified. In
148
  both of these cases, the underlying type is said to be *fixed*.
149
  Following the closing brace of an *enum-specifier*, each enumerator has
150
  the type of its enumeration. If the underlying type is fixed, the type
151
  of each enumerator prior to the closing brace is the underlying type and
 
185
  is as if the enumeration had a single enumerator with value 0.
186
 
187
  For an enumeration whose underlying type is fixed, the values of the
188
  enumeration are the values of the underlying type. Otherwise, for an
189
  enumeration where eₘin is the smallest enumerator and eₘax is the
190
+ largest, the values of the enumeration are the values in the range bₘin
191
+ to bₘax, defined as follows: Let K be 1 for a two’s complement
192
+ representation and 0 for a ones’ complement or sign-magnitude
193
+ representation. bₘax is the smallest value greater than or equal to
194
+ max(|eₘin| - K, |eₘax|) and equal to $2^M-1$, where M is a non-negative
195
+ integer. bₘin is zero if eₘin is non-negative and -(bₘax+K) otherwise.
196
  The size of the smallest bit-field large enough to hold all the values
197
+ of the enumeration type is max(M,1) if bₘin is zero and M+1 otherwise.
198
  It is possible to define an enumeration that has values not defined by
199
  any of its enumerators. If the *enumerator-list* is empty, the values of
200
  the enumeration are as if the enumeration had a single enumerator with
201
  value 0.[^4]
202
 
203
+ Two enumeration types are *layout-compatible enumerations* if they have
204
+ the same underlying type.
205
 
206
  The value of an enumerator or an object of an unscoped enumeration type
207
  is converted to an integer by integral promotion ([[conv.prom]]).
208
 
209
+ [*Example 3*:
210
+
211
  ``` cpp
212
  enum color { red, yellow, green=20, blue };
213
  color col = red;
214
  color* cp = &col;
215
  if (*cp == blue) // ...
 
221
  `yellow`, `green`, `blue`; these values can be converted to the integral
222
  values `0`, `1`, `20`, and `21`. Since enumerations are distinct types,
223
  objects of type `color` can be assigned only values of type `color`.
224
 
225
  ``` cpp
226
+ color c = 1; // error: type mismatch, no conversion from int to color
227
+ int i = yellow; // OK: yellow converted to integral value 1, integral promotion
 
 
 
228
  ```
229
 
230
  Note that this implicit `enum` to `int` conversion is not provided for a
231
  scoped enumeration:
232
 
 
235
  int x = Col::red; // error: no Col to int conversion
236
  Col y = Col::red;
237
  if (y) { } // error: no Col to bool conversion
238
  ```
239
 
240
+ — *end example*]
241
+
242
  Each *enum-name* and each unscoped *enumerator* is declared in the scope
243
  that immediately contains the *enum-specifier*. Each scoped *enumerator*
244
  is declared in the scope of the enumeration. These names obey the scope
245
+ rules defined for all names in  [[basic.scope]] and  [[basic.lookup]].
246
+
247
+ [*Example 4*:
248
 
249
  ``` cpp
250
  enum direction { left='l', right='r' };
251
 
252
  void g() {
 
262
  a = high; // error: high not in scope
263
  a = altitude::low; // OK
264
  }
265
  ```
266
 
267
+ — *end example*]
268
+
269
  An enumerator declared in class scope can be referred to using the class
270
  member access operators (`::`, `.` (dot) and `->` (arrow)), see 
271
  [[expr.ref]].
272
 
273
+ [*Example 5*:
274
+
275
  ``` cpp
276
  struct X {
277
  enum direction { left='l', right='r' };
278
  int f(int i) { return i==left ? 0 : i==right ? 1 : 2; }
279
  };
 
286
  i = p->f(p->left); // OK
287
  // ...
288
  }
289
  ```
290
 
291
+ — *end example*]
292
+
293
+ If an *enum-head* contains a *nested-name-specifier*, the
294
+ *enum-specifier* shall refer to an enumeration that was previously
295
+ declared directly in the class or namespace to which the
296
+ *nested-name-specifier* refers, or in an element of the inline namespace
297
+ set ([[namespace.def]]) of that namespace (i.e., not merely inherited
298
+ or introduced by a *using-declaration*), and the *enum-specifier* shall
299
+ appear in a namespace enclosing the previous declaration. In such cases,
300
+ the *nested-name-specifier* of the *enum-head* of the definition shall
301
+ not begin with a *decltype-specifier*.
302
+