【发布时间】:2018-07-03 07:11:38
【问题描述】:
我有一个简单的模板函数,在我正在使用的库中定义
template<class T>
T create(std::vector<char>& data)
{
T newValue{};
/* Do something with data */
return newValue;
}
如果 T 实现特定的接口,我想专门化这个函数
template<class T>
std::enable_if_t<std::is_base_of<Interface, T>::value, T> create( std::vector<char>& data)
{
T newValue{};
newValue.InterfaceFunction(data);
return newValue;
}
但我无法完成这项工作,我专门的功能没有使用。如何实现对已经定义的模板函数的特化?
【问题讨论】:
-
专精不应该只有
template<>吗? -
@Oneiros,我需要在谓词中定义类 T
-
我认为这就是它不起作用的原因,如果您仍然使用 T 类,您在技术上并没有专门化第一个函数......编译器将第二个函数视为完全不同的函数
-
对不起,这个例子不好,我已经重写了。还是技术上不可行吗?
-
this thread 有帮助吗?