13e519524SHoward Hinnant// -*- C++ -*-
2eb8650a7SLouis Dionne//===----------------------------------------------------------------------===//
33e519524SHoward Hinnant//
457b08b09SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
557b08b09SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
657b08b09SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
73e519524SHoward Hinnant//
83e519524SHoward Hinnant//===----------------------------------------------------------------------===//
93e519524SHoward Hinnant
103e519524SHoward Hinnant#ifndef _LIBCPP_UTILITY
113e519524SHoward Hinnant#define _LIBCPP_UTILITY
123e519524SHoward Hinnant
133e519524SHoward Hinnant/*
143e519524SHoward Hinnant    utility synopsis
153e519524SHoward Hinnant
1614c922a2SLouis Dionne#include <initializer_list>
1714c922a2SLouis Dionne
183e519524SHoward Hinnantnamespace std
193e519524SHoward Hinnant{
203e519524SHoward Hinnant
213e519524SHoward Hinnanttemplate <class T>
223e519524SHoward Hinnant    void
233e519524SHoward Hinnant    swap(T& a, T& b);
243e519524SHoward Hinnant
253e519524SHoward Hinnantnamespace rel_ops
263e519524SHoward Hinnant{
273e519524SHoward Hinnant    template<class T> bool operator!=(const T&, const T&);
283e519524SHoward Hinnant    template<class T> bool operator> (const T&, const T&);
293e519524SHoward Hinnant    template<class T> bool operator<=(const T&, const T&);
303e519524SHoward Hinnant    template<class T> bool operator>=(const T&, const T&);
313e519524SHoward Hinnant}
323e519524SHoward Hinnant
33a676f7d3SHoward Hinnanttemplate<class T>
34a676f7d3SHoward Hinnantvoid
35a676f7d3SHoward Hinnantswap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
36a676f7d3SHoward Hinnant                          is_nothrow_move_assignable<T>::value);
373e519524SHoward Hinnant
38a676f7d3SHoward Hinnanttemplate <class T, size_t N>
39a676f7d3SHoward Hinnantvoid
40a676f7d3SHoward Hinnantswap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
41a676f7d3SHoward Hinnant
421c682f0fSMarshall Clowtemplate <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;  // constexpr in C++14
431c682f0fSMarshall Clowtemplate <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14
44a676f7d3SHoward Hinnant
451c682f0fSMarshall Clowtemplate <class T> typename remove_reference<T>::type&& move(T&&) noexcept;      // constexpr in C++14
463e519524SHoward Hinnant
473e519524SHoward Hinnanttemplate <class T>
483e519524SHoward Hinnant    typename conditional
493e519524SHoward Hinnant    <
50ca740483SHoward Hinnant        !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
513e519524SHoward Hinnant        const T&,
523e519524SHoward Hinnant        T&&
533e519524SHoward Hinnant    >::type
541c682f0fSMarshall Clow    move_if_noexcept(T& x) noexcept; // constexpr in C++14
553e519524SHoward Hinnant
56026b75f2SMarshall Clowtemplate <class T> constexpr add_const_t<T>& as_const(T& t) noexcept;      // C++17
57dbd2d328SMarshall Clowtemplate <class T>                      void as_const(const T&&) = delete; // C++17
58dbd2d328SMarshall Clow
593e519524SHoward Hinnanttemplate <class T> typename add_rvalue_reference<T>::type declval() noexcept;
603e519524SHoward Hinnant
6136c3918eSKamlesh Kumartemplate<class T, class U> constexpr bool cmp_equal(T t, U u) noexcept;         // C++20
6236c3918eSKamlesh Kumartemplate<class T, class U> constexpr bool cmp_not_equal(T t, U u) noexcept;     // C++20
6336c3918eSKamlesh Kumartemplate<class T, class U> constexpr bool cmp_less(T t, U u) noexcept;          // C++20
6436c3918eSKamlesh Kumartemplate<class T, class U> constexpr bool cmp_greater(T t, U u) noexcept;       // C++20
6536c3918eSKamlesh Kumartemplate<class T, class U> constexpr bool cmp_less_equal(T t, U u) noexcept;    // C++20
6636c3918eSKamlesh Kumartemplate<class T, class U> constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20
6736c3918eSKamlesh Kumartemplate<class R, class T> constexpr bool in_range(T t) noexcept;               // C++20
6836c3918eSKamlesh Kumar
693e519524SHoward Hinnanttemplate <class T1, class T2>
703e519524SHoward Hinnantstruct pair
713e519524SHoward Hinnant{
723e519524SHoward Hinnant    typedef T1 first_type;
733e519524SHoward Hinnant    typedef T2 second_type;
743e519524SHoward Hinnant
753e519524SHoward Hinnant    T1 first;
763e519524SHoward Hinnant    T2 second;
773e519524SHoward Hinnant
783e519524SHoward Hinnant    pair(const pair&) = default;
79a676f7d3SHoward Hinnant    pair(pair&&) = default;
80e16f2cb6SLouis Dionne    explicit(see-below) constexpr pair();
81e35c5121SLouis Dionne    explicit(see-below) pair(const T1& x, const T2& y);                          // constexpr in C++14
82312ad74aSLouis Dionne    template <class U = T1, class V = T2> explicit(see-below) pair(U&&, V&&);    // constexpr in C++14
83e35c5121SLouis Dionne    template <class U, class V> explicit(see-below) pair(const pair<U, V>& p);   // constexpr in C++14
84e35c5121SLouis Dionne    template <class U, class V> explicit(see-below) pair(pair<U, V>&& p);        // constexpr in C++14
853e519524SHoward Hinnant    template <class... Args1, class... Args2>
863e519524SHoward Hinnant        pair(piecewise_construct_t, tuple<Args1...> first_args,
8706e2b737SArthur O'Dwyer             tuple<Args2...> second_args);                                       // constexpr in C++20
883e519524SHoward Hinnant
8906e2b737SArthur O'Dwyer    template <class U, class V> pair& operator=(const pair<U, V>& p);            // constexpr in C++20
90a676f7d3SHoward Hinnant    pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
9106e2b737SArthur O'Dwyer                                       is_nothrow_move_assignable<T2>::value);   // constexpr in C++20
9206e2b737SArthur O'Dwyer    template <class U, class V> pair& operator=(pair<U, V>&& p);                 // constexpr in C++20
933e519524SHoward Hinnant
94f07dd8d0SEric Fiselier    void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
9506e2b737SArthur O'Dwyer                                is_nothrow_swappable_v<T2>);                     // constexpr in C++20
963e519524SHoward Hinnant};
973e519524SHoward Hinnant
98598983d7SNikolas Klausertemplate<class T1, class T2, class U1, class U2, template<class> class TQual, template<class> class UQual>
99598983d7SNikolas Klauserstruct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual>;         // since C++23
100598983d7SNikolas Klauser
101598983d7SNikolas Klausertemplate<class T1, class T2, class U1, class U2>
102598983d7SNikolas Klauserstruct common_type<pair<T1, T2>, pair<U1, U2>>;                                  // since C++23
103598983d7SNikolas Klauser
104f9f97caeSKonstantin Varlamovtemplate<class T1, class T2> pair(T1, T2) -> pair<T1, T2>;
105f9f97caeSKonstantin Varlamov
10618191cebSMarshall Clowtemplate <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
107f4abdb0cSKent Rosstemplate <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
108f4abdb0cSKent Rosstemplate <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
109f4abdb0cSKent Rosstemplate <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
110f4abdb0cSKent Rosstemplate <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
111f4abdb0cSKent Rosstemplate <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
112f4abdb0cSKent Rosstemplate <class T1, class T2>
113f4abdb0cSKent Ross  constexpr common_comparison_type_t<synth-three-way-result<T1>,
114f4abdb0cSKent Ross                                     synth-three-way-result<T2>>
115f4abdb0cSKent Ross    operator<=>(const pair<T1,T2>&, const pair<T1,T2>&);                               // C++20
1163e519524SHoward Hinnant
11718191cebSMarshall Clowtemplate <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&);                // constexpr in C++14
118a676f7d3SHoward Hinnanttemplate <class T1, class T2>
119a676f7d3SHoward Hinnantvoid
12006e2b737SArthur O'Dwyerswap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));            // constexpr in C++20
1213e519524SHoward Hinnant
122e16f2cb6SLouis Dionnestruct piecewise_construct_t { explicit piecewise_construct_t() = default; };
12340a01d53SMarshall Clowinline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
1243e519524SHoward Hinnant
125e4957601SMarshall Clowtemplate <class T> struct tuple_size;
1262b0c7abbSLouis Dionnetemplate <size_t I, class T> struct tuple_element;
1273e519524SHoward Hinnant
12805c8dad2SMarshall Clowtemplate <class T1, class T2> struct tuple_size<pair<T1, T2> >;
12905c8dad2SMarshall Clowtemplate <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
13005c8dad2SMarshall Clowtemplate <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
1313e519524SHoward Hinnant
1323e519524SHoward Hinnanttemplate<size_t I, class T1, class T2>
13305c8dad2SMarshall Clow    typename tuple_element<I, pair<T1, T2> >::type&
13405c8dad2SMarshall Clow    get(pair<T1, T2>&) noexcept; // constexpr in C++14
1353e519524SHoward Hinnant
1363e519524SHoward Hinnanttemplate<size_t I, class T1, class T2>
137bfb3f2bfSMarshall Clow    const typename tuple_element<I, pair<T1, T2> >::type&
13805c8dad2SMarshall Clow    get(const pair<T1, T2>&) noexcept; // constexpr in C++14
1393e519524SHoward Hinnant
140601afb30SHoward Hinnanttemplate<size_t I, class T1, class T2>
14105c8dad2SMarshall Clow    typename tuple_element<I, pair<T1, T2> >::type&&
14205c8dad2SMarshall Clow    get(pair<T1, T2>&&) noexcept; // constexpr in C++14
143601afb30SHoward Hinnant
144545b8861SEric Fiseliertemplate<size_t I, class T1, class T2>
145545b8861SEric Fiselier    const typename tuple_element<I, pair<T1, T2> >::type&&
146545b8861SEric Fiselier    get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
147545b8861SEric Fiselier
148e99520c7SMarshall Clowtemplate<class T1, class T2>
14905c8dad2SMarshall Clow    constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
150e99520c7SMarshall Clow
151545b8861SEric Fiseliertemplate<class T1, class T2>
152bfb3f2bfSMarshall Clow    constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
153e99520c7SMarshall Clow
154545b8861SEric Fiseliertemplate<class T1, class T2>
15505c8dad2SMarshall Clow    constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
156e99520c7SMarshall Clow
157545b8861SEric Fiseliertemplate<class T1, class T2>
158545b8861SEric Fiselier    constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
159545b8861SEric Fiselier
160545b8861SEric Fiseliertemplate<class T1, class T2>
161545b8861SEric Fiselier    constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
162545b8861SEric Fiselier
163545b8861SEric Fiseliertemplate<class T1, class T2>
164545b8861SEric Fiselier    constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
165545b8861SEric Fiselier
166545b8861SEric Fiseliertemplate<class T1, class T2>
167545b8861SEric Fiselier    constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
168545b8861SEric Fiselier
169545b8861SEric Fiseliertemplate<class T1, class T2>
170545b8861SEric Fiselier    constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
171545b8861SEric Fiselier
172d5189106SMarshall Clow// C++14
173d5189106SMarshall Clow
174d5189106SMarshall Clowtemplate<class T, T... I>
175d5189106SMarshall Clowstruct integer_sequence
176d5189106SMarshall Clow{
177d5189106SMarshall Clow    typedef T value_type;
178d5189106SMarshall Clow
179d5189106SMarshall Clow    static constexpr size_t size() noexcept;
180d5189106SMarshall Clow};
181d5189106SMarshall Clow
182d5189106SMarshall Clowtemplate<size_t... I>
183d5189106SMarshall Clow  using index_sequence = integer_sequence<size_t, I...>;
184d5189106SMarshall Clow
185d5189106SMarshall Clowtemplate<class T, T N>
186d5189106SMarshall Clow  using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
187d5189106SMarshall Clowtemplate<size_t N>
188d5189106SMarshall Clow  using make_index_sequence = make_integer_sequence<size_t, N>;
189d5189106SMarshall Clow
190d5189106SMarshall Clowtemplate<class... T>
191d5189106SMarshall Clow  using index_sequence_for = make_index_sequence<sizeof...(T)>;
192d5189106SMarshall Clow
193a7b0e5ddSMarshall Clowtemplate<class T, class U=T>
1940d450aa6SJoe Loser    constexpr T exchange(T& obj, U&& new_value)
1950d450aa6SJoe Loser      noexcept(is_nothrow_move_constructible<T>::value && is_nothrow_assignable<T&, U>::value); // constexpr in C++17, noexcept in C++23
19658ad17dfSEric Fiselier
19758ad17dfSEric Fiselier// 20.2.7, in-place construction // C++17
198034555f1SEric Fiselierstruct in_place_t {
199034555f1SEric Fiselier  explicit in_place_t() = default;
200034555f1SEric Fiselier};
201034555f1SEric Fiselierinline constexpr in_place_t in_place{};
20258ad17dfSEric Fiseliertemplate <class T>
203034555f1SEric Fiselier  struct in_place_type_t {
204034555f1SEric Fiselier    explicit in_place_type_t() = default;
205034555f1SEric Fiselier  };
20658ad17dfSEric Fiseliertemplate <class T>
207034555f1SEric Fiselier  inline constexpr in_place_type_t<T> in_place_type{};
20858ad17dfSEric Fiseliertemplate <size_t I>
209034555f1SEric Fiselier  struct in_place_index_t {
210034555f1SEric Fiselier    explicit in_place_index_t() = default;
211034555f1SEric Fiselier  };
212034555f1SEric Fiseliertemplate <size_t I>
213034555f1SEric Fiselier  inline constexpr in_place_index_t<I> in_place_index{};
21458ad17dfSEric Fiselier
21543e42141SMarek Kurdej// [utility.underlying], to_underlying
21643e42141SMarek Kurdejtemplate <class T>
21743e42141SMarek Kurdej    constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
21843e42141SMarek Kurdej
2193e519524SHoward Hinnant}  // std
2203e519524SHoward Hinnant
2213e519524SHoward Hinnant*/
2223e519524SHoward Hinnant
223385cc25aSLouis Dionne#include <__assert> // all public C++ headers provide the assertion handler
2243e519524SHoward Hinnant#include <__config>
2253e519524SHoward Hinnant#include <__tuple>
22669d5a666SChristopher Di Bella#include <__utility/as_const.h>
227cb8a0b07SArthur O'Dwyer#include <__utility/auto_cast.h>
22869d5a666SChristopher Di Bella#include <__utility/cmp.h>
2296adbc83eSChristopher Di Bella#include <__utility/declval.h>
23069d5a666SChristopher Di Bella#include <__utility/exchange.h>
2316adbc83eSChristopher Di Bella#include <__utility/forward.h>
23269d5a666SChristopher Di Bella#include <__utility/in_place.h>
23369d5a666SChristopher Di Bella#include <__utility/integer_sequence.h>
2346adbc83eSChristopher Di Bella#include <__utility/move.h>
23569d5a666SChristopher Di Bella#include <__utility/pair.h>
23669d5a666SChristopher Di Bella#include <__utility/piecewise_construct.h>
237d8380ad9SArthur O'Dwyer#include <__utility/priority_tag.h>
23869d5a666SChristopher Di Bella#include <__utility/rel_ops.h>
2396adbc83eSChristopher Di Bella#include <__utility/swap.h>
240a354fd56SMark de Wever#include <__utility/to_underlying.h>
24137e6bd8bSLouis Dionne#include <__utility/transaction.h>
2422a8f9a5eSNikolas Klauser#include <__utility/unreachable.h>
2431972d1e8SNikolas Klauser#include <type_traits>
244f56972e2SMarshall Clow#include <version>
2453e519524SHoward Hinnant
246*de4a57cbSLouis Dionne#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
247*de4a57cbSLouis Dionne#  include <iosfwd>
248*de4a57cbSLouis Dionne#endif
249*de4a57cbSLouis Dionne
250db1978b6SNikolas Klauser// standard-mandated includes
251db1978b6SNikolas Klauser#include <compare>
252db1978b6SNikolas Klauser#include <initializer_list>
253db1978b6SNikolas Klauser
254413c3c4fSArthur O'Dwyer#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
255413c3c4fSArthur O'Dwyer#  pragma GCC system_header
256413c3c4fSArthur O'Dwyer#endif
257413c3c4fSArthur O'Dwyer
2583e519524SHoward Hinnant#endif // _LIBCPP_UTILITY
259