2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / include / bits / type_traits.h
blob80f7e39575e3be54fb20ad7a9d0f3fab742f5c13
1 // Type traits implementation -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Copyright (c) 1997
33 * Silicon Graphics Computer Systems, Inc.
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Silicon Graphics makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 /** @file type_traits.h
45 * This is an internal header file, included by other library headers.
46 * You should not attempt to use it directly.
49 #ifndef _TYPE_TRAITS_H
50 #define _TYPE_TRAITS_H 1
52 #pragma GCC system_header
54 #include <bits/c++config.h>
57 This header file provides a framework for allowing compile time dispatch
58 based on type attributes. This is useful when writing template code.
59 For example, when making a copy of an array of an unknown type, it helps
60 to know if the type has a trivial copy constructor or not, to help decide
61 if a memcpy can be used.
63 The class template __type_traits provides a series of typedefs each of
64 which is either __true_type or __false_type. The argument to
65 __type_traits can be any type. The typedefs within this template will
66 attain their correct values by one of these means:
67 1. The general instantiation contain conservative values which work
68 for all types.
69 2. Specializations may be declared to make distinctions between types.
70 3. Some compilers (such as the Silicon Graphics N32 and N64 compilers)
71 will automatically provide the appropriate specializations for all
72 types.
74 EXAMPLE:
76 //Copy an array of elements which have non-trivial copy constructors
77 template <class _Tp> void
78 copy(_Tp* __source,_Tp* __destination,int __n,__false_type);
79 //Copy an array of elements which have trivial copy constructors. Use memcpy.
80 template <class _Tp> void
81 copy(_Tp* __source,_Tp* __destination,int __n,__true_type);
83 //Copy an array of any type by using the most efficient copy mechanism
84 template <class _Tp> inline void copy(_Tp* __source,_Tp* __destination,int __n) {
85 copy(__source,__destination,__n,
86 typename __type_traits<_Tp>::has_trivial_copy_constructor());
90 struct __true_type {};
91 struct __false_type {};
93 template <class _Tp>
94 struct __type_traits {
95 typedef __true_type this_dummy_member_must_be_first;
96 /* Do not remove this member. It informs a compiler which
97 automatically specializes __type_traits that this
98 __type_traits template is special. It just makes sure that
99 things work if an implementation is using a template
100 called __type_traits for something unrelated. */
102 /* The following restrictions should be observed for the sake of
103 compilers which automatically produce type specific specializations
104 of this class:
105 - You may reorder the members below if you wish
106 - You may remove any of the members below if you wish
107 - You must not rename members without making the corresponding
108 name change in the compiler
109 - Members you add will be treated like regular members unless
110 you add the appropriate support in the compiler. */
113 typedef __false_type has_trivial_default_constructor;
114 typedef __false_type has_trivial_copy_constructor;
115 typedef __false_type has_trivial_assignment_operator;
116 typedef __false_type has_trivial_destructor;
117 typedef __false_type is_POD_type;
121 // Provide some specializations.
123 template<> struct __type_traits<bool> {
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<char> {
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<signed char> {
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<unsigned char> {
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<wchar_t> {
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<short> {
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 short> {
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 template<> struct __type_traits<int> {
180 typedef __true_type has_trivial_default_constructor;
181 typedef __true_type has_trivial_copy_constructor;
182 typedef __true_type has_trivial_assignment_operator;
183 typedef __true_type has_trivial_destructor;
184 typedef __true_type is_POD_type;
187 template<> struct __type_traits<unsigned int> {
188 typedef __true_type has_trivial_default_constructor;
189 typedef __true_type has_trivial_copy_constructor;
190 typedef __true_type has_trivial_assignment_operator;
191 typedef __true_type has_trivial_destructor;
192 typedef __true_type is_POD_type;
195 template<> struct __type_traits<long> {
196 typedef __true_type has_trivial_default_constructor;
197 typedef __true_type has_trivial_copy_constructor;
198 typedef __true_type has_trivial_assignment_operator;
199 typedef __true_type has_trivial_destructor;
200 typedef __true_type is_POD_type;
203 template<> struct __type_traits<unsigned long> {
204 typedef __true_type has_trivial_default_constructor;
205 typedef __true_type has_trivial_copy_constructor;
206 typedef __true_type has_trivial_assignment_operator;
207 typedef __true_type has_trivial_destructor;
208 typedef __true_type is_POD_type;
211 template<> struct __type_traits<long long> {
212 typedef __true_type has_trivial_default_constructor;
213 typedef __true_type has_trivial_copy_constructor;
214 typedef __true_type has_trivial_assignment_operator;
215 typedef __true_type has_trivial_destructor;
216 typedef __true_type is_POD_type;
219 template<> struct __type_traits<unsigned long long> {
220 typedef __true_type has_trivial_default_constructor;
221 typedef __true_type has_trivial_copy_constructor;
222 typedef __true_type has_trivial_assignment_operator;
223 typedef __true_type has_trivial_destructor;
224 typedef __true_type is_POD_type;
227 template<> struct __type_traits<float> {
228 typedef __true_type has_trivial_default_constructor;
229 typedef __true_type has_trivial_copy_constructor;
230 typedef __true_type has_trivial_assignment_operator;
231 typedef __true_type has_trivial_destructor;
232 typedef __true_type is_POD_type;
235 template<> struct __type_traits<double> {
236 typedef __true_type has_trivial_default_constructor;
237 typedef __true_type has_trivial_copy_constructor;
238 typedef __true_type has_trivial_assignment_operator;
239 typedef __true_type has_trivial_destructor;
240 typedef __true_type is_POD_type;
243 template<> struct __type_traits<long double> {
244 typedef __true_type has_trivial_default_constructor;
245 typedef __true_type has_trivial_copy_constructor;
246 typedef __true_type has_trivial_assignment_operator;
247 typedef __true_type has_trivial_destructor;
248 typedef __true_type is_POD_type;
251 template <class _Tp>
252 struct __type_traits<_Tp*> {
253 typedef __true_type has_trivial_default_constructor;
254 typedef __true_type has_trivial_copy_constructor;
255 typedef __true_type has_trivial_assignment_operator;
256 typedef __true_type has_trivial_destructor;
257 typedef __true_type is_POD_type;
261 // The following could be written in terms of numeric_limits.
262 // We're doing it separately to reduce the number of dependencies.
264 template <class _Tp> struct _Is_integer {
265 typedef __false_type _Integral;
268 template<> struct _Is_integer<bool> {
269 typedef __true_type _Integral;
272 template<> struct _Is_integer<char> {
273 typedef __true_type _Integral;
276 template<> struct _Is_integer<signed char> {
277 typedef __true_type _Integral;
280 template<> struct _Is_integer<unsigned char> {
281 typedef __true_type _Integral;
284 template<> struct _Is_integer<wchar_t> {
285 typedef __true_type _Integral;
288 template<> struct _Is_integer<short> {
289 typedef __true_type _Integral;
292 template<> struct _Is_integer<unsigned short> {
293 typedef __true_type _Integral;
296 template<> struct _Is_integer<int> {
297 typedef __true_type _Integral;
300 template<> struct _Is_integer<unsigned int> {
301 typedef __true_type _Integral;
304 template<> struct _Is_integer<long> {
305 typedef __true_type _Integral;
308 template<> struct _Is_integer<unsigned long> {
309 typedef __true_type _Integral;
312 template<> struct _Is_integer<long long> {
313 typedef __true_type _Integral;
316 template<> struct _Is_integer<unsigned long long> {
317 typedef __true_type _Integral;
320 template<typename _Tp> struct _Is_normal_iterator {
321 typedef __false_type _Normal;
324 // Forward declaration hack, should really include this from somewhere.
325 namespace __gnu_cxx
327 template<typename _Iterator, typename _Container> class __normal_iterator;
330 template<typename _Iterator, typename _Container>
331 struct _Is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator, _Container> > {
332 typedef __true_type _Normal;
335 #endif /* _TYPE_TRAITS_H */
337 // Local Variables:
338 // mode:C++
339 // End: