函数模板是实现泛型编程的基础,C++的函数模板有两种写法:
template<typename T>
T functionName(T){}
template<class T>
T functionName(T){}
#include <iostream>
template <typename T>
T abs(T n){
return n<0 ? -n : n;
}
int main(int argc,char** argv){
std::cout << abs(-5) << std::endl;
std::cout << abs(3.14) << std::endl;
std::cout << abs('a') << std::endl;
return 0;
}
2201

被折叠的 条评论
为什么被折叠?



