root/trunk/docsrc/enum.dd

Revision 2140, 4.8 kB (checked in by walter, 1 year ago)

bugzilla 1351 Discrepancies in the language specification

  • Property svn:eol-style set to native
Line 
1 Ddoc
2
3 $(SPEC_S Enums - Enumerated Types,
4
5 $(GRAMMAR
6 $(GNAME EnumDeclaration):
7     $(B enum) $(I EnumTag) $(I EnumBody)
8     $(B enum) $(I EnumBody)
9     $(B enum) $(I EnumTag) $(B :) $(I EnumBaseType) $(I EnumBody)
10     $(B enum) $(B :) $(I EnumBaseType) $(I EnumBody)
11
12 $(GNAME EnumTag):
13     $(I Identifier)
14
15 $(GNAME EnumBaseType):
16     $(LINK2 declaration.html#Type, $(I Type))
17
18 $(GNAME EnumBody):
19     $(B ;)
20     $(B {) $(I EnumMembers) $(B })
21
22 $(GNAME EnumMembers):
23     $(I EnumMember)
24     $(I EnumMember) $(B ,)
25     $(I EnumMember) $(B ,) $(I EnumMembers)
26
27 $(GNAME EnumMember):
28     $(I Identifier)
29     $(I Identifier) $(B =) $(ASSIGNEXPRESSION)
30 $(V2          $(LINK2 declaration.html#Type, $(I Type)) $(B =) $(ASSIGNEXPRESSION))
31 )
32
33     $(P Enum declarations are used to define a group of constants.
34     They come in two forms:
35     )
36     $(OL
37     $(LI Named enums, which have an $(I EnumTag).)
38     $(LI Anonymous enums, which do not have an $(I EnumTag).)
39     )
40
41 <h2>Named Enums</h2>
42
43     $(P
44     Named enums are used to declare related
45     constants and group them by giving them a unique type.
46     The $(I EnumMembers)
47     are declared in the scope of the enum $(I EnumTag).
48     The enum $(I EnumTag) declares a new type, and all
49     the $(I EnumMembers) have that type.
50     )
51
52     $(P This defines a new type $(CODE X) which has values
53     $(CODE X.A=0), $(CODE X.B=1), $(CODE X.C=2):)
54
55 ------
56 enum X { A, B, C }  // named enum
57 ------
58
59 $(V1
60     $(P If the $(I EnumBaseType) is not explicitly set, it is set to
61     type $(CODE int).)
62 )
63 $(V2
64     $(P If the $(I EnumBaseType) is not explicitly set, and the first
65     $(I EnumMember) has an initializer, it is set to the type of that
66     initializer. Otherwise, it defaults to
67     type $(CODE int).)
68
69     $(P Named enum members may not have individual $(I Type)s.
70     )
71 )
72
73     $(P A named enum member can be implicitly cast to its $(I EnumBaseType),
74     but $(I EnumBaseType) types
75     cannot be implicitly cast to an enum type.
76     )
77
78     $(P The value of an $(I EnumMember) is given by its initializer.
79     If there is no initializer, it is given the value of the
80     previous $(I EnumMember) + 1. If it is the first $(I EnumMember),
81     it's value is 0.
82     )
83
84     $(P Enums must have at least one member.
85     )
86
87 <h3>Enum Default Initializer</h3>
88
89     $(P The $(CODE .init) property of an enum type is the value
90     of the first member of that enum.
91     This is also the default initializer for the enum type.
92     )
93
94 ------
95 enum X { A=3, B, C }
96 X x;        // x is initialized to 3
97 ------
98
99 <h3>Enum Properties</h3>
100
101     $(P Enum properties only exist for named enums.
102     )
103
104     $(TABLE1
105     $(CAPTION Named Enum Properties)
106     $(TR $(TD .init) $(TD First enum member value))
107     $(TR $(TD .min) $(TD Smallest value of enum))
108     $(TR $(TD .max) $(TD Largest value of enum))
109     $(TR $(TD .sizeof) $(TD Size of storage for an enumerated value))
110     )
111
112     $(P For example:)
113
114 ---
115 enum X { A=3, B, C }
116 X.min     // is X.A
117 X.max     // is X.C
118 X.sizeof  // is same as int.sizeof
119 ---
120
121 $(V2
122     $(P The $(I EnumBaseType) of named enums must support comparison
123     in order to compute the $(CODE .max) and $(CODE .min) properties.
124     )
125 )
126
127 <h2>Anonymous Enums</h2>
128
129     $(P If the enum $(I Identifier) is not present, then the enum
130     is an $(I anonymous enum), and the $(I EnumMembers) are declared
131     in the scope the $(I EnumDeclaration) appears in.
132     No new type is created; the $(I EnumMembers) have the type of the
133     $(I EnumBaseType).
134     )
135
136     The $(I EnumBaseType) is the underlying type of the enum.
137 $(V1
138     $(P It must be an integral type.
139     If omitted, it defaults to $(B int).
140     )
141 )
142 $(V2
143     $(P If omitted, the $(I EnumMembers) can have different types.
144     Those types are given by the first of:
145     )
146
147     $(OL
148     $(LI The $(I Type), if present.)
149     $(LI The type of the $(I AssignExpression), if present.)
150     $(LI The type of the previous $(I EnumMember), if present.)
151     $(LI $(CODE int))
152     )
153 )
154
155 ------
156 enum { A, B, C }    // anonymous enum
157 ------
158
159     $(P Defines the constants A=0, B=1, C=2, all of type int.)
160
161     $(P Enums must have at least one member.
162     )
163
164     $(P The value of an $(I EnumMember) is given by its initializer.
165     If there is no initializer, it is given the value of the
166     previous $(I EnumMember) + 1. If it is the first $(I EnumMember),
167     it's value is 0.
168     )
169
170 ------
171 enum { A, B = 5+7, C, D = 8+C, E }
172 ------
173
174     $(P Sets A=0, B=12, C=13, D=21, and E=22, all of type int.)
175
176 ---
177 enum : long { A = 3, B }
178 ---
179
180     $(P Sets A=3, B=4 all of type long.)
181
182 $(V2
183 ---
184 enum : string
185 {
186     A = "hello",
187     B = "betty",
188     C     // error, cannot add 1 to "betty"
189 }
190 ---
191
192 ---
193 enum
194 {
195     A = 1.2f,   // A is 1.2f of type float
196     B,          // B is 2.2f of type float
197     int C = 3,  // C is 3 of type int
198     D           // D is 4 of type int
199 }
200 ---
201
202 <h3>Manifest Constants</h3>
203
204     $(P If there is only one member of an anonymous enum, the { } can
205     be omitted:
206     )
207
208 ---
209 enum i = 4;      // i is 4 of type int
210 enum long l = 3; // l is 3 of type long
211 ---
212
213     $(P Such declarations are not lvalues, meaning their address
214     cannot be taken.)
215 )
216
217 )
218
219 Macros:
220     TITLE=Enums
221     WIKI=Enum
Note: See TracBrowser for help on using the browser.