C++ 类成员函数的函数模板专门化 #包括 #包括 使用名称空间std; 课堂测试 { 公众: 模板 void f(常量std::string和path,std::vector和type_vec) { cout
可能是一个重复的问题,但答案很简短: 在类定义之外进行专门化,即:C++ 类成员函数的函数模板专门化 #包括 #包括 使用名称空间std; 课堂测试 { 公众: 模板 void f(常量std::string和path,std::vector和type_vec) { cout,c++,c++11,templates,overloading,template-specialization,C++,C++11,Templates,Overloading,Template Specialization,可能是一个重复的问题,但答案很简短: 在类定义之外进行专门化,即: main.cpp:24:15: error: explicit specialization in non-namespace scope 'class test' template <> ^ main.cpp:25:84: error: template-id 'f' in declaration of primary template void f<std:
main.cpp:24:15: error: explicit specialization in non-namespace scope 'class test'
template <>
^
main.cpp:25:84: error: template-id 'f' in declaration of primary template
void f<std::string>(const std::string& path, std::vector<std::string>& type_vec)
^
类测试
{ ... };
模板
无效测试::f(常量std::字符串和路径,std::向量和类型_vec)
{
在C++11[temp.expl.spec]/2中,cout要求模板函数的显式专门化发生在命名空间范围中
显式专门化应在包含专门化模板的命名空间中声明。[…]
因此,你必须使用
class test
{ ... };
template <>
void test::f<std::string>(const std::string& path, std::vector<std::string>& type_vec)
{
cout << "Specialization vec ";
}
类测试
{
公众:
模板
void f(常量std::string和path,std::vector和type_vec)
{
库特
class test
{ ... };
template <>
void test::f<std::string>(const std::string& path, std::vector<std::string>& type_vec)
{
cout << "Specialization vec ";
}
class test
{
public:
template <typename T>
void f(const std::string& path, std::vector<T>& type_vec)
{
cout << "Base template ";
}
};
template <>
void test::f<std::string>(const std::string& path, std::vector<std::string>& type_vec)
{
cout << "Specialization vec ";
}