Like Tree1Likes
  • 1 Post By kmdv

opinions on enum class in C++11

This is a discussion on opinions on enum class in C++11 within the C++ Programming forums, part of the General Programming Boards category; let's say we have an enum Code: enum someEnum { value1, value2 }; I'm finding myself hard pressed to find ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,457

    opinions on enum class in C++11

    let's say we have an enum

    Code:
    enum someEnum
    {
        value1,
        value2
    };
    I'm finding myself hard pressed to find any real advantage to using the new C++11 features like so:

    Code:
    enum class someEnum : int
    {
        value1,
        value2
    };
    I understand that this effectively requires the programmer to fully qualify the enum member name, but I, along with my colleagues are in the habit of fully qualifying enum member names, even pre-C++11. also, even though we've specified that the members use int as their backing storage, there is still no automatic conversion to int.

    I'm just wondering if there is any common situation that makes the new way work better.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    The few advantages I have personally encountered are:
    1. You don't have to take measures(like declaring inside classes or namespaces) to isolate enum 'names' from the global/outer scope.
    2. No need to clutter header files with multiple enum definitions at the top, just forward declare. (I'm not sure why this does not compile with old enums).
    3. Sugar !
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    No implicit conversions between two different enum types.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    If you want a practical example how to make use of the enum class scope, here is mine:

    Code:
    enum class Enum1
    {
        Low_ = 100,
        A,
        B,
        C,
        High_
    };
    
    enum class Enum2
    {
        Low_ = 200,
        A,
        B,
        C,
        D,
        High_
    };
    
    template <typename T>
    T LoadEnumFromStream(typename std::underlying_type<T>::type x)
    {
        typedef typename std::underlying_type<T>::type IntType;
    
        if (x <= static_cast<IntType>(T::Low_)
            || x >= static_cast<IntType>(T::High_))
        {
            throw 0; // Error.
        }
        return static_cast<T>(x);
    }
    
    ...
    
    
    Enum2 en = LoadEnumFromStream<Enum2>(1234);
    This safely loads different enums from external storage.
    Elkvis likes this.
    I never put signature, but I decided to make an exception.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,282
    No implicit conversions between two different enum types.
    This is pretty much the only real advantage.

    However, it is enough of an advantage that if you are already using C++11 features tying you into C++11 you should be using the new `enum' features as well.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you use enum delcared in a class?
    By derder in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2011, 08:33 AM
  2. Enum within a class
    By manasij7479 in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2011, 11:33 AM
  3. Enum type In class.
    By epidemic in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2007, 10:14 AM
  4. enum in class
    By gipsy in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2002, 06:16 AM
  5. friends class lil prblm with enum
    By rip1968 in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2002, 09:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21