c++ - 模板参数“(类型)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>()), "");
^
如果我使用int
and0
而不是Enum
and EnumValue
,错误就消失了。为什么这会因枚举而失败?
解决方案
有没有人知道如何在保留枚举的同时在损坏的 GCC 版本上解决这个问题?
你可以咬紧牙关告诉编译器它无法推断出什么:
static_assert(f<int>(Huh<int>()), "");
如果不美观会很普遍,也许您可以在一些有条件的编译包装中将其本地化。
推荐阅读
- javascript - 如果设置了条形宽度比率,则更改 C3 中的工具提示宽度比率
- python - 我的代码在另一个 numpy 数组中为一个列表创建了一个 numpy 数组,但它没有为另一个经历完全相同过程的列表
- scrapy - 在 javascript 中跟踪 url - Scrapy Splash
- firebase - 首次启动时从远程配置 Firebase 获取 0 而不是 #ffffff -React-native-
- c# - Cant bind RouteValueDictionary as HtmlAttributes to EditorFor
- ssl - 即使禁用验证,客户端也无法验证 SSL 证书
- jquery - 单击时如何将折叠按钮移至底部?
- javascript - 模态组件内的工具提示定位
- android - 为什么 Braintree 支付插件不起作用?
- karate - 空手道:试图让全局标题工作