Do we really need “enum class” in C++11?

后端 未结 5 1945
北恋
北恋 2020-12-08 13:24

When we have,

struct E { enum E_ { HELLO }; }; // \'E\' is inheritable

then why do we need,

enum class E { HELLO };  // \'E         


        
5条回答
  •  猫巷女王i
    2020-12-08 13:59

    Yes, we do. Looks like no one pointed out this before. What about if you need to set size of an enum and keep still according to C++ standard? enum class can do. And with type safety as already mentioned. It's reduce so much possible bugs in code and the mess to mixing int and enums. They wasn't never the same thing for me. Amazing. e.g., enum class foo : int16_t { ... } I'm sure each member is an int16_t and not up to implementation decide what's "better" for me.

    EDIT:

    Also, we can have duplicated values (not names) in the list. Which make a lot of sense depending to context.

提交回复
热议问题