我有这个hpp
namespace A
{
template<class T>
class MyC
{
public:
T a;
};
template<class T>
void F(T r);
}
还有这个cpp
template<>
void A::F<double>(double r)
{
r;
}
template<>
void A::F<int>(int r)
{
r;
}
template<class T>
void A::F<A::MyC<T>>(A::MyC<T> r)
{
r;
}
template void A::F<A::MyC<int>>(A::MyC<int>);
template void A::F<A::MyC<double>>(A::MyC<double>);
但是编译器对 F 说“无法将函数定义与现有声明匹配”。
这段代码有什么问题?
最佳答案
将这些声明全部放入命名空间 A { ... }
中并删除 A::
。另一方面,函数模板部分特化是不允许的,这会出错:
template<class T>
void F<MyC<T>>(MyC<T> r)
{
...
}
关于c++ - 在cpp中重载模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564499/