C++ 函数模板和模板类
2016-09-13 09:55
281 查看
#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();
}
相关文章推荐
- 【c++】模板函数、函数模板,模板类、类模板
- C++ Template中关于模板类的成员模板函数显示实例化的问题
- c++模板类如何定义模板成员函数
- C++的模板类和函数模板
- C++模板编程中只特化模板类的一个成员函数
- C++模板类和类模板
- [C++再学习系列] 函数模板和类模板
- C++模板的定制五:对定制成员函数的补充
- C++模板的定制一:定制函数模板
- 未解决-深入C++之函数模板
- C++ 函数模板和排序的函数模板——学习笔记
- C++ 函数模板和类模板(转)
- 王老师 C++ 函数重载和模板 第二讲
- 模板函数,函数模板,模板类,类模板的区别
- C++模板之函数模板
- 函数指针 指针函数 函数模板 模板函数 类模板 模板类
- 利用C++模板,代替虚函数实现类的静态多态性
- 利用C++模板,代替虚函数,实现类的静态多态性(加入性能测试部分)
- 王老师 C++ 函数重载和模板 第一讲
- C++ Primer Plus 第五版 复习题7 函数模板 函数模板具体化