代码之家  ›  专栏  ›  技术社区  ›  jotik

模板参数'(类型)0'与'EnumValue'不匹配

  •  7
  • jotik  · 技术社区  · 6 年前

    给出了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 ,错误消失。 为什么枚举失败?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Mike Kinghan Luchian Grigore    6 年前

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

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

    Success