Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason to use enum to define a single constant in C++ code?

The typical way to define an integer constant to use inside a function is:

const int NumbeOfElements = 10;

the same for using within a class:

class Class {
...
    static const int NumberOfElements = 10;
};

It can then be used as a fixed-size array bound which means it is known at compile time.

Long ago compilers didn't support the latter syntax and that's why enums were used:

enum NumberOfElementsEnum { NumberOfElements = 10; }

Now with almost every widely used compiler supporting both the in-function const int and the in-class static const int syntax is there any reason to use the enum for this purpose?

like image 874
sharptooth Avatar asked Sep 04 '09 07:09

sharptooth


People also ask

What is the benefit of using enum to declare a constant in C?

The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability.

Should I use enum for constants?

Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.

What is the use of using enum to declare a constant?

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.

Why is enum better than constant?

Enums limit you to the required set of inputs whereas even if you use constant strings you still can use other String not part of your logic. This helps you to not make a mistake, to enter something out of the domain, while entering data and also improves the program readability.


2 Answers

The reason is mainly brevity. First of all, an enum can be anonymous:

 class foo {
    enum { bar = 1 };
 };

This effectively introduces bar as an integral constant. Note that the above is shorter than static const int.

Also, no-one could possibly write &bar if it's an enum member. If you do this:

 class foo {
    static const int bar = 1;
 }

and then the client of your class does this:

 printf("%p", &foo::bar);

then he will get a compile-time linker error that foo::bar is not defined (because, well, as an lvalue, it's not). In practice, with the Standard as it currently stands, anywhere bar is used where an integral constant expression is not required (i.e. where it is merely allowed), it requires an out-of-class definition of foo::bar. The places where such an expression is required are: enum initializers, case labels, array size in types (excepting new[]), and template arguments of integral types. Thus, using bar anywhere else technically requires a definition. See C++ Core Language Active Issue 712 for more info - there are no proposed resolutions as of yet.

In practice, most compilers these days are more lenient about this, and will let you get away with most "common sense" uses of static const int variables without requiring a definition. However, the corner cases may differ, however, so many consider it to be better to just use anonymous enum, for which everything is crystal clear, and there's no ambiguity at all.

like image 162
Pavel Minaev Avatar answered Nov 08 '22 23:11

Pavel Minaev


Defining static constants directly in the class definition is a later addition to C++ and many still stick to the older workaround of using an enum for that. There might even be a few older compilers still in use which don't support static constants directly defined in class definitions.

like image 33
sbi Avatar answered Nov 08 '22 23:11

sbi