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

后端 未结 5 1928
北恋
北恋 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条回答
  •  离开以前
    2020-12-08 13:43

    In the first case, the type of HELLO is not E, whereas in the second case, the type of HELLO is E.

    For a good demonstration of why this is important, see Howard Hinnant's answer to "“enum class” emulation or solid alternative for MSVC 10.0."

    enum class and enum struct are "semantically equivalent" (i.e., the same), per C++0x FDIS §7.2/2.

提交回复
热议问题