STL是C++标准库的重要组成部分之一,它不仅是一个可复用的组件库,更是一个包含算法与数据结构的软件框架,同时也是C++泛型编程的很好例子。STL中运用了许多C++的高级技术。本文介绍模板特化技术的运用。主要参考了《C++ Primer》和《STL源码剖析》。
STL中大量运用了模块,可以说模板是创建类或函数的公式。但是,我们并不总能写出对所有可能被实例化的类型都最合适的模板。举个函数模板特化的例子。
- template <typename T>
- int Compare(const T &x, const T &y)
- {
- if(x < y)
- return -1;
- else if(x > y)
- return 1;
- else
- return 0;
- }
模板特化(template specialization)的定义为指定一个或多个模板形参的实际类型或实际值。例如可以为Compare模板函数定义一个特化版本。
- template <> //template关键字后面接空括号
- int Compare(const char * const &x, const char * const &y) //形参为指向常量的常指针的引用
- {
- return strcmp(x, y);
- }
迭代器是STL的关键所在,它将原本分开的数据容器和算法很好的胶合在一起。比如下面这个STL中的函数(摘自源码),命名上做了修改,同时略去了一些代码,但是足以说明问题。这个函数通过迭代器交换容器的数据,迭代器是数据容器和算法的桥梁,算法通过数据容器的迭代器访问容器中的数据,而不需关心容器的具体构造。
- //真正的交换函数,内部调用
- template <class Iter1, class Iter2, class T>
- inline void _iter_swap(Iter1 a, Iter2 b, T) {
- T tmp = *a;
- *a = *b;
- *b = tmp;
- }
- //交换两个迭代器所指的元素,外部接口
- template <class Iter1, class Iter2>
- inline void iter_swap(Iter1 a, Iter2 b) {
- _iter_swap(a, b, VALUE_TYPE(Iter1)); //VALUE_TYPE返回迭代器的值类型
- }
问一个问题,iter_swap这个函数的形参是迭代器,我们需要在函数内部定义一个临时变量,变量的数据类型为迭代器所指的数据类型。那么我们如何知道迭代器所指的数据类型呢?有人说,可以利用模板实参推断机制,解决这个问题。代码如下所示:
- //真正的交换函数
- template <class Iter1, class Iter2, class T>
- inline void _iter_swap(Iter1 a, Iter2 b, T) {
- T tmp = *a;
- *a = *b;
- *b = tmp;
- }
- //交换两个迭代器所指的元素
- template <class Iter1, class Iter2>
- inline void iter_swap(Iter1 a, Iter2 b) {
- _iter_swap(a, b, *a); //模板实参推断
- }
- class MyAlloc{
- };
- template<class T>
- struct List_iterator{
- typedef T value_type; //list 迭代器的内嵌型别
- ...
- };
- template <class T, class Alloc = MyAlloc>
- class list{
- public:
- typedef List_iterator<T> iterator; //list迭代器
- ...
- };
- template<class I>
- struct Iterator_traits{ //萃取剂定义
- typedef typename I::value_type value_type;
- };
- Iterator_traits<list<int>::iterator>::value_type x = 1;
- class MyAlloc{
- };
- template <class T, class Alloc = MyAlloc>
- class vector :
- {
- public:
- typedef T value_type; //内嵌型别
- typedef value_type* pointer;
- typedef const value_type* const_pointer;
- typedef value_type* iterator; //vector 迭代器,是原生指针
- typedef const value_type* const_iterator;
- typedef value_type& reference;
- typedef const value_type& const_reference;
- ...
- };
- template<class I>
- struct Iterator_traits{
- typedef typename I::value_type value_type;
- };
- //特化 原生指针
- template<class T>
- struct Iterator_traits<T*>{
- typedef T value_type;
- };
- //特化 原生常指针
- template<class T>
- struct Iterator_traits<const T*>{
- typedef T value_type;
- };
- #include <iostream>
- #include <vector>
- #include <list>
- using namespace std;
- //萃取剂
- template<class I>
- struct Iterator_traits{
- typedef typename I::value_type value_type;
- };
- //特化 原生指针
- template<class T>
- struct Iterator_traits<T*>{
- typedef T value_type;
- };
- //特化 原生常指针
- template<class T>
- struct Iterator_traits<const T*>{
- typedef T value_type;
- };
- #define VALUE_TYPE(I) Iterator_traits<I>::value_type()
- //交换两个迭代器所指的元素
- template <class Iter1, class Iter2>
- inline void iter_swap(Iter1 a, Iter2 b) {
- _iter_swap(a, b, VALUE_TYPE(Iter1)); //VALUE_TYPE返回迭代器的值类型
- }
- //真正的交换函数
- template <class Iter1, class Iter2, class T>
- inline void _iter_swap(Iter1 a, Iter2 b, T) {
- T tmp = *a;
- *a = *b;
- *b = tmp;
- }
- //测试函数
- int main()
- {
- int a = 1, b = 2;
- iter_swap(&a,&b);
- cout<<a<<' '<<b<<endl; //2 1
- list<int> l;
- l.push_back(3);
- l.push_back(4);
- iter_swap(l.begin(),++l.begin());
- cout<<*(l.begin())<<' '<<*(++l.begin())<<endl; //4 3
- Iterator_traits<int *>::value_type w = 5; //特化
- Iterator_traits<const int*>::value_type x = 6; //特化
- Iterator_traits<vector<int>::iterator>::value_type y = 7; //vector 容器
- Iterator_traits<list<int>::iterator>::value_type z = 8; //list 容器
- cout<<w<<' '<<x<<' '<<y<<' '<<z<<endl; //5 6 7 8
- return 0;
- }