23

I saw this construct in the project I work:

const enum SomeEnum
{
    val0,
    val1,
    val2
};

What is the purpose of const here?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211

2 Answers2

20

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.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
15

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 7
    I think it *does* make difference: "it shouldn't compile". – Nawaz Jul 23 '13 at 08:51
  • @Nawaz I saw that it didn't compile, however that might just be the compiler. Is there anything in the rules against it? I've seen compilers both accepting and rejecting `static struct X{};`. – Luchian Grigore Jul 23 '13 at 08:52
  • 1
    Same thing as doing `const int;` or similar I would imagine. – chris Jul 23 '13 at 08:53
  • In C++, expressions can be `const` or `volatile`, not the types. It is like writing `const int;` as @chris said. – Nawaz Jul 23 '13 at 08:57
  • @Nawaz I agree it doesn't make sense, what I'm saying is I don't think this is addressed directly (that I know of), so it might be up to the compiler. – Luchian Grigore Jul 23 '13 at 09:01
  • in vc++ 2012 i am able to do ++VAL0; I don't understand how his const is undergoing to ++. Can anyone help me to understand ? – mahendiran.b Apr 22 '15 at 13:49