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_FUNCTIONAL
11#define _LIBCPP_FUNCTIONAL
12
13/*
14    functional synopsis
15
16namespace std
17{
18
19template <class Arg, class Result>
20struct unary_function
21{
22    typedef Arg    argument_type;
23    typedef Result result_type;
24};
25
26template <class Arg1, class Arg2, class Result>
27struct binary_function
28{
29    typedef Arg1   first_argument_type;
30    typedef Arg2   second_argument_type;
31    typedef Result result_type;
32};
33
34template <class T>
35class reference_wrapper
36    : public unary_function<T1, R> // if wrapping a unary functor
37    : public binary_function<T1, T2, R> // if wraping a binary functor
38{
39public:
40    // types
41    typedef T type;
42    typedef see below result_type; // Not always defined
43
44    // construct/copy/destroy
45    template<class U>
46      reference_wrapper(U&&);
47    reference_wrapper(const reference_wrapper<T>& x) noexcept;
48
49    // assignment
50    reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
51
52    // access
53    operator T& () const noexcept;
54    T& get() const noexcept;
55
56    // invoke
57    template <class... ArgTypes>
58      typename result_of<T&(ArgTypes&&...)>::type
59          operator() (ArgTypes&&...) const;
60};
61
62template <class T>
63  reference_wrapper(T&) -> reference_wrapper<T>;
64
65template <class T> reference_wrapper<T> ref(T& t) noexcept;
66template <class T> void ref(const T&& t) = delete;
67template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
68
69template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
70template <class T> void cref(const T&& t) = delete;
71template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
72
73template <class T> struct unwrap_reference;                                       // since C++20
74template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { };    // since C++20
75template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
76template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
77
78template <class T> // <class T=void> in C++14
79struct plus {
80    T operator()(const T& x, const T& y) const;
81};
82
83template <class T> // <class T=void> in C++14
84struct minus {
85    T operator()(const T& x, const T& y) const;
86};
87
88template <class T> // <class T=void> in C++14
89struct multiplies {
90    T operator()(const T& x, const T& y) const;
91};
92
93template <class T> // <class T=void> in C++14
94struct divides {
95    T operator()(const T& x, const T& y) const;
96};
97
98template <class T> // <class T=void> in C++14
99struct modulus {
100    T operator()(const T& x, const T& y) const;
101};
102
103template <class T> // <class T=void> in C++14
104struct negate {
105    T operator()(const T& x) const;
106};
107
108template <class T> // <class T=void> in C++14
109struct equal_to {
110    bool operator()(const T& x, const T& y) const;
111};
112
113template <class T> // <class T=void> in C++14
114struct not_equal_to {
115    bool operator()(const T& x, const T& y) const;
116};
117
118template <class T> // <class T=void> in C++14
119struct greater {
120    bool operator()(const T& x, const T& y) const;
121};
122
123template <class T> // <class T=void> in C++14
124struct less {
125    bool operator()(const T& x, const T& y) const;
126};
127
128template <class T> // <class T=void> in C++14
129struct greater_equal {
130    bool operator()(const T& x, const T& y) const;
131};
132
133template <class T> // <class T=void> in C++14
134struct less_equal {
135    bool operator()(const T& x, const T& y) const;
136};
137
138// [comparisons.three.way], class compare_three_way
139struct compare_three_way;
140
141template <class T> // <class T=void> in C++14
142struct logical_and {
143    bool operator()(const T& x, const T& y) const;
144};
145
146template <class T> // <class T=void> in C++14
147struct logical_or {
148    bool operator()(const T& x, const T& y) const;
149};
150
151template <class T> // <class T=void> in C++14
152struct logical_not {
153    bool operator()(const T& x) const;
154};
155
156template <class T> // <class T=void> in C++14
157struct bit_and {
158    T operator()(const T& x, const T& y) const;
159};
160
161template <class T> // <class T=void> in C++14
162struct bit_or {
163    T operator()(const T& x, const T& y) const;
164};
165
166template <class T> // <class T=void> in C++14
167struct bit_xor {
168    T operator()(const T& x, const T& y) const;
169};
170
171template <class T=void> // C++14
172struct bit_not {
173    T operator()(const T& x) const;
174};
175
176struct identity; // C++20
177
178template <class Predicate>
179class unary_negate // deprecated in C++17, removed in C++20
180    : public unary_function<typename Predicate::argument_type, bool>
181{
182public:
183    explicit unary_negate(const Predicate& pred);
184    bool operator()(const typename Predicate::argument_type& x) const;
185};
186
187template <class Predicate> // deprecated in C++17, removed in C++20
188unary_negate<Predicate> not1(const Predicate& pred);
189
190template <class Predicate>
191class binary_negate // deprecated in C++17, removed in C++20
192    : public binary_function<typename Predicate::first_argument_type,
193                             typename Predicate::second_argument_type,
194                             bool>
195{
196public:
197    explicit binary_negate(const Predicate& pred);
198    bool operator()(const typename Predicate::first_argument_type& x,
199                    const typename Predicate::second_argument_type& y) const;
200};
201
202template <class Predicate> // deprecated in C++17, removed in C++20
203binary_negate<Predicate> not2(const Predicate& pred);
204
205template <class F>
206constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
207
208template<class T> struct is_bind_expression;
209template<class T> struct is_placeholder;
210
211    // See C++14 20.9.9, Function object binders
212template <class T> inline constexpr bool is_bind_expression_v
213  = is_bind_expression<T>::value; // C++17
214template <class T> inline constexpr int is_placeholder_v
215  = is_placeholder<T>::value; // C++17
216
217
218template<class Fn, class... BoundArgs>
219  constexpr unspecified bind(Fn&&, BoundArgs&&...);  // constexpr in C++20
220template<class R, class Fn, class... BoundArgs>
221  constexpr unspecified bind(Fn&&, BoundArgs&&...);  // constexpr in C++20
222
223template<class F, class... Args>
224 constexpr // constexpr in C++20
225 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
226    noexcept(is_nothrow_invocable_v<F, Args...>);
227
228namespace placeholders {
229  // M is the implementation-defined number of placeholders
230  extern unspecified _1;
231  extern unspecified _2;
232  .
233  .
234  .
235  extern unspecified _Mp;
236}
237
238template <class Operation>
239class binder1st     // deprecated in C++11, removed in C++17
240    : public unary_function<typename Operation::second_argument_type,
241                            typename Operation::result_type>
242{
243protected:
244    Operation                               op;
245    typename Operation::first_argument_type value;
246public:
247    binder1st(const Operation& x, const typename Operation::first_argument_type y);
248    typename Operation::result_type operator()(      typename Operation::second_argument_type& x) const;
249    typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
250};
251
252template <class Operation, class T>
253binder1st<Operation> bind1st(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
254
255template <class Operation>
256class binder2nd     // deprecated in C++11, removed in C++17
257    : public unary_function<typename Operation::first_argument_type,
258                            typename Operation::result_type>
259{
260protected:
261    Operation                                op;
262    typename Operation::second_argument_type value;
263public:
264    binder2nd(const Operation& x, const typename Operation::second_argument_type y);
265    typename Operation::result_type operator()(      typename Operation::first_argument_type& x) const;
266    typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
267};
268
269template <class Operation, class T>
270binder2nd<Operation> bind2nd(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
271
272template <class Arg, class Result>      // deprecated in C++11, removed in C++17
273class pointer_to_unary_function : public unary_function<Arg, Result>
274{
275public:
276    explicit pointer_to_unary_function(Result (*f)(Arg));
277    Result operator()(Arg x) const;
278};
279
280template <class Arg, class Result>
281pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg));      // deprecated in C++11, removed in C++17
282
283template <class Arg1, class Arg2, class Result>      // deprecated in C++11, removed in C++17
284class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
285{
286public:
287    explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
288    Result operator()(Arg1 x, Arg2 y) const;
289};
290
291template <class Arg1, class Arg2, class Result>
292pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2));      // deprecated in C++11, removed in C++17
293
294template<class S, class T>      // deprecated in C++11, removed in C++17
295class mem_fun_t : public unary_function<T*, S>
296{
297public:
298    explicit mem_fun_t(S (T::*p)());
299    S operator()(T* p) const;
300};
301
302template<class S, class T, class A>
303class mem_fun1_t : public binary_function<T*, A, S>      // deprecated in C++11, removed in C++17
304{
305public:
306    explicit mem_fun1_t(S (T::*p)(A));
307    S operator()(T* p, A x) const;
308};
309
310template<class S, class T>          mem_fun_t<S,T>    mem_fun(S (T::*f)());      // deprecated in C++11, removed in C++17
311template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));     // deprecated in C++11, removed in C++17
312
313template<class S, class T>
314class mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
315{
316public:
317    explicit mem_fun_ref_t(S (T::*p)());
318    S operator()(T& p) const;
319};
320
321template<class S, class T, class A>
322class mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
323{
324public:
325    explicit mem_fun1_ref_t(S (T::*p)(A));
326    S operator()(T& p, A x) const;
327};
328
329template<class S, class T>          mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)());      // deprecated in C++11, removed in C++17
330template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A));     // deprecated in C++11, removed in C++17
331
332template <class S, class T>
333class const_mem_fun_t : public unary_function<const T*, S>      // deprecated in C++11, removed in C++17
334{
335public:
336    explicit const_mem_fun_t(S (T::*p)() const);
337    S operator()(const T* p) const;
338};
339
340template <class S, class T, class A>
341class const_mem_fun1_t : public binary_function<const T*, A, S>      // deprecated in C++11, removed in C++17
342{
343public:
344    explicit const_mem_fun1_t(S (T::*p)(A) const);
345    S operator()(const T* p, A x) const;
346};
347
348template <class S, class T>          const_mem_fun_t<S,T>    mem_fun(S (T::*f)() const);      // deprecated in C++11, removed in C++17
349template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const);     // deprecated in C++11, removed in C++17
350
351template <class S, class T>
352class const_mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
353{
354public:
355    explicit const_mem_fun_ref_t(S (T::*p)() const);
356    S operator()(const T& p) const;
357};
358
359template <class S, class T, class A>
360class const_mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
361{
362public:
363    explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
364    S operator()(const T& p, A x) const;
365};
366
367template <class S, class T>          const_mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)() const);   // deprecated in C++11, removed in C++17
368template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const);  // deprecated in C++11, removed in C++17
369
370template<class R, class T>
371constexpr unspecified mem_fn(R T::*); // constexpr in C++20
372
373class bad_function_call
374    : public exception
375{
376};
377
378template<class> class function; // undefined
379
380template<class R, class... ArgTypes>
381class function<R(ArgTypes...)>
382  : public unary_function<T1, R>      // iff sizeof...(ArgTypes) == 1 and
383                                      // ArgTypes contains T1
384  : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
385                                      // ArgTypes contains T1 and T2
386{
387public:
388    typedef R result_type;
389
390    // construct/copy/destroy:
391    function() noexcept;
392    function(nullptr_t) noexcept;
393    function(const function&);
394    function(function&&) noexcept;
395    template<class F>
396      function(F);
397    template<Allocator Alloc>
398      function(allocator_arg_t, const Alloc&) noexcept;            // removed in C++17
399    template<Allocator Alloc>
400      function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
401    template<Allocator Alloc>
402      function(allocator_arg_t, const Alloc&, const function&);    // removed in C++17
403    template<Allocator Alloc>
404      function(allocator_arg_t, const Alloc&, function&&);         // removed in C++17
405    template<class F, Allocator Alloc>
406      function(allocator_arg_t, const Alloc&, F);                  // removed in C++17
407
408    function& operator=(const function&);
409    function& operator=(function&&) noexcept;
410    function& operator=(nullptr_t) noexcept;
411    template<class F>
412      function& operator=(F&&);
413    template<class F>
414      function& operator=(reference_wrapper<F>) noexcept;
415
416    ~function();
417
418    // function modifiers:
419    void swap(function&) noexcept;
420    template<class F, class Alloc>
421      void assign(F&&, const Alloc&);                 // Removed in C++17
422
423    // function capacity:
424    explicit operator bool() const noexcept;
425
426    // function invocation:
427    R operator()(ArgTypes...) const;
428
429    // function target access:
430    const std::type_info& target_type() const noexcept;
431    template <typename T>       T* target() noexcept;
432    template <typename T> const T* target() const noexcept;
433};
434
435// Deduction guides
436template<class R, class ...Args>
437function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
438
439template<class F>
440function(F) -> function<see-below>; // since C++17
441
442// Null pointer comparisons:
443template <class R, class ... ArgTypes>
444  bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
445
446template <class R, class ... ArgTypes>
447  bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
448
449template <class R, class ... ArgTypes>
450  bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
451
452template <class  R, class ... ArgTypes>
453  bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
454
455// specialized algorithms:
456template <class  R, class ... ArgTypes>
457  void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
458
459template <class T> struct hash;
460
461template <> struct hash<bool>;
462template <> struct hash<char>;
463template <> struct hash<signed char>;
464template <> struct hash<unsigned char>;
465template <> struct hash<char8_t>; // since C++20
466template <> struct hash<char16_t>;
467template <> struct hash<char32_t>;
468template <> struct hash<wchar_t>;
469template <> struct hash<short>;
470template <> struct hash<unsigned short>;
471template <> struct hash<int>;
472template <> struct hash<unsigned int>;
473template <> struct hash<long>;
474template <> struct hash<long long>;
475template <> struct hash<unsigned long>;
476template <> struct hash<unsigned long long>;
477
478template <> struct hash<float>;
479template <> struct hash<double>;
480template <> struct hash<long double>;
481
482template<class T> struct hash<T*>;
483template <> struct hash<nullptr_t>;  // C++17
484
485namespace ranges {
486  // [range.cmp], concept-constrained comparisons
487  struct equal_to;
488  struct not_equal_to;
489  struct greater;
490  struct less;
491  struct greater_equal;
492  struct less_equal;
493}
494
495}  // std
496
497POLICY:  For non-variadic implementations, the number of arguments is limited
498         to 3.  It is hoped that the need for non-variadic implementations
499         will be minimal.
500
501*/
502
503#include <__algorithm/search.h>
504#include <__assert> // all public C++ headers provide the assertion handler
505#include <__compare/compare_three_way.h>
506#include <__config>
507#include <__debug>
508#include <__functional/binary_function.h> // TODO: deprecate
509#include <__functional/binary_negate.h>
510#include <__functional/bind.h>
511#include <__functional/bind_back.h>
512#include <__functional/bind_front.h>
513#include <__functional/binder1st.h>
514#include <__functional/binder2nd.h>
515#include <__functional/boyer_moore_searcher.h>
516#include <__functional/compose.h>
517#include <__functional/default_searcher.h>
518#include <__functional/function.h>
519#include <__functional/hash.h>
520#include <__functional/identity.h>
521#include <__functional/invoke.h>
522#include <__functional/mem_fn.h> // TODO: deprecate
523#include <__functional/mem_fun_ref.h>
524#include <__functional/not_fn.h>
525#include <__functional/operations.h>
526#include <__functional/pointer_to_binary_function.h>
527#include <__functional/pointer_to_unary_function.h>
528#include <__functional/ranges_operations.h>
529#include <__functional/reference_wrapper.h>
530#include <__functional/unary_function.h> // TODO: deprecate
531#include <__functional/unary_negate.h>
532#include <__functional/unwrap_ref.h>
533#include <__utility/forward.h>
534#include <concepts>
535#include <exception>
536#include <memory>
537#include <tuple>
538#include <type_traits>
539#include <typeinfo>
540#include <version>
541
542#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
543#  include <utility>
544#endif
545
546#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
547#  pragma GCC system_header
548#endif
549
550#endif // _LIBCPP_FUNCTIONAL
551