编译器错误 C2893

未能使函数模板“template name”专用化

编译器无法专门化函数模板。 此问题的原因可能有很多。

通常,解决 C2893 错误的方法是查看函数的签名,并确保你可以实例化每一种类型。

示例

发生 C2893 的原因是,f 的模板参数 T 被推断为 std::map<int,int>,但 std::map<int,int> 没有成员 data_type(无法使用 T = std::map<int,int> 实例化 T::data_type)。 以下示例生成 C2893。

// C2893.cpp
// compile with: /c /EHsc
#include<map>
using namespace std;
class MyClass {};

template<class T>
inline typename T::data_type
// try the following line instead
// inline typename  T::mapped_type
f(T const& p1, MyClass const& p2);

template<class T>
void bar(T const& p1) {
    MyClass r;
    f(p1,r);   // C2893
}

int main() {
   map<int,int> m;
   bar(m);
}