仿SGI STL的traits技法

首先是iterator traits,这个是用来萃取迭代器的特性的

 1 #ifndef _STL_ITERATOR_H_
 2 #define _STL_ITERATOR_H_
 3 
 4 
 5 #include <cstddef>
 6 /*
 7 **    iterator_traits<Iterator>  ----> 负责萃取迭代器的特性
 8 */
 9 namespace zstd
10 {
11     struct inpt_iterator_tag{};
12     struct outpt_iterator_tag{};
13     struct forward_iterator_tag :public inpt_iterator_tag {};
14     struct bidirectional_iterator_tag :public forward_iterator_tag {};
15     struct random_access_iterator_tag :public bidirectional_iterator_tag {};
16 
17     template<class Category, class T, class Distance = ptrdiff_t, 
18             class Pointer = T*, class Reference = T&>
19     struct iterator
20     {
21         typedef Category    iterator_category;
22         typedef T            value_type;
23         typedef Distance    difference_type;
24         typedef Pointer        pointer;
25         typedef Reference    reference;
26     };
27 
28     template<class Iterator>
29     struct iterator_traits
30     {
31         typedef typename Iterator::Category        iterator_category;
32         typedef typename Iterator::T            value_type;
33         typedef typename Iterator::Distance        difference_type;
34         typedef typename Iterator::Pointer        pointer;
35         typedef typename Iterator::Reference     reference;
36     };
37     template<class T>
38     struct iterator_traits<T*>
39     {
40         typedef random_access_iterator_tag     iterator_category;
41         typedef T                             value_type;
42         typedef ptrdiff_t                     difference_type;
43         typedef T*                            pointer;
44         typedef T&                             reference;
45     };
46     template<class T>
47     struct iterator_traits<const T*>
48     {
49         typedef random_access_iterator_tag     iterator_category;
50         typedef T                             value_type;
51         typedef ptrdiff_t                     difference_type;
52         typedef const T*                    pointer;
53         typedef const T&                     reference;
54     };
55 
56     template<class Iterator>
57     inline typename Iterator::iterator_category
58     iterator_category(const Iterator& It)
59     {
60         typedef typename Iterator::iterator_category category;
61         return category();
62     }
63     template<class Iterator>
64     inline typename Iterator::value_type*
65     value_type(const Iterator& It)
66     {
67         return static_cast<typename iterator_traits<Iterator>::value_type*>(0);
68     }
69     template<class Iterator>
70     inline typename Iterator::difference_type*
71     difference_type(const Iterator& It)
72     {
73         return static_cast<typename iterator_traits<Iterator>::difference_type*>(0);
74     }
75 }
76 #endif

然后是type traits,这个是用来萃取c++语言型别(type)的特性的

  1 #ifndef _TYPE_TRAITS_H_
  2 #define _TYPE_TRAITS_H_
  3 
  4 /*
  5 **    _type_traits<T>  ----> 负责萃取型别T的特性
  6 */
  7 #include <iostream>
  8 using namespace std;
  9 namespace zstd
 10 {
 11     struct _true_type { void print(){ cout << "_true_type" << endl; } };
 12     struct _false_type { void print(){ cout << "_false_type" << endl; } };
 13 
 14     template<class T>
 15     struct _type_traits
 16     {
 17         typedef _false_type        has_trivial_default_constructor;
 18         typedef _false_type        has_trivial_copy_constructor;
 19         typedef _false_type        has_trivial_assignment_operator;
 20         typedef _false_type        has_trivial_destructor;
 21         typedef _false_type        is_POD_type;
 22     };
 23 
 24     template<>
 25     struct _type_traits<bool>
 26     {
 27         typedef _true_type        has_trivial_default_constructor;
 28         typedef _true_type        has_trivial_copy_constructor;
 29         typedef _true_type        has_trivial_assignment_operator;
 30         typedef _true_type        has_trivial_destructor;
 31         typedef _true_type        is_POD_type;
 32     };
 33     template<>
 34     struct _type_traits<char>
 35     {
 36         typedef _true_type        has_trivial_default_constructor;
 37         typedef _true_type        has_trivial_copy_constructor;
 38         typedef _true_type        has_trivial_assignment_operator;
 39         typedef _true_type        has_trivial_destructor;
 40         typedef _true_type        is_POD_type;
 41     };
 42     template<>
 43     struct _type_traits<unsigned char>
 44     {
 45         typedef _true_type        has_trivial_default_constructor;
 46         typedef _true_type        has_trivial_copy_constructor;
 47         typedef _true_type        has_trivial_assignment_operator;
 48         typedef _true_type        has_trivial_destructor;
 49         typedef _true_type        is_POD_type;
 50     };
 51     template<>
 52     struct _type_traits<signed char>
 53     {
 54         typedef _true_type        has_trivial_default_constructor;
 55         typedef _true_type        has_trivial_copy_constructor;
 56         typedef _true_type        has_trivial_assignment_operator;
 57         typedef _true_type        has_trivial_destructor;
 58         typedef _true_type        is_POD_type;
 59     };
 60     template<>
 61     struct _type_traits<wchar_t>
 62     {
 63         typedef _true_type        has_trivial_default_constructor;
 64         typedef _true_type        has_trivial_copy_constructor;
 65         typedef _true_type        has_trivial_assignment_operator;
 66         typedef _true_type        has_trivial_destructor;
 67         typedef _true_type        is_POD_type;
 68     };
 69     template<>
 70     struct _type_traits<short>
 71     {
 72         typedef _true_type        has_trivial_default_constructor;
 73         typedef _true_type        has_trivial_copy_constructor;
 74         typedef _true_type        has_trivial_assignment_operator;
 75         typedef _true_type        has_trivial_destructor;
 76         typedef _true_type        is_POD_type;
 77     };
 78     template<>
 79     struct _type_traits<unsigned short>
 80     {
 81         typedef _true_type        has_trivial_default_constructor;
 82         typedef _true_type        has_trivial_copy_constructor;
 83         typedef _true_type        has_trivial_assignment_operator;
 84         typedef _true_type        has_trivial_destructor;
 85         typedef _true_type        is_POD_type;
 86     };
 87     template<>
 88     struct _type_traits<int>
 89     {
 90         typedef _true_type        has_trivial_default_constructor;
 91         typedef _true_type        has_trivial_copy_constructor;
 92         typedef _true_type        has_trivial_assignment_operator;
 93         typedef _true_type        has_trivial_destructor;
 94         typedef _true_type        is_POD_type;
 95     };
 96     template<>
 97     struct _type_traits<unsigned int>
 98     {
 99         typedef _true_type        has_trivial_default_constructor;
100         typedef _true_type        has_trivial_copy_constructor;
101         typedef _true_type        has_trivial_assignment_operator;
102         typedef _true_type        has_trivial_destructor;
103         typedef _true_type        is_POD_type;
104     };
105     template<>
106     struct _type_traits<long>
107     {
108         typedef _true_type        has_trivial_default_constructor;
109         typedef _true_type        has_trivial_copy_constructor;
110         typedef _true_type        has_trivial_assignment_operator;
111         typedef _true_type        has_trivial_destructor;
112         typedef _true_type        is_POD_type;
113     };
114     template<>
115     struct _type_traits<unsigned long>
116     {
117         typedef _true_type        has_trivial_default_constructor;
118         typedef _true_type        has_trivial_copy_constructor;
119         typedef _true_type        has_trivial_assignment_operator;
120         typedef _true_type        has_trivial_destructor;
121         typedef _true_type        is_POD_type;
122     };
123     template<>
124     struct _type_traits<long long>
125     {
126         typedef _true_type        has_trivial_default_constructor;
127         typedef _true_type        has_trivial_copy_constructor;
128         typedef _true_type        has_trivial_assignment_operator;
129         typedef _true_type        has_trivial_destructor;
130         typedef _true_type        is_POD_type;
131     };
132     template<>
133     struct _type_traits<unsigned long long>
134     {
135         typedef _true_type        has_trivial_default_constructor;
136         typedef _true_type        has_trivial_copy_constructor;
137         typedef _true_type        has_trivial_assignment_operator;
138         typedef _true_type        has_trivial_destructor;
139         typedef _true_type        is_POD_type;
140     };
141     template<>
142     struct _type_traits<float>
143     {
144         typedef _true_type        has_trivial_default_constructor;
145         typedef _true_type        has_trivial_copy_constructor;
146         typedef _true_type        has_trivial_assignment_operator;
147         typedef _true_type        has_trivial_destructor;
148         typedef _true_type        is_POD_type;
149     };
150     template<>
151     struct _type_traits<double>
152     {
153         typedef _true_type        has_trivial_default_constructor;
154         typedef _true_type        has_trivial_copy_constructor;
155         typedef _true_type        has_trivial_assignment_operator;
156         typedef _true_type        has_trivial_destructor;
157         typedef _true_type        is_POD_type;
158     };
159     template<>
160     struct _type_traits<long double>
161     {
162         typedef _true_type        has_trivial_default_constructor;
163         typedef _true_type        has_trivial_copy_constructor;
164         typedef _true_type        has_trivial_assignment_operator;
165         typedef _true_type        has_trivial_destructor;
166         typedef _true_type        is_POD_type;
167     };
168 
169     template<class T>
170     struct _type_traits<T*>
171     {
172         typedef _true_type        has_trivial_default_constructor;
173         typedef _true_type        has_trivial_copy_constructor;
174         typedef _true_type        has_trivial_assignment_operator;
175         typedef _true_type        has_trivial_destructor;
176         typedef _true_type        is_POD_type;
177     };
178     template<class T>
179     struct _type_traits<const T*>
180     {
181         typedef _true_type        has_trivial_default_constructor;
182         typedef _true_type        has_trivial_copy_constructor;
183         typedef _true_type        has_trivial_assignment_operator;
184         typedef _true_type        has_trivial_destructor;
185         typedef _true_type        is_POD_type;
186     };
187     template<>
188     struct _type_traits<char*>
189     {
190         typedef _true_type        has_trivial_default_constructor;
191         typedef _true_type        has_trivial_copy_constructor;
192         typedef _true_type        has_trivial_assignment_operator;
193         typedef _true_type        has_trivial_destructor;
194         typedef _true_type        is_POD_type;
195     };
196     template<>
197     struct _type_traits<unsigned char*>
198     {
199         typedef _true_type        has_trivial_default_constructor;
200         typedef _true_type        has_trivial_copy_constructor;
201         typedef _true_type        has_trivial_assignment_operator;
202         typedef _true_type        has_trivial_destructor;
203         typedef _true_type        is_POD_type;
204     };
205     template<>
206     struct _type_traits<signed char*>
207     {
208         typedef _true_type        has_trivial_default_constructor;
209         typedef _true_type        has_trivial_copy_constructor;
210         typedef _true_type        has_trivial_assignment_operator;
211         typedef _true_type        has_trivial_destructor;
212         typedef _true_type        is_POD_type;
213     };
214     template<>
215     struct _type_traits<const char*>
216     {
217         typedef _true_type        has_trivial_default_constructor;
218         typedef _true_type        has_trivial_copy_constructor;
219         typedef _true_type        has_trivial_assignment_operator;
220         typedef _true_type        has_trivial_destructor;
221         typedef _true_type        is_POD_type;
222     };
223     template<>
224     struct _type_traits<const unsigned char*>
225     {
226         typedef _true_type        has_trivial_default_constructor;
227         typedef _true_type        has_trivial_copy_constructor;
228         typedef _true_type        has_trivial_assignment_operator;
229         typedef _true_type        has_trivial_destructor;
230         typedef _true_type        is_POD_type;
231     };
232     template<>
233     struct _type_traits<const signed char*>
234     {
235         typedef _true_type        has_trivial_default_constructor;
236         typedef _true_type        has_trivial_copy_constructor;
237         typedef _true_type        has_trivial_assignment_operator;
238         typedef _true_type        has_trivial_destructor;
239         typedef _true_type        is_POD_type;
240     };
241 }
242 #endif

 

posted @ 2014-02-24 19:07  老司机  阅读(641)  评论(0编辑  收藏  举报