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