2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_UTILITY
11 #define _LIBCPP_UTILITY
16 #include <initializer_list>
27 template<class T> bool operator!=(const T&, const T&);
28 template<class T> bool operator> (const T&, const T&);
29 template<class T> bool operator<=(const T&, const T&);
30 template<class T> bool operator>=(const T&, const T&);
35 swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
36 is_nothrow_move_assignable<T>::value);
38 template <class T, size_t N>
40 swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
42 template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept; // constexpr in C++14
43 template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14
46 [[nodiscard]] constexpr
47 auto forward_like(auto&& x) noexcept -> see below; // since C++23
49 template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14
54 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
58 move_if_noexcept(T& x) noexcept; // constexpr in C++14
60 template <class T> constexpr add_const_t<T>& as_const(T& t) noexcept; // C++17
61 template <class T> void as_const(const T&&) = delete; // C++17
63 template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
65 template<class T, class U> constexpr bool cmp_equal(T t, U u) noexcept; // C++20
66 template<class T, class U> constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20
67 template<class T, class U> constexpr bool cmp_less(T t, U u) noexcept; // C++20
68 template<class T, class U> constexpr bool cmp_greater(T t, U u) noexcept; // C++20
69 template<class T, class U> constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20
70 template<class T, class U> constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20
71 template<class R, class T> constexpr bool in_range(T t) noexcept; // C++20
73 template <class T1, class T2>
76 typedef T1 first_type;
77 typedef T2 second_type;
82 pair(const pair&) = default;
83 pair(pair&&) = default;
84 explicit(see-below) constexpr pair();
85 explicit(see-below) pair(const T1& x, const T2& y); // constexpr in C++14
86 template <class U = T1, class V = T2> explicit(see-below) pair(U&&, V&&); // constexpr in C++14
87 template <class U, class V> constexpr explicit(see-below) pair(pair<U, V>&); // since C++23
88 template <class U, class V> explicit(see-below) pair(const pair<U, V>& p); // constexpr in C++14
89 template <class U, class V> explicit(see-below) pair(pair<U, V>&& p); // constexpr in C++14
90 template <class U, class V>
91 constexpr explicit(see-below) pair(const pair<U, V>&&); // since C++23
92 template <pair-like P> constexpr explicit(see-below) pair(P&&); // since C++23
93 template <class... Args1, class... Args2>
94 pair(piecewise_construct_t, tuple<Args1...> first_args, // constexpr in C++20
95 tuple<Args2...> second_args);
97 constexpr const pair& operator=(const pair& p) const; // since C++23
98 template <class U, class V> pair& operator=(const pair<U, V>& p); // constexpr in C++20
99 template <class U, class V>
100 constexpr const pair& operator=(const pair<U, V>& p) const; // since C++23
101 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
102 is_nothrow_move_assignable<T2>::value); // constexpr in C++20
103 constexpr const pair& operator=(pair&& p) const; // since C++23
104 template <class U, class V> pair& operator=(pair<U, V>&& p); // constexpr in C++20
105 template <class U, class V>
106 constexpr const pair& operator=(pair<U, V>&& p) const; // since C++23
107 template <pair-like P> constexpr pair& operator=(P&&); // since C++23
108 template <pair-like P> constexpr const pair& operator=(P&&) const; // since C++23
110 void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
111 is_nothrow_swappable_v<T2>); // constexpr in C++20
112 constexpr void swap(const pair& p) const noexcept(see below); // since C++23
115 template<class T1, class T2, class U1, class U2, template<class> class TQual, template<class> class UQual>
116 struct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual>; // since C++23
118 template<class T1, class T2, class U1, class U2>
119 struct common_type<pair<T1, T2>, pair<U1, U2>>; // since C++23
121 template<class T1, class T2> pair(T1, T2) -> pair<T1, T2>;
123 template <class T1, class T2, class U1, class U2>
124 bool operator==(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14
125 template <class T1, class T2, class U1, class U2>
126 bool operator!=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20
127 template <class T1, class T2, class U1, class U2>
128 bool operator< (const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20
129 template <class T1, class T2, class U1, class U2>
130 bool operator> (const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20
131 template <class T1, class T2, class U1, class U2>
132 bool operator>=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20
133 template <class T1, class T2, class U1, class U2>
134 bool operator<=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20
135 template <class T1, class T2, class U1, class U2>
136 constexpr common_comparison_type_t<synth-three-way-result<T1,U1>,
137 synth-three-way-result<T2,U2>>
138 operator<=>(const pair<T1,T2>&, const pair<U1,U2>&); // C++20
140 template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
141 template <class T1, class T2>
143 swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
145 template<class T1, class T2> // since C++23
146 constexpr void swap(const pair<T1, T2>& x, const pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
148 struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
149 inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
151 template <class T> struct tuple_size;
152 template <size_t I, class T> struct tuple_element;
154 template <class T1, class T2> struct tuple_size<pair<T1, T2> >;
155 template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
156 template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
158 template<size_t I, class T1, class T2>
159 typename tuple_element<I, pair<T1, T2> >::type&
160 get(pair<T1, T2>&) noexcept; // constexpr in C++14
162 template<size_t I, class T1, class T2>
163 const typename tuple_element<I, pair<T1, T2> >::type&
164 get(const pair<T1, T2>&) noexcept; // constexpr in C++14
166 template<size_t I, class T1, class T2>
167 typename tuple_element<I, pair<T1, T2> >::type&&
168 get(pair<T1, T2>&&) noexcept; // constexpr in C++14
170 template<size_t I, class T1, class T2>
171 const typename tuple_element<I, pair<T1, T2> >::type&&
172 get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
174 template<class T1, class T2>
175 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
177 template<class T1, class T2>
178 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
180 template<class T1, class T2>
181 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
183 template<class T1, class T2>
184 constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
186 template<class T1, class T2>
187 constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
189 template<class T1, class T2>
190 constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
192 template<class T1, class T2>
193 constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
195 template<class T1, class T2>
196 constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
200 template<class T, T... I>
201 struct integer_sequence
203 typedef T value_type;
205 static constexpr size_t size() noexcept;
208 template<size_t... I>
209 using index_sequence = integer_sequence<size_t, I...>;
211 template<class T, T N>
212 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
214 using make_index_sequence = make_integer_sequence<size_t, N>;
217 using index_sequence_for = make_index_sequence<sizeof...(T)>;
219 template<class T, class U=T>
220 constexpr T exchange(T& obj, U&& new_value) // constexpr in C++17, noexcept in C++23
221 noexcept(is_nothrow_move_constructible<T>::value && is_nothrow_assignable<T&, U>::value);
223 // 20.2.7, in-place construction // C++17
225 explicit in_place_t() = default;
227 inline constexpr in_place_t in_place{};
229 struct in_place_type_t {
230 explicit in_place_type_t() = default;
233 inline constexpr in_place_type_t<T> in_place_type{};
235 struct in_place_index_t {
236 explicit in_place_index_t() = default;
239 inline constexpr in_place_index_t<I> in_place_index{};
241 // [utility.underlying], to_underlying
243 constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++23
251 #include <__utility/declval.h>
252 #include <__utility/forward.h>
253 #include <__utility/move.h>
254 #include <__utility/pair.h>
255 #include <__utility/piecewise_construct.h>
256 #include <__utility/rel_ops.h>
257 #include <__utility/swap.h>
259 #if _LIBCPP_STD_VER >= 14
260 # include <__utility/exchange.h>
261 # include <__utility/integer_sequence.h>
264 #if _LIBCPP_STD_VER >= 17
265 # include <__utility/as_const.h>
266 # include <__utility/in_place.h>
269 #if _LIBCPP_STD_VER >= 20
270 # include <__utility/cmp.h>
273 #if _LIBCPP_STD_VER >= 23
274 # include <__utility/forward_like.h>
275 # include <__utility/to_underlying.h>
276 # include <__utility/unreachable.h>
281 // standard-mandated includes
285 #include <initializer_list>
289 #include <__tuple/ignore.h>
292 #include <__tuple/tuple_element.h>
293 #include <__tuple/tuple_size.h>
295 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
296 # pragma GCC system_header
299 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
303 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
307 # include <type_traits>
310 #endif // _LIBCPP_UTILITY