#include <iostream>
using namespace std;
class GenericAdd
{
public:
template <typename T>
T operator()(T a, T b)
{
return a + b;
}
};
int main()
{
GenericAdd add;
cout<<"add(1, 2) = "<<add(1, 2)<<endl;
cout<<"add(1.23, 4.56) = "<<add(1.23, 4.56)<<endl;
return 0;
}
/*
Output:
add(1, 2) = 3
add(1.23, 4.56) = 5.79
*/
模板函数对象 (TemplateFunctionObject)
最新推荐文章于 2021-11-30 14:19:02 发布