繁体   English   中英

为什么我不能推断静态成员函数是否存在

[英]Why can I not deduce if the static member function exists

我有以下代码:

#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 )。 然后将选择主模板但不是specializatioin。

你可以使用std::void_t

template<class T>
constexpr inline bool has_func<T, std::void_t<decltype(func_impl<T>::apply(std::declval<T>()))>> = true;
//                                ^^^^^^^^^^^                                                 ^

生活

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM