Changeset a4da45e for doc/theses/jiada_liang_MMath/background.tex
- Timestamp:
- Feb 26, 2024, 3:53:42 AM (2 years ago)
- Branches:
- master, stuck-waitfor-destruct
- Children:
- 3f9a8d0
- Parents:
- 0522ebe (diff), 022bce0 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
doc/theses/jiada_liang_MMath/background.tex (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/background.tex
r0522ebe ra4da45e 1 1 \chapter{Background} 2 3 4 \section{C-Style Enum} 5 6 The C-Style enumeration has the following syntax and semantics. 7 \begin{cfa} 8 enum Weekday { Monday, Tuesday, Wednesday, Thursday@ = 10@, Friday, Saturday, Sunday }; 9 \end{cfa} 10 Enumerators without an explicitly designated constant value are \Newterm{auto-initialized} by the compiler: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@. 11 For example, @Monday@ to @Wednesday@ are implicitly assigned with constants @0@--@2@, @Thursday@ is explicitly set to constant @10@, and @Friday@ to @Sunday@ are implicitly assigned with constants @11@--@13@. 12 Initialization may occur in any order. 13 \begin{cfa} 14 enum Weekday { Thursday@ = 10@, Friday, Saturday, Sunday, Monday@ = 0@, Tuesday, Wednesday }; 15 \end{cfa} 16 Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma. 17 \begin{cfa} 18 enum Weekday { 19 Thursday = 10, Friday, Saturday, Sunday, 20 Monday = 0, Tuesday, Wednesday@,@ // terminating comma 21 }; 22 \end{cfa} 23 This feature allow enumerator lines to be interchanged without moving a comma.\footnote{ 24 A terminating comma appears in other C syntax, \eg the initializer list.} 25 Finally, C enumerators are \Newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type. 26 27 In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerated values. 28 In practice, since integral constants are used, which have type @int@ (unless qualified with a size suffix), C uses @int@ as the underlying type for enumeration variables. 29 Finally, there is an implicit bidirectional conversion between an enumeration and its integral type. 30 \begin{cfa} 31 { 32 enum Weekday { /* as above */ }; $\C{// enumerators implicitly projected into local scope}$ 33 Weekday weekday = Monday; $\C{// weekday == 0}$ 34 weekday = Friday; $\C{// weekday == 11}$ 35 int i = Sunday; $\C{// implicit conversion to int, i == 13}$ 36 weekday = 10000; $\C{// UNDEFINED! implicit conversion to Weekday}$ 37 } 38 int j = Wednesday; $\C{// ERROR! Wednesday is not declared in this scope}$ 39 \end{cfa} 40 The implicit conversion from @int@ to an enumeration type is an unnecessary source of error. 41 42 It is common for C programmers to ``believe'' there are 3 equivalent forms of constant enumeration. 43 \begin{cfa} 44 #define Monday 0 45 static const int Monday = 0; 46 enum { Monday }; 47 \end{cfa} 48 For @#define@, the programmer has to play compiler and explicitly manage the enumeration values; 49 furthermore, these are independent constants outside of any language type mechanism. 50 The same explicit management is true for @const@ declarations, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{ 51 C allows variable-length array-declarations (VLA), so this case does work, but it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} and immediate operands of assembler instructions. 52 Only the @enum@ form is managed by the compiler, is part of the language type-system, and works in all C constant-expression locations.
Note:
See TracChangeset
for help on using the changeset viewer.