一、函数模板概念
函数模板指的是函数参数类型和返回类型的参数化。
二、示例代码
template<class T>
inline
const T& min(const T& a, const T& b)
{
return b < a ? b : a;
}
class stone
{
public:
stone(int w, int h, int we)
: _w(w), _h(h), _weight(we)
{ }
int get_w() const { return _w; }
int get_h() const { return _h; }
int get_weight() const { return _weight; }
bool operator< (const stone& rhs) const
{ return _weight < rhs._weight; }
private:
int _w, _h, _weight;
};
ostream &
operator << (ostream& os, const stone& x)
{
return os << '(' << x.get_w() << ',' << x.get_h() << ')' << " " << x.get_weight();
}
void test_function_template()
{
std::cout << min(4,10) << std::endl;
stone s1(5, 5 ,20);
stone s2(6, 6, 30);
std::cout << min(s1,s2) << std::endl;
}