I've witnessed today a form of enum
that was written as such, compiled under VS 2017:
enum ens en1, en2, en3;
and then it was used as such:
int n = en2;
So my question is, what form of enum
was it and why n
was later set to 0
?
Live example:
enum ens en1, en2, en3;
int main()
{
int n = en2;
}
compiles without warning with the default compiler options
In an unscoped enum, the scope is the surrounding scope; in a scoped enum, the scope is the enum-list itself. In a scoped enum, the list may be empty, which in effect defines a new integral type. By using this keyword in the declaration, you specify the enum is scoped, and an identifier must be provided.
In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex.
An enumeration type declaration gives the name of the (optional) enumeration tag. And, it defines the set of named integer identifiers (called the enumeration set, enumerator constants, enumerators, or members). A variable of the enumeration type stores one of the values of the enumeration set defined by that type.
Enumerations offer an easy way to work with sets of related constants. An enumeration, or Enum , is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.
This example is not correct because declaration of enum must either include underlaying type or a list of items. This also means that it can not be combined with variable declaration. fixed variant:
enum ens: int; // ens is a complete type at this point
enum ens en1, en2, en3; // en1 en2 en3 are global variables of enum ens type
// alternative "traditional" enum definition with empty list of items:
enum ens{} en1, en2, en3;
int n = en2;
makes n
equal to 0 because en2
is a global variable and is implicitly initialized with 0.
enum ens en1, en2, en3;
is no enum declaration. It is a variable declaration. It declares 3 variables of type enum ens
. They are used in the code later.
At the assignment of
int n = en2;
the value of en2
happens to have an enum value that corresponds to the integer value 0.
This is non-conforming code, it is an extension as we can see if we use /Za MSVC also rejects the code.
We can see this is ill-formed from dcl.enum#2:
The enumeration type declared with an enum-key of only enum is an unscoped enumeration, and its enumerators are unscoped enumerators. The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators. The optional identifier shall not be omitted in the declaration of a scoped enumeration. The type-specifier-seq of an enum-base shall name an integral type; any cv-qualification is ignored. An opaque-enum-declaration declaring an unscoped enumeration shall not omit the enum-base. ...
This wording was added n2764 which allowed forward declaration of enums as long as the underlying type was specified.
For more details as to why we are not allowed to forward declare enums without an underlying type see Why must an enumeration's size be provided when it is forward declared?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With