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 _CPP_BITS_TYPE_TRAITS_H
16 #define _CPP_BITS_TYPE_TRAITS_H 1
18 #pragma GCC system_header
20 #include <bits/c++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 _Tp> void
44 copy(_Tp* __source,_Tp* __destination,int __n,__false_type);
45 //Copy an array of elements which have trivial copy constructors. Use memcpy.
46 template <class _Tp> void
47 copy(_Tp* __source,_Tp* __destination,int __n,__true_type);
49 //Copy an array of any type by using the most efficient copy mechanism
50 template <class _Tp> inline void copy(_Tp* __source,_Tp* __destination,int __n) {
51 copy(__source,__destination,__n,
52 typename __type_traits<_Tp>::has_trivial_copy_constructor());
57 template <bool _Truth> struct _Bool {};
58 typedef _Bool<true> __true_type;
59 typedef _Bool<false> __false_type;
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;
89 // Provide some specializations.
91 template<> struct __type_traits<bool> {
92 typedef __true_type has_trivial_default_constructor;
93 typedef __true_type has_trivial_copy_constructor;
94 typedef __true_type has_trivial_assignment_operator;
95 typedef __true_type has_trivial_destructor;
96 typedef __true_type is_POD_type;
99 template<> struct __type_traits<char> {
100 typedef __true_type has_trivial_default_constructor;
101 typedef __true_type has_trivial_copy_constructor;
102 typedef __true_type has_trivial_assignment_operator;
103 typedef __true_type has_trivial_destructor;
104 typedef __true_type is_POD_type;
107 template<> struct __type_traits<signed char> {
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;
115 template<> struct __type_traits<unsigned char> {
116 typedef __true_type has_trivial_default_constructor;
117 typedef __true_type has_trivial_copy_constructor;
118 typedef __true_type has_trivial_assignment_operator;
119 typedef __true_type has_trivial_destructor;
120 typedef __true_type is_POD_type;
123 template<> struct __type_traits<wchar_t> {
124 typedef __true_type has_trivial_default_constructor;
125 typedef __true_type has_trivial_copy_constructor;
126 typedef __true_type has_trivial_assignment_operator;
127 typedef __true_type has_trivial_destructor;
128 typedef __true_type is_POD_type;
131 template<> struct __type_traits<short> {
132 typedef __true_type has_trivial_default_constructor;
133 typedef __true_type has_trivial_copy_constructor;
134 typedef __true_type has_trivial_assignment_operator;
135 typedef __true_type has_trivial_destructor;
136 typedef __true_type is_POD_type;
139 template<> struct __type_traits<unsigned short> {
140 typedef __true_type has_trivial_default_constructor;
141 typedef __true_type has_trivial_copy_constructor;
142 typedef __true_type has_trivial_assignment_operator;
143 typedef __true_type has_trivial_destructor;
144 typedef __true_type is_POD_type;
147 template<> struct __type_traits<int> {
148 typedef __true_type has_trivial_default_constructor;
149 typedef __true_type has_trivial_copy_constructor;
150 typedef __true_type has_trivial_assignment_operator;
151 typedef __true_type has_trivial_destructor;
152 typedef __true_type is_POD_type;
155 template<> struct __type_traits<unsigned int> {
156 typedef __true_type has_trivial_default_constructor;
157 typedef __true_type has_trivial_copy_constructor;
158 typedef __true_type has_trivial_assignment_operator;
159 typedef __true_type has_trivial_destructor;
160 typedef __true_type is_POD_type;
163 template<> struct __type_traits<long> {
164 typedef __true_type has_trivial_default_constructor;
165 typedef __true_type has_trivial_copy_constructor;
166 typedef __true_type has_trivial_assignment_operator;
167 typedef __true_type has_trivial_destructor;
168 typedef __true_type is_POD_type;
171 template<> struct __type_traits<unsigned long> {
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;
179 #ifdef _GLIBCPP_USE_LONG_LONG
181 template<> struct __type_traits<long long> {
182 typedef __true_type has_trivial_default_constructor;
183 typedef __true_type has_trivial_copy_constructor;
184 typedef __true_type has_trivial_assignment_operator;
185 typedef __true_type has_trivial_destructor;
186 typedef __true_type is_POD_type;
189 template<> struct __type_traits<unsigned long long> {
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;
197 #endif /* _GLIBCPP_USE_LONG_LONG */
199 template<> struct __type_traits<float> {
200 typedef __true_type has_trivial_default_constructor;
201 typedef __true_type has_trivial_copy_constructor;
202 typedef __true_type has_trivial_assignment_operator;
203 typedef __true_type has_trivial_destructor;
204 typedef __true_type is_POD_type;
207 template<> struct __type_traits<double> {
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;
215 template<> struct __type_traits<long double> {
216 typedef __true_type has_trivial_default_constructor;
217 typedef __true_type has_trivial_copy_constructor;
218 typedef __true_type has_trivial_assignment_operator;
219 typedef __true_type has_trivial_destructor;
220 typedef __true_type is_POD_type;
224 struct __type_traits<_Tp*> {
225 typedef __true_type has_trivial_default_constructor;
226 typedef __true_type has_trivial_copy_constructor;
227 typedef __true_type has_trivial_assignment_operator;
228 typedef __true_type has_trivial_destructor;
229 typedef __true_type is_POD_type;
233 // The following could be written in terms of numeric_limits.
234 // We're doing it separately to reduce the number of dependencies.
236 template <class _Tp> struct _Is_integer {
237 typedef __false_type _Integral;
240 template<> struct _Is_integer<bool> {
241 typedef __true_type _Integral;
244 template<> struct _Is_integer<char> {
245 typedef __true_type _Integral;
248 template<> struct _Is_integer<signed char> {
249 typedef __true_type _Integral;
252 template<> struct _Is_integer<unsigned char> {
253 typedef __true_type _Integral;
256 template<> struct _Is_integer<wchar_t> {
257 typedef __true_type _Integral;
260 template<> struct _Is_integer<short> {
261 typedef __true_type _Integral;
264 template<> struct _Is_integer<unsigned short> {
265 typedef __true_type _Integral;
268 template<> struct _Is_integer<int> {
269 typedef __true_type _Integral;
272 template<> struct _Is_integer<unsigned int> {
273 typedef __true_type _Integral;
276 template<> struct _Is_integer<long> {
277 typedef __true_type _Integral;
280 template<> struct _Is_integer<unsigned long> {
281 typedef __true_type _Integral;
284 #ifdef _GLIBCPP_USE_LONG_LONG
286 template<> struct _Is_integer<long long> {
287 typedef __true_type _Integral;
290 template<> struct _Is_integer<unsigned long long> {
291 typedef __true_type _Integral;
294 #endif /* _GLIBCPP_USE_LONG_LONG */
296 template<typename _Tp> struct _Is_normal_iterator {
297 typedef __false_type _Normal;
300 // Forward declaration hack, should really include this from somewhere.
302 template<typename _Iterator, typename _Container> class __normal_iterator;
305 template<typename _Iterator, typename _Container>
306 struct _Is_normal_iterator< std::__normal_iterator<_Iterator, _Container> > {
307 typedef __true_type _Normal;
310 #endif /* _CPP_BITS_TYPE_TRAITS_H */