Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would an enum declaration be marked const?

I saw this construct in the project I work:

const enum SomeEnum
{
    val0,
    val1,
    val2
};

What is the purpose of const here?

like image 657
Mircea Ispas Avatar asked Jul 23 '13 08:07

Mircea Ispas


People also ask

Can an enum be const?

A const Enum is the same as a normal Enum. Except that no Object is generated at compile time. Instead, the literal values are substituted where the const Enum is used.

Should enums be passed by const reference?

"Plain" enums and enum class objects both are of integral type, so the decision of passing by const reference or by value is just the same as if you did for other arguments of integral type.

What is enum constant in Java?

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comma.

Are enums constants C++?

An enumeration is a user-defined type that consists of a set of named integral constants that are known as enumerators. This article covers the ISO Standard C++ Language enum type and the scoped (or strongly-typed) enum class type which is introduced in C++11.


2 Answers

Nothing at all. Actually, according to G++ it is a compiler error:

error: qualifiers can only be specified for objects and functions

However, in C it is allowed, but useless. GCC says:

warning: useless type qualifier in empty declaration

The issue is that const only applies to objects (variables) and member functions, but not to basic types.

like image 180
rodrigo Avatar answered Oct 18 '22 09:10

rodrigo


It doesn't make a difference in your code, but it would in this case:

const enum SomeEnum
{
    val0,
    val1,
    val2
} VAL0 = val0;

Here, VAL0 would be a const variable (with the value val0). TBH though, it isn't of much use.

like image 27
Luchian Grigore Avatar answered Oct 18 '22 08:10

Luchian Grigore