Ignore:
Timestamp:
Feb 26, 2024, 3:53:42 AM (2 years ago)
Author:
JiadaL <j82liang@…>
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.
Message:

Resolve conflict

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/background.tex

    r0522ebe ra4da45e  
    11\chapter{Background}
     2
     3
     4\section{C-Style Enum}
     5
     6The C-Style enumeration has the following syntax and semantics.
     7\begin{cfa}
     8enum Weekday { Monday, Tuesday, Wednesday, Thursday@ = 10@, Friday, Saturday, Sunday };
     9\end{cfa}
     10Enumerators 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@.
     11For 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@.
     12Initialization may occur in any order.
     13\begin{cfa}
     14enum Weekday { Thursday@ = 10@, Friday, Saturday, Sunday, Monday@ = 0@, Tuesday, Wednesday };
     15\end{cfa}
     16Note, 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}
     18enum Weekday {
     19        Thursday = 10, Friday, Saturday, Sunday,
     20        Monday = 0, Tuesday, Wednesday@,@ // terminating comma
     21};
     22\end{cfa}
     23This feature allow enumerator lines to be interchanged without moving a comma.\footnote{
     24A terminating comma appears in other C syntax, \eg the initializer list.}
     25Finally, C enumerators are \Newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type.
     26
     27In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerated values.
     28In 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.
     29Finally, 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}
     38int j = Wednesday;                                              $\C{// ERROR! Wednesday is not declared in this scope}$
     39\end{cfa}
     40The implicit conversion from @int@ to an enumeration type is an unnecessary source of error.
     41
     42It is common for C programmers to ``believe'' there are 3 equivalent forms of constant enumeration.
     43\begin{cfa}
     44#define Monday 0
     45static const int Monday = 0;
     46enum { Monday };
     47\end{cfa}
     48For @#define@, the programmer has to play compiler and explicitly manage the enumeration values;
     49furthermore, these are independent constants outside of any language type mechanism.
     50The same explicit management is true for @const@ declarations, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{
     51C 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.
     52Only 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.