blob: f03ba8bb554159f542f9ca020e743ff3c8bc467c [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
Louis Dionnee2391d72020-04-22 13:58:17 -0400511#if defined(_LIBCPP_HAS_EXTENSION_BLOCKS) && !defined(_LIBCPP_HAS_OBJC_ARC)
512#include <Block.h>
513#endif
514
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000515#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000517#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518
519_LIBCPP_BEGIN_NAMESPACE_STD
520
Marshall Clow974bae22013-07-29 14:21:53 +0000521#if _LIBCPP_STD_VER > 11
522template <class _Tp = void>
523#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000525#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000526struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527{
Marshall Clowd18ee652013-09-28 19:06:12 +0000528 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
529 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530 {return __x + __y;}
531};
532
Marshall Clow974bae22013-07-29 14:21:53 +0000533#if _LIBCPP_STD_VER > 11
534template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000535struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000536{
537 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000538 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
539 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000540 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
541 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
542 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000543 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000544};
545#endif
546
547
548#if _LIBCPP_STD_VER > 11
549template <class _Tp = void>
550#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000552#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000553struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554{
Marshall Clowd18ee652013-09-28 19:06:12 +0000555 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
556 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557 {return __x - __y;}
558};
559
Marshall Clow974bae22013-07-29 14:21:53 +0000560#if _LIBCPP_STD_VER > 11
561template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000562struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000563{
564 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000565 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
566 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000567 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
568 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
569 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000570 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000571};
572#endif
573
574
575#if _LIBCPP_STD_VER > 11
576template <class _Tp = void>
577#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000579#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000580struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581{
Marshall Clowd18ee652013-09-28 19:06:12 +0000582 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
583 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 {return __x * __y;}
585};
586
Marshall Clow974bae22013-07-29 14:21:53 +0000587#if _LIBCPP_STD_VER > 11
588template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000589struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000590{
591 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000592 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
593 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000594 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
595 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
596 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000597 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000598};
599#endif
600
601
602#if _LIBCPP_STD_VER > 11
603template <class _Tp = void>
604#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000606#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000607struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608{
Marshall Clowd18ee652013-09-28 19:06:12 +0000609 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
610 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611 {return __x / __y;}
612};
613
Marshall Clow974bae22013-07-29 14:21:53 +0000614#if _LIBCPP_STD_VER > 11
615template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000616struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000617{
618 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000619 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
620 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000621 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
622 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
623 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000624 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000625};
626#endif
627
628
629#if _LIBCPP_STD_VER > 11
630template <class _Tp = void>
631#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000633#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000634struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635{
Marshall Clowd18ee652013-09-28 19:06:12 +0000636 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
637 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638 {return __x % __y;}
639};
640
Marshall Clow974bae22013-07-29 14:21:53 +0000641#if _LIBCPP_STD_VER > 11
642template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000643struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000644{
645 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000646 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
647 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000648 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
649 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
650 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000651 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000652};
653#endif
654
655
656#if _LIBCPP_STD_VER > 11
657template <class _Tp = void>
658#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000660#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000661struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662{
Marshall Clowd18ee652013-09-28 19:06:12 +0000663 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
664 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665 {return -__x;}
666};
667
Marshall Clow974bae22013-07-29 14:21:53 +0000668#if _LIBCPP_STD_VER > 11
669template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000670struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000671{
672 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000673 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
674 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000675 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
676 -> decltype (- _VSTD::forward<_Tp>(__x))
677 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000678 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000679};
680#endif
681
682
683#if _LIBCPP_STD_VER > 11
684template <class _Tp = void>
685#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000687#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000688struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689{
Marshall Clowd18ee652013-09-28 19:06:12 +0000690 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
691 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 {return __x == __y;}
693};
694
Marshall Clow974bae22013-07-29 14:21:53 +0000695#if _LIBCPP_STD_VER > 11
696template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000697struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000698{
Marshall Clowd18ee652013-09-28 19:06:12 +0000699 template <class _T1, class _T2>
700 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000701 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000702 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
703 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
704 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000705 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000706};
707#endif
708
709
710#if _LIBCPP_STD_VER > 11
711template <class _Tp = void>
712#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000714#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000715struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716{
Marshall Clowd18ee652013-09-28 19:06:12 +0000717 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
718 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719 {return __x != __y;}
720};
721
Marshall Clow974bae22013-07-29 14:21:53 +0000722#if _LIBCPP_STD_VER > 11
723template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000724struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000725{
Marshall Clowd18ee652013-09-28 19:06:12 +0000726 template <class _T1, class _T2>
727 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000728 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000729 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
730 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
731 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000732 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000733};
734#endif
735
736
737#if _LIBCPP_STD_VER > 11
738template <class _Tp = void>
739#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000741#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000742struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743{
Marshall Clowd18ee652013-09-28 19:06:12 +0000744 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
745 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 {return __x > __y;}
747};
748
Marshall Clow974bae22013-07-29 14:21:53 +0000749#if _LIBCPP_STD_VER > 11
750template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000751struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000752{
Marshall Clowd18ee652013-09-28 19:06:12 +0000753 template <class _T1, class _T2>
754 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000755 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000756 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
757 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
758 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000759 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000760};
761#endif
762
763
Howard Hinnantb17caf92012-02-21 21:02:58 +0000764// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765
Marshall Clow974bae22013-07-29 14:21:53 +0000766#if _LIBCPP_STD_VER > 11
767template <class _Tp = void>
768#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000770#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000771struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772{
Marshall Clowd18ee652013-09-28 19:06:12 +0000773 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
774 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 {return __x >= __y;}
776};
777
Marshall Clow974bae22013-07-29 14:21:53 +0000778#if _LIBCPP_STD_VER > 11
779template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000780struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000781{
Marshall Clowd18ee652013-09-28 19:06:12 +0000782 template <class _T1, class _T2>
783 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000784 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000785 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
786 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
787 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000788 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000789};
790#endif
791
792
793#if _LIBCPP_STD_VER > 11
794template <class _Tp = void>
795#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000797#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000798struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799{
Marshall Clowd18ee652013-09-28 19:06:12 +0000800 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
801 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802 {return __x <= __y;}
803};
804
Marshall Clow974bae22013-07-29 14:21:53 +0000805#if _LIBCPP_STD_VER > 11
806template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000807struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000808{
Marshall Clowd18ee652013-09-28 19:06:12 +0000809 template <class _T1, class _T2>
810 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000811 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000812 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
813 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
814 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000815 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000816};
817#endif
818
819
820#if _LIBCPP_STD_VER > 11
821template <class _Tp = void>
822#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000824#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000825struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826{
Marshall Clowd18ee652013-09-28 19:06:12 +0000827 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
828 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829 {return __x && __y;}
830};
831
Marshall Clow974bae22013-07-29 14:21:53 +0000832#if _LIBCPP_STD_VER > 11
833template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000834struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000835{
Marshall Clowd18ee652013-09-28 19:06:12 +0000836 template <class _T1, class _T2>
837 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000838 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000839 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
840 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
841 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000842 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000843};
844#endif
845
846
847#if _LIBCPP_STD_VER > 11
848template <class _Tp = void>
849#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000851#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000852struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853{
Marshall Clowd18ee652013-09-28 19:06:12 +0000854 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
855 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 {return __x || __y;}
857};
858
Marshall Clow974bae22013-07-29 14:21:53 +0000859#if _LIBCPP_STD_VER > 11
860template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000861struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000862{
Marshall Clowd18ee652013-09-28 19:06:12 +0000863 template <class _T1, class _T2>
864 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000865 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000866 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
867 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
868 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000869 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000870};
871#endif
872
873
874#if _LIBCPP_STD_VER > 11
875template <class _Tp = void>
876#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000878#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000879struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880{
Marshall Clowd18ee652013-09-28 19:06:12 +0000881 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
882 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 {return !__x;}
884};
885
Marshall Clow974bae22013-07-29 14:21:53 +0000886#if _LIBCPP_STD_VER > 11
887template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000888struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000889{
890 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000891 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
892 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000893 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
894 -> decltype (!_VSTD::forward<_Tp>(__x))
895 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000896 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000897};
898#endif
899
900
901#if _LIBCPP_STD_VER > 11
902template <class _Tp = void>
903#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000905#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000906struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907{
Marshall Clowd18ee652013-09-28 19:06:12 +0000908 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
909 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 {return __x & __y;}
911};
912
Marshall Clow974bae22013-07-29 14:21:53 +0000913#if _LIBCPP_STD_VER > 11
914template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000915struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000916{
Marshall Clowd18ee652013-09-28 19:06:12 +0000917 template <class _T1, class _T2>
918 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000919 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000920 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
921 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
922 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000923 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000924};
925#endif
926
927
928#if _LIBCPP_STD_VER > 11
929template <class _Tp = void>
930#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000932#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000933struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934{
Marshall Clowd18ee652013-09-28 19:06:12 +0000935 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
936 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937 {return __x | __y;}
938};
939
Marshall Clow974bae22013-07-29 14:21:53 +0000940#if _LIBCPP_STD_VER > 11
941template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000942struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000943{
Marshall Clowd18ee652013-09-28 19:06:12 +0000944 template <class _T1, class _T2>
945 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000946 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000947 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
948 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
949 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000950 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000951};
952#endif
953
954
955#if _LIBCPP_STD_VER > 11
956template <class _Tp = void>
957#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000959#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000960struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961{
Marshall Clowd18ee652013-09-28 19:06:12 +0000962 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
963 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 {return __x ^ __y;}
965};
966
Marshall Clow974bae22013-07-29 14:21:53 +0000967#if _LIBCPP_STD_VER > 11
968template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000969struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000970{
Marshall Clowd18ee652013-09-28 19:06:12 +0000971 template <class _T1, class _T2>
972 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000973 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000974 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
975 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
976 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000977 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000978};
979#endif
980
981
982#if _LIBCPP_STD_VER > 11
983template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000984struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000985{
Marshall Clowd18ee652013-09-28 19:06:12 +0000986 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
987 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000988 {return ~__x;}
989};
990
991template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000992struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000993{
994 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000995 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
996 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000997 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
998 -> decltype (~_VSTD::forward<_Tp>(__x))
999 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +00001000 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +00001001};
1002#endif
1003
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001005class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 : public unary_function<typename _Predicate::argument_type, bool>
1007{
1008 _Predicate __pred_;
1009public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001010 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1011 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001013 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1014 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 {return !__pred_(__x);}
1016};
1017
1018template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001019_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020unary_negate<_Predicate>
1021not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1022
1023template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001024class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 : public binary_function<typename _Predicate::first_argument_type,
1026 typename _Predicate::second_argument_type,
1027 bool>
1028{
1029 _Predicate __pred_;
1030public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001031 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001032 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1033
1034 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1035 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 const typename _Predicate::second_argument_type& __y) const
1037 {return !__pred_(__x, __y);}
1038};
1039
1040template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001041_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042binary_negate<_Predicate>
1043not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1044
Marshall Clow26a027c2017-04-13 18:25:32 +00001045#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001047class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 : public unary_function<typename __Operation::second_argument_type,
1049 typename __Operation::result_type>
1050{
1051protected:
1052 __Operation op;
1053 typename __Operation::first_argument_type value;
1054public:
1055 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1056 const typename __Operation::first_argument_type __y)
1057 : op(__x), value(__y) {}
1058 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1059 (typename __Operation::second_argument_type& __x) const
1060 {return op(value, __x);}
1061 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1062 (const typename __Operation::second_argument_type& __x) const
1063 {return op(value, __x);}
1064};
1065
1066template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001067_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068binder1st<__Operation>
1069bind1st(const __Operation& __op, const _Tp& __x)
1070 {return binder1st<__Operation>(__op, __x);}
1071
1072template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001073class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 : public unary_function<typename __Operation::first_argument_type,
1075 typename __Operation::result_type>
1076{
1077protected:
1078 __Operation op;
1079 typename __Operation::second_argument_type value;
1080public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1083 : op(__x), value(__y) {}
1084 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1085 ( typename __Operation::first_argument_type& __x) const
1086 {return op(__x, value);}
1087 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1088 (const typename __Operation::first_argument_type& __x) const
1089 {return op(__x, value);}
1090};
1091
1092template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001093_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094binder2nd<__Operation>
1095bind2nd(const __Operation& __op, const _Tp& __x)
1096 {return binder2nd<__Operation>(__op, __x);}
1097
1098template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001099class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001100 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101{
1102 _Result (*__f_)(_Arg);
1103public:
1104 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1105 : __f_(__f) {}
1106 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1107 {return __f_(__x);}
1108};
1109
1110template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001111_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112pointer_to_unary_function<_Arg,_Result>
1113ptr_fun(_Result (*__f)(_Arg))
1114 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1115
1116template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001117class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001118 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119{
1120 _Result (*__f_)(_Arg1, _Arg2);
1121public:
1122 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1123 : __f_(__f) {}
1124 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1125 {return __f_(__x, __y);}
1126};
1127
1128template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001129_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130pointer_to_binary_function<_Arg1,_Arg2,_Result>
1131ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1132 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1133
1134template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001135class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1136 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137{
1138 _Sp (_Tp::*__p_)();
1139public:
1140 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1141 : __p_(__p) {}
1142 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1143 {return (__p->*__p_)();}
1144};
1145
1146template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001147class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1148 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149{
1150 _Sp (_Tp::*__p_)(_Ap);
1151public:
1152 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1153 : __p_(__p) {}
1154 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1155 {return (__p->*__p_)(__x);}
1156};
1157
1158template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001159_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160mem_fun_t<_Sp,_Tp>
1161mem_fun(_Sp (_Tp::*__f)())
1162 {return mem_fun_t<_Sp,_Tp>(__f);}
1163
1164template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001165_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166mem_fun1_t<_Sp,_Tp,_Ap>
1167mem_fun(_Sp (_Tp::*__f)(_Ap))
1168 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1169
1170template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001171class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1172 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173{
1174 _Sp (_Tp::*__p_)();
1175public:
1176 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1177 : __p_(__p) {}
1178 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1179 {return (__p.*__p_)();}
1180};
1181
1182template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001183class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1184 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185{
1186 _Sp (_Tp::*__p_)(_Ap);
1187public:
1188 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1189 : __p_(__p) {}
1190 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1191 {return (__p.*__p_)(__x);}
1192};
1193
1194template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001195_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196mem_fun_ref_t<_Sp,_Tp>
1197mem_fun_ref(_Sp (_Tp::*__f)())
1198 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1199
1200template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001201_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202mem_fun1_ref_t<_Sp,_Tp,_Ap>
1203mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1204 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1205
1206template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001207class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1208 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209{
1210 _Sp (_Tp::*__p_)() const;
1211public:
1212 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1213 : __p_(__p) {}
1214 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1215 {return (__p->*__p_)();}
1216};
1217
1218template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001219class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1220 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221{
1222 _Sp (_Tp::*__p_)(_Ap) const;
1223public:
1224 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1225 : __p_(__p) {}
1226 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1227 {return (__p->*__p_)(__x);}
1228};
1229
1230template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001231_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232const_mem_fun_t<_Sp,_Tp>
1233mem_fun(_Sp (_Tp::*__f)() const)
1234 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1235
1236template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001237_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238const_mem_fun1_t<_Sp,_Tp,_Ap>
1239mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1240 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1241
1242template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001243class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1244 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245{
1246 _Sp (_Tp::*__p_)() const;
1247public:
1248 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1249 : __p_(__p) {}
1250 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1251 {return (__p.*__p_)();}
1252};
1253
1254template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001255class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001256 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257{
1258 _Sp (_Tp::*__p_)(_Ap) const;
1259public:
1260 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1261 : __p_(__p) {}
1262 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1263 {return (__p.*__p_)(__x);}
1264};
1265
1266template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001267_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268const_mem_fun_ref_t<_Sp,_Tp>
1269mem_fun_ref(_Sp (_Tp::*__f)() const)
1270 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1271
1272template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001273_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1275mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1276 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001277#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001279////////////////////////////////////////////////////////////////////////////////
1280// MEMFUN
1281//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283template <class _Tp>
1284class __mem_fn
1285 : public __weak_result_type<_Tp>
1286{
1287public:
1288 // types
1289 typedef _Tp type;
1290private:
1291 type __f_;
1292
1293public:
Marshall Clowad8ff212015-10-25 20:12:16 +00001294 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001296#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297 // invoke
1298 template <class... _ArgTypes>
Eric Fiselier2cc48332015-07-22 22:43:27 +00001299 _LIBCPP_INLINE_VISIBILITY
1300 typename __invoke_return<type, _ArgTypes...>::type
1301 operator() (_ArgTypes&&... __args) const {
1302 return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
1303 }
1304#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001305
1306 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001307 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001308 typename __invoke_return0<type, _A0>::type
1309 operator() (_A0& __a0) const {
1310 return __invoke(__f_, __a0);
1311 }
1312
Eric Fiselierce1813a2015-08-26 20:15:02 +00001313 template <class _A0>
1314 _LIBCPP_INLINE_VISIBILITY
1315 typename __invoke_return0<type, _A0 const>::type
1316 operator() (_A0 const& __a0) const {
1317 return __invoke(__f_, __a0);
1318 }
1319
Eric Fiselier2cc48332015-07-22 22:43:27 +00001320 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001321 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001322 typename __invoke_return1<type, _A0, _A1>::type
1323 operator() (_A0& __a0, _A1& __a1) const {
1324 return __invoke(__f_, __a0, __a1);
1325 }
1326
Eric Fiselierce1813a2015-08-26 20:15:02 +00001327 template <class _A0, class _A1>
1328 _LIBCPP_INLINE_VISIBILITY
1329 typename __invoke_return1<type, _A0 const, _A1>::type
1330 operator() (_A0 const& __a0, _A1& __a1) const {
1331 return __invoke(__f_, __a0, __a1);
1332 }
1333
1334 template <class _A0, class _A1>
1335 _LIBCPP_INLINE_VISIBILITY
1336 typename __invoke_return1<type, _A0, _A1 const>::type
1337 operator() (_A0& __a0, _A1 const& __a1) const {
1338 return __invoke(__f_, __a0, __a1);
1339 }
1340
1341 template <class _A0, class _A1>
1342 _LIBCPP_INLINE_VISIBILITY
1343 typename __invoke_return1<type, _A0 const, _A1 const>::type
1344 operator() (_A0 const& __a0, _A1 const& __a1) const {
1345 return __invoke(__f_, __a0, __a1);
1346 }
1347
Eric Fiselier2cc48332015-07-22 22:43:27 +00001348 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001349 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001350 typename __invoke_return2<type, _A0, _A1, _A2>::type
1351 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
1352 return __invoke(__f_, __a0, __a1, __a2);
1353 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001354
1355 template <class _A0, class _A1, class _A2>
1356 _LIBCPP_INLINE_VISIBILITY
1357 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1358 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
1359 return __invoke(__f_, __a0, __a1, __a2);
1360 }
1361
1362 template <class _A0, class _A1, class _A2>
1363 _LIBCPP_INLINE_VISIBILITY
1364 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1365 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
1366 return __invoke(__f_, __a0, __a1, __a2);
1367 }
1368
1369 template <class _A0, class _A1, class _A2>
1370 _LIBCPP_INLINE_VISIBILITY
1371 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1372 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
1373 return __invoke(__f_, __a0, __a1, __a2);
1374 }
1375
1376 template <class _A0, class _A1, class _A2>
1377 _LIBCPP_INLINE_VISIBILITY
1378 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1379 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
1380 return __invoke(__f_, __a0, __a1, __a2);
1381 }
1382
1383 template <class _A0, class _A1, class _A2>
1384 _LIBCPP_INLINE_VISIBILITY
1385 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1386 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
1387 return __invoke(__f_, __a0, __a1, __a2);
1388 }
1389
1390 template <class _A0, class _A1, class _A2>
1391 _LIBCPP_INLINE_VISIBILITY
1392 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1393 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
1394 return __invoke(__f_, __a0, __a1, __a2);
1395 }
1396
1397 template <class _A0, class _A1, class _A2>
1398 _LIBCPP_INLINE_VISIBILITY
1399 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1400 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
1401 return __invoke(__f_, __a0, __a1, __a2);
1402 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001403#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404};
1405
Howard Hinnantc834c512011-11-29 18:15:50 +00001406template<class _Rp, class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001408__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001409mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410{
Howard Hinnantc834c512011-11-29 18:15:50 +00001411 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412}
1413
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001414////////////////////////////////////////////////////////////////////////////////
1415// FUNCTION
1416//==============================================================================
1417
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418// bad_function_call
1419
Howard Hinnant4ff57432010-09-21 22:55:27 +00001420class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 : public exception
1422{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001423#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1424public:
1425 virtual ~bad_function_call() _NOEXCEPT;
1426
1427 virtual const char* what() const _NOEXCEPT;
1428#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429};
1430
Louis Dionne16fe2952018-07-11 23:14:33 +00001431_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001432void __throw_bad_function_call()
1433{
1434#ifndef _LIBCPP_NO_EXCEPTIONS
1435 throw bad_function_call();
1436#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001437 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001438#endif
1439}
1440
Louis Dionne44d1f812020-03-09 11:16:22 -04001441#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1442# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1443 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1444#else
1445# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1446#endif
1447
1448template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449
1450namespace __function
1451{
1452
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001453template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454struct __maybe_derive_from_unary_function
1455{
1456};
1457
Howard Hinnantc834c512011-11-29 18:15:50 +00001458template<class _Rp, class _A1>
1459struct __maybe_derive_from_unary_function<_Rp(_A1)>
1460 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461{
1462};
1463
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001464template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465struct __maybe_derive_from_binary_function
1466{
1467};
1468
Howard Hinnantc834c512011-11-29 18:15:50 +00001469template<class _Rp, class _A1, class _A2>
1470struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1471 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472{
1473};
1474
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001475template <class _Fp>
1476_LIBCPP_INLINE_VISIBILITY
1477bool __not_null(_Fp const&) { return true; }
1478
1479template <class _Fp>
1480_LIBCPP_INLINE_VISIBILITY
1481bool __not_null(_Fp* __ptr) { return __ptr; }
1482
1483template <class _Ret, class _Class>
1484_LIBCPP_INLINE_VISIBILITY
1485bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1486
1487template <class _Fp>
1488_LIBCPP_INLINE_VISIBILITY
1489bool __not_null(function<_Fp> const& __f) { return !!__f; }
1490
Louis Dionnee2391d72020-04-22 13:58:17 -04001491#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1492template <class _Rp, class ..._Args>
1493_LIBCPP_INLINE_VISIBILITY
1494bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1495#endif
1496
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001497} // namespace __function
1498
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001499#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001500
1501namespace __function {
1502
Eric Fiselier125798e2018-12-10 18:14:09 +00001503// __alloc_func holds a functor and an allocator.
1504
1505template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001506template <class _Fp, class _FB>
1507class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001508
1509template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1510class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1511{
1512 __compressed_pair<_Fp, _Ap> __f_;
1513
1514 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001515 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1516 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001517
1518 _LIBCPP_INLINE_VISIBILITY
1519 const _Target& __target() const { return __f_.first(); }
1520
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001521 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001522 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001523 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001524
1525 _LIBCPP_INLINE_VISIBILITY
1526 explicit __alloc_func(_Target&& __f)
1527 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1528 _VSTD::forward_as_tuple())
1529 {
1530 }
1531
1532 _LIBCPP_INLINE_VISIBILITY
1533 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1534 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1535 _VSTD::forward_as_tuple(__a))
1536 {
1537 }
1538
1539 _LIBCPP_INLINE_VISIBILITY
1540 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1541 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1542 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1543 {
1544 }
1545
1546 _LIBCPP_INLINE_VISIBILITY
1547 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1548 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1549 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1550 {
1551 }
1552
1553 _LIBCPP_INLINE_VISIBILITY
1554 _Rp operator()(_ArgTypes&&... __arg)
1555 {
1556 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1557 return _Invoker::__call(__f_.first(),
1558 _VSTD::forward<_ArgTypes>(__arg)...);
1559 }
1560
1561 _LIBCPP_INLINE_VISIBILITY
1562 __alloc_func* __clone() const
1563 {
1564 typedef allocator_traits<_Alloc> __alloc_traits;
1565 typedef
1566 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1567 _AA;
1568 _AA __a(__f_.second());
1569 typedef __allocator_destructor<_AA> _Dp;
1570 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1571 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1572 return __hold.release();
1573 }
1574
1575 _LIBCPP_INLINE_VISIBILITY
1576 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001577
1578 static void __destroy_and_delete(__alloc_func* __f) {
1579 typedef allocator_traits<_Alloc> __alloc_traits;
1580 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1581 _FunAlloc;
1582 _FunAlloc __a(__f->__get_allocator());
1583 __f->destroy();
1584 __a.deallocate(__f, 1);
1585 }
1586};
1587
1588template <class _Fp, class _Rp, class... _ArgTypes>
1589class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1590 _Fp __f_;
1591
1592public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001593 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001594
1595 _LIBCPP_INLINE_VISIBILITY
1596 const _Target& __target() const { return __f_; }
1597
1598 _LIBCPP_INLINE_VISIBILITY
1599 explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
1600
1601 _LIBCPP_INLINE_VISIBILITY
1602 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1603
1604 _LIBCPP_INLINE_VISIBILITY
1605 _Rp operator()(_ArgTypes&&... __arg) {
1606 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1607 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1608 }
1609
1610 _LIBCPP_INLINE_VISIBILITY
1611 __default_alloc_func* __clone() const {
1612 __builtin_new_allocator::__holder_t __hold =
1613 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1614 __default_alloc_func* __res =
1615 ::new (__hold.get()) __default_alloc_func(__f_);
1616 (void)__hold.release();
1617 return __res;
1618 }
1619
1620 _LIBCPP_INLINE_VISIBILITY
1621 void destroy() _NOEXCEPT { __f_.~_Target(); }
1622
1623 static void __destroy_and_delete(__default_alloc_func* __f) {
1624 __f->destroy();
1625 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1626 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001627};
1628
1629// __base provides an abstract interface for copyable functors.
1630
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001631template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632
Howard Hinnantc834c512011-11-29 18:15:50 +00001633template<class _Rp, class ..._ArgTypes>
1634class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635{
1636 __base(const __base&);
1637 __base& operator=(const __base&);
1638public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001639 _LIBCPP_INLINE_VISIBILITY __base() {}
1640 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641 virtual __base* __clone() const = 0;
1642 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001643 virtual void destroy() _NOEXCEPT = 0;
1644 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001645 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001646#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001647 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1648 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001649#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650};
1651
Eric Fiselier125798e2018-12-10 18:14:09 +00001652// __func implements __base for a given functor type.
1653
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654template<class _FD, class _Alloc, class _FB> class __func;
1655
Howard Hinnantc834c512011-11-29 18:15:50 +00001656template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1657class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1658 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659{
Eric Fiselier125798e2018-12-10 18:14:09 +00001660 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001663 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001664 : __f_(_VSTD::move(__f)) {}
1665
Howard Hinnant4ff57432010-09-21 22:55:27 +00001666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001667 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001668 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001669
1670 _LIBCPP_INLINE_VISIBILITY
1671 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001672 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001673
1674 _LIBCPP_INLINE_VISIBILITY
1675 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001676 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1677
Howard Hinnantc834c512011-11-29 18:15:50 +00001678 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1679 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001680 virtual void destroy() _NOEXCEPT;
1681 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001682 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001683#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001684 virtual const void* target(const type_info&) const _NOEXCEPT;
1685 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001686#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687};
1688
Howard Hinnantc834c512011-11-29 18:15:50 +00001689template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1690__base<_Rp(_ArgTypes...)>*
1691__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001693 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001694 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001695 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001696 typedef __allocator_destructor<_Ap> _Dp;
1697 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001698 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699 return __hold.release();
1700}
1701
Howard Hinnantc834c512011-11-29 18:15:50 +00001702template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703void
Howard Hinnantc834c512011-11-29 18:15:50 +00001704__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705{
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001706 ::new (__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707}
1708
Howard Hinnantc834c512011-11-29 18:15:50 +00001709template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710void
Howard Hinnantc834c512011-11-29 18:15:50 +00001711__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712{
Eric Fiselier125798e2018-12-10 18:14:09 +00001713 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714}
1715
Howard Hinnantc834c512011-11-29 18:15:50 +00001716template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717void
Howard Hinnantc834c512011-11-29 18:15:50 +00001718__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001720 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001721 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001722 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001723 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 __a.deallocate(this, 1);
1725}
1726
Howard Hinnantc834c512011-11-29 18:15:50 +00001727template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1728_Rp
1729__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730{
Eric Fiselier125798e2018-12-10 18:14:09 +00001731 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732}
1733
Howard Hinnant72f73582010-08-11 17:04:31 +00001734#ifndef _LIBCPP_NO_RTTI
1735
Howard Hinnantc834c512011-11-29 18:15:50 +00001736template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001738__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739{
Howard Hinnantc834c512011-11-29 18:15:50 +00001740 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001741 return &__f_.__target();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742 return (const void*)0;
1743}
1744
Howard Hinnantc834c512011-11-29 18:15:50 +00001745template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001747__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748{
Howard Hinnantc834c512011-11-29 18:15:50 +00001749 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750}
1751
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001752#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001753
Eric Fiselier125798e2018-12-10 18:14:09 +00001754// __value_func creates a value-type from a __func.
1755
1756template <class _Fp> class __value_func;
1757
1758template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1759{
1760 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1761
1762 typedef __base<_Rp(_ArgTypes...)> __func;
1763 __func* __f_;
1764
1765 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1766 {
1767 return reinterpret_cast<__func*>(p);
1768 }
1769
1770 public:
1771 _LIBCPP_INLINE_VISIBILITY
1772 __value_func() _NOEXCEPT : __f_(0) {}
1773
1774 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001775 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001776 : __f_(0)
1777 {
1778 typedef allocator_traits<_Alloc> __alloc_traits;
1779 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1780 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1781 _FunAlloc;
1782
1783 if (__function::__not_null(__f))
1784 {
1785 _FunAlloc __af(__a);
1786 if (sizeof(_Fun) <= sizeof(__buf_) &&
1787 is_nothrow_copy_constructible<_Fp>::value &&
1788 is_nothrow_copy_constructible<_FunAlloc>::value)
1789 {
1790 __f_ =
1791 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1792 }
1793 else
1794 {
1795 typedef __allocator_destructor<_FunAlloc> _Dp;
1796 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1797 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1798 __f_ = __hold.release();
1799 }
1800 }
1801 }
1802
Eric Fiselier74ebee62019-06-08 01:31:19 +00001803 template <class _Fp,
1804 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1805 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
1806 : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
1807
Eric Fiselier125798e2018-12-10 18:14:09 +00001808 _LIBCPP_INLINE_VISIBILITY
1809 __value_func(const __value_func& __f)
1810 {
1811 if (__f.__f_ == 0)
1812 __f_ = 0;
1813 else if ((void*)__f.__f_ == &__f.__buf_)
1814 {
1815 __f_ = __as_base(&__buf_);
1816 __f.__f_->__clone(__f_);
1817 }
1818 else
1819 __f_ = __f.__f_->__clone();
1820 }
1821
1822 _LIBCPP_INLINE_VISIBILITY
1823 __value_func(__value_func&& __f) _NOEXCEPT
1824 {
1825 if (__f.__f_ == 0)
1826 __f_ = 0;
1827 else if ((void*)__f.__f_ == &__f.__buf_)
1828 {
1829 __f_ = __as_base(&__buf_);
1830 __f.__f_->__clone(__f_);
1831 }
1832 else
1833 {
1834 __f_ = __f.__f_;
1835 __f.__f_ = 0;
1836 }
1837 }
1838
1839 _LIBCPP_INLINE_VISIBILITY
1840 ~__value_func()
1841 {
1842 if ((void*)__f_ == &__buf_)
1843 __f_->destroy();
1844 else if (__f_)
1845 __f_->destroy_deallocate();
1846 }
1847
1848 _LIBCPP_INLINE_VISIBILITY
1849 __value_func& operator=(__value_func&& __f)
1850 {
1851 *this = nullptr;
1852 if (__f.__f_ == 0)
1853 __f_ = 0;
1854 else if ((void*)__f.__f_ == &__f.__buf_)
1855 {
1856 __f_ = __as_base(&__buf_);
1857 __f.__f_->__clone(__f_);
1858 }
1859 else
1860 {
1861 __f_ = __f.__f_;
1862 __f.__f_ = 0;
1863 }
1864 return *this;
1865 }
1866
1867 _LIBCPP_INLINE_VISIBILITY
1868 __value_func& operator=(nullptr_t)
1869 {
1870 __func* __f = __f_;
1871 __f_ = 0;
1872 if ((void*)__f == &__buf_)
1873 __f->destroy();
1874 else if (__f)
1875 __f->destroy_deallocate();
1876 return *this;
1877 }
1878
1879 _LIBCPP_INLINE_VISIBILITY
1880 _Rp operator()(_ArgTypes&&... __args) const
1881 {
1882 if (__f_ == 0)
1883 __throw_bad_function_call();
1884 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1885 }
1886
1887 _LIBCPP_INLINE_VISIBILITY
1888 void swap(__value_func& __f) _NOEXCEPT
1889 {
1890 if (&__f == this)
1891 return;
1892 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1893 {
1894 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1895 __func* __t = __as_base(&__tempbuf);
1896 __f_->__clone(__t);
1897 __f_->destroy();
1898 __f_ = 0;
1899 __f.__f_->__clone(__as_base(&__buf_));
1900 __f.__f_->destroy();
1901 __f.__f_ = 0;
1902 __f_ = __as_base(&__buf_);
1903 __t->__clone(__as_base(&__f.__buf_));
1904 __t->destroy();
1905 __f.__f_ = __as_base(&__f.__buf_);
1906 }
1907 else if ((void*)__f_ == &__buf_)
1908 {
1909 __f_->__clone(__as_base(&__f.__buf_));
1910 __f_->destroy();
1911 __f_ = __f.__f_;
1912 __f.__f_ = __as_base(&__f.__buf_);
1913 }
1914 else if ((void*)__f.__f_ == &__f.__buf_)
1915 {
1916 __f.__f_->__clone(__as_base(&__buf_));
1917 __f.__f_->destroy();
1918 __f.__f_ = __f_;
1919 __f_ = __as_base(&__buf_);
1920 }
1921 else
1922 _VSTD::swap(__f_, __f.__f_);
1923 }
1924
1925 _LIBCPP_INLINE_VISIBILITY
1926 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != 0; }
1927
1928#ifndef _LIBCPP_NO_RTTI
1929 _LIBCPP_INLINE_VISIBILITY
1930 const std::type_info& target_type() const _NOEXCEPT
1931 {
1932 if (__f_ == 0)
1933 return typeid(void);
1934 return __f_->target_type();
1935 }
1936
1937 template <typename _Tp>
1938 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1939 {
1940 if (__f_ == 0)
1941 return 0;
1942 return (const _Tp*)__f_->target(typeid(_Tp));
1943 }
1944#endif // _LIBCPP_NO_RTTI
1945};
1946
Eric Fiselierf2e64362018-12-11 00:14:34 +00001947// Storage for a functor object, to be used with __policy to manage copy and
1948// destruction.
1949union __policy_storage
1950{
1951 mutable char __small[sizeof(void*) * 2];
1952 void* __large;
1953};
1954
1955// True if _Fun can safely be held in __policy_storage.__small.
1956template <typename _Fun>
1957struct __use_small_storage
1958 : public _VSTD::integral_constant<
1959 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001960 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Eric Fiselierf2e64362018-12-11 00:14:34 +00001961 _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1962 _VSTD::is_trivially_destructible<_Fun>::value> {};
1963
1964// Policy contains information about how to copy, destroy, and move the
1965// underlying functor. You can think of it as a vtable of sorts.
1966struct __policy
1967{
1968 // Used to copy or destroy __large values. null for trivial objects.
1969 void* (*const __clone)(const void*);
1970 void (*const __destroy)(void*);
1971
1972 // True if this is the null policy (no value).
1973 const bool __is_null;
1974
1975 // The target type. May be null if RTTI is disabled.
1976 const std::type_info* const __type_info;
1977
1978 // Returns a pointer to a static policy object suitable for the functor
1979 // type.
1980 template <typename _Fun>
1981 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1982 {
1983 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1984 }
1985
1986 _LIBCPP_INLINE_VISIBILITY
1987 static const __policy* __create_empty()
1988 {
1989 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1990 true,
1991#ifndef _LIBCPP_NO_RTTI
1992 &typeid(void)
1993#else
1994 nullptr
1995#endif
1996 };
1997 return &__policy_;
1998 }
1999
2000 private:
2001 template <typename _Fun> static void* __large_clone(const void* __s)
2002 {
2003 const _Fun* __f = static_cast<const _Fun*>(__s);
2004 return __f->__clone();
2005 }
2006
Eric Fiselier74ebee62019-06-08 01:31:19 +00002007 template <typename _Fun>
2008 static void __large_destroy(void* __s) {
2009 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002010 }
2011
2012 template <typename _Fun>
2013 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002014 __choose_policy(/* is_small = */ false_type) {
2015 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2016 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002017#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002018 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002019#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002020 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002021#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002022 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002023 return &__policy_;
2024 }
2025
2026 template <typename _Fun>
2027 _LIBCPP_INLINE_VISIBILITY static const __policy*
2028 __choose_policy(/* is_small = */ true_type)
2029 {
2030 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2031 nullptr, nullptr, false,
2032#ifndef _LIBCPP_NO_RTTI
2033 &typeid(typename _Fun::_Target)
2034#else
2035 nullptr
2036#endif
2037 };
2038 return &__policy_;
2039 }
2040};
2041
2042// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2043// faster for types that can be passed in registers.
2044template <typename _Tp>
2045using __fast_forward =
2046 typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2047
2048// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2049
2050template <class _Fp> struct __policy_invoker;
2051
2052template <class _Rp, class... _ArgTypes>
2053struct __policy_invoker<_Rp(_ArgTypes...)>
2054{
2055 typedef _Rp (*__Call)(const __policy_storage*,
2056 __fast_forward<_ArgTypes>...);
2057
2058 __Call __call_;
2059
2060 // Creates an invoker that throws bad_function_call.
2061 _LIBCPP_INLINE_VISIBILITY
2062 __policy_invoker() : __call_(&__call_empty) {}
2063
2064 // Creates an invoker that calls the given instance of __func.
2065 template <typename _Fun>
2066 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2067 {
2068 return __policy_invoker(&__call_impl<_Fun>);
2069 }
2070
2071 private:
2072 _LIBCPP_INLINE_VISIBILITY
2073 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2074
2075 static _Rp __call_empty(const __policy_storage*,
2076 __fast_forward<_ArgTypes>...)
2077 {
2078 __throw_bad_function_call();
2079 }
2080
2081 template <typename _Fun>
2082 static _Rp __call_impl(const __policy_storage* __buf,
2083 __fast_forward<_ArgTypes>... __args)
2084 {
2085 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2086 ? &__buf->__small
2087 : __buf->__large);
2088 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2089 }
2090};
2091
2092// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2093// copyable functor.
2094
2095template <class _Fp> class __policy_func;
2096
2097template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2098{
2099 // Inline storage for small objects.
2100 __policy_storage __buf_;
2101
2102 // Calls the value stored in __buf_. This could technically be part of
2103 // policy, but storing it here eliminates a level of indirection inside
2104 // operator().
2105 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2106 __invoker __invoker_;
2107
2108 // The policy that describes how to move / copy / destroy __buf_. Never
2109 // null, even if the function is empty.
2110 const __policy* __policy_;
2111
2112 public:
2113 _LIBCPP_INLINE_VISIBILITY
2114 __policy_func() : __policy_(__policy::__create_empty()) {}
2115
2116 template <class _Fp, class _Alloc>
2117 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2118 : __policy_(__policy::__create_empty())
2119 {
2120 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2121 typedef allocator_traits<_Alloc> __alloc_traits;
2122 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2123 _FunAlloc;
2124
2125 if (__function::__not_null(__f))
2126 {
2127 __invoker_ = __invoker::template __create<_Fun>();
2128 __policy_ = __policy::__create<_Fun>();
2129
2130 _FunAlloc __af(__a);
2131 if (__use_small_storage<_Fun>())
2132 {
2133 ::new ((void*)&__buf_.__small)
2134 _Fun(_VSTD::move(__f), _Alloc(__af));
2135 }
2136 else
2137 {
2138 typedef __allocator_destructor<_FunAlloc> _Dp;
2139 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2140 ::new ((void*)__hold.get())
2141 _Fun(_VSTD::move(__f), _Alloc(__af));
2142 __buf_.__large = __hold.release();
2143 }
2144 }
2145 }
2146
Eric Fiselier74ebee62019-06-08 01:31:19 +00002147 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2148 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2149 : __policy_(__policy::__create_empty()) {
2150 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2151
2152 if (__function::__not_null(__f)) {
2153 __invoker_ = __invoker::template __create<_Fun>();
2154 __policy_ = __policy::__create<_Fun>();
2155 if (__use_small_storage<_Fun>()) {
2156 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2157 } else {
2158 __builtin_new_allocator::__holder_t __hold =
2159 __builtin_new_allocator::__allocate_type<_Fun>(1);
2160 __buf_.__large = ::new (__hold.get()) _Fun(_VSTD::move(__f));
2161 (void)__hold.release();
2162 }
2163 }
2164 }
2165
Eric Fiselierf2e64362018-12-11 00:14:34 +00002166 _LIBCPP_INLINE_VISIBILITY
2167 __policy_func(const __policy_func& __f)
2168 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2169 __policy_(__f.__policy_)
2170 {
2171 if (__policy_->__clone)
2172 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2173 }
2174
2175 _LIBCPP_INLINE_VISIBILITY
2176 __policy_func(__policy_func&& __f)
2177 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2178 __policy_(__f.__policy_)
2179 {
2180 if (__policy_->__destroy)
2181 {
2182 __f.__policy_ = __policy::__create_empty();
2183 __f.__invoker_ = __invoker();
2184 }
2185 }
2186
2187 _LIBCPP_INLINE_VISIBILITY
2188 ~__policy_func()
2189 {
2190 if (__policy_->__destroy)
2191 __policy_->__destroy(__buf_.__large);
2192 }
2193
2194 _LIBCPP_INLINE_VISIBILITY
2195 __policy_func& operator=(__policy_func&& __f)
2196 {
2197 *this = nullptr;
2198 __buf_ = __f.__buf_;
2199 __invoker_ = __f.__invoker_;
2200 __policy_ = __f.__policy_;
2201 __f.__policy_ = __policy::__create_empty();
2202 __f.__invoker_ = __invoker();
2203 return *this;
2204 }
2205
2206 _LIBCPP_INLINE_VISIBILITY
2207 __policy_func& operator=(nullptr_t)
2208 {
2209 const __policy* __p = __policy_;
2210 __policy_ = __policy::__create_empty();
2211 __invoker_ = __invoker();
2212 if (__p->__destroy)
2213 __p->__destroy(__buf_.__large);
2214 return *this;
2215 }
2216
2217 _LIBCPP_INLINE_VISIBILITY
2218 _Rp operator()(_ArgTypes&&... __args) const
2219 {
2220 return __invoker_.__call_(_VSTD::addressof(__buf_),
2221 _VSTD::forward<_ArgTypes>(__args)...);
2222 }
2223
2224 _LIBCPP_INLINE_VISIBILITY
2225 void swap(__policy_func& __f)
2226 {
2227 _VSTD::swap(__invoker_, __f.__invoker_);
2228 _VSTD::swap(__policy_, __f.__policy_);
2229 _VSTD::swap(__buf_, __f.__buf_);
2230 }
2231
2232 _LIBCPP_INLINE_VISIBILITY
2233 explicit operator bool() const _NOEXCEPT
2234 {
2235 return !__policy_->__is_null;
2236 }
2237
2238#ifndef _LIBCPP_NO_RTTI
2239 _LIBCPP_INLINE_VISIBILITY
2240 const std::type_info& target_type() const _NOEXCEPT
2241 {
2242 return *__policy_->__type_info;
2243 }
2244
2245 template <typename _Tp>
2246 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2247 {
2248 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2249 return nullptr;
2250 if (__policy_->__clone) // Out of line storage.
2251 return reinterpret_cast<const _Tp*>(__buf_.__large);
2252 else
2253 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2254 }
2255#endif // _LIBCPP_NO_RTTI
2256};
2257
Louis Dionnee2391d72020-04-22 13:58:17 -04002258#if defined(_LIBCPP_HAS_EXTENSION_BLOCKS) && !defined(_LIBCPP_HAS_OBJC_ARC)
2259
2260template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2261class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2262 : public __base<_Rp(_ArgTypes...)>
2263{
2264 typedef _Rp1(^__block_type)(_ArgTypes1...);
2265 __block_type __f_;
2266
2267public:
2268 _LIBCPP_INLINE_VISIBILITY
2269 explicit __func(__block_type const& __f)
2270 : __f_(__f ? Block_copy(__f) : (__block_type)0)
2271 { }
2272
2273 // [TODO] add && to save on a retain
2274
2275 _LIBCPP_INLINE_VISIBILITY
2276 explicit __func(__block_type __f, const _Alloc& /* unused */)
2277 : __f_(__f ? Block_copy(__f) : (__block_type)0)
2278 { }
2279
2280 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2281 _LIBCPP_ASSERT(false,
2282 "Block pointers are just pointers, so they should always fit into "
2283 "std::function's small buffer optimization. This function should "
2284 "never be invoked.");
2285 return nullptr;
2286 }
2287
2288 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
2289 ::new (__p) __func(__f_);
2290 }
2291
2292 virtual void destroy() _NOEXCEPT {
2293 if (__f_)
2294 Block_release(__f_);
2295 __f_ = 0;
2296 }
2297
2298 virtual void destroy_deallocate() _NOEXCEPT {
2299 _LIBCPP_ASSERT(false,
2300 "Block pointers are just pointers, so they should always fit into "
2301 "std::function's small buffer optimization. This function should "
2302 "never be invoked.");
2303 }
2304
2305 virtual _Rp operator()(_ArgTypes&& ... __arg) {
2306 return __invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
2307 }
2308
2309#ifndef _LIBCPP_NO_RTTI
2310 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2311 if (__ti == typeid(__func::__block_type))
2312 return &__f_;
2313 return (const void*)nullptr;
2314 }
2315
2316 virtual const std::type_info& target_type() const _NOEXCEPT {
2317 return typeid(__func::__block_type);
2318 }
2319#endif // _LIBCPP_NO_RTTI
2320};
2321
2322#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
2323
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324} // __function
2325
Howard Hinnantc834c512011-11-29 18:15:50 +00002326template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002327class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002328 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2329 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002331#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002332 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002333#else
2334 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2335#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336
Eric Fiselier125798e2018-12-10 18:14:09 +00002337 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002338
Eric Fiselier3906a132019-06-23 20:28:29 +00002339 template <class _Fp, bool = _And<
2340 _IsNotSame<__uncvref_t<_Fp>, function>,
Eric Fiselier43c04f72017-09-10 23:41:20 +00002341 __invokable<_Fp&, _ArgTypes...>
2342 >::value>
2343 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002344 template <class _Fp>
2345 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002346 {
Eric Fiselierea080702015-02-10 16:48:45 +00002347 static const bool value = is_same<void, _Rp>::value ||
Howard Hinnantc834c512011-11-29 18:15:50 +00002348 is_convertible<typename __invoke_of<_Fp&, _ArgTypes...>::type,
2349 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002350 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002351 template <class _Fp>
2352 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002353 {
2354 static const bool value = false;
2355 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002356
2357 template <class _Fp>
2358 using _EnableIfCallable = typename enable_if<__callable<_Fp>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002360 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002361
Howard Hinnantf06d9262010-08-20 19:36:46 +00002362 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002363 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002364 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002365 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002366 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002368 function(function&&) _NOEXCEPT;
Eric Fiselier43c04f72017-09-10 23:41:20 +00002369 template<class _Fp, class = _EnableIfCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002370 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371
Marshall Clow3148f422016-10-13 21:06:03 +00002372#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002373 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002374 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002375 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002376 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002377 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002378 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002379 template<class _Alloc>
2380 function(allocator_arg_t, const _Alloc&, const function&);
2381 template<class _Alloc>
2382 function(allocator_arg_t, const _Alloc&, function&&);
Eric Fiselier43c04f72017-09-10 23:41:20 +00002383 template<class _Fp, class _Alloc, class = _EnableIfCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002384 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002385#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386
2387 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002388 function& operator=(function&&) _NOEXCEPT;
2389 function& operator=(nullptr_t) _NOEXCEPT;
Eric Fiselier43c04f72017-09-10 23:41:20 +00002390 template<class _Fp, class = _EnableIfCallable<_Fp>>
2391 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392
2393 ~function();
2394
Howard Hinnantf06d9262010-08-20 19:36:46 +00002395 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002396 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002397
2398#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002399 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002401 void assign(_Fp&& __f, const _Alloc& __a)
2402 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002403#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404
Howard Hinnantf06d9262010-08-20 19:36:46 +00002405 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002406 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002407 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2408 return static_cast<bool>(__f_);
2409 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411 // deleted overloads close possible hole in the type system
2412 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002413 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002415 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002417 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002418 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419
Howard Hinnant72f73582010-08-11 17:04:31 +00002420#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002421 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002422 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002423 template <typename _Tp> _Tp* target() _NOEXCEPT;
2424 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002425#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426};
2427
Louis Dionne4af49712019-07-18 19:50:56 +00002428#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2429template<class _Rp, class ..._Ap>
2430function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2431
2432template<class _Fp>
2433struct __strip_signature;
2434
2435template<class _Rp, class _Gp, class ..._Ap>
2436struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2437template<class _Rp, class _Gp, class ..._Ap>
2438struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2439template<class _Rp, class _Gp, class ..._Ap>
2440struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2441template<class _Rp, class _Gp, class ..._Ap>
2442struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2443
2444template<class _Rp, class _Gp, class ..._Ap>
2445struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2446template<class _Rp, class _Gp, class ..._Ap>
2447struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2448template<class _Rp, class _Gp, class ..._Ap>
2449struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2450template<class _Rp, class _Gp, class ..._Ap>
2451struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2452
2453template<class _Rp, class _Gp, class ..._Ap>
2454struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2455template<class _Rp, class _Gp, class ..._Ap>
2456struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2457template<class _Rp, class _Gp, class ..._Ap>
2458struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2459template<class _Rp, class _Gp, class ..._Ap>
2460struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2461
2462template<class _Rp, class _Gp, class ..._Ap>
2463struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2464template<class _Rp, class _Gp, class ..._Ap>
2465struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2466template<class _Rp, class _Gp, class ..._Ap>
2467struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2468template<class _Rp, class _Gp, class ..._Ap>
2469struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2470
2471template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2472function(_Fp) -> function<_Stripped>;
2473#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2474
Howard Hinnantc834c512011-11-29 18:15:50 +00002475template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002476function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477
Marshall Clow3148f422016-10-13 21:06:03 +00002478#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002479template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002480template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002481function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002482 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002483#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002484
Eric Fiselier125798e2018-12-10 18:14:09 +00002485template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002486function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002487 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488
Marshall Clow3148f422016-10-13 21:06:03 +00002489#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002490template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002491template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002492function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002493 function&& __f)
2494 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002495#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002496
Eric Fiselier125798e2018-12-10 18:14:09 +00002497template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002498template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002499function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500
Marshall Clow3148f422016-10-13 21:06:03 +00002501#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002502template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002503template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002504function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2505 _Fp __f)
2506 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002507#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002508
Howard Hinnantc834c512011-11-29 18:15:50 +00002509template<class _Rp, class ..._ArgTypes>
2510function<_Rp(_ArgTypes...)>&
2511function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512{
2513 function(__f).swap(*this);
2514 return *this;
2515}
2516
Howard Hinnantc834c512011-11-29 18:15:50 +00002517template<class _Rp, class ..._ArgTypes>
2518function<_Rp(_ArgTypes...)>&
2519function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520{
Eric Fiselier125798e2018-12-10 18:14:09 +00002521 __f_ = std::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002522 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523}
2524
Howard Hinnantc834c512011-11-29 18:15:50 +00002525template<class _Rp, class ..._ArgTypes>
2526function<_Rp(_ArgTypes...)>&
2527function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528{
Eric Fiselier125798e2018-12-10 18:14:09 +00002529 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002530 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531}
2532
Howard Hinnantc834c512011-11-29 18:15:50 +00002533template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002534template <class _Fp, class>
2535function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002536function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537{
Howard Hinnantc834c512011-11-29 18:15:50 +00002538 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539 return *this;
2540}
2541
Howard Hinnantc834c512011-11-29 18:15:50 +00002542template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002543function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544
Howard Hinnantc834c512011-11-29 18:15:50 +00002545template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546void
Howard Hinnantc834c512011-11-29 18:15:50 +00002547function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548{
Eric Fiselier125798e2018-12-10 18:14:09 +00002549 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550}
2551
Howard Hinnantc834c512011-11-29 18:15:50 +00002552template<class _Rp, class ..._ArgTypes>
2553_Rp
2554function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555{
Eric Fiselier125798e2018-12-10 18:14:09 +00002556 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557}
2558
Howard Hinnant72f73582010-08-11 17:04:31 +00002559#ifndef _LIBCPP_NO_RTTI
2560
Howard Hinnantc834c512011-11-29 18:15:50 +00002561template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002563function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564{
Eric Fiselier125798e2018-12-10 18:14:09 +00002565 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566}
2567
Howard Hinnantc834c512011-11-29 18:15:50 +00002568template<class _Rp, class ..._ArgTypes>
2569template <typename _Tp>
2570_Tp*
2571function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572{
Eric Fiselier125798e2018-12-10 18:14:09 +00002573 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574}
2575
Howard Hinnantc834c512011-11-29 18:15:50 +00002576template<class _Rp, class ..._ArgTypes>
2577template <typename _Tp>
2578const _Tp*
2579function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580{
Eric Fiselier125798e2018-12-10 18:14:09 +00002581 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582}
2583
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002584#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002585
Howard Hinnantc834c512011-11-29 18:15:50 +00002586template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587inline _LIBCPP_INLINE_VISIBILITY
2588bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002589operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590
Howard Hinnantc834c512011-11-29 18:15:50 +00002591template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592inline _LIBCPP_INLINE_VISIBILITY
2593bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002594operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595
Howard Hinnantc834c512011-11-29 18:15:50 +00002596template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597inline _LIBCPP_INLINE_VISIBILITY
2598bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002599operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600
Howard Hinnantc834c512011-11-29 18:15:50 +00002601template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602inline _LIBCPP_INLINE_VISIBILITY
2603bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002604operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605
Howard Hinnantc834c512011-11-29 18:15:50 +00002606template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607inline _LIBCPP_INLINE_VISIBILITY
2608void
Howard Hinnantc834c512011-11-29 18:15:50 +00002609swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610{return __x.swap(__y);}
2611
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002612#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002613
2614#include <__functional_03>
2615
2616#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002617
2618////////////////////////////////////////////////////////////////////////////////
2619// BIND
2620//==============================================================================
2621
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002623template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2625
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002626#if _LIBCPP_STD_VER > 14
2627template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002628_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002629#endif
2630
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002632template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2634
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002635#if _LIBCPP_STD_VER > 14
2636template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002637_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002638#endif
2639
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640namespace placeholders
2641{
2642
Howard Hinnantc834c512011-11-29 18:15:50 +00002643template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002645#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002646_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2647_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2648_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2649_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2650_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2651_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2652_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2653_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2654_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2655_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2656#else
Marshall Clow396b2132018-01-02 19:01:45 +00002657/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2658/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2659/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2660/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2661/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2662/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2663/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2664/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2665/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2666/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002667#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668
2669} // placeholders
2670
Howard Hinnantc834c512011-11-29 18:15:50 +00002671template<int _Np>
2672struct __is_placeholder<placeholders::__ph<_Np> >
2673 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002675
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002676#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002677
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678template <class _Tp, class _Uj>
2679inline _LIBCPP_INLINE_VISIBILITY
2680_Tp&
2681__mu(reference_wrapper<_Tp> __t, _Uj&)
2682{
2683 return __t.get();
2684}
2685
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686template <class _Ti, class ..._Uj, size_t ..._Indx>
2687inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002688typename __invoke_of<_Ti&, _Uj...>::type
2689__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002691 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692}
2693
2694template <class _Ti, class ..._Uj>
2695inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002696typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697<
2698 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002699 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700>::type
2701__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2702{
2703 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
2704 return __mu_expand(__ti, __uj, __indices());
2705}
2706
2707template <bool IsPh, class _Ti, class _Uj>
2708struct __mu_return2 {};
2709
2710template <class _Ti, class _Uj>
2711struct __mu_return2<true, _Ti, _Uj>
2712{
2713 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2714};
2715
2716template <class _Ti, class _Uj>
2717inline _LIBCPP_INLINE_VISIBILITY
2718typename enable_if
2719<
2720 0 < is_placeholder<_Ti>::value,
2721 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2722>::type
2723__mu(_Ti&, _Uj& __uj)
2724{
2725 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002726 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727}
2728
2729template <class _Ti, class _Uj>
2730inline _LIBCPP_INLINE_VISIBILITY
2731typename enable_if
2732<
2733 !is_bind_expression<_Ti>::value &&
2734 is_placeholder<_Ti>::value == 0 &&
2735 !__is_reference_wrapper<_Ti>::value,
2736 _Ti&
2737>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002738__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739{
2740 return __ti;
2741}
2742
Howard Hinnant0415d792011-05-22 15:07:43 +00002743template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2744 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002745struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002747template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002748struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002749{
2750 typedef __nat type;
2751};
2752
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002754struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002756 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757};
2758
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002759template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002760struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2761 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002762{
2763};
2764
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002766struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767{
2768 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2769 _TupleUj>::type&& type;
2770};
2771
2772template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002773struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002774{
2775 typedef typename _Ti::type& type;
2776};
2777
2778template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002779struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780{
2781 typedef _Ti& type;
2782};
2783
2784template <class _Ti, class _TupleUj>
2785struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002786 : public __mu_return_impl<_Ti,
2787 __is_reference_wrapper<_Ti>::value,
2788 is_bind_expression<_Ti>::value,
2789 0 < is_placeholder<_Ti>::value &&
2790 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2791 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792{
2793};
2794
Howard Hinnantc834c512011-11-29 18:15:50 +00002795template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002796struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002797{
2798 static const bool value = false;
2799};
2800
2801template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002802struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002803{
2804 static const bool value = __invokable<_Fp,
2805 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2806};
2807
2808template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002809struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002810{
2811 static const bool value = __invokable<_Fp,
2812 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2813};
2814
2815template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002816 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817struct __bind_return;
2818
Howard Hinnantc834c512011-11-29 18:15:50 +00002819template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002820struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002822 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002824 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825 typename __mu_return
2826 <
2827 _BoundArgs,
2828 _TupleUj
2829 >::type...
2830 >::type type;
2831};
2832
Howard Hinnantc834c512011-11-29 18:15:50 +00002833template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002834struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002836 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002838 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839 typename __mu_return
2840 <
2841 const _BoundArgs,
2842 _TupleUj
2843 >::type...
2844 >::type type;
2845};
2846
Howard Hinnantc834c512011-11-29 18:15:50 +00002847template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002849typename __bind_return<_Fp, _BoundArgs, _Args>::type
2850__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851 _Args&& __args)
2852{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002853 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854}
2855
Howard Hinnantc834c512011-11-29 18:15:50 +00002856template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002858 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002860protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002861 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002862 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002863private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002864 _Fd __f_;
2865 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866
2867 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2868public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002869 template <class _Gp, class ..._BA,
2870 class = typename enable_if
2871 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002872 is_constructible<_Fd, _Gp>::value &&
2873 !is_same<typename remove_reference<_Gp>::type,
2874 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002875 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002877 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2878 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002879 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880
2881 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002883 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 operator()(_Args&& ...__args)
2885 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002886 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002887 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 }
2889
2890 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002892 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 operator()(_Args&& ...__args) const
2894 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002895 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002896 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897 }
2898};
2899
Howard Hinnantc834c512011-11-29 18:15:50 +00002900template<class _Fp, class ..._BoundArgs>
2901struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002902
Howard Hinnantc834c512011-11-29 18:15:50 +00002903template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002905 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906{
Howard Hinnantc834c512011-11-29 18:15:50 +00002907 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002908 typedef typename base::_Fd _Fd;
2909 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002911 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912
Howard Hinnant7091e652011-07-02 18:22:36 +00002913
Howard Hinnantf292a922013-07-01 00:01:51 +00002914 template <class _Gp, class ..._BA,
2915 class = typename enable_if
2916 <
2917 is_constructible<_Fd, _Gp>::value &&
2918 !is_same<typename remove_reference<_Gp>::type,
2919 __bind_r>::value
2920 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002922 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2923 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002924 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925
2926 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002928 typename enable_if
2929 <
2930 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002931 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002932 result_type
2933 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 operator()(_Args&& ...__args)
2935 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002936 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2937 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938 }
2939
2940 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002942 typename enable_if
2943 <
2944 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002945 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002946 result_type
2947 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948 operator()(_Args&& ...__args) const
2949 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002950 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2951 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952 }
2953};
2954
Howard Hinnantc834c512011-11-29 18:15:50 +00002955template<class _Rp, class _Fp, class ..._BoundArgs>
2956struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957
Howard Hinnantc834c512011-11-29 18:15:50 +00002958template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002960__bind<_Fp, _BoundArgs...>
2961bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962{
Howard Hinnantc834c512011-11-29 18:15:50 +00002963 typedef __bind<_Fp, _BoundArgs...> type;
2964 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965}
2966
Howard Hinnantc834c512011-11-29 18:15:50 +00002967template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002969__bind_r<_Rp, _Fp, _BoundArgs...>
2970bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971{
Howard Hinnantc834c512011-11-29 18:15:50 +00002972 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2973 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974}
2975
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002976#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977
Eric Fiselier0d974f12015-07-14 20:16:15 +00002978#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002979
Eric Fiselier0d974f12015-07-14 20:16:15 +00002980template <class _Fn, class ..._Args>
Louis Dionneaf34f122019-04-03 17:54:37 +00002981invoke_result_t<_Fn, _Args...>
Eric Fiselier934f63b2016-06-02 01:25:41 +00002982invoke(_Fn&& __f, _Args&&... __args)
Louis Dionneaf34f122019-04-03 17:54:37 +00002983 noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
Eric Fiselier934f63b2016-06-02 01:25:41 +00002984{
2985 return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
Eric Fiselier0d974f12015-07-14 20:16:15 +00002986}
Eric Fiselier934f63b2016-06-02 01:25:41 +00002987
2988template <class _DecayFunc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002989class _LIBCPP_TEMPLATE_VIS __not_fn_imp {
Eric Fiselier934f63b2016-06-02 01:25:41 +00002990 _DecayFunc __fd;
2991
2992public:
2993 __not_fn_imp() = delete;
2994
2995 template <class ..._Args>
2996 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00002997 auto operator()(_Args&& ...__args) &
Eric Fiselier934f63b2016-06-02 01:25:41 +00002998 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002999 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
3000 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003001
3002 template <class ..._Args>
3003 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00003004 auto operator()(_Args&& ...__args) &&
3005 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003006 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
3007 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00003008
3009 template <class ..._Args>
3010 _LIBCPP_INLINE_VISIBILITY
3011 auto operator()(_Args&& ...__args) const&
Eric Fiselier934f63b2016-06-02 01:25:41 +00003012 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003013 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
3014 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003015
Eric Fiseliere8303a32016-06-27 00:40:41 +00003016
3017 template <class ..._Args>
3018 _LIBCPP_INLINE_VISIBILITY
3019 auto operator()(_Args&& ...__args) const&&
3020 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003021 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
3022 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00003023
Eric Fiselier934f63b2016-06-02 01:25:41 +00003024private:
3025 template <class _RawFunc,
3026 class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
3027 _LIBCPP_INLINE_VISIBILITY
3028 explicit __not_fn_imp(_RawFunc&& __rf)
3029 : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
3030
3031 template <class _RawFunc>
3032 friend inline _LIBCPP_INLINE_VISIBILITY
3033 __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
3034};
3035
3036template <class _RawFunc>
3037inline _LIBCPP_INLINE_VISIBILITY
3038__not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
3039 return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
3040}
3041
Eric Fiselier0d974f12015-07-14 20:16:15 +00003042#endif
3043
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003044// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045
Marshall Clowa40686b2018-01-08 19:18:00 +00003046template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00003047pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00003048__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3049 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
3050 forward_iterator_tag, forward_iterator_tag)
3051{
3052 if (__first2 == __last2)
3053 return make_pair(__first1, __first1); // Everything matches an empty sequence
3054 while (true)
3055 {
3056 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
3057 while (true)
3058 {
3059 if (__first1 == __last1) // return __last1 if no element matches *__first2
3060 return make_pair(__last1, __last1);
3061 if (__pred(*__first1, *__first2))
3062 break;
3063 ++__first1;
3064 }
3065 // *__first1 matches *__first2, now match elements after here
3066 _ForwardIterator1 __m1 = __first1;
3067 _ForwardIterator2 __m2 = __first2;
3068 while (true)
3069 {
3070 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
3071 return make_pair(__first1, __m1);
3072 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
3073 return make_pair(__last1, __last1);
3074 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
3075 {
3076 ++__first1;
3077 break;
3078 } // else there is a match, check next elements
3079 }
3080 }
3081}
3082
3083template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
3084_LIBCPP_CONSTEXPR_AFTER_CXX11
3085pair<_RandomAccessIterator1, _RandomAccessIterator1>
3086__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
3087 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
3088 random_access_iterator_tag, random_access_iterator_tag)
3089{
3090 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
3091 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
3092 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
3093 const _D2 __len2 = __last2 - __first2;
3094 if (__len2 == 0)
3095 return make_pair(__first1, __first1);
3096 const _D1 __len1 = __last1 - __first1;
3097 if (__len1 < __len2)
3098 return make_pair(__last1, __last1);
3099 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
3100
3101 while (true)
3102 {
3103 while (true)
3104 {
3105 if (__first1 == __s)
3106 return make_pair(__last1, __last1);
3107 if (__pred(*__first1, *__first2))
3108 break;
3109 ++__first1;
3110 }
3111
3112 _RandomAccessIterator1 __m1 = __first1;
3113 _RandomAccessIterator2 __m2 = __first2;
3114 while (true)
3115 {
3116 if (++__m2 == __last2)
3117 return make_pair(__first1, __first1 + __len2);
3118 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
3119 if (!__pred(*__m1, *__m2))
3120 {
3121 ++__first1;
3122 break;
3123 }
3124 }
3125 }
3126}
3127
3128#if _LIBCPP_STD_VER > 14
3129
3130// default searcher
3131template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Dimitry Andricfd3633d2018-02-13 17:40:59 +00003132class _LIBCPP_TYPE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00003133public:
3134 _LIBCPP_INLINE_VISIBILITY
Louis Dionne44bcff92018-08-03 22:36:53 +00003135 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00003136 _BinaryPredicate __p = _BinaryPredicate())
3137 : __first_(__f), __last_(__l), __pred_(__p) {}
3138
3139 template <typename _ForwardIterator2>
3140 _LIBCPP_INLINE_VISIBILITY
3141 pair<_ForwardIterator2, _ForwardIterator2>
3142 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3143 {
3144 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
3145 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
3146 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
3147 }
3148
3149private:
3150 _ForwardIterator __first_;
3151 _ForwardIterator __last_;
3152 _BinaryPredicate __pred_;
Eric Fiselier80663c52019-08-04 07:13:43 +00003153 };
Marshall Clowa40686b2018-01-08 19:18:00 +00003154
3155#endif // _LIBCPP_STD_VER > 14
3156
Louis Dionnedb1892a2018-12-03 14:03:27 +00003157#if _LIBCPP_STD_VER > 17
3158template <class _Tp>
3159using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3160
3161template <class _Tp>
3162using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3163#endif // > C++17
3164
Marshall Clow29b53f22018-12-14 18:49:35 +00003165template <class _Container, class _Predicate>
3166inline void __libcpp_erase_if_container( _Container& __c, _Predicate __pred)
3167{
3168 for (typename _Container::iterator __iter = __c.begin(), __last = __c.end(); __iter != __last;)
3169 {
3170 if (__pred(*__iter))
3171 __iter = __c.erase(__iter);
3172 else
3173 ++__iter;
3174 }
3175}
3176
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177_LIBCPP_END_NAMESPACE_STD
3178
3179#endif // _LIBCPP_FUNCTIONAL