]>
Commit | Line | Data |
---|---|---|
54c1bf78 | 1 | // <utility> -*- C++ -*- |
de96ac46 | 2 | |
a945c346 | 3 | // Copyright (C) 2001-2024 Free Software Foundation, Inc. |
de96ac46 BK |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
de96ac46 BK |
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 | ||
748086b7 JJ |
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. | |
de96ac46 | 19 | |
748086b7 JJ |
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/>. | |
de96ac46 | 24 | |
54c1bf78 BK |
25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
30 | * Permission to use, copy, modify, distribute and sell this software | |
31 | * and its documentation for any purpose is hereby granted without fee, | |
32 | * provided that the above copyright notice appear in all copies and | |
33 | * that both that copyright notice and this permission notice appear | |
34 | * in supporting documentation. Hewlett-Packard Company makes no | |
35 | * representations about the suitability of this software for any | |
36 | * purpose. It is provided "as is" without express or implied warranty. | |
37 | * | |
38 | * | |
39 | * Copyright (c) 1996,1997 | |
40 | * Silicon Graphics Computer Systems, Inc. | |
41 | * | |
42 | * Permission to use, copy, modify, distribute and sell this software | |
43 | * and its documentation for any purpose is hereby granted without fee, | |
44 | * provided that the above copyright notice appear in all copies and | |
45 | * that both that copyright notice and this permission notice appear | |
46 | * in supporting documentation. Silicon Graphics makes no | |
47 | * representations about the suitability of this software for any | |
48 | * purpose. It is provided "as is" without express or implied warranty. | |
49 | */ | |
50 | ||
143c27b0 | 51 | /** @file include/utility |
33ac58d5 | 52 | * This is a Standard C++ Library header. |
2f9d51b8 PE |
53 | */ |
54 | ||
1143680e SE |
55 | #ifndef _GLIBCXX_UTILITY |
56 | #define _GLIBCXX_UTILITY 1 | |
54c1bf78 BK |
57 | |
58 | #pragma GCC system_header | |
285b36d6 | 59 | |
8e32aa11 BK |
60 | /** |
61 | * @defgroup utilities Utilities | |
62 | * | |
261d5a4a JW |
63 | * Basic function and class templates used with the rest of the library. |
64 | * Includes pair, swap, forward/move helpers, declval, integer_sequence. | |
8e32aa11 BK |
65 | */ |
66 | ||
54c1bf78 BK |
67 | #include <bits/c++config.h> |
68 | #include <bits/stl_relops.h> | |
69 | #include <bits/stl_pair.h> | |
70 | ||
734f5023 | 71 | #if __cplusplus >= 201103L |
a15f7cb8 | 72 | |
261d5a4a | 73 | #include <initializer_list> |
7d17de7f | 74 | #include <type_traits> |
53dc5044 | 75 | #include <bits/move.h> |
261d5a4a | 76 | #include <bits/utility.h> |
53dc5044 | 77 | |
261d5a4a JW |
78 | #if __cplusplus >= 202002L |
79 | #include <ext/numeric_traits.h> // __is_standard_integer, __int_traits | |
98cf2c26 JW |
80 | #endif |
81 | ||
7ffa63df JW |
82 | #define __glibcxx_want_addressof_constexpr |
83 | #define __glibcxx_want_as_const | |
84 | #define __glibcxx_want_constexpr_algorithms | |
85 | #define __glibcxx_want_constexpr_utility | |
86 | #define __glibcxx_want_exchange_function | |
87 | #define __glibcxx_want_forward_like | |
88 | #define __glibcxx_want_integer_comparison_functions | |
89 | #define __glibcxx_want_integer_sequence | |
90 | #define __glibcxx_want_ranges_zip | |
91 | #define __glibcxx_want_to_underlying | |
92 | #define __glibcxx_want_tuple_element_t | |
93 | #define __glibcxx_want_tuples_by_type | |
94 | #define __glibcxx_want_unreachable | |
95 | #include <bits/version.h> | |
96 | ||
12ffa228 BK |
97 | namespace std _GLIBCXX_VISIBILITY(default) |
98 | { | |
99 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
53dc5044 | 100 | |
083b7f28 | 101 | #ifdef __cpp_lib_exchange_function // C++ >= 14 |
c98b0598 | 102 | /// Assign @p __new_val to @p __obj and return its previous value. |
b5a8fed6 | 103 | template <typename _Tp, typename _Up = _Tp> |
3a66e68a | 104 | _GLIBCXX20_CONSTEXPR |
c98b0598 JW |
105 | inline _Tp |
106 | exchange(_Tp& __obj, _Up&& __new_val) | |
42cfa1bd JW |
107 | noexcept(__and_<is_nothrow_move_constructible<_Tp>, |
108 | is_nothrow_assignable<_Tp&, _Up>>::value) | |
9b817548 | 109 | { return std::__exchange(__obj, std::forward<_Up>(__new_val)); } |
083b7f28 | 110 | #endif |
3a66e68a | 111 | |
083b7f28 | 112 | #ifdef __cpp_lib_as_const // C++ >= 17 |
32eaac9c | 113 | template<typename _Tp> |
406f58e1 JW |
114 | [[nodiscard]] |
115 | constexpr add_const_t<_Tp>& | |
116 | as_const(_Tp& __t) noexcept | |
117 | { return __t; } | |
32eaac9c JW |
118 | |
119 | template<typename _Tp> | |
120 | void as_const(const _Tp&&) = delete; | |
083b7f28 | 121 | #endif |
32eaac9c | 122 | |
083b7f28 | 123 | #ifdef __cpp_lib_integer_comparison_functions // C++ >= 20 |
98cf2c26 JW |
124 | template<typename _Tp, typename _Up> |
125 | constexpr bool | |
126 | cmp_equal(_Tp __t, _Up __u) noexcept | |
127 | { | |
128 | static_assert(__is_standard_integer<_Tp>::value); | |
129 | static_assert(__is_standard_integer<_Up>::value); | |
130 | ||
131 | if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>) | |
132 | return __t == __u; | |
133 | else if constexpr (is_signed_v<_Tp>) | |
134 | return __t >= 0 && make_unsigned_t<_Tp>(__t) == __u; | |
135 | else | |
136 | return __u >= 0 && __t == make_unsigned_t<_Up>(__u); | |
137 | } | |
138 | ||
139 | template<typename _Tp, typename _Up> | |
140 | constexpr bool | |
141 | cmp_not_equal(_Tp __t, _Up __u) noexcept | |
142 | { return !std::cmp_equal(__t, __u); } | |
143 | ||
144 | template<typename _Tp, typename _Up> | |
145 | constexpr bool | |
146 | cmp_less(_Tp __t, _Up __u) noexcept | |
147 | { | |
148 | static_assert(__is_standard_integer<_Tp>::value); | |
149 | static_assert(__is_standard_integer<_Up>::value); | |
150 | ||
151 | if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>) | |
152 | return __t < __u; | |
153 | else if constexpr (is_signed_v<_Tp>) | |
154 | return __t < 0 || make_unsigned_t<_Tp>(__t) < __u; | |
155 | else | |
156 | return __u >= 0 && __t < make_unsigned_t<_Up>(__u); | |
157 | } | |
158 | ||
159 | template<typename _Tp, typename _Up> | |
160 | constexpr bool | |
161 | cmp_greater(_Tp __t, _Up __u) noexcept | |
162 | { return std::cmp_less(__u, __t); } | |
163 | ||
164 | template<typename _Tp, typename _Up> | |
165 | constexpr bool | |
166 | cmp_less_equal(_Tp __t, _Up __u) noexcept | |
167 | { return !std::cmp_less(__u, __t); } | |
168 | ||
169 | template<typename _Tp, typename _Up> | |
170 | constexpr bool | |
171 | cmp_greater_equal(_Tp __t, _Up __u) noexcept | |
172 | { return !std::cmp_less(__t, __u); } | |
173 | ||
97fc8851 | 174 | template<typename _Res, typename _Tp> |
98cf2c26 JW |
175 | constexpr bool |
176 | in_range(_Tp __t) noexcept | |
177 | { | |
97fc8851 | 178 | static_assert(__is_standard_integer<_Res>::value); |
98cf2c26 | 179 | static_assert(__is_standard_integer<_Tp>::value); |
eb04805b | 180 | using __gnu_cxx::__int_traits; |
98cf2c26 | 181 | |
97fc8851 JW |
182 | if constexpr (is_signed_v<_Tp> == is_signed_v<_Res>) |
183 | return __int_traits<_Res>::__min <= __t | |
184 | && __t <= __int_traits<_Res>::__max; | |
98cf2c26 JW |
185 | else if constexpr (is_signed_v<_Tp>) |
186 | return __t >= 0 | |
97fc8851 | 187 | && make_unsigned_t<_Tp>(__t) <= __int_traits<_Res>::__max; |
98cf2c26 | 188 | else |
97fc8851 | 189 | return __t <= make_unsigned_t<_Res>(__int_traits<_Res>::__max); |
98cf2c26 | 190 | } |
083b7f28 | 191 | #endif // __cpp_lib_integer_comparison_functions |
7244879b | 192 | |
083b7f28 | 193 | #ifdef __cpp_lib_to_underlying // C++ >= 23 |
7244879b JW |
194 | /// Convert an object of enumeration type to its underlying type. |
195 | template<typename _Tp> | |
406f58e1 | 196 | [[nodiscard]] |
7244879b JW |
197 | constexpr underlying_type_t<_Tp> |
198 | to_underlying(_Tp __value) noexcept | |
199 | { return static_cast<underlying_type_t<_Tp>>(__value); } | |
083b7f28 | 200 | #endif |
babaabbc | 201 | |
083b7f28 | 202 | #ifdef __cpp_lib_unreachable // C++ >= 23 |
babaabbc JW |
203 | /// Informs the compiler that program control flow never reaches this point. |
204 | /** | |
205 | * Evaluating a call to this function results in undefined behaviour. | |
206 | * This can be used as an assertion informing the compiler that certain | |
207 | * conditions are impossible, for when the compiler is unable to determine | |
208 | * that by itself. | |
209 | * | |
210 | * For example, it can be used to prevent warnings about reaching the | |
211 | * end of a non-void function without returning. | |
212 | * | |
213 | * @since C++23 | |
214 | */ | |
215 | [[noreturn,__gnu__::__always_inline__]] | |
216 | inline void | |
217 | unreachable() | |
218 | { | |
219 | #ifdef _GLIBCXX_DEBUG | |
220 | std::__glibcxx_assert_fail(nullptr, 0, "std::unreachable()", nullptr); | |
221 | #elif defined _GLIBCXX_ASSERTIONS | |
222 | __builtin_trap(); | |
223 | #else | |
224 | __builtin_unreachable(); | |
225 | #endif | |
226 | } | |
083b7f28 | 227 | #endif |
25a69162 | 228 | |
12ffa228 BK |
229 | _GLIBCXX_END_NAMESPACE_VERSION |
230 | } // namespace | |
53dc5044 | 231 | |
af13a7a6 BK |
232 | #endif |
233 | ||
1143680e | 234 | #endif /* _GLIBCXX_UTILITY */ |