libstdc++
utility.h
Go to the documentation of this file.
1// Utilities used throughout the library -*- C++ -*-
2
3// Copyright (C) 2004-2025 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
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU 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 include/bits/utility.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{utility}
28 *
29 * This file contains the parts of `<utility>` needed by other headers,
30 * so they don't need to include the whole of `<utility>`.
31 */
32
33#ifndef _GLIBCXX_UTILITY_H
34#define _GLIBCXX_UTILITY_H 1
35
36#ifdef _GLIBCXX_SYSHDR
37#pragma GCC system_header
38#endif
39
40#if __cplusplus >= 201103L
41
42#include <type_traits>
43#include <bits/move.h>
44
45namespace std _GLIBCXX_VISIBILITY(default)
46{
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
48
49 /// Finds the size of a given tuple type.
50 template<typename _Tp>
51 struct tuple_size;
52
53 // _GLIBCXX_RESOLVE_LIB_DEFECTS
54 // 2313. tuple_size should always derive from integral_constant<size_t, N>
55 // 2770. tuple_size<const T> specialization is not SFINAE compatible
56
57 template<typename _Tp,
58 typename _Up = typename remove_cv<_Tp>::type,
61 using __enable_if_has_tuple_size = _Tp;
62
63 template<typename _Tp>
64 struct tuple_size<const __enable_if_has_tuple_size<_Tp>>
65 : public tuple_size<_Tp> { };
66
67 template<typename _Tp>
68 struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>>
69 : public tuple_size<_Tp> { };
70
71 template<typename _Tp>
72 struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>>
73 : public tuple_size<_Tp> { };
74
75#if __cplusplus >= 201703L
76 template<typename _Tp>
77 inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
78#endif
79
80 /// Gives the type of the ith element of a given tuple type.
81 template<size_t __i, typename _Tp>
83
84 // Duplicate of C++14's tuple_element_t for internal use in C++11 mode
85 template<size_t __i, typename _Tp>
86 using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
87
88 template<size_t __i, typename _Tp>
89 struct tuple_element<__i, const _Tp>
90 {
91 using type = const __tuple_element_t<__i, _Tp>;
92 };
93
94 template<size_t __i, typename _Tp>
95 struct tuple_element<__i, volatile _Tp>
96 {
97 using type = volatile __tuple_element_t<__i, _Tp>;
98 };
99
100 template<size_t __i, typename _Tp>
101 struct tuple_element<__i, const volatile _Tp>
102 {
103 using type = const volatile __tuple_element_t<__i, _Tp>;
104 };
105
106#if __cplusplus >= 201402L
107
108 // Return the index of _Tp in _Types, if it occurs exactly once.
109 // Otherwise, return sizeof...(_Types).
110 template<typename _Tp, typename... _Types>
111 constexpr size_t
112 __find_uniq_type_in_pack()
113 {
114 constexpr size_t __sz = sizeof...(_Types);
115 constexpr bool __found[__sz] = { __is_same(_Tp, _Types) ... };
116 size_t __n = __sz;
117 for (size_t __i = 0; __i < __sz; ++__i)
118 {
119 if (__found[__i])
120 {
121 if (__n < __sz) // more than one _Tp found
122 return __sz;
123 __n = __i;
124 }
125 }
126 return __n;
127 }
128#endif // C++14
129
130// The standard says this macro and alias template should be in <tuple> but we
131// define them here, to be available in <array>, <utility> and <ranges> too.
132// _GLIBCXX_RESOLVE_LIB_DEFECTS
133// 3378. tuple_size_v/tuple_element_t should be available when
134// tuple_size/tuple_element are
135#ifdef __glibcxx_tuple_element_t // C++ >= 14
136 template<size_t __i, typename _Tp>
137 using tuple_element_t = typename tuple_element<__i, _Tp>::type;
138#endif
139
140#ifdef __glibcxx_integer_sequence // C++ >= 14
141
142 /// Class template integer_sequence
143 template<typename _Tp, _Tp... _Idx>
145 {
146#if __cplusplus >= 202002L
147 static_assert(is_integral_v<_Tp>);
148#endif
149 typedef _Tp value_type;
150 static constexpr size_t size() noexcept { return sizeof...(_Idx); }
151 };
152
153 /// Alias template make_integer_sequence
154 template<typename _Tp, _Tp _Num>
156#if __has_builtin(__make_integer_seq)
157 = __make_integer_seq<integer_sequence, _Tp, _Num>;
158#else
159 = integer_sequence<_Tp, __integer_pack(_Num)...>;
160#endif
161
162 /// Alias template index_sequence
163 template<size_t... _Idx>
164 using index_sequence = integer_sequence<size_t, _Idx...>;
165
166 /// Alias template make_index_sequence
167 template<size_t _Num>
169
170 /// Alias template index_sequence_for
171 template<typename... _Types>
172 using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
173#endif // __glibcxx_integer_sequence
174
175#if __cplusplus >= 201703L
176
177 struct in_place_t {
178 explicit in_place_t() = default;
179 };
180
181 inline constexpr in_place_t in_place{};
182
183 template<typename _Tp> struct in_place_type_t
184 {
185 explicit in_place_type_t() = default;
186 };
187
188 template<typename _Tp>
189 inline constexpr in_place_type_t<_Tp> in_place_type{};
190
191 template<size_t _Idx> struct in_place_index_t
192 {
193 explicit in_place_index_t() = default;
194 };
195
196 template<size_t _Idx>
197 inline constexpr in_place_index_t<_Idx> in_place_index{};
198
199 template<typename>
200 inline constexpr bool __is_in_place_type_v = false;
201
202 template<typename _Tp>
203 inline constexpr bool __is_in_place_type_v<in_place_type_t<_Tp>> = true;
204
205 template<typename>
206 inline constexpr bool __is_in_place_index_v = false;
207
208 template<size_t _Nm>
209 inline constexpr bool __is_in_place_index_v<in_place_index_t<_Nm>> = true;
210
211#endif // C++17
212
213#if _GLIBCXX_USE_BUILTIN_TRAIT(__type_pack_element)
214 template<size_t _Np, typename... _Types>
215 struct _Nth_type
216 { using type = __type_pack_element<_Np, _Types...>; };
217#else
218 template<size_t _Np, typename... _Types>
219 struct _Nth_type
220 { };
221
222 template<typename _Tp0, typename... _Rest>
223 struct _Nth_type<0, _Tp0, _Rest...>
224 { using type = _Tp0; };
225
226 template<typename _Tp0, typename _Tp1, typename... _Rest>
227 struct _Nth_type<1, _Tp0, _Tp1, _Rest...>
228 { using type = _Tp1; };
229
230 template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
231 struct _Nth_type<2, _Tp0, _Tp1, _Tp2, _Rest...>
232 { using type = _Tp2; };
233
234 template<size_t _Np, typename _Tp0, typename _Tp1, typename _Tp2,
235 typename... _Rest>
236#if __cpp_concepts
237 requires (_Np >= 3)
238#endif
239 struct _Nth_type<_Np, _Tp0, _Tp1, _Tp2, _Rest...>
240 : _Nth_type<_Np - 3, _Rest...>
241 { };
242
243#if ! __cpp_concepts // Need additional specializations to avoid ambiguities.
244 template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
245 struct _Nth_type<0, _Tp0, _Tp1, _Tp2, _Rest...>
246 { using type = _Tp0; };
247
248 template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
249 struct _Nth_type<1, _Tp0, _Tp1, _Tp2, _Rest...>
250 { using type = _Tp1; };
251#endif
252#endif
253
254#if __glibcxx_ranges
255 namespace ranges::__detail
256 {
257 template<typename _Range>
258 inline constexpr bool __is_subrange = false;
259 } // namespace __detail
260#endif
261
262 // A class (and instance) which can be used in 'tie' when an element
263 // of a tuple is not required.
264 struct _Swallow_assign
265 {
266 template<class _Tp>
267 constexpr const _Swallow_assign&
268 operator=(const _Tp&) const noexcept
269 { return *this; }
270 };
271
272 // _GLIBCXX_RESOLVE_LIB_DEFECTS
273 // 2773. Making std::ignore constexpr
274 /** Used with `std::tie` to ignore an element of a tuple
275 *
276 * When using `std::tie` to assign the elements of a tuple to variables,
277 * unwanted elements can be ignored by using `std::ignore`. For example:
278 *
279 * ```
280 * int x, y;
281 * std::tie(x, std::ignore, y) = std::make_tuple(1, 2, 3);
282 * ```
283 *
284 * This assignment will perform `x=1; std::ignore=2; y=3;` which results
285 * in the second element being ignored.
286 *
287 * @since C++11
288 */
289 _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
290
291#if __glibcxx_flat_map || __glibcxx_flat_set // >= C++23
292 struct sorted_unique_t { explicit sorted_unique_t() = default; };
293 inline constexpr sorted_unique_t sorted_unique{};
294
295 struct sorted_equivalent_t { explicit sorted_equivalent_t() = default; };
296 inline constexpr sorted_equivalent_t sorted_equivalent{};
297#endif
298
299#if __glibcxx_function_ref // >= C++26
300 template<auto>
301 struct nontype_t
302 {
303 explicit nontype_t() = default;
304 };
305
306 template<auto __val>
307 constexpr nontype_t<__val> nontype{};
308
309 template<typename>
310 inline constexpr bool __is_nontype_v = false;
311
312 template<auto __val>
313 inline constexpr bool __is_nontype_v<nontype_t<__val>> = true;
314#endif
315
316_GLIBCXX_END_NAMESPACE_VERSION
317} // namespace
318
319#endif // C++11
320#endif /* _GLIBCXX_UTILITY_H */
ISO C++ entities toplevel namespace is std.
make_integer_sequence< size_t, _Num > make_index_sequence
Alias template make_index_sequence.
Definition utility.h:166
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
Definition utility.h:162
__make_integer_seq< integer_sequence, _Tp, _Num > make_integer_sequence
Alias template make_integer_sequence.
Definition utility.h:155
constexpr _Swallow_assign ignore
Definition utility.h:287
make_index_sequence< sizeof...(_Types)> index_sequence_for
Alias template index_sequence_for.
Definition utility.h:170
Define a member typedef type only if a boolean constant is true.
Definition type_traits:134
Finds the size of a given tuple type.
Definition utility.h:51
Gives the type of the ith element of a given tuple type.
Definition utility.h:82
Class template integer_sequence.
Definition utility.h:145