xref: /freebsd/contrib/llvm-project/libcxx/include/utility (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
121template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
122template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
123template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
124template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
125template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
126template <class T1, class T2>
127  constexpr common_comparison_type_t<synth-three-way-result<T1>,
128                                     synth-three-way-result<T2>>
129    operator<=>(const pair<T1,T2>&, const pair<T1,T2>&);                               // C++20
130
131template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&);                // constexpr in C++14
132template <class T1, class T2>
133void
134swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));            // constexpr in C++20
135
136template<class T1, class T2>
137constexpr void swap(const pair<T1, T2>& x, const pair<T1, T2>& y) noexcept(noexcept(x.swap(y)));    // since C++23
138
139struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
140inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
141
142template <class T> struct tuple_size;
143template <size_t I, class T> struct tuple_element;
144
145template <class T1, class T2> struct tuple_size<pair<T1, T2> >;
146template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
147template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
148
149template<size_t I, class T1, class T2>
150    typename tuple_element<I, pair<T1, T2> >::type&
151    get(pair<T1, T2>&) noexcept; // constexpr in C++14
152
153template<size_t I, class T1, class T2>
154    const typename tuple_element<I, pair<T1, T2> >::type&
155    get(const pair<T1, T2>&) noexcept; // constexpr in C++14
156
157template<size_t I, class T1, class T2>
158    typename tuple_element<I, pair<T1, T2> >::type&&
159    get(pair<T1, T2>&&) noexcept; // constexpr in C++14
160
161template<size_t I, class T1, class T2>
162    const typename tuple_element<I, pair<T1, T2> >::type&&
163    get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
164
165template<class T1, class T2>
166    constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
167
168template<class T1, class T2>
169    constexpr const T1& get(const pair<T1, T2>&) noexcept; // 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<T2, T1>&) noexcept; // C++14
179
180template<class T1, class T2>
181    constexpr const T1& get(const pair<T2, T1>&) 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
189// C++14
190
191template<class T, T... I>
192struct integer_sequence
193{
194    typedef T value_type;
195
196    static constexpr size_t size() noexcept;
197};
198
199template<size_t... I>
200  using index_sequence = integer_sequence<size_t, I...>;
201
202template<class T, T N>
203  using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
204template<size_t N>
205  using make_index_sequence = make_integer_sequence<size_t, N>;
206
207template<class... T>
208  using index_sequence_for = make_index_sequence<sizeof...(T)>;
209
210template<class T, class U=T>
211    constexpr T exchange(T& obj, U&& new_value)
212      noexcept(is_nothrow_move_constructible<T>::value && is_nothrow_assignable<T&, U>::value); // constexpr in C++17, noexcept in C++23
213
214// 20.2.7, in-place construction // C++17
215struct in_place_t {
216  explicit in_place_t() = default;
217};
218inline constexpr in_place_t in_place{};
219template <class T>
220  struct in_place_type_t {
221    explicit in_place_type_t() = default;
222  };
223template <class T>
224  inline constexpr in_place_type_t<T> in_place_type{};
225template <size_t I>
226  struct in_place_index_t {
227    explicit in_place_index_t() = default;
228  };
229template <size_t I>
230  inline constexpr in_place_index_t<I> in_place_index{};
231
232// [utility.underlying], to_underlying
233template <class T>
234    constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
235
236}  // std
237
238*/
239
240#include <__assert> // all public C++ headers provide the assertion handler
241#include <__config>
242#include <__utility/as_const.h>
243#include <__utility/auto_cast.h>
244#include <__utility/cmp.h>
245#include <__utility/declval.h>
246#include <__utility/exception_guard.h>
247#include <__utility/exchange.h>
248#include <__utility/forward.h>
249#include <__utility/forward_like.h>
250#include <__utility/in_place.h>
251#include <__utility/integer_sequence.h>
252#include <__utility/move.h>
253#include <__utility/pair.h>
254#include <__utility/piecewise_construct.h>
255#include <__utility/priority_tag.h>
256#include <__utility/rel_ops.h>
257#include <__utility/swap.h>
258#include <__utility/to_underlying.h>
259#include <__utility/unreachable.h>
260#include <version>
261
262// standard-mandated includes
263
264// [utility.syn]
265#include <compare>
266#include <initializer_list>
267
268// [tuple.helper]
269#include <__tuple_dir/tuple_element.h>
270#include <__tuple_dir/tuple_size.h>
271
272#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
273#  pragma GCC system_header
274#endif
275
276#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
277#  include <cstdlib>
278#  include <iosfwd>
279#  include <type_traits>
280#endif
281
282#endif // _LIBCPP_UTILITY
283