模板函数有模板参数和函数参数,重载调用操作符的类 及 函数指针作为模板参数,其函数参数及函数参数的传参,测试代码如下:
#include <iostream> using namespace std; //key string s1("Hello"); string s2("World"); //重载了调用操作符的类其对象称为函数对象 class cmp{ public: bool operator()(const string& s1, const string& s2)const{ cout << "bool operator()(const string& s1, const string& s2)const\n"; return s1 < s2; }//类对象为const//成员函数为const//成员函数为const是防止修改类对象 }; bool compare(const string& s1, const string& s2){ cout << "bool compare(const string& s1, const string& s2)\n"; return true; } template<typename Compare> //模板类型 void test( const Compare&c = Compare() ){ //函数参数为const对象引用//默认值为函数对象// //默认值为临时对象,右值,const对象的引用 cout << "void test( const Compare&c = Compare() )\n"; c( s1,s2 ); } int main () { test<bool(*)(const string&, const string&) >(compare ); //传参 cout << "--------------------------------------\n"; test<cmp>( ); //默认 return 0; }
模板函数的函数参数,默认为临时对象,应为const对象引用,因为 临时对象为右值