blob: 1eab545bf8535d7fd93415830dbc5277106e59eb [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>
zoecarver9ce102c2021-04-22 17:33:04 -0700510#include <concepts>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000511#include <type_traits>
512#include <typeinfo>
513#include <exception>
514#include <memory>
515#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000516#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000517#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518
519#include <__functional_base>
520
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000521#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000523#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524
525_LIBCPP_BEGIN_NAMESPACE_STD
526
Marshall Clow974bae22013-07-29 14:21:53 +0000527#if _LIBCPP_STD_VER > 11
528template <class _Tp = void>
529#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000531#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000532struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533{
Marshall Clowd18ee652013-09-28 19:06:12 +0000534 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
535 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536 {return __x + __y;}
537};
538
Marshall Clow974bae22013-07-29 14:21:53 +0000539#if _LIBCPP_STD_VER > 11
540template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000541struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000542{
543 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000544 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
545 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000546 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
547 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
548 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000549 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000550};
551#endif
552
553
554#if _LIBCPP_STD_VER > 11
555template <class _Tp = void>
556#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000558#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000559struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560{
Marshall Clowd18ee652013-09-28 19:06:12 +0000561 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
562 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563 {return __x - __y;}
564};
565
Marshall Clow974bae22013-07-29 14:21:53 +0000566#if _LIBCPP_STD_VER > 11
567template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000568struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000569{
570 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000571 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
572 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000573 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
574 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
575 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000576 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000577};
578#endif
579
580
581#if _LIBCPP_STD_VER > 11
582template <class _Tp = void>
583#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000585#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000586struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587{
Marshall Clowd18ee652013-09-28 19:06:12 +0000588 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
589 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590 {return __x * __y;}
591};
592
Marshall Clow974bae22013-07-29 14:21:53 +0000593#if _LIBCPP_STD_VER > 11
594template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000595struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000596{
597 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000598 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
599 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000600 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
601 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
602 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000603 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000604};
605#endif
606
607
608#if _LIBCPP_STD_VER > 11
609template <class _Tp = void>
610#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000612#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000613struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614{
Marshall Clowd18ee652013-09-28 19:06:12 +0000615 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
616 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617 {return __x / __y;}
618};
619
Marshall Clow974bae22013-07-29 14:21:53 +0000620#if _LIBCPP_STD_VER > 11
621template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000622struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000623{
624 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000625 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
626 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000627 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
628 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
629 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000630 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000631};
632#endif
633
634
635#if _LIBCPP_STD_VER > 11
636template <class _Tp = void>
637#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000639#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000640struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641{
Marshall Clowd18ee652013-09-28 19:06:12 +0000642 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
643 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644 {return __x % __y;}
645};
646
Marshall Clow974bae22013-07-29 14:21:53 +0000647#if _LIBCPP_STD_VER > 11
648template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000649struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000650{
651 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000652 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
653 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000654 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
655 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
656 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000657 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000658};
659#endif
660
661
662#if _LIBCPP_STD_VER > 11
663template <class _Tp = void>
664#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000666#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000667struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668{
Marshall Clowd18ee652013-09-28 19:06:12 +0000669 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
670 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671 {return -__x;}
672};
673
Marshall Clow974bae22013-07-29 14:21:53 +0000674#if _LIBCPP_STD_VER > 11
675template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000676struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000677{
678 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000679 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
680 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000681 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
682 -> decltype (- _VSTD::forward<_Tp>(__x))
683 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000684 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000685};
686#endif
687
688
689#if _LIBCPP_STD_VER > 11
690template <class _Tp = void>
691#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000693#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000694struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695{
Marshall Clowd18ee652013-09-28 19:06:12 +0000696 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
697 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 {return __x == __y;}
699};
700
Marshall Clow974bae22013-07-29 14:21:53 +0000701#if _LIBCPP_STD_VER > 11
702template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000703struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000704{
Marshall Clowd18ee652013-09-28 19:06:12 +0000705 template <class _T1, class _T2>
706 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000707 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000708 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
709 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
710 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000711 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000712};
713#endif
714
715
716#if _LIBCPP_STD_VER > 11
717template <class _Tp = void>
718#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000720#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000721struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722{
Marshall Clowd18ee652013-09-28 19:06:12 +0000723 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
724 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725 {return __x != __y;}
726};
727
Marshall Clow974bae22013-07-29 14:21:53 +0000728#if _LIBCPP_STD_VER > 11
729template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000730struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000731{
Marshall Clowd18ee652013-09-28 19:06:12 +0000732 template <class _T1, class _T2>
733 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000734 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000735 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
736 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
737 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000738 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000739};
740#endif
741
742
743#if _LIBCPP_STD_VER > 11
744template <class _Tp = void>
745#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000747#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000748struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749{
Marshall Clowd18ee652013-09-28 19:06:12 +0000750 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
751 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 {return __x > __y;}
753};
754
Marshall Clow974bae22013-07-29 14:21:53 +0000755#if _LIBCPP_STD_VER > 11
756template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000757struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000758{
Marshall Clowd18ee652013-09-28 19:06:12 +0000759 template <class _T1, class _T2>
760 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000761 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000762 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
763 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
764 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000765 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000766};
767#endif
768
769
Howard Hinnantb17caf92012-02-21 21:02:58 +0000770// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771
Marshall Clow974bae22013-07-29 14:21:53 +0000772#if _LIBCPP_STD_VER > 11
773template <class _Tp = void>
774#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000776#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000777struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778{
Marshall Clowd18ee652013-09-28 19:06:12 +0000779 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
780 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781 {return __x >= __y;}
782};
783
Marshall Clow974bae22013-07-29 14:21:53 +0000784#if _LIBCPP_STD_VER > 11
785template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000786struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000787{
Marshall Clowd18ee652013-09-28 19:06:12 +0000788 template <class _T1, class _T2>
789 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000790 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000791 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
792 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
793 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000794 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000795};
796#endif
797
798
799#if _LIBCPP_STD_VER > 11
800template <class _Tp = void>
801#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000803#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000804struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805{
Marshall Clowd18ee652013-09-28 19:06:12 +0000806 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
807 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808 {return __x <= __y;}
809};
810
Marshall Clow974bae22013-07-29 14:21:53 +0000811#if _LIBCPP_STD_VER > 11
812template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000813struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000814{
Marshall Clowd18ee652013-09-28 19:06:12 +0000815 template <class _T1, class _T2>
816 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000817 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000818 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
819 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
820 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000821 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000822};
823#endif
824
825
826#if _LIBCPP_STD_VER > 11
827template <class _Tp = void>
828#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000830#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000831struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832{
Marshall Clowd18ee652013-09-28 19:06:12 +0000833 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
834 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835 {return __x && __y;}
836};
837
Marshall Clow974bae22013-07-29 14:21:53 +0000838#if _LIBCPP_STD_VER > 11
839template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000840struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000841{
Marshall Clowd18ee652013-09-28 19:06:12 +0000842 template <class _T1, class _T2>
843 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000844 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000845 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
846 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
847 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000848 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000849};
850#endif
851
852
853#if _LIBCPP_STD_VER > 11
854template <class _Tp = void>
855#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000857#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000858struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859{
Marshall Clowd18ee652013-09-28 19:06:12 +0000860 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
861 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 {return __x || __y;}
863};
864
Marshall Clow974bae22013-07-29 14:21:53 +0000865#if _LIBCPP_STD_VER > 11
866template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000867struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000868{
Marshall Clowd18ee652013-09-28 19:06:12 +0000869 template <class _T1, class _T2>
870 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000871 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000872 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
873 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
874 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000875 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000876};
877#endif
878
879
880#if _LIBCPP_STD_VER > 11
881template <class _Tp = void>
882#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000884#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000885struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886{
Marshall Clowd18ee652013-09-28 19:06:12 +0000887 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
888 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889 {return !__x;}
890};
891
Marshall Clow974bae22013-07-29 14:21:53 +0000892#if _LIBCPP_STD_VER > 11
893template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000894struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000895{
896 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000897 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
898 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000899 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
900 -> decltype (!_VSTD::forward<_Tp>(__x))
901 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000902 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000903};
904#endif
905
906
907#if _LIBCPP_STD_VER > 11
908template <class _Tp = void>
909#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000911#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000912struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913{
Marshall Clowd18ee652013-09-28 19:06:12 +0000914 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
915 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916 {return __x & __y;}
917};
918
Marshall Clow974bae22013-07-29 14:21:53 +0000919#if _LIBCPP_STD_VER > 11
920template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000921struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000922{
Marshall Clowd18ee652013-09-28 19:06:12 +0000923 template <class _T1, class _T2>
924 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000925 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000926 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
927 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
928 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000929 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000930};
931#endif
932
933
934#if _LIBCPP_STD_VER > 11
935template <class _Tp = void>
936#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000938#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000939struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940{
Marshall Clowd18ee652013-09-28 19:06:12 +0000941 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
942 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000943 {return __x | __y;}
944};
945
Marshall Clow974bae22013-07-29 14:21:53 +0000946#if _LIBCPP_STD_VER > 11
947template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000948struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000949{
Marshall Clowd18ee652013-09-28 19:06:12 +0000950 template <class _T1, class _T2>
951 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000952 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000953 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
954 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
955 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000956 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000957};
958#endif
959
960
961#if _LIBCPP_STD_VER > 11
962template <class _Tp = void>
963#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000965#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000966struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967{
Marshall Clowd18ee652013-09-28 19:06:12 +0000968 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
969 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 {return __x ^ __y;}
971};
972
Marshall Clow974bae22013-07-29 14:21:53 +0000973#if _LIBCPP_STD_VER > 11
974template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000975struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000976{
Marshall Clowd18ee652013-09-28 19:06:12 +0000977 template <class _T1, class _T2>
978 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000979 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000980 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
981 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
982 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000983 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000984};
985#endif
986
987
988#if _LIBCPP_STD_VER > 11
989template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000990struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000991{
Marshall Clowd18ee652013-09-28 19:06:12 +0000992 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
993 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000994 {return ~__x;}
995};
996
997template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000998struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000999{
1000 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +00001001 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1002 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +00001003 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
1004 -> decltype (~_VSTD::forward<_Tp>(__x))
1005 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +00001006 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +00001007};
1008#endif
1009
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001011class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 : public unary_function<typename _Predicate::argument_type, bool>
1013{
1014 _Predicate __pred_;
1015public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001016 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1017 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001019 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1020 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021 {return !__pred_(__x);}
1022};
1023
1024template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001025_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026unary_negate<_Predicate>
1027not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1028
1029template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001030class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 : public binary_function<typename _Predicate::first_argument_type,
1032 typename _Predicate::second_argument_type,
1033 bool>
1034{
1035 _Predicate __pred_;
1036public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001037 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001038 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1039
1040 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1041 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 const typename _Predicate::second_argument_type& __y) const
1043 {return !__pred_(__x, __y);}
1044};
1045
1046template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001047_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048binary_negate<_Predicate>
1049not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1050
Marshall Clow26a027c2017-04-13 18:25:32 +00001051#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001053class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 : public unary_function<typename __Operation::second_argument_type,
1055 typename __Operation::result_type>
1056{
1057protected:
1058 __Operation op;
1059 typename __Operation::first_argument_type value;
1060public:
1061 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1062 const typename __Operation::first_argument_type __y)
1063 : op(__x), value(__y) {}
1064 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1065 (typename __Operation::second_argument_type& __x) const
1066 {return op(value, __x);}
1067 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1068 (const typename __Operation::second_argument_type& __x) const
1069 {return op(value, __x);}
1070};
1071
1072template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001073_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074binder1st<__Operation>
1075bind1st(const __Operation& __op, const _Tp& __x)
1076 {return binder1st<__Operation>(__op, __x);}
1077
1078template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001079class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 : public unary_function<typename __Operation::first_argument_type,
1081 typename __Operation::result_type>
1082{
1083protected:
1084 __Operation op;
1085 typename __Operation::second_argument_type value;
1086public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1089 : op(__x), value(__y) {}
1090 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1091 ( typename __Operation::first_argument_type& __x) const
1092 {return op(__x, value);}
1093 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1094 (const typename __Operation::first_argument_type& __x) const
1095 {return op(__x, value);}
1096};
1097
1098template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001099_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100binder2nd<__Operation>
1101bind2nd(const __Operation& __op, const _Tp& __x)
1102 {return binder2nd<__Operation>(__op, __x);}
1103
1104template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001105class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001106 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107{
1108 _Result (*__f_)(_Arg);
1109public:
1110 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1111 : __f_(__f) {}
1112 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1113 {return __f_(__x);}
1114};
1115
1116template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001117_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118pointer_to_unary_function<_Arg,_Result>
1119ptr_fun(_Result (*__f)(_Arg))
1120 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1121
1122template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001123class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001124 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125{
1126 _Result (*__f_)(_Arg1, _Arg2);
1127public:
1128 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1129 : __f_(__f) {}
1130 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1131 {return __f_(__x, __y);}
1132};
1133
1134template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001135_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136pointer_to_binary_function<_Arg1,_Arg2,_Result>
1137ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1138 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1139
1140template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001141class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1142 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143{
1144 _Sp (_Tp::*__p_)();
1145public:
1146 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1147 : __p_(__p) {}
1148 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1149 {return (__p->*__p_)();}
1150};
1151
1152template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001153class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1154 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155{
1156 _Sp (_Tp::*__p_)(_Ap);
1157public:
1158 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1159 : __p_(__p) {}
1160 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1161 {return (__p->*__p_)(__x);}
1162};
1163
1164template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001165_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166mem_fun_t<_Sp,_Tp>
1167mem_fun(_Sp (_Tp::*__f)())
1168 {return mem_fun_t<_Sp,_Tp>(__f);}
1169
1170template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001171_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172mem_fun1_t<_Sp,_Tp,_Ap>
1173mem_fun(_Sp (_Tp::*__f)(_Ap))
1174 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1175
1176template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001177class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1178 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179{
1180 _Sp (_Tp::*__p_)();
1181public:
1182 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1183 : __p_(__p) {}
1184 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1185 {return (__p.*__p_)();}
1186};
1187
1188template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001189class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1190 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191{
1192 _Sp (_Tp::*__p_)(_Ap);
1193public:
1194 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1195 : __p_(__p) {}
1196 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1197 {return (__p.*__p_)(__x);}
1198};
1199
1200template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001201_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202mem_fun_ref_t<_Sp,_Tp>
1203mem_fun_ref(_Sp (_Tp::*__f)())
1204 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1205
1206template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001207_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208mem_fun1_ref_t<_Sp,_Tp,_Ap>
1209mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1210 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1211
1212template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001213class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1214 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215{
1216 _Sp (_Tp::*__p_)() const;
1217public:
1218 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1219 : __p_(__p) {}
1220 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1221 {return (__p->*__p_)();}
1222};
1223
1224template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001225class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1226 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227{
1228 _Sp (_Tp::*__p_)(_Ap) const;
1229public:
1230 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1231 : __p_(__p) {}
1232 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1233 {return (__p->*__p_)(__x);}
1234};
1235
1236template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001237_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238const_mem_fun_t<_Sp,_Tp>
1239mem_fun(_Sp (_Tp::*__f)() const)
1240 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1241
1242template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001243_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244const_mem_fun1_t<_Sp,_Tp,_Ap>
1245mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1246 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1247
1248template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001249class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1250 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251{
1252 _Sp (_Tp::*__p_)() const;
1253public:
1254 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1255 : __p_(__p) {}
1256 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1257 {return (__p.*__p_)();}
1258};
1259
1260template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001261class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001262 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263{
1264 _Sp (_Tp::*__p_)(_Ap) const;
1265public:
1266 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1267 : __p_(__p) {}
1268 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1269 {return (__p.*__p_)(__x);}
1270};
1271
1272template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001273_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274const_mem_fun_ref_t<_Sp,_Tp>
1275mem_fun_ref(_Sp (_Tp::*__f)() const)
1276 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1277
1278template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001279_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1281mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1282 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001283#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001285////////////////////////////////////////////////////////////////////////////////
1286// MEMFUN
1287//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289template <class _Tp>
1290class __mem_fn
1291 : public __weak_result_type<_Tp>
1292{
1293public:
1294 // types
1295 typedef _Tp type;
1296private:
1297 type __f_;
1298
1299public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001300 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1301 __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001303#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304 // invoke
1305 template <class... _ArgTypes>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001306 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier2cc48332015-07-22 22:43:27 +00001307 typename __invoke_return<type, _ArgTypes...>::type
1308 operator() (_ArgTypes&&... __args) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001309 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001310 }
1311#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001312
1313 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001314 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001315 typename __invoke_return0<type, _A0>::type
1316 operator() (_A0& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001317 return _VSTD::__invoke(__f_, __a0);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001318 }
1319
Eric Fiselierce1813a2015-08-26 20:15:02 +00001320 template <class _A0>
1321 _LIBCPP_INLINE_VISIBILITY
1322 typename __invoke_return0<type, _A0 const>::type
1323 operator() (_A0 const& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001324 return _VSTD::__invoke(__f_, __a0);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001325 }
1326
Eric Fiselier2cc48332015-07-22 22:43:27 +00001327 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001328 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001329 typename __invoke_return1<type, _A0, _A1>::type
1330 operator() (_A0& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001331 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001332 }
1333
Eric Fiselierce1813a2015-08-26 20:15:02 +00001334 template <class _A0, class _A1>
1335 _LIBCPP_INLINE_VISIBILITY
1336 typename __invoke_return1<type, _A0 const, _A1>::type
1337 operator() (_A0 const& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001338 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001339 }
1340
1341 template <class _A0, class _A1>
1342 _LIBCPP_INLINE_VISIBILITY
1343 typename __invoke_return1<type, _A0, _A1 const>::type
1344 operator() (_A0& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001345 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001346 }
1347
1348 template <class _A0, class _A1>
1349 _LIBCPP_INLINE_VISIBILITY
1350 typename __invoke_return1<type, _A0 const, _A1 const>::type
1351 operator() (_A0 const& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001352 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001353 }
1354
Eric Fiselier2cc48332015-07-22 22:43:27 +00001355 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001356 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001357 typename __invoke_return2<type, _A0, _A1, _A2>::type
1358 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001359 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001360 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001361
1362 template <class _A0, class _A1, class _A2>
1363 _LIBCPP_INLINE_VISIBILITY
1364 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1365 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001366 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001367 }
1368
1369 template <class _A0, class _A1, class _A2>
1370 _LIBCPP_INLINE_VISIBILITY
1371 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1372 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001373 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001374 }
1375
1376 template <class _A0, class _A1, class _A2>
1377 _LIBCPP_INLINE_VISIBILITY
1378 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1379 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001380 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001381 }
1382
1383 template <class _A0, class _A1, class _A2>
1384 _LIBCPP_INLINE_VISIBILITY
1385 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1386 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001387 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001388 }
1389
1390 template <class _A0, class _A1, class _A2>
1391 _LIBCPP_INLINE_VISIBILITY
1392 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1393 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001394 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001395 }
1396
1397 template <class _A0, class _A1, class _A2>
1398 _LIBCPP_INLINE_VISIBILITY
1399 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1400 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001401 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001402 }
1403
1404 template <class _A0, class _A1, class _A2>
1405 _LIBCPP_INLINE_VISIBILITY
1406 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1407 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001408 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001409 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001410#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411};
1412
Howard Hinnantc834c512011-11-29 18:15:50 +00001413template<class _Rp, class _Tp>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001414inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00001415__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001416mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417{
Howard Hinnantc834c512011-11-29 18:15:50 +00001418 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419}
1420
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001421////////////////////////////////////////////////////////////////////////////////
1422// FUNCTION
1423//==============================================================================
1424
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425// bad_function_call
1426
Howard Hinnant4ff57432010-09-21 22:55:27 +00001427class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 : public exception
1429{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001430#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1431public:
1432 virtual ~bad_function_call() _NOEXCEPT;
1433
1434 virtual const char* what() const _NOEXCEPT;
1435#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436};
1437
Louis Dionne16fe2952018-07-11 23:14:33 +00001438_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001439void __throw_bad_function_call()
1440{
1441#ifndef _LIBCPP_NO_EXCEPTIONS
1442 throw bad_function_call();
1443#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001444 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001445#endif
1446}
1447
Louis Dionne44d1f812020-03-09 11:16:22 -04001448#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1449# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1450 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1451#else
1452# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1453#endif
1454
1455template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456
1457namespace __function
1458{
1459
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001460template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461struct __maybe_derive_from_unary_function
1462{
1463};
1464
Howard Hinnantc834c512011-11-29 18:15:50 +00001465template<class _Rp, class _A1>
1466struct __maybe_derive_from_unary_function<_Rp(_A1)>
1467 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468{
1469};
1470
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001471template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472struct __maybe_derive_from_binary_function
1473{
1474};
1475
Howard Hinnantc834c512011-11-29 18:15:50 +00001476template<class _Rp, class _A1, class _A2>
1477struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1478 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479{
1480};
1481
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001482template <class _Fp>
1483_LIBCPP_INLINE_VISIBILITY
1484bool __not_null(_Fp const&) { return true; }
1485
1486template <class _Fp>
1487_LIBCPP_INLINE_VISIBILITY
1488bool __not_null(_Fp* __ptr) { return __ptr; }
1489
1490template <class _Ret, class _Class>
1491_LIBCPP_INLINE_VISIBILITY
1492bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1493
1494template <class _Fp>
1495_LIBCPP_INLINE_VISIBILITY
1496bool __not_null(function<_Fp> const& __f) { return !!__f; }
1497
Louis Dionnee2391d72020-04-22 13:58:17 -04001498#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1499template <class _Rp, class ..._Args>
1500_LIBCPP_INLINE_VISIBILITY
1501bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1502#endif
1503
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001504} // namespace __function
1505
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001506#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001507
1508namespace __function {
1509
Eric Fiselier125798e2018-12-10 18:14:09 +00001510// __alloc_func holds a functor and an allocator.
1511
1512template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001513template <class _Fp, class _FB>
1514class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001515
1516template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1517class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1518{
1519 __compressed_pair<_Fp, _Ap> __f_;
1520
1521 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001522 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1523 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001524
1525 _LIBCPP_INLINE_VISIBILITY
1526 const _Target& __target() const { return __f_.first(); }
1527
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001528 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001529 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001530 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001531
1532 _LIBCPP_INLINE_VISIBILITY
1533 explicit __alloc_func(_Target&& __f)
1534 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1535 _VSTD::forward_as_tuple())
1536 {
1537 }
1538
1539 _LIBCPP_INLINE_VISIBILITY
1540 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1541 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1542 _VSTD::forward_as_tuple(__a))
1543 {
1544 }
1545
1546 _LIBCPP_INLINE_VISIBILITY
1547 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1548 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1549 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1550 {
1551 }
1552
1553 _LIBCPP_INLINE_VISIBILITY
1554 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1555 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1556 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1557 {
1558 }
1559
1560 _LIBCPP_INLINE_VISIBILITY
1561 _Rp operator()(_ArgTypes&&... __arg)
1562 {
1563 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1564 return _Invoker::__call(__f_.first(),
1565 _VSTD::forward<_ArgTypes>(__arg)...);
1566 }
1567
1568 _LIBCPP_INLINE_VISIBILITY
1569 __alloc_func* __clone() const
1570 {
1571 typedef allocator_traits<_Alloc> __alloc_traits;
1572 typedef
1573 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1574 _AA;
1575 _AA __a(__f_.second());
1576 typedef __allocator_destructor<_AA> _Dp;
1577 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1578 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1579 return __hold.release();
1580 }
1581
1582 _LIBCPP_INLINE_VISIBILITY
1583 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001584
1585 static void __destroy_and_delete(__alloc_func* __f) {
1586 typedef allocator_traits<_Alloc> __alloc_traits;
1587 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1588 _FunAlloc;
1589 _FunAlloc __a(__f->__get_allocator());
1590 __f->destroy();
1591 __a.deallocate(__f, 1);
1592 }
1593};
1594
1595template <class _Fp, class _Rp, class... _ArgTypes>
1596class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1597 _Fp __f_;
1598
1599public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001600 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001601
1602 _LIBCPP_INLINE_VISIBILITY
1603 const _Target& __target() const { return __f_; }
1604
1605 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001606 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001607
1608 _LIBCPP_INLINE_VISIBILITY
1609 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1610
1611 _LIBCPP_INLINE_VISIBILITY
1612 _Rp operator()(_ArgTypes&&... __arg) {
1613 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1614 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1615 }
1616
1617 _LIBCPP_INLINE_VISIBILITY
1618 __default_alloc_func* __clone() const {
1619 __builtin_new_allocator::__holder_t __hold =
1620 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1621 __default_alloc_func* __res =
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001622 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00001623 (void)__hold.release();
1624 return __res;
1625 }
1626
1627 _LIBCPP_INLINE_VISIBILITY
1628 void destroy() _NOEXCEPT { __f_.~_Target(); }
1629
1630 static void __destroy_and_delete(__default_alloc_func* __f) {
1631 __f->destroy();
1632 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1633 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001634};
1635
1636// __base provides an abstract interface for copyable functors.
1637
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001638template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639
Howard Hinnantc834c512011-11-29 18:15:50 +00001640template<class _Rp, class ..._ArgTypes>
1641class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642{
1643 __base(const __base&);
1644 __base& operator=(const __base&);
1645public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001646 _LIBCPP_INLINE_VISIBILITY __base() {}
1647 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001648 virtual __base* __clone() const = 0;
1649 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001650 virtual void destroy() _NOEXCEPT = 0;
1651 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001652 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001653#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001654 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1655 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001656#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657};
1658
Eric Fiselier125798e2018-12-10 18:14:09 +00001659// __func implements __base for a given functor type.
1660
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661template<class _FD, class _Alloc, class _FB> class __func;
1662
Howard Hinnantc834c512011-11-29 18:15:50 +00001663template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1664class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1665 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666{
Eric Fiselier125798e2018-12-10 18:14:09 +00001667 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001670 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001671 : __f_(_VSTD::move(__f)) {}
1672
Howard Hinnant4ff57432010-09-21 22:55:27 +00001673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001674 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001675 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001676
1677 _LIBCPP_INLINE_VISIBILITY
1678 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001679 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001680
1681 _LIBCPP_INLINE_VISIBILITY
1682 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001683 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1684
Howard Hinnantc834c512011-11-29 18:15:50 +00001685 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1686 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001687 virtual void destroy() _NOEXCEPT;
1688 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001689 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001690#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001691 virtual const void* target(const type_info&) const _NOEXCEPT;
1692 virtual const std::type_info& target_type() const _NOEXCEPT;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001693#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694};
1695
Howard Hinnantc834c512011-11-29 18:15:50 +00001696template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1697__base<_Rp(_ArgTypes...)>*
1698__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001700 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001701 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001702 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001703 typedef __allocator_destructor<_Ap> _Dp;
1704 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001705 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 return __hold.release();
1707}
1708
Howard Hinnantc834c512011-11-29 18:15:50 +00001709template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710void
Howard Hinnantc834c512011-11-29 18:15:50 +00001711__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712{
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001713 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714}
1715
Howard Hinnantc834c512011-11-29 18:15:50 +00001716template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717void
Howard Hinnantc834c512011-11-29 18:15:50 +00001718__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719{
Eric Fiselier125798e2018-12-10 18:14:09 +00001720 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721}
1722
Howard Hinnantc834c512011-11-29 18:15:50 +00001723template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724void
Howard Hinnantc834c512011-11-29 18:15:50 +00001725__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001727 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001728 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001729 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001730 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731 __a.deallocate(this, 1);
1732}
1733
Howard Hinnantc834c512011-11-29 18:15:50 +00001734template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1735_Rp
1736__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737{
Eric Fiselier125798e2018-12-10 18:14:09 +00001738 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739}
1740
Howard Hinnant72f73582010-08-11 17:04:31 +00001741#ifndef _LIBCPP_NO_RTTI
1742
Howard Hinnantc834c512011-11-29 18:15:50 +00001743template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001745__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746{
Howard Hinnantc834c512011-11-29 18:15:50 +00001747 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001748 return &__f_.__target();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001749 return nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750}
1751
Howard Hinnantc834c512011-11-29 18:15:50 +00001752template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001754__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755{
Howard Hinnantc834c512011-11-29 18:15:50 +00001756 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757}
1758
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001759#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001760
Eric Fiselier125798e2018-12-10 18:14:09 +00001761// __value_func creates a value-type from a __func.
1762
1763template <class _Fp> class __value_func;
1764
1765template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1766{
1767 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1768
1769 typedef __base<_Rp(_ArgTypes...)> __func;
1770 __func* __f_;
1771
1772 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1773 {
1774 return reinterpret_cast<__func*>(p);
1775 }
1776
1777 public:
1778 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001779 __value_func() _NOEXCEPT : __f_(nullptr) {}
Eric Fiselier125798e2018-12-10 18:14:09 +00001780
1781 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001782 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001783 : __f_(nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001784 {
1785 typedef allocator_traits<_Alloc> __alloc_traits;
1786 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1787 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1788 _FunAlloc;
1789
1790 if (__function::__not_null(__f))
1791 {
1792 _FunAlloc __af(__a);
1793 if (sizeof(_Fun) <= sizeof(__buf_) &&
1794 is_nothrow_copy_constructible<_Fp>::value &&
1795 is_nothrow_copy_constructible<_FunAlloc>::value)
1796 {
1797 __f_ =
1798 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1799 }
1800 else
1801 {
1802 typedef __allocator_destructor<_FunAlloc> _Dp;
1803 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1804 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1805 __f_ = __hold.release();
1806 }
1807 }
1808 }
1809
Eric Fiselier74ebee62019-06-08 01:31:19 +00001810 template <class _Fp,
1811 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1812 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001813 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001814
Eric Fiselier125798e2018-12-10 18:14:09 +00001815 _LIBCPP_INLINE_VISIBILITY
1816 __value_func(const __value_func& __f)
1817 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001818 if (__f.__f_ == nullptr)
1819 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001820 else if ((void*)__f.__f_ == &__f.__buf_)
1821 {
1822 __f_ = __as_base(&__buf_);
1823 __f.__f_->__clone(__f_);
1824 }
1825 else
1826 __f_ = __f.__f_->__clone();
1827 }
1828
1829 _LIBCPP_INLINE_VISIBILITY
1830 __value_func(__value_func&& __f) _NOEXCEPT
1831 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001832 if (__f.__f_ == nullptr)
1833 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001834 else if ((void*)__f.__f_ == &__f.__buf_)
1835 {
1836 __f_ = __as_base(&__buf_);
1837 __f.__f_->__clone(__f_);
1838 }
1839 else
1840 {
1841 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001842 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001843 }
1844 }
1845
1846 _LIBCPP_INLINE_VISIBILITY
1847 ~__value_func()
1848 {
1849 if ((void*)__f_ == &__buf_)
1850 __f_->destroy();
1851 else if (__f_)
1852 __f_->destroy_deallocate();
1853 }
1854
1855 _LIBCPP_INLINE_VISIBILITY
1856 __value_func& operator=(__value_func&& __f)
1857 {
1858 *this = nullptr;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001859 if (__f.__f_ == nullptr)
1860 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001861 else if ((void*)__f.__f_ == &__f.__buf_)
1862 {
1863 __f_ = __as_base(&__buf_);
1864 __f.__f_->__clone(__f_);
1865 }
1866 else
1867 {
1868 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001869 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001870 }
1871 return *this;
1872 }
1873
1874 _LIBCPP_INLINE_VISIBILITY
1875 __value_func& operator=(nullptr_t)
1876 {
1877 __func* __f = __f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001878 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001879 if ((void*)__f == &__buf_)
1880 __f->destroy();
1881 else if (__f)
1882 __f->destroy_deallocate();
1883 return *this;
1884 }
1885
1886 _LIBCPP_INLINE_VISIBILITY
1887 _Rp operator()(_ArgTypes&&... __args) const
1888 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001889 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001890 __throw_bad_function_call();
1891 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1892 }
1893
1894 _LIBCPP_INLINE_VISIBILITY
1895 void swap(__value_func& __f) _NOEXCEPT
1896 {
1897 if (&__f == this)
1898 return;
1899 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1900 {
1901 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1902 __func* __t = __as_base(&__tempbuf);
1903 __f_->__clone(__t);
1904 __f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001905 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001906 __f.__f_->__clone(__as_base(&__buf_));
1907 __f.__f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001908 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001909 __f_ = __as_base(&__buf_);
1910 __t->__clone(__as_base(&__f.__buf_));
1911 __t->destroy();
1912 __f.__f_ = __as_base(&__f.__buf_);
1913 }
1914 else if ((void*)__f_ == &__buf_)
1915 {
1916 __f_->__clone(__as_base(&__f.__buf_));
1917 __f_->destroy();
1918 __f_ = __f.__f_;
1919 __f.__f_ = __as_base(&__f.__buf_);
1920 }
1921 else if ((void*)__f.__f_ == &__f.__buf_)
1922 {
1923 __f.__f_->__clone(__as_base(&__buf_));
1924 __f.__f_->destroy();
1925 __f.__f_ = __f_;
1926 __f_ = __as_base(&__buf_);
1927 }
1928 else
1929 _VSTD::swap(__f_, __f.__f_);
1930 }
1931
1932 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001933 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
Eric Fiselier125798e2018-12-10 18:14:09 +00001934
1935#ifndef _LIBCPP_NO_RTTI
1936 _LIBCPP_INLINE_VISIBILITY
1937 const std::type_info& target_type() const _NOEXCEPT
1938 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001939 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001940 return typeid(void);
1941 return __f_->target_type();
1942 }
1943
1944 template <typename _Tp>
1945 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1946 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001947 if (__f_ == nullptr)
1948 return nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001949 return (const _Tp*)__f_->target(typeid(_Tp));
1950 }
1951#endif // _LIBCPP_NO_RTTI
1952};
1953
Eric Fiselierf2e64362018-12-11 00:14:34 +00001954// Storage for a functor object, to be used with __policy to manage copy and
1955// destruction.
1956union __policy_storage
1957{
1958 mutable char __small[sizeof(void*) * 2];
1959 void* __large;
1960};
1961
1962// True if _Fun can safely be held in __policy_storage.__small.
1963template <typename _Fun>
1964struct __use_small_storage
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04001965 : public integral_constant<
Eric Fiselierf2e64362018-12-11 00:14:34 +00001966 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001967 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04001968 is_trivially_copy_constructible<_Fun>::value &&
1969 is_trivially_destructible<_Fun>::value> {};
Eric Fiselierf2e64362018-12-11 00:14:34 +00001970
1971// Policy contains information about how to copy, destroy, and move the
1972// underlying functor. You can think of it as a vtable of sorts.
1973struct __policy
1974{
1975 // Used to copy or destroy __large values. null for trivial objects.
1976 void* (*const __clone)(const void*);
1977 void (*const __destroy)(void*);
1978
1979 // True if this is the null policy (no value).
1980 const bool __is_null;
1981
1982 // The target type. May be null if RTTI is disabled.
1983 const std::type_info* const __type_info;
1984
1985 // Returns a pointer to a static policy object suitable for the functor
1986 // type.
1987 template <typename _Fun>
1988 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1989 {
1990 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1991 }
1992
1993 _LIBCPP_INLINE_VISIBILITY
1994 static const __policy* __create_empty()
1995 {
1996 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1997 true,
1998#ifndef _LIBCPP_NO_RTTI
1999 &typeid(void)
2000#else
2001 nullptr
2002#endif
2003 };
2004 return &__policy_;
2005 }
2006
2007 private:
2008 template <typename _Fun> static void* __large_clone(const void* __s)
2009 {
2010 const _Fun* __f = static_cast<const _Fun*>(__s);
2011 return __f->__clone();
2012 }
2013
Eric Fiselier74ebee62019-06-08 01:31:19 +00002014 template <typename _Fun>
2015 static void __large_destroy(void* __s) {
2016 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002017 }
2018
2019 template <typename _Fun>
2020 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002021 __choose_policy(/* is_small = */ false_type) {
2022 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2023 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002024#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002025 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002026#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002027 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002028#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002029 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002030 return &__policy_;
2031 }
2032
2033 template <typename _Fun>
2034 _LIBCPP_INLINE_VISIBILITY static const __policy*
2035 __choose_policy(/* is_small = */ true_type)
2036 {
2037 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2038 nullptr, nullptr, false,
2039#ifndef _LIBCPP_NO_RTTI
2040 &typeid(typename _Fun::_Target)
2041#else
2042 nullptr
2043#endif
2044 };
2045 return &__policy_;
2046 }
2047};
2048
2049// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2050// faster for types that can be passed in registers.
2051template <typename _Tp>
2052using __fast_forward =
Mark de Wever9d7aa7a2021-05-09 18:22:52 +02002053 typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002054
2055// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2056
2057template <class _Fp> struct __policy_invoker;
2058
2059template <class _Rp, class... _ArgTypes>
2060struct __policy_invoker<_Rp(_ArgTypes...)>
2061{
2062 typedef _Rp (*__Call)(const __policy_storage*,
2063 __fast_forward<_ArgTypes>...);
2064
2065 __Call __call_;
2066
2067 // Creates an invoker that throws bad_function_call.
2068 _LIBCPP_INLINE_VISIBILITY
2069 __policy_invoker() : __call_(&__call_empty) {}
2070
2071 // Creates an invoker that calls the given instance of __func.
2072 template <typename _Fun>
2073 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2074 {
2075 return __policy_invoker(&__call_impl<_Fun>);
2076 }
2077
2078 private:
2079 _LIBCPP_INLINE_VISIBILITY
2080 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2081
2082 static _Rp __call_empty(const __policy_storage*,
2083 __fast_forward<_ArgTypes>...)
2084 {
2085 __throw_bad_function_call();
2086 }
2087
2088 template <typename _Fun>
2089 static _Rp __call_impl(const __policy_storage* __buf,
2090 __fast_forward<_ArgTypes>... __args)
2091 {
2092 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2093 ? &__buf->__small
2094 : __buf->__large);
2095 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2096 }
2097};
2098
2099// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2100// copyable functor.
2101
2102template <class _Fp> class __policy_func;
2103
2104template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2105{
2106 // Inline storage for small objects.
2107 __policy_storage __buf_;
2108
2109 // Calls the value stored in __buf_. This could technically be part of
2110 // policy, but storing it here eliminates a level of indirection inside
2111 // operator().
2112 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2113 __invoker __invoker_;
2114
2115 // The policy that describes how to move / copy / destroy __buf_. Never
2116 // null, even if the function is empty.
2117 const __policy* __policy_;
2118
2119 public:
2120 _LIBCPP_INLINE_VISIBILITY
2121 __policy_func() : __policy_(__policy::__create_empty()) {}
2122
2123 template <class _Fp, class _Alloc>
2124 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2125 : __policy_(__policy::__create_empty())
2126 {
2127 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2128 typedef allocator_traits<_Alloc> __alloc_traits;
2129 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2130 _FunAlloc;
2131
2132 if (__function::__not_null(__f))
2133 {
2134 __invoker_ = __invoker::template __create<_Fun>();
2135 __policy_ = __policy::__create<_Fun>();
2136
2137 _FunAlloc __af(__a);
2138 if (__use_small_storage<_Fun>())
2139 {
2140 ::new ((void*)&__buf_.__small)
2141 _Fun(_VSTD::move(__f), _Alloc(__af));
2142 }
2143 else
2144 {
2145 typedef __allocator_destructor<_FunAlloc> _Dp;
2146 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2147 ::new ((void*)__hold.get())
2148 _Fun(_VSTD::move(__f), _Alloc(__af));
2149 __buf_.__large = __hold.release();
2150 }
2151 }
2152 }
2153
Eric Fiselier74ebee62019-06-08 01:31:19 +00002154 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2155 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2156 : __policy_(__policy::__create_empty()) {
2157 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2158
2159 if (__function::__not_null(__f)) {
2160 __invoker_ = __invoker::template __create<_Fun>();
2161 __policy_ = __policy::__create<_Fun>();
2162 if (__use_small_storage<_Fun>()) {
2163 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2164 } else {
2165 __builtin_new_allocator::__holder_t __hold =
2166 __builtin_new_allocator::__allocate_type<_Fun>(1);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002167 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
Eric Fiselier74ebee62019-06-08 01:31:19 +00002168 (void)__hold.release();
2169 }
2170 }
2171 }
2172
Eric Fiselierf2e64362018-12-11 00:14:34 +00002173 _LIBCPP_INLINE_VISIBILITY
2174 __policy_func(const __policy_func& __f)
2175 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2176 __policy_(__f.__policy_)
2177 {
2178 if (__policy_->__clone)
2179 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2180 }
2181
2182 _LIBCPP_INLINE_VISIBILITY
2183 __policy_func(__policy_func&& __f)
2184 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2185 __policy_(__f.__policy_)
2186 {
2187 if (__policy_->__destroy)
2188 {
2189 __f.__policy_ = __policy::__create_empty();
2190 __f.__invoker_ = __invoker();
2191 }
2192 }
2193
2194 _LIBCPP_INLINE_VISIBILITY
2195 ~__policy_func()
2196 {
2197 if (__policy_->__destroy)
2198 __policy_->__destroy(__buf_.__large);
2199 }
2200
2201 _LIBCPP_INLINE_VISIBILITY
2202 __policy_func& operator=(__policy_func&& __f)
2203 {
2204 *this = nullptr;
2205 __buf_ = __f.__buf_;
2206 __invoker_ = __f.__invoker_;
2207 __policy_ = __f.__policy_;
2208 __f.__policy_ = __policy::__create_empty();
2209 __f.__invoker_ = __invoker();
2210 return *this;
2211 }
2212
2213 _LIBCPP_INLINE_VISIBILITY
2214 __policy_func& operator=(nullptr_t)
2215 {
2216 const __policy* __p = __policy_;
2217 __policy_ = __policy::__create_empty();
2218 __invoker_ = __invoker();
2219 if (__p->__destroy)
2220 __p->__destroy(__buf_.__large);
2221 return *this;
2222 }
2223
2224 _LIBCPP_INLINE_VISIBILITY
2225 _Rp operator()(_ArgTypes&&... __args) const
2226 {
2227 return __invoker_.__call_(_VSTD::addressof(__buf_),
2228 _VSTD::forward<_ArgTypes>(__args)...);
2229 }
2230
2231 _LIBCPP_INLINE_VISIBILITY
2232 void swap(__policy_func& __f)
2233 {
2234 _VSTD::swap(__invoker_, __f.__invoker_);
2235 _VSTD::swap(__policy_, __f.__policy_);
2236 _VSTD::swap(__buf_, __f.__buf_);
2237 }
2238
2239 _LIBCPP_INLINE_VISIBILITY
2240 explicit operator bool() const _NOEXCEPT
2241 {
2242 return !__policy_->__is_null;
2243 }
2244
2245#ifndef _LIBCPP_NO_RTTI
2246 _LIBCPP_INLINE_VISIBILITY
2247 const std::type_info& target_type() const _NOEXCEPT
2248 {
2249 return *__policy_->__type_info;
2250 }
2251
2252 template <typename _Tp>
2253 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2254 {
2255 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2256 return nullptr;
2257 if (__policy_->__clone) // Out of line storage.
2258 return reinterpret_cast<const _Tp*>(__buf_.__large);
2259 else
2260 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2261 }
2262#endif // _LIBCPP_NO_RTTI
2263};
2264
Louis Dionne3a632922020-04-23 16:47:52 -04002265#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
Louis Dionnee2391d72020-04-22 13:58:17 -04002266
Louis Dionne91cf4442020-07-31 12:56:36 -04002267extern "C" void *_Block_copy(const void *);
2268extern "C" void _Block_release(const void *);
2269
Louis Dionnee2391d72020-04-22 13:58:17 -04002270template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2271class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2272 : public __base<_Rp(_ArgTypes...)>
2273{
2274 typedef _Rp1(^__block_type)(_ArgTypes1...);
2275 __block_type __f_;
2276
2277public:
2278 _LIBCPP_INLINE_VISIBILITY
2279 explicit __func(__block_type const& __f)
Louis Dionne91cf4442020-07-31 12:56:36 -04002280 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002281 { }
2282
2283 // [TODO] add && to save on a retain
2284
2285 _LIBCPP_INLINE_VISIBILITY
2286 explicit __func(__block_type __f, const _Alloc& /* unused */)
Louis Dionne91cf4442020-07-31 12:56:36 -04002287 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002288 { }
2289
2290 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2291 _LIBCPP_ASSERT(false,
2292 "Block pointers are just pointers, so they should always fit into "
2293 "std::function's small buffer optimization. This function should "
2294 "never be invoked.");
2295 return nullptr;
2296 }
2297
2298 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002299 ::new ((void*)__p) __func(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002300 }
2301
2302 virtual void destroy() _NOEXCEPT {
2303 if (__f_)
Louis Dionne91cf4442020-07-31 12:56:36 -04002304 _Block_release(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002305 __f_ = 0;
2306 }
2307
2308 virtual void destroy_deallocate() _NOEXCEPT {
2309 _LIBCPP_ASSERT(false,
2310 "Block pointers are just pointers, so they should always fit into "
2311 "std::function's small buffer optimization. This function should "
2312 "never be invoked.");
2313 }
2314
2315 virtual _Rp operator()(_ArgTypes&& ... __arg) {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05002316 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
Louis Dionnee2391d72020-04-22 13:58:17 -04002317 }
2318
2319#ifndef _LIBCPP_NO_RTTI
2320 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2321 if (__ti == typeid(__func::__block_type))
2322 return &__f_;
2323 return (const void*)nullptr;
2324 }
2325
2326 virtual const std::type_info& target_type() const _NOEXCEPT {
2327 return typeid(__func::__block_type);
2328 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002329#endif // _LIBCPP_NO_RTTI
Louis Dionnee2391d72020-04-22 13:58:17 -04002330};
2331
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002332#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
Louis Dionnee2391d72020-04-22 13:58:17 -04002333
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334} // __function
2335
Howard Hinnantc834c512011-11-29 18:15:50 +00002336template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002337class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002338 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2339 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002341#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002342 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002343#else
2344 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2345#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346
Eric Fiselier125798e2018-12-10 18:14:09 +00002347 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002348
Eric Fiselier3906a132019-06-23 20:28:29 +00002349 template <class _Fp, bool = _And<
2350 _IsNotSame<__uncvref_t<_Fp>, function>,
zoecarver3e722f22020-05-19 17:15:28 -07002351 __invokable<_Fp, _ArgTypes...>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002352 >::value>
2353 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002354 template <class _Fp>
2355 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002356 {
Arthur O'Dwyer4ba8e3d2021-01-11 16:29:17 -05002357 static const bool value = is_void<_Rp>::value ||
2358 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
2359 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002360 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002361 template <class _Fp>
2362 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002363 {
2364 static const bool value = false;
2365 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002366
2367 template <class _Fp>
zoecarver3e722f22020-05-19 17:15:28 -07002368 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002370 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371
Howard Hinnantf06d9262010-08-20 19:36:46 +00002372 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002373 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002374 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002375 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002376 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002377 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002378 function(function&&) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002379 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002380 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381
Marshall Clow3148f422016-10-13 21:06:03 +00002382#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002383 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002384 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002385 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002386 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002387 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002388 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002389 template<class _Alloc>
2390 function(allocator_arg_t, const _Alloc&, const function&);
2391 template<class _Alloc>
2392 function(allocator_arg_t, const _Alloc&, function&&);
zoecarver3e722f22020-05-19 17:15:28 -07002393 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002394 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002395#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396
2397 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002398 function& operator=(function&&) _NOEXCEPT;
2399 function& operator=(nullptr_t) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002400 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002401 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402
2403 ~function();
2404
Howard Hinnantf06d9262010-08-20 19:36:46 +00002405 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002406 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002407
2408#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002409 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002411 void assign(_Fp&& __f, const _Alloc& __a)
2412 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002413#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414
Howard Hinnantf06d9262010-08-20 19:36:46 +00002415 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002416 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002417 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2418 return static_cast<bool>(__f_);
2419 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 // deleted overloads close possible hole in the type system
2422 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002423 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002425 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002427 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002428 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429
Howard Hinnant72f73582010-08-11 17:04:31 +00002430#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002431 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002432 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002433 template <typename _Tp> _Tp* target() _NOEXCEPT;
2434 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002435#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436};
2437
Louis Dionne4af49712019-07-18 19:50:56 +00002438#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2439template<class _Rp, class ..._Ap>
2440function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2441
2442template<class _Fp>
2443struct __strip_signature;
2444
2445template<class _Rp, class _Gp, class ..._Ap>
2446struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2447template<class _Rp, class _Gp, class ..._Ap>
2448struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2449template<class _Rp, class _Gp, class ..._Ap>
2450struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2451template<class _Rp, class _Gp, class ..._Ap>
2452struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2453
2454template<class _Rp, class _Gp, class ..._Ap>
2455struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2456template<class _Rp, class _Gp, class ..._Ap>
2457struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2458template<class _Rp, class _Gp, class ..._Ap>
2459struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2460template<class _Rp, class _Gp, class ..._Ap>
2461struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2462
2463template<class _Rp, class _Gp, class ..._Ap>
2464struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2465template<class _Rp, class _Gp, class ..._Ap>
2466struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2467template<class _Rp, class _Gp, class ..._Ap>
2468struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2469template<class _Rp, class _Gp, class ..._Ap>
2470struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2471
2472template<class _Rp, class _Gp, class ..._Ap>
2473struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2474template<class _Rp, class _Gp, class ..._Ap>
2475struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2476template<class _Rp, class _Gp, class ..._Ap>
2477struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2478template<class _Rp, class _Gp, class ..._Ap>
2479struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2480
2481template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2482function(_Fp) -> function<_Stripped>;
2483#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2484
Howard Hinnantc834c512011-11-29 18:15:50 +00002485template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002486function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487
Marshall Clow3148f422016-10-13 21:06:03 +00002488#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002489template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002490template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002491function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002492 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002493#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002494
Eric Fiselier125798e2018-12-10 18:14:09 +00002495template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002496function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002497 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498
Marshall Clow3148f422016-10-13 21:06:03 +00002499#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002500template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002501template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002502function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002503 function&& __f)
2504 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002505#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002506
Eric Fiselier125798e2018-12-10 18:14:09 +00002507template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002508template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002509function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510
Marshall Clow3148f422016-10-13 21:06:03 +00002511#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002512template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002513template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002514function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2515 _Fp __f)
2516 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002517#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002518
Howard Hinnantc834c512011-11-29 18:15:50 +00002519template<class _Rp, class ..._ArgTypes>
2520function<_Rp(_ArgTypes...)>&
2521function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522{
2523 function(__f).swap(*this);
2524 return *this;
2525}
2526
Howard Hinnantc834c512011-11-29 18:15:50 +00002527template<class _Rp, class ..._ArgTypes>
2528function<_Rp(_ArgTypes...)>&
2529function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002531 __f_ = _VSTD::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002532 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533}
2534
Howard Hinnantc834c512011-11-29 18:15:50 +00002535template<class _Rp, class ..._ArgTypes>
2536function<_Rp(_ArgTypes...)>&
2537function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538{
Eric Fiselier125798e2018-12-10 18:14:09 +00002539 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002540 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541}
2542
Howard Hinnantc834c512011-11-29 18:15:50 +00002543template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002544template <class _Fp, class>
2545function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002546function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547{
Howard Hinnantc834c512011-11-29 18:15:50 +00002548 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549 return *this;
2550}
2551
Howard Hinnantc834c512011-11-29 18:15:50 +00002552template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002553function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554
Howard Hinnantc834c512011-11-29 18:15:50 +00002555template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556void
Howard Hinnantc834c512011-11-29 18:15:50 +00002557function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558{
Eric Fiselier125798e2018-12-10 18:14:09 +00002559 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560}
2561
Howard Hinnantc834c512011-11-29 18:15:50 +00002562template<class _Rp, class ..._ArgTypes>
2563_Rp
2564function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565{
Eric Fiselier125798e2018-12-10 18:14:09 +00002566 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567}
2568
Howard Hinnant72f73582010-08-11 17:04:31 +00002569#ifndef _LIBCPP_NO_RTTI
2570
Howard Hinnantc834c512011-11-29 18:15:50 +00002571template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002573function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574{
Eric Fiselier125798e2018-12-10 18:14:09 +00002575 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576}
2577
Howard Hinnantc834c512011-11-29 18:15:50 +00002578template<class _Rp, class ..._ArgTypes>
2579template <typename _Tp>
2580_Tp*
2581function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582{
Eric Fiselier125798e2018-12-10 18:14:09 +00002583 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584}
2585
Howard Hinnantc834c512011-11-29 18:15:50 +00002586template<class _Rp, class ..._ArgTypes>
2587template <typename _Tp>
2588const _Tp*
2589function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590{
Eric Fiselier125798e2018-12-10 18:14:09 +00002591 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592}
2593
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002594#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002595
Howard Hinnantc834c512011-11-29 18:15:50 +00002596template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597inline _LIBCPP_INLINE_VISIBILITY
2598bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002599operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600
Howard Hinnantc834c512011-11-29 18:15:50 +00002601template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602inline _LIBCPP_INLINE_VISIBILITY
2603bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002604operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605
Howard Hinnantc834c512011-11-29 18:15:50 +00002606template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607inline _LIBCPP_INLINE_VISIBILITY
2608bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002609operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610
Howard Hinnantc834c512011-11-29 18:15:50 +00002611template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612inline _LIBCPP_INLINE_VISIBILITY
2613bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002614operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615
Howard Hinnantc834c512011-11-29 18:15:50 +00002616template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617inline _LIBCPP_INLINE_VISIBILITY
2618void
Howard Hinnantc834c512011-11-29 18:15:50 +00002619swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620{return __x.swap(__y);}
2621
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002622#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002623
2624#include <__functional_03>
2625
2626#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002627
2628////////////////////////////////////////////////////////////////////////////////
2629// BIND
2630//==============================================================================
2631
Howard Hinnantc51e1022010-05-11 19:42:16 +00002632template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002633template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2635
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002636#if _LIBCPP_STD_VER > 14
2637template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002638_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002639#endif
2640
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002642template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2644
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002645#if _LIBCPP_STD_VER > 14
2646template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002647_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002648#endif
2649
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650namespace placeholders
2651{
2652
Howard Hinnantc834c512011-11-29 18:15:50 +00002653template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002655#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002656_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2657_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2658_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2659_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2660_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2661_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2662_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2663_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2664_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2665_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2666#else
Marshall Clow396b2132018-01-02 19:01:45 +00002667/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2668/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2669/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2670/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2671/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2672/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2673/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2674/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2675/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2676/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002677#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678
2679} // placeholders
2680
Howard Hinnantc834c512011-11-29 18:15:50 +00002681template<int _Np>
2682struct __is_placeholder<placeholders::__ph<_Np> >
2683 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002685
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002686#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002687
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688template <class _Tp, class _Uj>
2689inline _LIBCPP_INLINE_VISIBILITY
2690_Tp&
2691__mu(reference_wrapper<_Tp> __t, _Uj&)
2692{
2693 return __t.get();
2694}
2695
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696template <class _Ti, class ..._Uj, size_t ..._Indx>
2697inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002698typename __invoke_of<_Ti&, _Uj...>::type
2699__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002701 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702}
2703
2704template <class _Ti, class ..._Uj>
2705inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002706typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707<
2708 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002709 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710>::type
2711__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2712{
2713 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002714 return _VSTD::__mu_expand(__ti, __uj, __indices());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715}
2716
2717template <bool IsPh, class _Ti, class _Uj>
2718struct __mu_return2 {};
2719
2720template <class _Ti, class _Uj>
2721struct __mu_return2<true, _Ti, _Uj>
2722{
2723 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2724};
2725
2726template <class _Ti, class _Uj>
2727inline _LIBCPP_INLINE_VISIBILITY
2728typename enable_if
2729<
2730 0 < is_placeholder<_Ti>::value,
2731 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2732>::type
2733__mu(_Ti&, _Uj& __uj)
2734{
2735 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002736 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737}
2738
2739template <class _Ti, class _Uj>
2740inline _LIBCPP_INLINE_VISIBILITY
2741typename enable_if
2742<
2743 !is_bind_expression<_Ti>::value &&
2744 is_placeholder<_Ti>::value == 0 &&
2745 !__is_reference_wrapper<_Ti>::value,
2746 _Ti&
2747>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002748__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749{
2750 return __ti;
2751}
2752
Howard Hinnant0415d792011-05-22 15:07:43 +00002753template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2754 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002755struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002757template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002758struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002759{
2760 typedef __nat type;
2761};
2762
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002764struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002766 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767};
2768
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002769template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002770struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2771 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002772{
2773};
2774
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002776struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777{
2778 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2779 _TupleUj>::type&& type;
2780};
2781
2782template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002783struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002784{
2785 typedef typename _Ti::type& type;
2786};
2787
2788template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002789struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790{
2791 typedef _Ti& type;
2792};
2793
2794template <class _Ti, class _TupleUj>
2795struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002796 : public __mu_return_impl<_Ti,
2797 __is_reference_wrapper<_Ti>::value,
2798 is_bind_expression<_Ti>::value,
2799 0 < is_placeholder<_Ti>::value &&
2800 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2801 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802{
2803};
2804
Howard Hinnantc834c512011-11-29 18:15:50 +00002805template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002806struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002807{
2808 static const bool value = false;
2809};
2810
2811template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002812struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002813{
2814 static const bool value = __invokable<_Fp,
2815 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2816};
2817
2818template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002819struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002820{
2821 static const bool value = __invokable<_Fp,
2822 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2823};
2824
2825template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002826 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827struct __bind_return;
2828
Howard Hinnantc834c512011-11-29 18:15:50 +00002829template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002830struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002832 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002834 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 typename __mu_return
2836 <
2837 _BoundArgs,
2838 _TupleUj
2839 >::type...
2840 >::type type;
2841};
2842
Howard Hinnantc834c512011-11-29 18:15:50 +00002843template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002844struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002846 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002848 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 typename __mu_return
2850 <
2851 const _BoundArgs,
2852 _TupleUj
2853 >::type...
2854 >::type type;
2855};
2856
Howard Hinnantc834c512011-11-29 18:15:50 +00002857template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002859typename __bind_return<_Fp, _BoundArgs, _Args>::type
2860__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861 _Args&& __args)
2862{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002863 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864}
2865
Howard Hinnantc834c512011-11-29 18:15:50 +00002866template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002868 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002870protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002871 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002872 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002873private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002874 _Fd __f_;
2875 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876
2877 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2878public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002879 template <class _Gp, class ..._BA,
2880 class = typename enable_if
2881 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002882 is_constructible<_Fd, _Gp>::value &&
2883 !is_same<typename remove_reference<_Gp>::type,
2884 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002885 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002886 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002887 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2888 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002889 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890
2891 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002892 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002893 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894 operator()(_Args&& ...__args)
2895 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002896 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002897 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898 }
2899
2900 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002901 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002902 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903 operator()(_Args&& ...__args) const
2904 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002905 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002906 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 }
2908};
2909
Howard Hinnantc834c512011-11-29 18:15:50 +00002910template<class _Fp, class ..._BoundArgs>
2911struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912
Howard Hinnantc834c512011-11-29 18:15:50 +00002913template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002915 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916{
Howard Hinnantc834c512011-11-29 18:15:50 +00002917 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002918 typedef typename base::_Fd _Fd;
2919 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002921 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922
Howard Hinnant7091e652011-07-02 18:22:36 +00002923
Howard Hinnantf292a922013-07-01 00:01:51 +00002924 template <class _Gp, class ..._BA,
2925 class = typename enable_if
2926 <
2927 is_constructible<_Fd, _Gp>::value &&
2928 !is_same<typename remove_reference<_Gp>::type,
2929 __bind_r>::value
2930 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002931 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002932 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2933 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002934 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935
2936 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002937 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002938 typename enable_if
2939 <
2940 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002941 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002942 result_type
2943 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 operator()(_Args&& ...__args)
2945 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002946 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2947 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948 }
2949
2950 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002951 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002952 typename enable_if
2953 <
2954 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002955 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002956 result_type
2957 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958 operator()(_Args&& ...__args) const
2959 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002960 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2961 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 }
2963};
2964
Howard Hinnantc834c512011-11-29 18:15:50 +00002965template<class _Rp, class _Fp, class ..._BoundArgs>
2966struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967
Howard Hinnantc834c512011-11-29 18:15:50 +00002968template<class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002969inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002970__bind<_Fp, _BoundArgs...>
2971bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972{
Howard Hinnantc834c512011-11-29 18:15:50 +00002973 typedef __bind<_Fp, _BoundArgs...> type;
2974 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975}
2976
Howard Hinnantc834c512011-11-29 18:15:50 +00002977template<class _Rp, class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002978inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002979__bind_r<_Rp, _Fp, _BoundArgs...>
2980bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981{
Howard Hinnantc834c512011-11-29 18:15:50 +00002982 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2983 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984}
2985
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002986#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987
Eric Fiselier0d974f12015-07-14 20:16:15 +00002988#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002989
zoecarver3595cac2021-03-02 16:17:22 -08002990template<class _Op, class _Tuple,
2991 class _Idxs = typename __make_tuple_indices<tuple_size<_Tuple>::value>::type>
2992struct __perfect_forward_impl;
Eric Fiselier934f63b2016-06-02 01:25:41 +00002993
zoecarver3595cac2021-03-02 16:17:22 -08002994template<class _Op, class... _Bound, size_t... _Idxs>
2995struct __perfect_forward_impl<_Op, __tuple_types<_Bound...>, __tuple_indices<_Idxs...>>
2996{
2997 tuple<_Bound...> __bound_;
Eric Fiselier934f63b2016-06-02 01:25:41 +00002998
zoecarver3595cac2021-03-02 16:17:22 -08002999 template<class... _Args>
3000 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &
3001 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3002 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3003 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003004
zoecarver3595cac2021-03-02 16:17:22 -08003005 template<class... _Args>
3006 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&
3007 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3008 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3009 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003010
zoecarver3595cac2021-03-02 16:17:22 -08003011 template<class... _Args>
3012 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &&
3013 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3014 _VSTD::forward<_Args>(__args)...)))
3015 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3016 _VSTD::forward<_Args>(__args)...))
3017 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3018 _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003019
zoecarver3595cac2021-03-02 16:17:22 -08003020 template<class... _Args>
3021 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&&
3022 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3023 _VSTD::forward<_Args>(__args)...)))
3024 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3025 _VSTD::forward<_Args>(__args)...))
3026 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3027 _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003028
zoecarver3595cac2021-03-02 16:17:22 -08003029 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3030 class = _EnableIf<is_copy_constructible_v<_Fn>>>
3031 constexpr __perfect_forward_impl(__perfect_forward_impl const& __other)
3032 : __bound_(__other.__bound_) {}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003033
zoecarver3595cac2021-03-02 16:17:22 -08003034 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3035 class = _EnableIf<is_move_constructible_v<_Fn>>>
3036 constexpr __perfect_forward_impl(__perfect_forward_impl && __other)
3037 : __bound_(_VSTD::move(__other.__bound_)) {}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003038
zoecarver3595cac2021-03-02 16:17:22 -08003039 template<class... _BoundArgs>
3040 explicit constexpr __perfect_forward_impl(_BoundArgs&&... __bound) :
3041 __bound_(_VSTD::forward<_BoundArgs>(__bound)...) { }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003042};
3043
zoecarver3595cac2021-03-02 16:17:22 -08003044template<class _Op, class... _Args>
3045using __perfect_forward =
3046 __perfect_forward_impl<_Op, __tuple_types<decay_t<_Args>...>>;
3047
3048struct __not_fn_op
3049{
3050 template<class... _Args>
3051 static _LIBCPP_CONSTEXPR_AFTER_CXX17 auto __call(_Args&&... __args)
3052 noexcept(noexcept(!_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3053 -> decltype( !_VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3054 { return !_VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3055};
3056
3057template<class _Fn,
3058 class = _EnableIf<is_constructible_v<decay_t<_Fn>, _Fn> &&
3059 is_move_constructible_v<_Fn>>>
3060_LIBCPP_CONSTEXPR_AFTER_CXX17 auto not_fn(_Fn&& __f)
3061{
3062 return __perfect_forward<__not_fn_op, _Fn>(_VSTD::forward<_Fn>(__f));
Eric Fiselier934f63b2016-06-02 01:25:41 +00003063}
3064
zoecarver3595cac2021-03-02 16:17:22 -08003065#endif // _LIBCPP_STD_VER > 14
3066
3067#if _LIBCPP_STD_VER > 17
3068
3069struct __bind_front_op
3070{
3071 template<class... _Args>
3072 constexpr static auto __call(_Args&&... __args)
3073 noexcept(noexcept(_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3074 -> decltype( _VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3075 { return _VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3076};
3077
3078template<class _Fn, class... _Args,
3079 class = _EnableIf<conjunction<is_constructible<decay_t<_Fn>, _Fn>,
3080 is_move_constructible<decay_t<_Fn>>,
3081 is_constructible<decay_t<_Args>, _Args>...,
3082 is_move_constructible<decay_t<_Args>>...
3083 >::value>>
3084constexpr auto bind_front(_Fn&& __f, _Args&&... __args)
3085{
3086 return __perfect_forward<__bind_front_op, _Fn, _Args...>(_VSTD::forward<_Fn>(__f),
3087 _VSTD::forward<_Args>(__args)...);
3088}
3089
3090#endif // _LIBCPP_STD_VER > 17
Eric Fiselier0d974f12015-07-14 20:16:15 +00003091
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003092// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003093
Marshall Clowa40686b2018-01-08 19:18:00 +00003094template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00003095pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00003096__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3097 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
3098 forward_iterator_tag, forward_iterator_tag)
3099{
3100 if (__first2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003101 return _VSTD::make_pair(__first1, __first1); // Everything matches an empty sequence
Marshall Clowa40686b2018-01-08 19:18:00 +00003102 while (true)
3103 {
3104 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
3105 while (true)
3106 {
3107 if (__first1 == __last1) // return __last1 if no element matches *__first2
Logan Smith4528b5f2020-05-07 11:54:25 -04003108 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003109 if (__pred(*__first1, *__first2))
3110 break;
3111 ++__first1;
3112 }
3113 // *__first1 matches *__first2, now match elements after here
3114 _ForwardIterator1 __m1 = __first1;
3115 _ForwardIterator2 __m2 = __first2;
3116 while (true)
3117 {
3118 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Logan Smith4528b5f2020-05-07 11:54:25 -04003119 return _VSTD::make_pair(__first1, __m1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003120 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Logan Smith4528b5f2020-05-07 11:54:25 -04003121 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003122 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
3123 {
3124 ++__first1;
3125 break;
3126 } // else there is a match, check next elements
3127 }
3128 }
3129}
3130
3131template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
3132_LIBCPP_CONSTEXPR_AFTER_CXX11
3133pair<_RandomAccessIterator1, _RandomAccessIterator1>
3134__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
3135 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
3136 random_access_iterator_tag, random_access_iterator_tag)
3137{
3138 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
3139 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
3140 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
3141 const _D2 __len2 = __last2 - __first2;
3142 if (__len2 == 0)
Logan Smith4528b5f2020-05-07 11:54:25 -04003143 return _VSTD::make_pair(__first1, __first1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003144 const _D1 __len1 = __last1 - __first1;
3145 if (__len1 < __len2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003146 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003147 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
3148
3149 while (true)
3150 {
3151 while (true)
3152 {
3153 if (__first1 == __s)
Logan Smith4528b5f2020-05-07 11:54:25 -04003154 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003155 if (__pred(*__first1, *__first2))
3156 break;
3157 ++__first1;
3158 }
3159
3160 _RandomAccessIterator1 __m1 = __first1;
3161 _RandomAccessIterator2 __m2 = __first2;
3162 while (true)
3163 {
3164 if (++__m2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003165 return _VSTD::make_pair(__first1, __first1 + __len2);
Marshall Clowa40686b2018-01-08 19:18:00 +00003166 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
3167 if (!__pred(*__m1, *__m2))
3168 {
3169 ++__first1;
3170 break;
3171 }
3172 }
3173 }
3174}
3175
3176#if _LIBCPP_STD_VER > 14
3177
3178// default searcher
3179template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Martin Storsjö4d6f2212021-04-06 10:55:33 +03003180class _LIBCPP_TEMPLATE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00003181public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003182 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne44bcff92018-08-03 22:36:53 +00003183 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00003184 _BinaryPredicate __p = _BinaryPredicate())
3185 : __first_(__f), __last_(__l), __pred_(__p) {}
3186
3187 template <typename _ForwardIterator2>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003188 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00003189 pair<_ForwardIterator2, _ForwardIterator2>
3190 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3191 {
3192 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04003193 typename iterator_traits<_ForwardIterator>::iterator_category(),
3194 typename iterator_traits<_ForwardIterator2>::iterator_category());
Marshall Clowa40686b2018-01-08 19:18:00 +00003195 }
3196
3197private:
3198 _ForwardIterator __first_;
3199 _ForwardIterator __last_;
3200 _BinaryPredicate __pred_;
Eric Fiselier80663c52019-08-04 07:13:43 +00003201 };
Marshall Clowa40686b2018-01-08 19:18:00 +00003202
3203#endif // _LIBCPP_STD_VER > 14
3204
Louis Dionnedb1892a2018-12-03 14:03:27 +00003205#if _LIBCPP_STD_VER > 17
3206template <class _Tp>
3207using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3208
3209template <class _Tp>
3210using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3211#endif // > C++17
3212
Christopher Di Bellae68ec582021-03-18 17:21:35 +00003213#if _LIBCPP_STD_VER > 17
3214// [func.identity]
3215struct identity {
3216 template<class _Tp>
Arthur O'Dwyer108facb2021-04-05 14:56:03 -04003217 _LIBCPP_NODISCARD_EXT constexpr _Tp&& operator()(_Tp&& __t) const noexcept
Christopher Di Bellae68ec582021-03-18 17:21:35 +00003218 {
3219 return _VSTD::forward<_Tp>(__t);
3220 }
3221
3222 using is_transparent = void;
3223};
3224#endif // _LIBCPP_STD_VER > 17
3225
zoecarver9ce102c2021-04-22 17:33:04 -07003226#if !defined(_LIBCPP_HAS_NO_RANGES)
3227
3228namespace ranges {
3229
3230struct equal_to {
3231 template <class _Tp, class _Up>
3232 requires equality_comparable_with<_Tp, _Up>
3233 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3234 noexcept(noexcept(bool(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u)))) {
3235 return _VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u);
3236 }
3237
3238 using is_transparent = void;
3239};
3240
3241struct not_equal_to {
3242 template <class _Tp, class _Up>
3243 requires equality_comparable_with<_Tp, _Up>
3244 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3245 noexcept(noexcept(bool(!(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u))))) {
3246 return !(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u));
3247 }
3248
3249 using is_transparent = void;
3250};
3251
3252struct greater {
3253 template <class _Tp, class _Up>
3254 requires totally_ordered_with<_Tp, _Up>
3255 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3256 noexcept(noexcept(bool(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t)))) {
3257 return _VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t);
3258 }
3259
3260 using is_transparent = void;
3261};
3262
3263struct less {
3264 template <class _Tp, class _Up>
3265 requires totally_ordered_with<_Tp, _Up>
3266 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3267 noexcept(noexcept(bool(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u)))) {
3268 return _VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u);
3269 }
3270
3271 using is_transparent = void;
3272};
3273
3274struct greater_equal {
3275 template <class _Tp, class _Up>
3276 requires totally_ordered_with<_Tp, _Up>
3277 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3278 noexcept(noexcept(bool(!(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u))))) {
3279 return !(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u));
3280 }
3281
3282 using is_transparent = void;
3283};
3284
3285struct less_equal {
3286 template <class _Tp, class _Up>
3287 requires totally_ordered_with<_Tp, _Up>
3288 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3289 noexcept(noexcept(bool(!(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t))))) {
3290 return !(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t));
3291 }
3292
3293 using is_transparent = void;
3294};
3295
3296} // namespace ranges
3297
3298#endif // !defined(_LIBCPP_HAS_NO_RANGES)
3299
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300_LIBCPP_END_NAMESPACE_STD
3301
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003302#endif // _LIBCPP_FUNCTIONAL