c++ - 静态成员函数是否存在,为什么不能推断
问题描述
我有以下代码:
#include <utility>
template<class T,class E = void>
struct func_impl;
template<class T,class E = void>
constexpr inline bool has_func = false;
template<class T>
constexpr inline bool has_func<T,decltype(func_impl<T>::apply(std::declval<T>()))> = true;
template<>
struct func_impl<int>
{
static int apply(int i);
};
static_assert(has_func<int>);
static_assert 失败,我预计它会成功。我做错什么了?
解决方案
问题是第二个模板参数的默认值E
来自主模板,它void
与专门化的模板参数不匹配;这是专门的decltype(func_impl<T>::apply(std::declval<T>()))
(即int
在这种情况下)。然后将选择主要模板但不选择专业化。
你可以使用std::void_t
.
template<class T>
constexpr inline bool has_func<T, std::void_t<decltype(func_impl<T>::apply(std::declval<T>()))>> = true;
// ^^^^^^^^^^^ ^
推荐阅读
- html - 弹性盒项目中的文本导致弹性盒项目展开
- mysql - 如何在 MYSQL 中编写查询以避免 CSV 中的 qoutes
- hibernate - Spring 应用程序在生成用于测试的 schmea 时报告“字段 'id' 没有默认值”
- javascript - html 在 fullcalender 版本 5 中被视为文本
- tensorflow - 当我尝试使用我的 GPU 时调用 model.fit() 时,Python 3.8.8 Jupyter 笔记本内核死机
- nginx - Next.js 直接从 url 更改不起作用
- r - 在对逻辑套索回归进行重复交叉验证后,对 predict() 函数使用 type = "raw" 选项会返回空向量
- google-cloud-platform - 在谷歌云中寻找 REST API (s) 以提取部署在 kubernetes 中的服务器的运行状况、cpu 负载等信息
- javascript - 状态未在不同组件中更新
- java - 如何在 Android Java 中关闭 Imageview 的抗锯齿功能