编译器错误 C2910

更新:2007 年 11 月

错误消息

“function”: 无法显式专用化

编译器检测出两次尝试显式专用化一个函数。

下面的示例生成 C2910:

// C2910.cpp
// compile with: /c
template <class T>
struct S;

template <> struct S<int> { void f() {} };
template <> void S<int>::f() {}   // C2910 delete this specialization

如果尝试显式专用化非模板成员,也可能生成 C2910。即,只能显式专用化一个函数模板。

下面的示例生成 C2910:

// C2910b.cpp
// compile with: /c
template <class T> struct A {
   A(T* p);
};

template <> struct A<void> {
   A(void* p);
};

template <class T>
inline A<T>::A(T* p) {}

template <> A<void>::A(void* p){}   // C2910
// try the following line instead
// A<void>::A(void* p){}

该错误也可能是由于在 Visual Studio .NET 2003 中所做的编译器一致性工作而产生的:

为使代码在 Visual C++ 的 Visual Studio .NET 2003 和 Visual Studio .NET 版本中有效,请移除 template <>。

有关更多信息,请参见编译时的重大更改摘要

下面的示例生成 C2910:

// C2910c.cpp
// compile with: /c
template <class T> class A {
   void f();
};

template <> class A<int> {
   void f();
};

template <> void A<int>::f() {}   // C2910
// try the following line instead
// void A<int>::f(){}   // OK