模板特化 用法

函数模板特化 用法

#include <iostream>
using namespace std;

template <typename T>
bool Less(T x, T y)
{
	return x < y;
}

template <>
bool Less<int*>(int* p1, int* p2)
{
	return *p1 < *p2;
}

int main()
{
	int number1 = 10;
	int number2 = 20;
	cout << Less(number1, number2) << endl;
	//
	double number3 = 2.7;
	double number4 = 1.99;
	cout << Less(number3, number4) << endl;
	//
	int number5 = 10;
	int number6 = 100;
	cout << Less(&number5, &number6) << endl;
	return 0;
}

类模板特化 用法

#include <iostream>
using namespace std;

template <typename T1, typename T2>
class TwoObjs
{
public:
	TwoObjs(T1 x, T2 y)
		:_obj1(x)
		,_obj2(y)
	{
		cout << "实例化对象里有两个成员,我要比较他们咯:" << endl;
	}
	void LessCompare()
	{
		cout << "第一个成员:" << _obj1 << endl;
		cout << "第二个成员:" << _obj2 << endl;
		cout << "比较结果:";
		cout << (_obj1 < _obj2) << endl << endl;;
	}
private:
	T1 _obj1;
	T2 _obj2;
};

//全特化
template <>
class TwoObjs<int, char>
{
public:
	TwoObjs(int x, char y)
		:_obj1(x)
		, _obj2(y)
	{
		cout << "实例化对象里有两个成员,我要比较他们咯:" << endl;
	}
	void LessCompare()
	{
		cout << "第一个成员:" << _obj1 << endl;
		cout << "第二个成员:" << _obj2 << endl;
		cout << "说实话,不想比较,整数与字符比较太欺负人了" << endl;
	}
private:
	int _obj1;
	char _obj2;
};
template <>
class TwoObjs<char, int>
{
public:
	TwoObjs(char x, int y)
		:_obj1(x)
		, _obj2(y)
	{
		cout << "实例化对象里有两个成员,我要比较他们咯:" << endl;
	}
	void LessCompare()
	{
		cout << "第一个成员:" << _obj1 << endl;
		cout << "第二个成员:" << _obj2 << endl;
		cout << "说实话,不想比较,整数与字符比较太欺负人了" << endl;
	}
private:
	char _obj1;
	int _obj2;
};

//偏特化
template <typename T1, typename T2>
class TwoObjs<T1* , T2*>
{
public:
	TwoObjs(T1* x, T2* y)
		:_obj1(x)
		,_obj2(y)
	{}
	void LessCompare()
	{
		cout << "第一个成员(指针):" << _obj1 << endl;
		cout << "第二个成员(指针):" << _obj2 << endl;
		cout << "分别解引用:" << endl;
		cout << "第一个指针解引用出来:" << *_obj1 << endl;
		cout << "第二个指针解引用出来:" << *_obj2 << endl;
		cout << "比较结果:";
		cout << (*_obj1 < *_obj2) << endl << endl;;
	}
private:
	T1* _obj1;
	T2* _obj2;
};
template <typename T1, typename T2>
class TwoObjs<T1&, T2&>
{
public:
	TwoObjs(T1& x, T2& y)
		:_obj1(x)
		, _obj2(y)
	{}
	void LessCompare()
	{
		cout << "第一个成员(引用):" << _obj1 << endl;
		cout << "第二个成员(引用):" << _obj2 << endl;
		cout << "比较结果:";
		cout << (_obj1 < _obj2) << endl << endl;;
	}
private:
	T1& _obj1;
	T2& _obj2;
};
template <typename T>
class TwoObjs<T, size_t>
{
public:
	TwoObjs(T x, size_t y)
		:_obj1(x)
		, _obj2(y)
	{}
	void LessCompare()
	{
		cout << "第二个成员是无符号整形噢" << endl;
		cout << "第一个成员:" << _obj1 << endl;
		cout << "第二个成员:" << _obj2 << endl;
		cout << "比较结果:";
		cout << (_obj1 < _obj2) << endl << endl;;
	}
private:
	T _obj1;
	size_t _obj2;
};


int main()
{
	TwoObjs<int, int> a1(1, 2);
	a1.LessCompare();

	TwoObjs<double, double> a2(2.1, 1.9);
	a2.LessCompare();

	TwoObjs<int, char> a3(1, 'L');
	a3.LessCompare();
	cout << endl;

	TwoObjs<char, int> a4('D', 10);
	a4.LessCompare();
	cout << endl;

	int number1 = 10;
	int number2 = 20;
	TwoObjs<int*, int*> a5(&number1,&number2);
	a5.LessCompare();

	int number3 = 99;
	double number4 = 89.99;
	TwoObjs<int&, double&> a6(number3, number4);
	a6.LessCompare();

	int number5 = -1;
	size_t number6 = 1;
	TwoObjs<int,size_t> a7(number5, number6);
	a7.LessCompare();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

絕知此事要躬行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值