public:
TClass()
{
std::cout << "类模板,偏特化" << std::endl;
}
};
/// 类的成员函数模板
class TMemFun
{
public:
template <typename T>
void Test(T)
{
std::cout << "类的成员函数模板" << std::endl;
}
};
/// 测试模板类
void Test_Class()
{
TClass<bool,char> tClass1;
tClass1.Test('a'); // 模板成员函数,一般
tClass1.Test(0); // 模板成员函数,偏特化
TClass<bool,int> tClass2; // 类偏特化
TMemFun tMemFun;
tMemFun.Test(1); // 类的成员函数模板
std::cout << std::endl;
}
/*
--- 类型自动转换 ---
1、数组与指针互转
2、限制修饰符const与非const互转
*/
/// 参数为指针
template <typename T>
void PtrFun(T*){}
/// 参数为数组
template <typename T>
void ArrayFun(T[]){}
/// const参数
template <typename T>
void ConstFun(const T){}
/// 非const参数
template <typename T>
void UnConstFun(T){}
class CBase{}; // 父类
class CDerived : public CBase{}; // 子类
template <typename T>
void ClassFun(T){}
void Test_TypeConversion()
{
int nValue = 1;
ConstFun(nValue); // 非const --> const
const int cnValue = 1;
UnConstFun(cnValue); // const --> 非const
int nArray = {0,0};
PtrFun(nArray); // 数组 --> 指针
int* pInt = NULL;
ArrayFun(pInt); // 指针 --> 数组
CDerived derived;
ClassFun<CBase>(derived); // 子类 --> 父类
// CBase base;
// ClassFun<CDerived>(base); // 不允许,父类 --> 子类
}
void Test_Template()
{
Test_Func();
Test_Class();
Test_TypeConversion();
}