4 * Silicon Graphics Computer Systems, Inc.
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Silicon Graphics makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
15 #ifndef __TYPE_TRAITS_H
16 #define __TYPE_TRAITS_H
18 #ifndef __STL_CONFIG_H
19 #include <stl_config.h>
23 This header file provides a framework for allowing compile time dispatch
24 based on type attributes. This is useful when writing template code.
25 For example, when making a copy of an array of an unknown type, it helps
26 to know if the type has a trivial copy constructor or not, to help decide
27 if a memcpy can be used.
29 The class template __type_traits provides a series of typedefs each of
30 which is either __true_type or __false_type. The argument to
31 __type_traits can be any type. The typedefs within this template will
32 attain their correct values by one of these means:
33 1. The general instantiation contain conservative values which work
35 2. Specializations may be declared to make distinctions between types.
36 3. Some compilers (such as the Silicon Graphics N32 and N64 compilers)
37 will automatically provide the appropriate specializations for all
42 //Copy an array of elements which have non-trivial copy constructors
43 template <class T> void copy(T* source, T* destination, int n, __false_type);
44 //Copy an array of elements which have trivial copy constructors. Use memcpy.
45 template <class T> void copy(T* source, T* destination, int n, __true_type);
47 //Copy an array of any type by using the most efficient copy mechanism
48 template <class T> inline void copy(T* source,T* destination,int n) {
49 copy(source, destination, n,
50 typename __type_traits<T>::has_trivial_copy_constructor());
62 struct __type_traits
{
63 typedef __true_type this_dummy_member_must_be_first
;
64 /* Do not remove this member. It informs a compiler which
65 automatically specializes __type_traits that this
66 __type_traits template is special. It just makes sure that
67 things work if an implementation is using a template
68 called __type_traits for something unrelated. */
70 /* The following restrictions should be observed for the sake of
71 compilers which automatically produce type specific specializations
73 - You may reorder the members below if you wish
74 - You may remove any of the members below if you wish
75 - You must not rename members without making the corresponding
76 name change in the compiler
77 - Members you add will be treated like regular members unless
78 you add the appropriate support in the compiler. */
81 typedef __false_type has_trivial_default_constructor
;
82 typedef __false_type has_trivial_copy_constructor
;
83 typedef __false_type has_trivial_assignment_operator
;
84 typedef __false_type has_trivial_destructor
;
85 typedef __false_type is_POD_type
;
90 // Provide some specializations. This is harmless for compilers that
91 // have built-in __types_traits support, and essential for compilers
96 __STL_TEMPLATE_NULL
struct __type_traits
<bool> {
97 typedef __true_type has_trivial_default_constructor
;
98 typedef __true_type has_trivial_copy_constructor
;
99 typedef __true_type has_trivial_assignment_operator
;
100 typedef __true_type has_trivial_destructor
;
101 typedef __true_type is_POD_type
;
104 #endif /* __STL_NO_BOOL */
106 __STL_TEMPLATE_NULL
struct __type_traits
<char> {
107 typedef __true_type has_trivial_default_constructor
;
108 typedef __true_type has_trivial_copy_constructor
;
109 typedef __true_type has_trivial_assignment_operator
;
110 typedef __true_type has_trivial_destructor
;
111 typedef __true_type is_POD_type
;
114 __STL_TEMPLATE_NULL
struct __type_traits
<signed char> {
115 typedef __true_type has_trivial_default_constructor
;
116 typedef __true_type has_trivial_copy_constructor
;
117 typedef __true_type has_trivial_assignment_operator
;
118 typedef __true_type has_trivial_destructor
;
119 typedef __true_type is_POD_type
;
122 __STL_TEMPLATE_NULL
struct __type_traits
<unsigned char> {
123 typedef __true_type has_trivial_default_constructor
;
124 typedef __true_type has_trivial_copy_constructor
;
125 typedef __true_type has_trivial_assignment_operator
;
126 typedef __true_type has_trivial_destructor
;
127 typedef __true_type is_POD_type
;
130 #ifdef __STL_HAS_WCHAR_T
132 __STL_TEMPLATE_NULL
struct __type_traits
<wchar_t> {
133 typedef __true_type has_trivial_default_constructor
;
134 typedef __true_type has_trivial_copy_constructor
;
135 typedef __true_type has_trivial_assignment_operator
;
136 typedef __true_type has_trivial_destructor
;
137 typedef __true_type is_POD_type
;
140 #endif /* __STL_HAS_WCHAR_T */
142 __STL_TEMPLATE_NULL
struct __type_traits
<short> {
143 typedef __true_type has_trivial_default_constructor
;
144 typedef __true_type has_trivial_copy_constructor
;
145 typedef __true_type has_trivial_assignment_operator
;
146 typedef __true_type has_trivial_destructor
;
147 typedef __true_type is_POD_type
;
150 __STL_TEMPLATE_NULL
struct __type_traits
<unsigned short> {
151 typedef __true_type has_trivial_default_constructor
;
152 typedef __true_type has_trivial_copy_constructor
;
153 typedef __true_type has_trivial_assignment_operator
;
154 typedef __true_type has_trivial_destructor
;
155 typedef __true_type is_POD_type
;
158 __STL_TEMPLATE_NULL
struct __type_traits
<int> {
159 typedef __true_type has_trivial_default_constructor
;
160 typedef __true_type has_trivial_copy_constructor
;
161 typedef __true_type has_trivial_assignment_operator
;
162 typedef __true_type has_trivial_destructor
;
163 typedef __true_type is_POD_type
;
166 __STL_TEMPLATE_NULL
struct __type_traits
<unsigned int> {
167 typedef __true_type has_trivial_default_constructor
;
168 typedef __true_type has_trivial_copy_constructor
;
169 typedef __true_type has_trivial_assignment_operator
;
170 typedef __true_type has_trivial_destructor
;
171 typedef __true_type is_POD_type
;
174 __STL_TEMPLATE_NULL
struct __type_traits
<long> {
175 typedef __true_type has_trivial_default_constructor
;
176 typedef __true_type has_trivial_copy_constructor
;
177 typedef __true_type has_trivial_assignment_operator
;
178 typedef __true_type has_trivial_destructor
;
179 typedef __true_type is_POD_type
;
182 __STL_TEMPLATE_NULL
struct __type_traits
<unsigned long> {
183 typedef __true_type has_trivial_default_constructor
;
184 typedef __true_type has_trivial_copy_constructor
;
185 typedef __true_type has_trivial_assignment_operator
;
186 typedef __true_type has_trivial_destructor
;
187 typedef __true_type is_POD_type
;
190 #ifdef __STL_LONG_LONG
192 __STL_TEMPLATE_NULL
struct __type_traits
<long long> {
193 typedef __true_type has_trivial_default_constructor
;
194 typedef __true_type has_trivial_copy_constructor
;
195 typedef __true_type has_trivial_assignment_operator
;
196 typedef __true_type has_trivial_destructor
;
197 typedef __true_type is_POD_type
;
200 __STL_TEMPLATE_NULL
struct __type_traits
<unsigned long long> {
201 typedef __true_type has_trivial_default_constructor
;
202 typedef __true_type has_trivial_copy_constructor
;
203 typedef __true_type has_trivial_assignment_operator
;
204 typedef __true_type has_trivial_destructor
;
205 typedef __true_type is_POD_type
;
208 #endif /* __STL_LONG_LONG */
210 __STL_TEMPLATE_NULL
struct __type_traits
<float> {
211 typedef __true_type has_trivial_default_constructor
;
212 typedef __true_type has_trivial_copy_constructor
;
213 typedef __true_type has_trivial_assignment_operator
;
214 typedef __true_type has_trivial_destructor
;
215 typedef __true_type is_POD_type
;
218 __STL_TEMPLATE_NULL
struct __type_traits
<double> {
219 typedef __true_type has_trivial_default_constructor
;
220 typedef __true_type has_trivial_copy_constructor
;
221 typedef __true_type has_trivial_assignment_operator
;
222 typedef __true_type has_trivial_destructor
;
223 typedef __true_type is_POD_type
;
226 __STL_TEMPLATE_NULL
struct __type_traits
<long double> {
227 typedef __true_type has_trivial_default_constructor
;
228 typedef __true_type has_trivial_copy_constructor
;
229 typedef __true_type has_trivial_assignment_operator
;
230 typedef __true_type has_trivial_destructor
;
231 typedef __true_type is_POD_type
;
234 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
237 struct __type_traits
<_Tp
*> {
238 typedef __true_type has_trivial_default_constructor
;
239 typedef __true_type has_trivial_copy_constructor
;
240 typedef __true_type has_trivial_assignment_operator
;
241 typedef __true_type has_trivial_destructor
;
242 typedef __true_type is_POD_type
;
245 #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
247 __STL_TEMPLATE_NULL
struct __type_traits
<char*> {
248 typedef __true_type has_trivial_default_constructor
;
249 typedef __true_type has_trivial_copy_constructor
;
250 typedef __true_type has_trivial_assignment_operator
;
251 typedef __true_type has_trivial_destructor
;
252 typedef __true_type is_POD_type
;
255 __STL_TEMPLATE_NULL
struct __type_traits
<signed char*> {
256 typedef __true_type has_trivial_default_constructor
;
257 typedef __true_type has_trivial_copy_constructor
;
258 typedef __true_type has_trivial_assignment_operator
;
259 typedef __true_type has_trivial_destructor
;
260 typedef __true_type is_POD_type
;
263 __STL_TEMPLATE_NULL
struct __type_traits
<unsigned char*> {
264 typedef __true_type has_trivial_default_constructor
;
265 typedef __true_type has_trivial_copy_constructor
;
266 typedef __true_type has_trivial_assignment_operator
;
267 typedef __true_type has_trivial_destructor
;
268 typedef __true_type is_POD_type
;
271 __STL_TEMPLATE_NULL
struct __type_traits
<const char*> {
272 typedef __true_type has_trivial_default_constructor
;
273 typedef __true_type has_trivial_copy_constructor
;
274 typedef __true_type has_trivial_assignment_operator
;
275 typedef __true_type has_trivial_destructor
;
276 typedef __true_type is_POD_type
;
279 __STL_TEMPLATE_NULL
struct __type_traits
<const signed char*> {
280 typedef __true_type has_trivial_default_constructor
;
281 typedef __true_type has_trivial_copy_constructor
;
282 typedef __true_type has_trivial_assignment_operator
;
283 typedef __true_type has_trivial_destructor
;
284 typedef __true_type is_POD_type
;
287 __STL_TEMPLATE_NULL
struct __type_traits
<const unsigned char*> {
288 typedef __true_type has_trivial_default_constructor
;
289 typedef __true_type has_trivial_copy_constructor
;
290 typedef __true_type has_trivial_assignment_operator
;
291 typedef __true_type has_trivial_destructor
;
292 typedef __true_type is_POD_type
;
295 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
298 // The following could be written in terms of numeric_limits.
299 // We're doing it separately to reduce the number of dependencies.
301 template <class _Tp
> struct _Is_integer
{
302 typedef __false_type _Integral
;
305 #ifndef __STL_NO_BOOL
307 __STL_TEMPLATE_NULL
struct _Is_integer
<bool> {
308 typedef __true_type _Integral
;
311 #endif /* __STL_NO_BOOL */
313 __STL_TEMPLATE_NULL
struct _Is_integer
<char> {
314 typedef __true_type _Integral
;
317 __STL_TEMPLATE_NULL
struct _Is_integer
<signed char> {
318 typedef __true_type _Integral
;
321 __STL_TEMPLATE_NULL
struct _Is_integer
<unsigned char> {
322 typedef __true_type _Integral
;
325 #ifdef __STL_HAS_WCHAR_T
327 __STL_TEMPLATE_NULL
struct _Is_integer
<wchar_t> {
328 typedef __true_type _Integral
;
331 #endif /* __STL_HAS_WCHAR_T */
333 __STL_TEMPLATE_NULL
struct _Is_integer
<short> {
334 typedef __true_type _Integral
;
337 __STL_TEMPLATE_NULL
struct _Is_integer
<unsigned short> {
338 typedef __true_type _Integral
;
341 __STL_TEMPLATE_NULL
struct _Is_integer
<int> {
342 typedef __true_type _Integral
;
345 __STL_TEMPLATE_NULL
struct _Is_integer
<unsigned int> {
346 typedef __true_type _Integral
;
349 __STL_TEMPLATE_NULL
struct _Is_integer
<long> {
350 typedef __true_type _Integral
;
353 __STL_TEMPLATE_NULL
struct _Is_integer
<unsigned long> {
354 typedef __true_type _Integral
;
357 #ifdef __STL_LONG_LONG
359 __STL_TEMPLATE_NULL
struct _Is_integer
<long long> {
360 typedef __true_type _Integral
;
363 __STL_TEMPLATE_NULL
struct _Is_integer
<unsigned long long> {
364 typedef __true_type _Integral
;
367 #endif /* __STL_LONG_LONG */
369 #endif /* __TYPE_TRAITS_H */