Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the function of const specifier in enum types?

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?

like image 242
Thomas Avatar asked Jun 30 '19 10:06

Thomas


People also ask

What are enum constants?

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.

What is enum constant in C?

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 are enum constants in Java?

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.

What is the difference between enum and const?

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.


1 Answers

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.

like image 199
songyuanyao Avatar answered Sep 24 '22 03:09

songyuanyao