blob: 8687ab8f1d8a597926f39990b1d9d69c7041d8ab [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------ functional ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
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
Howard Hinnantf06d9262010-08-20 19:36:46 +000034template <class T>
Howard Hinnantc51e1022010-05-11 19:42:16 +000035class 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
Arthur O'Dwyer8fa87942020-12-05 19:37:41 -050045 template<class U>
46 reference_wrapper(U&&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +000047 reference_wrapper(const reference_wrapper<T>& x) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000048
49 // assignment
Howard Hinnantf7724cd2011-05-28 17:59:48 +000050 reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000051
52 // access
Howard Hinnantf7724cd2011-05-28 17:59:48 +000053 operator T& () const noexcept;
54 T& get() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 // invoke
57 template <class... ArgTypes>
Howard Hinnantc9c775b2013-09-21 17:58:58 +000058 typename result_of<T&(ArgTypes&&...)>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +000059 operator() (ArgTypes&&...) const;
60};
61
Arthur O'Dwyer8fa87942020-12-05 19:37:41 -050062template <class T>
63 reference_wrapper(T&) -> reference_wrapper<T>;
64
Howard Hinnantf7724cd2011-05-28 17:59:48 +000065template <class T> reference_wrapper<T> ref(T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000066template <class T> void ref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000067template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068
Howard Hinnantf7724cd2011-05-28 17:59:48 +000069template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000070template <class T> void cref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000071template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Louis Dionnedb1892a2018-12-03 14:03:27 +000073template <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
Marshall Clow974bae22013-07-29 14:21:53 +000078template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000079struct plus : binary_function<T, T, T>
80{
81 T operator()(const T& x, const T& y) const;
82};
83
Marshall Clow974bae22013-07-29 14:21:53 +000084template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000085struct minus : binary_function<T, T, T>
86{
87 T operator()(const T& x, const T& y) const;
88};
89
Marshall Clow974bae22013-07-29 14:21:53 +000090template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000091struct multiplies : binary_function<T, T, T>
92{
93 T operator()(const T& x, const T& y) const;
94};
95
Marshall Clow974bae22013-07-29 14:21:53 +000096template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000097struct divides : binary_function<T, T, T>
98{
99 T operator()(const T& x, const T& y) const;
100};
101
Marshall Clow974bae22013-07-29 14:21:53 +0000102template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103struct modulus : binary_function<T, T, T>
104{
105 T operator()(const T& x, const T& y) const;
106};
107
Marshall Clow974bae22013-07-29 14:21:53 +0000108template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000109struct negate : unary_function<T, T>
110{
111 T operator()(const T& x) const;
112};
113
Marshall Clow974bae22013-07-29 14:21:53 +0000114template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115struct equal_to : binary_function<T, T, bool>
116{
117 bool operator()(const T& x, const T& y) const;
118};
119
Marshall Clow974bae22013-07-29 14:21:53 +0000120template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121struct not_equal_to : binary_function<T, T, bool>
122{
123 bool operator()(const T& x, const T& y) const;
124};
125
Marshall Clow974bae22013-07-29 14:21:53 +0000126template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127struct greater : binary_function<T, T, bool>
128{
129 bool operator()(const T& x, const T& y) const;
130};
131
Marshall Clow974bae22013-07-29 14:21:53 +0000132template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133struct less : binary_function<T, T, bool>
134{
135 bool operator()(const T& x, const T& y) const;
136};
137
Marshall Clow974bae22013-07-29 14:21:53 +0000138template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139struct greater_equal : binary_function<T, T, bool>
140{
141 bool operator()(const T& x, const T& y) const;
142};
143
Marshall Clow974bae22013-07-29 14:21:53 +0000144template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145struct less_equal : binary_function<T, T, bool>
146{
147 bool operator()(const T& x, const T& y) const;
148};
149
Marshall Clow974bae22013-07-29 14:21:53 +0000150template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151struct logical_and : binary_function<T, T, bool>
152{
153 bool operator()(const T& x, const T& y) const;
154};
155
Marshall Clow974bae22013-07-29 14:21:53 +0000156template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157struct logical_or : binary_function<T, T, bool>
158{
159 bool operator()(const T& x, const T& y) const;
160};
161
Marshall Clow974bae22013-07-29 14:21:53 +0000162template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163struct logical_not : unary_function<T, bool>
164{
165 bool operator()(const T& x) const;
166};
167
Marshall Clow974bae22013-07-29 14:21:53 +0000168template <class T> // <class T=void> in C++14
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500169struct bit_and : binary_function<T, T, T>
Marshall Clow974bae22013-07-29 14:21:53 +0000170{
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500171 T operator()(const T& x, const T& y) const;
Marshall Clow974bae22013-07-29 14:21:53 +0000172};
173
174template <class T> // <class T=void> in C++14
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500175struct bit_or : binary_function<T, T, T>
Marshall Clow974bae22013-07-29 14:21:53 +0000176{
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500177 T operator()(const T& x, const T& y) const;
Marshall Clow974bae22013-07-29 14:21:53 +0000178};
179
180template <class T> // <class T=void> in C++14
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500181struct bit_xor : binary_function<T, T, T>
Marshall Clow974bae22013-07-29 14:21:53 +0000182{
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500183 T operator()(const T& x, const T& y) const;
Marshall Clow974bae22013-07-29 14:21:53 +0000184};
185
186template <class T=void> // C++14
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500187struct bit_not : unary_function<T, T>
Marshall Clow974bae22013-07-29 14:21:53 +0000188{
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500189 T operator()(const T& x) const;
Marshall Clow974bae22013-07-29 14:21:53 +0000190};
191
Christopher Di Bellae68ec582021-03-18 17:21:35 +0000192struct identity; // C++20
193
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000195class unary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196 : public unary_function<typename Predicate::argument_type, bool>
197{
198public:
199 explicit unary_negate(const Predicate& pred);
200 bool operator()(const typename Predicate::argument_type& x) const;
201};
202
Louis Dionne481a2662018-09-23 18:35:00 +0000203template <class Predicate> // deprecated in C++17
204unary_negate<Predicate> not1(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205
206template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000207class binary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208 : public binary_function<typename Predicate::first_argument_type,
209 typename Predicate::second_argument_type,
210 bool>
211{
212public:
213 explicit binary_negate(const Predicate& pred);
214 bool operator()(const typename Predicate::first_argument_type& x,
215 const typename Predicate::second_argument_type& y) const;
216};
217
Louis Dionne481a2662018-09-23 18:35:00 +0000218template <class Predicate> // deprecated in C++17
219binary_negate<Predicate> not2(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500221template <class F>
222constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
Eric Fiselier934f63b2016-06-02 01:25:41 +0000223
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224template<class T> struct is_bind_expression;
225template<class T> struct is_placeholder;
226
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000227 // See C++14 20.9.9, Function object binders
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000228template <class T> inline constexpr bool is_bind_expression_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000229 = is_bind_expression<T>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000230template <class T> inline constexpr int is_placeholder_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000231 = is_placeholder<T>::value; // C++17
232
233
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000234template<class Fn, class... BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500235 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000236template<class R, class Fn, class... BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500237 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238
Louis Dionneaf34f122019-04-03 17:54:37 +0000239template<class F, class... Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500240 constexpr // constexpr in C++20
Louis Dionneaf34f122019-04-03 17:54:37 +0000241 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
242 noexcept(is_nothrow_invocable_v<F, Args...>);
243
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000244namespace placeholders {
245 // M is the implementation-defined number of placeholders
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246 extern unspecified _1;
247 extern unspecified _2;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000248 .
249 .
250 .
Howard Hinnantc834c512011-11-29 18:15:50 +0000251 extern unspecified _Mp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252}
253
254template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000255class binder1st // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 : public unary_function<typename Operation::second_argument_type,
257 typename Operation::result_type>
258{
259protected:
260 Operation op;
261 typename Operation::first_argument_type value;
262public:
263 binder1st(const Operation& x, const typename Operation::first_argument_type y);
264 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
265 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
266};
267
268template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000269binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
271template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000272class binder2nd // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273 : public unary_function<typename Operation::first_argument_type,
274 typename Operation::result_type>
275{
276protected:
277 Operation op;
278 typename Operation::second_argument_type value;
279public:
280 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
281 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
282 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
283};
284
285template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000286binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287
Marshall Clow26a027c2017-04-13 18:25:32 +0000288template <class Arg, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289class pointer_to_unary_function : public unary_function<Arg, Result>
290{
291public:
292 explicit pointer_to_unary_function(Result (*f)(Arg));
293 Result operator()(Arg x) const;
294};
295
296template <class Arg, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000297pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Marshall Clow26a027c2017-04-13 18:25:32 +0000299template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
301{
302public:
303 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
304 Result operator()(Arg1 x, Arg2 y) const;
305};
306
307template <class Arg1, class Arg2, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000308pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309
Marshall Clow26a027c2017-04-13 18:25:32 +0000310template<class S, class T> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311class mem_fun_t : public unary_function<T*, S>
312{
313public:
314 explicit mem_fun_t(S (T::*p)());
315 S operator()(T* p) const;
316};
317
318template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000319class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320{
321public:
322 explicit mem_fun1_t(S (T::*p)(A));
323 S operator()(T* p, A x) const;
324};
325
Marshall Clow26a027c2017-04-13 18:25:32 +0000326template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
327template<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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328
329template<class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000330class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331{
332public:
333 explicit mem_fun_ref_t(S (T::*p)());
334 S operator()(T& p) const;
335};
336
337template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000338class mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339{
340public:
341 explicit mem_fun1_ref_t(S (T::*p)(A));
342 S operator()(T& p, A x) const;
343};
344
Marshall Clow26a027c2017-04-13 18:25:32 +0000345template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
346template<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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000347
348template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000349class const_mem_fun_t : public unary_function<const T*, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350{
351public:
352 explicit const_mem_fun_t(S (T::*p)() const);
353 S operator()(const T* p) const;
354};
355
356template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000357class const_mem_fun1_t : public binary_function<const T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358{
359public:
360 explicit const_mem_fun1_t(S (T::*p)(A) const);
361 S operator()(const T* p, A x) const;
362};
363
Marshall Clow26a027c2017-04-13 18:25:32 +0000364template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
365template <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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366
367template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000368class const_mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369{
370public:
371 explicit const_mem_fun_ref_t(S (T::*p)() const);
372 S operator()(const T& p) const;
373};
374
375template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000376class const_mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377{
378public:
379 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
380 S operator()(const T& p, A x) const;
381};
382
Marshall Clow26a027c2017-04-13 18:25:32 +0000383template <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
384template <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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500386template<class R, class T>
387constexpr unspecified mem_fn(R T::*); // constexpr in C++20
Howard Hinnantf06d9262010-08-20 19:36:46 +0000388
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389class bad_function_call
390 : public exception
391{
392};
393
Howard Hinnantf06d9262010-08-20 19:36:46 +0000394template<class> class function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395
Howard Hinnantf06d9262010-08-20 19:36:46 +0000396template<class R, class... ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397class function<R(ArgTypes...)>
398 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
399 // ArgTypes contains T1
400 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
401 // ArgTypes contains T1 and T2
402{
403public:
404 typedef R result_type;
405
Howard Hinnantf06d9262010-08-20 19:36:46 +0000406 // construct/copy/destroy:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000407 function() noexcept;
408 function(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000410 function(function&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 template<class F>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 function(F);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000414 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000416 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000418 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000420 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 template<class F, Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000422 function(allocator_arg_t, const Alloc&, F); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000425 function& operator=(function&&) noexcept;
Howard Hinnant7b85be02011-05-29 13:53:56 +0000426 function& operator=(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427 template<class F>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000428 function& operator=(F&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429 template<class F>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000430 function& operator=(reference_wrapper<F>) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431
432 ~function();
433
Howard Hinnantf06d9262010-08-20 19:36:46 +0000434 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000435 void swap(function&) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +0000436 template<class F, class Alloc>
Marshall Clowfc8fd832016-01-25 17:29:55 +0000437 void assign(F&&, const Alloc&); // Removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438
Howard Hinnantf06d9262010-08-20 19:36:46 +0000439 // function capacity:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000440 explicit operator bool() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441
Howard Hinnantf06d9262010-08-20 19:36:46 +0000442 // function invocation:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443 R operator()(ArgTypes...) const;
444
Howard Hinnantf06d9262010-08-20 19:36:46 +0000445 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000446 const std::type_info& target_type() const noexcept;
447 template <typename T> T* target() noexcept;
448 template <typename T> const T* target() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449};
450
Louis Dionne4af49712019-07-18 19:50:56 +0000451// Deduction guides
452template<class R, class ...Args>
453function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
454
455template<class F>
456function(F) -> function<see-below>; // since C++17
457
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000458// Null pointer comparisons:
459template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000460 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000462template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000463 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000465template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000466 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000468template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000469 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000471// specialized algorithms:
472template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000473 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474
475template <class T> struct hash;
476
477template <> struct hash<bool>;
478template <> struct hash<char>;
479template <> struct hash<signed char>;
480template <> struct hash<unsigned char>;
Yuriy Chernyshovfbb87fa2020-12-08 13:39:56 -0500481template <> struct hash<char8_t>; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482template <> struct hash<char16_t>;
483template <> struct hash<char32_t>;
484template <> struct hash<wchar_t>;
485template <> struct hash<short>;
486template <> struct hash<unsigned short>;
487template <> struct hash<int>;
488template <> struct hash<unsigned int>;
489template <> struct hash<long>;
490template <> struct hash<long long>;
491template <> struct hash<unsigned long>;
492template <> struct hash<unsigned long long>;
493
494template <> struct hash<float>;
495template <> struct hash<double>;
496template <> struct hash<long double>;
497
498template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000499template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000500
501} // std
502
503POLICY: For non-variadic implementations, the number of arguments is limited
504 to 3. It is hoped that the need for non-variadic implementations
505 will be minimal.
506
507*/
508
509#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400510#include <__debug>
zoecarver9ce102c2021-04-22 17:33:04 -0700511#include <concepts>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512#include <type_traits>
513#include <typeinfo>
514#include <exception>
515#include <memory>
516#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000517#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000518#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519
520#include <__functional_base>
521
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000522#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000524#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525
526_LIBCPP_BEGIN_NAMESPACE_STD
527
Marshall Clow974bae22013-07-29 14:21:53 +0000528#if _LIBCPP_STD_VER > 11
529template <class _Tp = void>
530#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000532#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000533struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534{
Marshall Clowd18ee652013-09-28 19:06:12 +0000535 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
536 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537 {return __x + __y;}
538};
539
Marshall Clow974bae22013-07-29 14:21:53 +0000540#if _LIBCPP_STD_VER > 11
541template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000542struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000543{
544 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000545 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
546 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000547 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
548 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
549 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000550 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000551};
552#endif
553
554
555#if _LIBCPP_STD_VER > 11
556template <class _Tp = void>
557#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000559#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000560struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561{
Marshall Clowd18ee652013-09-28 19:06:12 +0000562 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
563 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000564 {return __x - __y;}
565};
566
Marshall Clow974bae22013-07-29 14:21:53 +0000567#if _LIBCPP_STD_VER > 11
568template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000569struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000570{
571 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000572 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
573 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000574 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
575 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
576 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000577 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000578};
579#endif
580
581
582#if _LIBCPP_STD_VER > 11
583template <class _Tp = void>
584#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000586#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000587struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588{
Marshall Clowd18ee652013-09-28 19:06:12 +0000589 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
590 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591 {return __x * __y;}
592};
593
Marshall Clow974bae22013-07-29 14:21:53 +0000594#if _LIBCPP_STD_VER > 11
595template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000596struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000597{
598 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000599 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
600 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000601 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
602 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
603 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000604 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000605};
606#endif
607
608
609#if _LIBCPP_STD_VER > 11
610template <class _Tp = void>
611#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000613#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000614struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615{
Marshall Clowd18ee652013-09-28 19:06:12 +0000616 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
617 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618 {return __x / __y;}
619};
620
Marshall Clow974bae22013-07-29 14:21:53 +0000621#if _LIBCPP_STD_VER > 11
622template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000623struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000624{
625 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000626 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
627 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000628 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
629 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
630 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000631 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000632};
633#endif
634
635
636#if _LIBCPP_STD_VER > 11
637template <class _Tp = void>
638#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000639template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000640#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000641struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642{
Marshall Clowd18ee652013-09-28 19:06:12 +0000643 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
644 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645 {return __x % __y;}
646};
647
Marshall Clow974bae22013-07-29 14:21:53 +0000648#if _LIBCPP_STD_VER > 11
649template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000650struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000651{
652 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000653 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
654 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000655 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
656 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
657 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000658 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000659};
660#endif
661
662
663#if _LIBCPP_STD_VER > 11
664template <class _Tp = void>
665#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000667#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000668struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669{
Marshall Clowd18ee652013-09-28 19:06:12 +0000670 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
671 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 {return -__x;}
673};
674
Marshall Clow974bae22013-07-29 14:21:53 +0000675#if _LIBCPP_STD_VER > 11
676template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000677struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000678{
679 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000680 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
681 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000682 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
683 -> decltype (- _VSTD::forward<_Tp>(__x))
684 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000685 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000686};
687#endif
688
689
690#if _LIBCPP_STD_VER > 11
691template <class _Tp = void>
692#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000694#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000695struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696{
Marshall Clowd18ee652013-09-28 19:06:12 +0000697 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
698 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699 {return __x == __y;}
700};
701
Marshall Clow974bae22013-07-29 14:21:53 +0000702#if _LIBCPP_STD_VER > 11
703template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000704struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000705{
Marshall Clowd18ee652013-09-28 19:06:12 +0000706 template <class _T1, class _T2>
707 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000708 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000709 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
710 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
711 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000712 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000713};
714#endif
715
716
717#if _LIBCPP_STD_VER > 11
718template <class _Tp = void>
719#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000721#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000722struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723{
Marshall Clowd18ee652013-09-28 19:06:12 +0000724 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
725 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 {return __x != __y;}
727};
728
Marshall Clow974bae22013-07-29 14:21:53 +0000729#if _LIBCPP_STD_VER > 11
730template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000731struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000732{
Marshall Clowd18ee652013-09-28 19:06:12 +0000733 template <class _T1, class _T2>
734 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000735 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000736 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
737 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
738 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000739 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000740};
741#endif
742
743
744#if _LIBCPP_STD_VER > 11
745template <class _Tp = void>
746#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000748#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000749struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750{
Marshall Clowd18ee652013-09-28 19:06:12 +0000751 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
752 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753 {return __x > __y;}
754};
755
Marshall Clow974bae22013-07-29 14:21:53 +0000756#if _LIBCPP_STD_VER > 11
757template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000758struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000759{
Marshall Clowd18ee652013-09-28 19:06:12 +0000760 template <class _T1, class _T2>
761 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000762 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000763 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
764 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
765 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000766 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000767};
768#endif
769
770
Howard Hinnantb17caf92012-02-21 21:02:58 +0000771// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772
Marshall Clow974bae22013-07-29 14:21:53 +0000773#if _LIBCPP_STD_VER > 11
774template <class _Tp = void>
775#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000777#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000778struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779{
Marshall Clowd18ee652013-09-28 19:06:12 +0000780 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
781 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782 {return __x >= __y;}
783};
784
Marshall Clow974bae22013-07-29 14:21:53 +0000785#if _LIBCPP_STD_VER > 11
786template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000787struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000788{
Marshall Clowd18ee652013-09-28 19:06:12 +0000789 template <class _T1, class _T2>
790 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000791 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000792 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
793 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
794 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000795 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000796};
797#endif
798
799
800#if _LIBCPP_STD_VER > 11
801template <class _Tp = void>
802#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000804#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000805struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000806{
Marshall Clowd18ee652013-09-28 19:06:12 +0000807 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
808 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809 {return __x <= __y;}
810};
811
Marshall Clow974bae22013-07-29 14:21:53 +0000812#if _LIBCPP_STD_VER > 11
813template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000814struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000815{
Marshall Clowd18ee652013-09-28 19:06:12 +0000816 template <class _T1, class _T2>
817 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000818 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000819 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
820 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
821 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000822 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000823};
824#endif
825
826
827#if _LIBCPP_STD_VER > 11
828template <class _Tp = void>
829#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000831#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000832struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833{
Marshall Clowd18ee652013-09-28 19:06:12 +0000834 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
835 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 {return __x && __y;}
837};
838
Marshall Clow974bae22013-07-29 14:21:53 +0000839#if _LIBCPP_STD_VER > 11
840template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000841struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000842{
Marshall Clowd18ee652013-09-28 19:06:12 +0000843 template <class _T1, class _T2>
844 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000845 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000846 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
847 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
848 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000849 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000850};
851#endif
852
853
854#if _LIBCPP_STD_VER > 11
855template <class _Tp = void>
856#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000858#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000859struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860{
Marshall Clowd18ee652013-09-28 19:06:12 +0000861 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
862 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863 {return __x || __y;}
864};
865
Marshall Clow974bae22013-07-29 14:21:53 +0000866#if _LIBCPP_STD_VER > 11
867template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000868struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000869{
Marshall Clowd18ee652013-09-28 19:06:12 +0000870 template <class _T1, class _T2>
871 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000872 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000873 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
874 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
875 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000876 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000877};
878#endif
879
880
881#if _LIBCPP_STD_VER > 11
882template <class _Tp = void>
883#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000885#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000886struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887{
Marshall Clowd18ee652013-09-28 19:06:12 +0000888 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
889 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890 {return !__x;}
891};
892
Marshall Clow974bae22013-07-29 14:21:53 +0000893#if _LIBCPP_STD_VER > 11
894template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000895struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000896{
897 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000898 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
899 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000900 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
901 -> decltype (!_VSTD::forward<_Tp>(__x))
902 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000903 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000904};
905#endif
906
907
908#if _LIBCPP_STD_VER > 11
909template <class _Tp = void>
910#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000912#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000913struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914{
Marshall Clowd18ee652013-09-28 19:06:12 +0000915 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
916 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917 {return __x & __y;}
918};
919
Marshall Clow974bae22013-07-29 14:21:53 +0000920#if _LIBCPP_STD_VER > 11
921template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000922struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000923{
Marshall Clowd18ee652013-09-28 19:06:12 +0000924 template <class _T1, class _T2>
925 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000926 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000927 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
928 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
929 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000930 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000931};
932#endif
933
934
935#if _LIBCPP_STD_VER > 11
936template <class _Tp = void>
937#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000938template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000939#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000940struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941{
Marshall Clowd18ee652013-09-28 19:06:12 +0000942 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
943 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944 {return __x | __y;}
945};
946
Marshall Clow974bae22013-07-29 14:21:53 +0000947#if _LIBCPP_STD_VER > 11
948template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000949struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000950{
Marshall Clowd18ee652013-09-28 19:06:12 +0000951 template <class _T1, class _T2>
952 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000953 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000954 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
955 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
956 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000957 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000958};
959#endif
960
961
962#if _LIBCPP_STD_VER > 11
963template <class _Tp = void>
964#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000966#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968{
Marshall Clowd18ee652013-09-28 19:06:12 +0000969 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
970 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 {return __x ^ __y;}
972};
973
Marshall Clow974bae22013-07-29 14:21:53 +0000974#if _LIBCPP_STD_VER > 11
975template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000976struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000977{
Marshall Clowd18ee652013-09-28 19:06:12 +0000978 template <class _T1, class _T2>
979 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000980 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000981 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
982 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
983 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000984 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000985};
986#endif
987
988
989#if _LIBCPP_STD_VER > 11
990template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000991struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000992{
Marshall Clowd18ee652013-09-28 19:06:12 +0000993 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
994 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000995 {return ~__x;}
996};
997
998template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000999struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +00001000{
1001 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +00001002 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1003 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +00001004 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
1005 -> decltype (~_VSTD::forward<_Tp>(__x))
1006 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +00001007 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +00001008};
1009#endif
1010
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001012class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 : public unary_function<typename _Predicate::argument_type, bool>
1014{
1015 _Predicate __pred_;
1016public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001017 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1018 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001020 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1021 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022 {return !__pred_(__x);}
1023};
1024
1025template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001026_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027unary_negate<_Predicate>
1028not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1029
1030template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001031class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 : public binary_function<typename _Predicate::first_argument_type,
1033 typename _Predicate::second_argument_type,
1034 bool>
1035{
1036 _Predicate __pred_;
1037public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001038 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001039 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1040
1041 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1042 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 const typename _Predicate::second_argument_type& __y) const
1044 {return !__pred_(__x, __y);}
1045};
1046
1047template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001048_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049binary_negate<_Predicate>
1050not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1051
Marshall Clow26a027c2017-04-13 18:25:32 +00001052#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001054class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 : public unary_function<typename __Operation::second_argument_type,
1056 typename __Operation::result_type>
1057{
1058protected:
1059 __Operation op;
1060 typename __Operation::first_argument_type value;
1061public:
1062 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1063 const typename __Operation::first_argument_type __y)
1064 : op(__x), value(__y) {}
1065 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1066 (typename __Operation::second_argument_type& __x) const
1067 {return op(value, __x);}
1068 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1069 (const typename __Operation::second_argument_type& __x) const
1070 {return op(value, __x);}
1071};
1072
1073template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001074_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075binder1st<__Operation>
1076bind1st(const __Operation& __op, const _Tp& __x)
1077 {return binder1st<__Operation>(__op, __x);}
1078
1079template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001080class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 : public unary_function<typename __Operation::first_argument_type,
1082 typename __Operation::result_type>
1083{
1084protected:
1085 __Operation op;
1086 typename __Operation::second_argument_type value;
1087public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1090 : op(__x), value(__y) {}
1091 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1092 ( typename __Operation::first_argument_type& __x) const
1093 {return op(__x, value);}
1094 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1095 (const typename __Operation::first_argument_type& __x) const
1096 {return op(__x, value);}
1097};
1098
1099template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001100_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101binder2nd<__Operation>
1102bind2nd(const __Operation& __op, const _Tp& __x)
1103 {return binder2nd<__Operation>(__op, __x);}
1104
1105template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001106class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001107 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108{
1109 _Result (*__f_)(_Arg);
1110public:
1111 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1112 : __f_(__f) {}
1113 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1114 {return __f_(__x);}
1115};
1116
1117template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001118_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119pointer_to_unary_function<_Arg,_Result>
1120ptr_fun(_Result (*__f)(_Arg))
1121 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1122
1123template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001124class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001125 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126{
1127 _Result (*__f_)(_Arg1, _Arg2);
1128public:
1129 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1130 : __f_(__f) {}
1131 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1132 {return __f_(__x, __y);}
1133};
1134
1135template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001136_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137pointer_to_binary_function<_Arg1,_Arg2,_Result>
1138ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1139 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1140
1141template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001142class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1143 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144{
1145 _Sp (_Tp::*__p_)();
1146public:
1147 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1148 : __p_(__p) {}
1149 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1150 {return (__p->*__p_)();}
1151};
1152
1153template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001154class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1155 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156{
1157 _Sp (_Tp::*__p_)(_Ap);
1158public:
1159 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1160 : __p_(__p) {}
1161 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1162 {return (__p->*__p_)(__x);}
1163};
1164
1165template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001166_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167mem_fun_t<_Sp,_Tp>
1168mem_fun(_Sp (_Tp::*__f)())
1169 {return mem_fun_t<_Sp,_Tp>(__f);}
1170
1171template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001172_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173mem_fun1_t<_Sp,_Tp,_Ap>
1174mem_fun(_Sp (_Tp::*__f)(_Ap))
1175 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1176
1177template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001178class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1179 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180{
1181 _Sp (_Tp::*__p_)();
1182public:
1183 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1184 : __p_(__p) {}
1185 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1186 {return (__p.*__p_)();}
1187};
1188
1189template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001190class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1191 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192{
1193 _Sp (_Tp::*__p_)(_Ap);
1194public:
1195 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1196 : __p_(__p) {}
1197 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1198 {return (__p.*__p_)(__x);}
1199};
1200
1201template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001202_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203mem_fun_ref_t<_Sp,_Tp>
1204mem_fun_ref(_Sp (_Tp::*__f)())
1205 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1206
1207template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001208_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209mem_fun1_ref_t<_Sp,_Tp,_Ap>
1210mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1211 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1212
1213template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001214class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1215 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216{
1217 _Sp (_Tp::*__p_)() const;
1218public:
1219 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1220 : __p_(__p) {}
1221 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1222 {return (__p->*__p_)();}
1223};
1224
1225template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001226class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1227 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228{
1229 _Sp (_Tp::*__p_)(_Ap) const;
1230public:
1231 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1232 : __p_(__p) {}
1233 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1234 {return (__p->*__p_)(__x);}
1235};
1236
1237template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001238_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239const_mem_fun_t<_Sp,_Tp>
1240mem_fun(_Sp (_Tp::*__f)() const)
1241 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1242
1243template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001244_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245const_mem_fun1_t<_Sp,_Tp,_Ap>
1246mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1247 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1248
1249template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001250class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1251 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252{
1253 _Sp (_Tp::*__p_)() const;
1254public:
1255 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1256 : __p_(__p) {}
1257 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1258 {return (__p.*__p_)();}
1259};
1260
1261template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001262class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001263 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264{
1265 _Sp (_Tp::*__p_)(_Ap) const;
1266public:
1267 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1268 : __p_(__p) {}
1269 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1270 {return (__p.*__p_)(__x);}
1271};
1272
1273template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001274_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275const_mem_fun_ref_t<_Sp,_Tp>
1276mem_fun_ref(_Sp (_Tp::*__f)() const)
1277 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1278
1279template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001280_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1282mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1283 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001284#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001286////////////////////////////////////////////////////////////////////////////////
1287// MEMFUN
1288//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290template <class _Tp>
1291class __mem_fn
1292 : public __weak_result_type<_Tp>
1293{
1294public:
1295 // types
1296 typedef _Tp type;
1297private:
1298 type __f_;
1299
1300public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001301 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1302 __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001304#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305 // invoke
1306 template <class... _ArgTypes>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001307 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier2cc48332015-07-22 22:43:27 +00001308 typename __invoke_return<type, _ArgTypes...>::type
1309 operator() (_ArgTypes&&... __args) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001310 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001311 }
1312#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001313
1314 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001315 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001316 typename __invoke_return0<type, _A0>::type
1317 operator() (_A0& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001318 return _VSTD::__invoke(__f_, __a0);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001319 }
1320
Eric Fiselierce1813a2015-08-26 20:15:02 +00001321 template <class _A0>
1322 _LIBCPP_INLINE_VISIBILITY
1323 typename __invoke_return0<type, _A0 const>::type
1324 operator() (_A0 const& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001325 return _VSTD::__invoke(__f_, __a0);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001326 }
1327
Eric Fiselier2cc48332015-07-22 22:43:27 +00001328 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001329 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001330 typename __invoke_return1<type, _A0, _A1>::type
1331 operator() (_A0& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001332 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001333 }
1334
Eric Fiselierce1813a2015-08-26 20:15:02 +00001335 template <class _A0, class _A1>
1336 _LIBCPP_INLINE_VISIBILITY
1337 typename __invoke_return1<type, _A0 const, _A1>::type
1338 operator() (_A0 const& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001339 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001340 }
1341
1342 template <class _A0, class _A1>
1343 _LIBCPP_INLINE_VISIBILITY
1344 typename __invoke_return1<type, _A0, _A1 const>::type
1345 operator() (_A0& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001346 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001347 }
1348
1349 template <class _A0, class _A1>
1350 _LIBCPP_INLINE_VISIBILITY
1351 typename __invoke_return1<type, _A0 const, _A1 const>::type
1352 operator() (_A0 const& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001353 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001354 }
1355
Eric Fiselier2cc48332015-07-22 22:43:27 +00001356 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001357 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001358 typename __invoke_return2<type, _A0, _A1, _A2>::type
1359 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001360 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001361 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001362
1363 template <class _A0, class _A1, class _A2>
1364 _LIBCPP_INLINE_VISIBILITY
1365 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1366 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001367 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001368 }
1369
1370 template <class _A0, class _A1, class _A2>
1371 _LIBCPP_INLINE_VISIBILITY
1372 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1373 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001374 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001375 }
1376
1377 template <class _A0, class _A1, class _A2>
1378 _LIBCPP_INLINE_VISIBILITY
1379 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1380 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001381 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001382 }
1383
1384 template <class _A0, class _A1, class _A2>
1385 _LIBCPP_INLINE_VISIBILITY
1386 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1387 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001388 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001389 }
1390
1391 template <class _A0, class _A1, class _A2>
1392 _LIBCPP_INLINE_VISIBILITY
1393 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1394 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001395 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001396 }
1397
1398 template <class _A0, class _A1, class _A2>
1399 _LIBCPP_INLINE_VISIBILITY
1400 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1401 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001402 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001403 }
1404
1405 template <class _A0, class _A1, class _A2>
1406 _LIBCPP_INLINE_VISIBILITY
1407 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1408 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001409 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001410 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001411#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412};
1413
Howard Hinnantc834c512011-11-29 18:15:50 +00001414template<class _Rp, class _Tp>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001415inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00001416__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001417mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418{
Howard Hinnantc834c512011-11-29 18:15:50 +00001419 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420}
1421
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001422////////////////////////////////////////////////////////////////////////////////
1423// FUNCTION
1424//==============================================================================
1425
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426// bad_function_call
1427
Howard Hinnant4ff57432010-09-21 22:55:27 +00001428class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 : public exception
1430{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001431#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1432public:
1433 virtual ~bad_function_call() _NOEXCEPT;
1434
1435 virtual const char* what() const _NOEXCEPT;
1436#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437};
1438
Louis Dionne16fe2952018-07-11 23:14:33 +00001439_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001440void __throw_bad_function_call()
1441{
1442#ifndef _LIBCPP_NO_EXCEPTIONS
1443 throw bad_function_call();
1444#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001445 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001446#endif
1447}
1448
Louis Dionne44d1f812020-03-09 11:16:22 -04001449#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1450# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1451 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1452#else
1453# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1454#endif
1455
1456template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457
1458namespace __function
1459{
1460
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001461template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462struct __maybe_derive_from_unary_function
1463{
1464};
1465
Howard Hinnantc834c512011-11-29 18:15:50 +00001466template<class _Rp, class _A1>
1467struct __maybe_derive_from_unary_function<_Rp(_A1)>
1468 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469{
1470};
1471
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001472template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473struct __maybe_derive_from_binary_function
1474{
1475};
1476
Howard Hinnantc834c512011-11-29 18:15:50 +00001477template<class _Rp, class _A1, class _A2>
1478struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1479 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480{
1481};
1482
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001483template <class _Fp>
1484_LIBCPP_INLINE_VISIBILITY
1485bool __not_null(_Fp const&) { return true; }
1486
1487template <class _Fp>
1488_LIBCPP_INLINE_VISIBILITY
1489bool __not_null(_Fp* __ptr) { return __ptr; }
1490
1491template <class _Ret, class _Class>
1492_LIBCPP_INLINE_VISIBILITY
1493bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1494
1495template <class _Fp>
1496_LIBCPP_INLINE_VISIBILITY
1497bool __not_null(function<_Fp> const& __f) { return !!__f; }
1498
Louis Dionnee2391d72020-04-22 13:58:17 -04001499#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1500template <class _Rp, class ..._Args>
1501_LIBCPP_INLINE_VISIBILITY
1502bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1503#endif
1504
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001505} // namespace __function
1506
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001507#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001508
1509namespace __function {
1510
Eric Fiselier125798e2018-12-10 18:14:09 +00001511// __alloc_func holds a functor and an allocator.
1512
1513template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001514template <class _Fp, class _FB>
1515class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001516
1517template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1518class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1519{
1520 __compressed_pair<_Fp, _Ap> __f_;
1521
1522 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001523 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1524 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001525
1526 _LIBCPP_INLINE_VISIBILITY
1527 const _Target& __target() const { return __f_.first(); }
1528
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001529 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001530 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001531 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001532
1533 _LIBCPP_INLINE_VISIBILITY
1534 explicit __alloc_func(_Target&& __f)
1535 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1536 _VSTD::forward_as_tuple())
1537 {
1538 }
1539
1540 _LIBCPP_INLINE_VISIBILITY
1541 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1542 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1543 _VSTD::forward_as_tuple(__a))
1544 {
1545 }
1546
1547 _LIBCPP_INLINE_VISIBILITY
1548 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1549 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1550 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1551 {
1552 }
1553
1554 _LIBCPP_INLINE_VISIBILITY
1555 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1556 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1557 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1558 {
1559 }
1560
1561 _LIBCPP_INLINE_VISIBILITY
1562 _Rp operator()(_ArgTypes&&... __arg)
1563 {
1564 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1565 return _Invoker::__call(__f_.first(),
1566 _VSTD::forward<_ArgTypes>(__arg)...);
1567 }
1568
1569 _LIBCPP_INLINE_VISIBILITY
1570 __alloc_func* __clone() const
1571 {
1572 typedef allocator_traits<_Alloc> __alloc_traits;
1573 typedef
1574 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1575 _AA;
1576 _AA __a(__f_.second());
1577 typedef __allocator_destructor<_AA> _Dp;
1578 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1579 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1580 return __hold.release();
1581 }
1582
1583 _LIBCPP_INLINE_VISIBILITY
1584 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001585
1586 static void __destroy_and_delete(__alloc_func* __f) {
1587 typedef allocator_traits<_Alloc> __alloc_traits;
1588 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1589 _FunAlloc;
1590 _FunAlloc __a(__f->__get_allocator());
1591 __f->destroy();
1592 __a.deallocate(__f, 1);
1593 }
1594};
1595
1596template <class _Fp, class _Rp, class... _ArgTypes>
1597class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1598 _Fp __f_;
1599
1600public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001601 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001602
1603 _LIBCPP_INLINE_VISIBILITY
1604 const _Target& __target() const { return __f_; }
1605
1606 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001607 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001608
1609 _LIBCPP_INLINE_VISIBILITY
1610 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1611
1612 _LIBCPP_INLINE_VISIBILITY
1613 _Rp operator()(_ArgTypes&&... __arg) {
1614 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1615 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1616 }
1617
1618 _LIBCPP_INLINE_VISIBILITY
1619 __default_alloc_func* __clone() const {
1620 __builtin_new_allocator::__holder_t __hold =
1621 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1622 __default_alloc_func* __res =
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001623 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00001624 (void)__hold.release();
1625 return __res;
1626 }
1627
1628 _LIBCPP_INLINE_VISIBILITY
1629 void destroy() _NOEXCEPT { __f_.~_Target(); }
1630
1631 static void __destroy_and_delete(__default_alloc_func* __f) {
1632 __f->destroy();
1633 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1634 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001635};
1636
1637// __base provides an abstract interface for copyable functors.
1638
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001639template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640
Howard Hinnantc834c512011-11-29 18:15:50 +00001641template<class _Rp, class ..._ArgTypes>
1642class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643{
1644 __base(const __base&);
1645 __base& operator=(const __base&);
1646public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001647 _LIBCPP_INLINE_VISIBILITY __base() {}
1648 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649 virtual __base* __clone() const = 0;
1650 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001651 virtual void destroy() _NOEXCEPT = 0;
1652 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001653 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001654#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001655 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1656 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001657#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658};
1659
Eric Fiselier125798e2018-12-10 18:14:09 +00001660// __func implements __base for a given functor type.
1661
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662template<class _FD, class _Alloc, class _FB> class __func;
1663
Howard Hinnantc834c512011-11-29 18:15:50 +00001664template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1665class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1666 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667{
Eric Fiselier125798e2018-12-10 18:14:09 +00001668 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001671 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001672 : __f_(_VSTD::move(__f)) {}
1673
Howard Hinnant4ff57432010-09-21 22:55:27 +00001674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001675 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001676 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001677
1678 _LIBCPP_INLINE_VISIBILITY
1679 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001680 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001681
1682 _LIBCPP_INLINE_VISIBILITY
1683 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001684 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1685
Howard Hinnantc834c512011-11-29 18:15:50 +00001686 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1687 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001688 virtual void destroy() _NOEXCEPT;
1689 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001690 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001691#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001692 virtual const void* target(const type_info&) const _NOEXCEPT;
1693 virtual const std::type_info& target_type() const _NOEXCEPT;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001694#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695};
1696
Howard Hinnantc834c512011-11-29 18:15:50 +00001697template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1698__base<_Rp(_ArgTypes...)>*
1699__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001701 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001702 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001703 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001704 typedef __allocator_destructor<_Ap> _Dp;
1705 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001706 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707 return __hold.release();
1708}
1709
Howard Hinnantc834c512011-11-29 18:15:50 +00001710template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711void
Howard Hinnantc834c512011-11-29 18:15:50 +00001712__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713{
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001714 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715}
1716
Howard Hinnantc834c512011-11-29 18:15:50 +00001717template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718void
Howard Hinnantc834c512011-11-29 18:15:50 +00001719__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720{
Eric Fiselier125798e2018-12-10 18:14:09 +00001721 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722}
1723
Howard Hinnantc834c512011-11-29 18:15:50 +00001724template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725void
Howard Hinnantc834c512011-11-29 18:15:50 +00001726__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001728 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001729 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001730 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001731 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732 __a.deallocate(this, 1);
1733}
1734
Howard Hinnantc834c512011-11-29 18:15:50 +00001735template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1736_Rp
1737__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738{
Eric Fiselier125798e2018-12-10 18:14:09 +00001739 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740}
1741
Howard Hinnant72f73582010-08-11 17:04:31 +00001742#ifndef _LIBCPP_NO_RTTI
1743
Howard Hinnantc834c512011-11-29 18:15:50 +00001744template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001746__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747{
Howard Hinnantc834c512011-11-29 18:15:50 +00001748 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001749 return &__f_.__target();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001750 return nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751}
1752
Howard Hinnantc834c512011-11-29 18:15:50 +00001753template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001755__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756{
Howard Hinnantc834c512011-11-29 18:15:50 +00001757 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758}
1759
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001760#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001761
Eric Fiselier125798e2018-12-10 18:14:09 +00001762// __value_func creates a value-type from a __func.
1763
1764template <class _Fp> class __value_func;
1765
1766template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1767{
1768 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1769
1770 typedef __base<_Rp(_ArgTypes...)> __func;
1771 __func* __f_;
1772
1773 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1774 {
1775 return reinterpret_cast<__func*>(p);
1776 }
1777
1778 public:
1779 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001780 __value_func() _NOEXCEPT : __f_(nullptr) {}
Eric Fiselier125798e2018-12-10 18:14:09 +00001781
1782 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001783 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001784 : __f_(nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001785 {
1786 typedef allocator_traits<_Alloc> __alloc_traits;
1787 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1788 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1789 _FunAlloc;
1790
1791 if (__function::__not_null(__f))
1792 {
1793 _FunAlloc __af(__a);
1794 if (sizeof(_Fun) <= sizeof(__buf_) &&
1795 is_nothrow_copy_constructible<_Fp>::value &&
1796 is_nothrow_copy_constructible<_FunAlloc>::value)
1797 {
1798 __f_ =
1799 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1800 }
1801 else
1802 {
1803 typedef __allocator_destructor<_FunAlloc> _Dp;
1804 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1805 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1806 __f_ = __hold.release();
1807 }
1808 }
1809 }
1810
Eric Fiselier74ebee62019-06-08 01:31:19 +00001811 template <class _Fp,
1812 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1813 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001814 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001815
Eric Fiselier125798e2018-12-10 18:14:09 +00001816 _LIBCPP_INLINE_VISIBILITY
1817 __value_func(const __value_func& __f)
1818 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001819 if (__f.__f_ == nullptr)
1820 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001821 else if ((void*)__f.__f_ == &__f.__buf_)
1822 {
1823 __f_ = __as_base(&__buf_);
1824 __f.__f_->__clone(__f_);
1825 }
1826 else
1827 __f_ = __f.__f_->__clone();
1828 }
1829
1830 _LIBCPP_INLINE_VISIBILITY
1831 __value_func(__value_func&& __f) _NOEXCEPT
1832 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001833 if (__f.__f_ == nullptr)
1834 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001835 else if ((void*)__f.__f_ == &__f.__buf_)
1836 {
1837 __f_ = __as_base(&__buf_);
1838 __f.__f_->__clone(__f_);
1839 }
1840 else
1841 {
1842 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001843 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001844 }
1845 }
1846
1847 _LIBCPP_INLINE_VISIBILITY
1848 ~__value_func()
1849 {
1850 if ((void*)__f_ == &__buf_)
1851 __f_->destroy();
1852 else if (__f_)
1853 __f_->destroy_deallocate();
1854 }
1855
1856 _LIBCPP_INLINE_VISIBILITY
1857 __value_func& operator=(__value_func&& __f)
1858 {
1859 *this = nullptr;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001860 if (__f.__f_ == nullptr)
1861 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001862 else if ((void*)__f.__f_ == &__f.__buf_)
1863 {
1864 __f_ = __as_base(&__buf_);
1865 __f.__f_->__clone(__f_);
1866 }
1867 else
1868 {
1869 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001870 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001871 }
1872 return *this;
1873 }
1874
1875 _LIBCPP_INLINE_VISIBILITY
1876 __value_func& operator=(nullptr_t)
1877 {
1878 __func* __f = __f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001879 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001880 if ((void*)__f == &__buf_)
1881 __f->destroy();
1882 else if (__f)
1883 __f->destroy_deallocate();
1884 return *this;
1885 }
1886
1887 _LIBCPP_INLINE_VISIBILITY
1888 _Rp operator()(_ArgTypes&&... __args) const
1889 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001890 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001891 __throw_bad_function_call();
1892 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1893 }
1894
1895 _LIBCPP_INLINE_VISIBILITY
1896 void swap(__value_func& __f) _NOEXCEPT
1897 {
1898 if (&__f == this)
1899 return;
1900 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1901 {
1902 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1903 __func* __t = __as_base(&__tempbuf);
1904 __f_->__clone(__t);
1905 __f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001906 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001907 __f.__f_->__clone(__as_base(&__buf_));
1908 __f.__f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001909 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001910 __f_ = __as_base(&__buf_);
1911 __t->__clone(__as_base(&__f.__buf_));
1912 __t->destroy();
1913 __f.__f_ = __as_base(&__f.__buf_);
1914 }
1915 else if ((void*)__f_ == &__buf_)
1916 {
1917 __f_->__clone(__as_base(&__f.__buf_));
1918 __f_->destroy();
1919 __f_ = __f.__f_;
1920 __f.__f_ = __as_base(&__f.__buf_);
1921 }
1922 else if ((void*)__f.__f_ == &__f.__buf_)
1923 {
1924 __f.__f_->__clone(__as_base(&__buf_));
1925 __f.__f_->destroy();
1926 __f.__f_ = __f_;
1927 __f_ = __as_base(&__buf_);
1928 }
1929 else
1930 _VSTD::swap(__f_, __f.__f_);
1931 }
1932
1933 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001934 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
Eric Fiselier125798e2018-12-10 18:14:09 +00001935
1936#ifndef _LIBCPP_NO_RTTI
1937 _LIBCPP_INLINE_VISIBILITY
1938 const std::type_info& target_type() const _NOEXCEPT
1939 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001940 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001941 return typeid(void);
1942 return __f_->target_type();
1943 }
1944
1945 template <typename _Tp>
1946 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1947 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001948 if (__f_ == nullptr)
1949 return nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001950 return (const _Tp*)__f_->target(typeid(_Tp));
1951 }
1952#endif // _LIBCPP_NO_RTTI
1953};
1954
Eric Fiselierf2e64362018-12-11 00:14:34 +00001955// Storage for a functor object, to be used with __policy to manage copy and
1956// destruction.
1957union __policy_storage
1958{
1959 mutable char __small[sizeof(void*) * 2];
1960 void* __large;
1961};
1962
1963// True if _Fun can safely be held in __policy_storage.__small.
1964template <typename _Fun>
1965struct __use_small_storage
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04001966 : public integral_constant<
Eric Fiselierf2e64362018-12-11 00:14:34 +00001967 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001968 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04001969 is_trivially_copy_constructible<_Fun>::value &&
1970 is_trivially_destructible<_Fun>::value> {};
Eric Fiselierf2e64362018-12-11 00:14:34 +00001971
1972// Policy contains information about how to copy, destroy, and move the
1973// underlying functor. You can think of it as a vtable of sorts.
1974struct __policy
1975{
1976 // Used to copy or destroy __large values. null for trivial objects.
1977 void* (*const __clone)(const void*);
1978 void (*const __destroy)(void*);
1979
1980 // True if this is the null policy (no value).
1981 const bool __is_null;
1982
1983 // The target type. May be null if RTTI is disabled.
1984 const std::type_info* const __type_info;
1985
1986 // Returns a pointer to a static policy object suitable for the functor
1987 // type.
1988 template <typename _Fun>
1989 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1990 {
1991 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1992 }
1993
1994 _LIBCPP_INLINE_VISIBILITY
1995 static const __policy* __create_empty()
1996 {
1997 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1998 true,
1999#ifndef _LIBCPP_NO_RTTI
2000 &typeid(void)
2001#else
2002 nullptr
2003#endif
2004 };
2005 return &__policy_;
2006 }
2007
2008 private:
2009 template <typename _Fun> static void* __large_clone(const void* __s)
2010 {
2011 const _Fun* __f = static_cast<const _Fun*>(__s);
2012 return __f->__clone();
2013 }
2014
Eric Fiselier74ebee62019-06-08 01:31:19 +00002015 template <typename _Fun>
2016 static void __large_destroy(void* __s) {
2017 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002018 }
2019
2020 template <typename _Fun>
2021 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002022 __choose_policy(/* is_small = */ false_type) {
2023 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2024 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002025#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002026 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002027#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002028 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002029#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002030 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002031 return &__policy_;
2032 }
2033
2034 template <typename _Fun>
2035 _LIBCPP_INLINE_VISIBILITY static const __policy*
2036 __choose_policy(/* is_small = */ true_type)
2037 {
2038 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2039 nullptr, nullptr, false,
2040#ifndef _LIBCPP_NO_RTTI
2041 &typeid(typename _Fun::_Target)
2042#else
2043 nullptr
2044#endif
2045 };
2046 return &__policy_;
2047 }
2048};
2049
2050// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2051// faster for types that can be passed in registers.
2052template <typename _Tp>
2053using __fast_forward =
Mark de Wever9d7aa7a2021-05-09 18:22:52 +02002054 typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002055
2056// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2057
2058template <class _Fp> struct __policy_invoker;
2059
2060template <class _Rp, class... _ArgTypes>
2061struct __policy_invoker<_Rp(_ArgTypes...)>
2062{
2063 typedef _Rp (*__Call)(const __policy_storage*,
2064 __fast_forward<_ArgTypes>...);
2065
2066 __Call __call_;
2067
2068 // Creates an invoker that throws bad_function_call.
2069 _LIBCPP_INLINE_VISIBILITY
2070 __policy_invoker() : __call_(&__call_empty) {}
2071
2072 // Creates an invoker that calls the given instance of __func.
2073 template <typename _Fun>
2074 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2075 {
2076 return __policy_invoker(&__call_impl<_Fun>);
2077 }
2078
2079 private:
2080 _LIBCPP_INLINE_VISIBILITY
2081 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2082
2083 static _Rp __call_empty(const __policy_storage*,
2084 __fast_forward<_ArgTypes>...)
2085 {
2086 __throw_bad_function_call();
2087 }
2088
2089 template <typename _Fun>
2090 static _Rp __call_impl(const __policy_storage* __buf,
2091 __fast_forward<_ArgTypes>... __args)
2092 {
2093 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2094 ? &__buf->__small
2095 : __buf->__large);
2096 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2097 }
2098};
2099
2100// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2101// copyable functor.
2102
2103template <class _Fp> class __policy_func;
2104
2105template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2106{
2107 // Inline storage for small objects.
2108 __policy_storage __buf_;
2109
2110 // Calls the value stored in __buf_. This could technically be part of
2111 // policy, but storing it here eliminates a level of indirection inside
2112 // operator().
2113 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2114 __invoker __invoker_;
2115
2116 // The policy that describes how to move / copy / destroy __buf_. Never
2117 // null, even if the function is empty.
2118 const __policy* __policy_;
2119
2120 public:
2121 _LIBCPP_INLINE_VISIBILITY
2122 __policy_func() : __policy_(__policy::__create_empty()) {}
2123
2124 template <class _Fp, class _Alloc>
2125 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2126 : __policy_(__policy::__create_empty())
2127 {
2128 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2129 typedef allocator_traits<_Alloc> __alloc_traits;
2130 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2131 _FunAlloc;
2132
2133 if (__function::__not_null(__f))
2134 {
2135 __invoker_ = __invoker::template __create<_Fun>();
2136 __policy_ = __policy::__create<_Fun>();
2137
2138 _FunAlloc __af(__a);
2139 if (__use_small_storage<_Fun>())
2140 {
2141 ::new ((void*)&__buf_.__small)
2142 _Fun(_VSTD::move(__f), _Alloc(__af));
2143 }
2144 else
2145 {
2146 typedef __allocator_destructor<_FunAlloc> _Dp;
2147 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2148 ::new ((void*)__hold.get())
2149 _Fun(_VSTD::move(__f), _Alloc(__af));
2150 __buf_.__large = __hold.release();
2151 }
2152 }
2153 }
2154
Eric Fiselier74ebee62019-06-08 01:31:19 +00002155 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2156 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2157 : __policy_(__policy::__create_empty()) {
2158 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2159
2160 if (__function::__not_null(__f)) {
2161 __invoker_ = __invoker::template __create<_Fun>();
2162 __policy_ = __policy::__create<_Fun>();
2163 if (__use_small_storage<_Fun>()) {
2164 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2165 } else {
2166 __builtin_new_allocator::__holder_t __hold =
2167 __builtin_new_allocator::__allocate_type<_Fun>(1);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002168 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
Eric Fiselier74ebee62019-06-08 01:31:19 +00002169 (void)__hold.release();
2170 }
2171 }
2172 }
2173
Eric Fiselierf2e64362018-12-11 00:14:34 +00002174 _LIBCPP_INLINE_VISIBILITY
2175 __policy_func(const __policy_func& __f)
2176 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2177 __policy_(__f.__policy_)
2178 {
2179 if (__policy_->__clone)
2180 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2181 }
2182
2183 _LIBCPP_INLINE_VISIBILITY
2184 __policy_func(__policy_func&& __f)
2185 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2186 __policy_(__f.__policy_)
2187 {
2188 if (__policy_->__destroy)
2189 {
2190 __f.__policy_ = __policy::__create_empty();
2191 __f.__invoker_ = __invoker();
2192 }
2193 }
2194
2195 _LIBCPP_INLINE_VISIBILITY
2196 ~__policy_func()
2197 {
2198 if (__policy_->__destroy)
2199 __policy_->__destroy(__buf_.__large);
2200 }
2201
2202 _LIBCPP_INLINE_VISIBILITY
2203 __policy_func& operator=(__policy_func&& __f)
2204 {
2205 *this = nullptr;
2206 __buf_ = __f.__buf_;
2207 __invoker_ = __f.__invoker_;
2208 __policy_ = __f.__policy_;
2209 __f.__policy_ = __policy::__create_empty();
2210 __f.__invoker_ = __invoker();
2211 return *this;
2212 }
2213
2214 _LIBCPP_INLINE_VISIBILITY
2215 __policy_func& operator=(nullptr_t)
2216 {
2217 const __policy* __p = __policy_;
2218 __policy_ = __policy::__create_empty();
2219 __invoker_ = __invoker();
2220 if (__p->__destroy)
2221 __p->__destroy(__buf_.__large);
2222 return *this;
2223 }
2224
2225 _LIBCPP_INLINE_VISIBILITY
2226 _Rp operator()(_ArgTypes&&... __args) const
2227 {
2228 return __invoker_.__call_(_VSTD::addressof(__buf_),
2229 _VSTD::forward<_ArgTypes>(__args)...);
2230 }
2231
2232 _LIBCPP_INLINE_VISIBILITY
2233 void swap(__policy_func& __f)
2234 {
2235 _VSTD::swap(__invoker_, __f.__invoker_);
2236 _VSTD::swap(__policy_, __f.__policy_);
2237 _VSTD::swap(__buf_, __f.__buf_);
2238 }
2239
2240 _LIBCPP_INLINE_VISIBILITY
2241 explicit operator bool() const _NOEXCEPT
2242 {
2243 return !__policy_->__is_null;
2244 }
2245
2246#ifndef _LIBCPP_NO_RTTI
2247 _LIBCPP_INLINE_VISIBILITY
2248 const std::type_info& target_type() const _NOEXCEPT
2249 {
2250 return *__policy_->__type_info;
2251 }
2252
2253 template <typename _Tp>
2254 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2255 {
2256 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2257 return nullptr;
2258 if (__policy_->__clone) // Out of line storage.
2259 return reinterpret_cast<const _Tp*>(__buf_.__large);
2260 else
2261 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2262 }
2263#endif // _LIBCPP_NO_RTTI
2264};
2265
Louis Dionne3a632922020-04-23 16:47:52 -04002266#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
Louis Dionnee2391d72020-04-22 13:58:17 -04002267
Louis Dionne91cf4442020-07-31 12:56:36 -04002268extern "C" void *_Block_copy(const void *);
2269extern "C" void _Block_release(const void *);
2270
Louis Dionnee2391d72020-04-22 13:58:17 -04002271template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2272class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2273 : public __base<_Rp(_ArgTypes...)>
2274{
2275 typedef _Rp1(^__block_type)(_ArgTypes1...);
2276 __block_type __f_;
2277
2278public:
2279 _LIBCPP_INLINE_VISIBILITY
2280 explicit __func(__block_type const& __f)
Louis Dionne91cf4442020-07-31 12:56:36 -04002281 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002282 { }
2283
2284 // [TODO] add && to save on a retain
2285
2286 _LIBCPP_INLINE_VISIBILITY
2287 explicit __func(__block_type __f, const _Alloc& /* unused */)
Louis Dionne91cf4442020-07-31 12:56:36 -04002288 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002289 { }
2290
2291 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2292 _LIBCPP_ASSERT(false,
2293 "Block pointers are just pointers, so they should always fit into "
2294 "std::function's small buffer optimization. This function should "
2295 "never be invoked.");
2296 return nullptr;
2297 }
2298
2299 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002300 ::new ((void*)__p) __func(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002301 }
2302
2303 virtual void destroy() _NOEXCEPT {
2304 if (__f_)
Louis Dionne91cf4442020-07-31 12:56:36 -04002305 _Block_release(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002306 __f_ = 0;
2307 }
2308
2309 virtual void destroy_deallocate() _NOEXCEPT {
2310 _LIBCPP_ASSERT(false,
2311 "Block pointers are just pointers, so they should always fit into "
2312 "std::function's small buffer optimization. This function should "
2313 "never be invoked.");
2314 }
2315
2316 virtual _Rp operator()(_ArgTypes&& ... __arg) {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05002317 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
Louis Dionnee2391d72020-04-22 13:58:17 -04002318 }
2319
2320#ifndef _LIBCPP_NO_RTTI
2321 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2322 if (__ti == typeid(__func::__block_type))
2323 return &__f_;
2324 return (const void*)nullptr;
2325 }
2326
2327 virtual const std::type_info& target_type() const _NOEXCEPT {
2328 return typeid(__func::__block_type);
2329 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002330#endif // _LIBCPP_NO_RTTI
Louis Dionnee2391d72020-04-22 13:58:17 -04002331};
2332
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002333#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
Louis Dionnee2391d72020-04-22 13:58:17 -04002334
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335} // __function
2336
Howard Hinnantc834c512011-11-29 18:15:50 +00002337template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002338class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002339 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2340 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002342#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002343 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002344#else
2345 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2346#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347
Eric Fiselier125798e2018-12-10 18:14:09 +00002348 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002349
Eric Fiselier3906a132019-06-23 20:28:29 +00002350 template <class _Fp, bool = _And<
2351 _IsNotSame<__uncvref_t<_Fp>, function>,
zoecarver3e722f22020-05-19 17:15:28 -07002352 __invokable<_Fp, _ArgTypes...>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002353 >::value>
2354 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002355 template <class _Fp>
2356 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002357 {
Arthur O'Dwyer4ba8e3d2021-01-11 16:29:17 -05002358 static const bool value = is_void<_Rp>::value ||
2359 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
2360 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002361 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002362 template <class _Fp>
2363 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002364 {
2365 static const bool value = false;
2366 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002367
2368 template <class _Fp>
zoecarver3e722f22020-05-19 17:15:28 -07002369 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002371 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372
Howard Hinnantf06d9262010-08-20 19:36:46 +00002373 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002374 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002375 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002376 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002377 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002379 function(function&&) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002380 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002381 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382
Marshall Clow3148f422016-10-13 21:06:03 +00002383#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002384 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002385 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002386 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002387 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002388 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002389 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002390 template<class _Alloc>
2391 function(allocator_arg_t, const _Alloc&, const function&);
2392 template<class _Alloc>
2393 function(allocator_arg_t, const _Alloc&, function&&);
zoecarver3e722f22020-05-19 17:15:28 -07002394 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002395 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002396#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397
2398 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002399 function& operator=(function&&) _NOEXCEPT;
2400 function& operator=(nullptr_t) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002401 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002402 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403
2404 ~function();
2405
Howard Hinnantf06d9262010-08-20 19:36:46 +00002406 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002407 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002408
2409#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002410 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002412 void assign(_Fp&& __f, const _Alloc& __a)
2413 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002414#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415
Howard Hinnantf06d9262010-08-20 19:36:46 +00002416 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002417 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002418 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2419 return static_cast<bool>(__f_);
2420 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422 // deleted overloads close possible hole in the type system
2423 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002424 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002426 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002428 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002429 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430
Howard Hinnant72f73582010-08-11 17:04:31 +00002431#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002432 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002433 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002434 template <typename _Tp> _Tp* target() _NOEXCEPT;
2435 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002436#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437};
2438
Louis Dionne4af49712019-07-18 19:50:56 +00002439#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2440template<class _Rp, class ..._Ap>
2441function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2442
2443template<class _Fp>
2444struct __strip_signature;
2445
2446template<class _Rp, class _Gp, class ..._Ap>
2447struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2448template<class _Rp, class _Gp, class ..._Ap>
2449struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2450template<class _Rp, class _Gp, class ..._Ap>
2451struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2452template<class _Rp, class _Gp, class ..._Ap>
2453struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2454
2455template<class _Rp, class _Gp, class ..._Ap>
2456struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2457template<class _Rp, class _Gp, class ..._Ap>
2458struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2459template<class _Rp, class _Gp, class ..._Ap>
2460struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2461template<class _Rp, class _Gp, class ..._Ap>
2462struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2463
2464template<class _Rp, class _Gp, class ..._Ap>
2465struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2466template<class _Rp, class _Gp, class ..._Ap>
2467struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2468template<class _Rp, class _Gp, class ..._Ap>
2469struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2470template<class _Rp, class _Gp, class ..._Ap>
2471struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2472
2473template<class _Rp, class _Gp, class ..._Ap>
2474struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2475template<class _Rp, class _Gp, class ..._Ap>
2476struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2477template<class _Rp, class _Gp, class ..._Ap>
2478struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2479template<class _Rp, class _Gp, class ..._Ap>
2480struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2481
2482template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2483function(_Fp) -> function<_Stripped>;
2484#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2485
Howard Hinnantc834c512011-11-29 18:15:50 +00002486template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002487function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488
Marshall Clow3148f422016-10-13 21:06:03 +00002489#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002490template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002491template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002492function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002493 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002494#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002495
Eric Fiselier125798e2018-12-10 18:14:09 +00002496template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002497function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002498 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499
Marshall Clow3148f422016-10-13 21:06:03 +00002500#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002501template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002502template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002503function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002504 function&& __f)
2505 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002506#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002507
Eric Fiselier125798e2018-12-10 18:14:09 +00002508template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002509template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002510function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511
Marshall Clow3148f422016-10-13 21:06:03 +00002512#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002513template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002514template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002515function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2516 _Fp __f)
2517 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002518#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002519
Howard Hinnantc834c512011-11-29 18:15:50 +00002520template<class _Rp, class ..._ArgTypes>
2521function<_Rp(_ArgTypes...)>&
2522function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523{
2524 function(__f).swap(*this);
2525 return *this;
2526}
2527
Howard Hinnantc834c512011-11-29 18:15:50 +00002528template<class _Rp, class ..._ArgTypes>
2529function<_Rp(_ArgTypes...)>&
2530function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002532 __f_ = _VSTD::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002533 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534}
2535
Howard Hinnantc834c512011-11-29 18:15:50 +00002536template<class _Rp, class ..._ArgTypes>
2537function<_Rp(_ArgTypes...)>&
2538function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539{
Eric Fiselier125798e2018-12-10 18:14:09 +00002540 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002541 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002542}
2543
Howard Hinnantc834c512011-11-29 18:15:50 +00002544template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002545template <class _Fp, class>
2546function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002547function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548{
Howard Hinnantc834c512011-11-29 18:15:50 +00002549 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550 return *this;
2551}
2552
Howard Hinnantc834c512011-11-29 18:15:50 +00002553template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002554function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555
Howard Hinnantc834c512011-11-29 18:15:50 +00002556template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557void
Howard Hinnantc834c512011-11-29 18:15:50 +00002558function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559{
Eric Fiselier125798e2018-12-10 18:14:09 +00002560 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561}
2562
Howard Hinnantc834c512011-11-29 18:15:50 +00002563template<class _Rp, class ..._ArgTypes>
2564_Rp
2565function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566{
Eric Fiselier125798e2018-12-10 18:14:09 +00002567 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568}
2569
Howard Hinnant72f73582010-08-11 17:04:31 +00002570#ifndef _LIBCPP_NO_RTTI
2571
Howard Hinnantc834c512011-11-29 18:15:50 +00002572template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002574function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575{
Eric Fiselier125798e2018-12-10 18:14:09 +00002576 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577}
2578
Howard Hinnantc834c512011-11-29 18:15:50 +00002579template<class _Rp, class ..._ArgTypes>
2580template <typename _Tp>
2581_Tp*
2582function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583{
Eric Fiselier125798e2018-12-10 18:14:09 +00002584 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585}
2586
Howard Hinnantc834c512011-11-29 18:15:50 +00002587template<class _Rp, class ..._ArgTypes>
2588template <typename _Tp>
2589const _Tp*
2590function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591{
Eric Fiselier125798e2018-12-10 18:14:09 +00002592 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593}
2594
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002595#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002596
Howard Hinnantc834c512011-11-29 18:15:50 +00002597template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598inline _LIBCPP_INLINE_VISIBILITY
2599bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002600operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601
Howard Hinnantc834c512011-11-29 18:15:50 +00002602template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603inline _LIBCPP_INLINE_VISIBILITY
2604bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002605operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606
Howard Hinnantc834c512011-11-29 18:15:50 +00002607template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608inline _LIBCPP_INLINE_VISIBILITY
2609bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002610operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611
Howard Hinnantc834c512011-11-29 18:15:50 +00002612template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613inline _LIBCPP_INLINE_VISIBILITY
2614bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002615operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616
Howard Hinnantc834c512011-11-29 18:15:50 +00002617template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618inline _LIBCPP_INLINE_VISIBILITY
2619void
Howard Hinnantc834c512011-11-29 18:15:50 +00002620swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621{return __x.swap(__y);}
2622
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002623#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002624
2625#include <__functional_03>
2626
2627#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002628
2629////////////////////////////////////////////////////////////////////////////////
2630// BIND
2631//==============================================================================
2632
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002634template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2636
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002637#if _LIBCPP_STD_VER > 14
2638template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002639_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002640#endif
2641
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002643template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2645
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002646#if _LIBCPP_STD_VER > 14
2647template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002648_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002649#endif
2650
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651namespace placeholders
2652{
2653
Howard Hinnantc834c512011-11-29 18:15:50 +00002654template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002656#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002657_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2658_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2659_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2660_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2661_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2662_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2663_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2664_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2665_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2666_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2667#else
Marshall Clow396b2132018-01-02 19:01:45 +00002668/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2669/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2670/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2671/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2672/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2673/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2674/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2675/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2676/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2677/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002678#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679
2680} // placeholders
2681
Howard Hinnantc834c512011-11-29 18:15:50 +00002682template<int _Np>
2683struct __is_placeholder<placeholders::__ph<_Np> >
2684 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002686
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002687#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002688
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689template <class _Tp, class _Uj>
2690inline _LIBCPP_INLINE_VISIBILITY
2691_Tp&
2692__mu(reference_wrapper<_Tp> __t, _Uj&)
2693{
2694 return __t.get();
2695}
2696
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697template <class _Ti, class ..._Uj, size_t ..._Indx>
2698inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002699typename __invoke_of<_Ti&, _Uj...>::type
2700__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002702 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703}
2704
2705template <class _Ti, class ..._Uj>
2706inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002707typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708<
2709 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002710 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711>::type
2712__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2713{
2714 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002715 return _VSTD::__mu_expand(__ti, __uj, __indices());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716}
2717
2718template <bool IsPh, class _Ti, class _Uj>
2719struct __mu_return2 {};
2720
2721template <class _Ti, class _Uj>
2722struct __mu_return2<true, _Ti, _Uj>
2723{
2724 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2725};
2726
2727template <class _Ti, class _Uj>
2728inline _LIBCPP_INLINE_VISIBILITY
2729typename enable_if
2730<
2731 0 < is_placeholder<_Ti>::value,
2732 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2733>::type
2734__mu(_Ti&, _Uj& __uj)
2735{
2736 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002737 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738}
2739
2740template <class _Ti, class _Uj>
2741inline _LIBCPP_INLINE_VISIBILITY
2742typename enable_if
2743<
2744 !is_bind_expression<_Ti>::value &&
2745 is_placeholder<_Ti>::value == 0 &&
2746 !__is_reference_wrapper<_Ti>::value,
2747 _Ti&
2748>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002749__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750{
2751 return __ti;
2752}
2753
Howard Hinnant0415d792011-05-22 15:07:43 +00002754template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2755 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002756struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002758template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002759struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002760{
2761 typedef __nat type;
2762};
2763
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002765struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002767 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768};
2769
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002770template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002771struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2772 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002773{
2774};
2775
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002777struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778{
2779 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2780 _TupleUj>::type&& type;
2781};
2782
2783template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002784struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002785{
2786 typedef typename _Ti::type& type;
2787};
2788
2789template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002790struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791{
2792 typedef _Ti& type;
2793};
2794
2795template <class _Ti, class _TupleUj>
2796struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002797 : public __mu_return_impl<_Ti,
2798 __is_reference_wrapper<_Ti>::value,
2799 is_bind_expression<_Ti>::value,
2800 0 < is_placeholder<_Ti>::value &&
2801 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2802 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803{
2804};
2805
Howard Hinnantc834c512011-11-29 18:15:50 +00002806template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002807struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002808{
2809 static const bool value = false;
2810};
2811
2812template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002813struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002814{
2815 static const bool value = __invokable<_Fp,
2816 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2817};
2818
2819template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002820struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002821{
2822 static const bool value = __invokable<_Fp,
2823 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2824};
2825
2826template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002827 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828struct __bind_return;
2829
Howard Hinnantc834c512011-11-29 18:15:50 +00002830template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002831struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002833 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002835 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 typename __mu_return
2837 <
2838 _BoundArgs,
2839 _TupleUj
2840 >::type...
2841 >::type type;
2842};
2843
Howard Hinnantc834c512011-11-29 18:15:50 +00002844template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002845struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002847 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002849 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 typename __mu_return
2851 <
2852 const _BoundArgs,
2853 _TupleUj
2854 >::type...
2855 >::type type;
2856};
2857
Howard Hinnantc834c512011-11-29 18:15:50 +00002858template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002860typename __bind_return<_Fp, _BoundArgs, _Args>::type
2861__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862 _Args&& __args)
2863{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002864 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865}
2866
Howard Hinnantc834c512011-11-29 18:15:50 +00002867template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002869 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002871protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002872 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002873 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002874private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002875 _Fd __f_;
2876 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877
2878 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2879public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002880 template <class _Gp, class ..._BA,
2881 class = typename enable_if
2882 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002883 is_constructible<_Fd, _Gp>::value &&
2884 !is_same<typename remove_reference<_Gp>::type,
2885 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002886 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002887 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002888 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2889 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002890 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891
2892 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002893 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002894 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002895 operator()(_Args&& ...__args)
2896 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002897 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002898 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899 }
2900
2901 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002902 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002903 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 operator()(_Args&& ...__args) const
2905 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002906 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002907 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 }
2909};
2910
Howard Hinnantc834c512011-11-29 18:15:50 +00002911template<class _Fp, class ..._BoundArgs>
2912struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913
Howard Hinnantc834c512011-11-29 18:15:50 +00002914template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002916 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917{
Howard Hinnantc834c512011-11-29 18:15:50 +00002918 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002919 typedef typename base::_Fd _Fd;
2920 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002922 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923
Howard Hinnant7091e652011-07-02 18:22:36 +00002924
Howard Hinnantf292a922013-07-01 00:01:51 +00002925 template <class _Gp, class ..._BA,
2926 class = typename enable_if
2927 <
2928 is_constructible<_Fd, _Gp>::value &&
2929 !is_same<typename remove_reference<_Gp>::type,
2930 __bind_r>::value
2931 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002932 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002933 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2934 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002935 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936
2937 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002938 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002939 typename enable_if
2940 <
2941 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002942 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002943 result_type
2944 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 operator()(_Args&& ...__args)
2946 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002947 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2948 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949 }
2950
2951 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002952 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002953 typename enable_if
2954 <
2955 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002956 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002957 result_type
2958 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959 operator()(_Args&& ...__args) const
2960 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002961 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2962 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 }
2964};
2965
Howard Hinnantc834c512011-11-29 18:15:50 +00002966template<class _Rp, class _Fp, class ..._BoundArgs>
2967struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968
Howard Hinnantc834c512011-11-29 18:15:50 +00002969template<class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002970inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002971__bind<_Fp, _BoundArgs...>
2972bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973{
Howard Hinnantc834c512011-11-29 18:15:50 +00002974 typedef __bind<_Fp, _BoundArgs...> type;
2975 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976}
2977
Howard Hinnantc834c512011-11-29 18:15:50 +00002978template<class _Rp, class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002979inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002980__bind_r<_Rp, _Fp, _BoundArgs...>
2981bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982{
Howard Hinnantc834c512011-11-29 18:15:50 +00002983 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2984 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985}
2986
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002987#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988
Eric Fiselier0d974f12015-07-14 20:16:15 +00002989#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002990
zoecarver3595cac2021-03-02 16:17:22 -08002991template<class _Op, class _Tuple,
2992 class _Idxs = typename __make_tuple_indices<tuple_size<_Tuple>::value>::type>
2993struct __perfect_forward_impl;
Eric Fiselier934f63b2016-06-02 01:25:41 +00002994
zoecarver3595cac2021-03-02 16:17:22 -08002995template<class _Op, class... _Bound, size_t... _Idxs>
2996struct __perfect_forward_impl<_Op, __tuple_types<_Bound...>, __tuple_indices<_Idxs...>>
2997{
2998 tuple<_Bound...> __bound_;
Eric Fiselier934f63b2016-06-02 01:25:41 +00002999
zoecarver3595cac2021-03-02 16:17:22 -08003000 template<class... _Args>
3001 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &
3002 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3003 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3004 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003005
zoecarver3595cac2021-03-02 16:17:22 -08003006 template<class... _Args>
3007 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&
3008 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3009 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3010 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003011
zoecarver3595cac2021-03-02 16:17:22 -08003012 template<class... _Args>
3013 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &&
3014 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3015 _VSTD::forward<_Args>(__args)...)))
3016 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3017 _VSTD::forward<_Args>(__args)...))
3018 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3019 _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003020
zoecarver3595cac2021-03-02 16:17:22 -08003021 template<class... _Args>
3022 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&&
3023 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3024 _VSTD::forward<_Args>(__args)...)))
3025 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3026 _VSTD::forward<_Args>(__args)...))
3027 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3028 _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003029
zoecarver3595cac2021-03-02 16:17:22 -08003030 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3031 class = _EnableIf<is_copy_constructible_v<_Fn>>>
3032 constexpr __perfect_forward_impl(__perfect_forward_impl const& __other)
3033 : __bound_(__other.__bound_) {}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003034
zoecarver3595cac2021-03-02 16:17:22 -08003035 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3036 class = _EnableIf<is_move_constructible_v<_Fn>>>
3037 constexpr __perfect_forward_impl(__perfect_forward_impl && __other)
3038 : __bound_(_VSTD::move(__other.__bound_)) {}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003039
zoecarver3595cac2021-03-02 16:17:22 -08003040 template<class... _BoundArgs>
3041 explicit constexpr __perfect_forward_impl(_BoundArgs&&... __bound) :
3042 __bound_(_VSTD::forward<_BoundArgs>(__bound)...) { }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003043};
3044
zoecarver3595cac2021-03-02 16:17:22 -08003045template<class _Op, class... _Args>
3046using __perfect_forward =
3047 __perfect_forward_impl<_Op, __tuple_types<decay_t<_Args>...>>;
3048
3049struct __not_fn_op
3050{
3051 template<class... _Args>
3052 static _LIBCPP_CONSTEXPR_AFTER_CXX17 auto __call(_Args&&... __args)
3053 noexcept(noexcept(!_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3054 -> decltype( !_VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3055 { return !_VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3056};
3057
3058template<class _Fn,
3059 class = _EnableIf<is_constructible_v<decay_t<_Fn>, _Fn> &&
3060 is_move_constructible_v<_Fn>>>
3061_LIBCPP_CONSTEXPR_AFTER_CXX17 auto not_fn(_Fn&& __f)
3062{
3063 return __perfect_forward<__not_fn_op, _Fn>(_VSTD::forward<_Fn>(__f));
Eric Fiselier934f63b2016-06-02 01:25:41 +00003064}
3065
zoecarver3595cac2021-03-02 16:17:22 -08003066#endif // _LIBCPP_STD_VER > 14
3067
3068#if _LIBCPP_STD_VER > 17
3069
3070struct __bind_front_op
3071{
3072 template<class... _Args>
3073 constexpr static auto __call(_Args&&... __args)
3074 noexcept(noexcept(_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3075 -> decltype( _VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3076 { return _VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3077};
3078
3079template<class _Fn, class... _Args,
3080 class = _EnableIf<conjunction<is_constructible<decay_t<_Fn>, _Fn>,
3081 is_move_constructible<decay_t<_Fn>>,
3082 is_constructible<decay_t<_Args>, _Args>...,
3083 is_move_constructible<decay_t<_Args>>...
3084 >::value>>
3085constexpr auto bind_front(_Fn&& __f, _Args&&... __args)
3086{
3087 return __perfect_forward<__bind_front_op, _Fn, _Args...>(_VSTD::forward<_Fn>(__f),
3088 _VSTD::forward<_Args>(__args)...);
3089}
3090
3091#endif // _LIBCPP_STD_VER > 17
Eric Fiselier0d974f12015-07-14 20:16:15 +00003092
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003093// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094
Marshall Clowa40686b2018-01-08 19:18:00 +00003095template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00003096pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00003097__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3098 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
3099 forward_iterator_tag, forward_iterator_tag)
3100{
3101 if (__first2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003102 return _VSTD::make_pair(__first1, __first1); // Everything matches an empty sequence
Marshall Clowa40686b2018-01-08 19:18:00 +00003103 while (true)
3104 {
3105 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
3106 while (true)
3107 {
3108 if (__first1 == __last1) // return __last1 if no element matches *__first2
Logan Smith4528b5f2020-05-07 11:54:25 -04003109 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003110 if (__pred(*__first1, *__first2))
3111 break;
3112 ++__first1;
3113 }
3114 // *__first1 matches *__first2, now match elements after here
3115 _ForwardIterator1 __m1 = __first1;
3116 _ForwardIterator2 __m2 = __first2;
3117 while (true)
3118 {
3119 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Logan Smith4528b5f2020-05-07 11:54:25 -04003120 return _VSTD::make_pair(__first1, __m1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003121 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Logan Smith4528b5f2020-05-07 11:54:25 -04003122 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003123 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
3124 {
3125 ++__first1;
3126 break;
3127 } // else there is a match, check next elements
3128 }
3129 }
3130}
3131
3132template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
3133_LIBCPP_CONSTEXPR_AFTER_CXX11
3134pair<_RandomAccessIterator1, _RandomAccessIterator1>
3135__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
3136 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
3137 random_access_iterator_tag, random_access_iterator_tag)
3138{
3139 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
3140 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
3141 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
3142 const _D2 __len2 = __last2 - __first2;
3143 if (__len2 == 0)
Logan Smith4528b5f2020-05-07 11:54:25 -04003144 return _VSTD::make_pair(__first1, __first1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003145 const _D1 __len1 = __last1 - __first1;
3146 if (__len1 < __len2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003147 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003148 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
3149
3150 while (true)
3151 {
3152 while (true)
3153 {
3154 if (__first1 == __s)
Logan Smith4528b5f2020-05-07 11:54:25 -04003155 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003156 if (__pred(*__first1, *__first2))
3157 break;
3158 ++__first1;
3159 }
3160
3161 _RandomAccessIterator1 __m1 = __first1;
3162 _RandomAccessIterator2 __m2 = __first2;
3163 while (true)
3164 {
3165 if (++__m2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003166 return _VSTD::make_pair(__first1, __first1 + __len2);
Marshall Clowa40686b2018-01-08 19:18:00 +00003167 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
3168 if (!__pred(*__m1, *__m2))
3169 {
3170 ++__first1;
3171 break;
3172 }
3173 }
3174 }
3175}
3176
3177#if _LIBCPP_STD_VER > 14
3178
3179// default searcher
3180template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Martin Storsjö4d6f2212021-04-06 10:55:33 +03003181class _LIBCPP_TEMPLATE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00003182public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003183 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne44bcff92018-08-03 22:36:53 +00003184 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00003185 _BinaryPredicate __p = _BinaryPredicate())
3186 : __first_(__f), __last_(__l), __pred_(__p) {}
3187
3188 template <typename _ForwardIterator2>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003189 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00003190 pair<_ForwardIterator2, _ForwardIterator2>
3191 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3192 {
3193 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04003194 typename iterator_traits<_ForwardIterator>::iterator_category(),
3195 typename iterator_traits<_ForwardIterator2>::iterator_category());
Marshall Clowa40686b2018-01-08 19:18:00 +00003196 }
3197
3198private:
3199 _ForwardIterator __first_;
3200 _ForwardIterator __last_;
3201 _BinaryPredicate __pred_;
Eric Fiselier80663c52019-08-04 07:13:43 +00003202 };
Marshall Clowa40686b2018-01-08 19:18:00 +00003203
3204#endif // _LIBCPP_STD_VER > 14
3205
Louis Dionnedb1892a2018-12-03 14:03:27 +00003206#if _LIBCPP_STD_VER > 17
3207template <class _Tp>
3208using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3209
3210template <class _Tp>
3211using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3212#endif // > C++17
3213
Christopher Di Bellae68ec582021-03-18 17:21:35 +00003214#if _LIBCPP_STD_VER > 17
3215// [func.identity]
3216struct identity {
3217 template<class _Tp>
Arthur O'Dwyer108facb2021-04-05 14:56:03 -04003218 _LIBCPP_NODISCARD_EXT constexpr _Tp&& operator()(_Tp&& __t) const noexcept
Christopher Di Bellae68ec582021-03-18 17:21:35 +00003219 {
3220 return _VSTD::forward<_Tp>(__t);
3221 }
3222
3223 using is_transparent = void;
3224};
3225#endif // _LIBCPP_STD_VER > 17
3226
zoecarver9ce102c2021-04-22 17:33:04 -07003227#if !defined(_LIBCPP_HAS_NO_RANGES)
3228
3229namespace ranges {
3230
3231struct equal_to {
3232 template <class _Tp, class _Up>
3233 requires equality_comparable_with<_Tp, _Up>
3234 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3235 noexcept(noexcept(bool(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u)))) {
3236 return _VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u);
3237 }
3238
3239 using is_transparent = void;
3240};
3241
3242struct not_equal_to {
3243 template <class _Tp, class _Up>
3244 requires equality_comparable_with<_Tp, _Up>
3245 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3246 noexcept(noexcept(bool(!(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u))))) {
3247 return !(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u));
3248 }
3249
3250 using is_transparent = void;
3251};
3252
3253struct greater {
3254 template <class _Tp, class _Up>
3255 requires totally_ordered_with<_Tp, _Up>
3256 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3257 noexcept(noexcept(bool(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t)))) {
3258 return _VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t);
3259 }
3260
3261 using is_transparent = void;
3262};
3263
3264struct less {
3265 template <class _Tp, class _Up>
3266 requires totally_ordered_with<_Tp, _Up>
3267 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3268 noexcept(noexcept(bool(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u)))) {
3269 return _VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u);
3270 }
3271
3272 using is_transparent = void;
3273};
3274
3275struct greater_equal {
3276 template <class _Tp, class _Up>
3277 requires totally_ordered_with<_Tp, _Up>
3278 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3279 noexcept(noexcept(bool(!(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u))))) {
3280 return !(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u));
3281 }
3282
3283 using is_transparent = void;
3284};
3285
3286struct less_equal {
3287 template <class _Tp, class _Up>
3288 requires totally_ordered_with<_Tp, _Up>
3289 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3290 noexcept(noexcept(bool(!(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t))))) {
3291 return !(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t));
3292 }
3293
3294 using is_transparent = void;
3295};
3296
3297} // namespace ranges
3298
3299#endif // !defined(_LIBCPP_HAS_NO_RANGES)
3300
Howard Hinnantc51e1022010-05-11 19:42:16 +00003301_LIBCPP_END_NAMESPACE_STD
3302
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003303#endif // _LIBCPP_FUNCTIONAL