3 // Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file ext/type_traits.h
27 * This file is a GNU extension to the Standard C++ Library.
30 #ifndef _EXT_TYPE_TRAITS
31 #define _EXT_TYPE_TRAITS 1
33 #pragma GCC system_header
35 #include <bits/c++config.h>
36 #include <bits/cpp_type_traits.h>
38 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 // Define a nested type if some predicate holds.
43 template<bool, typename>
47 template<typename _Tp>
48 struct __enable_if<true, _Tp>
49 { typedef _Tp __type; };
52 // Conditional expression for types. If true, first, if false, second.
53 template<bool _Cond, typename _Iftrue, typename _Iffalse>
54 struct __conditional_type
55 { typedef _Iftrue __type; };
57 template<typename _Iftrue, typename _Iffalse>
58 struct __conditional_type<false, _Iftrue, _Iffalse>
59 { typedef _Iffalse __type; };
62 // Given an integral builtin type, return the corresponding unsigned type.
63 template<typename _Tp>
67 typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
70 typedef typename __if_type::__type __type;
74 struct __add_unsigned<char>
75 { typedef unsigned char __type; };
78 struct __add_unsigned<signed char>
79 { typedef unsigned char __type; };
82 struct __add_unsigned<short>
83 { typedef unsigned short __type; };
86 struct __add_unsigned<int>
87 { typedef unsigned int __type; };
90 struct __add_unsigned<long>
91 { typedef unsigned long __type; };
94 struct __add_unsigned<long long>
95 { typedef unsigned long long __type; };
97 // Declare but don't define.
99 struct __add_unsigned<bool>;
102 struct __add_unsigned<wchar_t>;
105 // Given an integral builtin type, return the corresponding signed type.
106 template<typename _Tp>
107 struct __remove_unsigned
110 typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
113 typedef typename __if_type::__type __type;
117 struct __remove_unsigned<char>
118 { typedef signed char __type; };
121 struct __remove_unsigned<unsigned char>
122 { typedef signed char __type; };
125 struct __remove_unsigned<unsigned short>
126 { typedef short __type; };
129 struct __remove_unsigned<unsigned int>
130 { typedef int __type; };
133 struct __remove_unsigned<unsigned long>
134 { typedef long __type; };
137 struct __remove_unsigned<unsigned long long>
138 { typedef long long __type; };
140 // Declare but don't define.
142 struct __remove_unsigned<bool>;
145 struct __remove_unsigned<wchar_t>;
148 // For use in string and vstring.
149 template<typename _Type>
151 __is_null_pointer(_Type* __ptr)
152 { return __ptr == 0; }
154 template<typename _Type>
156 __is_null_pointer(_Type)
160 // For complex and cmath
161 template<typename _Tp, bool = std::__is_integer<_Tp>::__value>
163 { typedef double __type; };
165 // No nested __type member for non-integer non-floating point types,
166 // allows this type to be used for SFINAE to constrain overloads in
167 // <cmath> and <complex> to only the intended types.
168 template<typename _Tp>
169 struct __promote<_Tp, false>
173 struct __promote<long double>
174 { typedef long double __type; };
177 struct __promote<double>
178 { typedef double __type; };
181 struct __promote<float>
182 { typedef float __type; };
184 template<typename _Tp, typename _Up,
185 typename _Tp2 = typename __promote<_Tp>::__type,
186 typename _Up2 = typename __promote<_Up>::__type>
189 typedef __typeof__(_Tp2() + _Up2()) __type;
192 template<typename _Tp, typename _Up, typename _Vp,
193 typename _Tp2 = typename __promote<_Tp>::__type,
194 typename _Up2 = typename __promote<_Up>::__type,
195 typename _Vp2 = typename __promote<_Vp>::__type>
198 typedef __typeof__(_Tp2() + _Up2() + _Vp2()) __type;
201 template<typename _Tp, typename _Up, typename _Vp, typename _Wp,
202 typename _Tp2 = typename __promote<_Tp>::__type,
203 typename _Up2 = typename __promote<_Up>::__type,
204 typename _Vp2 = typename __promote<_Vp>::__type,
205 typename _Wp2 = typename __promote<_Wp>::__type>
208 typedef __typeof__(_Tp2() + _Up2() + _Vp2() + _Wp2()) __type;
211 _GLIBCXX_END_NAMESPACE_VERSION