views:

1088

answers:

12

Hello. I was just wondering why people use enums in C++ as constants while they can use const.

Thanks

+16  A: 

An enumeration implies a set of related constants, so the added information about the relationship must be useful in their model of the problem at hand.

duffymo
A: 

One reason is that const requires more typing:

enum { Val1, Val2, Val3 };

...versus...

const int Val1=0, Val2=1, Val3=2;
Head Geek
It's not the only reason. Also, there's no big difference in typing.
Loai Najati
+23  A: 

Enums are distinct types, so you can do type-oriented things like overloading with them:

enum Color { Red,Green,Blue };
enum Size { Big,Little };

void f( Color c ) {
}

void f( Size s ) {
}

int main() {
    f( Red );
    f( Big );
}
anon
+8  A: 

There's a historical reason too when dealing with template metaprogramming. Some compilers could use values from an enum, but not a static const int to instantiate a class.

template <int N>
struct foo
{
    enum { Value = foo<N-1>::Value + N };
};

template <>
struct foo<0>
{
    enum { Value = 0; }
};

Now you can do it the more sensible way:

template <int N>
struct foo
{
    static const int Value = foo<N-1>::Value + N;
};

template <>
struct foo<0>
{
    static const int Value = 0;
};

Another possible reason, is that a static const int may have memory reserved for it at runtime, whereas an enum is never going to have an actual memory location reserved for it, and will be dealt at compile time. See this related question.

Eclipse
+1 for the great template meta-programming example.
ceretullis
+10  A: 

Enums are more descriptive when used. Consider:

int f(int fg, int bg)

versus

 int f(COLOR fg, COLOR bg)

In addition, enums give a bit more type-safety, because

  • integers are not implicitly convertible to enum types
  • enum of one type is not implicitly convertible to enum of another type
ASk
+4  A: 

enums also can be used as a type name. So you can define a function that takes an enum as a parameter, which makes it more clear what kinds of values should be given as arguments to the function, as compared to having the values defined as const variables and the function accepting just "int" as an argument.

Consider:

enum my_new_fangled_type {
  baz = 0,
  meh = 1
};

void foo (my_new_fangled_type bar) // bar can be a value listed in the enum
{
   ...
}

versus:

int const baz = 0;
int const meh = 1;

void foo (int bar) // what are valid values for bar?
{
   ...
}
Dan Moulding
A: 

Using an enum documents the valid choices in a terse manner and allows the compiler to enforce them.

If they are using enum store global constants, like Pi, for example, then I don't know what their goal is.

dss539
enum is an integral type, I don't believe it can store a constant such as Pi without significant loss of precision :)
ASk
they might store 314159 and just div by 10^5 every time :P hopefully the example gives the right idea about what I meant by global constants, however.
dss539
+6  A: 

I like the automatic behavior that can be used with enums, for example:

enum {NONE, START, HEY, HO, LAST};

Then it is easy to loop until LAST, and when a new state (or whatever is represented) is added, the logic adapts.

for (int i = NONE; i < LAST; i++)
{
    // Do stuff...
}

Add something...

enum {NONE, START, HEY, WEE, HO, LAST};

The loop adapts...

FeatureCreep
+4  A: 

Before compiler vendors implemented the ISO/IEC 14882:1998 C++ standard, this code to define a constant in a class scope resulted in a compile error:

class Foo {
    static const int MAX_LEN = 80;
    ...
};

If the constant is an integer type, a kludgy work around is define it in an enum inside the class:

class Foo {
    enum {
        MAX_LEN = 80
    };
    ...
};
Jim Huang
Non-static constants can't be initialized that way. I believe you meant "static const int Val1".Also, there are drawbacks in using this method of in-class initialization - the symbol for Val1 is not guaranteed to be created.
ASk
+8  A: 

Bruce Eckel gives a reason in Thinking in C++:

In older versions of C++, static const was not supported inside classes. This meant that const was useless for constant expressions inside classes. However, people still wanted to do this so a typical solution (usually referred to as the “enum hack”) was to use an untagged enum with no instances. An enumeration must have all its values established at compile time, it’s local to the class, and its values are available for constant expressions. Thus, you will commonly see:

#include <iostream>
using namespace std;

class Bunch {
  enum { size = 1000 };
  int i[size];
};

int main() {
  cout << "sizeof(Bunch) = " << sizeof(Bunch) 
       << ", sizeof(i[1000]) = " 
       << sizeof(int[1000]) << endl;
}

[Edit]

I think it would be more fair to link Bruce Eckel's site: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html.

Bastien Léonard
+1  A: 

Some debuggers will show the enumeration name instead of its value when debugging. This can be very helpful. I know that I would rather see day_of_week = MONDAY than day_of_week = 1.

Trent
+2  A: 

It's partly because older compilers did not support the declaration of a true class constant

class C
{
  const int ARealConstant = 10;
};

so had to do this

class C
{
  enum { ARealConstant = 10 };
};

For this reason, many portable libraries continue to use this form.

The other reason is that enums can be used as a convenient syntactic device to organise class constants into those that are related, and those that are not

class DirectorySearcher
{
  enum options
  {
    showFiles = 0x01,
    showDirectories = 0x02,
    showLinks = 0x04,
  };
};

vs

class Integer
{
   enum { treatAsNumeric = true };
   enum { treatAsIntegral = true };
   enum { treatAsString = false };
};
DannyT