xref: /freebsd/contrib/llvm-project/libcxx/include/utility (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_UTILITY
110b57cec5SDimitry Andric#define _LIBCPP_UTILITY
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    utility synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric#include <initializer_list>
170b57cec5SDimitry Andric
180b57cec5SDimitry Andricnamespace std
190b57cec5SDimitry Andric{
200b57cec5SDimitry Andric
210b57cec5SDimitry Andrictemplate <class T>
220b57cec5SDimitry Andric    void
230b57cec5SDimitry Andric    swap(T& a, T& b);
240b57cec5SDimitry Andric
250b57cec5SDimitry Andricnamespace rel_ops
260b57cec5SDimitry Andric{
270b57cec5SDimitry Andric    template<class T> bool operator!=(const T&, const T&);
280b57cec5SDimitry Andric    template<class T> bool operator> (const T&, const T&);
290b57cec5SDimitry Andric    template<class T> bool operator<=(const T&, const T&);
300b57cec5SDimitry Andric    template<class T> bool operator>=(const T&, const T&);
310b57cec5SDimitry Andric}
320b57cec5SDimitry Andric
330b57cec5SDimitry Andrictemplate<class T>
340b57cec5SDimitry Andricvoid
350b57cec5SDimitry Andricswap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
360b57cec5SDimitry Andric                          is_nothrow_move_assignable<T>::value);
370b57cec5SDimitry Andric
380b57cec5SDimitry Andrictemplate <class T, size_t N>
390b57cec5SDimitry Andricvoid
400b57cec5SDimitry Andricswap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
410b57cec5SDimitry Andric
420b57cec5SDimitry Andrictemplate <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;  // constexpr in C++14
430b57cec5SDimitry Andrictemplate <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14
440b57cec5SDimitry Andric
45bdd1243dSDimitry Andrictemplate <typename T>
46bdd1243dSDimitry Andric[[nodiscard]] constexpr
47bdd1243dSDimitry Andricauto forward_like(auto&& x) noexcept -> see below;                               // since C++23
48bdd1243dSDimitry Andric
490b57cec5SDimitry Andrictemplate <class T> typename remove_reference<T>::type&& move(T&&) noexcept;      // constexpr in C++14
500b57cec5SDimitry Andric
510b57cec5SDimitry Andrictemplate <class T>
520b57cec5SDimitry Andric    typename conditional
530b57cec5SDimitry Andric    <
540b57cec5SDimitry Andric        !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
550b57cec5SDimitry Andric        const T&,
560b57cec5SDimitry Andric        T&&
570b57cec5SDimitry Andric    >::type
580b57cec5SDimitry Andric    move_if_noexcept(T& x) noexcept; // constexpr in C++14
590b57cec5SDimitry Andric
600b57cec5SDimitry Andrictemplate <class T> constexpr add_const_t<T>& as_const(T& t) noexcept;      // C++17
610b57cec5SDimitry Andrictemplate <class T>                      void as_const(const T&&) = delete; // C++17
620b57cec5SDimitry Andric
630b57cec5SDimitry Andrictemplate <class T> typename add_rvalue_reference<T>::type declval() noexcept;
640b57cec5SDimitry Andric
65fe6060f1SDimitry Andrictemplate<class T, class U> constexpr bool cmp_equal(T t, U u) noexcept;         // C++20
66fe6060f1SDimitry Andrictemplate<class T, class U> constexpr bool cmp_not_equal(T t, U u) noexcept;     // C++20
67fe6060f1SDimitry Andrictemplate<class T, class U> constexpr bool cmp_less(T t, U u) noexcept;          // C++20
68fe6060f1SDimitry Andrictemplate<class T, class U> constexpr bool cmp_greater(T t, U u) noexcept;       // C++20
69fe6060f1SDimitry Andrictemplate<class T, class U> constexpr bool cmp_less_equal(T t, U u) noexcept;    // C++20
70fe6060f1SDimitry Andrictemplate<class T, class U> constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20
71fe6060f1SDimitry Andrictemplate<class R, class T> constexpr bool in_range(T t) noexcept;               // C++20
72fe6060f1SDimitry Andric
730b57cec5SDimitry Andrictemplate <class T1, class T2>
740b57cec5SDimitry Andricstruct pair
750b57cec5SDimitry Andric{
760b57cec5SDimitry Andric    typedef T1 first_type;
770b57cec5SDimitry Andric    typedef T2 second_type;
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric    T1 first;
800b57cec5SDimitry Andric    T2 second;
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric    pair(const pair&) = default;
830b57cec5SDimitry Andric    pair(pair&&) = default;
84e40139ffSDimitry Andric    explicit(see-below) constexpr pair();
85e40139ffSDimitry Andric    explicit(see-below) pair(const T1& x, const T2& y);                          // constexpr in C++14
86349cc55cSDimitry Andric    template <class U = T1, class V = T2> explicit(see-below) pair(U&&, V&&);    // constexpr in C++14
87*06c3fb27SDimitry Andric    template <class U, class V> constexpr explicit(see-below) pair(pair<U, V>&); // since C++23
88e40139ffSDimitry Andric    template <class U, class V> explicit(see-below) pair(const pair<U, V>& p);   // constexpr in C++14
89e40139ffSDimitry Andric    template <class U, class V> explicit(see-below) pair(pair<U, V>&& p);        // constexpr in C++14
90bdd1243dSDimitry Andric    template <class U, class V>
91*06c3fb27SDimitry Andric    constexpr explicit(see-below) pair(const pair<U, V>&&);                      // since C++23
92*06c3fb27SDimitry Andric    template <pair-like P> constexpr explicit(see-below) pair(P&&);              // since C++23
930b57cec5SDimitry Andric    template <class... Args1, class... Args2>
94*06c3fb27SDimitry Andric        pair(piecewise_construct_t, tuple<Args1...> first_args,                  // constexpr in C++20
95*06c3fb27SDimitry Andric                                    tuple<Args2...> second_args);
960b57cec5SDimitry Andric
97bdd1243dSDimitry Andric    constexpr const pair& operator=(const pair& p) const;                        // since C++23
98fe6060f1SDimitry Andric    template <class U, class V> pair& operator=(const pair<U, V>& p);            // constexpr in C++20
99bdd1243dSDimitry Andric    template <class U, class V>
100bdd1243dSDimitry Andric    constexpr const pair& operator=(const pair<U, V>& p) const;                  // since C++23
1010b57cec5SDimitry Andric    pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
102fe6060f1SDimitry Andric                                       is_nothrow_move_assignable<T2>::value);   // constexpr in C++20
103bdd1243dSDimitry Andric    constexpr const pair& operator=(pair&& p) const;                             // since C++23
104fe6060f1SDimitry Andric    template <class U, class V> pair& operator=(pair<U, V>&& p);                 // constexpr in C++20
105bdd1243dSDimitry Andric    template <class U, class V>
106bdd1243dSDimitry Andric    constexpr const pair& operator=(pair<U, V>&& p) const;                       // since C++23
107*06c3fb27SDimitry Andric    template <pair-like P> constexpr pair& operator=(P&&);                       // since C++23
108*06c3fb27SDimitry Andric    template <pair-like P> constexpr const pair& operator=(P&&) const;           // since C++23
1090b57cec5SDimitry Andric
1100b57cec5SDimitry Andric    void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
111fe6060f1SDimitry Andric                                is_nothrow_swappable_v<T2>);                     // constexpr in C++20
112bdd1243dSDimitry Andric    constexpr void swap(const pair& p) const noexcept(see below);                // since C++23
1130b57cec5SDimitry Andric};
1140b57cec5SDimitry Andric
11581ad6265SDimitry Andrictemplate<class T1, class T2, class U1, class U2, template<class> class TQual, template<class> class UQual>
11681ad6265SDimitry Andricstruct basic_common_reference<pair<T1, T2>, pair<U1, U2>, TQual, UQual>;         // since C++23
11781ad6265SDimitry Andric
11881ad6265SDimitry Andrictemplate<class T1, class T2, class U1, class U2>
11981ad6265SDimitry Andricstruct common_type<pair<T1, T2>, pair<U1, U2>>;                                  // since C++23
12081ad6265SDimitry Andric
121349cc55cSDimitry Andrictemplate<class T1, class T2> pair(T1, T2) -> pair<T1, T2>;
122349cc55cSDimitry Andric
123*06c3fb27SDimitry Andrictemplate <class T1, class T2, class U1, class U2>
124*06c3fb27SDimitry Andricbool operator==(const pair<T1,T2>&, const pair<U1,U2>&);                         // constexpr in C++14
125*06c3fb27SDimitry Andrictemplate <class T1, class T2, class U1, class U2>
126*06c3fb27SDimitry Andricbool operator!=(const pair<T1,T2>&, const pair<U1,U2>&);                         // constexpr in C++14, removed in C++20
127*06c3fb27SDimitry Andrictemplate <class T1, class T2, class U1, class U2>
128*06c3fb27SDimitry Andricbool operator< (const pair<T1,T2>&, const pair<U1,U2>&);                         // constexpr in C++14, removed in C++20
129*06c3fb27SDimitry Andrictemplate <class T1, class T2, class U1, class U2>
130*06c3fb27SDimitry Andricbool operator> (const pair<T1,T2>&, const pair<U1,U2>&);                         // constexpr in C++14, removed in C++20
131*06c3fb27SDimitry Andrictemplate <class T1, class T2, class U1, class U2>
132*06c3fb27SDimitry Andricbool operator>=(const pair<T1,T2>&, const pair<U1,U2>&);                         // constexpr in C++14, removed in C++20
133*06c3fb27SDimitry Andrictemplate <class T1, class T2, class U1, class U2>
134*06c3fb27SDimitry Andricbool operator<=(const pair<T1,T2>&, const pair<U1,U2>&);                         // constexpr in C++14, removed in C++20
135*06c3fb27SDimitry Andrictemplate <class T1, class T2, class U1, class U2>
136*06c3fb27SDimitry Andric  constexpr common_comparison_type_t<synth-three-way-result<T1,U1>,
137*06c3fb27SDimitry Andric                                     synth-three-way-result<T2,U2>>
138*06c3fb27SDimitry Andric    operator<=>(const pair<T1,T2>&, const pair<U1,U2>&);                         // C++20
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andrictemplate <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&);                // constexpr in C++14
1410b57cec5SDimitry Andrictemplate <class T1, class T2>
1420b57cec5SDimitry Andricvoid
143fe6060f1SDimitry Andricswap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));            // constexpr in C++20
1440b57cec5SDimitry Andric
145*06c3fb27SDimitry Andrictemplate<class T1, class T2>                                                     // since C++23
146*06c3fb27SDimitry Andricconstexpr void swap(const pair<T1, T2>& x, const pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));
147bdd1243dSDimitry Andric
148e40139ffSDimitry Andricstruct piecewise_construct_t { explicit piecewise_construct_t() = default; };
1490b57cec5SDimitry Andricinline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andrictemplate <class T> struct tuple_size;
1520b57cec5SDimitry Andrictemplate <size_t I, class T> struct tuple_element;
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andrictemplate <class T1, class T2> struct tuple_size<pair<T1, T2> >;
1550b57cec5SDimitry Andrictemplate <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
1560b57cec5SDimitry Andrictemplate <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andrictemplate<size_t I, class T1, class T2>
1590b57cec5SDimitry Andric    typename tuple_element<I, pair<T1, T2> >::type&
1600b57cec5SDimitry Andric    get(pair<T1, T2>&) noexcept; // constexpr in C++14
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andrictemplate<size_t I, class T1, class T2>
1630b57cec5SDimitry Andric    const typename tuple_element<I, pair<T1, T2> >::type&
1640b57cec5SDimitry Andric    get(const pair<T1, T2>&) noexcept; // constexpr in C++14
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andrictemplate<size_t I, class T1, class T2>
1670b57cec5SDimitry Andric    typename tuple_element<I, pair<T1, T2> >::type&&
1680b57cec5SDimitry Andric    get(pair<T1, T2>&&) noexcept; // constexpr in C++14
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andrictemplate<size_t I, class T1, class T2>
1710b57cec5SDimitry Andric    const typename tuple_element<I, pair<T1, T2> >::type&&
1720b57cec5SDimitry Andric    get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andrictemplate<class T1, class T2>
1750b57cec5SDimitry Andric    constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andrictemplate<class T1, class T2>
1780b57cec5SDimitry Andric    constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andrictemplate<class T1, class T2>
1810b57cec5SDimitry Andric    constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andrictemplate<class T1, class T2>
1840b57cec5SDimitry Andric    constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andrictemplate<class T1, class T2>
1870b57cec5SDimitry Andric    constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andrictemplate<class T1, class T2>
1900b57cec5SDimitry Andric    constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
1910b57cec5SDimitry Andric
1920b57cec5SDimitry Andrictemplate<class T1, class T2>
1930b57cec5SDimitry Andric    constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andrictemplate<class T1, class T2>
1960b57cec5SDimitry Andric    constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric// C++14
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andrictemplate<class T, T... I>
2010b57cec5SDimitry Andricstruct integer_sequence
2020b57cec5SDimitry Andric{
2030b57cec5SDimitry Andric    typedef T value_type;
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric    static constexpr size_t size() noexcept;
2060b57cec5SDimitry Andric};
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andrictemplate<size_t... I>
2090b57cec5SDimitry Andric  using index_sequence = integer_sequence<size_t, I...>;
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andrictemplate<class T, T N>
2120b57cec5SDimitry Andric  using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
2130b57cec5SDimitry Andrictemplate<size_t N>
2140b57cec5SDimitry Andric  using make_index_sequence = make_integer_sequence<size_t, N>;
2150b57cec5SDimitry Andric
2160b57cec5SDimitry Andrictemplate<class... T>
2170b57cec5SDimitry Andric  using index_sequence_for = make_index_sequence<sizeof...(T)>;
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andrictemplate<class T, class U=T>
220*06c3fb27SDimitry Andric    constexpr T exchange(T& obj, U&& new_value)                                 // constexpr in C++17, noexcept in C++23
221*06c3fb27SDimitry Andric      noexcept(is_nothrow_move_constructible<T>::value && is_nothrow_assignable<T&, U>::value);
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andric// 20.2.7, in-place construction // C++17
2240b57cec5SDimitry Andricstruct in_place_t {
2250b57cec5SDimitry Andric  explicit in_place_t() = default;
2260b57cec5SDimitry Andric};
2270b57cec5SDimitry Andricinline constexpr in_place_t in_place{};
2280b57cec5SDimitry Andrictemplate <class T>
2290b57cec5SDimitry Andric  struct in_place_type_t {
2300b57cec5SDimitry Andric    explicit in_place_type_t() = default;
2310b57cec5SDimitry Andric  };
2320b57cec5SDimitry Andrictemplate <class T>
2330b57cec5SDimitry Andric  inline constexpr in_place_type_t<T> in_place_type{};
2340b57cec5SDimitry Andrictemplate <size_t I>
2350b57cec5SDimitry Andric  struct in_place_index_t {
2360b57cec5SDimitry Andric    explicit in_place_index_t() = default;
2370b57cec5SDimitry Andric  };
2380b57cec5SDimitry Andrictemplate <size_t I>
2390b57cec5SDimitry Andric  inline constexpr in_place_index_t<I> in_place_index{};
2400b57cec5SDimitry Andric
241fe6060f1SDimitry Andric// [utility.underlying], to_underlying
242fe6060f1SDimitry Andrictemplate <class T>
243*06c3fb27SDimitry Andric    constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++23
244fe6060f1SDimitry Andric
2450b57cec5SDimitry Andric}  // std
2460b57cec5SDimitry Andric
2470b57cec5SDimitry Andric*/
2480b57cec5SDimitry Andric
24981ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
2500b57cec5SDimitry Andric#include <__config>
251fe6060f1SDimitry Andric#include <__utility/as_const.h>
2520eae32dcSDimitry Andric#include <__utility/auto_cast.h>
253fe6060f1SDimitry Andric#include <__utility/cmp.h>
254fe6060f1SDimitry Andric#include <__utility/declval.h>
255bdd1243dSDimitry Andric#include <__utility/exception_guard.h>
256fe6060f1SDimitry Andric#include <__utility/exchange.h>
257fe6060f1SDimitry Andric#include <__utility/forward.h>
258bdd1243dSDimitry Andric#include <__utility/forward_like.h>
259fe6060f1SDimitry Andric#include <__utility/in_place.h>
260fe6060f1SDimitry Andric#include <__utility/integer_sequence.h>
261fe6060f1SDimitry Andric#include <__utility/move.h>
262fe6060f1SDimitry Andric#include <__utility/pair.h>
263fe6060f1SDimitry Andric#include <__utility/piecewise_construct.h>
2644824e7fdSDimitry Andric#include <__utility/priority_tag.h>
265fe6060f1SDimitry Andric#include <__utility/rel_ops.h>
266fe6060f1SDimitry Andric#include <__utility/swap.h>
267fe6060f1SDimitry Andric#include <__utility/to_underlying.h>
26881ad6265SDimitry Andric#include <__utility/unreachable.h>
26981ad6265SDimitry Andric#include <version>
27081ad6265SDimitry Andric
27181ad6265SDimitry Andric// standard-mandated includes
272bdd1243dSDimitry Andric
273bdd1243dSDimitry Andric// [utility.syn]
274fe6060f1SDimitry Andric#include <compare>
275fe6060f1SDimitry Andric#include <initializer_list>
2760b57cec5SDimitry Andric
277bdd1243dSDimitry Andric// [tuple.helper]
278*06c3fb27SDimitry Andric#include <__tuple/tuple_element.h>
279*06c3fb27SDimitry Andric#include <__tuple/tuple_size.h>
280bdd1243dSDimitry Andric
2810eae32dcSDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2820eae32dcSDimitry Andric#  pragma GCC system_header
2830eae32dcSDimitry Andric#endif
2840eae32dcSDimitry Andric
285bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
286bdd1243dSDimitry Andric#  include <cstdlib>
287bdd1243dSDimitry Andric#  include <iosfwd>
288bdd1243dSDimitry Andric#  include <type_traits>
289bdd1243dSDimitry Andric#endif
290bdd1243dSDimitry Andric
2910b57cec5SDimitry Andric#endif // _LIBCPP_UTILITY
292