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<auto f, class... Args>
225 constexpr unspecified bind_front(Args&&...); // C++26
226template<class F, class... Args>
227 constexpr unspecified bind_back(F&&, Args&&...); // C++23
228
229template<class T> struct is_bind_expression;
230template<class T> struct is_placeholder;
231
232 // See C++14 20.9.9, Function object binders
233template <class T> inline constexpr bool is_bind_expression_v
234 = is_bind_expression<T>::value; // C++17
235template <class T> inline constexpr int is_placeholder_v
236 = is_placeholder<T>::value; // C++17
237
238
239template<class Fn, class... BoundArgs>
240 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
241template<class R, class Fn, class... BoundArgs>
242 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
243
244// [func.invoke]
245template<class F, class... Args>
246 constexpr // constexpr in C++20
247 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
248 noexcept(is_nothrow_invocable_v<F, Args...>);
249
250template<class R, class F, class... Args>
251 constexpr R invoke_r(F&& f, Args&&... args) // C++23
252 noexcept(is_nothrow_invocable_r_v<R, F, Args...>);
253
254namespace placeholders {
255 // M is the implementation-defined number of placeholders
256 extern unspecified _1;
257 extern unspecified _2;
258 .
259 .
260 .
261 extern unspecified _Mp;
262}
263
264template <class Operation>
265class binder1st // deprecated in C++11, removed in C++17
266 : public unary_function<typename Operation::second_argument_type,
267 typename Operation::result_type>
268{
269protected:
270 Operation op;
271 typename Operation::first_argument_type value;
272public:
273 binder1st(const Operation& x, const typename Operation::first_argument_type y);
274 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
275 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
276};
277
278template <class Operation, class T>
279binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
280
281template <class Operation>
282class binder2nd // deprecated in C++11, removed in C++17
283 : public unary_function<typename Operation::first_argument_type,
284 typename Operation::result_type>
285{
286protected:
287 Operation op;
288 typename Operation::second_argument_type value;
289public:
290 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
291 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
292 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
293};
294
295template <class Operation, class T>
296binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
297
298template <class Arg, class Result> // deprecated in C++11, removed in C++17
299class pointer_to_unary_function : public unary_function<Arg, Result>
300{
301public:
302 explicit pointer_to_unary_function(Result (*f)(Arg));
303 Result operator()(Arg x) const;
304};
305
306template <class Arg, class Result>
307pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); // deprecated in C++11, removed in C++17
308
309template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
310class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
311{
312public:
313 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
314 Result operator()(Arg1 x, Arg2 y) const;
315};
316
317template <class Arg1, class Arg2, class Result>
318pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); // deprecated in C++11, removed in C++17
319
320template<class S, class T> // deprecated in C++11, removed in C++17
321class mem_fun_t : public unary_function<T*, S>
322{
323public:
324 explicit mem_fun_t(S (T::*p)());
325 S operator()(T* p) const;
326};
327
328template<class S, class T, class A>
329class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
330{
331public:
332 explicit mem_fun1_t(S (T::*p)(A));
333 S operator()(T* p, A x) const;
334};
335
336template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
337template<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
338
339template<class S, class T>
340class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
341{
342public:
343 explicit mem_fun_ref_t(S (T::*p)());
344 S operator()(T& p) const;
345};
346
347template<class S, class T, class A>
348class mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
349{
350public:
351 explicit mem_fun1_ref_t(S (T::*p)(A));
352 S operator()(T& p, A x) const;
353};
354
355template<class S, class T>
356mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
357template<class S, class T, class A>
358mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)); // deprecated in C++11, removed in C++17
359
360template <class S, class T>
361class const_mem_fun_t : public unary_function<const T*, S> // deprecated in C++11, removed in C++17
362{
363public:
364 explicit const_mem_fun_t(S (T::*p)() const);
365 S operator()(const T* p) const;
366};
367
368template <class S, class T, class A>
369class const_mem_fun1_t : public binary_function<const T*, A, S> // deprecated in C++11, removed in C++17
370{
371public:
372 explicit const_mem_fun1_t(S (T::*p)(A) const);
373 S operator()(const T* p, A x) const;
374};
375
376template <class S, class T>
377const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
378template <class S, class T, class A>
379const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const); // deprecated in C++11, removed in C++17
380
381template <class S, class T>
382class const_mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
383{
384public:
385 explicit const_mem_fun_ref_t(S (T::*p)() const);
386 S operator()(const T& p) const;
387};
388
389template <class S, class T, class A>
390class const_mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
391{
392public:
393 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
394 S operator()(const T& p, A x) const;
395};
396
397template <class S, class T>
398const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const); // deprecated in C++11, removed in C++17
399template <class S, class T, class A>
400const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const); // deprecated in C++11, removed in C++17
401
402template<class R, class T> constexpr unspecified mem_fn(R T::*) noexcept; // constexpr in C++20
403
404class bad_function_call
405 : public exception
406{
407};
408
409template<class> class function; // undefined
410
411template<class R, class... ArgTypes>
412class function<R(ArgTypes...)>
413 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
414 // ArgTypes contains T1
415 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
416 // ArgTypes contains T1 and T2
417{
418public:
419 typedef R result_type;
420
421 // construct/copy/destroy:
422 function() noexcept;
423 function(nullptr_t) noexcept;
424 function(const function&);
425 function(function&&) noexcept;
426 template<class F>
427 function(F);
428 template<Allocator Alloc>
429 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
430 template<Allocator Alloc>
431 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
432 template<Allocator Alloc>
433 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
434 template<Allocator Alloc>
435 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
436 template<class F, Allocator Alloc>
437 function(allocator_arg_t, const Alloc&, F); // removed in C++17
438
439 function& operator=(const function&);
440 function& operator=(function&&) noexcept;
441 function& operator=(nullptr_t) noexcept;
442 template<class F>
443 function& operator=(F&&);
444 template<class F>
445 function& operator=(reference_wrapper<F>) noexcept;
446
447 ~function();
448
449 // function modifiers:
450 void swap(function&) noexcept;
451 template<class F, class Alloc>
452 void assign(F&&, const Alloc&); // Removed in C++17
453
454 // function capacity:
455 explicit operator bool() const noexcept;
456
457 // function invocation:
458 R operator()(ArgTypes...) const;
459
460 // function target access:
461 const std::type_info& target_type() const noexcept;
462 template <typename T> T* target() noexcept;
463 template <typename T> const T* target() const noexcept;
464};
465
466// Deduction guides
467template<class R, class ...Args>
468function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
469
470template<class F>
471function(F) -> function<see-below>; // since C++17
472
473// Null pointer comparisons:
474template <class R, class ... ArgTypes>
475 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
476
477template <class R, class ... ArgTypes>
478 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept; // removed in C++20
479
480template <class R, class ... ArgTypes>
481 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept; // removed in C++20
482
483template <class R, class ... ArgTypes>
484 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept; // removed in C++20
485
486// specialized algorithms:
487template <class R, class ... ArgTypes>
488 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
489
490template <class T> struct hash;
491
492template <> struct hash<bool>;
493template <> struct hash<char>;
494template <> struct hash<signed char>;
495template <> struct hash<unsigned char>;
496template <> struct hash<char8_t>; // since C++20
497template <> struct hash<char16_t>;
498template <> struct hash<char32_t>;
499template <> struct hash<wchar_t>;
500template <> struct hash<short>;
501template <> struct hash<unsigned short>;
502template <> struct hash<int>;
503template <> struct hash<unsigned int>;
504template <> struct hash<long>;
505template <> struct hash<long long>;
506template <> struct hash<unsigned long>;
507template <> struct hash<unsigned long long>;
508
509template <> struct hash<float>;
510template <> struct hash<double>;
511template <> struct hash<long double>;
512
513template<class T> struct hash<T*>;
514template <> struct hash<nullptr_t>; // C++17
515
516namespace ranges {
517 // [range.cmp], concept-constrained comparisons
518 struct equal_to;
519 struct not_equal_to;
520 struct greater;
521 struct less;
522 struct greater_equal;
523 struct less_equal;
524}
525
526} // std
527
528POLICY: For non-variadic implementations, the number of arguments is limited
529 to 3. It is hoped that the need for non-variadic implementations
530 will be minimal.
531
532*/
533
534#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
535# include <__cxx03/functional>
536#else
537# include <__config>
538
539# include <__functional/binary_function.h>
540# include <__functional/binary_negate.h>
541# include <__functional/bind.h>
542# include <__functional/binder1st.h>
543# include <__functional/binder2nd.h>
544# include <__functional/hash.h>
545# include <__functional/mem_fn.h>
546# include <__functional/mem_fun_ref.h>
547# include <__functional/operations.h>
548# include <__functional/pointer_to_binary_function.h>
549# include <__functional/pointer_to_unary_function.h>
550# include <__functional/reference_wrapper.h>
551# include <__functional/unary_function.h>
552# include <__functional/unary_negate.h>
553
554# ifndef _LIBCPP_CXX03_LANG
555# include <__functional/function.h>
556# endif
557
558# if _LIBCPP_STD_VER >= 17
559# include <__functional/boyer_moore_searcher.h>
560# include <__functional/default_searcher.h>
561# include <__functional/invoke.h>
562# include <__functional/not_fn.h>
563# endif
564
565# if _LIBCPP_STD_VER >= 20
566# include <__functional/bind_back.h>
567# include <__functional/bind_front.h>
568# include <__functional/identity.h>
569# include <__functional/ranges_operations.h>
570# include <__type_traits/common_reference.h>
571# include <__type_traits/unwrap_ref.h>
572# endif
573
574# include <version>
575
576# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
577# pragma GCC system_header
578# endif
579
580# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && defined(_LIBCPP_CXX03_LANG)
581# include <limits>
582# include <new>
583# endif
584
585# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 14
586# include <array>
587# include <initializer_list>
588# include <unordered_map>
589# endif
590
591# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
592# include <atomic>
593# include <concepts>
594# include <cstdlib>
595# include <exception>
596# include <iosfwd>
597# include <memory>
598# include <stdexcept>
599# include <tuple>
600# include <type_traits>
601# include <typeinfo>
602# include <utility>
603# include <vector>
604# endif
605
606# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER == 23
607# include <__vector/vector.h>
608# endif
609#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
610
611#endif // _LIBCPP_FUNCTIONAL
612