blob: 0979e7d2bd1d7121accc76bead46e66ac3eea9bd [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
Howard Hinnantf7724cd2011-05-28 17:59:48 +000045 reference_wrapper(T&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000046 reference_wrapper(T&&) = delete; // do not bind to temps
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
Howard Hinnantf7724cd2011-05-28 17:59:48 +000062template <class T> reference_wrapper<T> ref(T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000063template <class T> void ref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000064template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000065
Howard Hinnantf7724cd2011-05-28 17:59:48 +000066template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000067template <class T> void cref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000068template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000069
Louis Dionnedb1892a2018-12-03 14:03:27 +000070template <class T> struct unwrap_reference; // since C++20
71template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { }; // since C++20
72template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
73template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
74
Marshall Clow974bae22013-07-29 14:21:53 +000075template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000076struct plus : binary_function<T, T, T>
77{
78 T operator()(const T& x, const T& y) const;
79};
80
Marshall Clow974bae22013-07-29 14:21:53 +000081template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000082struct minus : binary_function<T, T, T>
83{
84 T operator()(const T& x, const T& y) const;
85};
86
Marshall Clow974bae22013-07-29 14:21:53 +000087template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000088struct multiplies : binary_function<T, T, T>
89{
90 T operator()(const T& x, const T& y) const;
91};
92
Marshall Clow974bae22013-07-29 14:21:53 +000093template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000094struct divides : binary_function<T, T, T>
95{
96 T operator()(const T& x, const T& y) const;
97};
98
Marshall Clow974bae22013-07-29 14:21:53 +000099template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100struct modulus : binary_function<T, T, T>
101{
102 T operator()(const T& x, const T& y) const;
103};
104
Marshall Clow974bae22013-07-29 14:21:53 +0000105template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106struct negate : unary_function<T, T>
107{
108 T operator()(const T& x) const;
109};
110
Marshall Clow974bae22013-07-29 14:21:53 +0000111template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112struct equal_to : binary_function<T, T, bool>
113{
114 bool operator()(const T& x, const T& y) const;
115};
116
Marshall Clow974bae22013-07-29 14:21:53 +0000117template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118struct not_equal_to : binary_function<T, T, bool>
119{
120 bool operator()(const T& x, const T& y) const;
121};
122
Marshall Clow974bae22013-07-29 14:21:53 +0000123template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124struct greater : binary_function<T, T, bool>
125{
126 bool operator()(const T& x, const T& y) const;
127};
128
Marshall Clow974bae22013-07-29 14:21:53 +0000129template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130struct less : binary_function<T, T, bool>
131{
132 bool operator()(const T& x, const T& y) const;
133};
134
Marshall Clow974bae22013-07-29 14:21:53 +0000135template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136struct greater_equal : binary_function<T, T, bool>
137{
138 bool operator()(const T& x, const T& y) const;
139};
140
Marshall Clow974bae22013-07-29 14:21:53 +0000141template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142struct less_equal : binary_function<T, T, bool>
143{
144 bool operator()(const T& x, const T& y) const;
145};
146
Marshall Clow974bae22013-07-29 14:21:53 +0000147template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148struct logical_and : binary_function<T, T, bool>
149{
150 bool operator()(const T& x, const T& y) const;
151};
152
Marshall Clow974bae22013-07-29 14:21:53 +0000153template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154struct logical_or : binary_function<T, T, bool>
155{
156 bool operator()(const T& x, const T& y) const;
157};
158
Marshall Clow974bae22013-07-29 14:21:53 +0000159template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160struct logical_not : unary_function<T, bool>
161{
162 bool operator()(const T& x) const;
163};
164
Marshall Clow974bae22013-07-29 14:21:53 +0000165template <class T> // <class T=void> in C++14
166struct bit_and : unary_function<T, bool>
167{
168 bool operator()(const T& x, const T& y) const;
169};
170
171template <class T> // <class T=void> in C++14
172struct bit_or : unary_function<T, bool>
173{
174 bool operator()(const T& x, const T& y) const;
175};
176
177template <class T> // <class T=void> in C++14
178struct bit_xor : unary_function<T, bool>
179{
180 bool operator()(const T& x, const T& y) const;
181};
182
183template <class T=void> // C++14
184struct bit_xor : unary_function<T, bool>
185{
186 bool operator()(const T& x) const;
187};
188
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000190class unary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 : public unary_function<typename Predicate::argument_type, bool>
192{
193public:
194 explicit unary_negate(const Predicate& pred);
195 bool operator()(const typename Predicate::argument_type& x) const;
196};
197
Louis Dionne481a2662018-09-23 18:35:00 +0000198template <class Predicate> // deprecated in C++17
199unary_negate<Predicate> not1(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
201template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000202class binary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203 : public binary_function<typename Predicate::first_argument_type,
204 typename Predicate::second_argument_type,
205 bool>
206{
207public:
208 explicit binary_negate(const Predicate& pred);
209 bool operator()(const typename Predicate::first_argument_type& x,
210 const typename Predicate::second_argument_type& y) const;
211};
212
Louis Dionne481a2662018-09-23 18:35:00 +0000213template <class Predicate> // deprecated in C++17
214binary_negate<Predicate> not2(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
Eric Fiselier934f63b2016-06-02 01:25:41 +0000216template <class F> unspecified not_fn(F&& f); // C++17
217
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218template<class T> struct is_bind_expression;
219template<class T> struct is_placeholder;
220
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000221 // See C++14 20.9.9, Function object binders
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000222template <class T> inline constexpr bool is_bind_expression_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000223 = is_bind_expression<T>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000224template <class T> inline constexpr int is_placeholder_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000225 = is_placeholder<T>::value; // C++17
226
227
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000228template<class Fn, class... BoundArgs>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000229 unspecified bind(Fn&&, BoundArgs&&...);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000230template<class R, class Fn, class... BoundArgs>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000231 unspecified bind(Fn&&, BoundArgs&&...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232
Louis Dionneaf34f122019-04-03 17:54:37 +0000233template<class F, class... Args>
234 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
235 noexcept(is_nothrow_invocable_v<F, Args...>);
236
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000237namespace placeholders {
238 // M is the implementation-defined number of placeholders
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 extern unspecified _1;
240 extern unspecified _2;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000241 .
242 .
243 .
Howard Hinnantc834c512011-11-29 18:15:50 +0000244 extern unspecified _Mp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245}
246
247template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000248class binder1st // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249 : public unary_function<typename Operation::second_argument_type,
250 typename Operation::result_type>
251{
252protected:
253 Operation op;
254 typename Operation::first_argument_type value;
255public:
256 binder1st(const Operation& x, const typename Operation::first_argument_type y);
257 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
258 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
259};
260
261template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000262binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263
264template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000265class binder2nd // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266 : public unary_function<typename Operation::first_argument_type,
267 typename Operation::result_type>
268{
269protected:
270 Operation op;
271 typename Operation::second_argument_type value;
272public:
273 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
274 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
275 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
276};
277
278template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000279binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280
Marshall Clow26a027c2017-04-13 18:25:32 +0000281template <class Arg, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282class pointer_to_unary_function : public unary_function<Arg, Result>
283{
284public:
285 explicit pointer_to_unary_function(Result (*f)(Arg));
286 Result operator()(Arg x) const;
287};
288
289template <class Arg, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000290pointer_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 +0000291
Marshall Clow26a027c2017-04-13 18:25:32 +0000292template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
294{
295public:
296 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
297 Result operator()(Arg1 x, Arg2 y) const;
298};
299
300template <class Arg1, class Arg2, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000301pointer_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 +0000302
Marshall Clow26a027c2017-04-13 18:25:32 +0000303template<class S, class T> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304class mem_fun_t : public unary_function<T*, S>
305{
306public:
307 explicit mem_fun_t(S (T::*p)());
308 S operator()(T* p) const;
309};
310
311template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000312class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313{
314public:
315 explicit mem_fun1_t(S (T::*p)(A));
316 S operator()(T* p, A x) const;
317};
318
Marshall Clow26a027c2017-04-13 18:25:32 +0000319template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
320template<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 +0000321
322template<class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000323class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324{
325public:
326 explicit mem_fun_ref_t(S (T::*p)());
327 S operator()(T& p) const;
328};
329
330template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000331class 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 +0000332{
333public:
334 explicit mem_fun1_ref_t(S (T::*p)(A));
335 S operator()(T& p, A x) const;
336};
337
Marshall Clow26a027c2017-04-13 18:25:32 +0000338template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
339template<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 +0000340
341template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000342class 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 +0000343{
344public:
345 explicit const_mem_fun_t(S (T::*p)() const);
346 S operator()(const T* p) const;
347};
348
349template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000350class 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 +0000351{
352public:
353 explicit const_mem_fun1_t(S (T::*p)(A) const);
354 S operator()(const T* p, A x) const;
355};
356
Marshall Clow26a027c2017-04-13 18:25:32 +0000357template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
358template <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 +0000359
360template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000361class 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 +0000362{
363public:
364 explicit const_mem_fun_ref_t(S (T::*p)() const);
365 S operator()(const T& p) const;
366};
367
368template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000369class 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 +0000370{
371public:
372 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
373 S operator()(const T& p, A x) const;
374};
375
Marshall Clow26a027c2017-04-13 18:25:32 +0000376template <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
377template <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 +0000378
Howard Hinnantf06d9262010-08-20 19:36:46 +0000379template<class R, class T> unspecified mem_fn(R T::*);
Howard Hinnantf06d9262010-08-20 19:36:46 +0000380
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381class bad_function_call
382 : public exception
383{
384};
385
Howard Hinnantf06d9262010-08-20 19:36:46 +0000386template<class> class function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387
Howard Hinnantf06d9262010-08-20 19:36:46 +0000388template<class R, class... ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389class function<R(ArgTypes...)>
390 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
391 // ArgTypes contains T1
392 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
393 // ArgTypes contains T1 and T2
394{
395public:
396 typedef R result_type;
397
Howard Hinnantf06d9262010-08-20 19:36:46 +0000398 // construct/copy/destroy:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000399 function() noexcept;
400 function(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000402 function(function&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 template<class F>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404 function(F);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000406 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000408 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000410 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000412 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 template<class F, Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000414 function(allocator_arg_t, const Alloc&, F); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415
416 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000417 function& operator=(function&&) noexcept;
Howard Hinnant7b85be02011-05-29 13:53:56 +0000418 function& operator=(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419 template<class F>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000420 function& operator=(F&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 template<class F>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000422 function& operator=(reference_wrapper<F>) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424 ~function();
425
Howard Hinnantf06d9262010-08-20 19:36:46 +0000426 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000427 void swap(function&) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +0000428 template<class F, class Alloc>
Marshall Clowfc8fd832016-01-25 17:29:55 +0000429 void assign(F&&, const Alloc&); // Removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430
Howard Hinnantf06d9262010-08-20 19:36:46 +0000431 // function capacity:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000432 explicit operator bool() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433
Howard Hinnantf06d9262010-08-20 19:36:46 +0000434 // function invocation:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435 R operator()(ArgTypes...) const;
436
Howard Hinnantf06d9262010-08-20 19:36:46 +0000437 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000438 const std::type_info& target_type() const noexcept;
439 template <typename T> T* target() noexcept;
440 template <typename T> const T* target() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441};
442
Louis Dionne4af49712019-07-18 19:50:56 +0000443// Deduction guides
444template<class R, class ...Args>
445function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
446
447template<class F>
448function(F) -> function<see-below>; // since C++17
449
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000450// Null pointer comparisons:
451template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000452 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000454template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000455 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000457template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000458 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000460template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000461 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000462
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000463// specialized algorithms:
464template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000465 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000466
467template <class T> struct hash;
468
469template <> struct hash<bool>;
470template <> struct hash<char>;
471template <> struct hash<signed char>;
472template <> struct hash<unsigned char>;
473template <> struct hash<char16_t>;
474template <> struct hash<char32_t>;
475template <> struct hash<wchar_t>;
476template <> struct hash<short>;
477template <> struct hash<unsigned short>;
478template <> struct hash<int>;
479template <> struct hash<unsigned int>;
480template <> struct hash<long>;
481template <> struct hash<long long>;
482template <> struct hash<unsigned long>;
483template <> struct hash<unsigned long long>;
484
485template <> struct hash<float>;
486template <> struct hash<double>;
487template <> struct hash<long double>;
488
489template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000490template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491
492} // std
493
494POLICY: For non-variadic implementations, the number of arguments is limited
495 to 3. It is hoped that the need for non-variadic implementations
496 will be minimal.
497
498*/
499
500#include <__config>
501#include <type_traits>
502#include <typeinfo>
503#include <exception>
504#include <memory>
505#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000506#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000507#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508
509#include <__functional_base>
510
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000511#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000513#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514
515_LIBCPP_BEGIN_NAMESPACE_STD
516
Marshall Clow974bae22013-07-29 14:21:53 +0000517#if _LIBCPP_STD_VER > 11
518template <class _Tp = void>
519#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000521#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000522struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523{
Marshall Clowd18ee652013-09-28 19:06:12 +0000524 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
525 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526 {return __x + __y;}
527};
528
Marshall Clow974bae22013-07-29 14:21:53 +0000529#if _LIBCPP_STD_VER > 11
530template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000531struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000532{
533 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000534 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
535 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000536 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
537 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
538 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000539 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000540};
541#endif
542
543
544#if _LIBCPP_STD_VER > 11
545template <class _Tp = void>
546#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000548#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000549struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550{
Marshall Clowd18ee652013-09-28 19:06:12 +0000551 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
552 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553 {return __x - __y;}
554};
555
Marshall Clow974bae22013-07-29 14:21:53 +0000556#if _LIBCPP_STD_VER > 11
557template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000558struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000559{
560 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000561 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
562 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000563 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
564 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
565 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000566 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000567};
568#endif
569
570
571#if _LIBCPP_STD_VER > 11
572template <class _Tp = void>
573#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000575#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000576struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577{
Marshall Clowd18ee652013-09-28 19:06:12 +0000578 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
579 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580 {return __x * __y;}
581};
582
Marshall Clow974bae22013-07-29 14:21:53 +0000583#if _LIBCPP_STD_VER > 11
584template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000585struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000586{
587 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000588 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
589 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000590 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
591 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
592 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000593 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000594};
595#endif
596
597
598#if _LIBCPP_STD_VER > 11
599template <class _Tp = void>
600#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000602#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000603struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604{
Marshall Clowd18ee652013-09-28 19:06:12 +0000605 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
606 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607 {return __x / __y;}
608};
609
Marshall Clow974bae22013-07-29 14:21:53 +0000610#if _LIBCPP_STD_VER > 11
611template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000612struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000613{
614 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000615 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
616 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000617 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
618 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
619 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000620 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000621};
622#endif
623
624
625#if _LIBCPP_STD_VER > 11
626template <class _Tp = void>
627#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000628template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000629#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000630struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631{
Marshall Clowd18ee652013-09-28 19:06:12 +0000632 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
633 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634 {return __x % __y;}
635};
636
Marshall Clow974bae22013-07-29 14:21:53 +0000637#if _LIBCPP_STD_VER > 11
638template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000639struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000640{
641 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000642 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
643 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000644 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
645 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
646 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000647 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000648};
649#endif
650
651
652#if _LIBCPP_STD_VER > 11
653template <class _Tp = void>
654#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000656#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000657struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658{
Marshall Clowd18ee652013-09-28 19:06:12 +0000659 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
660 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661 {return -__x;}
662};
663
Marshall Clow974bae22013-07-29 14:21:53 +0000664#if _LIBCPP_STD_VER > 11
665template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000666struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000667{
668 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000669 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
670 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000671 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
672 -> decltype (- _VSTD::forward<_Tp>(__x))
673 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000674 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000675};
676#endif
677
678
679#if _LIBCPP_STD_VER > 11
680template <class _Tp = void>
681#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000683#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000684struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685{
Marshall Clowd18ee652013-09-28 19:06:12 +0000686 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
687 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 {return __x == __y;}
689};
690
Marshall Clow974bae22013-07-29 14:21:53 +0000691#if _LIBCPP_STD_VER > 11
692template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000693struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000694{
Marshall Clowd18ee652013-09-28 19:06:12 +0000695 template <class _T1, class _T2>
696 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000697 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000698 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
699 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
700 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000701 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000702};
703#endif
704
705
706#if _LIBCPP_STD_VER > 11
707template <class _Tp = void>
708#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000709template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000710#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000711struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712{
Marshall Clowd18ee652013-09-28 19:06:12 +0000713 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
714 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715 {return __x != __y;}
716};
717
Marshall Clow974bae22013-07-29 14:21:53 +0000718#if _LIBCPP_STD_VER > 11
719template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000720struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000721{
Marshall Clowd18ee652013-09-28 19:06:12 +0000722 template <class _T1, class _T2>
723 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000724 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000725 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
726 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
727 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000728 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000729};
730#endif
731
732
733#if _LIBCPP_STD_VER > 11
734template <class _Tp = void>
735#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000737#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000738struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739{
Marshall Clowd18ee652013-09-28 19:06:12 +0000740 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
741 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742 {return __x > __y;}
743};
744
Marshall Clow974bae22013-07-29 14:21:53 +0000745#if _LIBCPP_STD_VER > 11
746template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000747struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000748{
Marshall Clowd18ee652013-09-28 19:06:12 +0000749 template <class _T1, class _T2>
750 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000751 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000752 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
753 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
754 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000755 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000756};
757#endif
758
759
Howard Hinnantb17caf92012-02-21 21:02:58 +0000760// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761
Marshall Clow974bae22013-07-29 14:21:53 +0000762#if _LIBCPP_STD_VER > 11
763template <class _Tp = void>
764#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000766#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000767struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768{
Marshall Clowd18ee652013-09-28 19:06:12 +0000769 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
770 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771 {return __x >= __y;}
772};
773
Marshall Clow974bae22013-07-29 14:21:53 +0000774#if _LIBCPP_STD_VER > 11
775template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000776struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000777{
Marshall Clowd18ee652013-09-28 19:06:12 +0000778 template <class _T1, class _T2>
779 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000780 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000781 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
782 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
783 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000784 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000785};
786#endif
787
788
789#if _LIBCPP_STD_VER > 11
790template <class _Tp = void>
791#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000793#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000794struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000795{
Marshall Clowd18ee652013-09-28 19:06:12 +0000796 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
797 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798 {return __x <= __y;}
799};
800
Marshall Clow974bae22013-07-29 14:21:53 +0000801#if _LIBCPP_STD_VER > 11
802template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000803struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000804{
Marshall Clowd18ee652013-09-28 19:06:12 +0000805 template <class _T1, class _T2>
806 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000807 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000808 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
809 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
810 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000811 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000812};
813#endif
814
815
816#if _LIBCPP_STD_VER > 11
817template <class _Tp = void>
818#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000820#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000821struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822{
Marshall Clowd18ee652013-09-28 19:06:12 +0000823 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
824 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000825 {return __x && __y;}
826};
827
Marshall Clow974bae22013-07-29 14:21:53 +0000828#if _LIBCPP_STD_VER > 11
829template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000830struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000831{
Marshall Clowd18ee652013-09-28 19:06:12 +0000832 template <class _T1, class _T2>
833 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000834 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000835 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
836 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
837 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000838 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000839};
840#endif
841
842
843#if _LIBCPP_STD_VER > 11
844template <class _Tp = void>
845#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000847#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000848struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849{
Marshall Clowd18ee652013-09-28 19:06:12 +0000850 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
851 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852 {return __x || __y;}
853};
854
Marshall Clow974bae22013-07-29 14:21:53 +0000855#if _LIBCPP_STD_VER > 11
856template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000857struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000858{
Marshall Clowd18ee652013-09-28 19:06:12 +0000859 template <class _T1, class _T2>
860 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000861 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000862 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
863 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
864 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000865 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000866};
867#endif
868
869
870#if _LIBCPP_STD_VER > 11
871template <class _Tp = void>
872#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000874#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000875struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876{
Marshall Clowd18ee652013-09-28 19:06:12 +0000877 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
878 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 {return !__x;}
880};
881
Marshall Clow974bae22013-07-29 14:21:53 +0000882#if _LIBCPP_STD_VER > 11
883template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000884struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000885{
886 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000887 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
888 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000889 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
890 -> decltype (!_VSTD::forward<_Tp>(__x))
891 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000892 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000893};
894#endif
895
896
897#if _LIBCPP_STD_VER > 11
898template <class _Tp = void>
899#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000901#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000902struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903{
Marshall Clowd18ee652013-09-28 19:06:12 +0000904 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
905 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000906 {return __x & __y;}
907};
908
Marshall Clow974bae22013-07-29 14:21:53 +0000909#if _LIBCPP_STD_VER > 11
910template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000911struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000912{
Marshall Clowd18ee652013-09-28 19:06:12 +0000913 template <class _T1, class _T2>
914 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000915 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000916 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
917 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
918 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000919 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000920};
921#endif
922
923
924#if _LIBCPP_STD_VER > 11
925template <class _Tp = void>
926#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000928#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000929struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930{
Marshall Clowd18ee652013-09-28 19:06:12 +0000931 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
932 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933 {return __x | __y;}
934};
935
Marshall Clow974bae22013-07-29 14:21:53 +0000936#if _LIBCPP_STD_VER > 11
937template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000938struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000939{
Marshall Clowd18ee652013-09-28 19:06:12 +0000940 template <class _T1, class _T2>
941 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000942 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000943 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
944 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
945 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000946 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000947};
948#endif
949
950
951#if _LIBCPP_STD_VER > 11
952template <class _Tp = void>
953#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000955#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000956struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957{
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_xor<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>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000980struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000981{
Marshall Clowd18ee652013-09-28 19:06:12 +0000982 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
983 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000984 {return ~__x;}
985};
986
987template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000988struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000989{
990 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000991 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
992 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000993 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
994 -> decltype (~_VSTD::forward<_Tp>(__x))
995 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000996 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000997};
998#endif
999
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001001class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002 : public unary_function<typename _Predicate::argument_type, bool>
1003{
1004 _Predicate __pred_;
1005public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001006 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1007 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001009 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1010 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 {return !__pred_(__x);}
1012};
1013
1014template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001015_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016unary_negate<_Predicate>
1017not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1018
1019template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001020class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021 : public binary_function<typename _Predicate::first_argument_type,
1022 typename _Predicate::second_argument_type,
1023 bool>
1024{
1025 _Predicate __pred_;
1026public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001027 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001028 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1029
1030 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1031 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 const typename _Predicate::second_argument_type& __y) const
1033 {return !__pred_(__x, __y);}
1034};
1035
1036template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001037_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038binary_negate<_Predicate>
1039not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1040
Marshall Clow26a027c2017-04-13 18:25:32 +00001041#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001043class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 : public unary_function<typename __Operation::second_argument_type,
1045 typename __Operation::result_type>
1046{
1047protected:
1048 __Operation op;
1049 typename __Operation::first_argument_type value;
1050public:
1051 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1052 const typename __Operation::first_argument_type __y)
1053 : op(__x), value(__y) {}
1054 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1055 (typename __Operation::second_argument_type& __x) const
1056 {return op(value, __x);}
1057 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1058 (const typename __Operation::second_argument_type& __x) const
1059 {return op(value, __x);}
1060};
1061
1062template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001063_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064binder1st<__Operation>
1065bind1st(const __Operation& __op, const _Tp& __x)
1066 {return binder1st<__Operation>(__op, __x);}
1067
1068template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001069class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 : public unary_function<typename __Operation::first_argument_type,
1071 typename __Operation::result_type>
1072{
1073protected:
1074 __Operation op;
1075 typename __Operation::second_argument_type value;
1076public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1079 : op(__x), value(__y) {}
1080 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1081 ( typename __Operation::first_argument_type& __x) const
1082 {return op(__x, value);}
1083 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1084 (const typename __Operation::first_argument_type& __x) const
1085 {return op(__x, value);}
1086};
1087
1088template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001089_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090binder2nd<__Operation>
1091bind2nd(const __Operation& __op, const _Tp& __x)
1092 {return binder2nd<__Operation>(__op, __x);}
1093
1094template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001095class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001096 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097{
1098 _Result (*__f_)(_Arg);
1099public:
1100 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1101 : __f_(__f) {}
1102 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1103 {return __f_(__x);}
1104};
1105
1106template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001107_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108pointer_to_unary_function<_Arg,_Result>
1109ptr_fun(_Result (*__f)(_Arg))
1110 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1111
1112template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001113class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001114 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115{
1116 _Result (*__f_)(_Arg1, _Arg2);
1117public:
1118 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1119 : __f_(__f) {}
1120 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1121 {return __f_(__x, __y);}
1122};
1123
1124template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001125_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126pointer_to_binary_function<_Arg1,_Arg2,_Result>
1127ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1128 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1129
1130template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001131class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1132 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133{
1134 _Sp (_Tp::*__p_)();
1135public:
1136 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1137 : __p_(__p) {}
1138 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1139 {return (__p->*__p_)();}
1140};
1141
1142template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001143class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1144 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145{
1146 _Sp (_Tp::*__p_)(_Ap);
1147public:
1148 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1149 : __p_(__p) {}
1150 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1151 {return (__p->*__p_)(__x);}
1152};
1153
1154template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001155_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156mem_fun_t<_Sp,_Tp>
1157mem_fun(_Sp (_Tp::*__f)())
1158 {return mem_fun_t<_Sp,_Tp>(__f);}
1159
1160template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001161_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162mem_fun1_t<_Sp,_Tp,_Ap>
1163mem_fun(_Sp (_Tp::*__f)(_Ap))
1164 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1165
1166template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001167class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1168 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169{
1170 _Sp (_Tp::*__p_)();
1171public:
1172 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1173 : __p_(__p) {}
1174 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1175 {return (__p.*__p_)();}
1176};
1177
1178template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001179class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1180 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181{
1182 _Sp (_Tp::*__p_)(_Ap);
1183public:
1184 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1185 : __p_(__p) {}
1186 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1187 {return (__p.*__p_)(__x);}
1188};
1189
1190template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001191_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192mem_fun_ref_t<_Sp,_Tp>
1193mem_fun_ref(_Sp (_Tp::*__f)())
1194 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1195
1196template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001197_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198mem_fun1_ref_t<_Sp,_Tp,_Ap>
1199mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1200 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1201
1202template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001203class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1204 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205{
1206 _Sp (_Tp::*__p_)() const;
1207public:
1208 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1209 : __p_(__p) {}
1210 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1211 {return (__p->*__p_)();}
1212};
1213
1214template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001215class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1216 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217{
1218 _Sp (_Tp::*__p_)(_Ap) const;
1219public:
1220 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1221 : __p_(__p) {}
1222 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1223 {return (__p->*__p_)(__x);}
1224};
1225
1226template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001227_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228const_mem_fun_t<_Sp,_Tp>
1229mem_fun(_Sp (_Tp::*__f)() const)
1230 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1231
1232template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001233_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234const_mem_fun1_t<_Sp,_Tp,_Ap>
1235mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1236 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1237
1238template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001239class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1240 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241{
1242 _Sp (_Tp::*__p_)() const;
1243public:
1244 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1245 : __p_(__p) {}
1246 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1247 {return (__p.*__p_)();}
1248};
1249
1250template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001251class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001252 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253{
1254 _Sp (_Tp::*__p_)(_Ap) const;
1255public:
1256 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1257 : __p_(__p) {}
1258 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1259 {return (__p.*__p_)(__x);}
1260};
1261
1262template <class _Sp, class _Tp>
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_fun_ref_t<_Sp,_Tp>
1265mem_fun_ref(_Sp (_Tp::*__f)() const)
1266 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1267
1268template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001269_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1271mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1272 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001273#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001275////////////////////////////////////////////////////////////////////////////////
1276// MEMFUN
1277//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279template <class _Tp>
1280class __mem_fn
1281 : public __weak_result_type<_Tp>
1282{
1283public:
1284 // types
1285 typedef _Tp type;
1286private:
1287 type __f_;
1288
1289public:
Marshall Clowad8ff212015-10-25 20:12:16 +00001290 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001292#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293 // invoke
1294 template <class... _ArgTypes>
Eric Fiselier2cc48332015-07-22 22:43:27 +00001295 _LIBCPP_INLINE_VISIBILITY
1296 typename __invoke_return<type, _ArgTypes...>::type
1297 operator() (_ArgTypes&&... __args) const {
1298 return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
1299 }
1300#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001301
1302 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001303 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001304 typename __invoke_return0<type, _A0>::type
1305 operator() (_A0& __a0) const {
1306 return __invoke(__f_, __a0);
1307 }
1308
Eric Fiselierce1813a2015-08-26 20:15:02 +00001309 template <class _A0>
1310 _LIBCPP_INLINE_VISIBILITY
1311 typename __invoke_return0<type, _A0 const>::type
1312 operator() (_A0 const& __a0) const {
1313 return __invoke(__f_, __a0);
1314 }
1315
Eric Fiselier2cc48332015-07-22 22:43:27 +00001316 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001317 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001318 typename __invoke_return1<type, _A0, _A1>::type
1319 operator() (_A0& __a0, _A1& __a1) const {
1320 return __invoke(__f_, __a0, __a1);
1321 }
1322
Eric Fiselierce1813a2015-08-26 20:15:02 +00001323 template <class _A0, class _A1>
1324 _LIBCPP_INLINE_VISIBILITY
1325 typename __invoke_return1<type, _A0 const, _A1>::type
1326 operator() (_A0 const& __a0, _A1& __a1) const {
1327 return __invoke(__f_, __a0, __a1);
1328 }
1329
1330 template <class _A0, class _A1>
1331 _LIBCPP_INLINE_VISIBILITY
1332 typename __invoke_return1<type, _A0, _A1 const>::type
1333 operator() (_A0& __a0, _A1 const& __a1) const {
1334 return __invoke(__f_, __a0, __a1);
1335 }
1336
1337 template <class _A0, class _A1>
1338 _LIBCPP_INLINE_VISIBILITY
1339 typename __invoke_return1<type, _A0 const, _A1 const>::type
1340 operator() (_A0 const& __a0, _A1 const& __a1) const {
1341 return __invoke(__f_, __a0, __a1);
1342 }
1343
Eric Fiselier2cc48332015-07-22 22:43:27 +00001344 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001345 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001346 typename __invoke_return2<type, _A0, _A1, _A2>::type
1347 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
1348 return __invoke(__f_, __a0, __a1, __a2);
1349 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001350
1351 template <class _A0, class _A1, class _A2>
1352 _LIBCPP_INLINE_VISIBILITY
1353 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1354 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
1355 return __invoke(__f_, __a0, __a1, __a2);
1356 }
1357
1358 template <class _A0, class _A1, class _A2>
1359 _LIBCPP_INLINE_VISIBILITY
1360 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1361 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
1362 return __invoke(__f_, __a0, __a1, __a2);
1363 }
1364
1365 template <class _A0, class _A1, class _A2>
1366 _LIBCPP_INLINE_VISIBILITY
1367 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1368 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
1369 return __invoke(__f_, __a0, __a1, __a2);
1370 }
1371
1372 template <class _A0, class _A1, class _A2>
1373 _LIBCPP_INLINE_VISIBILITY
1374 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1375 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
1376 return __invoke(__f_, __a0, __a1, __a2);
1377 }
1378
1379 template <class _A0, class _A1, class _A2>
1380 _LIBCPP_INLINE_VISIBILITY
1381 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1382 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
1383 return __invoke(__f_, __a0, __a1, __a2);
1384 }
1385
1386 template <class _A0, class _A1, class _A2>
1387 _LIBCPP_INLINE_VISIBILITY
1388 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1389 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
1390 return __invoke(__f_, __a0, __a1, __a2);
1391 }
1392
1393 template <class _A0, class _A1, class _A2>
1394 _LIBCPP_INLINE_VISIBILITY
1395 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1396 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
1397 return __invoke(__f_, __a0, __a1, __a2);
1398 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001399#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400};
1401
Howard Hinnantc834c512011-11-29 18:15:50 +00001402template<class _Rp, class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001404__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001405mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406{
Howard Hinnantc834c512011-11-29 18:15:50 +00001407 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408}
1409
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001410////////////////////////////////////////////////////////////////////////////////
1411// FUNCTION
1412//==============================================================================
1413
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414// bad_function_call
1415
Howard Hinnant4ff57432010-09-21 22:55:27 +00001416class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 : public exception
1418{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001419#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1420public:
1421 virtual ~bad_function_call() _NOEXCEPT;
1422
1423 virtual const char* what() const _NOEXCEPT;
1424#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425};
1426
Louis Dionne16fe2952018-07-11 23:14:33 +00001427_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001428void __throw_bad_function_call()
1429{
1430#ifndef _LIBCPP_NO_EXCEPTIONS
1431 throw bad_function_call();
1432#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001433 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001434#endif
1435}
1436
Louis Dionne44d1f812020-03-09 11:16:22 -04001437#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1438# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1439 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1440#else
1441# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1442#endif
1443
1444template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445
1446namespace __function
1447{
1448
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001449template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450struct __maybe_derive_from_unary_function
1451{
1452};
1453
Howard Hinnantc834c512011-11-29 18:15:50 +00001454template<class _Rp, class _A1>
1455struct __maybe_derive_from_unary_function<_Rp(_A1)>
1456 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457{
1458};
1459
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001460template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461struct __maybe_derive_from_binary_function
1462{
1463};
1464
Howard Hinnantc834c512011-11-29 18:15:50 +00001465template<class _Rp, class _A1, class _A2>
1466struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1467 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468{
1469};
1470
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001471template <class _Fp>
1472_LIBCPP_INLINE_VISIBILITY
1473bool __not_null(_Fp const&) { return true; }
1474
1475template <class _Fp>
1476_LIBCPP_INLINE_VISIBILITY
1477bool __not_null(_Fp* __ptr) { return __ptr; }
1478
1479template <class _Ret, class _Class>
1480_LIBCPP_INLINE_VISIBILITY
1481bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1482
1483template <class _Fp>
1484_LIBCPP_INLINE_VISIBILITY
1485bool __not_null(function<_Fp> const& __f) { return !!__f; }
1486
Louis Dionnee2391d72020-04-22 13:58:17 -04001487#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1488template <class _Rp, class ..._Args>
1489_LIBCPP_INLINE_VISIBILITY
1490bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1491#endif
1492
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001493} // namespace __function
1494
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001495#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001496
1497namespace __function {
1498
Eric Fiselier125798e2018-12-10 18:14:09 +00001499// __alloc_func holds a functor and an allocator.
1500
1501template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001502template <class _Fp, class _FB>
1503class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001504
1505template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1506class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1507{
1508 __compressed_pair<_Fp, _Ap> __f_;
1509
1510 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001511 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1512 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001513
1514 _LIBCPP_INLINE_VISIBILITY
1515 const _Target& __target() const { return __f_.first(); }
1516
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001517 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001518 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001519 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001520
1521 _LIBCPP_INLINE_VISIBILITY
1522 explicit __alloc_func(_Target&& __f)
1523 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1524 _VSTD::forward_as_tuple())
1525 {
1526 }
1527
1528 _LIBCPP_INLINE_VISIBILITY
1529 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1530 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1531 _VSTD::forward_as_tuple(__a))
1532 {
1533 }
1534
1535 _LIBCPP_INLINE_VISIBILITY
1536 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1537 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1538 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1539 {
1540 }
1541
1542 _LIBCPP_INLINE_VISIBILITY
1543 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1544 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1545 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1546 {
1547 }
1548
1549 _LIBCPP_INLINE_VISIBILITY
1550 _Rp operator()(_ArgTypes&&... __arg)
1551 {
1552 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1553 return _Invoker::__call(__f_.first(),
1554 _VSTD::forward<_ArgTypes>(__arg)...);
1555 }
1556
1557 _LIBCPP_INLINE_VISIBILITY
1558 __alloc_func* __clone() const
1559 {
1560 typedef allocator_traits<_Alloc> __alloc_traits;
1561 typedef
1562 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1563 _AA;
1564 _AA __a(__f_.second());
1565 typedef __allocator_destructor<_AA> _Dp;
1566 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1567 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1568 return __hold.release();
1569 }
1570
1571 _LIBCPP_INLINE_VISIBILITY
1572 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001573
1574 static void __destroy_and_delete(__alloc_func* __f) {
1575 typedef allocator_traits<_Alloc> __alloc_traits;
1576 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1577 _FunAlloc;
1578 _FunAlloc __a(__f->__get_allocator());
1579 __f->destroy();
1580 __a.deallocate(__f, 1);
1581 }
1582};
1583
1584template <class _Fp, class _Rp, class... _ArgTypes>
1585class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1586 _Fp __f_;
1587
1588public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001589 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001590
1591 _LIBCPP_INLINE_VISIBILITY
1592 const _Target& __target() const { return __f_; }
1593
1594 _LIBCPP_INLINE_VISIBILITY
1595 explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
1596
1597 _LIBCPP_INLINE_VISIBILITY
1598 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1599
1600 _LIBCPP_INLINE_VISIBILITY
1601 _Rp operator()(_ArgTypes&&... __arg) {
1602 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1603 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1604 }
1605
1606 _LIBCPP_INLINE_VISIBILITY
1607 __default_alloc_func* __clone() const {
1608 __builtin_new_allocator::__holder_t __hold =
1609 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1610 __default_alloc_func* __res =
1611 ::new (__hold.get()) __default_alloc_func(__f_);
1612 (void)__hold.release();
1613 return __res;
1614 }
1615
1616 _LIBCPP_INLINE_VISIBILITY
1617 void destroy() _NOEXCEPT { __f_.~_Target(); }
1618
1619 static void __destroy_and_delete(__default_alloc_func* __f) {
1620 __f->destroy();
1621 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1622 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001623};
1624
1625// __base provides an abstract interface for copyable functors.
1626
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001627template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628
Howard Hinnantc834c512011-11-29 18:15:50 +00001629template<class _Rp, class ..._ArgTypes>
1630class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631{
1632 __base(const __base&);
1633 __base& operator=(const __base&);
1634public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001635 _LIBCPP_INLINE_VISIBILITY __base() {}
1636 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637 virtual __base* __clone() const = 0;
1638 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001639 virtual void destroy() _NOEXCEPT = 0;
1640 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001641 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001642#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001643 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1644 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001645#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646};
1647
Eric Fiselier125798e2018-12-10 18:14:09 +00001648// __func implements __base for a given functor type.
1649
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650template<class _FD, class _Alloc, class _FB> class __func;
1651
Howard Hinnantc834c512011-11-29 18:15:50 +00001652template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1653class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1654 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655{
Eric Fiselier125798e2018-12-10 18:14:09 +00001656 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001659 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001660 : __f_(_VSTD::move(__f)) {}
1661
Howard Hinnant4ff57432010-09-21 22:55:27 +00001662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001663 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001664 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001665
1666 _LIBCPP_INLINE_VISIBILITY
1667 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001668 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001669
1670 _LIBCPP_INLINE_VISIBILITY
1671 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001672 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1673
Howard Hinnantc834c512011-11-29 18:15:50 +00001674 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1675 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001676 virtual void destroy() _NOEXCEPT;
1677 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001678 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001679#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001680 virtual const void* target(const type_info&) const _NOEXCEPT;
1681 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001682#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683};
1684
Howard Hinnantc834c512011-11-29 18:15:50 +00001685template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1686__base<_Rp(_ArgTypes...)>*
1687__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001689 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001690 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001691 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001692 typedef __allocator_destructor<_Ap> _Dp;
1693 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001694 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695 return __hold.release();
1696}
1697
Howard Hinnantc834c512011-11-29 18:15:50 +00001698template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699void
Howard Hinnantc834c512011-11-29 18:15:50 +00001700__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701{
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001702 ::new (__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703}
1704
Howard Hinnantc834c512011-11-29 18:15:50 +00001705template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706void
Howard Hinnantc834c512011-11-29 18:15:50 +00001707__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708{
Eric Fiselier125798e2018-12-10 18:14:09 +00001709 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710}
1711
Howard Hinnantc834c512011-11-29 18:15:50 +00001712template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713void
Howard Hinnantc834c512011-11-29 18:15:50 +00001714__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001716 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001717 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001718 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001719 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 __a.deallocate(this, 1);
1721}
1722
Howard Hinnantc834c512011-11-29 18:15:50 +00001723template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1724_Rp
1725__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726{
Eric Fiselier125798e2018-12-10 18:14:09 +00001727 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728}
1729
Howard Hinnant72f73582010-08-11 17:04:31 +00001730#ifndef _LIBCPP_NO_RTTI
1731
Howard Hinnantc834c512011-11-29 18:15:50 +00001732template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001734__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735{
Howard Hinnantc834c512011-11-29 18:15:50 +00001736 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001737 return &__f_.__target();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001738 return nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739}
1740
Howard Hinnantc834c512011-11-29 18:15:50 +00001741template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001743__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744{
Howard Hinnantc834c512011-11-29 18:15:50 +00001745 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746}
1747
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001748#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001749
Eric Fiselier125798e2018-12-10 18:14:09 +00001750// __value_func creates a value-type from a __func.
1751
1752template <class _Fp> class __value_func;
1753
1754template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1755{
1756 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1757
1758 typedef __base<_Rp(_ArgTypes...)> __func;
1759 __func* __f_;
1760
1761 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1762 {
1763 return reinterpret_cast<__func*>(p);
1764 }
1765
1766 public:
1767 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001768 __value_func() _NOEXCEPT : __f_(nullptr) {}
Eric Fiselier125798e2018-12-10 18:14:09 +00001769
1770 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001771 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001772 : __f_(nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001773 {
1774 typedef allocator_traits<_Alloc> __alloc_traits;
1775 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1776 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1777 _FunAlloc;
1778
1779 if (__function::__not_null(__f))
1780 {
1781 _FunAlloc __af(__a);
1782 if (sizeof(_Fun) <= sizeof(__buf_) &&
1783 is_nothrow_copy_constructible<_Fp>::value &&
1784 is_nothrow_copy_constructible<_FunAlloc>::value)
1785 {
1786 __f_ =
1787 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1788 }
1789 else
1790 {
1791 typedef __allocator_destructor<_FunAlloc> _Dp;
1792 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1793 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1794 __f_ = __hold.release();
1795 }
1796 }
1797 }
1798
Eric Fiselier74ebee62019-06-08 01:31:19 +00001799 template <class _Fp,
1800 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1801 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
1802 : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
1803
Eric Fiselier125798e2018-12-10 18:14:09 +00001804 _LIBCPP_INLINE_VISIBILITY
1805 __value_func(const __value_func& __f)
1806 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001807 if (__f.__f_ == nullptr)
1808 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001809 else if ((void*)__f.__f_ == &__f.__buf_)
1810 {
1811 __f_ = __as_base(&__buf_);
1812 __f.__f_->__clone(__f_);
1813 }
1814 else
1815 __f_ = __f.__f_->__clone();
1816 }
1817
1818 _LIBCPP_INLINE_VISIBILITY
1819 __value_func(__value_func&& __f) _NOEXCEPT
1820 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001821 if (__f.__f_ == nullptr)
1822 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001823 else if ((void*)__f.__f_ == &__f.__buf_)
1824 {
1825 __f_ = __as_base(&__buf_);
1826 __f.__f_->__clone(__f_);
1827 }
1828 else
1829 {
1830 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001831 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001832 }
1833 }
1834
1835 _LIBCPP_INLINE_VISIBILITY
1836 ~__value_func()
1837 {
1838 if ((void*)__f_ == &__buf_)
1839 __f_->destroy();
1840 else if (__f_)
1841 __f_->destroy_deallocate();
1842 }
1843
1844 _LIBCPP_INLINE_VISIBILITY
1845 __value_func& operator=(__value_func&& __f)
1846 {
1847 *this = nullptr;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001848 if (__f.__f_ == nullptr)
1849 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001850 else if ((void*)__f.__f_ == &__f.__buf_)
1851 {
1852 __f_ = __as_base(&__buf_);
1853 __f.__f_->__clone(__f_);
1854 }
1855 else
1856 {
1857 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001858 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001859 }
1860 return *this;
1861 }
1862
1863 _LIBCPP_INLINE_VISIBILITY
1864 __value_func& operator=(nullptr_t)
1865 {
1866 __func* __f = __f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001867 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001868 if ((void*)__f == &__buf_)
1869 __f->destroy();
1870 else if (__f)
1871 __f->destroy_deallocate();
1872 return *this;
1873 }
1874
1875 _LIBCPP_INLINE_VISIBILITY
1876 _Rp operator()(_ArgTypes&&... __args) const
1877 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001878 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001879 __throw_bad_function_call();
1880 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1881 }
1882
1883 _LIBCPP_INLINE_VISIBILITY
1884 void swap(__value_func& __f) _NOEXCEPT
1885 {
1886 if (&__f == this)
1887 return;
1888 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1889 {
1890 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1891 __func* __t = __as_base(&__tempbuf);
1892 __f_->__clone(__t);
1893 __f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001894 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001895 __f.__f_->__clone(__as_base(&__buf_));
1896 __f.__f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001897 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001898 __f_ = __as_base(&__buf_);
1899 __t->__clone(__as_base(&__f.__buf_));
1900 __t->destroy();
1901 __f.__f_ = __as_base(&__f.__buf_);
1902 }
1903 else if ((void*)__f_ == &__buf_)
1904 {
1905 __f_->__clone(__as_base(&__f.__buf_));
1906 __f_->destroy();
1907 __f_ = __f.__f_;
1908 __f.__f_ = __as_base(&__f.__buf_);
1909 }
1910 else if ((void*)__f.__f_ == &__f.__buf_)
1911 {
1912 __f.__f_->__clone(__as_base(&__buf_));
1913 __f.__f_->destroy();
1914 __f.__f_ = __f_;
1915 __f_ = __as_base(&__buf_);
1916 }
1917 else
1918 _VSTD::swap(__f_, __f.__f_);
1919 }
1920
1921 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001922 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
Eric Fiselier125798e2018-12-10 18:14:09 +00001923
1924#ifndef _LIBCPP_NO_RTTI
1925 _LIBCPP_INLINE_VISIBILITY
1926 const std::type_info& target_type() const _NOEXCEPT
1927 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001928 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001929 return typeid(void);
1930 return __f_->target_type();
1931 }
1932
1933 template <typename _Tp>
1934 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1935 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001936 if (__f_ == nullptr)
1937 return nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001938 return (const _Tp*)__f_->target(typeid(_Tp));
1939 }
1940#endif // _LIBCPP_NO_RTTI
1941};
1942
Eric Fiselierf2e64362018-12-11 00:14:34 +00001943// Storage for a functor object, to be used with __policy to manage copy and
1944// destruction.
1945union __policy_storage
1946{
1947 mutable char __small[sizeof(void*) * 2];
1948 void* __large;
1949};
1950
1951// True if _Fun can safely be held in __policy_storage.__small.
1952template <typename _Fun>
1953struct __use_small_storage
1954 : public _VSTD::integral_constant<
1955 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001956 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Eric Fiselierf2e64362018-12-11 00:14:34 +00001957 _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1958 _VSTD::is_trivially_destructible<_Fun>::value> {};
1959
1960// Policy contains information about how to copy, destroy, and move the
1961// underlying functor. You can think of it as a vtable of sorts.
1962struct __policy
1963{
1964 // Used to copy or destroy __large values. null for trivial objects.
1965 void* (*const __clone)(const void*);
1966 void (*const __destroy)(void*);
1967
1968 // True if this is the null policy (no value).
1969 const bool __is_null;
1970
1971 // The target type. May be null if RTTI is disabled.
1972 const std::type_info* const __type_info;
1973
1974 // Returns a pointer to a static policy object suitable for the functor
1975 // type.
1976 template <typename _Fun>
1977 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1978 {
1979 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1980 }
1981
1982 _LIBCPP_INLINE_VISIBILITY
1983 static const __policy* __create_empty()
1984 {
1985 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1986 true,
1987#ifndef _LIBCPP_NO_RTTI
1988 &typeid(void)
1989#else
1990 nullptr
1991#endif
1992 };
1993 return &__policy_;
1994 }
1995
1996 private:
1997 template <typename _Fun> static void* __large_clone(const void* __s)
1998 {
1999 const _Fun* __f = static_cast<const _Fun*>(__s);
2000 return __f->__clone();
2001 }
2002
Eric Fiselier74ebee62019-06-08 01:31:19 +00002003 template <typename _Fun>
2004 static void __large_destroy(void* __s) {
2005 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002006 }
2007
2008 template <typename _Fun>
2009 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002010 __choose_policy(/* is_small = */ false_type) {
2011 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2012 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002013#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002014 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002015#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002016 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002017#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002018 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002019 return &__policy_;
2020 }
2021
2022 template <typename _Fun>
2023 _LIBCPP_INLINE_VISIBILITY static const __policy*
2024 __choose_policy(/* is_small = */ true_type)
2025 {
2026 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2027 nullptr, nullptr, false,
2028#ifndef _LIBCPP_NO_RTTI
2029 &typeid(typename _Fun::_Target)
2030#else
2031 nullptr
2032#endif
2033 };
2034 return &__policy_;
2035 }
2036};
2037
2038// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2039// faster for types that can be passed in registers.
2040template <typename _Tp>
2041using __fast_forward =
2042 typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2043
2044// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2045
2046template <class _Fp> struct __policy_invoker;
2047
2048template <class _Rp, class... _ArgTypes>
2049struct __policy_invoker<_Rp(_ArgTypes...)>
2050{
2051 typedef _Rp (*__Call)(const __policy_storage*,
2052 __fast_forward<_ArgTypes>...);
2053
2054 __Call __call_;
2055
2056 // Creates an invoker that throws bad_function_call.
2057 _LIBCPP_INLINE_VISIBILITY
2058 __policy_invoker() : __call_(&__call_empty) {}
2059
2060 // Creates an invoker that calls the given instance of __func.
2061 template <typename _Fun>
2062 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2063 {
2064 return __policy_invoker(&__call_impl<_Fun>);
2065 }
2066
2067 private:
2068 _LIBCPP_INLINE_VISIBILITY
2069 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2070
2071 static _Rp __call_empty(const __policy_storage*,
2072 __fast_forward<_ArgTypes>...)
2073 {
2074 __throw_bad_function_call();
2075 }
2076
2077 template <typename _Fun>
2078 static _Rp __call_impl(const __policy_storage* __buf,
2079 __fast_forward<_ArgTypes>... __args)
2080 {
2081 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2082 ? &__buf->__small
2083 : __buf->__large);
2084 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2085 }
2086};
2087
2088// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2089// copyable functor.
2090
2091template <class _Fp> class __policy_func;
2092
2093template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2094{
2095 // Inline storage for small objects.
2096 __policy_storage __buf_;
2097
2098 // Calls the value stored in __buf_. This could technically be part of
2099 // policy, but storing it here eliminates a level of indirection inside
2100 // operator().
2101 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2102 __invoker __invoker_;
2103
2104 // The policy that describes how to move / copy / destroy __buf_. Never
2105 // null, even if the function is empty.
2106 const __policy* __policy_;
2107
2108 public:
2109 _LIBCPP_INLINE_VISIBILITY
2110 __policy_func() : __policy_(__policy::__create_empty()) {}
2111
2112 template <class _Fp, class _Alloc>
2113 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2114 : __policy_(__policy::__create_empty())
2115 {
2116 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2117 typedef allocator_traits<_Alloc> __alloc_traits;
2118 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2119 _FunAlloc;
2120
2121 if (__function::__not_null(__f))
2122 {
2123 __invoker_ = __invoker::template __create<_Fun>();
2124 __policy_ = __policy::__create<_Fun>();
2125
2126 _FunAlloc __af(__a);
2127 if (__use_small_storage<_Fun>())
2128 {
2129 ::new ((void*)&__buf_.__small)
2130 _Fun(_VSTD::move(__f), _Alloc(__af));
2131 }
2132 else
2133 {
2134 typedef __allocator_destructor<_FunAlloc> _Dp;
2135 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2136 ::new ((void*)__hold.get())
2137 _Fun(_VSTD::move(__f), _Alloc(__af));
2138 __buf_.__large = __hold.release();
2139 }
2140 }
2141 }
2142
Eric Fiselier74ebee62019-06-08 01:31:19 +00002143 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2144 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2145 : __policy_(__policy::__create_empty()) {
2146 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2147
2148 if (__function::__not_null(__f)) {
2149 __invoker_ = __invoker::template __create<_Fun>();
2150 __policy_ = __policy::__create<_Fun>();
2151 if (__use_small_storage<_Fun>()) {
2152 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2153 } else {
2154 __builtin_new_allocator::__holder_t __hold =
2155 __builtin_new_allocator::__allocate_type<_Fun>(1);
2156 __buf_.__large = ::new (__hold.get()) _Fun(_VSTD::move(__f));
2157 (void)__hold.release();
2158 }
2159 }
2160 }
2161
Eric Fiselierf2e64362018-12-11 00:14:34 +00002162 _LIBCPP_INLINE_VISIBILITY
2163 __policy_func(const __policy_func& __f)
2164 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2165 __policy_(__f.__policy_)
2166 {
2167 if (__policy_->__clone)
2168 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2169 }
2170
2171 _LIBCPP_INLINE_VISIBILITY
2172 __policy_func(__policy_func&& __f)
2173 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2174 __policy_(__f.__policy_)
2175 {
2176 if (__policy_->__destroy)
2177 {
2178 __f.__policy_ = __policy::__create_empty();
2179 __f.__invoker_ = __invoker();
2180 }
2181 }
2182
2183 _LIBCPP_INLINE_VISIBILITY
2184 ~__policy_func()
2185 {
2186 if (__policy_->__destroy)
2187 __policy_->__destroy(__buf_.__large);
2188 }
2189
2190 _LIBCPP_INLINE_VISIBILITY
2191 __policy_func& operator=(__policy_func&& __f)
2192 {
2193 *this = nullptr;
2194 __buf_ = __f.__buf_;
2195 __invoker_ = __f.__invoker_;
2196 __policy_ = __f.__policy_;
2197 __f.__policy_ = __policy::__create_empty();
2198 __f.__invoker_ = __invoker();
2199 return *this;
2200 }
2201
2202 _LIBCPP_INLINE_VISIBILITY
2203 __policy_func& operator=(nullptr_t)
2204 {
2205 const __policy* __p = __policy_;
2206 __policy_ = __policy::__create_empty();
2207 __invoker_ = __invoker();
2208 if (__p->__destroy)
2209 __p->__destroy(__buf_.__large);
2210 return *this;
2211 }
2212
2213 _LIBCPP_INLINE_VISIBILITY
2214 _Rp operator()(_ArgTypes&&... __args) const
2215 {
2216 return __invoker_.__call_(_VSTD::addressof(__buf_),
2217 _VSTD::forward<_ArgTypes>(__args)...);
2218 }
2219
2220 _LIBCPP_INLINE_VISIBILITY
2221 void swap(__policy_func& __f)
2222 {
2223 _VSTD::swap(__invoker_, __f.__invoker_);
2224 _VSTD::swap(__policy_, __f.__policy_);
2225 _VSTD::swap(__buf_, __f.__buf_);
2226 }
2227
2228 _LIBCPP_INLINE_VISIBILITY
2229 explicit operator bool() const _NOEXCEPT
2230 {
2231 return !__policy_->__is_null;
2232 }
2233
2234#ifndef _LIBCPP_NO_RTTI
2235 _LIBCPP_INLINE_VISIBILITY
2236 const std::type_info& target_type() const _NOEXCEPT
2237 {
2238 return *__policy_->__type_info;
2239 }
2240
2241 template <typename _Tp>
2242 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2243 {
2244 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2245 return nullptr;
2246 if (__policy_->__clone) // Out of line storage.
2247 return reinterpret_cast<const _Tp*>(__buf_.__large);
2248 else
2249 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2250 }
2251#endif // _LIBCPP_NO_RTTI
2252};
2253
Louis Dionne3a632922020-04-23 16:47:52 -04002254#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
Louis Dionnee2391d72020-04-22 13:58:17 -04002255
Louis Dionne91cf4442020-07-31 12:56:36 -04002256extern "C" void *_Block_copy(const void *);
2257extern "C" void _Block_release(const void *);
2258
Louis Dionnee2391d72020-04-22 13:58:17 -04002259template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2260class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2261 : public __base<_Rp(_ArgTypes...)>
2262{
2263 typedef _Rp1(^__block_type)(_ArgTypes1...);
2264 __block_type __f_;
2265
2266public:
2267 _LIBCPP_INLINE_VISIBILITY
2268 explicit __func(__block_type const& __f)
Louis Dionne91cf4442020-07-31 12:56:36 -04002269 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002270 { }
2271
2272 // [TODO] add && to save on a retain
2273
2274 _LIBCPP_INLINE_VISIBILITY
2275 explicit __func(__block_type __f, const _Alloc& /* unused */)
Louis Dionne91cf4442020-07-31 12:56:36 -04002276 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002277 { }
2278
2279 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2280 _LIBCPP_ASSERT(false,
2281 "Block pointers are just pointers, so they should always fit into "
2282 "std::function's small buffer optimization. This function should "
2283 "never be invoked.");
2284 return nullptr;
2285 }
2286
2287 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
2288 ::new (__p) __func(__f_);
2289 }
2290
2291 virtual void destroy() _NOEXCEPT {
2292 if (__f_)
Louis Dionne91cf4442020-07-31 12:56:36 -04002293 _Block_release(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002294 __f_ = 0;
2295 }
2296
2297 virtual void destroy_deallocate() _NOEXCEPT {
2298 _LIBCPP_ASSERT(false,
2299 "Block pointers are just pointers, so they should always fit into "
2300 "std::function's small buffer optimization. This function should "
2301 "never be invoked.");
2302 }
2303
2304 virtual _Rp operator()(_ArgTypes&& ... __arg) {
2305 return __invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
2306 }
2307
2308#ifndef _LIBCPP_NO_RTTI
2309 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2310 if (__ti == typeid(__func::__block_type))
2311 return &__f_;
2312 return (const void*)nullptr;
2313 }
2314
2315 virtual const std::type_info& target_type() const _NOEXCEPT {
2316 return typeid(__func::__block_type);
2317 }
2318#endif // _LIBCPP_NO_RTTI
2319};
2320
2321#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
2322
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323} // __function
2324
Howard Hinnantc834c512011-11-29 18:15:50 +00002325template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002326class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002327 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2328 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002330#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002331 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002332#else
2333 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2334#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335
Eric Fiselier125798e2018-12-10 18:14:09 +00002336 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002337
Eric Fiselier3906a132019-06-23 20:28:29 +00002338 template <class _Fp, bool = _And<
2339 _IsNotSame<__uncvref_t<_Fp>, function>,
zoecarver3e722f22020-05-19 17:15:28 -07002340 __invokable<_Fp, _ArgTypes...>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002341 >::value>
2342 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002343 template <class _Fp>
2344 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002345 {
Eric Fiselierea080702015-02-10 16:48:45 +00002346 static const bool value = is_same<void, _Rp>::value ||
zoecarver3e722f22020-05-19 17:15:28 -07002347 is_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
Howard Hinnantc834c512011-11-29 18:15:50 +00002348 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002349 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002350 template <class _Fp>
2351 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002352 {
2353 static const bool value = false;
2354 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002355
2356 template <class _Fp>
zoecarver3e722f22020-05-19 17:15:28 -07002357 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002359 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360
Howard Hinnantf06d9262010-08-20 19:36:46 +00002361 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002362 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002363 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002364 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002365 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002367 function(function&&) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002368 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002369 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370
Marshall Clow3148f422016-10-13 21:06:03 +00002371#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002372 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002373 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002374 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002375 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002376 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002377 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002378 template<class _Alloc>
2379 function(allocator_arg_t, const _Alloc&, const function&);
2380 template<class _Alloc>
2381 function(allocator_arg_t, const _Alloc&, function&&);
zoecarver3e722f22020-05-19 17:15:28 -07002382 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002383 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002384#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385
2386 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002387 function& operator=(function&&) _NOEXCEPT;
2388 function& operator=(nullptr_t) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002389 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002390 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391
2392 ~function();
2393
Howard Hinnantf06d9262010-08-20 19:36:46 +00002394 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002395 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002396
2397#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002398 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002400 void assign(_Fp&& __f, const _Alloc& __a)
2401 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002402#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403
Howard Hinnantf06d9262010-08-20 19:36:46 +00002404 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002405 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002406 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2407 return static_cast<bool>(__f_);
2408 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410 // deleted overloads close possible hole in the type system
2411 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002412 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002414 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002416 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002417 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418
Howard Hinnant72f73582010-08-11 17:04:31 +00002419#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002420 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002421 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002422 template <typename _Tp> _Tp* target() _NOEXCEPT;
2423 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002424#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425};
2426
Louis Dionne4af49712019-07-18 19:50:56 +00002427#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2428template<class _Rp, class ..._Ap>
2429function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2430
2431template<class _Fp>
2432struct __strip_signature;
2433
2434template<class _Rp, class _Gp, class ..._Ap>
2435struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2436template<class _Rp, class _Gp, class ..._Ap>
2437struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2438template<class _Rp, class _Gp, class ..._Ap>
2439struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2440template<class _Rp, class _Gp, class ..._Ap>
2441struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2442
2443template<class _Rp, class _Gp, class ..._Ap>
2444struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2445template<class _Rp, class _Gp, class ..._Ap>
2446struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2447template<class _Rp, class _Gp, class ..._Ap>
2448struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2449template<class _Rp, class _Gp, class ..._Ap>
2450struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2451
2452template<class _Rp, class _Gp, class ..._Ap>
2453struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2454template<class _Rp, class _Gp, class ..._Ap>
2455struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2456template<class _Rp, class _Gp, class ..._Ap>
2457struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2458template<class _Rp, class _Gp, class ..._Ap>
2459struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2460
2461template<class _Rp, class _Gp, class ..._Ap>
2462struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2463template<class _Rp, class _Gp, class ..._Ap>
2464struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2465template<class _Rp, class _Gp, class ..._Ap>
2466struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2467template<class _Rp, class _Gp, class ..._Ap>
2468struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2469
2470template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2471function(_Fp) -> function<_Stripped>;
2472#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2473
Howard Hinnantc834c512011-11-29 18:15:50 +00002474template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002475function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476
Marshall Clow3148f422016-10-13 21:06:03 +00002477#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002478template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002479template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002480function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002481 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002482#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002483
Eric Fiselier125798e2018-12-10 18:14:09 +00002484template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002485function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002486 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487
Marshall Clow3148f422016-10-13 21:06:03 +00002488#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002489template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002490template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002491function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002492 function&& __f)
2493 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002494#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002495
Eric Fiselier125798e2018-12-10 18:14:09 +00002496template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002497template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002498function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499
Marshall Clow3148f422016-10-13 21:06:03 +00002500#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002501template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002502template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002503function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2504 _Fp __f)
2505 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002506#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002507
Howard Hinnantc834c512011-11-29 18:15:50 +00002508template<class _Rp, class ..._ArgTypes>
2509function<_Rp(_ArgTypes...)>&
2510function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511{
2512 function(__f).swap(*this);
2513 return *this;
2514}
2515
Howard Hinnantc834c512011-11-29 18:15:50 +00002516template<class _Rp, class ..._ArgTypes>
2517function<_Rp(_ArgTypes...)>&
2518function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519{
Eric Fiselier125798e2018-12-10 18:14:09 +00002520 __f_ = std::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002521 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522}
2523
Howard Hinnantc834c512011-11-29 18:15:50 +00002524template<class _Rp, class ..._ArgTypes>
2525function<_Rp(_ArgTypes...)>&
2526function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527{
Eric Fiselier125798e2018-12-10 18:14:09 +00002528 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002529 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530}
2531
Howard Hinnantc834c512011-11-29 18:15:50 +00002532template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002533template <class _Fp, class>
2534function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002535function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536{
Howard Hinnantc834c512011-11-29 18:15:50 +00002537 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538 return *this;
2539}
2540
Howard Hinnantc834c512011-11-29 18:15:50 +00002541template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002542function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543
Howard Hinnantc834c512011-11-29 18:15:50 +00002544template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545void
Howard Hinnantc834c512011-11-29 18:15:50 +00002546function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547{
Eric Fiselier125798e2018-12-10 18:14:09 +00002548 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549}
2550
Howard Hinnantc834c512011-11-29 18:15:50 +00002551template<class _Rp, class ..._ArgTypes>
2552_Rp
2553function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554{
Eric Fiselier125798e2018-12-10 18:14:09 +00002555 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556}
2557
Howard Hinnant72f73582010-08-11 17:04:31 +00002558#ifndef _LIBCPP_NO_RTTI
2559
Howard Hinnantc834c512011-11-29 18:15:50 +00002560template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002562function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563{
Eric Fiselier125798e2018-12-10 18:14:09 +00002564 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565}
2566
Howard Hinnantc834c512011-11-29 18:15:50 +00002567template<class _Rp, class ..._ArgTypes>
2568template <typename _Tp>
2569_Tp*
2570function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571{
Eric Fiselier125798e2018-12-10 18:14:09 +00002572 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573}
2574
Howard Hinnantc834c512011-11-29 18:15:50 +00002575template<class _Rp, class ..._ArgTypes>
2576template <typename _Tp>
2577const _Tp*
2578function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579{
Eric Fiselier125798e2018-12-10 18:14:09 +00002580 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581}
2582
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002583#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002584
Howard Hinnantc834c512011-11-29 18:15:50 +00002585template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586inline _LIBCPP_INLINE_VISIBILITY
2587bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002588operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589
Howard Hinnantc834c512011-11-29 18:15:50 +00002590template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591inline _LIBCPP_INLINE_VISIBILITY
2592bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002593operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594
Howard Hinnantc834c512011-11-29 18:15:50 +00002595template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596inline _LIBCPP_INLINE_VISIBILITY
2597bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002598operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599
Howard Hinnantc834c512011-11-29 18:15:50 +00002600template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601inline _LIBCPP_INLINE_VISIBILITY
2602bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002603operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604
Howard Hinnantc834c512011-11-29 18:15:50 +00002605template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606inline _LIBCPP_INLINE_VISIBILITY
2607void
Howard Hinnantc834c512011-11-29 18:15:50 +00002608swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609{return __x.swap(__y);}
2610
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002611#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002612
2613#include <__functional_03>
2614
2615#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002616
2617////////////////////////////////////////////////////////////////////////////////
2618// BIND
2619//==============================================================================
2620
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002622template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2624
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002625#if _LIBCPP_STD_VER > 14
2626template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002627_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002628#endif
2629
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002631template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002632 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2633
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002634#if _LIBCPP_STD_VER > 14
2635template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002636_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002637#endif
2638
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639namespace placeholders
2640{
2641
Howard Hinnantc834c512011-11-29 18:15:50 +00002642template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002644#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002645_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2646_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2647_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2648_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2649_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2650_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2651_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2652_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2653_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2654_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2655#else
Marshall Clow396b2132018-01-02 19:01:45 +00002656/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2657/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2658/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2659/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2660/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2661/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2662/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2663/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2664/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2665/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002666#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667
2668} // placeholders
2669
Howard Hinnantc834c512011-11-29 18:15:50 +00002670template<int _Np>
2671struct __is_placeholder<placeholders::__ph<_Np> >
2672 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002674
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002675#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002676
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677template <class _Tp, class _Uj>
2678inline _LIBCPP_INLINE_VISIBILITY
2679_Tp&
2680__mu(reference_wrapper<_Tp> __t, _Uj&)
2681{
2682 return __t.get();
2683}
2684
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685template <class _Ti, class ..._Uj, size_t ..._Indx>
2686inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002687typename __invoke_of<_Ti&, _Uj...>::type
2688__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002690 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691}
2692
2693template <class _Ti, class ..._Uj>
2694inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002695typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696<
2697 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002698 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699>::type
2700__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2701{
2702 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
2703 return __mu_expand(__ti, __uj, __indices());
2704}
2705
2706template <bool IsPh, class _Ti, class _Uj>
2707struct __mu_return2 {};
2708
2709template <class _Ti, class _Uj>
2710struct __mu_return2<true, _Ti, _Uj>
2711{
2712 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2713};
2714
2715template <class _Ti, class _Uj>
2716inline _LIBCPP_INLINE_VISIBILITY
2717typename enable_if
2718<
2719 0 < is_placeholder<_Ti>::value,
2720 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2721>::type
2722__mu(_Ti&, _Uj& __uj)
2723{
2724 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002725 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726}
2727
2728template <class _Ti, class _Uj>
2729inline _LIBCPP_INLINE_VISIBILITY
2730typename enable_if
2731<
2732 !is_bind_expression<_Ti>::value &&
2733 is_placeholder<_Ti>::value == 0 &&
2734 !__is_reference_wrapper<_Ti>::value,
2735 _Ti&
2736>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002737__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738{
2739 return __ti;
2740}
2741
Howard Hinnant0415d792011-05-22 15:07:43 +00002742template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2743 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002744struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002746template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002747struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002748{
2749 typedef __nat type;
2750};
2751
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002753struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002755 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756};
2757
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002758template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002759struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2760 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002761{
2762};
2763
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002765struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766{
2767 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2768 _TupleUj>::type&& type;
2769};
2770
2771template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002772struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002773{
2774 typedef typename _Ti::type& type;
2775};
2776
2777template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002778struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779{
2780 typedef _Ti& type;
2781};
2782
2783template <class _Ti, class _TupleUj>
2784struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002785 : public __mu_return_impl<_Ti,
2786 __is_reference_wrapper<_Ti>::value,
2787 is_bind_expression<_Ti>::value,
2788 0 < is_placeholder<_Ti>::value &&
2789 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2790 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791{
2792};
2793
Howard Hinnantc834c512011-11-29 18:15:50 +00002794template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002795struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002796{
2797 static const bool value = false;
2798};
2799
2800template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002801struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002802{
2803 static const bool value = __invokable<_Fp,
2804 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2805};
2806
2807template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002808struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002809{
2810 static const bool value = __invokable<_Fp,
2811 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2812};
2813
2814template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002815 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816struct __bind_return;
2817
Howard Hinnantc834c512011-11-29 18:15:50 +00002818template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002819struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002821 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002823 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 typename __mu_return
2825 <
2826 _BoundArgs,
2827 _TupleUj
2828 >::type...
2829 >::type type;
2830};
2831
Howard Hinnantc834c512011-11-29 18:15:50 +00002832template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002833struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002835 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002837 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838 typename __mu_return
2839 <
2840 const _BoundArgs,
2841 _TupleUj
2842 >::type...
2843 >::type type;
2844};
2845
Howard Hinnantc834c512011-11-29 18:15:50 +00002846template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002848typename __bind_return<_Fp, _BoundArgs, _Args>::type
2849__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 _Args&& __args)
2851{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002852 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853}
2854
Howard Hinnantc834c512011-11-29 18:15:50 +00002855template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002857 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002859protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002860 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002861 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002862private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002863 _Fd __f_;
2864 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865
2866 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2867public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002868 template <class _Gp, class ..._BA,
2869 class = typename enable_if
2870 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002871 is_constructible<_Fd, _Gp>::value &&
2872 !is_same<typename remove_reference<_Gp>::type,
2873 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002874 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002876 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2877 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002878 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879
2880 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002882 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 operator()(_Args&& ...__args)
2884 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002885 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002886 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887 }
2888
2889 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002891 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 operator()(_Args&& ...__args) const
2893 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002894 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002895 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 }
2897};
2898
Howard Hinnantc834c512011-11-29 18:15:50 +00002899template<class _Fp, class ..._BoundArgs>
2900struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901
Howard Hinnantc834c512011-11-29 18:15:50 +00002902template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002904 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905{
Howard Hinnantc834c512011-11-29 18:15:50 +00002906 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002907 typedef typename base::_Fd _Fd;
2908 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002910 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911
Howard Hinnant7091e652011-07-02 18:22:36 +00002912
Howard Hinnantf292a922013-07-01 00:01:51 +00002913 template <class _Gp, class ..._BA,
2914 class = typename enable_if
2915 <
2916 is_constructible<_Fd, _Gp>::value &&
2917 !is_same<typename remove_reference<_Gp>::type,
2918 __bind_r>::value
2919 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002921 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2922 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002923 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924
2925 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002927 typename enable_if
2928 <
2929 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002930 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002931 result_type
2932 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933 operator()(_Args&& ...__args)
2934 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002935 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2936 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937 }
2938
2939 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002941 typename enable_if
2942 <
2943 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002944 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002945 result_type
2946 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 operator()(_Args&& ...__args) const
2948 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002949 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2950 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951 }
2952};
2953
Howard Hinnantc834c512011-11-29 18:15:50 +00002954template<class _Rp, class _Fp, class ..._BoundArgs>
2955struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956
Howard Hinnantc834c512011-11-29 18:15:50 +00002957template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002959__bind<_Fp, _BoundArgs...>
2960bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961{
Howard Hinnantc834c512011-11-29 18:15:50 +00002962 typedef __bind<_Fp, _BoundArgs...> type;
2963 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964}
2965
Howard Hinnantc834c512011-11-29 18:15:50 +00002966template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002968__bind_r<_Rp, _Fp, _BoundArgs...>
2969bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970{
Howard Hinnantc834c512011-11-29 18:15:50 +00002971 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2972 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973}
2974
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002975#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976
Eric Fiselier0d974f12015-07-14 20:16:15 +00002977#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002978
Eric Fiselier0d974f12015-07-14 20:16:15 +00002979template <class _Fn, class ..._Args>
Louis Dionneaf34f122019-04-03 17:54:37 +00002980invoke_result_t<_Fn, _Args...>
Eric Fiselier934f63b2016-06-02 01:25:41 +00002981invoke(_Fn&& __f, _Args&&... __args)
Louis Dionneaf34f122019-04-03 17:54:37 +00002982 noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
Eric Fiselier934f63b2016-06-02 01:25:41 +00002983{
2984 return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
Eric Fiselier0d974f12015-07-14 20:16:15 +00002985}
Eric Fiselier934f63b2016-06-02 01:25:41 +00002986
2987template <class _DecayFunc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002988class _LIBCPP_TEMPLATE_VIS __not_fn_imp {
Eric Fiselier934f63b2016-06-02 01:25:41 +00002989 _DecayFunc __fd;
2990
2991public:
2992 __not_fn_imp() = delete;
2993
2994 template <class ..._Args>
2995 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00002996 auto operator()(_Args&& ...__args) &
Eric Fiselier934f63b2016-06-02 01:25:41 +00002997 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002998 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2999 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003000
3001 template <class ..._Args>
3002 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00003003 auto operator()(_Args&& ...__args) &&
3004 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003005 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
3006 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00003007
3008 template <class ..._Args>
3009 _LIBCPP_INLINE_VISIBILITY
3010 auto operator()(_Args&& ...__args) const&
Eric Fiselier934f63b2016-06-02 01:25:41 +00003011 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003012 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
3013 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003014
Eric Fiseliere8303a32016-06-27 00:40:41 +00003015
3016 template <class ..._Args>
3017 _LIBCPP_INLINE_VISIBILITY
3018 auto operator()(_Args&& ...__args) const&&
3019 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003020 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
3021 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00003022
Eric Fiselier934f63b2016-06-02 01:25:41 +00003023private:
3024 template <class _RawFunc,
3025 class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
3026 _LIBCPP_INLINE_VISIBILITY
3027 explicit __not_fn_imp(_RawFunc&& __rf)
3028 : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
3029
3030 template <class _RawFunc>
3031 friend inline _LIBCPP_INLINE_VISIBILITY
3032 __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
3033};
3034
3035template <class _RawFunc>
3036inline _LIBCPP_INLINE_VISIBILITY
3037__not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
3038 return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
3039}
3040
Eric Fiselier0d974f12015-07-14 20:16:15 +00003041#endif
3042
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003043// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003044
Marshall Clowa40686b2018-01-08 19:18:00 +00003045template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00003046pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00003047__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3048 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
3049 forward_iterator_tag, forward_iterator_tag)
3050{
3051 if (__first2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003052 return _VSTD::make_pair(__first1, __first1); // Everything matches an empty sequence
Marshall Clowa40686b2018-01-08 19:18:00 +00003053 while (true)
3054 {
3055 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
3056 while (true)
3057 {
3058 if (__first1 == __last1) // return __last1 if no element matches *__first2
Logan Smith4528b5f2020-05-07 11:54:25 -04003059 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003060 if (__pred(*__first1, *__first2))
3061 break;
3062 ++__first1;
3063 }
3064 // *__first1 matches *__first2, now match elements after here
3065 _ForwardIterator1 __m1 = __first1;
3066 _ForwardIterator2 __m2 = __first2;
3067 while (true)
3068 {
3069 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Logan Smith4528b5f2020-05-07 11:54:25 -04003070 return _VSTD::make_pair(__first1, __m1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003071 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Logan Smith4528b5f2020-05-07 11:54:25 -04003072 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003073 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
3074 {
3075 ++__first1;
3076 break;
3077 } // else there is a match, check next elements
3078 }
3079 }
3080}
3081
3082template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
3083_LIBCPP_CONSTEXPR_AFTER_CXX11
3084pair<_RandomAccessIterator1, _RandomAccessIterator1>
3085__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
3086 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
3087 random_access_iterator_tag, random_access_iterator_tag)
3088{
3089 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
3090 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
3091 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
3092 const _D2 __len2 = __last2 - __first2;
3093 if (__len2 == 0)
Logan Smith4528b5f2020-05-07 11:54:25 -04003094 return _VSTD::make_pair(__first1, __first1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003095 const _D1 __len1 = __last1 - __first1;
3096 if (__len1 < __len2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003097 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003098 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
3099
3100 while (true)
3101 {
3102 while (true)
3103 {
3104 if (__first1 == __s)
Logan Smith4528b5f2020-05-07 11:54:25 -04003105 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003106 if (__pred(*__first1, *__first2))
3107 break;
3108 ++__first1;
3109 }
3110
3111 _RandomAccessIterator1 __m1 = __first1;
3112 _RandomAccessIterator2 __m2 = __first2;
3113 while (true)
3114 {
3115 if (++__m2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003116 return _VSTD::make_pair(__first1, __first1 + __len2);
Marshall Clowa40686b2018-01-08 19:18:00 +00003117 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
3118 if (!__pred(*__m1, *__m2))
3119 {
3120 ++__first1;
3121 break;
3122 }
3123 }
3124 }
3125}
3126
3127#if _LIBCPP_STD_VER > 14
3128
3129// default searcher
3130template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Dimitry Andricfd3633d2018-02-13 17:40:59 +00003131class _LIBCPP_TYPE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00003132public:
3133 _LIBCPP_INLINE_VISIBILITY
Louis Dionne44bcff92018-08-03 22:36:53 +00003134 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00003135 _BinaryPredicate __p = _BinaryPredicate())
3136 : __first_(__f), __last_(__l), __pred_(__p) {}
3137
3138 template <typename _ForwardIterator2>
3139 _LIBCPP_INLINE_VISIBILITY
3140 pair<_ForwardIterator2, _ForwardIterator2>
3141 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3142 {
3143 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
3144 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
3145 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
3146 }
3147
3148private:
3149 _ForwardIterator __first_;
3150 _ForwardIterator __last_;
3151 _BinaryPredicate __pred_;
Eric Fiselier80663c52019-08-04 07:13:43 +00003152 };
Marshall Clowa40686b2018-01-08 19:18:00 +00003153
3154#endif // _LIBCPP_STD_VER > 14
3155
Louis Dionnedb1892a2018-12-03 14:03:27 +00003156#if _LIBCPP_STD_VER > 17
3157template <class _Tp>
3158using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3159
3160template <class _Tp>
3161using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3162#endif // > C++17
3163
Marshall Clow29b53f22018-12-14 18:49:35 +00003164template <class _Container, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003165inline typename _Container::size_type
3166__libcpp_erase_if_container(_Container& __c, _Predicate __pred) {
3167 typename _Container::size_type __old_size = __c.size();
3168
3169 const typename _Container::iterator __last = __c.end();
3170 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
3171 if (__pred(*__iter))
3172 __iter = __c.erase(__iter);
3173 else
3174 ++__iter;
3175 }
3176
3177 return __old_size - __c.size();
Marshall Clow29b53f22018-12-14 18:49:35 +00003178}
3179
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180_LIBCPP_END_NAMESPACE_STD
3181
3182#endif // _LIBCPP_FUNCTIONAL