【问题标题】:template argument '(type)0' does not match 'EnumValue'模板参数“(类型)0”与“EnumValue”不匹配
【发布时间】:2018-09-07 15:08:43
【问题描述】:

鉴于此 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>()), "");
                           ^

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

【问题讨论】:

  • 看起来像一个错误。 gcc 8.x+ 和 clang 一样编译
  • 它为我编译了 G++ 8.2.1 和 Clang++ 6.0.1。也许这是一个编译器错误。
  • 使用 MinGW g++ 7.3.0 转载。看起来它无法处理两个级别的模板。设法处理一个级别。
  • 有没有人知道如何在损坏的 GCC 版本上解决这个问题,同时保留枚举?

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


【解决方案1】:

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

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

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

Success

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多