#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' …Run Code Online (Sandbox Code Playgroud)