Difference between enum class and enum struct

B

Brian

Shalom

Is there any difference between

enum class Fruit : uint8_t { love = 1, joy, peace, patience,
kindness };

and

enum struct Fruit : uint8_t { love = 1, joy, peace, patience,
kindness };

?

If there is, what is it? If there isn't any difference, my opinion is
that it is kind of an unfortunate point for flexibility in the
language.
I may modify my support for strongly-typed enums to only support
enum class if there's no difference between the two.


Brian Wood
http://webEbenezer.net
(651) 251-9384
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Shalom

Is there any difference between

enum class Fruit : uint8_t { love = 1, joy, peace, patience,
kindness };

and

enum struct Fruit : uint8_t { love = 1, joy, peace, patience,
kindness };

?

If there is, what is it? If there isn't any difference, my opinion is
that it is kind of an unfortunate point for flexibility in the
language.
I may modify my support for strongly-typed enums to only support
enum class if there's no difference between the two.

No difference at all. The only differences between "class" and "struct" are

1. The default access level of "class" is "private", the default access
level of "struct" is "public".

2. "class" can declare a template parameter but "struct" cannot.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkuIk3UACgkQm4klUUKw07DW9ACeO/trSCQFNv5xf4IPtVOhn2S9
USEAniRq6MHIHxmROUzyWJ2WU3c/U7n+
=WXLh
-----END PGP SIGNATURE-----
 
I

Ian Collins

Brian said:
Shalom

Is there any difference between

enum class Fruit : uint8_t { love = 1, joy, peace, patience,
kindness };

and

enum struct Fruit : uint8_t { love = 1, joy, peace, patience,
kindness };

?

If there is, what is it? If there isn't any difference, my opinion is
that it is kind of an unfortunate point for flexibility in the
language.

There doesn't appear to be. According to n3035, "The enum-keys enum
class and enum struct are semantically equivalent.
 
I

Ian Collins

Michael said:
-----BEGIN PGP SIGNED MESSAGE-----

Please drop the PGP nonsense on Usenet!
No difference at all. The only differences between "class" and "struct" are

1. The default access level of "class" is "private", the default access
level of "struct" is "public".

2. "class" can declare a template parameter but "struct" cannot.

He was referring to C++0x scoped enums, not classes and structs.
 
B

Brian

Please drop the PGP nonsense on Usenet!









He was referring to C++0x scoped enums, not classes and structs.

I thought about mentioning that as well, but then thought
about how his post mentions struct is not acceptable in
some contexts. That was an interesting reminder.


Brian Wood
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top