Do we really need an "enum class" in C ++ 0x?
No, we do not need an enum class . We can get quite equivalent functionality in other ways. But by this logic, we do not need a lot of things in C ++. We do not need virtual functions and inheritance, since we can simply implement it manually using vtables, etc. We do not need "member functions"; they can be emulated if they take an extra argument.
There are language features that make life easier for programmers. Just because something can be done manually does not mean that it should.
enum class has the following properties:
- Easy to understand; it reflects how enumerations work in other languages.
- For compiler compilers, this is relatively small. Compare implementation efforts with features such as r-value references, varadic patterns, or user-defined literals.
- It does not violate the syntax. It may seem a little strange at first to see the
enum class , but this is true for most of the new features. Once you get used to it, all is well. - This is 100% backward compatible as it does not override how regular enumerations work. Old-style modes work the same as before.
- This prevents you from writing many templates. Boost has a macro to create an
enum class effect. Without this macro, you will have to spend a lot of effort for all corner cases to work. And even so, someone had to write and debug this macro.
So no, we donβt βneedβ them. But they are still a great addition to the language.
Nicol Bolas Aug 04 '11 at 7:00 2011-08-04 07:00
source share