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