1 // Type traits implementation -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
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)
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,
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.
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 _CPP_BITS_TYPE_TRAITS_H
50 #define _CPP_BITS_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
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
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
{};
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
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 #ifdef _GLIBCPP_USE_LONG_LONG
213 template<> struct __type_traits
<long long> {
214 typedef __true_type has_trivial_default_constructor
;
215 typedef __true_type has_trivial_copy_constructor
;
216 typedef __true_type has_trivial_assignment_operator
;
217 typedef __true_type has_trivial_destructor
;
218 typedef __true_type is_POD_type
;
221 template<> struct __type_traits
<unsigned long long> {
222 typedef __true_type has_trivial_default_constructor
;
223 typedef __true_type has_trivial_copy_constructor
;
224 typedef __true_type has_trivial_assignment_operator
;
225 typedef __true_type has_trivial_destructor
;
226 typedef __true_type is_POD_type
;
229 #endif /* _GLIBCPP_USE_LONG_LONG */
231 template<> struct __type_traits
<float> {
232 typedef __true_type has_trivial_default_constructor
;
233 typedef __true_type has_trivial_copy_constructor
;
234 typedef __true_type has_trivial_assignment_operator
;
235 typedef __true_type has_trivial_destructor
;
236 typedef __true_type is_POD_type
;
239 template<> struct __type_traits
<double> {
240 typedef __true_type has_trivial_default_constructor
;
241 typedef __true_type has_trivial_copy_constructor
;
242 typedef __true_type has_trivial_assignment_operator
;
243 typedef __true_type has_trivial_destructor
;
244 typedef __true_type is_POD_type
;
247 template<> struct __type_traits
<long double> {
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
;
256 struct __type_traits
<_Tp
*> {
257 typedef __true_type has_trivial_default_constructor
;
258 typedef __true_type has_trivial_copy_constructor
;
259 typedef __true_type has_trivial_assignment_operator
;
260 typedef __true_type has_trivial_destructor
;
261 typedef __true_type is_POD_type
;
265 // The following could be written in terms of numeric_limits.
266 // We're doing it separately to reduce the number of dependencies.
268 template <class _Tp
> struct _Is_integer
{
269 typedef __false_type _Integral
;
272 template<> struct _Is_integer
<bool> {
273 typedef __true_type _Integral
;
276 template<> struct _Is_integer
<char> {
277 typedef __true_type _Integral
;
280 template<> struct _Is_integer
<signed char> {
281 typedef __true_type _Integral
;
284 template<> struct _Is_integer
<unsigned char> {
285 typedef __true_type _Integral
;
288 template<> struct _Is_integer
<wchar_t> {
289 typedef __true_type _Integral
;
292 template<> struct _Is_integer
<short> {
293 typedef __true_type _Integral
;
296 template<> struct _Is_integer
<unsigned short> {
297 typedef __true_type _Integral
;
300 template<> struct _Is_integer
<int> {
301 typedef __true_type _Integral
;
304 template<> struct _Is_integer
<unsigned int> {
305 typedef __true_type _Integral
;
308 template<> struct _Is_integer
<long> {
309 typedef __true_type _Integral
;
312 template<> struct _Is_integer
<unsigned long> {
313 typedef __true_type _Integral
;
316 #ifdef _GLIBCPP_USE_LONG_LONG
318 template<> struct _Is_integer
<long long> {
319 typedef __true_type _Integral
;
322 template<> struct _Is_integer
<unsigned long long> {
323 typedef __true_type _Integral
;
326 #endif /* _GLIBCPP_USE_LONG_LONG */
328 template<typename _Tp
> struct _Is_normal_iterator
{
329 typedef __false_type _Normal
;
332 // Forward declaration hack, should really include this from somewhere.
335 template<typename _Iterator
, typename _Container
> class __normal_iterator
;
338 template<typename _Iterator
, typename _Container
>
339 struct _Is_normal_iterator
< std::__normal_iterator
<_Iterator
, _Container
> > {
340 typedef __true_type _Normal
;
343 #endif /* _CPP_BITS_TYPE_TRAITS_H */