【C++】模版
1.非类型模版参数
模板参数分类类型形参与非类型形参。
类型形参即:出现在模板参数列表中,跟在class或者typename之类的参数类型名称。
非类型形参:用一个常量作为类(函数)模板的一个参数,在类(函数)模板中可将该参数当成常
量来使用。
注意:在C++20之前,只允许整形做非类型模板参数,C++20之后,可以支持double类型等其他内置类型,但不支持自定义类型做非类型模板参数
template<class T,size_t N = 10>
class Stack
{
public:
void func()
{
//N++;//不支持,N是常量
}
private:
int a[N];
int _top;
};
template<double d>
class A
{};
int main()
{
Stack<int, 10> st1;
Stack<int, 100> st2;
A<1.1> a;
st1.func();//没有调用函数时编译N++不会报错,因为按需实例化
//不调用不报错,没有使用就没有实例化
}
注意:在用浮点数作为非类型模版参数的时候,需要把编译器支持的C++标准调为C++20才可运行。
(右键创建的C++源文件,点击属性即可看到)
1.1 类模版实例化(typename的妙用)
在编译的时候,类模版是不会被实例化的,编译器就不会检查类模版中的一些细节,比如下面的代码:在vector中的const_iterator是不能被确定是变量还是类型的,这时候就需要用typename来进行明确其是类型
template<class T>
void PrintVector(const vector<T>& v)
{
//类模版没有实例化,不去里面查细节
//vector<T>::const_iterator it = v.begin();//不可以,没有去类里面去看是类型还是变量,编译器不能确定
typename vector<T>::const_iterator it = v.begin();
//加typename明确告诉是类型
//auto it = v.begin();//可以,auto是类型
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
int main()
{
vector<int> v1 = {1,2,3,4,5,6};
PrintVector(v1);
return 0;
}
2.模版的特化
在原模版类的基础上,针对特殊类型进行特殊化实现方式。模版特化就分为函数模版特化和类模版特化
2.1函数模版特化
通常情况下,使用模板可以实现一些与类型无关的代码,但对于一些特殊类型的可能会得到一些
错误的结果,需要特殊处理,比如:实现了一个专门用来进行小于比较的函数模板。
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
~Date()
{
cout << "~Date():" << this << endl;
}
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
cout << "Date(const Date& d):" << this << endl;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day < d._day)
{
return true;
}
}
}
return false;
}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "-" << d._month << "-" << d._day << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
template<class T>
bool Less(const T& l, const T& r)
{
return l < r;
}
//对函数模板进行特化
template<>
bool Less<Date*>(Date* const & l, Date* const& r)
{
return *l < *r;
}
int main()
{
cout << Less(1, 2) << endl;
Date d1(2022, 3, 4);
Date d2(2022, 2, 5);
cout << Less(d1, d2) << endl;
Date* p1 = &d1;
Date* p2 = &d2;
cout << Less(p1, p2) << endl;
return 0;
}
这里就用到了函数模版的特化,对Date*类型进行函数模版的特化,比较指针指向的日期的大小。
(这是由于普通函数模版不能满足参数为Date*类型的参数,于是就写了一个特化版本来实现比较)
2.2类模版特化
函数模板的特化步骤:
- 必须要先有一个基础的函数模板
- 关键字template后面接一对空的尖括号<>
- 函数名后跟一对尖括号,尖括号中指定需要特化的类型
- 函数形参表: 必须要和模板函数的基础参数类型完全相同,如果不同编译器可能会报一些奇怪的错误。
2.2.1全特化
全特化指模版参数列表中所有参数都确定化
template<class A, class B>
class Date
{
public:
Date()
{
cout << "Date<A,B>-原模板" << endl;
}
};
//类模版特化
template<>
class Date<int,char>
{
public:
Date()
{
cout << "Date<int,char>-全特化" << endl;
}
};
int main()
{
Date<int, char> d3;//调用全特化
}
这里可能有人就要问了:为什么d3两个模版参数都匹配,为什么要调用全特化呢?
那是因为在多个类模版中,编译器就会选择更适合口味的那一个类模版参数进行调用。
2.2.2 偏特化
偏特化:任何模版参数进行条件限制设计的特化版本。
偏特化有以下两种表现:
- 部分特化
将模版参数类表中的一部分参数特化template <class T1> class Data<T1, int> { public: Data() {cout<<"Data<T1, int>" <<endl;} private: T1 _d1; int _d2; };
- 参数进一步限制
对模版参数进一步的限制条件设计出来的一个特化版本//两个参数偏特化为指针类型 template <typename T1, typename T2> class Data <T1*, T2*> { public: Data() {cout<<"Data<T1*, T2*>" <<endl;} private: T1 _d1; T2 _d2; }; //两个参数偏特化为引用类型 template <typename T1, typename T2> class Data <T1&, T2&> { public: Data(const T1& d1, const T2& d2) : _d1(d1) , _d2(d2) { cout<<"Data<T1&, T2&>" <<endl; } private: const T1 & _d1; const T2 & _d2; };
2.2.3 测试
template<class A, class B>
class Date
{
public:
Date()
{
cout << "Date<A,B>-原模板" << endl;
}
};
//类模版特化
template<>
class Date<int,char>
{
public:
Date()
{
cout << "Date<int,char>-全特化" << endl;
}
};
template<class T>
class Date<T, char>
{
public:
Date()
{
cout << "Date<T,char>-偏特化" << endl;
}
};
template<class T>
class Date<T,int>
{
public:
Date()
{
cout << "Date<T,int>-偏特化" << endl;
}
};
template<class T1,class T2>
class Date<T1*, T2*>//只要是指针就调用这个
{
public:
Date()
{
cout << typeid(T1).name() << endl;
cout << typeid(T2).name() << endl;
cout << "Date<T1*, T2*>-偏特化" << endl;
}
};
template<class T1, class T2>
class Date<T1&, T2&>//只要是指针就调用这个
{
public:
Date()
{
cout << typeid(T1).name() << endl;
cout << typeid(T2).name() << endl;
cout << "Date<T1&, T2&>-偏特化" << endl;
}
};
int main()
{
Date<int,int> d1;//调用偏特化
Date<int*, char*> d4;
Date<int*, double*> d5;
Date<int&, double&> d6;
}

3.模板分离编译
3.1什么是分离编译
一个程序由若干个源文件组成,不同源文件单独编译形成目标文件,最后将所有目标文件链接起来形成单一的可执行文件的过程称为分离编译模式。
3.2 模板的分离编译模式
//模版分离编译是不被允许的
// a.h
template<class T>
T Add(const T& left, const T& right);
// a.cpp
template<class T>
T Add(const T& left, const T& right)
{
return left + right;
}
// main.cpp
#include"a.h"
int main()
{
Add(1, 2);
Add(1.0, 2.0);
return 0;
}
分析:
结论:
函数模板只有在被调用时候才会被实例化,如果采用分离编译模式,就会导致找不到对应的函数地址,类模板也是一样,只有在被使用时才会被实例化,例如,定义一个对象、计算对象大小、动态分配内存等操作会触发实例化 。
解决方案:
1. 将声明和定义放到一个文件 "xxx.hpp" 里面或者xxx.h其实也是可以的。推荐使用这种。
2. 模板定义的位置显式实例化。这种方法不实用,不推荐使用。
//a.cpp
template<class T>
T Add(const T& left, const T& right)
{
return left + right;
}
int Add(const int& left, const int& right)
{
return left + right;
}
double Add(const double& left, const double& right)
{
return left + right;
}
//a.h
template<class T>
T Add(const T& left, const T& right);
int Add(const int& left, const int& right);
double Add(const double& left, const double& right);
//main.cpp
#include"a.h"
int main()
{
Add(1, 2);
Add(1.0, 2.0);
return 0;
}
4.模板总结
【优点】
1. 模板复用了代码,节省资源,更快的迭代开发,C++的标准模板库(STL)因此而产生
2. 增强了代码的灵活性
【缺陷】
1. 模板会导致代码膨胀问题,也会导致编译时间变长
2. 出现模板编译错误时,错误信息非常凌乱,不易定位错误