如何使用模板化类专门化模板化函数?

How do you specialise a templated function with a templated class?

本文关键字:函数 专门化 何使用      更新时间:2023-10-16

所以我有一个模板化函数:

template<typename T>
int func(const T& input){
//do stuff
}

我想用一个模板化类(如 std::vector(来专门化它,所以像这样:

template<typename T>
int func(const std::vector<T>& input){
//do specialised stuff
}

但我不知道你到底是怎么做到的。谢谢!

继续

#include <vector>
#include <iostream>  
using namespace std;
template<typename T>
int func(const vector<T>& a){
for(auto i: a)         //do specialised stuff
cout<< (i<<1) <<"n";
return 0;
}
int main() {
vector<int> a={9,8,7};
func(a);
}
18
16
14

将每个阵列 A 乘以 2(左移一次,<<1(