小编Shi*_*sak的帖子

类成员函数的函数模板特化

#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)

c++ templates overloading template-specialization c++11

3
推荐指数
1
解决办法
84
查看次数