【问题标题】:How to implement is_enum_class type trait? [duplicate]如何实现 is_enum_class 类型特征? [复制]
【发布时间】:2015-01-12 05:41:12
【问题描述】:

当且仅当传入的类型 T 是类枚举时,如何实现其值成员为 true 的类型特征?虽然我知道例如

+T{};

如果 T 是一个枚举将起作用,如果它是一个枚举类则失败,到目前为止我找不到将它用于 SFINAE 的方法。

【问题讨论】:

  • 将 C++ 标记更新为正确的 C++ 版本...这令人困惑。 is_enum 类型特征也应该与类枚举一起使用。但我也有这个问题。

标签: c++ metaprogramming sfinae typetraits


【解决方案1】:

基于您的+T{} 测试:

选项#1:

尾随返回类型中的表达式 SFINAE:

#include <type_traits>

template <typename T>
auto test(int) -> decltype((void)+T{}, std::false_type{});

template <typename T>
auto test(...) -> std::true_type;

template <typename T>
using is_enum_class = std::integral_constant<bool, decltype(test<T>(0))::value && std::is_enum<T>::value>;

DEMO

选项 #2:

void_t-fashion中:

template <typename T, typename V = void>
struct test : std::false_type {};

template <typename T>
struct test<T, decltype((void)+T{})> : std::true_type {};

template <typename T>
using is_enum_class = std::integral_constant<bool, !test<T>::value && std::is_enum<T>::value>;

DEMO 2

测试:

enum class EC { a, b };
enum E { c, d };

int main()
{
    static_assert(is_enum_class<EC>::value, "!");
    static_assert(!is_enum_class<E>::value, "!");
    static_assert(!is_enum_class<int>::value, "!");
}

【讨论】:

  • 我很想知道匿名投票的原因
  • 没有模板类型别名就不行吗?只需继承自 std::is_enum&lt;T&gt; 而不是 std::true_type
【解决方案2】:

您检查枚举是否可转换为int

template <class T>
using is_scoped_enum = std::integral_constant<bool, !std::is_convertible<T,int>{}
                                                  && std::is_enum<T>{}>;

这些静态断言会成功:

enum e {};
enum class e2 {};

static_assert( is_scoped_enum<e2>::value, "" );
static_assert( !is_scoped_enum<e>::value, "" );
static_assert( !is_scoped_enum<char>::value, "" );

Demo.

【讨论】:

  • 我建议使用std::underlying_type&lt;T&gt;::type 而不是int
猜你喜欢
  • 1970-01-01
  • 2021-07-02
  • 2019-12-08
  • 2020-08-12
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多