一、可变参数类模板的特化
1.1可变参数类模板的特化
可变参数模板不存在特化,只有偏特化。
以下是一个特化的示例:
//可变参模板的偏特化//泛化版本
template<typename... args>
class myclasst {
public:myclasst() {std::cout << "myclasst泛化版本执行了,this = " << this << " sizeof...(args) = "<< sizeof...(args) << "\n";}
};//偏特化版本1
template<typename First,typename... Others>
class myclasst<First, Others...> {
public:myclasst() {std::cout << "myclasst<First,Others>偏特化版本执行了,this = " << this<< " sizeof...(args) = "<< sizeof...(Others) << "\n";}
};//偏特化版本2
template<typename arg>
class myclasst<arg> {
public:myclasst() {std::cout << "myclasst<arg>偏特化版本执行了,this = " << this << "\n";}};//偏特化版本3
template<typename arg1, typename arg2>
class myclasst<arg1,arg2> {
public:myclasst() {std::cout << "myclasst<arg1,arg2>偏特化版本执行了,this = " << this << "\n";}
};//偏特化版本4
template<>
class myclasst<> {
public:myclasst() {std::cout << "myclasst<>偏特化版本执行了,this = " << this << "\n";}
};
可以发现,我们这里的偏特化版本都是基于参数范围上的特化。
调用测试函数:
void Test() {myclasst<>myc0;myclasst<int>myc1;myclasst<int,char>myc2;myclasst<double, int, char>myc3;myclasst<double, int, char,long long>myc4;}
可以发现,调用的顺序是基于偏特化参数范围优先的: