如何在不指定枚举的情况下将 SFINAE 用于枚举?

How to use SFINAE for enum without specifying the enum?

使用来自 Substitution failure is not an error (SFINAE) for enum 我试图编写一个代码,从 class 中获取枚举值,如果找不到该枚举值,它将有一个回退值。 由于我是模板的初学者,几个小时后我放弃了并找到了一个使用宏的 "solution" :(

有没有办法在不使用宏并且不为每个可能的枚举值复制代码的情况下做同样的事情?

这是我想出的:

struct foo
{
    enum FooFields
    {
        enumFoo,
        enumHehe
    };
};

struct bar
{
    enum BarFields
    {
        enumHehe = 2
    };
};

#define GETENUM_DEF(testedEnum) \
template<class T> \
struct get_ ## testedEnum{\
  typedef char yes;\
  typedef yes (&no)[2];\
\
  template<int>\
  struct test2;\
\
  template<class U>\
  static int test(test2<U::testedEnum>*){return U::testedEnum;};\
  template<class U>\
  static int test(...){return -1;};\
\
  static int value(){return test<T>(0);}\
};

GETENUM_DEF(enumFoo)
GETENUM_DEF(enumHehe)

int main() {

    std::cout<<get_enumFoo<foo>::value()<<std::endl; //returns 0;
    std::cout<<get_enumFoo<bar>::value()<<std::endl; //returns -1;

    std::cout<<get_enumHehe<foo>::value()<<std::endl; //returns 1;
    std::cout<<get_enumHehe<bar>::value()<<std::endl; //returns 2;

    return 0;
}

C++ 要求您为每个要获取的字段定义一个 get_someField,但无论是否使用宏,您都必须这样做。

使用 SFINAE 是所谓的模板元编程的一部分。您正在做的是有效地检测表达式 T::enumFoo 是否有效,如果有效则返回该值,否则 -1.

要检测表达式是否有效,我们可以这样做:

#include <type_traits>

// You need void_t to avoid a warning about the lhs of the comma operator 
// having no effect. C++ 17 has std::void_t
template<class...> using void_t = void;

template<class T, class = void>
struct get_enumFoo
{
    static constexpr int value = -1;
};

template<class T>
struct get_enumFoo<T, void_t<decltype(T::enumFoo)>>
{
    static constexpr int value = T::enumFoo;
};

template<class T, class = void>
struct get_enumHehe
{
    static constexpr int value = -1;
};

template<class T>
struct get_enumHehe<T, void_t<decltype(T::enumHehe)>>
{
    static constexpr int value = T::enumHehe;
};

使用它(你的例子):

#include <iostream>

int main() {
    std::cout << get_enumFoo<foo>::value << std::endl; //returns 0;
    std::cout << get_enumFoo<bar>::value << std::endl; //returns -1;

    std::cout << get_enumHehe<foo>::value << std::endl; //returns 1;
    std::cout << get_enumHehe<bar>::value << std::endl; //returns 2;
}

它的工作原理如下:

我们定义了 SFINAE 在表达式 T::enumFoo 无效的情况下回退的情况:

template<class T, class = void>
struct get_enumFoo
{
    static constexpr int value = -1;
};

然后我们定义判断T::enumFoo是否有效的情况:

template<class T>
struct get_enumFoo<T, void_t<decltype(T::enumFoo)>>
{
    static constexpr int value = T::enumFoo;
};

如果 T::enumFoo 无效,则 decltype(T::enumFoo) 是无效表达式,因此 SFINAE 开始,我们回到之前的情况。

如果T::enumFoo有效,则decltype(T::enumFoo)是某种类型,但void_t<decltype(T::enumFoo)>无效。所以我们特化 get_enumFoo<T, void> 以获得字段 value = T::enumFoo.

Try it online


为了进一步减少添加新 get_field 特征背后的样板文件,您可以定义一个基础 class:

namespace detail {
    struct get_foo_defaultValue
    {
        static constexpr int value = -1;
    };
}

那么基本情况就是

template<class T, class = void>
struct get_enumFoo
    : detail::get_foo_defaultValue
{};