| 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 | #ifdef __glibcxx_constant_wrapper // C++ >= 26 |
| 45 | # include <bits/invoke.h> |
| 46 | #endif |
| 47 | |
| 48 | namespace std _GLIBCXX_VISIBILITY(default) |
| 49 | { |
| 50 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 51 | |
| 52 | /// Finds the size of a given tuple type. |
| 53 | template<typename _Tp> |
| 54 | struct tuple_size; |
| 55 | |
| 56 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 57 | // 2313. tuple_size should always derive from integral_constant<size_t, N> |
| 58 | // 2770. tuple_size<const T> specialization is not SFINAE compatible |
| 59 | |
| 60 | template<typename _Tp, |
| 61 | typename _Up = typename remove_cv<_Tp>::type, |
| 62 | typename = typename enable_if<is_same<_Tp, _Up>::value>::type, |
| 63 | size_t = tuple_size<_Tp>::value> |
| 64 | using __enable_if_has_tuple_size = _Tp; |
| 65 | |
| 66 | template<typename _Tp> |
| 67 | struct tuple_size<const __enable_if_has_tuple_size<_Tp>> |
| 68 | : public tuple_size<_Tp> { }; |
| 69 | |
| 70 | template<typename _Tp> |
| 71 | struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>> |
| 72 | : public tuple_size<_Tp> { }; |
| 73 | |
| 74 | template<typename _Tp> |
| 75 | struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>> |
| 76 | : public tuple_size<_Tp> { }; |
| 77 | |
| 78 | #if __cplusplus >= 201703L |
| 79 | template<typename _Tp> |
| 80 | inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value; |
| 81 | #endif |
| 82 | |
| 83 | /// Gives the type of the ith element of a given tuple type. |
| 84 | template<size_t __i, typename _Tp> |
| 85 | struct tuple_element; |
| 86 | |
| 87 | // Duplicate of C++14's tuple_element_t for internal use in C++11 mode |
| 88 | template<size_t __i, typename _Tp> |
| 89 | using __tuple_element_t = typename tuple_element<__i, _Tp>::type; |
| 90 | |
| 91 | template<size_t __i, typename _Tp> |
| 92 | struct tuple_element<__i, const _Tp> |
| 93 | { |
| 94 | using type = const __tuple_element_t<__i, _Tp>; |
| 95 | }; |
| 96 | |
| 97 | template<size_t __i, typename _Tp> |
| 98 | struct tuple_element<__i, volatile _Tp> |
| 99 | { |
| 100 | using type = volatile __tuple_element_t<__i, _Tp>; |
| 101 | }; |
| 102 | |
| 103 | template<size_t __i, typename _Tp> |
| 104 | struct tuple_element<__i, const volatile _Tp> |
| 105 | { |
| 106 | using type = const volatile __tuple_element_t<__i, _Tp>; |
| 107 | }; |
| 108 | |
| 109 | #if __cplusplus >= 201402L |
| 110 | |
| 111 | // Return the index of _Tp in _Types, if it occurs exactly once. |
| 112 | // Otherwise, return sizeof...(_Types). |
| 113 | template<typename _Tp, typename... _Types> |
| 114 | constexpr size_t |
| 115 | __find_uniq_type_in_pack() |
| 116 | { |
| 117 | constexpr size_t __sz = sizeof...(_Types); |
| 118 | constexpr bool __found[__sz] = { __is_same(_Tp, _Types) ... }; |
| 119 | size_t __n = __sz; |
| 120 | for (size_t __i = 0; __i < __sz; ++__i) |
| 121 | { |
| 122 | if (__found[__i]) |
| 123 | { |
| 124 | if (__n < __sz) // more than one _Tp found |
| 125 | return __sz; |
| 126 | __n = __i; |
| 127 | } |
| 128 | } |
| 129 | return __n; |
| 130 | } |
| 131 | #endif // C++14 |
| 132 | |
| 133 | // The standard says this macro and alias template should be in <tuple> but we |
| 134 | // define them here, to be available in <array>, <utility> and <ranges> too. |
| 135 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 136 | // 3378. tuple_size_v/tuple_element_t should be available when |
| 137 | // tuple_size/tuple_element are |
| 138 | #ifdef __glibcxx_tuple_element_t // C++ >= 14 |
| 139 | template<size_t __i, typename _Tp> |
| 140 | using tuple_element_t = typename tuple_element<__i, _Tp>::type; |
| 141 | #endif |
| 142 | |
| 143 | #ifdef __glibcxx_constant_wrapper // C++ >= 26 |
| 144 | template<typename _Tp> |
| 145 | struct _CwFixedValue |
| 146 | { |
| 147 | using __type = _Tp; |
| 148 | |
| 149 | constexpr |
| 150 | _CwFixedValue(__type __v) noexcept |
| 151 | : _M_data(__v) { } |
| 152 | |
| 153 | __type _M_data; |
| 154 | }; |
| 155 | |
| 156 | template<typename _Tp, size_t _Extent> |
| 157 | struct _CwFixedValue<_Tp[_Extent]> |
| 158 | { |
| 159 | using __type = _Tp[_Extent]; |
| 160 | |
| 161 | constexpr |
| 162 | _CwFixedValue(_Tp (&__arr)[_Extent]) noexcept |
| 163 | : _CwFixedValue(__arr, typename _Build_index_tuple<_Extent>::__type()) |
| 164 | { } |
| 165 | |
| 166 | template<size_t... _Indices> |
| 167 | constexpr |
| 168 | _CwFixedValue(_Tp (&__arr)[_Extent], _Index_tuple<_Indices...>) noexcept |
| 169 | : _M_data{__arr[_Indices]...} |
| 170 | { } |
| 171 | |
| 172 | _Tp _M_data[_Extent]; |
| 173 | }; |
| 174 | |
| 175 | template<typename _Tp, size_t _Extent> |
| 176 | _CwFixedValue(_Tp (&)[_Extent]) -> _CwFixedValue<_Tp[_Extent]>; |
| 177 | |
| 178 | template<_CwFixedValue _Xv, |
| 179 | typename = typename decltype(_CwFixedValue(_Xv))::__type> |
| 180 | struct constant_wrapper; |
| 181 | |
| 182 | template<typename _Tp> |
| 183 | concept _ConstExprParam = requires |
| 184 | { |
| 185 | typename constant_wrapper<_Tp::value>; |
| 186 | }; |
| 187 | |
| 188 | struct _CwOperators |
| 189 | { |
| 190 | template<_ConstExprParam _Tp> |
| 191 | friend constexpr auto |
| 192 | operator+(_Tp) noexcept -> constant_wrapper<(+_Tp::value)> |
| 193 | { return {}; } |
| 194 | |
| 195 | template<_ConstExprParam _Tp> |
| 196 | friend constexpr auto |
| 197 | operator-(_Tp) noexcept -> constant_wrapper<(-_Tp::value)> |
| 198 | { return {}; } |
| 199 | |
| 200 | template<_ConstExprParam _Tp> |
| 201 | friend constexpr auto |
| 202 | operator~(_Tp) noexcept -> constant_wrapper<(~_Tp::value)> |
| 203 | { return {}; } |
| 204 | |
| 205 | template<_ConstExprParam _Tp> |
| 206 | friend constexpr auto |
| 207 | operator!(_Tp) noexcept -> constant_wrapper<(!_Tp::value)> |
| 208 | { return {}; } |
| 209 | |
| 210 | template<_ConstExprParam _Tp> |
| 211 | friend constexpr auto |
| 212 | operator&(_Tp) noexcept -> constant_wrapper<(&_Tp::value)> |
| 213 | { return {}; } |
| 214 | |
| 215 | template<_ConstExprParam _Tp> |
| 216 | friend constexpr auto |
| 217 | operator*(_Tp) noexcept -> constant_wrapper<(*_Tp::value)> |
| 218 | { return {}; } |
| 219 | |
| 220 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 221 | friend constexpr auto |
| 222 | operator+(_Left, _Right) noexcept |
| 223 | -> constant_wrapper<(_Left::value + _Right::value)> |
| 224 | { return {}; } |
| 225 | |
| 226 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 227 | friend constexpr auto |
| 228 | operator-(_Left, _Right) noexcept |
| 229 | -> constant_wrapper<(_Left::value - _Right::value)> |
| 230 | { return {}; } |
| 231 | |
| 232 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 233 | friend constexpr auto |
| 234 | operator*(_Left, _Right) noexcept |
| 235 | -> constant_wrapper<(_Left::value * _Right::value)> |
| 236 | { return {}; } |
| 237 | |
| 238 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 239 | friend constexpr auto |
| 240 | operator/(_Left, _Right) noexcept |
| 241 | -> constant_wrapper<(_Left::value / _Right::value)> |
| 242 | { return {}; } |
| 243 | |
| 244 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 245 | friend constexpr auto |
| 246 | operator%(_Left, _Right) noexcept |
| 247 | -> constant_wrapper<(_Left::value % _Right::value)> |
| 248 | { return {}; } |
| 249 | |
| 250 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 251 | friend constexpr auto |
| 252 | operator<<(_Left, _Right) noexcept |
| 253 | -> constant_wrapper<(_Left::value << _Right::value)> |
| 254 | { return {}; } |
| 255 | |
| 256 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 257 | friend constexpr auto |
| 258 | operator>>(_Left, _Right) noexcept |
| 259 | -> constant_wrapper<(_Left::value >> _Right::value)> |
| 260 | { return {}; } |
| 261 | |
| 262 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 263 | friend constexpr auto |
| 264 | operator&(_Left, _Right) noexcept |
| 265 | -> constant_wrapper<(_Left::value & _Right::value)> |
| 266 | { return {}; } |
| 267 | |
| 268 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 269 | friend constexpr auto |
| 270 | operator|(_Left, _Right) noexcept |
| 271 | -> constant_wrapper<(_Left::value | _Right::value)> |
| 272 | { return {}; } |
| 273 | |
| 274 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 275 | friend constexpr auto |
| 276 | operator^(_Left, _Right) noexcept |
| 277 | -> constant_wrapper<(_Left::value ^ _Right::value)> |
| 278 | { return {}; } |
| 279 | |
| 280 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 281 | requires (!is_constructible_v<bool, decltype(_Left::value)> |
| 282 | || !is_constructible_v<bool, decltype(_Right::value)>) |
| 283 | friend constexpr auto |
| 284 | operator&&(_Left, _Right) noexcept |
| 285 | -> constant_wrapper<(_Left::value && _Right::value)> |
| 286 | { return {}; } |
| 287 | |
| 288 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 289 | requires (!is_constructible_v<bool, decltype(_Left::value)> |
| 290 | || !is_constructible_v<bool, decltype(_Right::value)>) |
| 291 | friend constexpr auto |
| 292 | operator||(_Left, _Right) noexcept |
| 293 | -> constant_wrapper<(_Left::value || _Right::value)> |
| 294 | { return {}; } |
| 295 | |
| 296 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 297 | friend constexpr auto |
| 298 | operator<=>(_Left, _Right) noexcept |
| 299 | -> constant_wrapper<(_Left::value <=> _Right::value)> |
| 300 | { return {}; } |
| 301 | |
| 302 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 303 | friend constexpr auto |
| 304 | operator<(_Left, _Right) noexcept |
| 305 | -> constant_wrapper<(_Left::value < _Right::value)> |
| 306 | { return {}; } |
| 307 | |
| 308 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 309 | friend constexpr auto |
| 310 | operator<=(_Left, _Right) noexcept |
| 311 | -> constant_wrapper<(_Left::value <= _Right::value)> |
| 312 | { return {}; } |
| 313 | |
| 314 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 315 | friend constexpr auto |
| 316 | operator==(_Left, _Right) noexcept |
| 317 | -> constant_wrapper<(_Left::value == _Right::value)> |
| 318 | { return {}; } |
| 319 | |
| 320 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 321 | friend constexpr auto |
| 322 | operator!=(_Left, _Right) noexcept |
| 323 | -> constant_wrapper<(_Left::value != _Right::value)> |
| 324 | { return {}; } |
| 325 | |
| 326 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 327 | friend constexpr auto |
| 328 | operator>(_Left, _Right) noexcept |
| 329 | -> constant_wrapper<(_Left::value > _Right::value)> |
| 330 | { return {}; } |
| 331 | |
| 332 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 333 | friend constexpr auto |
| 334 | operator>=(_Left, _Right) noexcept |
| 335 | -> constant_wrapper<(_Left::value >= _Right::value)> |
| 336 | { return {}; } |
| 337 | |
| 338 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 339 | friend constexpr auto |
| 340 | operator,(_Left, _Right) noexcept = delete; |
| 341 | |
| 342 | template<_ConstExprParam _Left, _ConstExprParam _Right> |
| 343 | friend constexpr auto |
| 344 | operator->*(_Left, _Right) noexcept |
| 345 | -> constant_wrapper<_Left::value->*(_Right::value)> |
| 346 | { return {}; } |
| 347 | |
| 348 | template<_ConstExprParam _Tp> |
| 349 | constexpr auto |
| 350 | operator++(this _Tp) noexcept |
| 351 | -> constant_wrapper<(++_Tp::value)> |
| 352 | { return {}; } |
| 353 | |
| 354 | template<_ConstExprParam _Tp> |
| 355 | constexpr auto |
| 356 | operator++(this _Tp, int) noexcept |
| 357 | -> constant_wrapper<(_Tp::value++)> |
| 358 | { return {}; } |
| 359 | |
| 360 | template<_ConstExprParam _Tp> |
| 361 | constexpr auto |
| 362 | operator--(this _Tp) noexcept |
| 363 | -> constant_wrapper<(--_Tp::value)> |
| 364 | { return {}; } |
| 365 | |
| 366 | template<_ConstExprParam _Tp> |
| 367 | constexpr auto |
| 368 | operator--(this _Tp, int) noexcept |
| 369 | -> constant_wrapper<(_Tp::value--)> |
| 370 | { return {}; } |
| 371 | |
| 372 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 373 | constexpr auto |
| 374 | operator+=(this _Tp, _Right) noexcept |
| 375 | -> constant_wrapper<(_Tp::value += _Right::value)> |
| 376 | { return {}; } |
| 377 | |
| 378 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 379 | constexpr auto |
| 380 | operator-=(this _Tp, _Right) noexcept |
| 381 | -> constant_wrapper<(_Tp::value -= _Right::value)> |
| 382 | { return {}; } |
| 383 | |
| 384 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 385 | constexpr auto |
| 386 | operator*=(this _Tp, _Right) noexcept |
| 387 | -> constant_wrapper<(_Tp::value *= _Right::value)> |
| 388 | { return {}; } |
| 389 | |
| 390 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 391 | constexpr auto |
| 392 | operator/=(this _Tp, _Right) noexcept |
| 393 | -> constant_wrapper<(_Tp::value /= _Right::value)> |
| 394 | { return {}; } |
| 395 | |
| 396 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 397 | constexpr auto |
| 398 | operator%=(this _Tp, _Right) noexcept |
| 399 | -> constant_wrapper<(_Tp::value %= _Right::value)> |
| 400 | { return {}; } |
| 401 | |
| 402 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 403 | constexpr auto |
| 404 | operator&=(this _Tp, _Right) noexcept |
| 405 | -> constant_wrapper<(_Tp::value &= _Right::value)> |
| 406 | { return {}; } |
| 407 | |
| 408 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 409 | constexpr auto |
| 410 | operator|=(this _Tp, _Right) noexcept |
| 411 | -> constant_wrapper<(_Tp::value |= _Right::value)> |
| 412 | { return {}; } |
| 413 | |
| 414 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 415 | constexpr auto |
| 416 | operator^=(this _Tp, _Right) noexcept |
| 417 | -> constant_wrapper<(_Tp::value ^= _Right::value)> |
| 418 | { return {}; } |
| 419 | |
| 420 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 421 | constexpr auto |
| 422 | operator<<=(this _Tp, _Right) noexcept |
| 423 | -> constant_wrapper<(_Tp::value <<= _Right::value)> |
| 424 | { return {}; } |
| 425 | |
| 426 | template<_ConstExprParam _Tp, _ConstExprParam _Right> |
| 427 | constexpr auto |
| 428 | operator>>=(this _Tp, _Right) noexcept |
| 429 | -> constant_wrapper<(_Tp::value >>= _Right::value)> |
| 430 | { return {}; } |
| 431 | }; |
| 432 | |
| 433 | template<_CwFixedValue _Xv, typename> |
| 434 | struct constant_wrapper : _CwOperators |
| 435 | { |
| 436 | static constexpr const auto& value = _Xv._M_data; |
| 437 | using type = constant_wrapper; |
| 438 | using value_type = typename decltype(_Xv)::__type; |
| 439 | |
| 440 | template<_ConstExprParam _Right> |
| 441 | constexpr auto |
| 442 | operator=(_Right) const noexcept |
| 443 | -> constant_wrapper<(value = _Right::value)> |
| 444 | { return {}; } |
| 445 | |
| 446 | template<typename... _Args, |
| 447 | bool _ConstExprInvocable = requires { |
| 448 | requires (_ConstExprParam<remove_cvref_t<_Args>> && ...); |
| 449 | typename constant_wrapper<std::__invoke(value, remove_cvref_t<_Args>::value...)>; |
| 450 | }> |
| 451 | requires _ConstExprInvocable || is_invocable_v<const value_type&, _Args...> |
| 452 | static constexpr decltype(auto) |
| 453 | operator()(_Args&&... __args) |
| 454 | noexcept(requires { |
| 455 | requires _ConstExprInvocable || is_nothrow_invocable_v<const value_type&, _Args...>; |
| 456 | }) |
| 457 | { |
| 458 | if constexpr (_ConstExprInvocable) |
| 459 | return constant_wrapper<std::__invoke(value, remove_cvref_t<_Args>::value...)>{}; |
| 460 | else |
| 461 | return std::__invoke(value, std::forward<_Args>(__args)...); |
| 462 | } |
| 463 | |
| 464 | template<typename... _Args, |
| 465 | bool _ConstExprSubscriptable = requires { |
| 466 | requires (_ConstExprParam<remove_cvref_t<_Args>> && ...); |
| 467 | typename constant_wrapper<value[remove_cvref_t<_Args>::value...]>; |
| 468 | }> |
| 469 | requires _ConstExprSubscriptable || requires { value[std::declval<_Args>()...]; } |
| 470 | static constexpr decltype(auto) |
| 471 | operator[](_Args&&... __args) |
| 472 | noexcept(requires { |
| 473 | requires _ConstExprSubscriptable || noexcept(value[std::declval<_Args>()...]); |
| 474 | }) |
| 475 | { |
| 476 | if constexpr (_ConstExprSubscriptable) |
| 477 | return constant_wrapper<value[remove_cvref_t<_Args>::value...]>{}; |
| 478 | else |
| 479 | return value[std::forward<_Args>(__args)...]; |
| 480 | } |
| 481 | |
| 482 | constexpr |
| 483 | operator decltype(value)() const noexcept |
| 484 | { return value; } |
| 485 | }; |
| 486 | |
| 487 | template<typename> |
| 488 | constexpr bool __is_constant_wrapper_v = false; |
| 489 | |
| 490 | template<auto __cw, typename _Fn> |
| 491 | constexpr bool __is_constant_wrapper_v<constant_wrapper<__cw, _Fn>> = true; |
| 492 | |
| 493 | template<_CwFixedValue _Tp> |
| 494 | constexpr auto cw = constant_wrapper<_Tp>{}; |
| 495 | #endif |
| 496 | |
| 497 | #ifdef __glibcxx_integer_sequence // C++ >= 14 |
| 498 | |
| 499 | /// Class template integer_sequence |
| 500 | template<typename _Tp, _Tp... _Idx> |
| 501 | struct integer_sequence |
| 502 | { |
| 503 | #if __cplusplus >= 202002L |
| 504 | static_assert(is_integral_v<_Tp>); |
| 505 | #endif |
| 506 | typedef _Tp value_type; |
| 507 | static constexpr size_t size() noexcept { return sizeof...(_Idx); } |
| 508 | }; |
| 509 | |
| 510 | #if __glibcxx_integer_sequence >= 202511L // C++ >= 26 |
| 511 | /** @brief Structured binding support for `integer_sequence` |
| 512 | |
| 513 | * @since C++26 |
| 514 | * @{ |
| 515 | */ |
| 516 | /// Structured binding support |
| 517 | template<typename _Tp, _Tp... _Idx> |
| 518 | struct tuple_size<integer_sequence<_Tp, _Idx...>> |
| 519 | : integral_constant<size_t, sizeof...(_Idx)> { }; |
| 520 | |
| 521 | template<size_t __i, class _Tp, _Tp... _Idx> |
| 522 | struct tuple_element<__i, integer_sequence<_Tp, _Idx...>> |
| 523 | { |
| 524 | static_assert(__i < sizeof...(_Idx)); |
| 525 | using type = _Tp; |
| 526 | }; |
| 527 | |
| 528 | template<size_t __i, class _Tp, _Tp... _Idx> |
| 529 | struct tuple_element<__i, const integer_sequence<_Tp, _Idx...>> |
| 530 | { |
| 531 | static_assert(__i < sizeof...(_Idx)); |
| 532 | using type = _Tp; |
| 533 | }; |
| 534 | |
| 535 | template<size_t __i, class _Tp, _Tp... _Idx> |
| 536 | [[nodiscard]] |
| 537 | constexpr _Tp |
| 538 | get(integer_sequence<_Tp, _Idx...>) noexcept |
| 539 | { |
| 540 | static_assert(__i < sizeof...(_Idx)); |
| 541 | return _Idx...[__i]; |
| 542 | } |
| 543 | /// @} |
| 544 | #endif // __glibcxx_integer_sequence >= 202511L |
| 545 | |
| 546 | /// Alias template make_integer_sequence |
| 547 | template<typename _Tp, _Tp _Num> |
| 548 | using make_integer_sequence |
| 549 | #if __has_builtin(__make_integer_seq) |
| 550 | = __make_integer_seq<integer_sequence, _Tp, _Num>; |
| 551 | #else |
| 552 | = integer_sequence<_Tp, __integer_pack(_Num)...>; |
| 553 | #endif |
| 554 | |
| 555 | /// Alias template index_sequence |
| 556 | template<size_t... _Idx> |
| 557 | using index_sequence = integer_sequence<size_t, _Idx...>; |
| 558 | |
| 559 | /// Alias template make_index_sequence |
| 560 | template<size_t _Num> |
| 561 | using make_index_sequence = make_integer_sequence<size_t, _Num>; |
| 562 | |
| 563 | /// Alias template index_sequence_for |
| 564 | template<typename... _Types> |
| 565 | using index_sequence_for = make_index_sequence<sizeof...(_Types)>; |
| 566 | #endif // __glibcxx_integer_sequence |
| 567 | |
| 568 | #if __cpp_structured_bindings >= 202411L |
| 569 | #if __has_builtin(__integer_pack) |
| 570 | template <auto _Num, typename _Tp = decltype(_Num)> |
| 571 | inline constexpr _Tp |
| 572 | _IotaArray[_Num] = {__integer_pack(_Tp(_Num))...}; |
| 573 | #elif defined __glibcxx_integer_sequence |
| 574 | template <auto _Num, typename _Tp = decltype(_Num), typename = make_integer_sequence<_Tp, _Num>> |
| 575 | inline constexpr _Tp |
| 576 | _IotaArray[_Num]; |
| 577 | |
| 578 | template <auto _Num, typename _Tp, _Tp... _Is> |
| 579 | inline constexpr _Tp |
| 580 | _IotaArray<_Num, _Tp, integer_sequence<_Tp, _Is...>>[_Num] = {_Is...}; |
| 581 | #endif // __integer_pack |
| 582 | #endif // __cpp_structured_bindings >= 202411L |
| 583 | |
| 584 | #if __cplusplus >= 201703L |
| 585 | |
| 586 | struct in_place_t { |
| 587 | explicit in_place_t() = default; |
| 588 | }; |
| 589 | |
| 590 | inline constexpr in_place_t in_place{}; |
| 591 | |
| 592 | template<typename _Tp> struct in_place_type_t |
| 593 | { |
| 594 | explicit in_place_type_t() = default; |
| 595 | }; |
| 596 | |
| 597 | template<typename _Tp> |
| 598 | inline constexpr in_place_type_t<_Tp> in_place_type{}; |
| 599 | |
| 600 | template<size_t _Idx> struct in_place_index_t |
| 601 | { |
| 602 | explicit in_place_index_t() = default; |
| 603 | }; |
| 604 | |
| 605 | template<size_t _Idx> |
| 606 | inline constexpr in_place_index_t<_Idx> in_place_index{}; |
| 607 | |
| 608 | template<typename> |
| 609 | inline constexpr bool __is_in_place_type_v = false; |
| 610 | |
| 611 | template<typename _Tp> |
| 612 | inline constexpr bool __is_in_place_type_v<in_place_type_t<_Tp>> = true; |
| 613 | |
| 614 | template<typename> |
| 615 | inline constexpr bool __is_in_place_index_v = false; |
| 616 | |
| 617 | template<size_t _Nm> |
| 618 | inline constexpr bool __is_in_place_index_v<in_place_index_t<_Nm>> = true; |
| 619 | |
| 620 | #endif // C++17 |
| 621 | |
| 622 | #if _GLIBCXX_USE_BUILTIN_TRAIT(__type_pack_element) |
| 623 | template<size_t _Np, typename... _Types> |
| 624 | struct _Nth_type |
| 625 | { using type = __type_pack_element<_Np, _Types...>; }; |
| 626 | #else |
| 627 | template<size_t _Np, typename... _Types> |
| 628 | struct _Nth_type |
| 629 | { }; |
| 630 | |
| 631 | template<typename _Tp0, typename... _Rest> |
| 632 | struct _Nth_type<0, _Tp0, _Rest...> |
| 633 | { using type = _Tp0; }; |
| 634 | |
| 635 | template<typename _Tp0, typename _Tp1, typename... _Rest> |
| 636 | struct _Nth_type<1, _Tp0, _Tp1, _Rest...> |
| 637 | { using type = _Tp1; }; |
| 638 | |
| 639 | template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest> |
| 640 | struct _Nth_type<2, _Tp0, _Tp1, _Tp2, _Rest...> |
| 641 | { using type = _Tp2; }; |
| 642 | |
| 643 | template<size_t _Np, typename _Tp0, typename _Tp1, typename _Tp2, |
| 644 | typename... _Rest> |
| 645 | #if __cpp_concepts |
| 646 | requires (_Np >= 3) |
| 647 | #endif |
| 648 | struct _Nth_type<_Np, _Tp0, _Tp1, _Tp2, _Rest...> |
| 649 | : _Nth_type<_Np - 3, _Rest...> |
| 650 | { }; |
| 651 | |
| 652 | #if ! __cpp_concepts // Need additional specializations to avoid ambiguities. |
| 653 | template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest> |
| 654 | struct _Nth_type<0, _Tp0, _Tp1, _Tp2, _Rest...> |
| 655 | { using type = _Tp0; }; |
| 656 | |
| 657 | template<typename _Tp0, typename _Tp1, typename _Tp2, typename... _Rest> |
| 658 | struct _Nth_type<1, _Tp0, _Tp1, _Tp2, _Rest...> |
| 659 | { using type = _Tp1; }; |
| 660 | #endif |
| 661 | #endif |
| 662 | |
| 663 | #if __glibcxx_ranges |
| 664 | namespace ranges::__detail |
| 665 | { |
| 666 | template<typename _Range> |
| 667 | inline constexpr bool __is_subrange = false; |
| 668 | } // namespace __detail |
| 669 | #endif |
| 670 | |
| 671 | // A class (and instance) which can be used in 'tie' when an element |
| 672 | // of a tuple is not required. |
| 673 | struct _Swallow_assign |
| 674 | { |
| 675 | template<class _Tp> |
| 676 | constexpr const _Swallow_assign& |
| 677 | operator=(const _Tp&) const noexcept |
| 678 | { return *this; } |
| 679 | }; |
| 680 | |
| 681 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 682 | // 2773. Making std::ignore constexpr |
| 683 | /** Used with `std::tie` to ignore an element of a tuple |
| 684 | * |
| 685 | * When using `std::tie` to assign the elements of a tuple to variables, |
| 686 | * unwanted elements can be ignored by using `std::ignore`. For example: |
| 687 | * |
| 688 | * ``` |
| 689 | * int x, y; |
| 690 | * std::tie(x, std::ignore, y) = std::make_tuple(1, 2, 3); |
| 691 | * ``` |
| 692 | * |
| 693 | * This assignment will perform `x=1; std::ignore=2; y=3;` which results |
| 694 | * in the second element being ignored. |
| 695 | * |
| 696 | * @since C++11 |
| 697 | */ |
| 698 | _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{}; |
| 699 | |
| 700 | #if __glibcxx_flat_map || __glibcxx_flat_set // >= C++23 |
| 701 | struct sorted_unique_t { explicit sorted_unique_t() = default; }; |
| 702 | inline constexpr sorted_unique_t sorted_unique{}; |
| 703 | |
| 704 | struct sorted_equivalent_t { explicit sorted_equivalent_t() = default; }; |
| 705 | inline constexpr sorted_equivalent_t sorted_equivalent{}; |
| 706 | #endif |
| 707 | |
| 708 | _GLIBCXX_END_NAMESPACE_VERSION |
| 709 | } // namespace |
| 710 | |
| 711 | #endif // C++11 |
| 712 | #endif /* _GLIBCXX_UTILITY_H */ |
| 713 | |