Under the new standard, we can forward declare an enum
. An enum
forward declaration must specify (implicitly or explicitly) the underlying size of the enum
:
// forward declaration of unscoped enum named intValues
enum intValues : unsigned long long; // unscoped, must specify a type
enum class open_modes; // scoped enums can use int by default
Because there is no default size for an unscoped enum
, every declaration must include the size of that enum
. We can declare a scoped enum
without specifying a size, in which case the size is implicitly defined as int
.
As with any declaration, all the declarations and the definition of a given enum
must match one another. In the case of enum
s, this requirement means that the size of the enum
must be the same across all declarations and the enum
definition. Moreover, we cannot declare a name as an unscoped enum
in one context and redeclare it as a scoped enum
later:
// error: declarations and definition must agree whether the enum is scoped or unscoped
enum class intValues;
enum intValues; // error: intValues previously declared as scoped enum
enum intValues : long; // error: intValues previously declared as int