Do we really need “enum class” in C++0x?
No, we don't "need" enum class
. We can get sufficiently equivalent functionality in other ways. But by that logic, we don't "need" a lot of stuff in C++. We don't "need" virtual functions and inheritance, since we can just implement it manually with vtables and such. We don't "need" member functions; these can be emulated by having them take an additional argument.
Language features exist to make programmers lives easier. Just because something can be done manually doesn't mean that it should.
enum class
has the following properties:
- It is easy to understand; it mirrors how enums work in other languages.
- It requires relatively little from compiler writers. Contrast the implementation effort with features like r-value references, varadic templates, or user-defined literals.
- It doesn't break the syntax in any way. It may look a bit weird at first to see
enum class
, but that's true for most new features. Once you get used to it, it's fine.
- It is 100% backwards compatible, in that it doesn't redefine how regular enums work. Old-style enums work the same as they used to.
- It keeps you from having to write a lot of boilerplate code. Boost has a macro to create the effect of
enum class
definitions. Without this macro, you have to spend quite a bit of effort getting all of the corner cases to work. And even so, someone had to write and debug that macro.
So no, we do not "need" them. But they're still a great addition to the language.