检查枚举类是否包含特定标识符

Plu*_*uin 5 c++ enum-class c++14 c++17

我在 SO 上搜索了一下,很惊讶我没有找到任何类似的问题。如果已经回答了任何提示,我们很高兴。

我有一个定义了很多枚举类的代码库。其中一些指定了 totalNum 常量,例如

enum class Foo : int
{
    a,
    b,
    c,

    totalNum
}
Run Code Online (Sandbox Code Playgroud)

其他人没有这样的

enum class Bar : bool
{
    oneOption,
    otherOption
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个基本上像这样的功能

template <class EnumClassType>
EnumClassType typeToEnum (typename std::underlying_type<EnumClassType>::type value)
{
    // If you hit this assertion, the value is outside of the valid enum range
    assert (isPositiveAndBelow (value, decltype (value) (EnumClassType::totalNum)));

    return EnumClassType (value);
}
Run Code Online (Sandbox Code Playgroud)

虽然这对totalNum指定的枚举有效并且有意义,但我想跳过这个断言,以防枚举中没有这样的标识符。有没有办法做到这一点?代码库目前使用 C++ 14,但由于即将进行的编译器更改,也欢迎使用 C++ 17 解决方案。

Plu*_*uin 4

与此同时,我自己使用评论中提到的@jfh 等方法找到了答案。

首先,这是一种检查枚举类是否包含具有特定名称的标识符的方法

template <class EnumToTest>
class EnumConstantDefined_totalNum
{
private:
    using Yes = int8_t;
    using No = int16_t;

    template <class E>
    static Yes test (decltype (E::totalNum)*);

    template <class E>
    static No test (...);

public:
    static constexpr bool value = sizeof (test<EnumToTest> (0)) == sizeof (Yes);
};

Run Code Online (Sandbox Code Playgroud)

然后我们可以使用 SFINAE 为两种枚举指定两个重载。

template <class EnumType>
std::enable_if_t<EnumConstantDefined_totalNum<EnumType>::value, void> assertValueIsInRange (typename std::underlying_type<EnumType>::type value)
{
    assert (isPositiveAndBelow (value, decltype (value) (EnumType::totalNum)));
}

template <class EnumType>
std::enable_if_t<! EnumConstantDefined_totalNum<EnumType>::value, void> assertValueIsInRange (typename std::underlying_type<EnumType>::type)
{
    // do nothing
}

Run Code Online (Sandbox Code Playgroud)

然后在实际的转换函数中使用这个断言函数

/**
   Casts a value matching an enum class underlying type to an enum class constant and asserts that the
   value is inside the valid enum range
 */
template <class EnumClassType>
EnumClassType typeToEnum (typename std::underlying_type<EnumClassType>::type value)
{
    assertValueIsInRange<EnumClassType> (value);

    return EnumClassType (value);
}
Run Code Online (Sandbox Code Playgroud)