Thread: opinions on enum class in C++11

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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 !

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    733
    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.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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, 12:33 PM
  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, 07: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