首页 > 解决方案 > 模板参数“(类型)0”与“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>()), "");
                           ^

如果我使用intand0而不是Enumand EnumValue,错误就消失了。为什么这会因枚举而失败?

标签: c++c++11g++compiler-bug

解决方案


有没有人知道如何在保留枚举的同时在损坏的 GCC 版本上解决这个问题?

你可以咬紧牙关告诉编译器它无法推断出什么:

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

成功

如果不美观会很普遍,也许您可​​以在一些有条件的编译包装中将其本地化。


推荐阅读