libstdc++
utility.h
Go to the documentation of this file.
1 // Utilities used throughout the library -*- C++ -*-
2 
3 // Copyright (C) 2004-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
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 
45 namespace 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,
59  typename = typename enable_if<is_same<_Tp, _Up>::value>::type,
60  size_t = tuple_size<_Tp>::value>
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>
82  struct tuple_element;
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 __cpp_structured_bindings >= 202411L
176 #if __has_builtin(__integer_pack)
177  template <auto _Num, typename _Tp = decltype(_Num)>
178  inline constexpr _Tp
179  _IotaArray[_Num] = {__integer_pack(_Tp(_Num))...};
180 #elif defined __glibcxx_integer_sequence
181  template <auto _Num, typename _Tp = decltype(_Num), typename = make_integer_sequence<_Tp, _Num>>
182  inline constexpr _Tp
183  _IotaArray[_Num];
184 
185  template <auto _Num, typename _Tp, _Tp... _Is>
186  inline constexpr _Tp
187  _IotaArray<_Num, _Tp, integer_sequence<_Tp, _Is...>>[_Num] = {_Is...};
188 #endif // __integer_pack
189 #endif // __cpp_structured_bindings >= 202411L
190 
191 #if __cplusplus >= 201703L
192 
193  struct in_place_t {
194  explicit in_place_t() = default;
195  };
196 
197  inline constexpr in_place_t in_place{};
198 
199  template<typename _Tp> struct in_place_type_t
200  {
201  explicit in_place_type_t() = default;
202  };
203 
204  template<typename _Tp>
205  inline constexpr in_place_type_t<_Tp> in_place_type{};
206 
207  template<size_t _Idx> struct in_place_index_t
208  {
209  explicit in_place_index_t() = default;
210  };
211 
212  template<size_t _Idx>
213  inline constexpr in_place_index_t<_Idx> in_place_index{};
214 
215  template<typename>
216  inline constexpr bool __is_in_place_type_v = false;
217 
218  template<typename _Tp>
219  inline constexpr bool __is_in_place_type_v<in_place_type_t<_Tp>> = true;
220 
221  template<typename>
222  inline constexpr bool __is_in_place_index_v = false;
223 
224  template<size_t _Nm>
225  inline constexpr bool __is_in_place_index_v<in_place_index_t<_Nm>> = true;
226 
227 #endif // C++17
228 
229 #if _GLIBCXX_USE_BUILTIN_TRAIT(__type_pack_element)
230  template<size_t _Np, typename... _Types>
231  struct _Nth_type
232  { using type = __type_pack_element<_Np, _Types...>; };
233 #else
234  template<size_t _Np, typename... _Types>
235  struct _Nth_type
236  { };
237 
238  template<typename _Tp0, typename... _Rest>
239  struct _Nth_type<0, _Tp0, _Rest...>
240  { using type = _Tp0; };
241 
242  template<typename _Tp0, typename _Tp1, typename... _Rest>
243  struct _Nth_type<1, _Tp0, _Tp1, _Rest...>
244  { using type = _Tp1; };
245 
246  template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
247  struct _Nth_type<2, _Tp0, _Tp1, _Tp2, _Rest...>
248  { using type = _Tp2; };
249 
250  template<size_t _Np, typename _Tp0, typename _Tp1, typename _Tp2,
251  typename... _Rest>
252 #if __cpp_concepts
253  requires (_Np >= 3)
254 #endif
255  struct _Nth_type<_Np, _Tp0, _Tp1, _Tp2, _Rest...>
256  : _Nth_type<_Np - 3, _Rest...>
257  { };
258 
259 #if ! __cpp_concepts // Need additional specializations to avoid ambiguities.
260  template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
261  struct _Nth_type<0, _Tp0, _Tp1, _Tp2, _Rest...>
262  { using type = _Tp0; };
263 
264  template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest>
265  struct _Nth_type<1, _Tp0, _Tp1, _Tp2, _Rest...>
266  { using type = _Tp1; };
267 #endif
268 #endif
269 
270 #if __glibcxx_ranges
271  namespace ranges::__detail
272  {
273  template<typename _Range>
274  inline constexpr bool __is_subrange = false;
275  } // namespace __detail
276 #endif
277 
278  // A class (and instance) which can be used in 'tie' when an element
279  // of a tuple is not required.
280  struct _Swallow_assign
281  {
282  template<class _Tp>
283  constexpr const _Swallow_assign&
284  operator=(const _Tp&) const noexcept
285  { return *this; }
286  };
287 
288  // _GLIBCXX_RESOLVE_LIB_DEFECTS
289  // 2773. Making std::ignore constexpr
290  /** Used with `std::tie` to ignore an element of a tuple
291  *
292  * When using `std::tie` to assign the elements of a tuple to variables,
293  * unwanted elements can be ignored by using `std::ignore`. For example:
294  *
295  * ```
296  * int x, y;
297  * std::tie(x, std::ignore, y) = std::make_tuple(1, 2, 3);
298  * ```
299  *
300  * This assignment will perform `x=1; std::ignore=2; y=3;` which results
301  * in the second element being ignored.
302  *
303  * @since C++11
304  */
305  _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
306 
307 #if __glibcxx_flat_map || __glibcxx_flat_set // >= C++23
308  struct sorted_unique_t { explicit sorted_unique_t() = default; };
309  inline constexpr sorted_unique_t sorted_unique{};
310 
311  struct sorted_equivalent_t { explicit sorted_equivalent_t() = default; };
312  inline constexpr sorted_equivalent_t sorted_equivalent{};
313 #endif
314 
315 #if __glibcxx_function_ref // >= C++26
316  template<auto>
317  struct nontype_t
318  {
319  explicit nontype_t() = default;
320  };
321 
322  template<auto __val>
323  constexpr nontype_t<__val> nontype{};
324 
325  template<typename>
326  inline constexpr bool __is_nontype_v = false;
327 
328  template<auto __val>
329  inline constexpr bool __is_nontype_v<nontype_t<__val>> = true;
330 #endif
331 
332 _GLIBCXX_END_NAMESPACE_VERSION
333 } // namespace
334 
335 #endif // C++11
336 #endif /* _GLIBCXX_UTILITY_H */
make_integer_sequence< size_t, _Num > make_index_sequence
Alias template make_index_sequence.
Definition: utility.h:168
Class template integer_sequence.
Definition: utility.h:144
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition: range_access.h:274
Gives the type of the ith element of a given tuple type.
Definition: utility.h:82
__make_integer_seq< integer_sequence, _Tp, _Num > make_integer_sequence
Alias template make_integer_sequence.
Definition: utility.h:157
ISO C++ entities toplevel namespace is std.
constexpr _Swallow_assign ignore
Definition: utility.h:305
Finds the size of a given tuple type.
Definition: utility.h:51
make_index_sequence< sizeof...(_Types)> index_sequence_for
Alias template index_sequence_for.
Definition: utility.h:172
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:136