From Jason Turner

[enum]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpiaixi_7w/{from.md → to.md} +357 -0
tmp/tmpiaixi_7w/{from.md → to.md} RENAMED
@@ -0,0 +1,357 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Enumerations <a id="enum">[[enum]]</a>
2
+
3
+ ### Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
4
+
5
+ An enumeration is a distinct type [[basic.compound]] with named
6
+ constants. Its name becomes an *enum-name* within its scope.
7
+
8
+ ``` bnf
9
+ enum-name:
10
+ identifier
11
+ ```
12
+
13
+ ``` bnf
14
+ enum-specifier:
15
+ enum-head '{' enumerator-listₒₚₜ '}'
16
+ enum-head '{' enumerator-list ',' '}'
17
+ ```
18
+
19
+ ``` bnf
20
+ enum-head:
21
+ enum-key attribute-specifier-seqₒₚₜ enum-head-nameₒₚₜ enum-baseₒₚₜ
22
+ ```
23
+
24
+ ``` bnf
25
+ enum-head-name:
26
+ nested-name-specifierₒₚₜ identifier
27
+ ```
28
+
29
+ ``` bnf
30
+ opaque-enum-declaration:
31
+ enum-key attribute-specifier-seqₒₚₜ enum-head-name enum-baseₒₚₜ ';'
32
+ ```
33
+
34
+ ``` bnf
35
+ enum-key:
36
+ enum
37
+ enum class
38
+ enum struct
39
+ ```
40
+
41
+ ``` bnf
42
+ enum-base:
43
+ ':' type-specifier-seq
44
+ ```
45
+
46
+ ``` bnf
47
+ enumerator-list:
48
+ enumerator-definition
49
+ enumerator-list ',' enumerator-definition
50
+ ```
51
+
52
+ ``` bnf
53
+ enumerator-definition:
54
+ enumerator
55
+ enumerator '=' constant-expression
56
+ ```
57
+
58
+ ``` bnf
59
+ enumerator:
60
+ identifier attribute-specifier-seqₒₚₜ
61
+ ```
62
+
63
+ The optional *attribute-specifier-seq* in the *enum-head* and the
64
+ *opaque-enum-declaration* appertains to the enumeration; the attributes
65
+ in that *attribute-specifier-seq* are thereafter considered attributes
66
+ of the enumeration whenever it is named. A `:` following “`enum`
67
+ *nested-name-specifier*ₒₚₜ *identifier*” within the
68
+ *decl-specifier-seq* of a *member-declaration* is parsed as part of an
69
+ *enum-base*.
70
+
71
+ [*Note 1*:
72
+
73
+ This resolves a potential ambiguity between the declaration of an
74
+ enumeration with an *enum-base* and the declaration of an unnamed
75
+ bit-field of enumeration type.
76
+
77
+ [*Example 1*:
78
+
79
+ ``` cpp
80
+ struct S {
81
+ enum E : int {};
82
+ enum E : int {}; // error: redeclaration of enumeration
83
+ };
84
+ ```
85
+
86
+ — *end example*]
87
+
88
+ — *end note*]
89
+
90
+ If the *enum-head-name* of an *opaque-enum-declaration* contains a
91
+ *nested-name-specifier*, the declaration shall be an explicit
92
+ specialization [[temp.expl.spec]].
93
+
94
+ The enumeration type declared with an *enum-key* of only `enum` is an
95
+ *unscoped enumeration*, and its *enumerator*s are *unscoped
96
+ enumerators*. The *enum-key*s `enum class` and `enum struct` are
97
+ semantically equivalent; an enumeration type declared with one of these
98
+ is a *scoped enumeration*, and its *enumerator*s are *scoped
99
+ enumerators*. The optional *enum-head-name* shall not be omitted in the
100
+ declaration of a scoped enumeration. The *type-specifier-seq* of an
101
+ *enum-base* shall name an integral type; any cv-qualification is
102
+ ignored. An *opaque-enum-declaration* declaring an unscoped enumeration
103
+ shall not omit the *enum-base*. The identifiers in an *enumerator-list*
104
+ are declared as constants, and can appear wherever constants are
105
+ required. An *enumerator-definition* with `=` gives the associated
106
+ *enumerator* the value indicated by the *constant-expression*. If the
107
+ first *enumerator* has no *initializer*, the value of the corresponding
108
+ constant is zero. An *enumerator-definition* without an *initializer*
109
+ gives the *enumerator* the value obtained by increasing the value of the
110
+ previous *enumerator* by one.
111
+
112
+ [*Example 2*:
113
+
114
+ ``` cpp
115
+ enum { a, b, c=0 };
116
+ enum { d, e, f=e+2 };
117
+ ```
118
+
119
+ defines `a`, `c`, and `d` to be zero, `b` and `e` to be `1`, and `f` to
120
+ be `3`.
121
+
122
+ — *end example*]
123
+
124
+ The optional *attribute-specifier-seq* in an *enumerator* appertains to
125
+ that enumerator.
126
+
127
+ An *opaque-enum-declaration* is either a redeclaration of an enumeration
128
+ in the current scope or a declaration of a new enumeration.
129
+
130
+ [*Note 2*: An enumeration declared by an *opaque-enum-declaration* has
131
+ a fixed underlying type and is a complete type. The list of enumerators
132
+ can be provided in a later redeclaration with an
133
+ *enum-specifier*. — *end note*]
134
+
135
+ A scoped enumeration shall not be later redeclared as unscoped or with a
136
+ different underlying type. An unscoped enumeration shall not be later
137
+ redeclared as scoped and each redeclaration shall include an *enum-base*
138
+ specifying the same underlying type as in the original declaration.
139
+
140
+ If an *enum-head-name* contains a *nested-name-specifier*, it shall not
141
+ begin with a *decltype-specifier* and the enclosing *enum-specifier* or
142
+ *opaque-enum-declaration* shall refer to an enumeration that was
143
+ previously declared directly in the class or namespace to which the
144
+ *nested-name-specifier* refers, or in an element of the inline namespace
145
+ set [[namespace.def]] of that namespace (i.e., neither inherited nor
146
+ introduced by a *using-declaration*), and the *enum-specifier* or
147
+ *opaque-enum-declaration* shall appear in a namespace enclosing the
148
+ previous declaration.
149
+
150
+ Each enumeration defines a type that is different from all other types.
151
+ Each enumeration also has an *underlying type*. The underlying type can
152
+ be explicitly specified using an *enum-base*. For a scoped enumeration
153
+ type, the underlying type is `int` if it is not explicitly specified. In
154
+ both of these cases, the underlying type is said to be *fixed*.
155
+ Following the closing brace of an *enum-specifier*, each enumerator has
156
+ the type of its enumeration. If the underlying type is fixed, the type
157
+ of each enumerator prior to the closing brace is the underlying type and
158
+ the *constant-expression* in the *enumerator-definition* shall be a
159
+ converted constant expression of the underlying type [[expr.const]]. If
160
+ the underlying type is not fixed, the type of each enumerator prior to
161
+ the closing brace is determined as follows:
162
+
163
+ - If an initializer is specified for an enumerator, the
164
+ *constant-expression* shall be an integral constant expression
165
+ [[expr.const]]. If the expression has unscoped enumeration type, the
166
+ enumerator has the underlying type of that enumeration type, otherwise
167
+ it has the same type as the expression.
168
+ - If no initializer is specified for the first enumerator, its type is
169
+ an unspecified signed integral type.
170
+ - Otherwise the type of the enumerator is the same as that of the
171
+ preceding enumerator unless the incremented value is not representable
172
+ in that type, in which case the type is an unspecified integral type
173
+ sufficient to contain the incremented value. If no such type exists,
174
+ the program is ill-formed.
175
+
176
+ An enumeration whose underlying type is fixed is an incomplete type from
177
+ its point of declaration [[basic.scope.pdecl]] to immediately after its
178
+ *enum-base* (if any), at which point it becomes a complete type. An
179
+ enumeration whose underlying type is not fixed is an incomplete type
180
+ from its point of declaration to immediately after the closing `}` of
181
+ its *enum-specifier*, at which point it becomes a complete type.
182
+
183
+ For an enumeration whose underlying type is not fixed, the underlying
184
+ type is an integral type that can represent all the enumerator values
185
+ defined in the enumeration. If no integral type can represent all the
186
+ enumerator values, the enumeration is ill-formed. It is
187
+ *implementation-defined* which integral type is used as the underlying
188
+ type except that the underlying type shall not be larger than `int`
189
+ unless the value of an enumerator cannot fit in an `int` or
190
+ `unsigned int`. If the *enumerator-list* is empty, the underlying type
191
+ is as if the enumeration had a single enumerator with value 0.
192
+
193
+ For an enumeration whose underlying type is fixed, the values of the
194
+ enumeration are the values of the underlying type. Otherwise, the values
195
+ of the enumeration are the values representable by a hypothetical
196
+ integer type with minimal width M such that all enumerators can be
197
+ represented. The width of the smallest bit-field large enough to hold
198
+ all the values of the enumeration type is M. It is possible to define an
199
+ enumeration that has values not defined by any of its enumerators. If
200
+ the *enumerator-list* is empty, the values of the enumeration are as if
201
+ the enumeration had a single enumerator with value 0.[^9]
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) // ...
216
+ ```
217
+
218
+ makes `color` a type describing various colors, and then declares `col`
219
+ as an object of that type, and `cp` as a pointer to an object of that
220
+ type. The possible values of an object of type `color` are `red`,
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
+
233
+ ``` cpp
234
+ enum class Col { red, yellow, green };
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. An unnamed enumeration that
245
+ does not have a typedef name for linkage purposes [[dcl.typedef]] and
246
+ that has a first enumerator is denoted, for linkage purposes
247
+ [[basic.link]], by its underlying type and its first enumerator; such an
248
+ enumeration is said to have an enumerator as a name for linkage
249
+ purposes. These names obey the scope rules defined for all names in 
250
+ [[basic.scope]] and  [[basic.lookup]].
251
+
252
+ [*Note 3*: Each unnamed enumeration with no enumerators is a distinct
253
+ type. — *end note*]
254
+
255
+ [*Example 4*:
256
+
257
+ ``` cpp
258
+ enum direction { left='l', right='r' };
259
+
260
+ void g() {
261
+ direction d; // OK
262
+ d = left; // OK
263
+ d = direction::right; // OK
264
+ }
265
+
266
+ enum class altitude { high='h', low='l' };
267
+
268
+ void h() {
269
+ altitude a; // OK
270
+ a = high; // error: high not in scope
271
+ a = altitude::low; // OK
272
+ }
273
+ ```
274
+
275
+ — *end example*]
276
+
277
+ An enumerator declared in class scope can be referred to using the class
278
+ member access operators (`::`, `.` (dot) and `->` (arrow)), see 
279
+ [[expr.ref]].
280
+
281
+ [*Example 5*:
282
+
283
+ ``` cpp
284
+ struct X {
285
+ enum direction { left='l', right='r' };
286
+ int f(int i) { return i==left ? 0 : i==right ? 1 : 2; }
287
+ };
288
+
289
+ void g(X* p) {
290
+ direction d; // error: direction not in scope
291
+ int i;
292
+ i = p->f(left); // error: left not in scope
293
+ i = p->f(X::right); // OK
294
+ i = p->f(p->left); // OK
295
+ // ...
296
+ }
297
+ ```
298
+
299
+ — *end example*]
300
+
301
+ ### The `using enum` declaration <a id="enum.udecl">[[enum.udecl]]</a>
302
+
303
+ ``` bnf
304
+ using-enum-declaration:
305
+ 'using' elaborated-enum-specifier ';'
306
+ ```
307
+
308
+ The *elaborated-enum-specifier* shall not name a dependent type and the
309
+ type shall have a reachable *enum-specifier*.
310
+
311
+ A *using-enum-declaration* introduces the enumerator names of the named
312
+ enumeration as if by a *using-declaration* for each enumerator.
313
+
314
+ [*Note 1*:
315
+
316
+ A *using-enum-declaration* in class scope adds the enumerators of the
317
+ named enumeration as members to the scope. This means they are
318
+ accessible for member lookup.
319
+
320
+ [*Example 1*:
321
+
322
+ ``` cpp
323
+ enum class fruit { orange, apple };
324
+ struct S {
325
+ using enum fruit; // OK, introduces orange and apple into S
326
+ };
327
+ void f() {
328
+ S s;
329
+ s.orange; // OK, names fruit::orange
330
+ S::orange; // OK, names fruit::orange
331
+ }
332
+ ```
333
+
334
+ — *end example*]
335
+
336
+ — *end note*]
337
+
338
+ [*Note 2*:
339
+
340
+ Two *using-enum-declaration*s that introduce two enumerators of the same
341
+ name conflict.
342
+
343
+ [*Example 2*:
344
+
345
+ ``` cpp
346
+ enum class fruit { orange, apple };
347
+ enum class color { red, orange };
348
+ void f() {
349
+ using enum fruit; // OK
350
+ using enum color; // error: color::orange and fruit::orange conflict
351
+ }
352
+ ```
353
+
354
+ — *end example*]
355
+
356
+ — *end note*]
357
+