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 <class... Args1, class... Args2> 93 pair(piecewise_construct_t, tuple<Args1...> first_args, 94 tuple<Args2...> second_args); // constexpr in C++20 95 96 constexpr const pair& operator=(const pair& p) const; // since C++23 97 template <class U, class V> pair& operator=(const pair<U, V>& p); // constexpr in C++20 98 template <class U, class V> 99 constexpr const pair& operator=(const pair<U, V>& p) const; // since C++23 100 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value && 101 is_nothrow_move_assignable<T2>::value); // constexpr in C++20 102 constexpr const pair& operator=(pair&& p) const; // since C++23 103 template <class U, class V> pair& operator=(pair<U, V>&& p); // constexpr in C++20 104 template <class U, class V> 105 constexpr const pair& operator=(pair<U, V>&& p) const; // since C++23 106 107 void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> && 108 is_nothrow_swappable_v<T2>); // constexpr in C++20 109 constexpr void swap(const pair& p) const noexcept(see below); // since C++23 110}; 111 112template<class T1, class T2, class U1, class U2, template<class> class TQual, template<class> class UQual> 113struct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual>; // since C++23 114 115template<class T1, class T2, class U1, class U2> 116struct common_type<pair<T1, T2>, pair<U1, U2>>; // since C++23 117 118template<class T1, class T2> pair(T1, T2) -> pair<T1, T2>; 119 120template <class T1, class T2, class U1, class U2> 121bool operator==(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14 122template <class T1, class T2, class U1, class U2> 123bool operator!=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 124template <class T1, class T2, class U1, class U2> 125bool operator< (const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 126template <class T1, class T2, class U1, class U2> 127bool operator> (const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 128template <class T1, class T2, class U1, class U2> 129bool operator>=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 130template <class T1, class T2, class U1, class U2> 131bool operator<=(const pair<T1,T2>&, const pair<U1,U2>&); // constexpr in C++14, removed in C++20 132template <class T1, class T2, class U1, class U2> 133 constexpr common_comparison_type_t<synth-three-way-result<T1,U1>, 134 synth-three-way-result<T2,U2>> 135 operator<=>(const pair<T1,T2>&, const pair<U1,U2>&); // C++20 136 137template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14 138template <class T1, class T2> 139void 140swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20 141 142template<class T1, class T2> // since C++23 143constexpr void swap(const pair<T1, T2>& x, const pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); 144 145struct piecewise_construct_t { explicit piecewise_construct_t() = default; }; 146inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t(); 147 148template <class T> struct tuple_size; 149template <size_t I, class T> struct tuple_element; 150 151template <class T1, class T2> struct tuple_size<pair<T1, T2> >; 152template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >; 153template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >; 154 155template<size_t I, class T1, class T2> 156 typename tuple_element<I, pair<T1, T2> >::type& 157 get(pair<T1, T2>&) noexcept; // constexpr in C++14 158 159template<size_t I, class T1, class T2> 160 const typename tuple_element<I, pair<T1, T2> >::type& 161 get(const pair<T1, T2>&) noexcept; // constexpr in C++14 162 163template<size_t I, class T1, class T2> 164 typename tuple_element<I, pair<T1, T2> >::type&& 165 get(pair<T1, T2>&&) noexcept; // constexpr in C++14 166 167template<size_t I, class T1, class T2> 168 const typename tuple_element<I, pair<T1, T2> >::type&& 169 get(const pair<T1, T2>&&) noexcept; // constexpr in C++14 170 171template<class T1, class T2> 172 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14 173 174template<class T1, class T2> 175 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14 176 177template<class T1, class T2> 178 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14 179 180template<class T1, class T2> 181 constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14 182 183template<class T1, class T2> 184 constexpr T1& get(pair<T2, T1>&) noexcept; // C++14 185 186template<class T1, class T2> 187 constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14 188 189template<class T1, class T2> 190 constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14 191 192template<class T1, class T2> 193 constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14 194 195// C++14 196 197template<class T, T... I> 198struct integer_sequence 199{ 200 typedef T value_type; 201 202 static constexpr size_t size() noexcept; 203}; 204 205template<size_t... I> 206 using index_sequence = integer_sequence<size_t, I...>; 207 208template<class T, T N> 209 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>; 210template<size_t N> 211 using make_index_sequence = make_integer_sequence<size_t, N>; 212 213template<class... T> 214 using index_sequence_for = make_index_sequence<sizeof...(T)>; 215 216template<class T, class U=T> 217 constexpr T exchange(T& obj, U&& new_value) // constexpr in C++17, noexcept in C++23 218 noexcept(is_nothrow_move_constructible<T>::value && is_nothrow_assignable<T&, U>::value); 219 220// 20.2.7, in-place construction // C++17 221struct in_place_t { 222 explicit in_place_t() = default; 223}; 224inline constexpr in_place_t in_place{}; 225template <class T> 226 struct in_place_type_t { 227 explicit in_place_type_t() = default; 228 }; 229template <class T> 230 inline constexpr in_place_type_t<T> in_place_type{}; 231template <size_t I> 232 struct in_place_index_t { 233 explicit in_place_index_t() = default; 234 }; 235template <size_t I> 236 inline constexpr in_place_index_t<I> in_place_index{}; 237 238// [utility.underlying], to_underlying 239template <class T> 240 constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b 241 242} // std 243 244*/ 245 246#include <__assert> // all public C++ headers provide the assertion handler 247#include <__config> 248#include <__utility/as_const.h> 249#include <__utility/auto_cast.h> 250#include <__utility/cmp.h> 251#include <__utility/declval.h> 252#include <__utility/exception_guard.h> 253#include <__utility/exchange.h> 254#include <__utility/forward.h> 255#include <__utility/forward_like.h> 256#include <__utility/in_place.h> 257#include <__utility/integer_sequence.h> 258#include <__utility/move.h> 259#include <__utility/pair.h> 260#include <__utility/piecewise_construct.h> 261#include <__utility/priority_tag.h> 262#include <__utility/rel_ops.h> 263#include <__utility/swap.h> 264#include <__utility/to_underlying.h> 265#include <__utility/unreachable.h> 266#include <version> 267 268// standard-mandated includes 269 270// [utility.syn] 271#include <compare> 272#include <initializer_list> 273 274// [tuple.helper] 275#include <__tuple/tuple_element.h> 276#include <__tuple/tuple_size.h> 277 278#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 279# pragma GCC system_header 280#endif 281 282#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 283# include <cstdlib> 284# include <iosfwd> 285# include <type_traits> 286#endif 287 288#endif // _LIBCPP_UTILITY 289