Shi*_*sak 3 c++ templates overloading template-specialization c++11
#include <iostream>
#include <vector>
using namespace std;
class test
{
public:
template <typename T>
void f(const std::string& path, std::vector<T>& type_vec)
{
cout << "Base template ";
}
template <>
void f<std::string>(const std::string& path, std::vector<std::string>& type_vec)
{
cout << "Specialization vec ";
}
};
int main()
{
std::vector<int> vec_int;
std::vector<std::string> vec_str;
std::string path = "";
test T;
T.f(path, vec_int);
T.f(path, vec_str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
获得以下编译错误:
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)
^
Run Code Online (Sandbox Code Playgroud)
错误1:有解决方法吗?对于非命名空间范围(在这种情况下是类),是否完全不允许专门化?
错误2:我知道不允许对函数模板进行部分特化.但是,我没有得到这是如何部分专业化?任何解决方法?
注意:如果函数不是类成员方法,则代码编译正常.
在C++ 11中,[temp.expl.spec]/2要求模板函数的显式特化在命名空间范围内发生.
应在包含专用模板的命名空间中声明显式特化.[...]
因为你必须使用
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 ";
}
Run Code Online (Sandbox Code Playgroud)
那说DR CWG 727已被应用并且clang(8.0.0+)和MSVS(2015 update 3+)将编译代码,因为[temp.expl.spec]/2的新措辞是
可以在可以定义相应主模板的任何范围中声明显式特化([namespace.memdef],[class.mem],[temp.mem]).
这是C++ 17采用的,但也被追溯应用于C++ 14
已经为gcc提交了一个错误报告,但它还没有进展:Bug 85282 - CWG 727(非命名空间范围内的完全专业化)