\chapter{Background}


\section{C-Style Enum}

The C-Style enumeration has the following syntax and semantics.
\begin{cfa}
enum Weekday { Monday, Tuesday, Wednesday, Thursday@ = 10@, Friday, Saturday, Sunday };
\end{cfa}
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@.
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@.
Initialization may occur in any order.
\begin{cfa}
enum Weekday { Thursday@ = 10@, Friday, Saturday, Sunday, Monday@ = 0@, Tuesday, Wednesday };
\end{cfa}
Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma.
\begin{cfa}
enum Weekday {
	Thursday = 10, Friday, Saturday, Sunday,
	Monday = 0, Tuesday, Wednesday@,@ // terminating comma
};
\end{cfa}
This feature allow enumerator lines to be interchanged without moving a comma.\footnote{
A terminating comma appears in other C syntax, e.g., the initializer list.}
Finally, C enumerators are \Newterm{unscoped}, i.e., enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type.

In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerated values.
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.
Finally, there is an implicit bidirectional conversion between an enumeration and integral types.
\begin{cfa}
{
	enum Weekday { /* as above */ };	$\C{// enumerators implicitly projected into local scope}$
	Weekday weekday = Monday;			$\C{// weekday == 0}$
	weekday = Friday;					$\C{// weekday == 11}$
	int i = Sunday;						$\C{// implicit conversion to int, i == 13}$
	weekday = 10000;					$\C{// UNDEFINED! implicit conversion to Weekday}$
}
int j = Wednesday;						$\C{// ERROR! Wednesday is not declared in this scope}$
\end{cfa}
The implicit conversion from @int@ to an enumeration type is an unnecessary source of error.

It is common for C programmers to ``believe'' there are 3 equivalent forms of constant enumeration.
\begin{cfa}
#define Monday 0
static const int Monday = 0;
enum { Monday };
\end{cfa}
For @#define@, the programmer has to play compiler and explicitly manage the enumeration values;
furthermore, these are independent constants outside of any language type mechanism.
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{
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.
Only the @enum@ form is managed by the compiler, is part of the language type-system, and works in all C constant-expression locations.
