c++ - 在结构部分特化中使用 SFINAE 捕获仿函数

标签 c++ templates sfinae

出于某些复杂的原因,我想将任何受支持的类型 T(来自模板)转换为我选择的类型列表。为此,我尝试使用名为“Convert”的模板结构。例如:

Convert<short>::type should be int
Convert<int>::type should be int
Convert<char>::type should be int
Convert<float>::type should be double
Convert<double>::type should be double
Convert<const char*>::type should be std::string
Convert<std::string>::type should be std::string
etc.

使用模板特化很容易实现上述内容。但是有一种情况会导致问题:

Convert<T>::type where T is a functor should be T

要处理这个,我想我必须使用 SFINAE但我无法让它编译。<​​/p>

下面的代码给我“partial specialization cannot match argument list for primary template”(即禁止写“Convert”):

template<typename T, typename = decltype(&T::operator())>
struct Convert<T>       { typedef T type; };

而这个给了我“template parameter not used or deducible in partial specialization”(即它认为没有使用 T):

template<typename T>
struct Convert<typename std::enable_if<std::is_function<typename T::operator()>::value,T>::type>
 { typedef T type; };

我不知道该怎么做,我所有的尝试都会导致上述两个错误之一。

编辑:我想使用相同的模型捕捉其他通用的东西,所以我不能只在非专用结构中写“typedef T type”。

谢谢

最佳答案

我建议您使用第一种方法,但要使其有效,您必须

用一个未使用的模板参数声明主模板:

template <class T, class = void> Convert;

为您现在使用的模板的所有特化添加一个void参数。

像这样定义你的“仿函数特化”:

template<typename T, typename std::enable_if<std::is_function<typename T::operator()>::value,void>::type>

这意味着如果第二个参数是仿函数(因此它与默认模板参数匹配),则将其设为 void,如果不是,则设为不存在。

顺便说一句,为什么要在 typename T::operator() 中使用 typename?据我所知,operator() 不是类型。

关于c++ - 在结构部分特化中使用 SFINAE 捕获仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3324554/

相关文章:

c# - 对象公寓与线程公寓

c++ - 字节顺序转换和 g++ 警告

c++ - 如何在C++中获取线程的结果

c# - 在 DotLiquid 中编写递归循环

c++ - 如何简化模板模板参数中的enable_if别名

c++ - 我一定是在构建 DLL 时做错了什么

c++ - 有没有办法突破 boost::mpl for_each?

php - 如何在 ZF2 中渲染带有布局的邮件模板?

c++ - 如果原始源代码行无法编译,是否可以让模板选择备用源代码行?

c++ - 模板默认类型与默认值