libstdc++: type_traits.h Source File

libstdc++

type_traits.h
Go to the documentation of this file.
1// -*- C++ -*-
2
3// Copyright (C) 2005-2026 Free Software Foundation, Inc.
4//
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
9// version.
10
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.
15
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.
19
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/>.
24
25/** @file ext/type_traits.h
26 * This file is a GNU extension to the Standard C++ Library.
27 */
28
29#ifndef _EXT_TYPE_TRAITS
30#define _EXT_TYPE_TRAITS 1
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36#include <bits/c++config.h>
37#include <bits/cpp_type_traits.h>
38
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wlong-long"
41
42extern "C++" {
43
44namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
45{
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47
48 // Define a nested type if some predicate holds.
49 template<bool, typename>
50 struct __enable_if
51 { };
52
53 template<typename _Tp>
54 struct __enable_if<true, _Tp>
55 { typedef _Tp __type; };
56
57
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; };
62
63 template<typename _Iftrue, typename _Iffalse>
64 struct __conditional_type<false, _Iftrue, _Iffalse>
65 { typedef _Iffalse __type; };
66
67
68 // Given an integral builtin type, return the corresponding unsigned type.
69 template<typename _Tp>
70 struct __add_unsigned
71 {
72 private:
73 typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
74
75 public:
76 typedef typename __if_type::__type __type;
77 };
78
79 template<>
80 struct __add_unsigned<char>
81 { typedef unsigned char __type; };
82
83 template<>
84 struct __add_unsigned<signed char>
85 { typedef unsigned char __type; };
86
87 template<>
88 struct __add_unsigned<short>
89 { typedef unsigned short __type; };
90
91 template<>
92 struct __add_unsigned<int>
93 { typedef unsigned int __type; };
94
95 template<>
96 struct __add_unsigned<long>
97 { typedef unsigned long __type; };
98
99 template<>
100 struct __add_unsigned<long long>
101 { typedef unsigned long long __type; };
102
103 // Declare but don't define.
104 template<>
105 struct __add_unsigned<bool>;
106
107 template<>
108 struct __add_unsigned<wchar_t>;
109
110
111 // Given an integral builtin type, return the corresponding signed type.
112 template<typename _Tp>
113 struct __remove_unsigned
114 {
115 private:
116 typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type;
117
118 public:
119 typedef typename __if_type::__type __type;
120 };
121
122 template<>
123 struct __remove_unsigned<char>
124 { typedef signed char __type; };
125
126 template<>
127 struct __remove_unsigned<unsigned char>
128 { typedef signed char __type; };
129
130 template<>
131 struct __remove_unsigned<unsigned short>
132 { typedef short __type; };
133
134 template<>
135 struct __remove_unsigned<unsigned int>
136 { typedef int __type; };
137
138 template<>
139 struct __remove_unsigned<unsigned long>
140 { typedef long __type; };
141
142 template<>
143 struct __remove_unsigned<unsigned long long>
144 { typedef long long __type; };
145
146 // Declare but don't define.
147 template<>
148 struct __remove_unsigned<bool>;
149
150 template<>
151 struct __remove_unsigned<wchar_t>;
152
153
154 // For use in string and vstring.
155 template<typename _Type>
156 _GLIBCXX_CONSTEXPR
157 inline bool
158 __is_null_pointer(_Type* __ptr)
159 { return __ptr == 0; }
160
161 template<typename _Type>
162 _GLIBCXX_CONSTEXPR
163 inline bool
164 __is_null_pointer(_Type)
165 { return false; }
166
167#if __cplusplus >= 201103L
168 constexpr bool
169 __is_null_pointer(std::nullptr_t)
170 { return true; }
171#endif
172
173 // For arithmetic promotions in <complex> and <cmath>
174
175 template<typename _Tp, bool = std::__is_integer<_Tp>::__value>
176 struct __promote
177 { typedef double __type; };
178
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>
184 { };
185
186 template<>
187 struct __promote<long double>
188 { typedef long double __type; };
189
190 template<>
191 struct __promote<double>
192 { typedef double __type; };
193
194 template<>
195 struct __promote<float>
196 { typedef float __type; };
197
198#ifdef __STDCPP_FLOAT16_T__
199 template<>
200 struct __promote<_Float16>
201 { typedef _Float16 __type; };
202#endif
203
204#ifdef __STDCPP_FLOAT32_T__
205 template<>
206 struct __promote<_Float32>
207 { typedef _Float32 __type; };
208#endif
209
210#ifdef __STDCPP_FLOAT64_T__
211 template<>
212 struct __promote<_Float64>
213 { typedef _Float64 __type; };
214#endif
215
216#ifdef __STDCPP_FLOAT128_T__
217 template<>
218 struct __promote<_Float128>
219 { typedef _Float128 __type; };
220#endif
221
222#ifdef __STDCPP_BFLOAT16_T__
223 template<>
224 struct __promote<__gnu_cxx::__bfloat16_t>
225 { typedef __gnu_cxx::__bfloat16_t __type; };
226#endif
227
228#if __cpp_fold_expressions
229
230 template<typename... _Tp>
231 using __promoted_t = decltype((typename __promote<_Tp>::__type(0) + ...));
232
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>>;
237
238 template<typename _Tp, typename _Up, typename _Vp>
239 using __promote_3 = __promote<__promoted_t<_Tp, _Up, _Vp>>;
240
241 template<typename _Tp, typename _Up, typename _Vp, typename _Wp>
242 using __promote_4 = __promote<__promoted_t<_Tp, _Up, _Vp, _Wp>>;
243
244#else
245
246 template<typename _Tp, typename _Up,
247 typename _Tp2 = typename __promote<_Tp>::__type,
248 typename _Up2 = typename __promote<_Up>::__type>
249 struct __promote_2
250 {
251 typedef __typeof__(_Tp2() + _Up2()) __type;
252 };
253
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>
258 struct __promote_3
259 {
260 typedef __typeof__(_Tp2() + _Up2() + _Vp2()) __type;
261 };
262
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>
268 struct __promote_4
269 {
270 typedef __typeof__(_Tp2() + _Up2() + _Vp2() + _Wp2()) __type;
271 };
272#endif
273
274_GLIBCXX_END_NAMESPACE_VERSION
275} // namespace
276} // extern "C++"
277
278#pragma GCC diagnostic pop
279
280#endif
cpp_type_traits.h
c++config.h
__gnu_cxx
GNU extensions for public use.