1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 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 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_UTILITY 11#define _LIBCPP_UTILITY 12 13/* 14 utility synopsis 15 16#include <initializer_list> 17 18namespace std 19{ 20 21template <class T> 22 void 23 swap(T& a, T& b); 24 25namespace rel_ops 26{ 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&); 31} 32 33template<class T> 34void 35swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value && 36 is_nothrow_move_assignable<T>::value); 37 38template <class T, size_t N> 39void 40swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b))); 41 42template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept; // constexpr in C++14 43template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14 44 45template <typename T> 46[[nodiscard]] constexpr 47auto forward_like(auto&& x) noexcept -> see below; // since C++23 48 49template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14 50 51template <class T> 52 typename conditional 53 < 54 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value, 55 const T&, 56 T&& 57 >::type 58 move_if_noexcept(T& x) noexcept; // constexpr in C++14 59 60template <class T> constexpr add_const_t<T>& as_const(T& t) noexcept; // C++17 61template <class T> void as_const(const T&&) = delete; // C++17 62 63template <class T> typename add_rvalue_reference<T>::type declval() noexcept; 64 65template<class T, class U> constexpr bool cmp_equal(T t, U u) noexcept; // C++20 66template<class T, class U> constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20 67template<class T, class U> constexpr bool cmp_less(T t, U u) noexcept; // C++20 68template<class T, class U> constexpr bool cmp_greater(T t, U u) noexcept; // C++20 69template<class T, class U> constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20 70template<class T, class U> constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20 71template<class R, class T> constexpr bool in_range(T t) noexcept; // C++20 72 73template <class T1, class T2> 74struct pair 75{ 76 typedef T1 first_type; 77 typedef T2 second_type; 78 79 T1 first; 80 T2 second; 81 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); 96 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 109 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 113}; 114 115template<class T1, class T2, class U1, class U2, template<class> class TQual, template<class> class UQual> 116struct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual>; // since C++23 117 118template<class T1, class T2, class U1, class U2> 119struct common_type<pair<T1, T2>, pair<U1, U2>>; // since C++23 120 121template<class T1, class T2> pair(T1, T2) -> pair<T1, T2>; 122 123template <class T1, class T2, class U1, class U2> 124bool operator==(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14 125template <class T1, class T2, class U1, class U2> 126bool operator!=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 127template <class T1, class T2, class U1, class U2> 128bool operator< (const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 129template <class T1, class T2, class U1, class U2> 130bool operator> (const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 131template <class T1, class T2, class U1, class U2> 132bool operator>=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 133template <class T1, class T2, class U1, class U2> 134bool operator<=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 135template <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 139 140template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14 141template <class T1, class T2> 142void 143swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20 144 145template<class T1, class T2> // since C++23 146constexpr void swap(const pair<T1, T2>& x, const pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); 147 148struct piecewise_construct_t { explicit piecewise_construct_t() = default; }; 149inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t(); 150 151template <class T> struct tuple_size; 152template <size_t I, class T> struct tuple_element; 153 154template <class T1, class T2> struct tuple_size<pair<T1, T2> >; 155template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >; 156template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >; 157 158template<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 161 162template<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 165 166template<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 169 170template<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 173 174template<class T1, class T2> 175 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14 176 177template<class T1, class T2> 178 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14 179 180template<class T1, class T2> 181 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14 182 183template<class T1, class T2> 184 constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14 185 186template<class T1, class T2> 187 constexpr T1& get(pair<T2, T1>&) noexcept; // C++14 188 189template<class T1, class T2> 190 constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14 191 192template<class T1, class T2> 193 constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14 194 195template<class T1, class T2> 196 constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14 197 198// C++14 199 200template<class T, T... I> 201struct integer_sequence 202{ 203 typedef T value_type; 204 205 static constexpr size_t size() noexcept; 206}; 207 208template<size_t... I> 209 using index_sequence = integer_sequence<size_t, I...>; 210 211template<class T, T N> 212 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>; 213template<size_t N> 214 using make_index_sequence = make_integer_sequence<size_t, N>; 215 216template<class... T> 217 using index_sequence_for = make_index_sequence<sizeof...(T)>; 218 219template<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); 222 223// 20.2.7, in-place construction // C++17 224struct in_place_t { 225 explicit in_place_t() = default; 226}; 227inline constexpr in_place_t in_place{}; 228template <class T> 229 struct in_place_type_t { 230 explicit in_place_type_t() = default; 231 }; 232template <class T> 233 inline constexpr in_place_type_t<T> in_place_type{}; 234template <size_t I> 235 struct in_place_index_t { 236 explicit in_place_index_t() = default; 237 }; 238template <size_t I> 239 inline constexpr in_place_index_t<I> in_place_index{}; 240 241// [utility.underlying], to_underlying 242template <class T> 243 constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++23 244 245} // std 246 247*/ 248 249#include <__config> 250 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> 258 259#if _LIBCPP_STD_VER >= 14 260# include <__utility/exchange.h> 261# include <__utility/integer_sequence.h> 262#endif 263 264#if _LIBCPP_STD_VER >= 17 265# include <__utility/as_const.h> 266# include <__utility/in_place.h> 267#endif 268 269#if _LIBCPP_STD_VER >= 20 270# include <__utility/cmp.h> 271#endif 272 273#if _LIBCPP_STD_VER >= 23 274# include <__utility/forward_like.h> 275# include <__utility/to_underlying.h> 276# include <__utility/unreachable.h> 277#endif 278 279#include <version> 280 281// standard-mandated includes 282 283// [utility.syn] 284#include <compare> 285#include <initializer_list> 286 287// [tuple.creation] 288 289#include <__tuple/ignore.h> 290 291// [tuple.helper] 292#include <__tuple/tuple_element.h> 293#include <__tuple/tuple_size.h> 294 295#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 296# pragma GCC system_header 297#endif 298 299#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 300# include <limits> 301#endif 302 303#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 304# include <cstdlib> 305# include <iosfwd> 306# include <type_traits> 307#endif 308 309#endif // _LIBCPP_UTILITY 310