您如何使用模板化 class 专门化模板化函数?
How do you specialise a templated function with a templated class?
所以我有一个模板函数:
template<typename T>
int func(const T& input){
//do stuff
}
我想用模板化 class(如 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)
所以我有一个模板函数:
template<typename T>
int func(const T& input){
//do stuff
}
我想用模板化 class(如 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)