How can I output the value of an enum class in C++11

前端 未结 7 2186
面向向阳花
面向向阳花 2020-11-30 19:00

How can I output the value of an enum class in C++11? In C++03 it\'s like this:

#include 

using namespace std;

enum A {
  a =          


        
相关标签:
7条回答
  • 2020-11-30 19:43
    #include <iostream>
    #include <type_traits>
    
    using namespace std;
    
    enum class A {
      a = 1,
      b = 69,
      c= 666
    };
    
    std::ostream& operator << (std::ostream& os, const A& obj)
    {
       os << static_cast<std::underlying_type<A>::type>(obj);
       return os;
    }
    
    int main () {
      A a = A::c;
      cout << a << endl;
    }
    
    0 讨论(0)
  • 2020-11-30 19:49

    You could do something like this:

    //outside of main
    namespace A
    {
        enum A
        {
            a = 0,
            b = 69,
            c = 666
        };
    };
    
    //in main:
    
    A::A a = A::c;
    std::cout << a << std::endl;
    
    0 讨论(0)
  • 2020-11-30 19:53

    Unlike an unscoped enumeration, a scoped enumeration is not implicitly convertible to its integer value. You need to explicitly convert it to an integer using a cast:

    std::cout << static_cast<std::underlying_type<A>::type>(a) << std::endl;
    

    You may want to encapsulate the logic into a function template:

    template <typename Enumeration>
    auto as_integer(Enumeration const value)
        -> typename std::underlying_type<Enumeration>::type
    {
        return static_cast<typename std::underlying_type<Enumeration>::type>(value);
    }
    

    used as:

    std::cout << as_integer(a) << std::endl;
    
    0 讨论(0)
  • 2020-11-30 19:56

    To write simpler,

    enum class Color
    {
        Red = 1,
        Green = 11,
        Blue = 111
    };
    
    int value = static_cast<int>(Color::Blue); // 111
    
    0 讨论(0)
  • 2020-11-30 20:00

    Following worked for me in C++11:

    template <typename Enum>
    constexpr typename std::enable_if<std::is_enum<Enum>::value,
                                      typename std::underlying_type<Enum>::type>::type
    to_integral(Enum const& value) {
        return static_cast<typename std::underlying_type<Enum>::type>(value);
    }
    
    0 讨论(0)
  • 2020-11-30 20:04

    (I'm not allowed to comment yet.) I would suggest the following improvements to the already great answer of James McNellis:

    template <typename Enumeration>
    constexpr auto as_integer(Enumeration const value)
        -> typename std::underlying_type<Enumeration>::type
    {
        static_assert(std::is_enum<Enumeration>::value, "parameter is not of type enum or enum class");
        return static_cast<typename std::underlying_type<Enumeration>::type>(value);
    }
    

    with

    • constexpr: allowing me to use an enum member value as compile-time array size
    • static_assert+is_enum: to 'ensure' compile-time that the function does sth. with enumerations only, as suggested

    By the way I'm asking myself: Why should I ever use enum class when I would like to assign number values to my enum members?! Considering the conversion effort.

    Perhaps I would then go back to ordinary enum as I suggested here: How to use enums as flags in C++?


    Yet another (better) flavor of it without static_assert, based on a suggestion of @TobySpeight:

    template <typename Enumeration>
    constexpr std::enable_if_t<std::is_enum<Enumeration>::value,
    std::underlying_type_t<Enumeration>> as_number(const Enumeration value)
    {
        return static_cast<std::underlying_type_t<Enumeration>>(value);
    }
    
    0 讨论(0)
提交回复
热议问题