函数模板特化 用法
#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;
}