STL 偏特化設(shè)計(jì)
概念:如果class template 中存在一個(gè)或者多個(gè)template參數(shù),可以針對(duì)其中某個(gè)或者幾個(gè)洒闸,但不是全部的template參數(shù)進(jìn)行特化工作檀蹋。
- 迭代器型別 value_type,指的是迭代器所指的對(duì)象的型別
typedef T value_type
- difference_type 表示兩個(gè)迭代器之間的距離耻台,可以用來表示一個(gè)容器的最大容量(連續(xù)空間上,頭尾之間的距離)刑然,stl中的count(),表示的結(jié)果就是difference_type
typedef typename T::difference_type difference_type 一般情況 typedef ptrdiff_t difference_type 原生指針 //任何時(shí)候需要任何迭代器I 的difference_type 可以寫成 typename iterator_traits<I>::difference_type
- reference type 引用類型
typedef T& reference;
- pointer type 指針類型
typedef T& pointer;
- iterator_category; 標(biāo)記類型
typedef xxx_iterator_tag iterator_category;
例如:
template <class Iterator>
struct iterator_traits {
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};
注意點(diǎn):
設(shè)計(jì)了五個(gè)標(biāo)簽:
struct input_iterator_tag {}; //只讀
struct output_iterator_tag {}; //只寫
struct forward_iterator_tag : public input_iterator_tag {}; // 可讀寫
struct bidirectional_iterator_tag : public forward_iterator_tag {}; // 雙向移動(dòng)
struct random_access_iterator_tag : public bidirectional_iterator_tag {}; // 隨機(jī)讀寫省古,即是所有可操作行為-
標(biāo)簽用來標(biāo)記重載函數(shù)特征例如
template <class InputIterator, class Distance> inline void __advance(InputIterator& i, Distance n, input_iterator_tag) { //逐一前進(jìn) while (n--) ++i; } template <class BidirectionalIterator, class Distance> inline void __advance(BidirectionalIterator& i, Distance n, bidirectional_iterator_tag) { if (n >= 0) while (n--) ++i; else while (n++) --i; } template <class RandomAccessIterator, class Distance> inline void __advance(RandomAccessIterator& i, Distance n, random_access_iterator_tag) { i += n; }
以及計(jì)算兩個(gè)迭代器的distance()函數(shù)
以上兩者詮釋了迭代器分為五類的設(shè)計(jì)與應(yīng)用隐砸,利用iterator_traits城看,將迭代器萃取出來,結(jié)果就是講類似的進(jìn)行標(biāo)記佛南,然后通過各個(gè)iterator_traits形式通過內(nèi)嵌的類型,和template進(jìn)行參數(shù)推導(dǎo),尤其是iterator_category等提取驗(yàn)證
taits,通過將類型等信息聲明為局部變量紊遵,對(duì)外接口func,實(shí)際操作有funcImpl執(zhí)行,根據(jù)funcImpl的參數(shù)類型進(jìn)行推導(dǎo)侥蒙,沒有返回值的易于理解處理暗膜,可以直接通過參數(shù)個(gè)數(shù),參數(shù)誒下進(jìn)行處理 模板參數(shù)類型推導(dǎo)
template<typename Iterator>
typename Iterator::value_type //返回類型
function(Iterator iter)
{
return *iter;
}
//通過iterator_traits作用后的版本
template<typename Iterator>
typename iterator_traits<Iterator>::value_type //返回類型
function(Iterator iter)
{
return *iter;
}
利用typename告訴編譯器這是個(gè)類型鞭衩,保證編譯順利学搜,指針沒辦法進(jìn)行定義內(nèi)嵌的類型(value_type等)通過特化處理,就是直接將<T*>進(jìn)行替代<T>進(jìn)行特化處理
- 繼承迭代處理
template <typename T>
class ListIter : public std::iterator<std::forward_iterator_tag, T>
{
}
標(biāo)記登記:
- 標(biāo)記目標(biāo)函數(shù)(一般模板)具備的特性论衍,返回類型瑞佩,傳入類型類別
- 登記目標(biāo)函數(shù)(特化模板)傳入?yún)?shù)的特征,返回特征坯台,返回標(biāo)記
template <class type>
struct __type_traits {
typedef __true_type this_dummy_member_must_be_first;
typedef __false_type has_trivial_default_constructor;
typedef __false_type has_trivial_copy_constructor;
typedef __false_type has_trivial_assignment_operator;
typedef __false_type has_trivial_destructor;
typedef __false_type is_POD_type;
};
has_trivial_xxx:判斷準(zhǔn)則:class內(nèi)含指針成員炬丸,并且對(duì)改成員進(jìn)行了內(nèi)存動(dòng)態(tài)配置,分配的操作類型判定has_trivial_xxx