cpp_type_traits.h

Go to the documentation of this file.
00001 // The  -*- C++ -*- type traits classes for internal use in libstdc++
00002 
00003 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file cpp_type_traits.h
00032  *  This is an internal header file, included by other library headers.
00033  *  You should not attempt to use it directly.
00034  */
00035 
00036 // Written by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
00037 
00038 #ifndef _CPP_TYPE_TRAITS_H
00039 #define _CPP_TYPE_TRAITS_H 1
00040 
00041 #pragma GCC system_header
00042 
00043 #include <bits/c++config.h>
00044 
00045 //
00046 // This file provides some compile-time information about various types.
00047 // These representations were designed, on purpose, to be constant-expressions
00048 // and not types as found in <bits/type_traits.h>.  In particular, they
00049 // can be used in control structures and the optimizer hopefully will do
00050 // the obvious thing.
00051 //
00052 // Why integral expressions, and not functions nor types?
00053 // Firstly, these compile-time entities are used as template-arguments
00054 // so function return values won't work:  We need compile-time entities.
00055 // We're left with types and constant  integral expressions.
00056 // Secondly, from the point of view of ease of use, type-based compile-time
00057 // information is -not- *that* convenient.  On has to write lots of
00058 // overloaded functions and to hope that the compiler will select the right
00059 // one. As a net effect, the overall structure isn't very clear at first
00060 // glance.
00061 // Thirdly, partial ordering and overload resolution (of function templates)
00062 // is highly costly in terms of compiler-resource.  It is a Good Thing to
00063 // keep these resource consumption as least as possible.
00064 //
00065 // See valarray_array.h for a case use.
00066 //
00067 // -- Gaby (dosreis@cmla.ens-cachan.fr) 2000-03-06.
00068 //
00069 // Update 2005: types are also provided and <bits/type_traits.h> has been
00070 // removed.
00071 //
00072 
00073 // Forward declaration hack, should really include this from somewhere.
00074 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00075 
00076   template<typename _Iterator, typename _Container>
00077     class __normal_iterator;
00078 
00079 _GLIBCXX_END_NAMESPACE
00080 
00081 _GLIBCXX_BEGIN_NAMESPACE(std)
00082 
00083   struct __true_type { };
00084   struct __false_type { };
00085 
00086   template<bool>
00087     struct __truth_type
00088     { typedef __false_type __type; };
00089 
00090   template<>
00091     struct __truth_type<true>
00092     { typedef __true_type __type; };
00093 
00094   // N.B. The conversions to bool are needed due to the issue
00095   // explained in c++/19404.
00096   template<class _Sp, class _Tp>
00097     struct __traitor
00098     {
00099       enum { __value = bool(_Sp::__value) || bool(_Tp::__value) };
00100       typedef typename __truth_type<__value>::__type __type;
00101     };
00102 
00103   // N.B. The conversions to bool are needed due to the issue
00104   // explained in c++/19404.
00105   template<class _Sp, class _Tp>
00106     struct __traitand
00107     {
00108       enum { __value = bool(_Sp::__value) && bool(_Tp::__value) };
00109       typedef typename __truth_type<__value>::__type __type;
00110     };
00111 
00112   // Compare for equality of types.
00113   template<typename, typename>
00114     struct __are_same
00115     {
00116       enum { __value = 0 };
00117       typedef __false_type __type;
00118     };
00119 
00120   template<typename _Tp>
00121     struct __are_same<_Tp, _Tp>
00122     {
00123       enum { __value = 1 };
00124       typedef __true_type __type;
00125     };
00126 
00127   // Holds if the template-argument is a void type.
00128   template<typename _Tp>
00129     struct __is_void
00130     {
00131       enum { __value = 0 };
00132       typedef __false_type __type;
00133     };
00134 
00135   template<>
00136     struct __is_void<void>
00137     {
00138       enum { __value = 1 };
00139       typedef __true_type __type;
00140     };
00141 
00142   //
00143   // Integer types
00144   //
00145   template<typename _Tp>
00146     struct __is_integer
00147     {
00148       enum { __value = 0 };
00149       typedef __false_type __type;
00150     };
00151 
00152   // Thirteen specializations (yes there are eleven standard integer
00153   // types; 'long long' and 'unsigned long long' are supported as
00154   // extensions)
00155   template<>
00156     struct __is_integer<bool>
00157     {
00158       enum { __value = 1 };
00159       typedef __true_type __type;
00160     };
00161 
00162   template<>
00163     struct __is_integer<char>
00164     {
00165       enum { __value = 1 };
00166       typedef __true_type __type;
00167     };
00168 
00169   template<>
00170     struct __is_integer<signed char>
00171     {
00172       enum { __value = 1 };
00173       typedef __true_type __type;
00174     };
00175 
00176   template<>
00177     struct __is_integer<unsigned char>
00178     {
00179       enum { __value = 1 };
00180       typedef __true_type __type;
00181     };
00182 
00183 # ifdef _GLIBCXX_USE_WCHAR_T
00184   template<>
00185     struct __is_integer<wchar_t>
00186     {
00187       enum { __value = 1 };
00188       typedef __true_type __type;
00189     };
00190 # endif
00191 
00192   template<>
00193     struct __is_integer<short>
00194     {
00195       enum { __value = 1 };
00196       typedef __true_type __type;
00197     };
00198 
00199   template<>
00200     struct __is_integer<unsigned short>
00201     {
00202       enum { __value = 1 };
00203       typedef __true_type __type;
00204     };
00205 
00206   template<>
00207     struct __is_integer<int>
00208     {
00209       enum { __value = 1 };
00210       typedef __true_type __type;
00211     };
00212 
00213   template<>
00214     struct __is_integer<unsigned int>
00215     {
00216       enum { __value = 1 };
00217       typedef __true_type __type;
00218     };
00219 
00220   template<>
00221     struct __is_integer<long>
00222     {
00223       enum { __value = 1 };
00224       typedef __true_type __type;
00225     };
00226 
00227   template<>
00228     struct __is_integer<unsigned long>
00229     {
00230       enum { __value = 1 };
00231       typedef __true_type __type;
00232     };
00233 
00234   template<>
00235     struct __is_integer<long long>
00236     {
00237       enum { __value = 1 };
00238       typedef __true_type __type;
00239     };
00240 
00241   template<>
00242     struct __is_integer<unsigned long long>
00243     {
00244       enum { __value = 1 };
00245       typedef __true_type __type;
00246     };
00247 
00248   //
00249   // Floating point types
00250   //
00251   template<typename _Tp>
00252     struct __is_floating
00253     {
00254       enum { __value = 0 };
00255       typedef __false_type __type;
00256     };
00257 
00258   // three specializations (float, double and 'long double')
00259   template<>
00260     struct __is_floating<float>
00261     {
00262       enum { __value = 1 };
00263       typedef __true_type __type;
00264     };
00265 
00266   template<>
00267     struct __is_floating<double>
00268     {
00269       enum { __value = 1 };
00270       typedef __true_type __type;
00271     };
00272 
00273   template<>
00274     struct __is_floating<long double>
00275     {
00276       enum { __value = 1 };
00277       typedef __true_type __type;
00278     };
00279 
00280   //
00281   // Pointer types
00282   //
00283   template<typename _Tp>
00284     struct __is_pointer
00285     {
00286       enum { __value = 0 };
00287       typedef __false_type __type;
00288     };
00289 
00290   template<typename _Tp>
00291     struct __is_pointer<_Tp*>
00292     {
00293       enum { __value = 1 };
00294       typedef __true_type __type;
00295     };
00296 
00297   //
00298   // Normal iterator type
00299   //
00300   template<typename _Tp>
00301     struct __is_normal_iterator
00302     {
00303       enum { __value = 0 };
00304       typedef __false_type __type;
00305     };
00306 
00307   template<typename _Iterator, typename _Container>
00308     struct __is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator,
00309                                   _Container> >
00310     {
00311       enum { __value = 1 };
00312       typedef __true_type __type;
00313     };
00314 
00315   //
00316   // An arithmetic type is an integer type or a floating point type
00317   //
00318   template<typename _Tp>
00319     struct __is_arithmetic
00320     : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
00321     { };
00322 
00323   //
00324   // A fundamental type is `void' or and arithmetic type
00325   //
00326   template<typename _Tp>
00327     struct __is_fundamental
00328     : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
00329     { };
00330 
00331   //
00332   // A scalar type is an arithmetic type or a pointer type
00333   // 
00334   template<typename _Tp>
00335     struct __is_scalar
00336     : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
00337     { };
00338 
00339   //
00340   // For use in std::copy and std::find overloads for streambuf iterators.
00341   //
00342   template<typename _Tp>
00343     struct __is_char
00344     {
00345       enum { __value = 0 };
00346       typedef __false_type __type;
00347     };
00348 
00349   template<>
00350     struct __is_char<char>
00351     {
00352       enum { __value = 1 };
00353       typedef __true_type __type;
00354     };
00355 
00356 #ifdef _GLIBCXX_USE_WCHAR_T
00357   template<>
00358     struct __is_char<wchar_t>
00359     {
00360       enum { __value = 1 };
00361       typedef __true_type __type;
00362     };
00363 #endif
00364 
00365   template<typename _Tp>
00366     struct __is_byte
00367     {
00368       enum { __value = 0 };
00369       typedef __false_type __type;
00370     };
00371 
00372   template<>
00373     struct __is_byte<char>
00374     {
00375       enum { __value = 1 };
00376       typedef __true_type __type;
00377     };
00378 
00379   template<>
00380     struct __is_byte<signed char>
00381     {
00382       enum { __value = 1 };
00383       typedef __true_type __type;
00384     };
00385 
00386   template<>
00387     struct __is_byte<unsigned char>
00388     {
00389       enum { __value = 1 };
00390       typedef __true_type __type;
00391     };
00392 
00393   //
00394   // Move iterator type
00395   //
00396   template<typename _Tp>
00397     struct __is_move_iterator
00398     {
00399       enum { __value = 0 };
00400       typedef __false_type __type;
00401     };
00402 
00403 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00404   template<typename _Iterator>
00405     class move_iterator;
00406 
00407   template<typename _Iterator>
00408     struct __is_move_iterator< move_iterator<_Iterator> >
00409     {
00410       enum { __value = 1 };
00411       typedef __true_type __type;
00412     };
00413 #endif
00414 
00415 _GLIBCXX_END_NAMESPACE
00416 
00417 #endif //_CPP_TYPE_TRAITS_H

Generated on Sat Dec 12 09:40:09 2009 for libstdc++ by  doxygen 1.5.6