//class template,类模板
template<typename T>
class complex{
public:
complex(T r = 0, T i = 0)
: re(r), im(i)
{}
complex& operator += (const complex&);
T real() const { return re; }
T imag() const { return im;}
private:
T re, im;
};
{
complex<double> c1(2.4, 5.3);
//function template,函数模板
stone r1(2,3), r2(3,3), r3;
r3 = min(r1, r2); //编译器进行实参推导(argument deduction)
template<class T>
inline
const T& min(const T& a, const T& b){
return b < a ? b : a;
}
class stone
{
public:
...
bool operator < (cosnt stone& rhs) cosnt{
return _weight < rhs._weight;
}
private:
...
};
complex<int> c2(2, 6);
}