//函数模板通用版
template<typename T>
void fun(T a)
{
cout << "The main template function(): " << a << endl;
}
//对int类型特例化
template<>
void fun(int a)
{
cout << "Specialized template for int type: " << a << endl;
}
//类模板通用版
template<typename T1,typename T2>
class test {
public:
void print()
{
cout << "General template" << endl;
}
};
//特例化版
template<>
class test<char, char> {
public:
void print()
{
cout << "Specialized template: <char,char>" << endl;
}
};
//偏特化版
template<typename T1>
class test <T1,int>{
public:
void print()
{
cout << "Particial specialization : <T1,int>" << endl;
}
};
template<typename T2>
class test<int ,T2> {
public:
void print()
{
cout << "Partical specialization : <int,T2>" << endl;
}
};
//特例化成员而不是类
template<typename T>class Foo {
public:
void print()
{
cout << "General template" << endl;
}
};
template<>
void Foo<int>::print()
{
cout << "Specializer member not the class" << endl;
}
模板特例化,偏特化
最新推荐文章于 2022-11-22 13:27:48 发布