traits

1.STL萃取机利用Traits技术可以区别迭代器与普通指针

2.STL萃取机利用Traits技术可以获取迭代器内部数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

using namespace std;


template <class >
struct iterators
{
typedef T type;
};

//迭代器萃取机
template <class >
struct traits
{
typedef typename T::type type;
};

//普通指针萃取机
template <class >
struct traits<T*>
{
typedef T* type;
};

//获取萃取机类型
template <class >
typename traits<T>::type get(const T&)
{
return typename traits<T>::type();
}

//类型重载函数
void fun(int)
{
cout<<"This is Iterator"<<endl;
}
void fun(int*)
{
cout<<"This is Pointer"<<endl;
}

int main()
{
iterators<int> it1;
int* it2;
fun(get(it1)); //This is Iterator
fun(get(it2)); //This is Pointer
return 0;
}

注:对此文章若有问题请往issue留言(ps:注明title为traits)