在STL中為了提供通用的操作而又不損失效率,我們用到了一種特殊的技巧,叫traits編程技巧。具體的來(lái)說(shuō),traits就是
通過(guò)定義一些結(jié)構(gòu)體或類,并利用模板類特化和偏特化的能力,給類型賦予一些特性,這些特性根據(jù)類型的不同而異。在程序設(shè)計(jì)中可以使用這些traits來(lái)判
斷一個(gè)類型的一些特性,引發(fā)C++的函數(shù)重載機(jī)制,實(shí)現(xiàn)同一種操作因類型不同而異的效果。traits的編程技巧極度彌補(bǔ)了C++語(yǔ)言的不足 。
舉例:
現(xiàn)在定義一個(gè)__type_traits可以獲得類型的如下屬性:
1. 是否存在non-trivial default constructor
2. 是否存在non-trivial copy constructor
3. 是否存在non-trivial assignment operator
4. 是否存在non-trivial destructor
struct __true_type {
};
struct __false_type {
};
template <class _Tp>
struct __type_traits {
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;
};
問(wèn)題:為什么把對(duì)象的所有的屬性都定義為_(kāi)_false_type?
這樣是采用最保守的做法,先把所有的對(duì)象屬性都設(shè)置為_(kāi)_false_type,然后在針對(duì)每個(gè)基本數(shù)據(jù)類型設(shè)計(jì)特化的__type_traits,就可以達(dá)到預(yù)期的目的,如可以定義__type_traits<int>如下:
template <>
struct __type_traits<int> {
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
};
template <>
struct __type_traits<char> {
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
};
......
......
其他基本類型的traits也可以有相應(yīng)的定義
__type_traits的偏特化版本
template <class _Tp>
struct __type_traits<_Tp*> {
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
我們可以自定義__type_traits的特化版本
比如對(duì)與自定義的Shape類型,我們可以這樣定義__type_traits<Shape>
struct __type_traits<Shape> {
typedef __false_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
如果編譯器夠厲害,我們甚至可以不用自己去定義特化的__type_traits,編譯器就能夠幫我們搞定:)
如何使用呢?
假設(shè)現(xiàn)在用個(gè)模板函數(shù)fun需要根據(jù)類型T是否有non-trivial constructor來(lái)進(jìn)行不同的操作,可以這樣來(lái)實(shí)現(xiàn):
template<class T>
void fun()
{
typedef typename __type_traits<T>::has_trivial_constructor _Trivial_constructor;
__fun(_Trivial_constructor()); // 根據(jù)得到的_Trivial_constructor來(lái)調(diào)用相應(yīng)的函數(shù)
}
// 兩個(gè)重載的函數(shù)
void _fun(_true_type)
{
cout<<"fun(_true_type)called"<<endl;
}
void _fun(_false_type)
{
cout<<"fun(_false_type) called"<<endl;
}
//測(cè)試代碼
int main()
{
fun<char>();
fun<int>();
fun<char *>();
fun<double>();
}
posted on 2010-10-11 09:24
小果子 閱讀(1613)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
C++