c++ - 在cpp中定义模板特化?

标签 c++ visual-studio templates template-specialization

我可以像这样在 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/

相关文章:

c++ - 结构数组不会初始化

c++ - 创建 "shortcuts"类宏定义

c++ - 在 QToolBar 中移动 QMenuBar

visual-studio - 如何在命令行参数中将解决方案文件夹作为参数传递(用于调试)?

c++ - OnTouchMove 事件未触发

visual-studio - 删除 Visual Studio 2019 中菜单下方的空行

visual-studio - Visual Studio 2015 扩展管理器索引超出范围错误

c++ - 类似模板参数的函数

c++ - 是否存在近似等同于运行时#ifdef 的C++ 编程技术?

python - Eclipse 模板中的条件语句