我可以像这样在 cpp 中定义一个专门的函数...
//标题
template<typename T>
void func(T){}
template<>
void func<int>(int);
//cpp
template<>
void func<int>(int)
{}
如何在 cpp 的专用类中定义方法?像这样(这不起作用,我得到 error C2910: 'A<int>::func' : cannot be explicitly specialized
)...
//标题
template<typename T>
struct A
{
static void func(T){}
};
template<>
struct A<int>
{
static void func(int);
};
//cpp
template<>
void A<int>::func(int)
{}
最佳答案
在您的 .cpp
文件中使用以下语法:
void A<int>::func(int)
{
}
这是 Visual C++ 的一种特性。
见 MSDN C2910 error description详情:
This error will also be generated as a result of compiler conformance work that was done in Visual Studio .NET 2003:. For code will be valid in the Visual Studio .NET 2003 and Visual Studio .NET versions of Visual C++, remove template <>.
关于c++ - 在cpp中定义模板特化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13034799/