3 // Copyright (C) 2005-2025 Free Software Foundation, Inc.
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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file ext/type_traits.h
26 * This file is a GNU extension to the Standard C++ Library.
29 #ifndef _EXT_TYPE_TRAITS
30 #define _EXT_TYPE_TRAITS 1
32 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
36 #include <bits/c++config.h>
37 #include <bits/cpp_type_traits.h>
39 #pragma GCC diagnostic push
40 #pragma GCC diagnostic ignored "-Wlong-long"
44 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 // Define a nested type if some predicate holds.
49 template<bool, typename
>
53 template<typename _Tp
>
54 struct __enable_if
<true, _Tp
>
55 { typedef _Tp __type
; };
58 // Conditional expression for types. If true, first, if false, second.
59 template<bool _Cond
, typename _Iftrue
, typename _Iffalse
>
60 struct __conditional_type
61 { typedef _Iftrue __type
; };
63 template<typename _Iftrue
, typename _Iffalse
>
64 struct __conditional_type
<false, _Iftrue
, _Iffalse
>
65 { typedef _Iffalse __type
; };
68 // Given an integral builtin type, return the corresponding unsigned type.
69 template<typename _Tp
>
73 typedef __enable_if
<std::__is_integer
<_Tp
>::__value
, _Tp
> __if_type
;
76 typedef typename
__if_type::__type __type
;
80 struct __add_unsigned
<char>
81 { typedef unsigned char __type
; };
84 struct __add_unsigned
<signed char>
85 { typedef unsigned char __type
; };
88 struct __add_unsigned
<short>
89 { typedef unsigned short __type
; };
92 struct __add_unsigned
<int>
93 { typedef unsigned int __type
; };
96 struct __add_unsigned
<long>
97 { typedef unsigned long __type
; };
100 struct __add_unsigned
<long long>
101 { typedef unsigned long long __type
; };
103 // Declare but don't define.
105 struct __add_unsigned
<bool>;
108 struct __add_unsigned
<wchar_t>;
111 // Given an integral builtin type, return the corresponding signed type.
112 template<typename _Tp
>
113 struct __remove_unsigned
116 typedef __enable_if
<std::__is_integer
<_Tp
>::__value
, _Tp
> __if_type
;
119 typedef typename
__if_type::__type __type
;
123 struct __remove_unsigned
<char>
124 { typedef signed char __type
; };
127 struct __remove_unsigned
<unsigned char>
128 { typedef signed char __type
; };
131 struct __remove_unsigned
<unsigned short>
132 { typedef short __type
; };
135 struct __remove_unsigned
<unsigned int>
136 { typedef int __type
; };
139 struct __remove_unsigned
<unsigned long>
140 { typedef long __type
; };
143 struct __remove_unsigned
<unsigned long long>
144 { typedef long long __type
; };
146 // Declare but don't define.
148 struct __remove_unsigned
<bool>;
151 struct __remove_unsigned
<wchar_t>;
154 // For use in string and vstring.
155 template<typename _Type
>
158 __is_null_pointer(_Type
* __ptr
)
159 { return __ptr
== 0; }
161 template<typename _Type
>
164 __is_null_pointer(_Type
)
167 #if __cplusplus >= 201103L
169 __is_null_pointer(std::nullptr_t
)
173 // For arithmetic promotions in <complex> and <cmath>
175 template<typename _Tp
, bool = std::__is_integer
<_Tp
>::__value
>
177 { typedef double __type
; };
179 // No nested __type member for non-integer non-floating point types,
180 // allows this type to be used for SFINAE to constrain overloads in
181 // <cmath> and <complex> to only the intended types.
182 template<typename _Tp
>
183 struct __promote
<_Tp
, false>
187 struct __promote
<long double>
188 { typedef long double __type
; };
191 struct __promote
<double>
192 { typedef double __type
; };
195 struct __promote
<float>
196 { typedef float __type
; };
198 #ifdef __STDCPP_FLOAT16_T__
200 struct __promote
<_Float16
>
201 { typedef _Float16 __type
; };
204 #ifdef __STDCPP_FLOAT32_T__
206 struct __promote
<_Float32
>
207 { typedef _Float32 __type
; };
210 #ifdef __STDCPP_FLOAT64_T__
212 struct __promote
<_Float64
>
213 { typedef _Float64 __type
; };
216 #ifdef __STDCPP_FLOAT128_T__
218 struct __promote
<_Float128
>
219 { typedef _Float128 __type
; };
222 #ifdef __STDCPP_BFLOAT16_T__
224 struct __promote
<__gnu_cxx::__bfloat16_t
>
225 { typedef __gnu_cxx::__bfloat16_t __type
; };
228 #if __cpp_fold_expressions
230 template<typename
... _Tp
>
231 using __promoted_t
= decltype((typename __promote
<_Tp
>::__type(0) + ...));
233 // Deducing the promoted type is done by __promoted_t<_Tp...>,
234 // then __promote is used to provide the nested __type member.
235 template<typename _Tp
, typename _Up
>
236 using __promote_2
= __promote
<__promoted_t
<_Tp
, _Up
>>;
238 template<typename _Tp
, typename _Up
, typename _Vp
>
239 using __promote_3
= __promote
<__promoted_t
<_Tp
, _Up
, _Vp
>>;
241 template<typename _Tp
, typename _Up
, typename _Vp
, typename _Wp
>
242 using __promote_4
= __promote
<__promoted_t
<_Tp
, _Up
, _Vp
, _Wp
>>;
246 template<typename _Tp
, typename _Up
,
247 typename _Tp2
= typename __promote
<_Tp
>::__type
,
248 typename _Up2
= typename __promote
<_Up
>::__type
>
251 typedef __typeof__(_Tp2() + _Up2()) __type
;
254 template<typename _Tp
, typename _Up
, typename _Vp
,
255 typename _Tp2
= typename __promote
<_Tp
>::__type
,
256 typename _Up2
= typename __promote
<_Up
>::__type
,
257 typename _Vp2
= typename __promote
<_Vp
>::__type
>
260 typedef __typeof__(_Tp2() + _Up2() + _Vp2()) __type
;
263 template<typename _Tp
, typename _Up
, typename _Vp
, typename _Wp
,
264 typename _Tp2
= typename __promote
<_Tp
>::__type
,
265 typename _Up2
= typename __promote
<_Up
>::__type
,
266 typename _Vp2
= typename __promote
<_Vp
>::__type
,
267 typename _Wp2
= typename __promote
<_Wp
>::__type
>
270 typedef __typeof__(_Tp2() + _Up2() + _Vp2() + _Wp2()) __type
;
274 _GLIBCXX_END_NAMESPACE_VERSION
278 #pragma GCC diagnostic pop