1 // Type traits implementation -*- C++ -*- 2 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. 10 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. 15 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. 20 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. 29 30 /* 31 * 32 * Copyright (c) 1997 33 * Silicon Graphics Computer Systems, Inc. 34 * 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. 42 */ 43 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. 47 */ 48 49 #ifndef _CPP_BITS_TYPE_TRAITS_H 50 #define _CPP_BITS_TYPE_TRAITS_H 1 51 52 #pragma GCC system_header 53 54 #include <bits/c++config.h> 55 56 /* 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. 62 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. 73 74 EXAMPLE: 75 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); 82 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()); 87 } 88 */ 89 90 struct __true_type {}; 91 struct __false_type {}; 92 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. */ 101 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. */ 111 112 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; 118 }; 119 120 121 // Provide some specializations. 122 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; 129 }; 130 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; 137 }; 138 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; 145 }; 146 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; 153 }; 154 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; 161 }; 162 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; 169 }; 170 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; 177 }; 178 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; 185 }; 186 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; 193 }; 194 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; 201 }; 202 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; 209 }; 210 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; 217 }; 218 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; 225 }; 226 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; 233 }; 234 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; 241 }; 242 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; 249 }; 250 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; 258 }; 259 260 261 // The following could be written in terms of numeric_limits. 262 // We're doing it separately to reduce the number of dependencies. 263 264 template <class _Tp> struct _Is_integer { 265 typedef __false_type _Integral; 266 }; 267 268 template<> struct _Is_integer<bool> { 269 typedef __true_type _Integral; 270 }; 271 272 template<> struct _Is_integer<char> { 273 typedef __true_type _Integral; 274 }; 275 276 template<> struct _Is_integer<signed char> { 277 typedef __true_type _Integral; 278 }; 279 280 template<> struct _Is_integer<unsigned char> { 281 typedef __true_type _Integral; 282 }; 283 284 template<> struct _Is_integer<wchar_t> { 285 typedef __true_type _Integral; 286 }; 287 288 template<> struct _Is_integer<short> { 289 typedef __true_type _Integral; 290 }; 291 292 template<> struct _Is_integer<unsigned short> { 293 typedef __true_type _Integral; 294 }; 295 296 template<> struct _Is_integer<int> { 297 typedef __true_type _Integral; 298 }; 299 300 template<> struct _Is_integer<unsigned int> { 301 typedef __true_type _Integral; 302 }; 303 304 template<> struct _Is_integer<long> { 305 typedef __true_type _Integral; 306 }; 307 308 template<> struct _Is_integer<unsigned long> { 309 typedef __true_type _Integral; 310 }; 311 312 template<> struct _Is_integer<long long> { 313 typedef __true_type _Integral; 314 }; 315 316 template<> struct _Is_integer<unsigned long long> { 317 typedef __true_type _Integral; 318 }; 319 320 template<typename _Tp> struct _Is_normal_iterator { 321 typedef __false_type _Normal; 322 }; 323 324 // Forward declaration hack, should really include this from somewhere. 325 namespace __gnu_cxx 326 { 327 template<typename _Iterator, typename _Container> class __normal_iterator; 328 } 329 330 template<typename _Iterator, typename _Container> 331 struct _Is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator, _Container> > { 332 typedef __true_type _Normal; 333 }; 334 335 #endif /* _CPP_BITS_TYPE_TRAITS_H */ 336 337 // Local Variables: 338 // mode:C++ 339 // End: 340