I want strong enum types. C++0x has this feature but unfortunately they also require explicit scoping:
enum class E {e1, e2, e3};
E x = E::e1; //OK
E y = e1; //error
Sometimes this is desirable, but sometimes it's just unnecessarily verbose. The identifiers might be unique enough by themselves or the enum might already be nested inside a class or namespace.
So I'm looking for a workaround. What would be the best way to declare the enum values also in the surrounding scope?
Enum Class C++11 has introduced enum classes (also called scoped enumerations), that makes enumerations both strongly typed and strongly scoped.
Unfortunately big IntFlag enums are extremely slow in combining attributes - I thought of using them instead of frozensets (reduce memory) but or'ing a few hundred int flags takes a lot of time when an IntFlag has ~10k members.
Overview. Scoped enums (enum class/struct) are strongly typed enumerations introduced in C++11. They address several shortcomings of the old C-style (C++98) enums, mainly associated with type-safety and name collisions.
Numeric EnumsEnums don't always start with 0. We can define enums as numbers during initialization.
If you want the values visible in the surrounding scope, just add a couple of constants:
enum class E {e1, e2, e3};
const E e1 = E::e1;
const E e2 = E::e2;
const E e3 = E::e3;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With