53

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?

2
  • What is the point of not including both syntax? It makes sense to just have both given that struct and class are usually equivalent (apart from public/private) in c++. Commented Jul 22, 2016 at 23:34
  • @FantasticMrFox, the point of only having one would be to avoid confusing people, and to save everyone the effort of asking and answering this question. Commented Jun 14 at 19:40

2 Answers 2

55

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.

Sign up to request clarification or add additional context in comments.

Comments

30

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.

3 Comments

But template<typename T> is allowed, and is also equivalent to template<class T>! The standard is quite confusing on these 2 points, isn't it?
@ZeeByeZon: No. class and struct being near synonyms in type declarations is common. The only difference is the default public/private access of their members. Since scoped enums have no private members, the only difference between the two is meaningless.
@NicolBolas Not just members. It also affects inheritance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.