在cpp中定义模板专业化?

Dav*_*vid 12 c++ templates template-specialization visual-studio

我可以在cpp中定义一个专门的函数,就像这样......

//标题

template<typename T>
void func(T){}

template<>
void func<int>(int);
Run Code Online (Sandbox Code Playgroud)

// cpp

template<>
void func<int>(int)
{}
Run Code Online (Sandbox Code Playgroud)

如何在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);
};
Run Code Online (Sandbox Code Playgroud)

// cpp

template<>
void A<int>::func(int)
{}
Run Code Online (Sandbox Code Playgroud)

Ros*_*ost 5

.cpp文件中使用以下语法:

void A<int>::func(int)
{
}
Run Code Online (Sandbox Code Playgroud)

这是Visual C++有点特色.

有关详细信息,请参阅MSDN C2910错误说明:

由于在Visual Studio .NET 2003中完成的编译器一致性工作,也会生成此错误:对于代码将在Visual Studio .NET 2003和Visual Studio .NET的Visual Studio .NET版本中有效,删除模板<>.

  • 这是Visual Studio的编译器相关功能.请参阅http://stackoverflow.com/questions/3749099/why-should-the-implementation-and-the-declaration-of-a-template-class-be-in-the和http://stackoverflow.com/问题/ 495021 /为什么灿模板,只待实施,在最头文件 (3认同)