Difference between enum class and enum struct

Discussion in 'C++' started by Brian, Feb 26, 2010.

  1. Brian

    Brian Guest

    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
     
    Brian, Feb 26, 2010
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Brian wrote:

    >
    > 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-----
     
    Michael Tsang, Feb 27, 2010
    #2
    1. Advertising

  3. Brian

    Ian Collins Guest

    Brian wrote:
    > 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.

    --
    Ian Collins
     
    Ian Collins, Feb 27, 2010
    #3
  4. Brian

    Ian Collins Guest

    Michael Tsang wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----


    Please drop the PGP nonsense on Usenet!

    > Brian wrote:
    >
    >> 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.


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

    --
    Ian Collins
     
    Ian Collins, Feb 27, 2010
    #4
  5. Brian

    Brian Guest

    On Feb 27, 3:04 am, Ian Collins <> wrote:
    > Michael Tsang wrote:
    > > -----BEGIN PGP SIGNED MESSAGE-----

    >
    > Please drop the PGP nonsense on Usenet!
    >
    >
    >
    >
    >
    > > Brian wrote:

    >
    > >> 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.

    >
    > 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
     
    Brian, Feb 27, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,240
  2. Robben
    Replies:
    3
    Views:
    535
    tmartsum
    Sep 7, 2005
  3. Ook
    Replies:
    22
    Views:
    854
    Fabio Fracassi
    Oct 5, 2005
  4. Replies:
    4
    Views:
    389
    James Kanze
    Oct 11, 2007
  5. Pep
    Replies:
    4
    Views:
    828
    James Kanze
    Sep 10, 2009
Loading...

Share This Page