C++ 函数模板和模板类

来源:互联网 发布:智能电视机软件下载 编辑:程序博客网 时间:2024/06/07 05:31
#include <QCoreApplication>
#include <iostream>
using namespace std;
//函数模板
template<typename T>
bool equivalent(const T&a, const T&b)
{
    return !(a<b)&& !(b<a);
}
//类模板
template<typename T=int>//默认参数
class Bignumber{
  typedef T value_type;
  public:
    Bignumber(value_type a):
        m_v(a)
        {
        }
    inline bool operator <(const Bignumber &obj)const
    {
        return m_v< obj.m_v;
    }
  private:
    value_type m_v;
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Bignumber<> x(1), b(1);
    bool ret = equivalent(x,b);//函数模板自动推导
    cout<<ret<<endl;
    cout<<equivalent<double>(5,1);//函数模板特化
    while (1) {
    }
    return a.exec();
}
0 0
原创粉丝点击