Quick check on enums with values

Started by
2 comments, last by Alberth 6 years, 11 months ago

Hi all,

I've noticed that I was using typedef enum and enum without typedef, mixed together.
​Some research/ reading tought me that there's really no difference when you use a c++ compiler, in either way I can simply declare variables without nothing 'enum' (in C it would be this exact difference I believe).

No my question, so far I've explicitly assigned values to my enums, like this:


enum CR_MOUSE_BUTTON
{
	CR_MOUSE_BUTTON_L			= 0,
	CR_MOUSE_BUTTON_M			= 1,
	CR_MOUSE_BUTTON_R			= 2
};

But from what I understand, is that this results in exactly the same as this:


enum CR_MOUSE_BUTTON
{
	CR_MOUSE_BUTTON_L,
	CR_MOUSE_BUTTON_M,
	CR_MOUSE_BUTTON_R,
};

Because the compiler will automatically assign values, going through a list of possible standard types to use (int's, longs etc.).
​I also believe that this also works:


enum CR_MOUSE_BUTTON
{
	CR_MOUSE_BUTTON_L,
	CR_MOUSE_BUTTON_M	= 10,
	CR_MOUSE_BUTTON_R
};

In this case, L/left would get value 0, and R would get value 11 (++ from the forelast enum with value 10).

I just want to be sure if the assumptions above our correct, so I can continue with this knowledge (and remove unnecessary values assignments, because in my use-cases I think I can easily get away without assigning any values to the enums).

Any input is appreciated, as always.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

http://en.cppreference.com/w/cpp/language/enum

Each enumerator is associated with a value of the underlying type. When initializers are provided in the enumerator-list, the values of enumerators are defined by those initializers. If the first enumerator does not have an initializer, the associated value is zero. For any other enumerator whose definition does not have an initializer, the associated value is the value of the previous enumerator plus one.

enum Foo { a, b, c = 10, d, e = 1, f, g = f + c };
//a = 0, b = 1, c = 10, d = 11, e = 1, f = 2, g = 12

(Why does this need to be a forum question, outside of "For beginners"? This took me 30 seconds to look up, c'mon, we are not google or a reference guide for you. You could have also tested this yourself with ease inside your IDE/application.)

Thanks, also for the feedback. Point taken

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

While discussing enum, for modern C++11, you may want to use enum class, as in


enum class Foo { X, Y, Z };

The constants are now in 'Foo' scope, so you need to write Foo::X rather than just X, making it explicit where X is coming from. This also means having two X-es in different enums won't be a problem any more.

Implicit cast to int is removed with an enum class


Foo p = X;
if (p == 2) // Fails, cannot implicitly cast p to int.
if (static_cast<int>(p) == 2) // works

Finally, you can state the underlying numeric type of the enum values by stating the type as a super-class


enum class Foo : uint8_t { ... };

This topic is closed to new replies.

Advertisement