#include <iostream>

struct a {
  enum LOCAL_A { A1, A2 };
};
enum class b { B1, B2 };

int foo(int input) { return input; }

int main(void) {
  std::cout << foo(a::A1) << std::endl;
  std::cout << foo(static_cast<int>(b::B2)) << std::endl;
}

a::LOCAL_A是强类型enum试图实现的目标,但有一个小小的区别:普通enum可以转换为整数类型,而强类型enum如果没有强制转换就不能做到这一点。

那么,有没有一种方法可以将强类型枚举值转换为整数类型而不进行强制转换呢?如果是,怎么做?


当前回答

正如其他人所说,你不能有隐式转换,那是设计的。

如果愿意,可以避免在强制转换中指定底层类型。

template <typename E>
constexpr typename std::underlying_type<E>::type to_underlying(E e) noexcept {
    return static_cast<typename std::underlying_type<E>::type>(e);
}

std::cout << foo(to_underlying(b::B2)) << std::endl;

其他回答

不。没有自然的方法。

事实上,在c++ 11中使用强类型枚举类的动机之一是防止它们无声地转换为int。

由R. Martinho Fernandes提供的答案的c++ 14版本将是:

#include <type_traits>

template <typename E>
constexpr auto to_underlying(E e) noexcept
{
    return static_cast<std::underlying_type_t<E>>(e);
}

与前面的答案一样,这将适用于任何类型的枚举和底层类型。我添加了noexcept关键字,因为它永远不会抛出异常。


更新 这也出现在Scott Meyers的《Effective Modern c++》中。见第10项(在我这本书的最后几页有详细说明)。


c++ 23版本将使用std:: to_底层函数:

#include <utility>

std::cout << std::to_underlying(b::B2) << std::endl;

...或者如果底层类型可以是1字节类型:

std::cout << +(std::to_underlying(b::B2)) << std::endl;

强类型枚举旨在解决多个问题,而不仅仅是你在问题中提到的范围问题:

提供类型安全,从而消除通过积分提升到整数的隐式转换。 指定基础类型。 提供强作用域。

因此,不可能隐式地将强类型枚举转换为整数,甚至是它的底层类型——这就是其思想。因此必须使用static_cast来显式地进行转换。

如果你唯一的问题是作用域,并且你真的想要隐式提升为整数,那么你最好使用非强类型enum和它声明的结构的作用域。

c++委员会向前迈出了一步(将枚举范围从全局命名空间中排除),又后退了五十步(没有枚举类型衰减为整数)。遗憾的是,如果您需要以任何非符号的方式获取枚举的值,枚举类是不可用的。

最好的解决方案是根本不使用它,而是自己使用名称空间或结构来确定枚举的范围。出于这个目的,它们是可以互换的。当引用枚举类型本身时,您将需要输入一些额外的内容,但这种情况可能不会经常发生。

struct TextureUploadFormat {
    enum Type : uint32 {
        r,
        rg,
        rgb,
        rgba,
        __count
    };
};

// must use ::Type, which is the extra typing with this method; beats all the static_cast<>()
uint32 getFormatStride(TextureUploadFormat::Type format){
    const uint32 formatStride[TextureUploadFormat::__count] = {
        1,
        2,
        3,
        4
    };
    return formatStride[format]; // decays without complaint
}

正如其他人所说,你不能有隐式转换,那是设计的。

如果愿意,可以避免在强制转换中指定底层类型。

template <typename E>
constexpr typename std::underlying_type<E>::type to_underlying(E e) noexcept {
    return static_cast<typename std::underlying_type<E>::type>(e);
}

std::cout << foo(to_underlying(b::B2)) << std::endl;