enum foo : const unsigned int { F, S, T }; void func() { foo p; p = F; }
The above compiles so the underlying type is not a const type?
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.
What is Enum in Java? A Java Enum is a Java type-class used to define collections of constants for your software according to your own needs. Each item in a Java enum is called a constant, an immutable variable — a value that cannot be changed.
Solution 1. No, enum is a type that defines named values, a constant variable or value can be any type. You use an enum variable to hold a value of the enum type, you use a const to define a variable that cannot change and whose value is known at compile time.
The const
qualifier is ignored in the specification of enum-base; which just expects an integral type to be used as the underlying type of the enumeration type, specifying const
(or volatile
) doesn't make much sense.
(emphasis mine)
colon (:), followed by a type-specifier-seq that names an integral type (if it is cv-qualified, qualifications are ignored) that will serve as the fixed underlying type for this enumeration type
From the standard, [dcl.enum]/2:
(emphasis mine)
The type-specifier-seq of an enum-base shall name an integral type; any cv-qualification is ignored.
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