blob: 62e9373ad14d0ca4f7133641ce19c590e331fdc5 [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>
510#include <type_traits>
511#include <typeinfo>
512#include <exception>
513#include <memory>
514#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000515#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000516#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517
518#include <__functional_base>
519
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000520#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000522#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523
524_LIBCPP_BEGIN_NAMESPACE_STD
525
Marshall Clow974bae22013-07-29 14:21:53 +0000526#if _LIBCPP_STD_VER > 11
527template <class _Tp = void>
528#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000530#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000531struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532{
Marshall Clowd18ee652013-09-28 19:06:12 +0000533 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
534 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535 {return __x + __y;}
536};
537
Marshall Clow974bae22013-07-29 14:21:53 +0000538#if _LIBCPP_STD_VER > 11
539template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000540struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000541{
542 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000543 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
544 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000545 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
546 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
547 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000548 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000549};
550#endif
551
552
553#if _LIBCPP_STD_VER > 11
554template <class _Tp = void>
555#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000557#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000558struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559{
Marshall Clowd18ee652013-09-28 19:06:12 +0000560 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
561 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 {return __x - __y;}
563};
564
Marshall Clow974bae22013-07-29 14:21:53 +0000565#if _LIBCPP_STD_VER > 11
566template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000567struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000568{
569 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000570 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
571 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000572 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
573 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
574 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000575 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000576};
577#endif
578
579
580#if _LIBCPP_STD_VER > 11
581template <class _Tp = void>
582#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000583template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000584#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000585struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586{
Marshall Clowd18ee652013-09-28 19:06:12 +0000587 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
588 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589 {return __x * __y;}
590};
591
Marshall Clow974bae22013-07-29 14:21:53 +0000592#if _LIBCPP_STD_VER > 11
593template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000594struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000595{
596 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000597 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
598 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000599 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
600 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
601 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000602 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000603};
604#endif
605
606
607#if _LIBCPP_STD_VER > 11
608template <class _Tp = void>
609#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000611#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000612struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613{
Marshall Clowd18ee652013-09-28 19:06:12 +0000614 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
615 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616 {return __x / __y;}
617};
618
Marshall Clow974bae22013-07-29 14:21:53 +0000619#if _LIBCPP_STD_VER > 11
620template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000621struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000622{
623 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000624 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
625 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000626 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
627 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
628 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000629 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000630};
631#endif
632
633
634#if _LIBCPP_STD_VER > 11
635template <class _Tp = void>
636#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000638#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000639struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640{
Marshall Clowd18ee652013-09-28 19:06:12 +0000641 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
642 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643 {return __x % __y;}
644};
645
Marshall Clow974bae22013-07-29 14:21:53 +0000646#if _LIBCPP_STD_VER > 11
647template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000648struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000649{
650 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000651 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
652 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000653 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
654 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
655 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000656 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000657};
658#endif
659
660
661#if _LIBCPP_STD_VER > 11
662template <class _Tp = void>
663#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000665#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000666struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667{
Marshall Clowd18ee652013-09-28 19:06:12 +0000668 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
669 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 {return -__x;}
671};
672
Marshall Clow974bae22013-07-29 14:21:53 +0000673#if _LIBCPP_STD_VER > 11
674template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000675struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000676{
677 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000678 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
679 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000680 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
681 -> decltype (- _VSTD::forward<_Tp>(__x))
682 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000683 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000684};
685#endif
686
687
688#if _LIBCPP_STD_VER > 11
689template <class _Tp = void>
690#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000691template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000692#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000693struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694{
Marshall Clowd18ee652013-09-28 19:06:12 +0000695 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
696 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697 {return __x == __y;}
698};
699
Marshall Clow974bae22013-07-29 14:21:53 +0000700#if _LIBCPP_STD_VER > 11
701template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000702struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000703{
Marshall Clowd18ee652013-09-28 19:06:12 +0000704 template <class _T1, class _T2>
705 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000706 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000707 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
708 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
709 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000710 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000711};
712#endif
713
714
715#if _LIBCPP_STD_VER > 11
716template <class _Tp = void>
717#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000719#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000720struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721{
Marshall Clowd18ee652013-09-28 19:06:12 +0000722 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
723 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724 {return __x != __y;}
725};
726
Marshall Clow974bae22013-07-29 14:21:53 +0000727#if _LIBCPP_STD_VER > 11
728template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000729struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000730{
Marshall Clowd18ee652013-09-28 19:06:12 +0000731 template <class _T1, class _T2>
732 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000733 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000734 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
735 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
736 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000737 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000738};
739#endif
740
741
742#if _LIBCPP_STD_VER > 11
743template <class _Tp = void>
744#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000746#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000747struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748{
Marshall Clowd18ee652013-09-28 19:06:12 +0000749 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
750 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751 {return __x > __y;}
752};
753
Marshall Clow974bae22013-07-29 14:21:53 +0000754#if _LIBCPP_STD_VER > 11
755template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000756struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000757{
Marshall Clowd18ee652013-09-28 19:06:12 +0000758 template <class _T1, class _T2>
759 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000760 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000761 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
762 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
763 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000764 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000765};
766#endif
767
768
Howard Hinnantb17caf92012-02-21 21:02:58 +0000769// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770
Marshall Clow974bae22013-07-29 14:21:53 +0000771#if _LIBCPP_STD_VER > 11
772template <class _Tp = void>
773#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000775#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000776struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777{
Marshall Clowd18ee652013-09-28 19:06:12 +0000778 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
779 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780 {return __x >= __y;}
781};
782
Marshall Clow974bae22013-07-29 14:21:53 +0000783#if _LIBCPP_STD_VER > 11
784template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000785struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000786{
Marshall Clowd18ee652013-09-28 19:06:12 +0000787 template <class _T1, class _T2>
788 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000789 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000790 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
791 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
792 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000793 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000794};
795#endif
796
797
798#if _LIBCPP_STD_VER > 11
799template <class _Tp = void>
800#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000802#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000803struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804{
Marshall Clowd18ee652013-09-28 19:06:12 +0000805 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
806 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807 {return __x <= __y;}
808};
809
Marshall Clow974bae22013-07-29 14:21:53 +0000810#if _LIBCPP_STD_VER > 11
811template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000812struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000813{
Marshall Clowd18ee652013-09-28 19:06:12 +0000814 template <class _T1, class _T2>
815 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000816 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000817 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
818 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
819 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000820 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000821};
822#endif
823
824
825#if _LIBCPP_STD_VER > 11
826template <class _Tp = void>
827#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000829#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000830struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831{
Marshall Clowd18ee652013-09-28 19:06:12 +0000832 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
833 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834 {return __x && __y;}
835};
836
Marshall Clow974bae22013-07-29 14:21:53 +0000837#if _LIBCPP_STD_VER > 11
838template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000839struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000840{
Marshall Clowd18ee652013-09-28 19:06:12 +0000841 template <class _T1, class _T2>
842 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000843 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000844 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
845 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
846 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000847 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000848};
849#endif
850
851
852#if _LIBCPP_STD_VER > 11
853template <class _Tp = void>
854#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000856#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000857struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000858{
Marshall Clowd18ee652013-09-28 19:06:12 +0000859 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
860 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861 {return __x || __y;}
862};
863
Marshall Clow974bae22013-07-29 14:21:53 +0000864#if _LIBCPP_STD_VER > 11
865template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000866struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000867{
Marshall Clowd18ee652013-09-28 19:06:12 +0000868 template <class _T1, class _T2>
869 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000870 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000871 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
872 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
873 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000874 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000875};
876#endif
877
878
879#if _LIBCPP_STD_VER > 11
880template <class _Tp = void>
881#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000883#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000884struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885{
Marshall Clowd18ee652013-09-28 19:06:12 +0000886 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
887 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888 {return !__x;}
889};
890
Marshall Clow974bae22013-07-29 14:21:53 +0000891#if _LIBCPP_STD_VER > 11
892template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000893struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000894{
895 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000896 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
897 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000898 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
899 -> decltype (!_VSTD::forward<_Tp>(__x))
900 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000901 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000902};
903#endif
904
905
906#if _LIBCPP_STD_VER > 11
907template <class _Tp = void>
908#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000910#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000911struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912{
Marshall Clowd18ee652013-09-28 19:06:12 +0000913 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
914 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915 {return __x & __y;}
916};
917
Marshall Clow974bae22013-07-29 14:21:53 +0000918#if _LIBCPP_STD_VER > 11
919template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000920struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000921{
Marshall Clowd18ee652013-09-28 19:06:12 +0000922 template <class _T1, class _T2>
923 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000924 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000925 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
926 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
927 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000928 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000929};
930#endif
931
932
933#if _LIBCPP_STD_VER > 11
934template <class _Tp = void>
935#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000937#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000938struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939{
Marshall Clowd18ee652013-09-28 19:06:12 +0000940 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
941 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942 {return __x | __y;}
943};
944
Marshall Clow974bae22013-07-29 14:21:53 +0000945#if _LIBCPP_STD_VER > 11
946template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000947struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000948{
Marshall Clowd18ee652013-09-28 19:06:12 +0000949 template <class _T1, class _T2>
950 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000951 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000952 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
953 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
954 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000955 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000956};
957#endif
958
959
960#if _LIBCPP_STD_VER > 11
961template <class _Tp = void>
962#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000964#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000965struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966{
Marshall Clowd18ee652013-09-28 19:06:12 +0000967 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
968 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 {return __x ^ __y;}
970};
971
Marshall Clow974bae22013-07-29 14:21:53 +0000972#if _LIBCPP_STD_VER > 11
973template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000974struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000975{
Marshall Clowd18ee652013-09-28 19:06:12 +0000976 template <class _T1, class _T2>
977 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000978 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000979 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
980 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
981 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000982 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000983};
984#endif
985
986
987#if _LIBCPP_STD_VER > 11
988template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000989struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000990{
Marshall Clowd18ee652013-09-28 19:06:12 +0000991 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
992 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000993 {return ~__x;}
994};
995
996template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000997struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000998{
999 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +00001000 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1001 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +00001002 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
1003 -> decltype (~_VSTD::forward<_Tp>(__x))
1004 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +00001005 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +00001006};
1007#endif
1008
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001010class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 : public unary_function<typename _Predicate::argument_type, bool>
1012{
1013 _Predicate __pred_;
1014public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001015 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1016 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001018 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1019 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020 {return !__pred_(__x);}
1021};
1022
1023template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001024_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025unary_negate<_Predicate>
1026not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1027
1028template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001029class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 : public binary_function<typename _Predicate::first_argument_type,
1031 typename _Predicate::second_argument_type,
1032 bool>
1033{
1034 _Predicate __pred_;
1035public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001036 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001037 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1038
1039 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1040 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 const typename _Predicate::second_argument_type& __y) const
1042 {return !__pred_(__x, __y);}
1043};
1044
1045template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001046_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047binary_negate<_Predicate>
1048not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1049
Marshall Clow26a027c2017-04-13 18:25:32 +00001050#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001052class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 : public unary_function<typename __Operation::second_argument_type,
1054 typename __Operation::result_type>
1055{
1056protected:
1057 __Operation op;
1058 typename __Operation::first_argument_type value;
1059public:
1060 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1061 const typename __Operation::first_argument_type __y)
1062 : op(__x), value(__y) {}
1063 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1064 (typename __Operation::second_argument_type& __x) const
1065 {return op(value, __x);}
1066 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1067 (const typename __Operation::second_argument_type& __x) const
1068 {return op(value, __x);}
1069};
1070
1071template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001072_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073binder1st<__Operation>
1074bind1st(const __Operation& __op, const _Tp& __x)
1075 {return binder1st<__Operation>(__op, __x);}
1076
1077template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001078class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 : public unary_function<typename __Operation::first_argument_type,
1080 typename __Operation::result_type>
1081{
1082protected:
1083 __Operation op;
1084 typename __Operation::second_argument_type value;
1085public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1088 : op(__x), value(__y) {}
1089 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1090 ( typename __Operation::first_argument_type& __x) const
1091 {return op(__x, value);}
1092 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1093 (const typename __Operation::first_argument_type& __x) const
1094 {return op(__x, value);}
1095};
1096
1097template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001098_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099binder2nd<__Operation>
1100bind2nd(const __Operation& __op, const _Tp& __x)
1101 {return binder2nd<__Operation>(__op, __x);}
1102
1103template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001104class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001105 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106{
1107 _Result (*__f_)(_Arg);
1108public:
1109 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1110 : __f_(__f) {}
1111 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1112 {return __f_(__x);}
1113};
1114
1115template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001116_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117pointer_to_unary_function<_Arg,_Result>
1118ptr_fun(_Result (*__f)(_Arg))
1119 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1120
1121template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001122class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001123 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124{
1125 _Result (*__f_)(_Arg1, _Arg2);
1126public:
1127 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1128 : __f_(__f) {}
1129 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1130 {return __f_(__x, __y);}
1131};
1132
1133template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001134_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135pointer_to_binary_function<_Arg1,_Arg2,_Result>
1136ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1137 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1138
1139template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001140class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1141 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142{
1143 _Sp (_Tp::*__p_)();
1144public:
1145 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1146 : __p_(__p) {}
1147 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1148 {return (__p->*__p_)();}
1149};
1150
1151template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001152class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1153 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154{
1155 _Sp (_Tp::*__p_)(_Ap);
1156public:
1157 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1158 : __p_(__p) {}
1159 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1160 {return (__p->*__p_)(__x);}
1161};
1162
1163template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001164_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165mem_fun_t<_Sp,_Tp>
1166mem_fun(_Sp (_Tp::*__f)())
1167 {return mem_fun_t<_Sp,_Tp>(__f);}
1168
1169template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001170_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171mem_fun1_t<_Sp,_Tp,_Ap>
1172mem_fun(_Sp (_Tp::*__f)(_Ap))
1173 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1174
1175template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001176class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1177 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178{
1179 _Sp (_Tp::*__p_)();
1180public:
1181 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1182 : __p_(__p) {}
1183 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1184 {return (__p.*__p_)();}
1185};
1186
1187template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001188class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1189 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190{
1191 _Sp (_Tp::*__p_)(_Ap);
1192public:
1193 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1194 : __p_(__p) {}
1195 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1196 {return (__p.*__p_)(__x);}
1197};
1198
1199template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001200_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201mem_fun_ref_t<_Sp,_Tp>
1202mem_fun_ref(_Sp (_Tp::*__f)())
1203 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1204
1205template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001206_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207mem_fun1_ref_t<_Sp,_Tp,_Ap>
1208mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1209 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1210
1211template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001212class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1213 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214{
1215 _Sp (_Tp::*__p_)() const;
1216public:
1217 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1218 : __p_(__p) {}
1219 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1220 {return (__p->*__p_)();}
1221};
1222
1223template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001224class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1225 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226{
1227 _Sp (_Tp::*__p_)(_Ap) const;
1228public:
1229 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1230 : __p_(__p) {}
1231 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1232 {return (__p->*__p_)(__x);}
1233};
1234
1235template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001236_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237const_mem_fun_t<_Sp,_Tp>
1238mem_fun(_Sp (_Tp::*__f)() const)
1239 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1240
1241template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001242_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243const_mem_fun1_t<_Sp,_Tp,_Ap>
1244mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1245 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1246
1247template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001248class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1249 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250{
1251 _Sp (_Tp::*__p_)() const;
1252public:
1253 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1254 : __p_(__p) {}
1255 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1256 {return (__p.*__p_)();}
1257};
1258
1259template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001260class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001261 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262{
1263 _Sp (_Tp::*__p_)(_Ap) const;
1264public:
1265 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1266 : __p_(__p) {}
1267 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1268 {return (__p.*__p_)(__x);}
1269};
1270
1271template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001272_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273const_mem_fun_ref_t<_Sp,_Tp>
1274mem_fun_ref(_Sp (_Tp::*__f)() const)
1275 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1276
1277template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001278_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1280mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1281 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001284////////////////////////////////////////////////////////////////////////////////
1285// MEMFUN
1286//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288template <class _Tp>
1289class __mem_fn
1290 : public __weak_result_type<_Tp>
1291{
1292public:
1293 // types
1294 typedef _Tp type;
1295private:
1296 type __f_;
1297
1298public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001299 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1300 __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001302#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303 // invoke
1304 template <class... _ArgTypes>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001305 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier2cc48332015-07-22 22:43:27 +00001306 typename __invoke_return<type, _ArgTypes...>::type
1307 operator() (_ArgTypes&&... __args) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001308 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001309 }
1310#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001311
1312 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001313 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001314 typename __invoke_return0<type, _A0>::type
1315 operator() (_A0& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001316 return _VSTD::__invoke(__f_, __a0);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001317 }
1318
Eric Fiselierce1813a2015-08-26 20:15:02 +00001319 template <class _A0>
1320 _LIBCPP_INLINE_VISIBILITY
1321 typename __invoke_return0<type, _A0 const>::type
1322 operator() (_A0 const& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001323 return _VSTD::__invoke(__f_, __a0);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001324 }
1325
Eric Fiselier2cc48332015-07-22 22:43:27 +00001326 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001327 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001328 typename __invoke_return1<type, _A0, _A1>::type
1329 operator() (_A0& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001330 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001331 }
1332
Eric Fiselierce1813a2015-08-26 20:15:02 +00001333 template <class _A0, class _A1>
1334 _LIBCPP_INLINE_VISIBILITY
1335 typename __invoke_return1<type, _A0 const, _A1>::type
1336 operator() (_A0 const& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001337 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001338 }
1339
1340 template <class _A0, class _A1>
1341 _LIBCPP_INLINE_VISIBILITY
1342 typename __invoke_return1<type, _A0, _A1 const>::type
1343 operator() (_A0& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001344 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001345 }
1346
1347 template <class _A0, class _A1>
1348 _LIBCPP_INLINE_VISIBILITY
1349 typename __invoke_return1<type, _A0 const, _A1 const>::type
1350 operator() (_A0 const& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001351 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001352 }
1353
Eric Fiselier2cc48332015-07-22 22:43:27 +00001354 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001355 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001356 typename __invoke_return2<type, _A0, _A1, _A2>::type
1357 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001358 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001359 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001360
1361 template <class _A0, class _A1, class _A2>
1362 _LIBCPP_INLINE_VISIBILITY
1363 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1364 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001365 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001366 }
1367
1368 template <class _A0, class _A1, class _A2>
1369 _LIBCPP_INLINE_VISIBILITY
1370 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1371 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001372 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001373 }
1374
1375 template <class _A0, class _A1, class _A2>
1376 _LIBCPP_INLINE_VISIBILITY
1377 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1378 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001379 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001380 }
1381
1382 template <class _A0, class _A1, class _A2>
1383 _LIBCPP_INLINE_VISIBILITY
1384 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1385 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001386 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001387 }
1388
1389 template <class _A0, class _A1, class _A2>
1390 _LIBCPP_INLINE_VISIBILITY
1391 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1392 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001393 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001394 }
1395
1396 template <class _A0, class _A1, class _A2>
1397 _LIBCPP_INLINE_VISIBILITY
1398 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1399 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001400 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001401 }
1402
1403 template <class _A0, class _A1, class _A2>
1404 _LIBCPP_INLINE_VISIBILITY
1405 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1406 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001407 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001408 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001409#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410};
1411
Howard Hinnantc834c512011-11-29 18:15:50 +00001412template<class _Rp, class _Tp>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001413inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00001414__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001415mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416{
Howard Hinnantc834c512011-11-29 18:15:50 +00001417 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418}
1419
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001420////////////////////////////////////////////////////////////////////////////////
1421// FUNCTION
1422//==============================================================================
1423
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424// bad_function_call
1425
Howard Hinnant4ff57432010-09-21 22:55:27 +00001426class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427 : public exception
1428{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001429#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1430public:
1431 virtual ~bad_function_call() _NOEXCEPT;
1432
1433 virtual const char* what() const _NOEXCEPT;
1434#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435};
1436
Louis Dionne16fe2952018-07-11 23:14:33 +00001437_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001438void __throw_bad_function_call()
1439{
1440#ifndef _LIBCPP_NO_EXCEPTIONS
1441 throw bad_function_call();
1442#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001443 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001444#endif
1445}
1446
Louis Dionne44d1f812020-03-09 11:16:22 -04001447#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1448# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1449 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1450#else
1451# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1452#endif
1453
1454template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455
1456namespace __function
1457{
1458
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001459template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460struct __maybe_derive_from_unary_function
1461{
1462};
1463
Howard Hinnantc834c512011-11-29 18:15:50 +00001464template<class _Rp, class _A1>
1465struct __maybe_derive_from_unary_function<_Rp(_A1)>
1466 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467{
1468};
1469
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001470template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471struct __maybe_derive_from_binary_function
1472{
1473};
1474
Howard Hinnantc834c512011-11-29 18:15:50 +00001475template<class _Rp, class _A1, class _A2>
1476struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1477 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478{
1479};
1480
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001481template <class _Fp>
1482_LIBCPP_INLINE_VISIBILITY
1483bool __not_null(_Fp const&) { return true; }
1484
1485template <class _Fp>
1486_LIBCPP_INLINE_VISIBILITY
1487bool __not_null(_Fp* __ptr) { return __ptr; }
1488
1489template <class _Ret, class _Class>
1490_LIBCPP_INLINE_VISIBILITY
1491bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1492
1493template <class _Fp>
1494_LIBCPP_INLINE_VISIBILITY
1495bool __not_null(function<_Fp> const& __f) { return !!__f; }
1496
Louis Dionnee2391d72020-04-22 13:58:17 -04001497#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1498template <class _Rp, class ..._Args>
1499_LIBCPP_INLINE_VISIBILITY
1500bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1501#endif
1502
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001503} // namespace __function
1504
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001505#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001506
1507namespace __function {
1508
Eric Fiselier125798e2018-12-10 18:14:09 +00001509// __alloc_func holds a functor and an allocator.
1510
1511template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001512template <class _Fp, class _FB>
1513class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001514
1515template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1516class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1517{
1518 __compressed_pair<_Fp, _Ap> __f_;
1519
1520 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001521 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1522 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001523
1524 _LIBCPP_INLINE_VISIBILITY
1525 const _Target& __target() const { return __f_.first(); }
1526
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001527 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001528 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001529 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001530
1531 _LIBCPP_INLINE_VISIBILITY
1532 explicit __alloc_func(_Target&& __f)
1533 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1534 _VSTD::forward_as_tuple())
1535 {
1536 }
1537
1538 _LIBCPP_INLINE_VISIBILITY
1539 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1540 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1541 _VSTD::forward_as_tuple(__a))
1542 {
1543 }
1544
1545 _LIBCPP_INLINE_VISIBILITY
1546 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1547 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1548 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1549 {
1550 }
1551
1552 _LIBCPP_INLINE_VISIBILITY
1553 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1554 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1555 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1556 {
1557 }
1558
1559 _LIBCPP_INLINE_VISIBILITY
1560 _Rp operator()(_ArgTypes&&... __arg)
1561 {
1562 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1563 return _Invoker::__call(__f_.first(),
1564 _VSTD::forward<_ArgTypes>(__arg)...);
1565 }
1566
1567 _LIBCPP_INLINE_VISIBILITY
1568 __alloc_func* __clone() const
1569 {
1570 typedef allocator_traits<_Alloc> __alloc_traits;
1571 typedef
1572 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1573 _AA;
1574 _AA __a(__f_.second());
1575 typedef __allocator_destructor<_AA> _Dp;
1576 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1577 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1578 return __hold.release();
1579 }
1580
1581 _LIBCPP_INLINE_VISIBILITY
1582 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001583
1584 static void __destroy_and_delete(__alloc_func* __f) {
1585 typedef allocator_traits<_Alloc> __alloc_traits;
1586 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1587 _FunAlloc;
1588 _FunAlloc __a(__f->__get_allocator());
1589 __f->destroy();
1590 __a.deallocate(__f, 1);
1591 }
1592};
1593
1594template <class _Fp, class _Rp, class... _ArgTypes>
1595class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1596 _Fp __f_;
1597
1598public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001599 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001600
1601 _LIBCPP_INLINE_VISIBILITY
1602 const _Target& __target() const { return __f_; }
1603
1604 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001605 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001606
1607 _LIBCPP_INLINE_VISIBILITY
1608 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1609
1610 _LIBCPP_INLINE_VISIBILITY
1611 _Rp operator()(_ArgTypes&&... __arg) {
1612 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1613 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1614 }
1615
1616 _LIBCPP_INLINE_VISIBILITY
1617 __default_alloc_func* __clone() const {
1618 __builtin_new_allocator::__holder_t __hold =
1619 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1620 __default_alloc_func* __res =
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001621 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00001622 (void)__hold.release();
1623 return __res;
1624 }
1625
1626 _LIBCPP_INLINE_VISIBILITY
1627 void destroy() _NOEXCEPT { __f_.~_Target(); }
1628
1629 static void __destroy_and_delete(__default_alloc_func* __f) {
1630 __f->destroy();
1631 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1632 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001633};
1634
1635// __base provides an abstract interface for copyable functors.
1636
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001637template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638
Howard Hinnantc834c512011-11-29 18:15:50 +00001639template<class _Rp, class ..._ArgTypes>
1640class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641{
1642 __base(const __base&);
1643 __base& operator=(const __base&);
1644public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001645 _LIBCPP_INLINE_VISIBILITY __base() {}
1646 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647 virtual __base* __clone() const = 0;
1648 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001649 virtual void destroy() _NOEXCEPT = 0;
1650 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001651 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001652#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001653 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1654 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001655#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656};
1657
Eric Fiselier125798e2018-12-10 18:14:09 +00001658// __func implements __base for a given functor type.
1659
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660template<class _FD, class _Alloc, class _FB> class __func;
1661
Howard Hinnantc834c512011-11-29 18:15:50 +00001662template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1663class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1664 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665{
Eric Fiselier125798e2018-12-10 18:14:09 +00001666 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001669 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001670 : __f_(_VSTD::move(__f)) {}
1671
Howard Hinnant4ff57432010-09-21 22:55:27 +00001672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001673 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001674 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001675
1676 _LIBCPP_INLINE_VISIBILITY
1677 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001678 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001679
1680 _LIBCPP_INLINE_VISIBILITY
1681 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001682 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1683
Howard Hinnantc834c512011-11-29 18:15:50 +00001684 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1685 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001686 virtual void destroy() _NOEXCEPT;
1687 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001688 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001689#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001690 virtual const void* target(const type_info&) const _NOEXCEPT;
1691 virtual const std::type_info& target_type() const _NOEXCEPT;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001692#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693};
1694
Howard Hinnantc834c512011-11-29 18:15:50 +00001695template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1696__base<_Rp(_ArgTypes...)>*
1697__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001699 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001700 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001701 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001702 typedef __allocator_destructor<_Ap> _Dp;
1703 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001704 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 return __hold.release();
1706}
1707
Howard Hinnantc834c512011-11-29 18:15:50 +00001708template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709void
Howard Hinnantc834c512011-11-29 18:15:50 +00001710__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711{
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001712 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713}
1714
Howard Hinnantc834c512011-11-29 18:15:50 +00001715template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716void
Howard Hinnantc834c512011-11-29 18:15:50 +00001717__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718{
Eric Fiselier125798e2018-12-10 18:14:09 +00001719 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720}
1721
Howard Hinnantc834c512011-11-29 18:15:50 +00001722template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723void
Howard Hinnantc834c512011-11-29 18:15:50 +00001724__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001726 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001727 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001728 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001729 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 __a.deallocate(this, 1);
1731}
1732
Howard Hinnantc834c512011-11-29 18:15:50 +00001733template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1734_Rp
1735__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736{
Eric Fiselier125798e2018-12-10 18:14:09 +00001737 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738}
1739
Howard Hinnant72f73582010-08-11 17:04:31 +00001740#ifndef _LIBCPP_NO_RTTI
1741
Howard Hinnantc834c512011-11-29 18:15:50 +00001742template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001744__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745{
Howard Hinnantc834c512011-11-29 18:15:50 +00001746 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001747 return &__f_.__target();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001748 return nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749}
1750
Howard Hinnantc834c512011-11-29 18:15:50 +00001751template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001753__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754{
Howard Hinnantc834c512011-11-29 18:15:50 +00001755 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756}
1757
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001758#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001759
Eric Fiselier125798e2018-12-10 18:14:09 +00001760// __value_func creates a value-type from a __func.
1761
1762template <class _Fp> class __value_func;
1763
1764template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1765{
1766 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1767
1768 typedef __base<_Rp(_ArgTypes...)> __func;
1769 __func* __f_;
1770
1771 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1772 {
1773 return reinterpret_cast<__func*>(p);
1774 }
1775
1776 public:
1777 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001778 __value_func() _NOEXCEPT : __f_(nullptr) {}
Eric Fiselier125798e2018-12-10 18:14:09 +00001779
1780 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001781 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001782 : __f_(nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001783 {
1784 typedef allocator_traits<_Alloc> __alloc_traits;
1785 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1786 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1787 _FunAlloc;
1788
1789 if (__function::__not_null(__f))
1790 {
1791 _FunAlloc __af(__a);
1792 if (sizeof(_Fun) <= sizeof(__buf_) &&
1793 is_nothrow_copy_constructible<_Fp>::value &&
1794 is_nothrow_copy_constructible<_FunAlloc>::value)
1795 {
1796 __f_ =
1797 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1798 }
1799 else
1800 {
1801 typedef __allocator_destructor<_FunAlloc> _Dp;
1802 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1803 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1804 __f_ = __hold.release();
1805 }
1806 }
1807 }
1808
Eric Fiselier74ebee62019-06-08 01:31:19 +00001809 template <class _Fp,
1810 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1811 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001812 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001813
Eric Fiselier125798e2018-12-10 18:14:09 +00001814 _LIBCPP_INLINE_VISIBILITY
1815 __value_func(const __value_func& __f)
1816 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001817 if (__f.__f_ == nullptr)
1818 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001819 else if ((void*)__f.__f_ == &__f.__buf_)
1820 {
1821 __f_ = __as_base(&__buf_);
1822 __f.__f_->__clone(__f_);
1823 }
1824 else
1825 __f_ = __f.__f_->__clone();
1826 }
1827
1828 _LIBCPP_INLINE_VISIBILITY
1829 __value_func(__value_func&& __f) _NOEXCEPT
1830 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001831 if (__f.__f_ == nullptr)
1832 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001833 else if ((void*)__f.__f_ == &__f.__buf_)
1834 {
1835 __f_ = __as_base(&__buf_);
1836 __f.__f_->__clone(__f_);
1837 }
1838 else
1839 {
1840 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001841 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001842 }
1843 }
1844
1845 _LIBCPP_INLINE_VISIBILITY
1846 ~__value_func()
1847 {
1848 if ((void*)__f_ == &__buf_)
1849 __f_->destroy();
1850 else if (__f_)
1851 __f_->destroy_deallocate();
1852 }
1853
1854 _LIBCPP_INLINE_VISIBILITY
1855 __value_func& operator=(__value_func&& __f)
1856 {
1857 *this = nullptr;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001858 if (__f.__f_ == nullptr)
1859 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001860 else if ((void*)__f.__f_ == &__f.__buf_)
1861 {
1862 __f_ = __as_base(&__buf_);
1863 __f.__f_->__clone(__f_);
1864 }
1865 else
1866 {
1867 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001868 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001869 }
1870 return *this;
1871 }
1872
1873 _LIBCPP_INLINE_VISIBILITY
1874 __value_func& operator=(nullptr_t)
1875 {
1876 __func* __f = __f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001877 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001878 if ((void*)__f == &__buf_)
1879 __f->destroy();
1880 else if (__f)
1881 __f->destroy_deallocate();
1882 return *this;
1883 }
1884
1885 _LIBCPP_INLINE_VISIBILITY
1886 _Rp operator()(_ArgTypes&&... __args) const
1887 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001888 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001889 __throw_bad_function_call();
1890 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1891 }
1892
1893 _LIBCPP_INLINE_VISIBILITY
1894 void swap(__value_func& __f) _NOEXCEPT
1895 {
1896 if (&__f == this)
1897 return;
1898 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1899 {
1900 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1901 __func* __t = __as_base(&__tempbuf);
1902 __f_->__clone(__t);
1903 __f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001904 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001905 __f.__f_->__clone(__as_base(&__buf_));
1906 __f.__f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001907 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001908 __f_ = __as_base(&__buf_);
1909 __t->__clone(__as_base(&__f.__buf_));
1910 __t->destroy();
1911 __f.__f_ = __as_base(&__f.__buf_);
1912 }
1913 else if ((void*)__f_ == &__buf_)
1914 {
1915 __f_->__clone(__as_base(&__f.__buf_));
1916 __f_->destroy();
1917 __f_ = __f.__f_;
1918 __f.__f_ = __as_base(&__f.__buf_);
1919 }
1920 else if ((void*)__f.__f_ == &__f.__buf_)
1921 {
1922 __f.__f_->__clone(__as_base(&__buf_));
1923 __f.__f_->destroy();
1924 __f.__f_ = __f_;
1925 __f_ = __as_base(&__buf_);
1926 }
1927 else
1928 _VSTD::swap(__f_, __f.__f_);
1929 }
1930
1931 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001932 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
Eric Fiselier125798e2018-12-10 18:14:09 +00001933
1934#ifndef _LIBCPP_NO_RTTI
1935 _LIBCPP_INLINE_VISIBILITY
1936 const std::type_info& target_type() const _NOEXCEPT
1937 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001938 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001939 return typeid(void);
1940 return __f_->target_type();
1941 }
1942
1943 template <typename _Tp>
1944 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1945 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001946 if (__f_ == nullptr)
1947 return nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001948 return (const _Tp*)__f_->target(typeid(_Tp));
1949 }
1950#endif // _LIBCPP_NO_RTTI
1951};
1952
Eric Fiselierf2e64362018-12-11 00:14:34 +00001953// Storage for a functor object, to be used with __policy to manage copy and
1954// destruction.
1955union __policy_storage
1956{
1957 mutable char __small[sizeof(void*) * 2];
1958 void* __large;
1959};
1960
1961// True if _Fun can safely be held in __policy_storage.__small.
1962template <typename _Fun>
1963struct __use_small_storage
1964 : public _VSTD::integral_constant<
1965 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001966 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Eric Fiselierf2e64362018-12-11 00:14:34 +00001967 _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1968 _VSTD::is_trivially_destructible<_Fun>::value> {};
1969
1970// Policy contains information about how to copy, destroy, and move the
1971// underlying functor. You can think of it as a vtable of sorts.
1972struct __policy
1973{
1974 // Used to copy or destroy __large values. null for trivial objects.
1975 void* (*const __clone)(const void*);
1976 void (*const __destroy)(void*);
1977
1978 // True if this is the null policy (no value).
1979 const bool __is_null;
1980
1981 // The target type. May be null if RTTI is disabled.
1982 const std::type_info* const __type_info;
1983
1984 // Returns a pointer to a static policy object suitable for the functor
1985 // type.
1986 template <typename _Fun>
1987 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1988 {
1989 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1990 }
1991
1992 _LIBCPP_INLINE_VISIBILITY
1993 static const __policy* __create_empty()
1994 {
1995 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1996 true,
1997#ifndef _LIBCPP_NO_RTTI
1998 &typeid(void)
1999#else
2000 nullptr
2001#endif
2002 };
2003 return &__policy_;
2004 }
2005
2006 private:
2007 template <typename _Fun> static void* __large_clone(const void* __s)
2008 {
2009 const _Fun* __f = static_cast<const _Fun*>(__s);
2010 return __f->__clone();
2011 }
2012
Eric Fiselier74ebee62019-06-08 01:31:19 +00002013 template <typename _Fun>
2014 static void __large_destroy(void* __s) {
2015 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002016 }
2017
2018 template <typename _Fun>
2019 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002020 __choose_policy(/* is_small = */ false_type) {
2021 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2022 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002023#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002024 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002025#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002026 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002027#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002028 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002029 return &__policy_;
2030 }
2031
2032 template <typename _Fun>
2033 _LIBCPP_INLINE_VISIBILITY static const __policy*
2034 __choose_policy(/* is_small = */ true_type)
2035 {
2036 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2037 nullptr, nullptr, false,
2038#ifndef _LIBCPP_NO_RTTI
2039 &typeid(typename _Fun::_Target)
2040#else
2041 nullptr
2042#endif
2043 };
2044 return &__policy_;
2045 }
2046};
2047
2048// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2049// faster for types that can be passed in registers.
2050template <typename _Tp>
2051using __fast_forward =
2052 typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2053
2054// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2055
2056template <class _Fp> struct __policy_invoker;
2057
2058template <class _Rp, class... _ArgTypes>
2059struct __policy_invoker<_Rp(_ArgTypes...)>
2060{
2061 typedef _Rp (*__Call)(const __policy_storage*,
2062 __fast_forward<_ArgTypes>...);
2063
2064 __Call __call_;
2065
2066 // Creates an invoker that throws bad_function_call.
2067 _LIBCPP_INLINE_VISIBILITY
2068 __policy_invoker() : __call_(&__call_empty) {}
2069
2070 // Creates an invoker that calls the given instance of __func.
2071 template <typename _Fun>
2072 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2073 {
2074 return __policy_invoker(&__call_impl<_Fun>);
2075 }
2076
2077 private:
2078 _LIBCPP_INLINE_VISIBILITY
2079 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2080
2081 static _Rp __call_empty(const __policy_storage*,
2082 __fast_forward<_ArgTypes>...)
2083 {
2084 __throw_bad_function_call();
2085 }
2086
2087 template <typename _Fun>
2088 static _Rp __call_impl(const __policy_storage* __buf,
2089 __fast_forward<_ArgTypes>... __args)
2090 {
2091 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2092 ? &__buf->__small
2093 : __buf->__large);
2094 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2095 }
2096};
2097
2098// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2099// copyable functor.
2100
2101template <class _Fp> class __policy_func;
2102
2103template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2104{
2105 // Inline storage for small objects.
2106 __policy_storage __buf_;
2107
2108 // Calls the value stored in __buf_. This could technically be part of
2109 // policy, but storing it here eliminates a level of indirection inside
2110 // operator().
2111 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2112 __invoker __invoker_;
2113
2114 // The policy that describes how to move / copy / destroy __buf_. Never
2115 // null, even if the function is empty.
2116 const __policy* __policy_;
2117
2118 public:
2119 _LIBCPP_INLINE_VISIBILITY
2120 __policy_func() : __policy_(__policy::__create_empty()) {}
2121
2122 template <class _Fp, class _Alloc>
2123 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2124 : __policy_(__policy::__create_empty())
2125 {
2126 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2127 typedef allocator_traits<_Alloc> __alloc_traits;
2128 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2129 _FunAlloc;
2130
2131 if (__function::__not_null(__f))
2132 {
2133 __invoker_ = __invoker::template __create<_Fun>();
2134 __policy_ = __policy::__create<_Fun>();
2135
2136 _FunAlloc __af(__a);
2137 if (__use_small_storage<_Fun>())
2138 {
2139 ::new ((void*)&__buf_.__small)
2140 _Fun(_VSTD::move(__f), _Alloc(__af));
2141 }
2142 else
2143 {
2144 typedef __allocator_destructor<_FunAlloc> _Dp;
2145 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2146 ::new ((void*)__hold.get())
2147 _Fun(_VSTD::move(__f), _Alloc(__af));
2148 __buf_.__large = __hold.release();
2149 }
2150 }
2151 }
2152
Eric Fiselier74ebee62019-06-08 01:31:19 +00002153 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2154 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2155 : __policy_(__policy::__create_empty()) {
2156 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2157
2158 if (__function::__not_null(__f)) {
2159 __invoker_ = __invoker::template __create<_Fun>();
2160 __policy_ = __policy::__create<_Fun>();
2161 if (__use_small_storage<_Fun>()) {
2162 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2163 } else {
2164 __builtin_new_allocator::__holder_t __hold =
2165 __builtin_new_allocator::__allocate_type<_Fun>(1);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002166 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
Eric Fiselier74ebee62019-06-08 01:31:19 +00002167 (void)__hold.release();
2168 }
2169 }
2170 }
2171
Eric Fiselierf2e64362018-12-11 00:14:34 +00002172 _LIBCPP_INLINE_VISIBILITY
2173 __policy_func(const __policy_func& __f)
2174 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2175 __policy_(__f.__policy_)
2176 {
2177 if (__policy_->__clone)
2178 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2179 }
2180
2181 _LIBCPP_INLINE_VISIBILITY
2182 __policy_func(__policy_func&& __f)
2183 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2184 __policy_(__f.__policy_)
2185 {
2186 if (__policy_->__destroy)
2187 {
2188 __f.__policy_ = __policy::__create_empty();
2189 __f.__invoker_ = __invoker();
2190 }
2191 }
2192
2193 _LIBCPP_INLINE_VISIBILITY
2194 ~__policy_func()
2195 {
2196 if (__policy_->__destroy)
2197 __policy_->__destroy(__buf_.__large);
2198 }
2199
2200 _LIBCPP_INLINE_VISIBILITY
2201 __policy_func& operator=(__policy_func&& __f)
2202 {
2203 *this = nullptr;
2204 __buf_ = __f.__buf_;
2205 __invoker_ = __f.__invoker_;
2206 __policy_ = __f.__policy_;
2207 __f.__policy_ = __policy::__create_empty();
2208 __f.__invoker_ = __invoker();
2209 return *this;
2210 }
2211
2212 _LIBCPP_INLINE_VISIBILITY
2213 __policy_func& operator=(nullptr_t)
2214 {
2215 const __policy* __p = __policy_;
2216 __policy_ = __policy::__create_empty();
2217 __invoker_ = __invoker();
2218 if (__p->__destroy)
2219 __p->__destroy(__buf_.__large);
2220 return *this;
2221 }
2222
2223 _LIBCPP_INLINE_VISIBILITY
2224 _Rp operator()(_ArgTypes&&... __args) const
2225 {
2226 return __invoker_.__call_(_VSTD::addressof(__buf_),
2227 _VSTD::forward<_ArgTypes>(__args)...);
2228 }
2229
2230 _LIBCPP_INLINE_VISIBILITY
2231 void swap(__policy_func& __f)
2232 {
2233 _VSTD::swap(__invoker_, __f.__invoker_);
2234 _VSTD::swap(__policy_, __f.__policy_);
2235 _VSTD::swap(__buf_, __f.__buf_);
2236 }
2237
2238 _LIBCPP_INLINE_VISIBILITY
2239 explicit operator bool() const _NOEXCEPT
2240 {
2241 return !__policy_->__is_null;
2242 }
2243
2244#ifndef _LIBCPP_NO_RTTI
2245 _LIBCPP_INLINE_VISIBILITY
2246 const std::type_info& target_type() const _NOEXCEPT
2247 {
2248 return *__policy_->__type_info;
2249 }
2250
2251 template <typename _Tp>
2252 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2253 {
2254 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2255 return nullptr;
2256 if (__policy_->__clone) // Out of line storage.
2257 return reinterpret_cast<const _Tp*>(__buf_.__large);
2258 else
2259 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2260 }
2261#endif // _LIBCPP_NO_RTTI
2262};
2263
Louis Dionne3a632922020-04-23 16:47:52 -04002264#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
Louis Dionnee2391d72020-04-22 13:58:17 -04002265
Louis Dionne91cf4442020-07-31 12:56:36 -04002266extern "C" void *_Block_copy(const void *);
2267extern "C" void _Block_release(const void *);
2268
Louis Dionnee2391d72020-04-22 13:58:17 -04002269template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2270class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2271 : public __base<_Rp(_ArgTypes...)>
2272{
2273 typedef _Rp1(^__block_type)(_ArgTypes1...);
2274 __block_type __f_;
2275
2276public:
2277 _LIBCPP_INLINE_VISIBILITY
2278 explicit __func(__block_type const& __f)
Louis Dionne91cf4442020-07-31 12:56:36 -04002279 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002280 { }
2281
2282 // [TODO] add && to save on a retain
2283
2284 _LIBCPP_INLINE_VISIBILITY
2285 explicit __func(__block_type __f, const _Alloc& /* unused */)
Louis Dionne91cf4442020-07-31 12:56:36 -04002286 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002287 { }
2288
2289 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2290 _LIBCPP_ASSERT(false,
2291 "Block pointers are just pointers, so they should always fit into "
2292 "std::function's small buffer optimization. This function should "
2293 "never be invoked.");
2294 return nullptr;
2295 }
2296
2297 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002298 ::new ((void*)__p) __func(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002299 }
2300
2301 virtual void destroy() _NOEXCEPT {
2302 if (__f_)
Louis Dionne91cf4442020-07-31 12:56:36 -04002303 _Block_release(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002304 __f_ = 0;
2305 }
2306
2307 virtual void destroy_deallocate() _NOEXCEPT {
2308 _LIBCPP_ASSERT(false,
2309 "Block pointers are just pointers, so they should always fit into "
2310 "std::function's small buffer optimization. This function should "
2311 "never be invoked.");
2312 }
2313
2314 virtual _Rp operator()(_ArgTypes&& ... __arg) {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05002315 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
Louis Dionnee2391d72020-04-22 13:58:17 -04002316 }
2317
2318#ifndef _LIBCPP_NO_RTTI
2319 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2320 if (__ti == typeid(__func::__block_type))
2321 return &__f_;
2322 return (const void*)nullptr;
2323 }
2324
2325 virtual const std::type_info& target_type() const _NOEXCEPT {
2326 return typeid(__func::__block_type);
2327 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002328#endif // _LIBCPP_NO_RTTI
Louis Dionnee2391d72020-04-22 13:58:17 -04002329};
2330
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002331#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
Louis Dionnee2391d72020-04-22 13:58:17 -04002332
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333} // __function
2334
Howard Hinnantc834c512011-11-29 18:15:50 +00002335template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002336class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002337 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2338 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002340#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002341 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002342#else
2343 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2344#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345
Eric Fiselier125798e2018-12-10 18:14:09 +00002346 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002347
Eric Fiselier3906a132019-06-23 20:28:29 +00002348 template <class _Fp, bool = _And<
2349 _IsNotSame<__uncvref_t<_Fp>, function>,
zoecarver3e722f22020-05-19 17:15:28 -07002350 __invokable<_Fp, _ArgTypes...>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002351 >::value>
2352 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002353 template <class _Fp>
2354 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002355 {
Arthur O'Dwyer4ba8e3d2021-01-11 16:29:17 -05002356 static const bool value = is_void<_Rp>::value ||
2357 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
2358 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002359 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002360 template <class _Fp>
2361 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002362 {
2363 static const bool value = false;
2364 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002365
2366 template <class _Fp>
zoecarver3e722f22020-05-19 17:15:28 -07002367 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002369 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370
Howard Hinnantf06d9262010-08-20 19:36:46 +00002371 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002372 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002373 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002374 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002375 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002377 function(function&&) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002378 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002379 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380
Marshall Clow3148f422016-10-13 21:06:03 +00002381#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002382 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002383 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002384 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002385 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002386 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002387 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002388 template<class _Alloc>
2389 function(allocator_arg_t, const _Alloc&, const function&);
2390 template<class _Alloc>
2391 function(allocator_arg_t, const _Alloc&, function&&);
zoecarver3e722f22020-05-19 17:15:28 -07002392 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002393 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002394#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395
2396 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002397 function& operator=(function&&) _NOEXCEPT;
2398 function& operator=(nullptr_t) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002399 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002400 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401
2402 ~function();
2403
Howard Hinnantf06d9262010-08-20 19:36:46 +00002404 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002405 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002406
2407#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002408 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002410 void assign(_Fp&& __f, const _Alloc& __a)
2411 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002412#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413
Howard Hinnantf06d9262010-08-20 19:36:46 +00002414 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002415 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002416 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2417 return static_cast<bool>(__f_);
2418 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420 // deleted overloads close possible hole in the type system
2421 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002422 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002424 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002426 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002427 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428
Howard Hinnant72f73582010-08-11 17:04:31 +00002429#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002430 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002431 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002432 template <typename _Tp> _Tp* target() _NOEXCEPT;
2433 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002434#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435};
2436
Louis Dionne4af49712019-07-18 19:50:56 +00002437#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2438template<class _Rp, class ..._Ap>
2439function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2440
2441template<class _Fp>
2442struct __strip_signature;
2443
2444template<class _Rp, class _Gp, class ..._Ap>
2445struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2446template<class _Rp, class _Gp, class ..._Ap>
2447struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2448template<class _Rp, class _Gp, class ..._Ap>
2449struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2450template<class _Rp, class _Gp, class ..._Ap>
2451struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2452
2453template<class _Rp, class _Gp, class ..._Ap>
2454struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2455template<class _Rp, class _Gp, class ..._Ap>
2456struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2457template<class _Rp, class _Gp, class ..._Ap>
2458struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2459template<class _Rp, class _Gp, class ..._Ap>
2460struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2461
2462template<class _Rp, class _Gp, class ..._Ap>
2463struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2464template<class _Rp, class _Gp, class ..._Ap>
2465struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2466template<class _Rp, class _Gp, class ..._Ap>
2467struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2468template<class _Rp, class _Gp, class ..._Ap>
2469struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2470
2471template<class _Rp, class _Gp, class ..._Ap>
2472struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2473template<class _Rp, class _Gp, class ..._Ap>
2474struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2475template<class _Rp, class _Gp, class ..._Ap>
2476struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2477template<class _Rp, class _Gp, class ..._Ap>
2478struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2479
2480template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2481function(_Fp) -> function<_Stripped>;
2482#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2483
Howard Hinnantc834c512011-11-29 18:15:50 +00002484template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002485function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486
Marshall Clow3148f422016-10-13 21:06:03 +00002487#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002488template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002489template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002490function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002491 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002492#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002493
Eric Fiselier125798e2018-12-10 18:14:09 +00002494template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002495function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002496 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497
Marshall Clow3148f422016-10-13 21:06:03 +00002498#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002499template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002500template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002501function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002502 function&& __f)
2503 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002504#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002505
Eric Fiselier125798e2018-12-10 18:14:09 +00002506template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002507template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002508function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509
Marshall Clow3148f422016-10-13 21:06:03 +00002510#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002511template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002512template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002513function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2514 _Fp __f)
2515 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002516#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002517
Howard Hinnantc834c512011-11-29 18:15:50 +00002518template<class _Rp, class ..._ArgTypes>
2519function<_Rp(_ArgTypes...)>&
2520function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521{
2522 function(__f).swap(*this);
2523 return *this;
2524}
2525
Howard Hinnantc834c512011-11-29 18:15:50 +00002526template<class _Rp, class ..._ArgTypes>
2527function<_Rp(_ArgTypes...)>&
2528function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002530 __f_ = _VSTD::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002531 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532}
2533
Howard Hinnantc834c512011-11-29 18:15:50 +00002534template<class _Rp, class ..._ArgTypes>
2535function<_Rp(_ArgTypes...)>&
2536function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537{
Eric Fiselier125798e2018-12-10 18:14:09 +00002538 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002539 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540}
2541
Howard Hinnantc834c512011-11-29 18:15:50 +00002542template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002543template <class _Fp, class>
2544function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002545function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546{
Howard Hinnantc834c512011-11-29 18:15:50 +00002547 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548 return *this;
2549}
2550
Howard Hinnantc834c512011-11-29 18:15:50 +00002551template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002552function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553
Howard Hinnantc834c512011-11-29 18:15:50 +00002554template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555void
Howard Hinnantc834c512011-11-29 18:15:50 +00002556function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557{
Eric Fiselier125798e2018-12-10 18:14:09 +00002558 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559}
2560
Howard Hinnantc834c512011-11-29 18:15:50 +00002561template<class _Rp, class ..._ArgTypes>
2562_Rp
2563function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564{
Eric Fiselier125798e2018-12-10 18:14:09 +00002565 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566}
2567
Howard Hinnant72f73582010-08-11 17:04:31 +00002568#ifndef _LIBCPP_NO_RTTI
2569
Howard Hinnantc834c512011-11-29 18:15:50 +00002570template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002572function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573{
Eric Fiselier125798e2018-12-10 18:14:09 +00002574 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575}
2576
Howard Hinnantc834c512011-11-29 18:15:50 +00002577template<class _Rp, class ..._ArgTypes>
2578template <typename _Tp>
2579_Tp*
2580function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581{
Eric Fiselier125798e2018-12-10 18:14:09 +00002582 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583}
2584
Howard Hinnantc834c512011-11-29 18:15:50 +00002585template<class _Rp, class ..._ArgTypes>
2586template <typename _Tp>
2587const _Tp*
2588function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589{
Eric Fiselier125798e2018-12-10 18:14:09 +00002590 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591}
2592
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002593#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002594
Howard Hinnantc834c512011-11-29 18:15:50 +00002595template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596inline _LIBCPP_INLINE_VISIBILITY
2597bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002598operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599
Howard Hinnantc834c512011-11-29 18:15:50 +00002600template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601inline _LIBCPP_INLINE_VISIBILITY
2602bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002603operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604
Howard Hinnantc834c512011-11-29 18:15:50 +00002605template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606inline _LIBCPP_INLINE_VISIBILITY
2607bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002608operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609
Howard Hinnantc834c512011-11-29 18:15:50 +00002610template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611inline _LIBCPP_INLINE_VISIBILITY
2612bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002613operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614
Howard Hinnantc834c512011-11-29 18:15:50 +00002615template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616inline _LIBCPP_INLINE_VISIBILITY
2617void
Howard Hinnantc834c512011-11-29 18:15:50 +00002618swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619{return __x.swap(__y);}
2620
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002621#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002622
2623#include <__functional_03>
2624
2625#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002626
2627////////////////////////////////////////////////////////////////////////////////
2628// BIND
2629//==============================================================================
2630
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002632template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2634
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002635#if _LIBCPP_STD_VER > 14
2636template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002637_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002638#endif
2639
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002641template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2643
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002644#if _LIBCPP_STD_VER > 14
2645template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002646_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002647#endif
2648
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649namespace placeholders
2650{
2651
Howard Hinnantc834c512011-11-29 18:15:50 +00002652template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002654#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002655_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2656_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2657_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2658_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2659_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2660_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2661_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2662_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2663_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2664_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2665#else
Marshall Clow396b2132018-01-02 19:01:45 +00002666/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2667/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2668/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2669/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2670/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2671/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2672/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2673/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2674/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2675/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002676#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677
2678} // placeholders
2679
Howard Hinnantc834c512011-11-29 18:15:50 +00002680template<int _Np>
2681struct __is_placeholder<placeholders::__ph<_Np> >
2682 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002684
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002685#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002686
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687template <class _Tp, class _Uj>
2688inline _LIBCPP_INLINE_VISIBILITY
2689_Tp&
2690__mu(reference_wrapper<_Tp> __t, _Uj&)
2691{
2692 return __t.get();
2693}
2694
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695template <class _Ti, class ..._Uj, size_t ..._Indx>
2696inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002697typename __invoke_of<_Ti&, _Uj...>::type
2698__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002700 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701}
2702
2703template <class _Ti, class ..._Uj>
2704inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002705typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706<
2707 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002708 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709>::type
2710__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2711{
2712 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002713 return _VSTD::__mu_expand(__ti, __uj, __indices());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714}
2715
2716template <bool IsPh, class _Ti, class _Uj>
2717struct __mu_return2 {};
2718
2719template <class _Ti, class _Uj>
2720struct __mu_return2<true, _Ti, _Uj>
2721{
2722 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2723};
2724
2725template <class _Ti, class _Uj>
2726inline _LIBCPP_INLINE_VISIBILITY
2727typename enable_if
2728<
2729 0 < is_placeholder<_Ti>::value,
2730 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2731>::type
2732__mu(_Ti&, _Uj& __uj)
2733{
2734 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002735 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736}
2737
2738template <class _Ti, class _Uj>
2739inline _LIBCPP_INLINE_VISIBILITY
2740typename enable_if
2741<
2742 !is_bind_expression<_Ti>::value &&
2743 is_placeholder<_Ti>::value == 0 &&
2744 !__is_reference_wrapper<_Ti>::value,
2745 _Ti&
2746>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002747__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748{
2749 return __ti;
2750}
2751
Howard Hinnant0415d792011-05-22 15:07:43 +00002752template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2753 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002754struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002756template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002757struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002758{
2759 typedef __nat type;
2760};
2761
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002763struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002765 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766};
2767
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002768template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002769struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2770 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002771{
2772};
2773
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002775struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776{
2777 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2778 _TupleUj>::type&& type;
2779};
2780
2781template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002782struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002783{
2784 typedef typename _Ti::type& type;
2785};
2786
2787template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002788struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789{
2790 typedef _Ti& type;
2791};
2792
2793template <class _Ti, class _TupleUj>
2794struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002795 : public __mu_return_impl<_Ti,
2796 __is_reference_wrapper<_Ti>::value,
2797 is_bind_expression<_Ti>::value,
2798 0 < is_placeholder<_Ti>::value &&
2799 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2800 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801{
2802};
2803
Howard Hinnantc834c512011-11-29 18:15:50 +00002804template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002805struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002806{
2807 static const bool value = false;
2808};
2809
2810template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002811struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002812{
2813 static const bool value = __invokable<_Fp,
2814 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2815};
2816
2817template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002818struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002819{
2820 static const bool value = __invokable<_Fp,
2821 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2822};
2823
2824template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002825 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826struct __bind_return;
2827
Howard Hinnantc834c512011-11-29 18:15:50 +00002828template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002829struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002831 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002833 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 typename __mu_return
2835 <
2836 _BoundArgs,
2837 _TupleUj
2838 >::type...
2839 >::type type;
2840};
2841
Howard Hinnantc834c512011-11-29 18:15:50 +00002842template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002843struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002845 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002847 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 typename __mu_return
2849 <
2850 const _BoundArgs,
2851 _TupleUj
2852 >::type...
2853 >::type type;
2854};
2855
Howard Hinnantc834c512011-11-29 18:15:50 +00002856template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002858typename __bind_return<_Fp, _BoundArgs, _Args>::type
2859__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 _Args&& __args)
2861{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002862 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863}
2864
Howard Hinnantc834c512011-11-29 18:15:50 +00002865template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002867 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002869protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002870 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002871 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002872private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002873 _Fd __f_;
2874 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875
2876 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2877public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002878 template <class _Gp, class ..._BA,
2879 class = typename enable_if
2880 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002881 is_constructible<_Fd, _Gp>::value &&
2882 !is_same<typename remove_reference<_Gp>::type,
2883 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002884 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002885 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002886 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2887 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002888 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889
2890 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002891 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002892 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 operator()(_Args&& ...__args)
2894 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002895 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002896 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897 }
2898
2899 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002900 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002901 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002902 operator()(_Args&& ...__args) const
2903 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002904 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002905 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906 }
2907};
2908
Howard Hinnantc834c512011-11-29 18:15:50 +00002909template<class _Fp, class ..._BoundArgs>
2910struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911
Howard Hinnantc834c512011-11-29 18:15:50 +00002912template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002914 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915{
Howard Hinnantc834c512011-11-29 18:15:50 +00002916 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002917 typedef typename base::_Fd _Fd;
2918 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002920 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921
Howard Hinnant7091e652011-07-02 18:22:36 +00002922
Howard Hinnantf292a922013-07-01 00:01:51 +00002923 template <class _Gp, class ..._BA,
2924 class = typename enable_if
2925 <
2926 is_constructible<_Fd, _Gp>::value &&
2927 !is_same<typename remove_reference<_Gp>::type,
2928 __bind_r>::value
2929 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002930 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002931 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2932 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002933 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934
2935 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002936 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002937 typename enable_if
2938 <
2939 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002940 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002941 result_type
2942 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 operator()(_Args&& ...__args)
2944 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002945 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2946 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 }
2948
2949 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002950 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002951 typename enable_if
2952 <
2953 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002954 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002955 result_type
2956 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957 operator()(_Args&& ...__args) const
2958 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002959 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2960 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 }
2962};
2963
Howard Hinnantc834c512011-11-29 18:15:50 +00002964template<class _Rp, class _Fp, class ..._BoundArgs>
2965struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966
Howard Hinnantc834c512011-11-29 18:15:50 +00002967template<class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002968inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002969__bind<_Fp, _BoundArgs...>
2970bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971{
Howard Hinnantc834c512011-11-29 18:15:50 +00002972 typedef __bind<_Fp, _BoundArgs...> type;
2973 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974}
2975
Howard Hinnantc834c512011-11-29 18:15:50 +00002976template<class _Rp, class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002977inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002978__bind_r<_Rp, _Fp, _BoundArgs...>
2979bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980{
Howard Hinnantc834c512011-11-29 18:15:50 +00002981 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2982 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983}
2984
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002985#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986
Eric Fiselier0d974f12015-07-14 20:16:15 +00002987#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002988
zoecarver3595cac2021-03-02 16:17:22 -08002989template<class _Op, class _Tuple,
2990 class _Idxs = typename __make_tuple_indices<tuple_size<_Tuple>::value>::type>
2991struct __perfect_forward_impl;
Eric Fiselier934f63b2016-06-02 01:25:41 +00002992
zoecarver3595cac2021-03-02 16:17:22 -08002993template<class _Op, class... _Bound, size_t... _Idxs>
2994struct __perfect_forward_impl<_Op, __tuple_types<_Bound...>, __tuple_indices<_Idxs...>>
2995{
2996 tuple<_Bound...> __bound_;
Eric Fiselier934f63b2016-06-02 01:25:41 +00002997
zoecarver3595cac2021-03-02 16:17:22 -08002998 template<class... _Args>
2999 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &
3000 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3001 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3002 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003003
zoecarver3595cac2021-03-02 16:17:22 -08003004 template<class... _Args>
3005 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&
3006 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3007 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3008 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003009
zoecarver3595cac2021-03-02 16:17:22 -08003010 template<class... _Args>
3011 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &&
3012 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3013 _VSTD::forward<_Args>(__args)...)))
3014 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3015 _VSTD::forward<_Args>(__args)...))
3016 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3017 _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003018
zoecarver3595cac2021-03-02 16:17:22 -08003019 template<class... _Args>
3020 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&&
3021 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3022 _VSTD::forward<_Args>(__args)...)))
3023 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3024 _VSTD::forward<_Args>(__args)...))
3025 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3026 _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003027
zoecarver3595cac2021-03-02 16:17:22 -08003028 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3029 class = _EnableIf<is_copy_constructible_v<_Fn>>>
3030 constexpr __perfect_forward_impl(__perfect_forward_impl const& __other)
3031 : __bound_(__other.__bound_) {}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003032
zoecarver3595cac2021-03-02 16:17:22 -08003033 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3034 class = _EnableIf<is_move_constructible_v<_Fn>>>
3035 constexpr __perfect_forward_impl(__perfect_forward_impl && __other)
3036 : __bound_(_VSTD::move(__other.__bound_)) {}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003037
zoecarver3595cac2021-03-02 16:17:22 -08003038 template<class... _BoundArgs>
3039 explicit constexpr __perfect_forward_impl(_BoundArgs&&... __bound) :
3040 __bound_(_VSTD::forward<_BoundArgs>(__bound)...) { }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003041};
3042
zoecarver3595cac2021-03-02 16:17:22 -08003043template<class _Op, class... _Args>
3044using __perfect_forward =
3045 __perfect_forward_impl<_Op, __tuple_types<decay_t<_Args>...>>;
3046
3047struct __not_fn_op
3048{
3049 template<class... _Args>
3050 static _LIBCPP_CONSTEXPR_AFTER_CXX17 auto __call(_Args&&... __args)
3051 noexcept(noexcept(!_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3052 -> decltype( !_VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3053 { return !_VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3054};
3055
3056template<class _Fn,
3057 class = _EnableIf<is_constructible_v<decay_t<_Fn>, _Fn> &&
3058 is_move_constructible_v<_Fn>>>
3059_LIBCPP_CONSTEXPR_AFTER_CXX17 auto not_fn(_Fn&& __f)
3060{
3061 return __perfect_forward<__not_fn_op, _Fn>(_VSTD::forward<_Fn>(__f));
Eric Fiselier934f63b2016-06-02 01:25:41 +00003062}
3063
zoecarver3595cac2021-03-02 16:17:22 -08003064#endif // _LIBCPP_STD_VER > 14
3065
3066#if _LIBCPP_STD_VER > 17
3067
3068struct __bind_front_op
3069{
3070 template<class... _Args>
3071 constexpr static auto __call(_Args&&... __args)
3072 noexcept(noexcept(_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3073 -> decltype( _VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3074 { return _VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3075};
3076
3077template<class _Fn, class... _Args,
3078 class = _EnableIf<conjunction<is_constructible<decay_t<_Fn>, _Fn>,
3079 is_move_constructible<decay_t<_Fn>>,
3080 is_constructible<decay_t<_Args>, _Args>...,
3081 is_move_constructible<decay_t<_Args>>...
3082 >::value>>
3083constexpr auto bind_front(_Fn&& __f, _Args&&... __args)
3084{
3085 return __perfect_forward<__bind_front_op, _Fn, _Args...>(_VSTD::forward<_Fn>(__f),
3086 _VSTD::forward<_Args>(__args)...);
3087}
3088
3089#endif // _LIBCPP_STD_VER > 17
Eric Fiselier0d974f12015-07-14 20:16:15 +00003090
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003091// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092
Marshall Clowa40686b2018-01-08 19:18:00 +00003093template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00003094pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00003095__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3096 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
3097 forward_iterator_tag, forward_iterator_tag)
3098{
3099 if (__first2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003100 return _VSTD::make_pair(__first1, __first1); // Everything matches an empty sequence
Marshall Clowa40686b2018-01-08 19:18:00 +00003101 while (true)
3102 {
3103 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
3104 while (true)
3105 {
3106 if (__first1 == __last1) // return __last1 if no element matches *__first2
Logan Smith4528b5f2020-05-07 11:54:25 -04003107 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003108 if (__pred(*__first1, *__first2))
3109 break;
3110 ++__first1;
3111 }
3112 // *__first1 matches *__first2, now match elements after here
3113 _ForwardIterator1 __m1 = __first1;
3114 _ForwardIterator2 __m2 = __first2;
3115 while (true)
3116 {
3117 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Logan Smith4528b5f2020-05-07 11:54:25 -04003118 return _VSTD::make_pair(__first1, __m1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003119 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Logan Smith4528b5f2020-05-07 11:54:25 -04003120 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003121 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
3122 {
3123 ++__first1;
3124 break;
3125 } // else there is a match, check next elements
3126 }
3127 }
3128}
3129
3130template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
3131_LIBCPP_CONSTEXPR_AFTER_CXX11
3132pair<_RandomAccessIterator1, _RandomAccessIterator1>
3133__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
3134 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
3135 random_access_iterator_tag, random_access_iterator_tag)
3136{
3137 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
3138 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
3139 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
3140 const _D2 __len2 = __last2 - __first2;
3141 if (__len2 == 0)
Logan Smith4528b5f2020-05-07 11:54:25 -04003142 return _VSTD::make_pair(__first1, __first1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003143 const _D1 __len1 = __last1 - __first1;
3144 if (__len1 < __len2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003145 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003146 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
3147
3148 while (true)
3149 {
3150 while (true)
3151 {
3152 if (__first1 == __s)
Logan Smith4528b5f2020-05-07 11:54:25 -04003153 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003154 if (__pred(*__first1, *__first2))
3155 break;
3156 ++__first1;
3157 }
3158
3159 _RandomAccessIterator1 __m1 = __first1;
3160 _RandomAccessIterator2 __m2 = __first2;
3161 while (true)
3162 {
3163 if (++__m2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003164 return _VSTD::make_pair(__first1, __first1 + __len2);
Marshall Clowa40686b2018-01-08 19:18:00 +00003165 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
3166 if (!__pred(*__m1, *__m2))
3167 {
3168 ++__first1;
3169 break;
3170 }
3171 }
3172 }
3173}
3174
3175#if _LIBCPP_STD_VER > 14
3176
3177// default searcher
3178template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Martin Storsjö4d6f2212021-04-06 10:55:33 +03003179class _LIBCPP_TEMPLATE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00003180public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003181 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne44bcff92018-08-03 22:36:53 +00003182 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00003183 _BinaryPredicate __p = _BinaryPredicate())
3184 : __first_(__f), __last_(__l), __pred_(__p) {}
3185
3186 template <typename _ForwardIterator2>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003187 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00003188 pair<_ForwardIterator2, _ForwardIterator2>
3189 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3190 {
3191 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
3192 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
3193 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
3194 }
3195
3196private:
3197 _ForwardIterator __first_;
3198 _ForwardIterator __last_;
3199 _BinaryPredicate __pred_;
Eric Fiselier80663c52019-08-04 07:13:43 +00003200 };
Marshall Clowa40686b2018-01-08 19:18:00 +00003201
3202#endif // _LIBCPP_STD_VER > 14
3203
Louis Dionnedb1892a2018-12-03 14:03:27 +00003204#if _LIBCPP_STD_VER > 17
3205template <class _Tp>
3206using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3207
3208template <class _Tp>
3209using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3210#endif // > C++17
3211
Christopher Di Bellae68ec582021-03-18 17:21:35 +00003212#if _LIBCPP_STD_VER > 17
3213// [func.identity]
3214struct identity {
3215 template<class _Tp>
Arthur O'Dwyer108facb2021-04-05 14:56:03 -04003216 _LIBCPP_NODISCARD_EXT constexpr _Tp&& operator()(_Tp&& __t) const noexcept
Christopher Di Bellae68ec582021-03-18 17:21:35 +00003217 {
3218 return _VSTD::forward<_Tp>(__t);
3219 }
3220
3221 using is_transparent = void;
3222};
3223#endif // _LIBCPP_STD_VER > 17
3224
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225_LIBCPP_END_NAMESPACE_STD
3226
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003227#endif // _LIBCPP_FUNCTIONAL