# type_traits.h

Go to the documentation of this file.

    00001 // Type traits implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2004 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 _TYPE_TRAITS_H
    00050 #define _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   {
    00096     typedef __true_type     this_dummy_member_must_be_first;
    00097     /* Do not remove this member. It informs a compiler which
    00098        automatically specializes __type_traits that this
    00099        __type_traits template is special. It just makes sure that
    00100        things work if an implementation is using a template
    00101        called __type_traits for something unrelated. */
    00102
    00103    /* The following restrictions should be observed for the sake of
    00104       compilers which automatically produce type specific specializations
    00105       of this class:
    00106           - You may reorder the members below if you wish
    00107           - You may remove any of the members below if you wish
    00108           - You must not rename members without making the corresponding
    00109             name change in the compiler
    00110           - Members you add will be treated like regular members unless
    00111             you add the appropriate support in the compiler. */
    00112
    00113
    00114     typedef __false_type    has_trivial_default_constructor;
    00115     typedef __false_type    has_trivial_copy_constructor;
    00116     typedef __false_type    has_trivial_assignment_operator;
    00117     typedef __false_type    has_trivial_destructor;
    00118     typedef __false_type    is_POD_type;
    00119   };
    00120
    00121
    00122 // Provide some specializations.
    00123
    00124 template<>
    00125   struct __type_traits<bool>
    00126   {
    00127     typedef __true_type    has_trivial_default_constructor;
    00128     typedef __true_type    has_trivial_copy_constructor;
    00129     typedef __true_type    has_trivial_assignment_operator;
    00130     typedef __true_type    has_trivial_destructor;
    00131     typedef __true_type    is_POD_type;
    00132   };
    00133
    00134 template<>
    00135   struct __type_traits<char>
    00136   {
    00137     typedef __true_type    has_trivial_default_constructor;
    00138     typedef __true_type    has_trivial_copy_constructor;
    00139     typedef __true_type    has_trivial_assignment_operator;
    00140     typedef __true_type    has_trivial_destructor;
    00141     typedef __true_type    is_POD_type;
    00142   };
    00143
    00144 template<>
    00145   struct __type_traits<signed char>
    00146   {
    00147     typedef __true_type    has_trivial_default_constructor;
    00148     typedef __true_type    has_trivial_copy_constructor;
    00149     typedef __true_type    has_trivial_assignment_operator;
    00150     typedef __true_type    has_trivial_destructor;
    00151     typedef __true_type    is_POD_type;
    00152   };
    00153
    00154 template<>
    00155   struct __type_traits<unsigned char>
    00156   {
    00157     typedef __true_type    has_trivial_default_constructor;
    00158     typedef __true_type    has_trivial_copy_constructor;
    00159     typedef __true_type    has_trivial_assignment_operator;
    00160     typedef __true_type    has_trivial_destructor;
    00161     typedef __true_type    is_POD_type;
    00162   };
    00163
    00164 template<>
    00165   struct __type_traits<wchar_t>
    00166   {
    00167     typedef __true_type    has_trivial_default_constructor;
    00168     typedef __true_type    has_trivial_copy_constructor;
    00169     typedef __true_type    has_trivial_assignment_operator;
    00170     typedef __true_type    has_trivial_destructor;
    00171     typedef __true_type    is_POD_type;
    00172   };
    00173
    00174 template<>
    00175   struct __type_traits<short>
    00176   {
    00177     typedef __true_type    has_trivial_default_constructor;
    00178     typedef __true_type    has_trivial_copy_constructor;
    00179     typedef __true_type    has_trivial_assignment_operator;
    00180     typedef __true_type    has_trivial_destructor;
    00181     typedef __true_type    is_POD_type;
    00182   };
    00183
    00184 template<>
    00185   struct __type_traits<unsigned short>
    00186   {
    00187     typedef __true_type    has_trivial_default_constructor;
    00188     typedef __true_type    has_trivial_copy_constructor;
    00189     typedef __true_type    has_trivial_assignment_operator;
    00190     typedef __true_type    has_trivial_destructor;
    00191     typedef __true_type    is_POD_type;
    00192   };
    00193
    00194 template<>
    00195   struct __type_traits<int>
    00196   {
    00197     typedef __true_type    has_trivial_default_constructor;
    00198     typedef __true_type    has_trivial_copy_constructor;
    00199     typedef __true_type    has_trivial_assignment_operator;
    00200     typedef __true_type    has_trivial_destructor;
    00201     typedef __true_type    is_POD_type;
    00202   };
    00203
    00204 template<>
    00205   struct __type_traits<unsigned int>
    00206   {
    00207     typedef __true_type    has_trivial_default_constructor;
    00208     typedef __true_type    has_trivial_copy_constructor;
    00209     typedef __true_type    has_trivial_assignment_operator;
    00210     typedef __true_type    has_trivial_destructor;
    00211     typedef __true_type    is_POD_type;
    00212   };
    00213
    00214 template<>
    00215   struct __type_traits<long>
    00216   {
    00217     typedef __true_type    has_trivial_default_constructor;
    00218     typedef __true_type    has_trivial_copy_constructor;
    00219     typedef __true_type    has_trivial_assignment_operator;
    00220     typedef __true_type    has_trivial_destructor;
    00221     typedef __true_type    is_POD_type;
    00222   };
    00223
    00224 template<>
    00225   struct __type_traits<unsigned long>
    00226   {
    00227     typedef __true_type    has_trivial_default_constructor;
    00228     typedef __true_type    has_trivial_copy_constructor;
    00229     typedef __true_type    has_trivial_assignment_operator;
    00230     typedef __true_type    has_trivial_destructor;
    00231     typedef __true_type    is_POD_type;
    00232   };
    00233
    00234 template<>
    00235   struct __type_traits<long long>
    00236   {
    00237     typedef __true_type    has_trivial_default_constructor;
    00238     typedef __true_type    has_trivial_copy_constructor;
    00239     typedef __true_type    has_trivial_assignment_operator;
    00240     typedef __true_type    has_trivial_destructor;
    00241     typedef __true_type    is_POD_type;
    00242   };
    00243
    00244 template<>
    00245   struct __type_traits<unsigned long long>
    00246   {
    00247     typedef __true_type    has_trivial_default_constructor;
    00248     typedef __true_type    has_trivial_copy_constructor;
    00249     typedef __true_type    has_trivial_assignment_operator;
    00250     typedef __true_type    has_trivial_destructor;
    00251     typedef __true_type    is_POD_type;
    00252   };
    00253
    00254 template<>
    00255   struct __type_traits<float>
    00256   {
    00257     typedef __true_type    has_trivial_default_constructor;
    00258     typedef __true_type    has_trivial_copy_constructor;
    00259     typedef __true_type    has_trivial_assignment_operator;
    00260     typedef __true_type    has_trivial_destructor;
    00261     typedef __true_type    is_POD_type;
    00262   };
    00263
    00264 template<>
    00265   struct __type_traits<double>
    00266   {
    00267     typedef __true_type    has_trivial_default_constructor;
    00268     typedef __true_type    has_trivial_copy_constructor;
    00269     typedef __true_type    has_trivial_assignment_operator;
    00270     typedef __true_type    has_trivial_destructor;
    00271     typedef __true_type    is_POD_type;
    00272   };
    00273
    00274 template<>
    00275   struct __type_traits<long double>
    00276   {
    00277     typedef __true_type    has_trivial_default_constructor;
    00278     typedef __true_type    has_trivial_copy_constructor;
    00279     typedef __true_type    has_trivial_assignment_operator;
    00280     typedef __true_type    has_trivial_destructor;
    00281     typedef __true_type    is_POD_type;
    00282   };
    00283
    00284 template <class _Tp>
    00285   struct __type_traits<_Tp*>
    00286   {
    00287     typedef __true_type    has_trivial_default_constructor;
    00288     typedef __true_type    has_trivial_copy_constructor;
    00289     typedef __true_type    has_trivial_assignment_operator;
    00290     typedef __true_type    has_trivial_destructor;
    00291     typedef __true_type    is_POD_type;
    00292   };
    00293
    00294 // The following could be written in terms of numeric_limits.
    00295 // We're doing it separately to reduce the number of dependencies.
    00296
    00297 template <class _Tp>
    00298   struct _Is_integer
    00299   {
    00300     typedef __false_type _Integral;
    00301   };
    00302
    00303 template<>
    00304   struct _Is_integer<bool>
    00305   {
    00306     typedef __true_type _Integral;
    00307   };
    00308
    00309 template<>
    00310   struct _Is_integer<char>
    00311   {
    00312     typedef __true_type _Integral;
    00313   };
    00314
    00315 template<>
    00316   struct _Is_integer<signed char>
    00317   {
    00318     typedef __true_type _Integral;
    00319   };
    00320
    00321 template<>
    00322   struct _Is_integer<unsigned char>
    00323   {
    00324     typedef __true_type _Integral;
    00325   };
    00326
    00327 template<>
    00328   struct _Is_integer<wchar_t>
    00329   {
    00330     typedef __true_type _Integral;
    00331   };
    00332
    00333 template<>
    00334   struct _Is_integer<short>
    00335   {
    00336     typedef __true_type _Integral;
    00337   };
    00338
    00339 template<>
    00340   struct _Is_integer<unsigned short>
    00341   {
    00342     typedef __true_type _Integral;
    00343   };
    00344
    00345 template<>
    00346   struct _Is_integer<int>
    00347   {
    00348     typedef __true_type _Integral;
    00349   };
    00350
    00351 template<>
    00352   struct _Is_integer<unsigned int>
    00353   {
    00354     typedef __true_type _Integral;
    00355   };
    00356
    00357 template<>
    00358   struct _Is_integer<long>
    00359   {
    00360     typedef __true_type _Integral;
    00361   };
    00362
    00363 template<>
    00364   struct _Is_integer<unsigned long>
    00365   {
    00366     typedef __true_type _Integral;
    00367   };
    00368
    00369 template<>
    00370   struct _Is_integer<long long>
    00371   {
    00372     typedef __true_type _Integral;
    00373   };
    00374
    00375 template<>
    00376   struct _Is_integer<unsigned long long>
    00377   {
    00378     typedef __true_type _Integral;
    00379   };
    00380
    00381 template<typename _Tp>
    00382   struct _Is_normal_iterator
    00383   {
    00384     typedef __false_type _Normal;
    00385   };
    00386
    00387 // Forward declaration hack, should really include this from somewhere.
    00388 namespace __gnu_cxx
    00389 {
    00390   template<typename _Iterator, typename _Container>
    00391     class __normal_iterator;
    00392 }
    00393
    00394 template<typename _Iterator, typename _Container>
    00395   struct _Is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator,
    00396                                _Container> >
    00397   {
    00398     typedef __true_type _Normal;
    00399   };
    00400
    00401 #endif /* _TYPE_TRAITS_H */
    00402
    00403 // Local Variables:
    00404 // mode:C++
    00405 // End: