libstdc++
type_traits.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011
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 terms
00008 // of the GNU General Public License as published by the Free Software
00009 // Foundation; either version 3, or (at your option) any later
00010 // version.
00011 
00012 // This library is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file ext/type_traits.h
00027  *  This file is a GNU extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _EXT_TYPE_TRAITS
00031 #define _EXT_TYPE_TRAITS 1
00032 
00033 #pragma GCC system_header
00034 
00035 #include <bits/c++config.h>
00036 #include <bits/cpp_type_traits.h>
00037 
00038 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00039 {
00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00041 
00042   // Define a nested type if some predicate holds.
00043   template<bool, typename>
00044     struct __enable_if 
00045     { };
00046 
00047   template<typename _Tp>
00048     struct __enable_if<true, _Tp>
00049     { typedef _Tp __type; };
00050 
00051 
00052   // Conditional expression for types. If true, first, if false, second.
00053   template<bool _Cond, typename _Iftrue, typename _Iffalse>
00054     struct __conditional_type
00055     { typedef _Iftrue __type; };
00056 
00057   template<typename _Iftrue, typename _Iffalse>
00058     struct __conditional_type<false, _Iftrue, _Iffalse>
00059     { typedef _Iffalse __type; };
00060 
00061 
00062   // Given an integral builtin type, return the corresponding unsigned type.
00063   template<typename _Tp>
00064     struct __add_unsigned
00065     { 
00066     private:
00067       typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
00068       
00069     public:
00070       typedef typename __if_type::__type __type; 
00071     };
00072 
00073   template<>
00074     struct __add_unsigned<char>
00075     { typedef unsigned char __type; };
00076 
00077   template<>
00078     struct __add_unsigned<signed char>
00079     { typedef unsigned char __type; };
00080 
00081   template<>
00082     struct __add_unsigned<short>
00083     { typedef unsigned short __type; };
00084 
00085   template<>
00086     struct __add_unsigned<int>
00087     { typedef unsigned int __type; };
00088 
00089   template<>
00090     struct __add_unsigned<long>
00091     { typedef unsigned long __type; };
00092 
00093   template<>
00094     struct __add_unsigned<long long>
00095     { typedef unsigned long long __type; };
00096 
00097   // Declare but don't define.
00098   template<>
00099     struct __add_unsigned<bool>;
00100 
00101   template<>
00102     struct __add_unsigned<wchar_t>;
00103 
00104 
00105   // Given an integral builtin type, return the corresponding signed type.
00106   template<typename _Tp>
00107     struct __remove_unsigned
00108     { 
00109     private:
00110       typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
00111       
00112     public:
00113       typedef typename __if_type::__type __type; 
00114     };
00115 
00116   template<>
00117     struct __remove_unsigned<char>
00118     { typedef signed char __type; };
00119 
00120   template<>
00121     struct __remove_unsigned<unsigned char>
00122     { typedef signed char __type; };
00123 
00124   template<>
00125     struct __remove_unsigned<unsigned short>
00126     { typedef short __type; };
00127 
00128   template<>
00129     struct __remove_unsigned<unsigned int>
00130     { typedef int __type; };
00131 
00132   template<>
00133     struct __remove_unsigned<unsigned long>
00134     { typedef long __type; };
00135 
00136   template<>
00137     struct __remove_unsigned<unsigned long long>
00138     { typedef long long __type; };
00139 
00140   // Declare but don't define.
00141   template<>
00142     struct __remove_unsigned<bool>;
00143 
00144   template<>
00145     struct __remove_unsigned<wchar_t>;
00146 
00147 
00148   // For use in string and vstring.
00149   template<typename _Type>
00150     inline bool
00151     __is_null_pointer(_Type* __ptr)
00152     { return __ptr == 0; }
00153 
00154   template<typename _Type>
00155     inline bool
00156     __is_null_pointer(_Type)
00157     { return false; }
00158 
00159 
00160   // For complex and cmath
00161   template<typename _Tp, bool = std::__is_integer<_Tp>::__value>
00162     struct __promote
00163     { typedef double __type; };
00164 
00165   // No nested __type member for non-integer non-floating point types,
00166   // allows this type to be used for SFINAE to constrain overloads in
00167   // <cmath> and <complex> to only the intended types.
00168   template<typename _Tp>
00169     struct __promote<_Tp, false>
00170     { };
00171 
00172   template<>
00173     struct __promote<long double>
00174     { typedef long double __type; };
00175 
00176   template<>
00177     struct __promote<double>
00178     { typedef double __type; };
00179 
00180   template<>
00181     struct __promote<float>
00182     { typedef float __type; };
00183 
00184   template<typename _Tp, typename _Up,
00185            typename _Tp2 = typename __promote<_Tp>::__type,
00186            typename _Up2 = typename __promote<_Up>::__type>
00187     struct __promote_2
00188     {
00189       typedef __typeof__(_Tp2() + _Up2()) __type;
00190     };
00191 
00192   template<typename _Tp, typename _Up, typename _Vp,
00193            typename _Tp2 = typename __promote<_Tp>::__type,
00194            typename _Up2 = typename __promote<_Up>::__type,
00195            typename _Vp2 = typename __promote<_Vp>::__type>
00196     struct __promote_3
00197     {
00198       typedef __typeof__(_Tp2() + _Up2() + _Vp2()) __type;
00199     };
00200 
00201   template<typename _Tp, typename _Up, typename _Vp, typename _Wp,
00202            typename _Tp2 = typename __promote<_Tp>::__type,
00203            typename _Up2 = typename __promote<_Up>::__type,
00204            typename _Vp2 = typename __promote<_Vp>::__type,
00205            typename _Wp2 = typename __promote<_Wp>::__type>
00206     struct __promote_4
00207     {
00208       typedef __typeof__(_Tp2() + _Up2() + _Vp2() + _Wp2()) __type;
00209     };
00210 
00211 _GLIBCXX_END_NAMESPACE_VERSION
00212 } // namespace
00213 
00214 #endif