模板参数“(type)0”不匹配 'EnumValue'

template argument '(type)0' does not match 'EnumValue'

鉴于此 C++11 代码:

#include <type_traits>

enum Enum { EnumValue };

template <typename>
struct Pred { constexpr static bool const value = true; };

template <
        typename T,
        typename ::std::enable_if<
            Pred<T>::value,
            Enum
        >::type = EnumValue>
class Huh {};

template <typename T>
constexpr bool f(Huh<T> const &) noexcept { return true; }

static_assert(f(Huh<int>()), "");

我从 GCC 7.3.0 收到以下错误消息:

test.cpp:19:27: error: no matching function for call to 'f(Huh<int>)'
 static_assert(f(Huh<int>()), "");
                           ^
test.cpp:17:16: note: candidate: template<class T> constexpr bool f(const Huh<T>&)
 constexpr bool f(Huh<T> const &) noexcept { return true; }
                ^
test.cpp:17:16: note:   template argument deduction/substitution failed:
test.cpp:19:27: note:   template argument '(type)0' does not match 'EnumValue'
 static_assert(f(Huh<int>()), "");
                           ^

如果我使用 int0 而不是 EnumEnumValue,错误就消失了。 为什么枚举会失败?

Does anyone have any idea about how to work around this on broken versions of GCC while keeping the enum?

你可以咬咬牙告诉编译器它没有推导出什么:

static_assert(f<int>(Huh<int>()), "");

Success

如果这种不雅观现象很普遍,也许您可​​以将其本地化到一些有条件编译的包装中。