type_traits.h

Go to the documentation of this file.
00001 // Type traits implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 2, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 /* 00031 * 00032 * Copyright (c) 1997 00033 * Silicon Graphics Computer Systems, Inc. 00034 * 00035 * Permission to use, copy, modify, distribute and sell this software 00036 * and its documentation for any purpose is hereby granted without fee, 00037 * provided that the above copyright notice appear in all copies and 00038 * that both that copyright notice and this permission notice appear 00039 * in supporting documentation. Silicon Graphics makes no 00040 * representations about the suitability of this software for any 00041 * purpose. It is provided "as is" without express or implied warranty. 00042 */ 00043 00044 /** @file type_traits.h 00045 * This is an internal header file, included by other library headers. 00046 * You should not attempt to use it directly. 00047 */ 00048 00049 #ifndef _CPP_BITS_TYPE_TRAITS_H 00050 #define _CPP_BITS_TYPE_TRAITS_H 1 00051 00052 #pragma GCC system_header 00053 00054 #include <bits/c++config.h> 00055 00056 /* 00057 This header file provides a framework for allowing compile time dispatch 00058 based on type attributes. This is useful when writing template code. 00059 For example, when making a copy of an array of an unknown type, it helps 00060 to know if the type has a trivial copy constructor or not, to help decide 00061 if a memcpy can be used. 00062 00063 The class template __type_traits provides a series of typedefs each of 00064 which is either __true_type or __false_type. The argument to 00065 __type_traits can be any type. The typedefs within this template will 00066 attain their correct values by one of these means: 00067 1. The general instantiation contain conservative values which work 00068 for all types. 00069 2. Specializations may be declared to make distinctions between types. 00070 3. Some compilers (such as the Silicon Graphics N32 and N64 compilers) 00071 will automatically provide the appropriate specializations for all 00072 types. 00073 00074 EXAMPLE: 00075 00076 //Copy an array of elements which have non-trivial copy constructors 00077 template <class _Tp> void 00078 copy(_Tp* __source,_Tp* __destination,int __n,__false_type); 00079 //Copy an array of elements which have trivial copy constructors. Use memcpy. 00080 template <class _Tp> void 00081 copy(_Tp* __source,_Tp* __destination,int __n,__true_type); 00082 00083 //Copy an array of any type by using the most efficient copy mechanism 00084 template <class _Tp> inline void copy(_Tp* __source,_Tp* __destination,int __n) { 00085 copy(__source,__destination,__n, 00086 typename __type_traits<_Tp>::has_trivial_copy_constructor()); 00087 } 00088 */ 00089 00090 struct __true_type {}; 00091 struct __false_type {}; 00092 00093 template <class _Tp> 00094 struct __type_traits { 00095 typedef __true_type this_dummy_member_must_be_first; 00096 /* Do not remove this member. It informs a compiler which 00097 automatically specializes __type_traits that this 00098 __type_traits template is special. It just makes sure that 00099 things work if an implementation is using a template 00100 called __type_traits for something unrelated. */ 00101 00102 /* The following restrictions should be observed for the sake of 00103 compilers which automatically produce type specific specializations 00104 of this class: 00105 - You may reorder the members below if you wish 00106 - You may remove any of the members below if you wish 00107 - You must not rename members without making the corresponding 00108 name change in the compiler 00109 - Members you add will be treated like regular members unless 00110 you add the appropriate support in the compiler. */ 00111 00112 00113 typedef __false_type has_trivial_default_constructor; 00114 typedef __false_type has_trivial_copy_constructor; 00115 typedef __false_type has_trivial_assignment_operator; 00116 typedef __false_type has_trivial_destructor; 00117 typedef __false_type is_POD_type; 00118 }; 00119 00120 00121 // Provide some specializations. 00122 00123 template<> struct __type_traits<bool> { 00124 typedef __true_type has_trivial_default_constructor; 00125 typedef __true_type has_trivial_copy_constructor; 00126 typedef __true_type has_trivial_assignment_operator; 00127 typedef __true_type has_trivial_destructor; 00128 typedef __true_type is_POD_type; 00129 }; 00130 00131 template<> struct __type_traits<char> { 00132 typedef __true_type has_trivial_default_constructor; 00133 typedef __true_type has_trivial_copy_constructor; 00134 typedef __true_type has_trivial_assignment_operator; 00135 typedef __true_type has_trivial_destructor; 00136 typedef __true_type is_POD_type; 00137 }; 00138 00139 template<> struct __type_traits<signed char> { 00140 typedef __true_type has_trivial_default_constructor; 00141 typedef __true_type has_trivial_copy_constructor; 00142 typedef __true_type has_trivial_assignment_operator; 00143 typedef __true_type has_trivial_destructor; 00144 typedef __true_type is_POD_type; 00145 }; 00146 00147 template<> struct __type_traits<unsigned char> { 00148 typedef __true_type has_trivial_default_constructor; 00149 typedef __true_type has_trivial_copy_constructor; 00150 typedef __true_type has_trivial_assignment_operator; 00151 typedef __true_type has_trivial_destructor; 00152 typedef __true_type is_POD_type; 00153 }; 00154 00155 template<> struct __type_traits<wchar_t> { 00156 typedef __true_type has_trivial_default_constructor; 00157 typedef __true_type has_trivial_copy_constructor; 00158 typedef __true_type has_trivial_assignment_operator; 00159 typedef __true_type has_trivial_destructor; 00160 typedef __true_type is_POD_type; 00161 }; 00162 00163 template<> struct __type_traits<short> { 00164 typedef __true_type has_trivial_default_constructor; 00165 typedef __true_type has_trivial_copy_constructor; 00166 typedef __true_type has_trivial_assignment_operator; 00167 typedef __true_type has_trivial_destructor; 00168 typedef __true_type is_POD_type; 00169 }; 00170 00171 template<> struct __type_traits<unsigned short> { 00172 typedef __true_type has_trivial_default_constructor; 00173 typedef __true_type has_trivial_copy_constructor; 00174 typedef __true_type has_trivial_assignment_operator; 00175 typedef __true_type has_trivial_destructor; 00176 typedef __true_type is_POD_type; 00177 }; 00178 00179 template<> struct __type_traits<int> { 00180 typedef __true_type has_trivial_default_constructor; 00181 typedef __true_type has_trivial_copy_constructor; 00182 typedef __true_type has_trivial_assignment_operator; 00183 typedef __true_type has_trivial_destructor; 00184 typedef __true_type is_POD_type; 00185 }; 00186 00187 template<> struct __type_traits<unsigned int> { 00188 typedef __true_type has_trivial_default_constructor; 00189 typedef __true_type has_trivial_copy_constructor; 00190 typedef __true_type has_trivial_assignment_operator; 00191 typedef __true_type has_trivial_destructor; 00192 typedef __true_type is_POD_type; 00193 }; 00194 00195 template<> struct __type_traits<long> { 00196 typedef __true_type has_trivial_default_constructor; 00197 typedef __true_type has_trivial_copy_constructor; 00198 typedef __true_type has_trivial_assignment_operator; 00199 typedef __true_type has_trivial_destructor; 00200 typedef __true_type is_POD_type; 00201 }; 00202 00203 template<> struct __type_traits<unsigned long> { 00204 typedef __true_type has_trivial_default_constructor; 00205 typedef __true_type has_trivial_copy_constructor; 00206 typedef __true_type has_trivial_assignment_operator; 00207 typedef __true_type has_trivial_destructor; 00208 typedef __true_type is_POD_type; 00209 }; 00210 00211 template<> struct __type_traits<long long> { 00212 typedef __true_type has_trivial_default_constructor; 00213 typedef __true_type has_trivial_copy_constructor; 00214 typedef __true_type has_trivial_assignment_operator; 00215 typedef __true_type has_trivial_destructor; 00216 typedef __true_type is_POD_type; 00217 }; 00218 00219 template<> struct __type_traits<unsigned long long> { 00220 typedef __true_type has_trivial_default_constructor; 00221 typedef __true_type has_trivial_copy_constructor; 00222 typedef __true_type has_trivial_assignment_operator; 00223 typedef __true_type has_trivial_destructor; 00224 typedef __true_type is_POD_type; 00225 }; 00226 00227 template<> struct __type_traits<float> { 00228 typedef __true_type has_trivial_default_constructor; 00229 typedef __true_type has_trivial_copy_constructor; 00230 typedef __true_type has_trivial_assignment_operator; 00231 typedef __true_type has_trivial_destructor; 00232 typedef __true_type is_POD_type; 00233 }; 00234 00235 template<> struct __type_traits<double> { 00236 typedef __true_type has_trivial_default_constructor; 00237 typedef __true_type has_trivial_copy_constructor; 00238 typedef __true_type has_trivial_assignment_operator; 00239 typedef __true_type has_trivial_destructor; 00240 typedef __true_type is_POD_type; 00241 }; 00242 00243 template<> struct __type_traits<long double> { 00244 typedef __true_type has_trivial_default_constructor; 00245 typedef __true_type has_trivial_copy_constructor; 00246 typedef __true_type has_trivial_assignment_operator; 00247 typedef __true_type has_trivial_destructor; 00248 typedef __true_type is_POD_type; 00249 }; 00250 00251 template <class _Tp> 00252 struct __type_traits<_Tp*> { 00253 typedef __true_type has_trivial_default_constructor; 00254 typedef __true_type has_trivial_copy_constructor; 00255 typedef __true_type has_trivial_assignment_operator; 00256 typedef __true_type has_trivial_destructor; 00257 typedef __true_type is_POD_type; 00258 }; 00259 00260 00261 // The following could be written in terms of numeric_limits. 00262 // We're doing it separately to reduce the number of dependencies. 00263 00264 template <class _Tp> struct _Is_integer { 00265 typedef __false_type _Integral; 00266 }; 00267 00268 template<> struct _Is_integer<bool> { 00269 typedef __true_type _Integral; 00270 }; 00271 00272 template<> struct _Is_integer<char> { 00273 typedef __true_type _Integral; 00274 }; 00275 00276 template<> struct _Is_integer<signed char> { 00277 typedef __true_type _Integral; 00278 }; 00279 00280 template<> struct _Is_integer<unsigned char> { 00281 typedef __true_type _Integral; 00282 }; 00283 00284 template<> struct _Is_integer<wchar_t> { 00285 typedef __true_type _Integral; 00286 }; 00287 00288 template<> struct _Is_integer<short> { 00289 typedef __true_type _Integral; 00290 }; 00291 00292 template<> struct _Is_integer<unsigned short> { 00293 typedef __true_type _Integral; 00294 }; 00295 00296 template<> struct _Is_integer<int> { 00297 typedef __true_type _Integral; 00298 }; 00299 00300 template<> struct _Is_integer<unsigned int> { 00301 typedef __true_type _Integral; 00302 }; 00303 00304 template<> struct _Is_integer<long> { 00305 typedef __true_type _Integral; 00306 }; 00307 00308 template<> struct _Is_integer<unsigned long> { 00309 typedef __true_type _Integral; 00310 }; 00311 00312 template<> struct _Is_integer<long long> { 00313 typedef __true_type _Integral; 00314 }; 00315 00316 template<> struct _Is_integer<unsigned long long> { 00317 typedef __true_type _Integral; 00318 }; 00319 00320 template<typename _Tp> struct _Is_normal_iterator { 00321 typedef __false_type _Normal; 00322 }; 00323 00324 // Forward declaration hack, should really include this from somewhere. 00325 namespace __gnu_cxx 00326 { 00327 template<typename _Iterator, typename _Container> class __normal_iterator; 00328 } 00329 00330 template<typename _Iterator, typename _Container> 00331 struct _Is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator, _Container> > { 00332 typedef __true_type _Normal; 00333 }; 00334 00335 #endif /* _CPP_BITS_TYPE_TRAITS_H */ 00336 00337 // Local Variables: 00338 // mode:C++ 00339 // End:

Generated on Wed Aug 4 21:43:23 2004 for libstdc++-v3 Source by doxygen 1.3.8