Rakete1111
Rakete1111

Reputation: 48958

What is the difference between enum struct and enum class?

Looking at the enum documentation, there was one thing that I noticed:

enum-key - one of enum, enum class(since C++11), or enum struct(since C++11)

enum and enum class, sure, but what is a enum struct?

The docs seem to say that enum class and enum struct are exactly the same:

[...] scoped enumeration (declared with the enum-key enum class or enum struct)


  • enum struct|class name { enumerator = constexpr , enumerator = constexpr , ... }
  • [...]

Are they really exactly the same? Or are there any differences that I missed? What is the point (if they are the same) to have 2 different syntax for the same thing?

Upvotes: 51

Views: 28627

Answers (2)

eerorika
eerorika

Reputation: 238351

enum class, sure, but what is a enum struct?

Same as enum class.

Are they really exactly the same?

Yes. The documentation is correct.

Or are there any differences that I missed?

No. There are no differences.

What is the point (if they are the same) to have 2 different syntax for the same thing?

I haven't found any written rationalization for the decision. There isn't any in the standard nor the proposal. One might guess that it is some sort of counterpart to class vs struct class-keys. This is an opposite decision than was made when template<class T> syntax was specified, where struct is not allowed.

Upvotes: 29

AlexD
AlexD

Reputation: 32576

enum class and enum struct are the same (emphasis mine).

7.2 Enumeration declarations
...
2 .... The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators.

Upvotes: 54

Related Questions