blob: 6e9a334e623f2a332b5c4eaafde19df53542c59f [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>
Arthur O'Dwyera6b51072021-05-24 18:36:17 -0400195class unary_negate // deprecated in C++17, removed in C++20
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
Arthur O'Dwyera6b51072021-05-24 18:36:17 -0400203template <class Predicate> // deprecated in C++17, removed in C++20
Louis Dionne481a2662018-09-23 18:35:00 +0000204unary_negate<Predicate> not1(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205
206template <class Predicate>
Arthur O'Dwyera6b51072021-05-24 18:36:17 -0400207class binary_negate // deprecated in C++17, removed in C++20
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
Arthur O'Dwyera6b51072021-05-24 18:36:17 -0400218template <class Predicate> // deprecated in C++17, removed in C++20
Louis Dionne481a2662018-09-23 18:35:00 +0000219binary_negate<Predicate> not2(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500221template <class F>
222constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
Eric Fiselier934f63b2016-06-02 01:25:41 +0000223
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224template<class T> struct is_bind_expression;
225template<class T> struct is_placeholder;
226
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000227 // See C++14 20.9.9, Function object binders
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000228template <class T> inline constexpr bool is_bind_expression_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000229 = is_bind_expression<T>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000230template <class T> inline constexpr int is_placeholder_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000231 = is_placeholder<T>::value; // C++17
232
233
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000234template<class Fn, class... BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500235 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000236template<class R, class Fn, class... BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500237 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238
Louis Dionneaf34f122019-04-03 17:54:37 +0000239template<class F, class... Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500240 constexpr // constexpr in C++20
Louis Dionneaf34f122019-04-03 17:54:37 +0000241 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
242 noexcept(is_nothrow_invocable_v<F, Args...>);
243
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000244namespace placeholders {
245 // M is the implementation-defined number of placeholders
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246 extern unspecified _1;
247 extern unspecified _2;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000248 .
249 .
250 .
Howard Hinnantc834c512011-11-29 18:15:50 +0000251 extern unspecified _Mp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252}
253
254template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000255class binder1st // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 : public unary_function<typename Operation::second_argument_type,
257 typename Operation::result_type>
258{
259protected:
260 Operation op;
261 typename Operation::first_argument_type value;
262public:
263 binder1st(const Operation& x, const typename Operation::first_argument_type y);
264 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
265 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
266};
267
268template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000269binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
271template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000272class binder2nd // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273 : public unary_function<typename Operation::first_argument_type,
274 typename Operation::result_type>
275{
276protected:
277 Operation op;
278 typename Operation::second_argument_type value;
279public:
280 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
281 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
282 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
283};
284
285template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000286binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287
Marshall Clow26a027c2017-04-13 18:25:32 +0000288template <class Arg, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289class pointer_to_unary_function : public unary_function<Arg, Result>
290{
291public:
292 explicit pointer_to_unary_function(Result (*f)(Arg));
293 Result operator()(Arg x) const;
294};
295
296template <class Arg, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000297pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Marshall Clow26a027c2017-04-13 18:25:32 +0000299template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
301{
302public:
303 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
304 Result operator()(Arg1 x, Arg2 y) const;
305};
306
307template <class Arg1, class Arg2, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000308pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309
Marshall Clow26a027c2017-04-13 18:25:32 +0000310template<class S, class T> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311class mem_fun_t : public unary_function<T*, S>
312{
313public:
314 explicit mem_fun_t(S (T::*p)());
315 S operator()(T* p) const;
316};
317
318template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000319class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320{
321public:
322 explicit mem_fun1_t(S (T::*p)(A));
323 S operator()(T* p, A x) const;
324};
325
Marshall Clow26a027c2017-04-13 18:25:32 +0000326template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
327template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A)); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000328
329template<class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000330class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331{
332public:
333 explicit mem_fun_ref_t(S (T::*p)());
334 S operator()(T& p) const;
335};
336
337template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000338class mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339{
340public:
341 explicit mem_fun1_ref_t(S (T::*p)(A));
342 S operator()(T& p, A x) const;
343};
344
Marshall Clow26a027c2017-04-13 18:25:32 +0000345template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
346template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000347
348template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000349class const_mem_fun_t : public unary_function<const T*, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350{
351public:
352 explicit const_mem_fun_t(S (T::*p)() const);
353 S operator()(const T* p) const;
354};
355
356template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000357class const_mem_fun1_t : public binary_function<const T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000358{
359public:
360 explicit const_mem_fun1_t(S (T::*p)(A) const);
361 S operator()(const T* p, A x) const;
362};
363
Marshall Clow26a027c2017-04-13 18:25:32 +0000364template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
365template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366
367template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000368class const_mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369{
370public:
371 explicit const_mem_fun_ref_t(S (T::*p)() const);
372 S operator()(const T& p) const;
373};
374
375template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000376class const_mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377{
378public:
379 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
380 S operator()(const T& p, A x) const;
381};
382
Marshall Clow26a027c2017-04-13 18:25:32 +0000383template <class S, class T> const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const); // deprecated in C++11, removed in C++17
384template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500386template<class R, class T>
387constexpr unspecified mem_fn(R T::*); // constexpr in C++20
Howard Hinnantf06d9262010-08-20 19:36:46 +0000388
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389class bad_function_call
390 : public exception
391{
392};
393
Howard Hinnantf06d9262010-08-20 19:36:46 +0000394template<class> class function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395
Howard Hinnantf06d9262010-08-20 19:36:46 +0000396template<class R, class... ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397class function<R(ArgTypes...)>
398 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
399 // ArgTypes contains T1
400 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
401 // ArgTypes contains T1 and T2
402{
403public:
404 typedef R result_type;
405
Howard Hinnantf06d9262010-08-20 19:36:46 +0000406 // construct/copy/destroy:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000407 function() noexcept;
408 function(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000410 function(function&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 template<class F>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 function(F);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000414 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000416 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000418 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000420 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 template<class F, Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000422 function(allocator_arg_t, const Alloc&, F); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000425 function& operator=(function&&) noexcept;
Howard Hinnant7b85be02011-05-29 13:53:56 +0000426 function& operator=(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427 template<class F>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000428 function& operator=(F&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429 template<class F>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000430 function& operator=(reference_wrapper<F>) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431
432 ~function();
433
Howard Hinnantf06d9262010-08-20 19:36:46 +0000434 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000435 void swap(function&) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +0000436 template<class F, class Alloc>
Marshall Clowfc8fd832016-01-25 17:29:55 +0000437 void assign(F&&, const Alloc&); // Removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438
Howard Hinnantf06d9262010-08-20 19:36:46 +0000439 // function capacity:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000440 explicit operator bool() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441
Howard Hinnantf06d9262010-08-20 19:36:46 +0000442 // function invocation:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443 R operator()(ArgTypes...) const;
444
Howard Hinnantf06d9262010-08-20 19:36:46 +0000445 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000446 const std::type_info& target_type() const noexcept;
447 template <typename T> T* target() noexcept;
448 template <typename T> const T* target() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449};
450
Louis Dionne4af49712019-07-18 19:50:56 +0000451// Deduction guides
452template<class R, class ...Args>
453function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
454
455template<class F>
456function(F) -> function<see-below>; // since C++17
457
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000458// Null pointer comparisons:
459template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000460 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000462template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000463 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000465template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000466 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000468template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000469 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000471// specialized algorithms:
472template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000473 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474
475template <class T> struct hash;
476
477template <> struct hash<bool>;
478template <> struct hash<char>;
479template <> struct hash<signed char>;
480template <> struct hash<unsigned char>;
Yuriy Chernyshovfbb87fa2020-12-08 13:39:56 -0500481template <> struct hash<char8_t>; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482template <> struct hash<char16_t>;
483template <> struct hash<char32_t>;
484template <> struct hash<wchar_t>;
485template <> struct hash<short>;
486template <> struct hash<unsigned short>;
487template <> struct hash<int>;
488template <> struct hash<unsigned int>;
489template <> struct hash<long>;
490template <> struct hash<long long>;
491template <> struct hash<unsigned long>;
492template <> struct hash<unsigned long long>;
493
494template <> struct hash<float>;
495template <> struct hash<double>;
496template <> struct hash<long double>;
497
498template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000499template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000500
501} // std
502
503POLICY: For non-variadic implementations, the number of arguments is limited
504 to 3. It is hoped that the need for non-variadic implementations
505 will be minimal.
506
507*/
508
509#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400510#include <__debug>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400511#include <__functional_base>
zoecarver79e927a2021-06-03 11:26:03 -0700512#include <__functional/search.h>
zoecarver9ce102c2021-04-22 17:33:04 -0700513#include <concepts>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514#include <exception>
515#include <memory>
516#include <tuple>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400517#include <type_traits>
518#include <typeinfo>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000519#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000520#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000522#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000524#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525
526_LIBCPP_BEGIN_NAMESPACE_STD
527
Marshall Clow974bae22013-07-29 14:21:53 +0000528#if _LIBCPP_STD_VER > 11
529template <class _Tp = void>
530#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000532#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000533struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400535 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000536 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
537 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000538 {return __x + __y;}
539};
540
Marshall Clow974bae22013-07-29 14:21:53 +0000541#if _LIBCPP_STD_VER > 11
542template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000543struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000544{
545 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000546 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
547 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000548 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
549 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
550 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000551 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000552};
553#endif
554
555
556#if _LIBCPP_STD_VER > 11
557template <class _Tp = void>
558#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000560#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000561struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400563 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000564 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
565 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566 {return __x - __y;}
567};
568
Marshall Clow974bae22013-07-29 14:21:53 +0000569#if _LIBCPP_STD_VER > 11
570template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000571struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000572{
573 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000574 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
575 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000576 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
577 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
578 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000579 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000580};
581#endif
582
583
584#if _LIBCPP_STD_VER > 11
585template <class _Tp = void>
586#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000588#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000589struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400591 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000592 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
593 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594 {return __x * __y;}
595};
596
Marshall Clow974bae22013-07-29 14:21:53 +0000597#if _LIBCPP_STD_VER > 11
598template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000599struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000600{
601 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000602 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
603 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000604 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
605 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
606 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000607 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000608};
609#endif
610
611
612#if _LIBCPP_STD_VER > 11
613template <class _Tp = void>
614#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000616#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000617struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400619 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000620 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
621 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622 {return __x / __y;}
623};
624
Marshall Clow974bae22013-07-29 14:21:53 +0000625#if _LIBCPP_STD_VER > 11
626template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000627struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000628{
629 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000630 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
631 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000632 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
633 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
634 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000635 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000636};
637#endif
638
639
640#if _LIBCPP_STD_VER > 11
641template <class _Tp = void>
642#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000644#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000645struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400647 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000648 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
649 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 {return __x % __y;}
651};
652
Marshall Clow974bae22013-07-29 14:21:53 +0000653#if _LIBCPP_STD_VER > 11
654template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000655struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000656{
657 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000658 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
659 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000660 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
661 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
662 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000663 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000664};
665#endif
666
667
668#if _LIBCPP_STD_VER > 11
669template <class _Tp = void>
670#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000672#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000673struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400675 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000676 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
677 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 {return -__x;}
679};
680
Marshall Clow974bae22013-07-29 14:21:53 +0000681#if _LIBCPP_STD_VER > 11
682template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000683struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000684{
685 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000686 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
687 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000688 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
689 -> decltype (- _VSTD::forward<_Tp>(__x))
690 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000691 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000692};
693#endif
694
695
696#if _LIBCPP_STD_VER > 11
697template <class _Tp = void>
698#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000700#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000701struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400703 typedef bool __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000704 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
705 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706 {return __x == __y;}
707};
708
Marshall Clow974bae22013-07-29 14:21:53 +0000709#if _LIBCPP_STD_VER > 11
710template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000711struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000712{
Marshall Clowd18ee652013-09-28 19:06:12 +0000713 template <class _T1, class _T2>
714 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000715 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000716 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
717 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
718 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000719 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000720};
721#endif
722
723
724#if _LIBCPP_STD_VER > 11
725template <class _Tp = void>
726#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000728#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000729struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400731 typedef bool __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000732 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
733 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734 {return __x != __y;}
735};
736
Marshall Clow974bae22013-07-29 14:21:53 +0000737#if _LIBCPP_STD_VER > 11
738template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000739struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000740{
Marshall Clowd18ee652013-09-28 19:06:12 +0000741 template <class _T1, class _T2>
742 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000743 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000744 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
745 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
746 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000747 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000748};
749#endif
750
751
752#if _LIBCPP_STD_VER > 11
753template <class _Tp = void>
754#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000756#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000757struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400759 typedef bool __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000760 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
761 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 {return __x > __y;}
763};
764
Marshall Clow974bae22013-07-29 14:21:53 +0000765#if _LIBCPP_STD_VER > 11
766template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000767struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000768{
Marshall Clowd18ee652013-09-28 19:06:12 +0000769 template <class _T1, class _T2>
770 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000771 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000772 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
773 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
774 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000775 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000776};
777#endif
778
779
Howard Hinnantb17caf92012-02-21 21:02:58 +0000780// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781
Marshall Clow974bae22013-07-29 14:21:53 +0000782#if _LIBCPP_STD_VER > 11
783template <class _Tp = void>
784#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000786#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000787struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400789 typedef bool __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000790 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
791 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 {return __x >= __y;}
793};
794
Marshall Clow974bae22013-07-29 14:21:53 +0000795#if _LIBCPP_STD_VER > 11
796template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000797struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000798{
Marshall Clowd18ee652013-09-28 19:06:12 +0000799 template <class _T1, class _T2>
800 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000801 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000802 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
803 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
804 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000805 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000806};
807#endif
808
809
810#if _LIBCPP_STD_VER > 11
811template <class _Tp = void>
812#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000814#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000815struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400817 typedef bool __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000818 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
819 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820 {return __x <= __y;}
821};
822
Marshall Clow974bae22013-07-29 14:21:53 +0000823#if _LIBCPP_STD_VER > 11
824template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000825struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000826{
Marshall Clowd18ee652013-09-28 19:06:12 +0000827 template <class _T1, class _T2>
828 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000829 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000830 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
831 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
832 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000833 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000834};
835#endif
836
837
838#if _LIBCPP_STD_VER > 11
839template <class _Tp = void>
840#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000842#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000843struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400845 typedef bool __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000846 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
847 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000848 {return __x && __y;}
849};
850
Marshall Clow974bae22013-07-29 14:21:53 +0000851#if _LIBCPP_STD_VER > 11
852template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000853struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000854{
Marshall Clowd18ee652013-09-28 19:06:12 +0000855 template <class _T1, class _T2>
856 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000857 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000858 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
859 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
860 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000861 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000862};
863#endif
864
865
866#if _LIBCPP_STD_VER > 11
867template <class _Tp = void>
868#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000870#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000871struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400873 typedef bool __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000874 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
875 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876 {return __x || __y;}
877};
878
Marshall Clow974bae22013-07-29 14:21:53 +0000879#if _LIBCPP_STD_VER > 11
880template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000881struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000882{
Marshall Clowd18ee652013-09-28 19:06:12 +0000883 template <class _T1, class _T2>
884 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000885 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000886 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
887 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
888 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000889 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000890};
891#endif
892
893
894#if _LIBCPP_STD_VER > 11
895template <class _Tp = void>
896#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000898#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000899struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400901 typedef bool __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000902 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
903 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904 {return !__x;}
905};
906
Marshall Clow974bae22013-07-29 14:21:53 +0000907#if _LIBCPP_STD_VER > 11
908template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000909struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000910{
911 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000912 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
913 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000914 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
915 -> decltype (!_VSTD::forward<_Tp>(__x))
916 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000917 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000918};
919#endif
920
921
922#if _LIBCPP_STD_VER > 11
923template <class _Tp = void>
924#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000926#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000927struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400929 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000930 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
931 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932 {return __x & __y;}
933};
934
Marshall Clow974bae22013-07-29 14:21:53 +0000935#if _LIBCPP_STD_VER > 11
936template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000937struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000938{
Marshall Clowd18ee652013-09-28 19:06:12 +0000939 template <class _T1, class _T2>
940 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000941 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000942 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
943 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
944 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000945 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000946};
947#endif
948
949
950#if _LIBCPP_STD_VER > 11
951template <class _Tp = void>
952#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000954#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000955struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400957 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000958 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
959 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 {return __x | __y;}
961};
962
Marshall Clow974bae22013-07-29 14:21:53 +0000963#if _LIBCPP_STD_VER > 11
964template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000965struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000966{
Marshall Clowd18ee652013-09-28 19:06:12 +0000967 template <class _T1, class _T2>
968 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000969 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000970 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
971 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
972 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000973 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000974};
975#endif
976
977
978#if _LIBCPP_STD_VER > 11
979template <class _Tp = void>
980#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000982#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000983struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984{
Arthur O'Dwyer66bf60a2021-05-29 13:09:07 -0400985 typedef _Tp __result_type; // used by valarray
Marshall Clowd18ee652013-09-28 19:06:12 +0000986 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
987 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 {return __x ^ __y;}
989};
990
Marshall Clow974bae22013-07-29 14:21:53 +0000991#if _LIBCPP_STD_VER > 11
992template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000993struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000994{
Marshall Clowd18ee652013-09-28 19:06:12 +0000995 template <class _T1, class _T2>
996 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000997 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000998 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
999 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
1000 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +00001001 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +00001002};
1003#endif
1004
1005
1006#if _LIBCPP_STD_VER > 11
1007template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001008struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +00001009{
Marshall Clowd18ee652013-09-28 19:06:12 +00001010 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1011 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +00001012 {return ~__x;}
1013};
1014
1015template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001016struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +00001017{
1018 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +00001019 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1020 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +00001021 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
1022 -> decltype (~_VSTD::forward<_Tp>(__x))
1023 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +00001024 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +00001025};
1026#endif
1027
Arthur O'Dwyera6b51072021-05-24 18:36:17 -04001028#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001030class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 : public unary_function<typename _Predicate::argument_type, bool>
1032{
1033 _Predicate __pred_;
1034public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001035 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1036 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001038 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1039 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 {return !__pred_(__x);}
1041};
1042
1043template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001044_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045unary_negate<_Predicate>
1046not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1047
1048template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001049class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 : public binary_function<typename _Predicate::first_argument_type,
1051 typename _Predicate::second_argument_type,
1052 bool>
1053{
1054 _Predicate __pred_;
1055public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001056 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001057 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1058
1059 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1060 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061 const typename _Predicate::second_argument_type& __y) const
1062 {return !__pred_(__x, __y);}
1063};
1064
1065template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001066_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067binary_negate<_Predicate>
1068not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
Arthur O'Dwyera6b51072021-05-24 18:36:17 -04001069#endif // _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_NEGATORS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070
Marshall Clow26a027c2017-04-13 18:25:32 +00001071#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001073class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 : public unary_function<typename __Operation::second_argument_type,
1075 typename __Operation::result_type>
1076{
1077protected:
1078 __Operation op;
1079 typename __Operation::first_argument_type value;
1080public:
1081 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1082 const typename __Operation::first_argument_type __y)
1083 : op(__x), value(__y) {}
1084 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1085 (typename __Operation::second_argument_type& __x) const
1086 {return op(value, __x);}
1087 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1088 (const typename __Operation::second_argument_type& __x) const
1089 {return op(value, __x);}
1090};
1091
1092template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001093_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094binder1st<__Operation>
1095bind1st(const __Operation& __op, const _Tp& __x)
1096 {return binder1st<__Operation>(__op, __x);}
1097
1098template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001099class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100 : public unary_function<typename __Operation::first_argument_type,
1101 typename __Operation::result_type>
1102{
1103protected:
1104 __Operation op;
1105 typename __Operation::second_argument_type value;
1106public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1109 : op(__x), value(__y) {}
1110 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1111 ( typename __Operation::first_argument_type& __x) const
1112 {return op(__x, value);}
1113 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1114 (const typename __Operation::first_argument_type& __x) const
1115 {return op(__x, value);}
1116};
1117
1118template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001119_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120binder2nd<__Operation>
1121bind2nd(const __Operation& __op, const _Tp& __x)
1122 {return binder2nd<__Operation>(__op, __x);}
1123
1124template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001125class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001126 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127{
1128 _Result (*__f_)(_Arg);
1129public:
1130 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1131 : __f_(__f) {}
1132 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1133 {return __f_(__x);}
1134};
1135
1136template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001137_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138pointer_to_unary_function<_Arg,_Result>
1139ptr_fun(_Result (*__f)(_Arg))
1140 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1141
1142template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001143class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001144 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145{
1146 _Result (*__f_)(_Arg1, _Arg2);
1147public:
1148 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1149 : __f_(__f) {}
1150 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1151 {return __f_(__x, __y);}
1152};
1153
1154template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001155_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156pointer_to_binary_function<_Arg1,_Arg2,_Result>
1157ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1158 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1159
1160template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001161class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1162 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163{
1164 _Sp (_Tp::*__p_)();
1165public:
1166 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1167 : __p_(__p) {}
1168 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1169 {return (__p->*__p_)();}
1170};
1171
1172template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001173class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1174 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175{
1176 _Sp (_Tp::*__p_)(_Ap);
1177public:
1178 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1179 : __p_(__p) {}
1180 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1181 {return (__p->*__p_)(__x);}
1182};
1183
1184template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001185_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186mem_fun_t<_Sp,_Tp>
1187mem_fun(_Sp (_Tp::*__f)())
1188 {return mem_fun_t<_Sp,_Tp>(__f);}
1189
1190template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001191_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192mem_fun1_t<_Sp,_Tp,_Ap>
1193mem_fun(_Sp (_Tp::*__f)(_Ap))
1194 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1195
1196template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001197class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1198 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199{
1200 _Sp (_Tp::*__p_)();
1201public:
1202 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1203 : __p_(__p) {}
1204 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1205 {return (__p.*__p_)();}
1206};
1207
1208template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001209class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1210 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211{
1212 _Sp (_Tp::*__p_)(_Ap);
1213public:
1214 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1215 : __p_(__p) {}
1216 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1217 {return (__p.*__p_)(__x);}
1218};
1219
1220template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001221_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222mem_fun_ref_t<_Sp,_Tp>
1223mem_fun_ref(_Sp (_Tp::*__f)())
1224 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1225
1226template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001227_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228mem_fun1_ref_t<_Sp,_Tp,_Ap>
1229mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1230 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1231
1232template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001233class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1234 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235{
1236 _Sp (_Tp::*__p_)() const;
1237public:
1238 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1239 : __p_(__p) {}
1240 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1241 {return (__p->*__p_)();}
1242};
1243
1244template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001245class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1246 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247{
1248 _Sp (_Tp::*__p_)(_Ap) const;
1249public:
1250 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1251 : __p_(__p) {}
1252 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1253 {return (__p->*__p_)(__x);}
1254};
1255
1256template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001257_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258const_mem_fun_t<_Sp,_Tp>
1259mem_fun(_Sp (_Tp::*__f)() const)
1260 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1261
1262template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001263_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264const_mem_fun1_t<_Sp,_Tp,_Ap>
1265mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1266 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1267
1268template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001269class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1270 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271{
1272 _Sp (_Tp::*__p_)() const;
1273public:
1274 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1275 : __p_(__p) {}
1276 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1277 {return (__p.*__p_)();}
1278};
1279
1280template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001281class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001282 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283{
1284 _Sp (_Tp::*__p_)(_Ap) const;
1285public:
1286 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1287 : __p_(__p) {}
1288 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1289 {return (__p.*__p_)(__x);}
1290};
1291
1292template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001293_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294const_mem_fun_ref_t<_Sp,_Tp>
1295mem_fun_ref(_Sp (_Tp::*__f)() const)
1296 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1297
1298template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001299_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001300const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1301mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1302 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001303#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001305////////////////////////////////////////////////////////////////////////////////
1306// MEMFUN
1307//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <class _Tp>
1310class __mem_fn
1311 : public __weak_result_type<_Tp>
1312{
1313public:
1314 // types
1315 typedef _Tp type;
1316private:
1317 type __f_;
1318
1319public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001320 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1321 __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001323#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324 // invoke
1325 template <class... _ArgTypes>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001326 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier2cc48332015-07-22 22:43:27 +00001327 typename __invoke_return<type, _ArgTypes...>::type
1328 operator() (_ArgTypes&&... __args) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001329 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001330 }
1331#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001332
1333 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001334 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001335 typename __invoke_return0<type, _A0>::type
1336 operator() (_A0& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001337 return _VSTD::__invoke(__f_, __a0);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001338 }
1339
Eric Fiselierce1813a2015-08-26 20:15:02 +00001340 template <class _A0>
1341 _LIBCPP_INLINE_VISIBILITY
1342 typename __invoke_return0<type, _A0 const>::type
1343 operator() (_A0 const& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001344 return _VSTD::__invoke(__f_, __a0);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001345 }
1346
Eric Fiselier2cc48332015-07-22 22:43:27 +00001347 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001348 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001349 typename __invoke_return1<type, _A0, _A1>::type
1350 operator() (_A0& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001351 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001352 }
1353
Eric Fiselierce1813a2015-08-26 20:15:02 +00001354 template <class _A0, class _A1>
1355 _LIBCPP_INLINE_VISIBILITY
1356 typename __invoke_return1<type, _A0 const, _A1>::type
1357 operator() (_A0 const& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001358 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001359 }
1360
1361 template <class _A0, class _A1>
1362 _LIBCPP_INLINE_VISIBILITY
1363 typename __invoke_return1<type, _A0, _A1 const>::type
1364 operator() (_A0& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001365 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001366 }
1367
1368 template <class _A0, class _A1>
1369 _LIBCPP_INLINE_VISIBILITY
1370 typename __invoke_return1<type, _A0 const, _A1 const>::type
1371 operator() (_A0 const& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001372 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001373 }
1374
Eric Fiselier2cc48332015-07-22 22:43:27 +00001375 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001376 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001377 typename __invoke_return2<type, _A0, _A1, _A2>::type
1378 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001379 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001380 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001381
1382 template <class _A0, class _A1, class _A2>
1383 _LIBCPP_INLINE_VISIBILITY
1384 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1385 operator() (_A0 const& __a0, _A1& __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, _A1 const, _A2>::type
1392 operator() (_A0& __a0, _A1 const& __a1, _A2& __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, _A2 const>::type
1399 operator() (_A0& __a0, _A1& __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>::type
1406 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __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 }
1409
1410 template <class _A0, class _A1, class _A2>
1411 _LIBCPP_INLINE_VISIBILITY
1412 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1413 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001414 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001415 }
1416
1417 template <class _A0, class _A1, class _A2>
1418 _LIBCPP_INLINE_VISIBILITY
1419 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1420 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001421 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001422 }
1423
1424 template <class _A0, class _A1, class _A2>
1425 _LIBCPP_INLINE_VISIBILITY
1426 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1427 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001428 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001429 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001430#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431};
1432
Howard Hinnantc834c512011-11-29 18:15:50 +00001433template<class _Rp, class _Tp>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001434inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00001435__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001436mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437{
Howard Hinnantc834c512011-11-29 18:15:50 +00001438 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439}
1440
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001441////////////////////////////////////////////////////////////////////////////////
1442// FUNCTION
1443//==============================================================================
1444
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445// bad_function_call
1446
Howard Hinnant4ff57432010-09-21 22:55:27 +00001447class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 : public exception
1449{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001450#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1451public:
1452 virtual ~bad_function_call() _NOEXCEPT;
1453
1454 virtual const char* what() const _NOEXCEPT;
1455#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456};
1457
Louis Dionne16fe2952018-07-11 23:14:33 +00001458_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001459void __throw_bad_function_call()
1460{
1461#ifndef _LIBCPP_NO_EXCEPTIONS
1462 throw bad_function_call();
1463#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001464 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001465#endif
1466}
1467
Louis Dionne44d1f812020-03-09 11:16:22 -04001468#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1469# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1470 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1471#else
1472# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1473#endif
1474
1475template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476
1477namespace __function
1478{
1479
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001480template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481struct __maybe_derive_from_unary_function
1482{
1483};
1484
Howard Hinnantc834c512011-11-29 18:15:50 +00001485template<class _Rp, class _A1>
1486struct __maybe_derive_from_unary_function<_Rp(_A1)>
1487 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488{
1489};
1490
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001491template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492struct __maybe_derive_from_binary_function
1493{
1494};
1495
Howard Hinnantc834c512011-11-29 18:15:50 +00001496template<class _Rp, class _A1, class _A2>
1497struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1498 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499{
1500};
1501
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001502template <class _Fp>
1503_LIBCPP_INLINE_VISIBILITY
1504bool __not_null(_Fp const&) { return true; }
1505
1506template <class _Fp>
1507_LIBCPP_INLINE_VISIBILITY
1508bool __not_null(_Fp* __ptr) { return __ptr; }
1509
1510template <class _Ret, class _Class>
1511_LIBCPP_INLINE_VISIBILITY
1512bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1513
1514template <class _Fp>
1515_LIBCPP_INLINE_VISIBILITY
1516bool __not_null(function<_Fp> const& __f) { return !!__f; }
1517
Louis Dionnee2391d72020-04-22 13:58:17 -04001518#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1519template <class _Rp, class ..._Args>
1520_LIBCPP_INLINE_VISIBILITY
1521bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1522#endif
1523
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001524} // namespace __function
1525
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001526#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001527
1528namespace __function {
1529
Eric Fiselier125798e2018-12-10 18:14:09 +00001530// __alloc_func holds a functor and an allocator.
1531
1532template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001533template <class _Fp, class _FB>
1534class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001535
1536template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1537class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1538{
1539 __compressed_pair<_Fp, _Ap> __f_;
1540
1541 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001542 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1543 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001544
1545 _LIBCPP_INLINE_VISIBILITY
1546 const _Target& __target() const { return __f_.first(); }
1547
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001548 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001549 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001550 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001551
1552 _LIBCPP_INLINE_VISIBILITY
1553 explicit __alloc_func(_Target&& __f)
1554 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1555 _VSTD::forward_as_tuple())
1556 {
1557 }
1558
1559 _LIBCPP_INLINE_VISIBILITY
1560 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1561 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1562 _VSTD::forward_as_tuple(__a))
1563 {
1564 }
1565
1566 _LIBCPP_INLINE_VISIBILITY
1567 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1568 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1569 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1570 {
1571 }
1572
1573 _LIBCPP_INLINE_VISIBILITY
1574 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1575 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1576 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1577 {
1578 }
1579
1580 _LIBCPP_INLINE_VISIBILITY
1581 _Rp operator()(_ArgTypes&&... __arg)
1582 {
1583 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1584 return _Invoker::__call(__f_.first(),
1585 _VSTD::forward<_ArgTypes>(__arg)...);
1586 }
1587
1588 _LIBCPP_INLINE_VISIBILITY
1589 __alloc_func* __clone() const
1590 {
1591 typedef allocator_traits<_Alloc> __alloc_traits;
1592 typedef
1593 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1594 _AA;
1595 _AA __a(__f_.second());
1596 typedef __allocator_destructor<_AA> _Dp;
1597 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1598 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1599 return __hold.release();
1600 }
1601
1602 _LIBCPP_INLINE_VISIBILITY
1603 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001604
1605 static void __destroy_and_delete(__alloc_func* __f) {
1606 typedef allocator_traits<_Alloc> __alloc_traits;
1607 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1608 _FunAlloc;
1609 _FunAlloc __a(__f->__get_allocator());
1610 __f->destroy();
1611 __a.deallocate(__f, 1);
1612 }
1613};
1614
1615template <class _Fp, class _Rp, class... _ArgTypes>
1616class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1617 _Fp __f_;
1618
1619public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001620 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001621
1622 _LIBCPP_INLINE_VISIBILITY
1623 const _Target& __target() const { return __f_; }
1624
1625 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001626 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001627
1628 _LIBCPP_INLINE_VISIBILITY
1629 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1630
1631 _LIBCPP_INLINE_VISIBILITY
1632 _Rp operator()(_ArgTypes&&... __arg) {
1633 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1634 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1635 }
1636
1637 _LIBCPP_INLINE_VISIBILITY
1638 __default_alloc_func* __clone() const {
1639 __builtin_new_allocator::__holder_t __hold =
1640 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1641 __default_alloc_func* __res =
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001642 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00001643 (void)__hold.release();
1644 return __res;
1645 }
1646
1647 _LIBCPP_INLINE_VISIBILITY
1648 void destroy() _NOEXCEPT { __f_.~_Target(); }
1649
1650 static void __destroy_and_delete(__default_alloc_func* __f) {
1651 __f->destroy();
1652 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1653 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001654};
1655
1656// __base provides an abstract interface for copyable functors.
1657
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001658template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659
Howard Hinnantc834c512011-11-29 18:15:50 +00001660template<class _Rp, class ..._ArgTypes>
1661class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662{
1663 __base(const __base&);
1664 __base& operator=(const __base&);
1665public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001666 _LIBCPP_INLINE_VISIBILITY __base() {}
1667 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668 virtual __base* __clone() const = 0;
1669 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001670 virtual void destroy() _NOEXCEPT = 0;
1671 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001672 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001673#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001674 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1675 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001676#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677};
1678
Eric Fiselier125798e2018-12-10 18:14:09 +00001679// __func implements __base for a given functor type.
1680
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681template<class _FD, class _Alloc, class _FB> class __func;
1682
Howard Hinnantc834c512011-11-29 18:15:50 +00001683template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1684class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1685 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686{
Eric Fiselier125798e2018-12-10 18:14:09 +00001687 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001690 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001691 : __f_(_VSTD::move(__f)) {}
1692
Howard Hinnant4ff57432010-09-21 22:55:27 +00001693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001694 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001695 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001696
1697 _LIBCPP_INLINE_VISIBILITY
1698 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001699 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001700
1701 _LIBCPP_INLINE_VISIBILITY
1702 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001703 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1704
Howard Hinnantc834c512011-11-29 18:15:50 +00001705 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1706 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001707 virtual void destroy() _NOEXCEPT;
1708 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001709 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001710#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001711 virtual const void* target(const type_info&) const _NOEXCEPT;
1712 virtual const std::type_info& target_type() const _NOEXCEPT;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001713#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714};
1715
Howard Hinnantc834c512011-11-29 18:15:50 +00001716template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1717__base<_Rp(_ArgTypes...)>*
1718__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001720 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001721 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001722 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001723 typedef __allocator_destructor<_Ap> _Dp;
1724 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001725 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 return __hold.release();
1727}
1728
Howard Hinnantc834c512011-11-29 18:15:50 +00001729template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730void
Howard Hinnantc834c512011-11-29 18:15:50 +00001731__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732{
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001733 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734}
1735
Howard Hinnantc834c512011-11-29 18:15:50 +00001736template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737void
Howard Hinnantc834c512011-11-29 18:15:50 +00001738__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739{
Eric Fiselier125798e2018-12-10 18:14:09 +00001740 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741}
1742
Howard Hinnantc834c512011-11-29 18:15:50 +00001743template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744void
Howard Hinnantc834c512011-11-29 18:15:50 +00001745__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001747 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001748 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001749 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001750 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751 __a.deallocate(this, 1);
1752}
1753
Howard Hinnantc834c512011-11-29 18:15:50 +00001754template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1755_Rp
1756__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757{
Eric Fiselier125798e2018-12-10 18:14:09 +00001758 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759}
1760
Howard Hinnant72f73582010-08-11 17:04:31 +00001761#ifndef _LIBCPP_NO_RTTI
1762
Howard Hinnantc834c512011-11-29 18:15:50 +00001763template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001765__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766{
Howard Hinnantc834c512011-11-29 18:15:50 +00001767 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001768 return &__f_.__target();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001769 return nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770}
1771
Howard Hinnantc834c512011-11-29 18:15:50 +00001772template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001774__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775{
Howard Hinnantc834c512011-11-29 18:15:50 +00001776 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777}
1778
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001779#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001780
Eric Fiselier125798e2018-12-10 18:14:09 +00001781// __value_func creates a value-type from a __func.
1782
1783template <class _Fp> class __value_func;
1784
1785template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1786{
1787 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1788
1789 typedef __base<_Rp(_ArgTypes...)> __func;
1790 __func* __f_;
1791
1792 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1793 {
1794 return reinterpret_cast<__func*>(p);
1795 }
1796
1797 public:
1798 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001799 __value_func() _NOEXCEPT : __f_(nullptr) {}
Eric Fiselier125798e2018-12-10 18:14:09 +00001800
1801 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001802 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001803 : __f_(nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001804 {
1805 typedef allocator_traits<_Alloc> __alloc_traits;
1806 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1807 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1808 _FunAlloc;
1809
1810 if (__function::__not_null(__f))
1811 {
1812 _FunAlloc __af(__a);
1813 if (sizeof(_Fun) <= sizeof(__buf_) &&
1814 is_nothrow_copy_constructible<_Fp>::value &&
1815 is_nothrow_copy_constructible<_FunAlloc>::value)
1816 {
1817 __f_ =
1818 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1819 }
1820 else
1821 {
1822 typedef __allocator_destructor<_FunAlloc> _Dp;
1823 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1824 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1825 __f_ = __hold.release();
1826 }
1827 }
1828 }
1829
Eric Fiselier74ebee62019-06-08 01:31:19 +00001830 template <class _Fp,
1831 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1832 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001833 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001834
Eric Fiselier125798e2018-12-10 18:14:09 +00001835 _LIBCPP_INLINE_VISIBILITY
1836 __value_func(const __value_func& __f)
1837 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001838 if (__f.__f_ == nullptr)
1839 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001840 else if ((void*)__f.__f_ == &__f.__buf_)
1841 {
1842 __f_ = __as_base(&__buf_);
1843 __f.__f_->__clone(__f_);
1844 }
1845 else
1846 __f_ = __f.__f_->__clone();
1847 }
1848
1849 _LIBCPP_INLINE_VISIBILITY
1850 __value_func(__value_func&& __f) _NOEXCEPT
1851 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001852 if (__f.__f_ == nullptr)
1853 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001854 else if ((void*)__f.__f_ == &__f.__buf_)
1855 {
1856 __f_ = __as_base(&__buf_);
1857 __f.__f_->__clone(__f_);
1858 }
1859 else
1860 {
1861 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001862 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001863 }
1864 }
1865
1866 _LIBCPP_INLINE_VISIBILITY
1867 ~__value_func()
1868 {
1869 if ((void*)__f_ == &__buf_)
1870 __f_->destroy();
1871 else if (__f_)
1872 __f_->destroy_deallocate();
1873 }
1874
1875 _LIBCPP_INLINE_VISIBILITY
1876 __value_func& operator=(__value_func&& __f)
1877 {
1878 *this = nullptr;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001879 if (__f.__f_ == nullptr)
1880 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001881 else if ((void*)__f.__f_ == &__f.__buf_)
1882 {
1883 __f_ = __as_base(&__buf_);
1884 __f.__f_->__clone(__f_);
1885 }
1886 else
1887 {
1888 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001889 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001890 }
1891 return *this;
1892 }
1893
1894 _LIBCPP_INLINE_VISIBILITY
1895 __value_func& operator=(nullptr_t)
1896 {
1897 __func* __f = __f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001898 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001899 if ((void*)__f == &__buf_)
1900 __f->destroy();
1901 else if (__f)
1902 __f->destroy_deallocate();
1903 return *this;
1904 }
1905
1906 _LIBCPP_INLINE_VISIBILITY
1907 _Rp operator()(_ArgTypes&&... __args) const
1908 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001909 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001910 __throw_bad_function_call();
1911 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1912 }
1913
1914 _LIBCPP_INLINE_VISIBILITY
1915 void swap(__value_func& __f) _NOEXCEPT
1916 {
1917 if (&__f == this)
1918 return;
1919 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1920 {
1921 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1922 __func* __t = __as_base(&__tempbuf);
1923 __f_->__clone(__t);
1924 __f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001925 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001926 __f.__f_->__clone(__as_base(&__buf_));
1927 __f.__f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001928 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001929 __f_ = __as_base(&__buf_);
1930 __t->__clone(__as_base(&__f.__buf_));
1931 __t->destroy();
1932 __f.__f_ = __as_base(&__f.__buf_);
1933 }
1934 else if ((void*)__f_ == &__buf_)
1935 {
1936 __f_->__clone(__as_base(&__f.__buf_));
1937 __f_->destroy();
1938 __f_ = __f.__f_;
1939 __f.__f_ = __as_base(&__f.__buf_);
1940 }
1941 else if ((void*)__f.__f_ == &__f.__buf_)
1942 {
1943 __f.__f_->__clone(__as_base(&__buf_));
1944 __f.__f_->destroy();
1945 __f.__f_ = __f_;
1946 __f_ = __as_base(&__buf_);
1947 }
1948 else
1949 _VSTD::swap(__f_, __f.__f_);
1950 }
1951
1952 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001953 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
Eric Fiselier125798e2018-12-10 18:14:09 +00001954
1955#ifndef _LIBCPP_NO_RTTI
1956 _LIBCPP_INLINE_VISIBILITY
1957 const std::type_info& target_type() const _NOEXCEPT
1958 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001959 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001960 return typeid(void);
1961 return __f_->target_type();
1962 }
1963
1964 template <typename _Tp>
1965 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1966 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001967 if (__f_ == nullptr)
1968 return nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001969 return (const _Tp*)__f_->target(typeid(_Tp));
1970 }
1971#endif // _LIBCPP_NO_RTTI
1972};
1973
Eric Fiselierf2e64362018-12-11 00:14:34 +00001974// Storage for a functor object, to be used with __policy to manage copy and
1975// destruction.
1976union __policy_storage
1977{
1978 mutable char __small[sizeof(void*) * 2];
1979 void* __large;
1980};
1981
1982// True if _Fun can safely be held in __policy_storage.__small.
1983template <typename _Fun>
1984struct __use_small_storage
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04001985 : public integral_constant<
Eric Fiselierf2e64362018-12-11 00:14:34 +00001986 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001987 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04001988 is_trivially_copy_constructible<_Fun>::value &&
1989 is_trivially_destructible<_Fun>::value> {};
Eric Fiselierf2e64362018-12-11 00:14:34 +00001990
1991// Policy contains information about how to copy, destroy, and move the
1992// underlying functor. You can think of it as a vtable of sorts.
1993struct __policy
1994{
1995 // Used to copy or destroy __large values. null for trivial objects.
1996 void* (*const __clone)(const void*);
1997 void (*const __destroy)(void*);
1998
1999 // True if this is the null policy (no value).
2000 const bool __is_null;
2001
2002 // The target type. May be null if RTTI is disabled.
2003 const std::type_info* const __type_info;
2004
2005 // Returns a pointer to a static policy object suitable for the functor
2006 // type.
2007 template <typename _Fun>
2008 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
2009 {
2010 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
2011 }
2012
2013 _LIBCPP_INLINE_VISIBILITY
2014 static const __policy* __create_empty()
2015 {
2016 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
2017 true,
2018#ifndef _LIBCPP_NO_RTTI
2019 &typeid(void)
2020#else
2021 nullptr
2022#endif
2023 };
2024 return &__policy_;
2025 }
2026
2027 private:
2028 template <typename _Fun> static void* __large_clone(const void* __s)
2029 {
2030 const _Fun* __f = static_cast<const _Fun*>(__s);
2031 return __f->__clone();
2032 }
2033
Eric Fiselier74ebee62019-06-08 01:31:19 +00002034 template <typename _Fun>
2035 static void __large_destroy(void* __s) {
2036 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002037 }
2038
2039 template <typename _Fun>
2040 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002041 __choose_policy(/* is_small = */ false_type) {
2042 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2043 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002044#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002045 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002046#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002047 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002048#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002049 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002050 return &__policy_;
2051 }
2052
2053 template <typename _Fun>
2054 _LIBCPP_INLINE_VISIBILITY static const __policy*
2055 __choose_policy(/* is_small = */ true_type)
2056 {
2057 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2058 nullptr, nullptr, false,
2059#ifndef _LIBCPP_NO_RTTI
2060 &typeid(typename _Fun::_Target)
2061#else
2062 nullptr
2063#endif
2064 };
2065 return &__policy_;
2066 }
2067};
2068
2069// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2070// faster for types that can be passed in registers.
2071template <typename _Tp>
2072using __fast_forward =
Mark de Wever9d7aa7a2021-05-09 18:22:52 +02002073 typename conditional<is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002074
2075// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2076
2077template <class _Fp> struct __policy_invoker;
2078
2079template <class _Rp, class... _ArgTypes>
2080struct __policy_invoker<_Rp(_ArgTypes...)>
2081{
2082 typedef _Rp (*__Call)(const __policy_storage*,
2083 __fast_forward<_ArgTypes>...);
2084
2085 __Call __call_;
2086
2087 // Creates an invoker that throws bad_function_call.
2088 _LIBCPP_INLINE_VISIBILITY
2089 __policy_invoker() : __call_(&__call_empty) {}
2090
2091 // Creates an invoker that calls the given instance of __func.
2092 template <typename _Fun>
2093 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2094 {
2095 return __policy_invoker(&__call_impl<_Fun>);
2096 }
2097
2098 private:
2099 _LIBCPP_INLINE_VISIBILITY
2100 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2101
2102 static _Rp __call_empty(const __policy_storage*,
2103 __fast_forward<_ArgTypes>...)
2104 {
2105 __throw_bad_function_call();
2106 }
2107
2108 template <typename _Fun>
2109 static _Rp __call_impl(const __policy_storage* __buf,
2110 __fast_forward<_ArgTypes>... __args)
2111 {
2112 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2113 ? &__buf->__small
2114 : __buf->__large);
2115 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2116 }
2117};
2118
2119// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2120// copyable functor.
2121
2122template <class _Fp> class __policy_func;
2123
2124template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2125{
2126 // Inline storage for small objects.
2127 __policy_storage __buf_;
2128
2129 // Calls the value stored in __buf_. This could technically be part of
2130 // policy, but storing it here eliminates a level of indirection inside
2131 // operator().
2132 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2133 __invoker __invoker_;
2134
2135 // The policy that describes how to move / copy / destroy __buf_. Never
2136 // null, even if the function is empty.
2137 const __policy* __policy_;
2138
2139 public:
2140 _LIBCPP_INLINE_VISIBILITY
2141 __policy_func() : __policy_(__policy::__create_empty()) {}
2142
2143 template <class _Fp, class _Alloc>
2144 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2145 : __policy_(__policy::__create_empty())
2146 {
2147 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2148 typedef allocator_traits<_Alloc> __alloc_traits;
2149 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2150 _FunAlloc;
2151
2152 if (__function::__not_null(__f))
2153 {
2154 __invoker_ = __invoker::template __create<_Fun>();
2155 __policy_ = __policy::__create<_Fun>();
2156
2157 _FunAlloc __af(__a);
2158 if (__use_small_storage<_Fun>())
2159 {
2160 ::new ((void*)&__buf_.__small)
2161 _Fun(_VSTD::move(__f), _Alloc(__af));
2162 }
2163 else
2164 {
2165 typedef __allocator_destructor<_FunAlloc> _Dp;
2166 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2167 ::new ((void*)__hold.get())
2168 _Fun(_VSTD::move(__f), _Alloc(__af));
2169 __buf_.__large = __hold.release();
2170 }
2171 }
2172 }
2173
Eric Fiselier74ebee62019-06-08 01:31:19 +00002174 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2175 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2176 : __policy_(__policy::__create_empty()) {
2177 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2178
2179 if (__function::__not_null(__f)) {
2180 __invoker_ = __invoker::template __create<_Fun>();
2181 __policy_ = __policy::__create<_Fun>();
2182 if (__use_small_storage<_Fun>()) {
2183 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2184 } else {
2185 __builtin_new_allocator::__holder_t __hold =
2186 __builtin_new_allocator::__allocate_type<_Fun>(1);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002187 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
Eric Fiselier74ebee62019-06-08 01:31:19 +00002188 (void)__hold.release();
2189 }
2190 }
2191 }
2192
Eric Fiselierf2e64362018-12-11 00:14:34 +00002193 _LIBCPP_INLINE_VISIBILITY
2194 __policy_func(const __policy_func& __f)
2195 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2196 __policy_(__f.__policy_)
2197 {
2198 if (__policy_->__clone)
2199 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2200 }
2201
2202 _LIBCPP_INLINE_VISIBILITY
2203 __policy_func(__policy_func&& __f)
2204 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2205 __policy_(__f.__policy_)
2206 {
2207 if (__policy_->__destroy)
2208 {
2209 __f.__policy_ = __policy::__create_empty();
2210 __f.__invoker_ = __invoker();
2211 }
2212 }
2213
2214 _LIBCPP_INLINE_VISIBILITY
2215 ~__policy_func()
2216 {
2217 if (__policy_->__destroy)
2218 __policy_->__destroy(__buf_.__large);
2219 }
2220
2221 _LIBCPP_INLINE_VISIBILITY
2222 __policy_func& operator=(__policy_func&& __f)
2223 {
2224 *this = nullptr;
2225 __buf_ = __f.__buf_;
2226 __invoker_ = __f.__invoker_;
2227 __policy_ = __f.__policy_;
2228 __f.__policy_ = __policy::__create_empty();
2229 __f.__invoker_ = __invoker();
2230 return *this;
2231 }
2232
2233 _LIBCPP_INLINE_VISIBILITY
2234 __policy_func& operator=(nullptr_t)
2235 {
2236 const __policy* __p = __policy_;
2237 __policy_ = __policy::__create_empty();
2238 __invoker_ = __invoker();
2239 if (__p->__destroy)
2240 __p->__destroy(__buf_.__large);
2241 return *this;
2242 }
2243
2244 _LIBCPP_INLINE_VISIBILITY
2245 _Rp operator()(_ArgTypes&&... __args) const
2246 {
2247 return __invoker_.__call_(_VSTD::addressof(__buf_),
2248 _VSTD::forward<_ArgTypes>(__args)...);
2249 }
2250
2251 _LIBCPP_INLINE_VISIBILITY
2252 void swap(__policy_func& __f)
2253 {
2254 _VSTD::swap(__invoker_, __f.__invoker_);
2255 _VSTD::swap(__policy_, __f.__policy_);
2256 _VSTD::swap(__buf_, __f.__buf_);
2257 }
2258
2259 _LIBCPP_INLINE_VISIBILITY
2260 explicit operator bool() const _NOEXCEPT
2261 {
2262 return !__policy_->__is_null;
2263 }
2264
2265#ifndef _LIBCPP_NO_RTTI
2266 _LIBCPP_INLINE_VISIBILITY
2267 const std::type_info& target_type() const _NOEXCEPT
2268 {
2269 return *__policy_->__type_info;
2270 }
2271
2272 template <typename _Tp>
2273 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2274 {
2275 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2276 return nullptr;
2277 if (__policy_->__clone) // Out of line storage.
2278 return reinterpret_cast<const _Tp*>(__buf_.__large);
2279 else
2280 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2281 }
2282#endif // _LIBCPP_NO_RTTI
2283};
2284
Louis Dionne3a632922020-04-23 16:47:52 -04002285#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
Louis Dionnee2391d72020-04-22 13:58:17 -04002286
Louis Dionne91cf4442020-07-31 12:56:36 -04002287extern "C" void *_Block_copy(const void *);
2288extern "C" void _Block_release(const void *);
2289
Louis Dionnee2391d72020-04-22 13:58:17 -04002290template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2291class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2292 : public __base<_Rp(_ArgTypes...)>
2293{
2294 typedef _Rp1(^__block_type)(_ArgTypes1...);
2295 __block_type __f_;
2296
2297public:
2298 _LIBCPP_INLINE_VISIBILITY
2299 explicit __func(__block_type const& __f)
Louis Dionne91cf4442020-07-31 12:56:36 -04002300 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002301 { }
2302
2303 // [TODO] add && to save on a retain
2304
2305 _LIBCPP_INLINE_VISIBILITY
2306 explicit __func(__block_type __f, const _Alloc& /* unused */)
Louis Dionne91cf4442020-07-31 12:56:36 -04002307 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002308 { }
2309
2310 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2311 _LIBCPP_ASSERT(false,
2312 "Block pointers are just pointers, so they should always fit into "
2313 "std::function's small buffer optimization. This function should "
2314 "never be invoked.");
2315 return nullptr;
2316 }
2317
2318 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002319 ::new ((void*)__p) __func(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002320 }
2321
2322 virtual void destroy() _NOEXCEPT {
2323 if (__f_)
Louis Dionne91cf4442020-07-31 12:56:36 -04002324 _Block_release(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002325 __f_ = 0;
2326 }
2327
2328 virtual void destroy_deallocate() _NOEXCEPT {
2329 _LIBCPP_ASSERT(false,
2330 "Block pointers are just pointers, so they should always fit into "
2331 "std::function's small buffer optimization. This function should "
2332 "never be invoked.");
2333 }
2334
2335 virtual _Rp operator()(_ArgTypes&& ... __arg) {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05002336 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
Louis Dionnee2391d72020-04-22 13:58:17 -04002337 }
2338
2339#ifndef _LIBCPP_NO_RTTI
2340 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2341 if (__ti == typeid(__func::__block_type))
2342 return &__f_;
2343 return (const void*)nullptr;
2344 }
2345
2346 virtual const std::type_info& target_type() const _NOEXCEPT {
2347 return typeid(__func::__block_type);
2348 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002349#endif // _LIBCPP_NO_RTTI
Louis Dionnee2391d72020-04-22 13:58:17 -04002350};
2351
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002352#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
Louis Dionnee2391d72020-04-22 13:58:17 -04002353
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354} // __function
2355
Howard Hinnantc834c512011-11-29 18:15:50 +00002356template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002357class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002358 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2359 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002361#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002362 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002363#else
2364 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2365#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366
Eric Fiselier125798e2018-12-10 18:14:09 +00002367 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002368
Eric Fiselier3906a132019-06-23 20:28:29 +00002369 template <class _Fp, bool = _And<
2370 _IsNotSame<__uncvref_t<_Fp>, function>,
zoecarver3e722f22020-05-19 17:15:28 -07002371 __invokable<_Fp, _ArgTypes...>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002372 >::value>
2373 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002374 template <class _Fp>
2375 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002376 {
Arthur O'Dwyer4ba8e3d2021-01-11 16:29:17 -05002377 static const bool value = is_void<_Rp>::value ||
2378 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
2379 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002380 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002381 template <class _Fp>
2382 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002383 {
2384 static const bool value = false;
2385 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002386
2387 template <class _Fp>
zoecarver3e722f22020-05-19 17:15:28 -07002388 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002389public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002390 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391
Howard Hinnantf06d9262010-08-20 19:36:46 +00002392 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002393 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002394 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002395 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002396 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002398 function(function&&) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002399 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002400 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401
Marshall Clow3148f422016-10-13 21:06:03 +00002402#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002403 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002404 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002405 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002406 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002407 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002408 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002409 template<class _Alloc>
2410 function(allocator_arg_t, const _Alloc&, const function&);
2411 template<class _Alloc>
2412 function(allocator_arg_t, const _Alloc&, function&&);
zoecarver3e722f22020-05-19 17:15:28 -07002413 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002414 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002415#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416
2417 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002418 function& operator=(function&&) _NOEXCEPT;
2419 function& operator=(nullptr_t) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002420 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002421 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422
2423 ~function();
2424
Howard Hinnantf06d9262010-08-20 19:36:46 +00002425 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002426 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002427
2428#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002429 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002431 void assign(_Fp&& __f, const _Alloc& __a)
2432 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002433#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434
Howard Hinnantf06d9262010-08-20 19:36:46 +00002435 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002436 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002437 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2438 return static_cast<bool>(__f_);
2439 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441 // deleted overloads close possible hole in the type system
2442 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002443 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002445 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002447 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002448 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449
Howard Hinnant72f73582010-08-11 17:04:31 +00002450#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002451 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002452 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002453 template <typename _Tp> _Tp* target() _NOEXCEPT;
2454 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002455#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456};
2457
Louis Dionne4af49712019-07-18 19:50:56 +00002458#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2459template<class _Rp, class ..._Ap>
2460function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2461
2462template<class _Fp>
2463struct __strip_signature;
2464
2465template<class _Rp, class _Gp, class ..._Ap>
2466struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2467template<class _Rp, class _Gp, class ..._Ap>
2468struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2469template<class _Rp, class _Gp, class ..._Ap>
2470struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2471template<class _Rp, class _Gp, class ..._Ap>
2472struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2473
2474template<class _Rp, class _Gp, class ..._Ap>
2475struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2476template<class _Rp, class _Gp, class ..._Ap>
2477struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2478template<class _Rp, class _Gp, class ..._Ap>
2479struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2480template<class _Rp, class _Gp, class ..._Ap>
2481struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2482
2483template<class _Rp, class _Gp, class ..._Ap>
2484struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2485template<class _Rp, class _Gp, class ..._Ap>
2486struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2487template<class _Rp, class _Gp, class ..._Ap>
2488struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2489template<class _Rp, class _Gp, class ..._Ap>
2490struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2491
2492template<class _Rp, class _Gp, class ..._Ap>
2493struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2494template<class _Rp, class _Gp, class ..._Ap>
2495struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2496template<class _Rp, class _Gp, class ..._Ap>
2497struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2498template<class _Rp, class _Gp, class ..._Ap>
2499struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2500
2501template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2502function(_Fp) -> function<_Stripped>;
2503#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2504
Howard Hinnantc834c512011-11-29 18:15:50 +00002505template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002506function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507
Marshall Clow3148f422016-10-13 21:06:03 +00002508#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002509template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002510template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002511function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002512 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002513#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002514
Eric Fiselier125798e2018-12-10 18:14:09 +00002515template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002516function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002517 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518
Marshall Clow3148f422016-10-13 21:06:03 +00002519#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002520template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002521template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002522function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002523 function&& __f)
2524 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002525#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002526
Eric Fiselier125798e2018-12-10 18:14:09 +00002527template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002528template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002529function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530
Marshall Clow3148f422016-10-13 21:06:03 +00002531#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002532template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002533template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002534function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2535 _Fp __f)
2536 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002537#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002538
Howard Hinnantc834c512011-11-29 18:15:50 +00002539template<class _Rp, class ..._ArgTypes>
2540function<_Rp(_ArgTypes...)>&
2541function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002542{
2543 function(__f).swap(*this);
2544 return *this;
2545}
2546
Howard Hinnantc834c512011-11-29 18:15:50 +00002547template<class _Rp, class ..._ArgTypes>
2548function<_Rp(_ArgTypes...)>&
2549function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002551 __f_ = _VSTD::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002552 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553}
2554
Howard Hinnantc834c512011-11-29 18:15:50 +00002555template<class _Rp, class ..._ArgTypes>
2556function<_Rp(_ArgTypes...)>&
2557function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558{
Eric Fiselier125798e2018-12-10 18:14:09 +00002559 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002560 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561}
2562
Howard Hinnantc834c512011-11-29 18:15:50 +00002563template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002564template <class _Fp, class>
2565function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002566function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567{
Howard Hinnantc834c512011-11-29 18:15:50 +00002568 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 return *this;
2570}
2571
Howard Hinnantc834c512011-11-29 18:15:50 +00002572template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002573function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574
Howard Hinnantc834c512011-11-29 18:15:50 +00002575template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576void
Howard Hinnantc834c512011-11-29 18:15:50 +00002577function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578{
Eric Fiselier125798e2018-12-10 18:14:09 +00002579 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580}
2581
Howard Hinnantc834c512011-11-29 18:15:50 +00002582template<class _Rp, class ..._ArgTypes>
2583_Rp
2584function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585{
Eric Fiselier125798e2018-12-10 18:14:09 +00002586 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587}
2588
Howard Hinnant72f73582010-08-11 17:04:31 +00002589#ifndef _LIBCPP_NO_RTTI
2590
Howard Hinnantc834c512011-11-29 18:15:50 +00002591template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002593function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594{
Eric Fiselier125798e2018-12-10 18:14:09 +00002595 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596}
2597
Howard Hinnantc834c512011-11-29 18:15:50 +00002598template<class _Rp, class ..._ArgTypes>
2599template <typename _Tp>
2600_Tp*
2601function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602{
Eric Fiselier125798e2018-12-10 18:14:09 +00002603 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604}
2605
Howard Hinnantc834c512011-11-29 18:15:50 +00002606template<class _Rp, class ..._ArgTypes>
2607template <typename _Tp>
2608const _Tp*
2609function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610{
Eric Fiselier125798e2018-12-10 18:14:09 +00002611 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612}
2613
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002614#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002615
Howard Hinnantc834c512011-11-29 18:15:50 +00002616template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617inline _LIBCPP_INLINE_VISIBILITY
2618bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002619operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620
Howard Hinnantc834c512011-11-29 18:15:50 +00002621template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622inline _LIBCPP_INLINE_VISIBILITY
2623bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002624operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625
Howard Hinnantc834c512011-11-29 18:15:50 +00002626template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627inline _LIBCPP_INLINE_VISIBILITY
2628bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002629operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630
Howard Hinnantc834c512011-11-29 18:15:50 +00002631template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002632inline _LIBCPP_INLINE_VISIBILITY
2633bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002634operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635
Howard Hinnantc834c512011-11-29 18:15:50 +00002636template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637inline _LIBCPP_INLINE_VISIBILITY
2638void
Howard Hinnantc834c512011-11-29 18:15:50 +00002639swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640{return __x.swap(__y);}
2641
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002642#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002643
2644#include <__functional_03>
2645
2646#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002647
2648////////////////////////////////////////////////////////////////////////////////
2649// BIND
2650//==============================================================================
2651
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002653template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2655
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002656#if _LIBCPP_STD_VER > 14
2657template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002658_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002659#endif
2660
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002662template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2664
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002665#if _LIBCPP_STD_VER > 14
2666template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002667_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002668#endif
2669
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670namespace placeholders
2671{
2672
Howard Hinnantc834c512011-11-29 18:15:50 +00002673template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002675#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002676_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2677_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2678_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2679_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2680_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2681_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2682_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2683_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2684_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2685_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2686#else
Marshall Clow396b2132018-01-02 19:01:45 +00002687/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2688/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2689/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2690/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2691/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2692/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2693/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2694/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2695/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2696/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002697#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698
2699} // placeholders
2700
Howard Hinnantc834c512011-11-29 18:15:50 +00002701template<int _Np>
2702struct __is_placeholder<placeholders::__ph<_Np> >
2703 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002705
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002706#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002707
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708template <class _Tp, class _Uj>
2709inline _LIBCPP_INLINE_VISIBILITY
2710_Tp&
2711__mu(reference_wrapper<_Tp> __t, _Uj&)
2712{
2713 return __t.get();
2714}
2715
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716template <class _Ti, class ..._Uj, size_t ..._Indx>
2717inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002718typename __invoke_of<_Ti&, _Uj...>::type
2719__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002721 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722}
2723
2724template <class _Ti, class ..._Uj>
2725inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002726typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727<
2728 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002729 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730>::type
2731__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2732{
2733 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002734 return _VSTD::__mu_expand(__ti, __uj, __indices());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735}
2736
2737template <bool IsPh, class _Ti, class _Uj>
2738struct __mu_return2 {};
2739
2740template <class _Ti, class _Uj>
2741struct __mu_return2<true, _Ti, _Uj>
2742{
2743 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2744};
2745
2746template <class _Ti, class _Uj>
2747inline _LIBCPP_INLINE_VISIBILITY
2748typename enable_if
2749<
2750 0 < is_placeholder<_Ti>::value,
2751 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2752>::type
2753__mu(_Ti&, _Uj& __uj)
2754{
2755 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002756 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757}
2758
2759template <class _Ti, class _Uj>
2760inline _LIBCPP_INLINE_VISIBILITY
2761typename enable_if
2762<
2763 !is_bind_expression<_Ti>::value &&
2764 is_placeholder<_Ti>::value == 0 &&
2765 !__is_reference_wrapper<_Ti>::value,
2766 _Ti&
2767>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002768__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769{
2770 return __ti;
2771}
2772
Howard Hinnant0415d792011-05-22 15:07:43 +00002773template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2774 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002775struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002777template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002778struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002779{
2780 typedef __nat type;
2781};
2782
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002784struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002786 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787};
2788
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002789template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002790struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2791 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002792{
2793};
2794
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002796struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797{
2798 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2799 _TupleUj>::type&& type;
2800};
2801
2802template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002803struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002804{
2805 typedef typename _Ti::type& type;
2806};
2807
2808template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002809struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810{
2811 typedef _Ti& type;
2812};
2813
2814template <class _Ti, class _TupleUj>
2815struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002816 : public __mu_return_impl<_Ti,
2817 __is_reference_wrapper<_Ti>::value,
2818 is_bind_expression<_Ti>::value,
2819 0 < is_placeholder<_Ti>::value &&
2820 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2821 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822{
2823};
2824
Howard Hinnantc834c512011-11-29 18:15:50 +00002825template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002826struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002827{
2828 static const bool value = false;
2829};
2830
2831template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002832struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002833{
2834 static const bool value = __invokable<_Fp,
2835 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2836};
2837
2838template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002839struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002840{
2841 static const bool value = __invokable<_Fp,
2842 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2843};
2844
2845template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002846 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847struct __bind_return;
2848
Howard Hinnantc834c512011-11-29 18:15:50 +00002849template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002850struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002852 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002854 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 typename __mu_return
2856 <
2857 _BoundArgs,
2858 _TupleUj
2859 >::type...
2860 >::type type;
2861};
2862
Howard Hinnantc834c512011-11-29 18:15:50 +00002863template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002864struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002866 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002868 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869 typename __mu_return
2870 <
2871 const _BoundArgs,
2872 _TupleUj
2873 >::type...
2874 >::type type;
2875};
2876
Howard Hinnantc834c512011-11-29 18:15:50 +00002877template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002879typename __bind_return<_Fp, _BoundArgs, _Args>::type
2880__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 _Args&& __args)
2882{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002883 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884}
2885
Howard Hinnantc834c512011-11-29 18:15:50 +00002886template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002888 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002890protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002891 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002892 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002893private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002894 _Fd __f_;
2895 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896
2897 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2898public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002899 template <class _Gp, class ..._BA,
2900 class = typename enable_if
2901 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002902 is_constructible<_Fd, _Gp>::value &&
2903 !is_same<typename remove_reference<_Gp>::type,
2904 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002905 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002906 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002907 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2908 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002909 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910
2911 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002912 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002913 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914 operator()(_Args&& ...__args)
2915 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002916 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002917 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918 }
2919
2920 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002921 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002922 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 operator()(_Args&& ...__args) const
2924 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002925 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002926 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 }
2928};
2929
Howard Hinnantc834c512011-11-29 18:15:50 +00002930template<class _Fp, class ..._BoundArgs>
2931struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932
Howard Hinnantc834c512011-11-29 18:15:50 +00002933template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002935 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002936{
Howard Hinnantc834c512011-11-29 18:15:50 +00002937 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002938 typedef typename base::_Fd _Fd;
2939 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002941 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942
Howard Hinnant7091e652011-07-02 18:22:36 +00002943
Howard Hinnantf292a922013-07-01 00:01:51 +00002944 template <class _Gp, class ..._BA,
2945 class = typename enable_if
2946 <
2947 is_constructible<_Fd, _Gp>::value &&
2948 !is_same<typename remove_reference<_Gp>::type,
2949 __bind_r>::value
2950 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002951 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002952 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2953 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002954 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955
2956 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002957 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002958 typename enable_if
2959 <
2960 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002961 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002962 result_type
2963 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 operator()(_Args&& ...__args)
2965 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002966 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2967 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968 }
2969
2970 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002971 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002972 typename enable_if
2973 <
2974 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002975 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002976 result_type
2977 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978 operator()(_Args&& ...__args) const
2979 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002980 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2981 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982 }
2983};
2984
Howard Hinnantc834c512011-11-29 18:15:50 +00002985template<class _Rp, class _Fp, class ..._BoundArgs>
2986struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987
Howard Hinnantc834c512011-11-29 18:15:50 +00002988template<class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002989inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002990__bind<_Fp, _BoundArgs...>
2991bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992{
Howard Hinnantc834c512011-11-29 18:15:50 +00002993 typedef __bind<_Fp, _BoundArgs...> type;
2994 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995}
2996
Howard Hinnantc834c512011-11-29 18:15:50 +00002997template<class _Rp, class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002998inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002999__bind_r<_Rp, _Fp, _BoundArgs...>
3000bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001{
Howard Hinnantc834c512011-11-29 18:15:50 +00003002 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
3003 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004}
3005
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003006#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007
Eric Fiselier0d974f12015-07-14 20:16:15 +00003008#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00003009
zoecarver3595cac2021-03-02 16:17:22 -08003010template<class _Op, class _Tuple,
3011 class _Idxs = typename __make_tuple_indices<tuple_size<_Tuple>::value>::type>
3012struct __perfect_forward_impl;
Eric Fiselier934f63b2016-06-02 01:25:41 +00003013
zoecarver3595cac2021-03-02 16:17:22 -08003014template<class _Op, class... _Bound, size_t... _Idxs>
3015struct __perfect_forward_impl<_Op, __tuple_types<_Bound...>, __tuple_indices<_Idxs...>>
3016{
3017 tuple<_Bound...> __bound_;
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) &
3021 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3022 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3023 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003024
zoecarver3595cac2021-03-02 16:17:22 -08003025 template<class... _Args>
3026 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&
3027 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3028 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3029 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003030
zoecarver3595cac2021-03-02 16:17:22 -08003031 template<class... _Args>
3032 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &&
3033 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3034 _VSTD::forward<_Args>(__args)...)))
3035 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3036 _VSTD::forward<_Args>(__args)...))
3037 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3038 _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003039
zoecarver3595cac2021-03-02 16:17:22 -08003040 template<class... _Args>
3041 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&&
3042 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3043 _VSTD::forward<_Args>(__args)...)))
3044 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3045 _VSTD::forward<_Args>(__args)...))
3046 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3047 _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003048
zoecarver3595cac2021-03-02 16:17:22 -08003049 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3050 class = _EnableIf<is_copy_constructible_v<_Fn>>>
3051 constexpr __perfect_forward_impl(__perfect_forward_impl const& __other)
3052 : __bound_(__other.__bound_) {}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003053
zoecarver3595cac2021-03-02 16:17:22 -08003054 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3055 class = _EnableIf<is_move_constructible_v<_Fn>>>
3056 constexpr __perfect_forward_impl(__perfect_forward_impl && __other)
3057 : __bound_(_VSTD::move(__other.__bound_)) {}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003058
zoecarver3595cac2021-03-02 16:17:22 -08003059 template<class... _BoundArgs>
3060 explicit constexpr __perfect_forward_impl(_BoundArgs&&... __bound) :
3061 __bound_(_VSTD::forward<_BoundArgs>(__bound)...) { }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003062};
3063
zoecarver3595cac2021-03-02 16:17:22 -08003064template<class _Op, class... _Args>
3065using __perfect_forward =
3066 __perfect_forward_impl<_Op, __tuple_types<decay_t<_Args>...>>;
3067
3068struct __not_fn_op
3069{
3070 template<class... _Args>
3071 static _LIBCPP_CONSTEXPR_AFTER_CXX17 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,
3078 class = _EnableIf<is_constructible_v<decay_t<_Fn>, _Fn> &&
3079 is_move_constructible_v<_Fn>>>
3080_LIBCPP_CONSTEXPR_AFTER_CXX17 auto not_fn(_Fn&& __f)
3081{
3082 return __perfect_forward<__not_fn_op, _Fn>(_VSTD::forward<_Fn>(__f));
Eric Fiselier934f63b2016-06-02 01:25:41 +00003083}
3084
zoecarver3595cac2021-03-02 16:17:22 -08003085#endif // _LIBCPP_STD_VER > 14
3086
3087#if _LIBCPP_STD_VER > 17
3088
3089struct __bind_front_op
3090{
3091 template<class... _Args>
3092 constexpr static auto __call(_Args&&... __args)
3093 noexcept(noexcept(_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3094 -> decltype( _VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3095 { return _VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3096};
3097
3098template<class _Fn, class... _Args,
3099 class = _EnableIf<conjunction<is_constructible<decay_t<_Fn>, _Fn>,
3100 is_move_constructible<decay_t<_Fn>>,
3101 is_constructible<decay_t<_Args>, _Args>...,
3102 is_move_constructible<decay_t<_Args>>...
3103 >::value>>
3104constexpr auto bind_front(_Fn&& __f, _Args&&... __args)
3105{
3106 return __perfect_forward<__bind_front_op, _Fn, _Args...>(_VSTD::forward<_Fn>(__f),
3107 _VSTD::forward<_Args>(__args)...);
3108}
3109
3110#endif // _LIBCPP_STD_VER > 17
Eric Fiselier0d974f12015-07-14 20:16:15 +00003111
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003112// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113
Marshall Clowa40686b2018-01-08 19:18:00 +00003114#if _LIBCPP_STD_VER > 14
3115
3116// default searcher
3117template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Martin Storsjö4d6f2212021-04-06 10:55:33 +03003118class _LIBCPP_TEMPLATE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00003119public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003120 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne44bcff92018-08-03 22:36:53 +00003121 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00003122 _BinaryPredicate __p = _BinaryPredicate())
3123 : __first_(__f), __last_(__l), __pred_(__p) {}
3124
3125 template <typename _ForwardIterator2>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003126 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00003127 pair<_ForwardIterator2, _ForwardIterator2>
3128 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3129 {
3130 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
Arthur O'Dwyerd8dddf52021-05-10 13:13:04 -04003131 typename iterator_traits<_ForwardIterator>::iterator_category(),
3132 typename iterator_traits<_ForwardIterator2>::iterator_category());
Marshall Clowa40686b2018-01-08 19:18:00 +00003133 }
3134
3135private:
3136 _ForwardIterator __first_;
3137 _ForwardIterator __last_;
3138 _BinaryPredicate __pred_;
Eric Fiselier80663c52019-08-04 07:13:43 +00003139 };
Marshall Clowa40686b2018-01-08 19:18:00 +00003140
3141#endif // _LIBCPP_STD_VER > 14
3142
Louis Dionnedb1892a2018-12-03 14:03:27 +00003143#if _LIBCPP_STD_VER > 17
3144template <class _Tp>
3145using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3146
3147template <class _Tp>
3148using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3149#endif // > C++17
3150
Christopher Di Bellae68ec582021-03-18 17:21:35 +00003151#if _LIBCPP_STD_VER > 17
3152// [func.identity]
3153struct identity {
3154 template<class _Tp>
Arthur O'Dwyer108facb2021-04-05 14:56:03 -04003155 _LIBCPP_NODISCARD_EXT constexpr _Tp&& operator()(_Tp&& __t) const noexcept
Christopher Di Bellae68ec582021-03-18 17:21:35 +00003156 {
3157 return _VSTD::forward<_Tp>(__t);
3158 }
3159
3160 using is_transparent = void;
3161};
3162#endif // _LIBCPP_STD_VER > 17
3163
zoecarver9ce102c2021-04-22 17:33:04 -07003164#if !defined(_LIBCPP_HAS_NO_RANGES)
3165
3166namespace ranges {
3167
3168struct equal_to {
3169 template <class _Tp, class _Up>
3170 requires equality_comparable_with<_Tp, _Up>
3171 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3172 noexcept(noexcept(bool(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u)))) {
3173 return _VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u);
3174 }
3175
3176 using is_transparent = void;
3177};
3178
3179struct not_equal_to {
3180 template <class _Tp, class _Up>
3181 requires equality_comparable_with<_Tp, _Up>
3182 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3183 noexcept(noexcept(bool(!(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u))))) {
3184 return !(_VSTD::forward<_Tp>(__t) == _VSTD::forward<_Up>(__u));
3185 }
3186
3187 using is_transparent = void;
3188};
3189
3190struct greater {
3191 template <class _Tp, class _Up>
3192 requires totally_ordered_with<_Tp, _Up>
3193 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3194 noexcept(noexcept(bool(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t)))) {
3195 return _VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t);
3196 }
3197
3198 using is_transparent = void;
3199};
3200
3201struct less {
3202 template <class _Tp, class _Up>
3203 requires totally_ordered_with<_Tp, _Up>
3204 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3205 noexcept(noexcept(bool(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u)))) {
3206 return _VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u);
3207 }
3208
3209 using is_transparent = void;
3210};
3211
3212struct greater_equal {
3213 template <class _Tp, class _Up>
3214 requires totally_ordered_with<_Tp, _Up>
3215 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3216 noexcept(noexcept(bool(!(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u))))) {
3217 return !(_VSTD::forward<_Tp>(__t) < _VSTD::forward<_Up>(__u));
3218 }
3219
3220 using is_transparent = void;
3221};
3222
3223struct less_equal {
3224 template <class _Tp, class _Up>
3225 requires totally_ordered_with<_Tp, _Up>
3226 [[nodiscard]] constexpr bool operator()(_Tp &&__t, _Up &&__u) const
3227 noexcept(noexcept(bool(!(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t))))) {
3228 return !(_VSTD::forward<_Up>(__u) < _VSTD::forward<_Tp>(__t));
3229 }
3230
3231 using is_transparent = void;
3232};
3233
3234} // namespace ranges
3235
3236#endif // !defined(_LIBCPP_HAS_NO_RANGES)
3237
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238_LIBCPP_END_NAMESPACE_STD
3239
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003240#endif // _LIBCPP_FUNCTIONAL