blob: a54868e822203fce494003ee4ab11db75b21b075 [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>;
Yuriy Chernyshovfbb87fa2020-12-08 13:39:56 -0500473template <> struct hash<char8_t>; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474template <> struct hash<char16_t>;
475template <> struct hash<char32_t>;
476template <> struct hash<wchar_t>;
477template <> struct hash<short>;
478template <> struct hash<unsigned short>;
479template <> struct hash<int>;
480template <> struct hash<unsigned int>;
481template <> struct hash<long>;
482template <> struct hash<long long>;
483template <> struct hash<unsigned long>;
484template <> struct hash<unsigned long long>;
485
486template <> struct hash<float>;
487template <> struct hash<double>;
488template <> struct hash<long double>;
489
490template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000491template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492
493} // std
494
495POLICY: For non-variadic implementations, the number of arguments is limited
496 to 3. It is hoped that the need for non-variadic implementations
497 will be minimal.
498
499*/
500
501#include <__config>
502#include <type_traits>
503#include <typeinfo>
504#include <exception>
505#include <memory>
506#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000507#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000508#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000509
510#include <__functional_base>
511
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000512#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000513#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000514#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515
516_LIBCPP_BEGIN_NAMESPACE_STD
517
Marshall Clow974bae22013-07-29 14:21:53 +0000518#if _LIBCPP_STD_VER > 11
519template <class _Tp = void>
520#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000522#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000523struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524{
Marshall Clowd18ee652013-09-28 19:06:12 +0000525 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
526 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527 {return __x + __y;}
528};
529
Marshall Clow974bae22013-07-29 14:21:53 +0000530#if _LIBCPP_STD_VER > 11
531template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000532struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000533{
534 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000535 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
536 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000537 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
538 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
539 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000540 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000541};
542#endif
543
544
545#if _LIBCPP_STD_VER > 11
546template <class _Tp = void>
547#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000549#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000550struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551{
Marshall Clowd18ee652013-09-28 19:06:12 +0000552 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
553 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554 {return __x - __y;}
555};
556
Marshall Clow974bae22013-07-29 14:21:53 +0000557#if _LIBCPP_STD_VER > 11
558template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000559struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000560{
561 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000562 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
563 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000564 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
565 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
566 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000567 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000568};
569#endif
570
571
572#if _LIBCPP_STD_VER > 11
573template <class _Tp = void>
574#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000576#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000577struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578{
Marshall Clowd18ee652013-09-28 19:06:12 +0000579 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
580 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581 {return __x * __y;}
582};
583
Marshall Clow974bae22013-07-29 14:21:53 +0000584#if _LIBCPP_STD_VER > 11
585template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000586struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000587{
588 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000589 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
590 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000591 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
592 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
593 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000594 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000595};
596#endif
597
598
599#if _LIBCPP_STD_VER > 11
600template <class _Tp = void>
601#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000603#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000604struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605{
Marshall Clowd18ee652013-09-28 19:06:12 +0000606 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
607 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608 {return __x / __y;}
609};
610
Marshall Clow974bae22013-07-29 14:21:53 +0000611#if _LIBCPP_STD_VER > 11
612template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000613struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000614{
615 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000616 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
617 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000618 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
619 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
620 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000621 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000622};
623#endif
624
625
626#if _LIBCPP_STD_VER > 11
627template <class _Tp = void>
628#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000630#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000631struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632{
Marshall Clowd18ee652013-09-28 19:06:12 +0000633 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
634 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635 {return __x % __y;}
636};
637
Marshall Clow974bae22013-07-29 14:21:53 +0000638#if _LIBCPP_STD_VER > 11
639template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000640struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000641{
642 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000643 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
644 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000645 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
646 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
647 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000648 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000649};
650#endif
651
652
653#if _LIBCPP_STD_VER > 11
654template <class _Tp = void>
655#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000657#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000658struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659{
Marshall Clowd18ee652013-09-28 19:06:12 +0000660 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
661 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 {return -__x;}
663};
664
Marshall Clow974bae22013-07-29 14:21:53 +0000665#if _LIBCPP_STD_VER > 11
666template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000667struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000668{
669 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000670 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
671 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000672 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
673 -> decltype (- _VSTD::forward<_Tp>(__x))
674 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000675 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000676};
677#endif
678
679
680#if _LIBCPP_STD_VER > 11
681template <class _Tp = void>
682#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000683template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000684#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000685struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686{
Marshall Clowd18ee652013-09-28 19:06:12 +0000687 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
688 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689 {return __x == __y;}
690};
691
Marshall Clow974bae22013-07-29 14:21:53 +0000692#if _LIBCPP_STD_VER > 11
693template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000694struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000695{
Marshall Clowd18ee652013-09-28 19:06:12 +0000696 template <class _T1, class _T2>
697 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000698 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000699 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
700 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
701 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000702 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000703};
704#endif
705
706
707#if _LIBCPP_STD_VER > 11
708template <class _Tp = void>
709#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000711#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000712struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713{
Marshall Clowd18ee652013-09-28 19:06:12 +0000714 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
715 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716 {return __x != __y;}
717};
718
Marshall Clow974bae22013-07-29 14:21:53 +0000719#if _LIBCPP_STD_VER > 11
720template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000721struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000722{
Marshall Clowd18ee652013-09-28 19:06:12 +0000723 template <class _T1, class _T2>
724 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000725 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000726 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
727 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
728 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000729 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000730};
731#endif
732
733
734#if _LIBCPP_STD_VER > 11
735template <class _Tp = void>
736#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000738#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000739struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740{
Marshall Clowd18ee652013-09-28 19:06:12 +0000741 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
742 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 {return __x > __y;}
744};
745
Marshall Clow974bae22013-07-29 14:21:53 +0000746#if _LIBCPP_STD_VER > 11
747template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000748struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000749{
Marshall Clowd18ee652013-09-28 19:06:12 +0000750 template <class _T1, class _T2>
751 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000752 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000753 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
754 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
755 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000756 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000757};
758#endif
759
760
Howard Hinnantb17caf92012-02-21 21:02:58 +0000761// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762
Marshall Clow974bae22013-07-29 14:21:53 +0000763#if _LIBCPP_STD_VER > 11
764template <class _Tp = void>
765#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000767#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000768struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769{
Marshall Clowd18ee652013-09-28 19:06:12 +0000770 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
771 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 {return __x >= __y;}
773};
774
Marshall Clow974bae22013-07-29 14:21:53 +0000775#if _LIBCPP_STD_VER > 11
776template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000777struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000778{
Marshall Clowd18ee652013-09-28 19:06:12 +0000779 template <class _T1, class _T2>
780 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000781 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000782 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
783 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
784 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000785 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000786};
787#endif
788
789
790#if _LIBCPP_STD_VER > 11
791template <class _Tp = void>
792#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000794#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000795struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796{
Marshall Clowd18ee652013-09-28 19:06:12 +0000797 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
798 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 {return __x <= __y;}
800};
801
Marshall Clow974bae22013-07-29 14:21:53 +0000802#if _LIBCPP_STD_VER > 11
803template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000804struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000805{
Marshall Clowd18ee652013-09-28 19:06:12 +0000806 template <class _T1, class _T2>
807 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000808 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000809 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
810 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
811 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000812 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000813};
814#endif
815
816
817#if _LIBCPP_STD_VER > 11
818template <class _Tp = void>
819#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000821#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000822struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823{
Marshall Clowd18ee652013-09-28 19:06:12 +0000824 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
825 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826 {return __x && __y;}
827};
828
Marshall Clow974bae22013-07-29 14:21:53 +0000829#if _LIBCPP_STD_VER > 11
830template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000831struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000832{
Marshall Clowd18ee652013-09-28 19:06:12 +0000833 template <class _T1, class _T2>
834 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000835 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000836 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
837 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
838 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000839 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000840};
841#endif
842
843
844#if _LIBCPP_STD_VER > 11
845template <class _Tp = void>
846#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000848#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000849struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850{
Marshall Clowd18ee652013-09-28 19:06:12 +0000851 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
852 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853 {return __x || __y;}
854};
855
Marshall Clow974bae22013-07-29 14:21:53 +0000856#if _LIBCPP_STD_VER > 11
857template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000858struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000859{
Marshall Clowd18ee652013-09-28 19:06:12 +0000860 template <class _T1, class _T2>
861 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000862 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000863 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
864 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
865 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000866 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000867};
868#endif
869
870
871#if _LIBCPP_STD_VER > 11
872template <class _Tp = void>
873#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000875#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000876struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877{
Marshall Clowd18ee652013-09-28 19:06:12 +0000878 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
879 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880 {return !__x;}
881};
882
Marshall Clow974bae22013-07-29 14:21:53 +0000883#if _LIBCPP_STD_VER > 11
884template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000885struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000886{
887 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000888 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
889 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000890 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
891 -> decltype (!_VSTD::forward<_Tp>(__x))
892 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000893 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000894};
895#endif
896
897
898#if _LIBCPP_STD_VER > 11
899template <class _Tp = void>
900#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000902#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000903struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904{
Marshall Clowd18ee652013-09-28 19:06:12 +0000905 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
906 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907 {return __x & __y;}
908};
909
Marshall Clow974bae22013-07-29 14:21:53 +0000910#if _LIBCPP_STD_VER > 11
911template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000912struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000913{
Marshall Clowd18ee652013-09-28 19:06:12 +0000914 template <class _T1, class _T2>
915 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000916 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000917 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
918 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
919 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000920 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000921};
922#endif
923
924
925#if _LIBCPP_STD_VER > 11
926template <class _Tp = void>
927#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000929#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000930struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931{
Marshall Clowd18ee652013-09-28 19:06:12 +0000932 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
933 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 {return __x | __y;}
935};
936
Marshall Clow974bae22013-07-29 14:21:53 +0000937#if _LIBCPP_STD_VER > 11
938template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000939struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000940{
Marshall Clowd18ee652013-09-28 19:06:12 +0000941 template <class _T1, class _T2>
942 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000943 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000944 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
945 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
946 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000947 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000948};
949#endif
950
951
952#if _LIBCPP_STD_VER > 11
953template <class _Tp = void>
954#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000956#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000957struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958{
Marshall Clowd18ee652013-09-28 19:06:12 +0000959 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
960 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 {return __x ^ __y;}
962};
963
Marshall Clow974bae22013-07-29 14:21:53 +0000964#if _LIBCPP_STD_VER > 11
965template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000966struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000967{
Marshall Clowd18ee652013-09-28 19:06:12 +0000968 template <class _T1, class _T2>
969 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000970 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000971 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
972 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
973 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000974 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000975};
976#endif
977
978
979#if _LIBCPP_STD_VER > 11
980template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000981struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000982{
Marshall Clowd18ee652013-09-28 19:06:12 +0000983 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
984 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000985 {return ~__x;}
986};
987
988template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000989struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000990{
991 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000992 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
993 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000994 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
995 -> decltype (~_VSTD::forward<_Tp>(__x))
996 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000997 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000998};
999#endif
1000
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001002class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 : public unary_function<typename _Predicate::argument_type, bool>
1004{
1005 _Predicate __pred_;
1006public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001007 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1008 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001010 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1011 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 {return !__pred_(__x);}
1013};
1014
1015template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001016_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017unary_negate<_Predicate>
1018not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1019
1020template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001021class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022 : public binary_function<typename _Predicate::first_argument_type,
1023 typename _Predicate::second_argument_type,
1024 bool>
1025{
1026 _Predicate __pred_;
1027public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001028 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001029 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1030
1031 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1032 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 const typename _Predicate::second_argument_type& __y) const
1034 {return !__pred_(__x, __y);}
1035};
1036
1037template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001038_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039binary_negate<_Predicate>
1040not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1041
Marshall Clow26a027c2017-04-13 18:25:32 +00001042#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001044class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 : public unary_function<typename __Operation::second_argument_type,
1046 typename __Operation::result_type>
1047{
1048protected:
1049 __Operation op;
1050 typename __Operation::first_argument_type value;
1051public:
1052 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1053 const typename __Operation::first_argument_type __y)
1054 : op(__x), value(__y) {}
1055 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1056 (typename __Operation::second_argument_type& __x) const
1057 {return op(value, __x);}
1058 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1059 (const typename __Operation::second_argument_type& __x) const
1060 {return op(value, __x);}
1061};
1062
1063template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001064_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065binder1st<__Operation>
1066bind1st(const __Operation& __op, const _Tp& __x)
1067 {return binder1st<__Operation>(__op, __x);}
1068
1069template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001070class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071 : public unary_function<typename __Operation::first_argument_type,
1072 typename __Operation::result_type>
1073{
1074protected:
1075 __Operation op;
1076 typename __Operation::second_argument_type value;
1077public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1080 : op(__x), value(__y) {}
1081 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1082 ( typename __Operation::first_argument_type& __x) const
1083 {return op(__x, value);}
1084 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1085 (const typename __Operation::first_argument_type& __x) const
1086 {return op(__x, value);}
1087};
1088
1089template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001090_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091binder2nd<__Operation>
1092bind2nd(const __Operation& __op, const _Tp& __x)
1093 {return binder2nd<__Operation>(__op, __x);}
1094
1095template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001096class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001097 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098{
1099 _Result (*__f_)(_Arg);
1100public:
1101 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1102 : __f_(__f) {}
1103 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1104 {return __f_(__x);}
1105};
1106
1107template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001108_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109pointer_to_unary_function<_Arg,_Result>
1110ptr_fun(_Result (*__f)(_Arg))
1111 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1112
1113template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001114class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001115 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116{
1117 _Result (*__f_)(_Arg1, _Arg2);
1118public:
1119 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1120 : __f_(__f) {}
1121 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1122 {return __f_(__x, __y);}
1123};
1124
1125template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001126_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127pointer_to_binary_function<_Arg1,_Arg2,_Result>
1128ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1129 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1130
1131template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001132class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1133 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134{
1135 _Sp (_Tp::*__p_)();
1136public:
1137 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1138 : __p_(__p) {}
1139 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1140 {return (__p->*__p_)();}
1141};
1142
1143template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001144class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1145 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146{
1147 _Sp (_Tp::*__p_)(_Ap);
1148public:
1149 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1150 : __p_(__p) {}
1151 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1152 {return (__p->*__p_)(__x);}
1153};
1154
1155template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001156_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157mem_fun_t<_Sp,_Tp>
1158mem_fun(_Sp (_Tp::*__f)())
1159 {return mem_fun_t<_Sp,_Tp>(__f);}
1160
1161template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001162_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163mem_fun1_t<_Sp,_Tp,_Ap>
1164mem_fun(_Sp (_Tp::*__f)(_Ap))
1165 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1166
1167template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001168class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1169 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170{
1171 _Sp (_Tp::*__p_)();
1172public:
1173 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1174 : __p_(__p) {}
1175 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1176 {return (__p.*__p_)();}
1177};
1178
1179template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001180class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1181 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182{
1183 _Sp (_Tp::*__p_)(_Ap);
1184public:
1185 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1186 : __p_(__p) {}
1187 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1188 {return (__p.*__p_)(__x);}
1189};
1190
1191template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001192_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193mem_fun_ref_t<_Sp,_Tp>
1194mem_fun_ref(_Sp (_Tp::*__f)())
1195 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1196
1197template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001198_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199mem_fun1_ref_t<_Sp,_Tp,_Ap>
1200mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1201 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1202
1203template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001204class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1205 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206{
1207 _Sp (_Tp::*__p_)() const;
1208public:
1209 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1210 : __p_(__p) {}
1211 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1212 {return (__p->*__p_)();}
1213};
1214
1215template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001216class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1217 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218{
1219 _Sp (_Tp::*__p_)(_Ap) const;
1220public:
1221 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1222 : __p_(__p) {}
1223 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1224 {return (__p->*__p_)(__x);}
1225};
1226
1227template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001228_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229const_mem_fun_t<_Sp,_Tp>
1230mem_fun(_Sp (_Tp::*__f)() const)
1231 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1232
1233template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001234_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235const_mem_fun1_t<_Sp,_Tp,_Ap>
1236mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1237 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1238
1239template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001240class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1241 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242{
1243 _Sp (_Tp::*__p_)() const;
1244public:
1245 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1246 : __p_(__p) {}
1247 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1248 {return (__p.*__p_)();}
1249};
1250
1251template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001252class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001253 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254{
1255 _Sp (_Tp::*__p_)(_Ap) const;
1256public:
1257 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1258 : __p_(__p) {}
1259 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1260 {return (__p.*__p_)(__x);}
1261};
1262
1263template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001264_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265const_mem_fun_ref_t<_Sp,_Tp>
1266mem_fun_ref(_Sp (_Tp::*__f)() const)
1267 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1268
1269template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001270_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1272mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1273 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001274#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001276////////////////////////////////////////////////////////////////////////////////
1277// MEMFUN
1278//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280template <class _Tp>
1281class __mem_fn
1282 : public __weak_result_type<_Tp>
1283{
1284public:
1285 // types
1286 typedef _Tp type;
1287private:
1288 type __f_;
1289
1290public:
Marshall Clowad8ff212015-10-25 20:12:16 +00001291 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001293#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294 // invoke
1295 template <class... _ArgTypes>
Eric Fiselier2cc48332015-07-22 22:43:27 +00001296 _LIBCPP_INLINE_VISIBILITY
1297 typename __invoke_return<type, _ArgTypes...>::type
1298 operator() (_ArgTypes&&... __args) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001299 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001300 }
1301#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001302
1303 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001304 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001305 typename __invoke_return0<type, _A0>::type
1306 operator() (_A0& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001307 return _VSTD::__invoke(__f_, __a0);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001308 }
1309
Eric Fiselierce1813a2015-08-26 20:15:02 +00001310 template <class _A0>
1311 _LIBCPP_INLINE_VISIBILITY
1312 typename __invoke_return0<type, _A0 const>::type
1313 operator() (_A0 const& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001314 return _VSTD::__invoke(__f_, __a0);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001315 }
1316
Eric Fiselier2cc48332015-07-22 22:43:27 +00001317 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001318 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001319 typename __invoke_return1<type, _A0, _A1>::type
1320 operator() (_A0& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001321 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001322 }
1323
Eric Fiselierce1813a2015-08-26 20:15:02 +00001324 template <class _A0, class _A1>
1325 _LIBCPP_INLINE_VISIBILITY
1326 typename __invoke_return1<type, _A0 const, _A1>::type
1327 operator() (_A0 const& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001328 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001329 }
1330
1331 template <class _A0, class _A1>
1332 _LIBCPP_INLINE_VISIBILITY
1333 typename __invoke_return1<type, _A0, _A1 const>::type
1334 operator() (_A0& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001335 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001336 }
1337
1338 template <class _A0, class _A1>
1339 _LIBCPP_INLINE_VISIBILITY
1340 typename __invoke_return1<type, _A0 const, _A1 const>::type
1341 operator() (_A0 const& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001342 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001343 }
1344
Eric Fiselier2cc48332015-07-22 22:43:27 +00001345 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001346 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001347 typename __invoke_return2<type, _A0, _A1, _A2>::type
1348 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001349 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001350 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001351
1352 template <class _A0, class _A1, class _A2>
1353 _LIBCPP_INLINE_VISIBILITY
1354 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1355 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001356 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001357 }
1358
1359 template <class _A0, class _A1, class _A2>
1360 _LIBCPP_INLINE_VISIBILITY
1361 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1362 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001363 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001364 }
1365
1366 template <class _A0, class _A1, class _A2>
1367 _LIBCPP_INLINE_VISIBILITY
1368 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1369 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001370 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001371 }
1372
1373 template <class _A0, class _A1, class _A2>
1374 _LIBCPP_INLINE_VISIBILITY
1375 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1376 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001377 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001378 }
1379
1380 template <class _A0, class _A1, class _A2>
1381 _LIBCPP_INLINE_VISIBILITY
1382 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1383 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001384 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001385 }
1386
1387 template <class _A0, class _A1, class _A2>
1388 _LIBCPP_INLINE_VISIBILITY
1389 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1390 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001391 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001392 }
1393
1394 template <class _A0, class _A1, class _A2>
1395 _LIBCPP_INLINE_VISIBILITY
1396 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1397 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001398 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001399 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001400#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401};
1402
Howard Hinnantc834c512011-11-29 18:15:50 +00001403template<class _Rp, class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001405__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001406mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407{
Howard Hinnantc834c512011-11-29 18:15:50 +00001408 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409}
1410
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001411////////////////////////////////////////////////////////////////////////////////
1412// FUNCTION
1413//==============================================================================
1414
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415// bad_function_call
1416
Howard Hinnant4ff57432010-09-21 22:55:27 +00001417class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418 : public exception
1419{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001420#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1421public:
1422 virtual ~bad_function_call() _NOEXCEPT;
1423
1424 virtual const char* what() const _NOEXCEPT;
1425#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426};
1427
Louis Dionne16fe2952018-07-11 23:14:33 +00001428_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001429void __throw_bad_function_call()
1430{
1431#ifndef _LIBCPP_NO_EXCEPTIONS
1432 throw bad_function_call();
1433#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001434 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001435#endif
1436}
1437
Louis Dionne44d1f812020-03-09 11:16:22 -04001438#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1439# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1440 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1441#else
1442# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1443#endif
1444
1445template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446
1447namespace __function
1448{
1449
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001450template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451struct __maybe_derive_from_unary_function
1452{
1453};
1454
Howard Hinnantc834c512011-11-29 18:15:50 +00001455template<class _Rp, class _A1>
1456struct __maybe_derive_from_unary_function<_Rp(_A1)>
1457 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458{
1459};
1460
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001461template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462struct __maybe_derive_from_binary_function
1463{
1464};
1465
Howard Hinnantc834c512011-11-29 18:15:50 +00001466template<class _Rp, class _A1, class _A2>
1467struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1468 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469{
1470};
1471
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001472template <class _Fp>
1473_LIBCPP_INLINE_VISIBILITY
1474bool __not_null(_Fp const&) { return true; }
1475
1476template <class _Fp>
1477_LIBCPP_INLINE_VISIBILITY
1478bool __not_null(_Fp* __ptr) { return __ptr; }
1479
1480template <class _Ret, class _Class>
1481_LIBCPP_INLINE_VISIBILITY
1482bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1483
1484template <class _Fp>
1485_LIBCPP_INLINE_VISIBILITY
1486bool __not_null(function<_Fp> const& __f) { return !!__f; }
1487
Louis Dionnee2391d72020-04-22 13:58:17 -04001488#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1489template <class _Rp, class ..._Args>
1490_LIBCPP_INLINE_VISIBILITY
1491bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1492#endif
1493
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001494} // namespace __function
1495
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001496#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001497
1498namespace __function {
1499
Eric Fiselier125798e2018-12-10 18:14:09 +00001500// __alloc_func holds a functor and an allocator.
1501
1502template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001503template <class _Fp, class _FB>
1504class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001505
1506template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1507class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1508{
1509 __compressed_pair<_Fp, _Ap> __f_;
1510
1511 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001512 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1513 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001514
1515 _LIBCPP_INLINE_VISIBILITY
1516 const _Target& __target() const { return __f_.first(); }
1517
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001518 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001519 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001520 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001521
1522 _LIBCPP_INLINE_VISIBILITY
1523 explicit __alloc_func(_Target&& __f)
1524 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1525 _VSTD::forward_as_tuple())
1526 {
1527 }
1528
1529 _LIBCPP_INLINE_VISIBILITY
1530 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1531 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1532 _VSTD::forward_as_tuple(__a))
1533 {
1534 }
1535
1536 _LIBCPP_INLINE_VISIBILITY
1537 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1538 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1539 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1540 {
1541 }
1542
1543 _LIBCPP_INLINE_VISIBILITY
1544 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1545 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1546 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1547 {
1548 }
1549
1550 _LIBCPP_INLINE_VISIBILITY
1551 _Rp operator()(_ArgTypes&&... __arg)
1552 {
1553 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1554 return _Invoker::__call(__f_.first(),
1555 _VSTD::forward<_ArgTypes>(__arg)...);
1556 }
1557
1558 _LIBCPP_INLINE_VISIBILITY
1559 __alloc_func* __clone() const
1560 {
1561 typedef allocator_traits<_Alloc> __alloc_traits;
1562 typedef
1563 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1564 _AA;
1565 _AA __a(__f_.second());
1566 typedef __allocator_destructor<_AA> _Dp;
1567 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1568 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1569 return __hold.release();
1570 }
1571
1572 _LIBCPP_INLINE_VISIBILITY
1573 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001574
1575 static void __destroy_and_delete(__alloc_func* __f) {
1576 typedef allocator_traits<_Alloc> __alloc_traits;
1577 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1578 _FunAlloc;
1579 _FunAlloc __a(__f->__get_allocator());
1580 __f->destroy();
1581 __a.deallocate(__f, 1);
1582 }
1583};
1584
1585template <class _Fp, class _Rp, class... _ArgTypes>
1586class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1587 _Fp __f_;
1588
1589public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001590 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001591
1592 _LIBCPP_INLINE_VISIBILITY
1593 const _Target& __target() const { return __f_; }
1594
1595 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001596 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001597
1598 _LIBCPP_INLINE_VISIBILITY
1599 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1600
1601 _LIBCPP_INLINE_VISIBILITY
1602 _Rp operator()(_ArgTypes&&... __arg) {
1603 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1604 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1605 }
1606
1607 _LIBCPP_INLINE_VISIBILITY
1608 __default_alloc_func* __clone() const {
1609 __builtin_new_allocator::__holder_t __hold =
1610 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1611 __default_alloc_func* __res =
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001612 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00001613 (void)__hold.release();
1614 return __res;
1615 }
1616
1617 _LIBCPP_INLINE_VISIBILITY
1618 void destroy() _NOEXCEPT { __f_.~_Target(); }
1619
1620 static void __destroy_and_delete(__default_alloc_func* __f) {
1621 __f->destroy();
1622 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1623 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001624};
1625
1626// __base provides an abstract interface for copyable functors.
1627
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001628template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629
Howard Hinnantc834c512011-11-29 18:15:50 +00001630template<class _Rp, class ..._ArgTypes>
1631class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632{
1633 __base(const __base&);
1634 __base& operator=(const __base&);
1635public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001636 _LIBCPP_INLINE_VISIBILITY __base() {}
1637 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 virtual __base* __clone() const = 0;
1639 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001640 virtual void destroy() _NOEXCEPT = 0;
1641 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001642 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001643#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001644 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1645 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001646#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647};
1648
Eric Fiselier125798e2018-12-10 18:14:09 +00001649// __func implements __base for a given functor type.
1650
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651template<class _FD, class _Alloc, class _FB> class __func;
1652
Howard Hinnantc834c512011-11-29 18:15:50 +00001653template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1654class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1655 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656{
Eric Fiselier125798e2018-12-10 18:14:09 +00001657 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001660 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001661 : __f_(_VSTD::move(__f)) {}
1662
Howard Hinnant4ff57432010-09-21 22:55:27 +00001663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001664 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001665 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001666
1667 _LIBCPP_INLINE_VISIBILITY
1668 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001669 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001670
1671 _LIBCPP_INLINE_VISIBILITY
1672 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001673 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1674
Howard Hinnantc834c512011-11-29 18:15:50 +00001675 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1676 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001677 virtual void destroy() _NOEXCEPT;
1678 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001679 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001680#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001681 virtual const void* target(const type_info&) const _NOEXCEPT;
1682 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001683#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684};
1685
Howard Hinnantc834c512011-11-29 18:15:50 +00001686template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1687__base<_Rp(_ArgTypes...)>*
1688__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001690 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001691 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001692 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001693 typedef __allocator_destructor<_Ap> _Dp;
1694 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001695 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696 return __hold.release();
1697}
1698
Howard Hinnantc834c512011-11-29 18:15:50 +00001699template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700void
Howard Hinnantc834c512011-11-29 18:15:50 +00001701__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702{
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001703 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704}
1705
Howard Hinnantc834c512011-11-29 18:15:50 +00001706template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707void
Howard Hinnantc834c512011-11-29 18:15:50 +00001708__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709{
Eric Fiselier125798e2018-12-10 18:14:09 +00001710 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711}
1712
Howard Hinnantc834c512011-11-29 18:15:50 +00001713template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714void
Howard Hinnantc834c512011-11-29 18:15:50 +00001715__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001717 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001718 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001719 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001720 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721 __a.deallocate(this, 1);
1722}
1723
Howard Hinnantc834c512011-11-29 18:15:50 +00001724template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1725_Rp
1726__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727{
Eric Fiselier125798e2018-12-10 18:14:09 +00001728 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729}
1730
Howard Hinnant72f73582010-08-11 17:04:31 +00001731#ifndef _LIBCPP_NO_RTTI
1732
Howard Hinnantc834c512011-11-29 18:15:50 +00001733template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001735__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736{
Howard Hinnantc834c512011-11-29 18:15:50 +00001737 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001738 return &__f_.__target();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001739 return nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740}
1741
Howard Hinnantc834c512011-11-29 18:15:50 +00001742template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001744__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745{
Howard Hinnantc834c512011-11-29 18:15:50 +00001746 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747}
1748
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001749#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001750
Eric Fiselier125798e2018-12-10 18:14:09 +00001751// __value_func creates a value-type from a __func.
1752
1753template <class _Fp> class __value_func;
1754
1755template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1756{
1757 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1758
1759 typedef __base<_Rp(_ArgTypes...)> __func;
1760 __func* __f_;
1761
1762 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1763 {
1764 return reinterpret_cast<__func*>(p);
1765 }
1766
1767 public:
1768 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001769 __value_func() _NOEXCEPT : __f_(nullptr) {}
Eric Fiselier125798e2018-12-10 18:14:09 +00001770
1771 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001772 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001773 : __f_(nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001774 {
1775 typedef allocator_traits<_Alloc> __alloc_traits;
1776 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1777 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1778 _FunAlloc;
1779
1780 if (__function::__not_null(__f))
1781 {
1782 _FunAlloc __af(__a);
1783 if (sizeof(_Fun) <= sizeof(__buf_) &&
1784 is_nothrow_copy_constructible<_Fp>::value &&
1785 is_nothrow_copy_constructible<_FunAlloc>::value)
1786 {
1787 __f_ =
1788 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1789 }
1790 else
1791 {
1792 typedef __allocator_destructor<_FunAlloc> _Dp;
1793 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1794 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1795 __f_ = __hold.release();
1796 }
1797 }
1798 }
1799
Eric Fiselier74ebee62019-06-08 01:31:19 +00001800 template <class _Fp,
1801 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1802 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001803 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001804
Eric Fiselier125798e2018-12-10 18:14:09 +00001805 _LIBCPP_INLINE_VISIBILITY
1806 __value_func(const __value_func& __f)
1807 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001808 if (__f.__f_ == nullptr)
1809 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001810 else if ((void*)__f.__f_ == &__f.__buf_)
1811 {
1812 __f_ = __as_base(&__buf_);
1813 __f.__f_->__clone(__f_);
1814 }
1815 else
1816 __f_ = __f.__f_->__clone();
1817 }
1818
1819 _LIBCPP_INLINE_VISIBILITY
1820 __value_func(__value_func&& __f) _NOEXCEPT
1821 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001822 if (__f.__f_ == nullptr)
1823 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001824 else if ((void*)__f.__f_ == &__f.__buf_)
1825 {
1826 __f_ = __as_base(&__buf_);
1827 __f.__f_->__clone(__f_);
1828 }
1829 else
1830 {
1831 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001832 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001833 }
1834 }
1835
1836 _LIBCPP_INLINE_VISIBILITY
1837 ~__value_func()
1838 {
1839 if ((void*)__f_ == &__buf_)
1840 __f_->destroy();
1841 else if (__f_)
1842 __f_->destroy_deallocate();
1843 }
1844
1845 _LIBCPP_INLINE_VISIBILITY
1846 __value_func& operator=(__value_func&& __f)
1847 {
1848 *this = nullptr;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001849 if (__f.__f_ == nullptr)
1850 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001851 else if ((void*)__f.__f_ == &__f.__buf_)
1852 {
1853 __f_ = __as_base(&__buf_);
1854 __f.__f_->__clone(__f_);
1855 }
1856 else
1857 {
1858 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001859 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001860 }
1861 return *this;
1862 }
1863
1864 _LIBCPP_INLINE_VISIBILITY
1865 __value_func& operator=(nullptr_t)
1866 {
1867 __func* __f = __f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001868 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001869 if ((void*)__f == &__buf_)
1870 __f->destroy();
1871 else if (__f)
1872 __f->destroy_deallocate();
1873 return *this;
1874 }
1875
1876 _LIBCPP_INLINE_VISIBILITY
1877 _Rp operator()(_ArgTypes&&... __args) const
1878 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001879 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001880 __throw_bad_function_call();
1881 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1882 }
1883
1884 _LIBCPP_INLINE_VISIBILITY
1885 void swap(__value_func& __f) _NOEXCEPT
1886 {
1887 if (&__f == this)
1888 return;
1889 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1890 {
1891 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1892 __func* __t = __as_base(&__tempbuf);
1893 __f_->__clone(__t);
1894 __f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001895 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001896 __f.__f_->__clone(__as_base(&__buf_));
1897 __f.__f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001898 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001899 __f_ = __as_base(&__buf_);
1900 __t->__clone(__as_base(&__f.__buf_));
1901 __t->destroy();
1902 __f.__f_ = __as_base(&__f.__buf_);
1903 }
1904 else if ((void*)__f_ == &__buf_)
1905 {
1906 __f_->__clone(__as_base(&__f.__buf_));
1907 __f_->destroy();
1908 __f_ = __f.__f_;
1909 __f.__f_ = __as_base(&__f.__buf_);
1910 }
1911 else if ((void*)__f.__f_ == &__f.__buf_)
1912 {
1913 __f.__f_->__clone(__as_base(&__buf_));
1914 __f.__f_->destroy();
1915 __f.__f_ = __f_;
1916 __f_ = __as_base(&__buf_);
1917 }
1918 else
1919 _VSTD::swap(__f_, __f.__f_);
1920 }
1921
1922 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001923 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
Eric Fiselier125798e2018-12-10 18:14:09 +00001924
1925#ifndef _LIBCPP_NO_RTTI
1926 _LIBCPP_INLINE_VISIBILITY
1927 const std::type_info& target_type() const _NOEXCEPT
1928 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001929 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001930 return typeid(void);
1931 return __f_->target_type();
1932 }
1933
1934 template <typename _Tp>
1935 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1936 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001937 if (__f_ == nullptr)
1938 return nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001939 return (const _Tp*)__f_->target(typeid(_Tp));
1940 }
1941#endif // _LIBCPP_NO_RTTI
1942};
1943
Eric Fiselierf2e64362018-12-11 00:14:34 +00001944// Storage for a functor object, to be used with __policy to manage copy and
1945// destruction.
1946union __policy_storage
1947{
1948 mutable char __small[sizeof(void*) * 2];
1949 void* __large;
1950};
1951
1952// True if _Fun can safely be held in __policy_storage.__small.
1953template <typename _Fun>
1954struct __use_small_storage
1955 : public _VSTD::integral_constant<
1956 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001957 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Eric Fiselierf2e64362018-12-11 00:14:34 +00001958 _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1959 _VSTD::is_trivially_destructible<_Fun>::value> {};
1960
1961// Policy contains information about how to copy, destroy, and move the
1962// underlying functor. You can think of it as a vtable of sorts.
1963struct __policy
1964{
1965 // Used to copy or destroy __large values. null for trivial objects.
1966 void* (*const __clone)(const void*);
1967 void (*const __destroy)(void*);
1968
1969 // True if this is the null policy (no value).
1970 const bool __is_null;
1971
1972 // The target type. May be null if RTTI is disabled.
1973 const std::type_info* const __type_info;
1974
1975 // Returns a pointer to a static policy object suitable for the functor
1976 // type.
1977 template <typename _Fun>
1978 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1979 {
1980 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1981 }
1982
1983 _LIBCPP_INLINE_VISIBILITY
1984 static const __policy* __create_empty()
1985 {
1986 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1987 true,
1988#ifndef _LIBCPP_NO_RTTI
1989 &typeid(void)
1990#else
1991 nullptr
1992#endif
1993 };
1994 return &__policy_;
1995 }
1996
1997 private:
1998 template <typename _Fun> static void* __large_clone(const void* __s)
1999 {
2000 const _Fun* __f = static_cast<const _Fun*>(__s);
2001 return __f->__clone();
2002 }
2003
Eric Fiselier74ebee62019-06-08 01:31:19 +00002004 template <typename _Fun>
2005 static void __large_destroy(void* __s) {
2006 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002007 }
2008
2009 template <typename _Fun>
2010 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002011 __choose_policy(/* is_small = */ false_type) {
2012 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2013 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002014#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002015 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002016#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002017 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002018#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002019 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002020 return &__policy_;
2021 }
2022
2023 template <typename _Fun>
2024 _LIBCPP_INLINE_VISIBILITY static const __policy*
2025 __choose_policy(/* is_small = */ true_type)
2026 {
2027 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2028 nullptr, nullptr, false,
2029#ifndef _LIBCPP_NO_RTTI
2030 &typeid(typename _Fun::_Target)
2031#else
2032 nullptr
2033#endif
2034 };
2035 return &__policy_;
2036 }
2037};
2038
2039// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2040// faster for types that can be passed in registers.
2041template <typename _Tp>
2042using __fast_forward =
2043 typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2044
2045// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2046
2047template <class _Fp> struct __policy_invoker;
2048
2049template <class _Rp, class... _ArgTypes>
2050struct __policy_invoker<_Rp(_ArgTypes...)>
2051{
2052 typedef _Rp (*__Call)(const __policy_storage*,
2053 __fast_forward<_ArgTypes>...);
2054
2055 __Call __call_;
2056
2057 // Creates an invoker that throws bad_function_call.
2058 _LIBCPP_INLINE_VISIBILITY
2059 __policy_invoker() : __call_(&__call_empty) {}
2060
2061 // Creates an invoker that calls the given instance of __func.
2062 template <typename _Fun>
2063 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2064 {
2065 return __policy_invoker(&__call_impl<_Fun>);
2066 }
2067
2068 private:
2069 _LIBCPP_INLINE_VISIBILITY
2070 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2071
2072 static _Rp __call_empty(const __policy_storage*,
2073 __fast_forward<_ArgTypes>...)
2074 {
2075 __throw_bad_function_call();
2076 }
2077
2078 template <typename _Fun>
2079 static _Rp __call_impl(const __policy_storage* __buf,
2080 __fast_forward<_ArgTypes>... __args)
2081 {
2082 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2083 ? &__buf->__small
2084 : __buf->__large);
2085 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2086 }
2087};
2088
2089// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2090// copyable functor.
2091
2092template <class _Fp> class __policy_func;
2093
2094template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2095{
2096 // Inline storage for small objects.
2097 __policy_storage __buf_;
2098
2099 // Calls the value stored in __buf_. This could technically be part of
2100 // policy, but storing it here eliminates a level of indirection inside
2101 // operator().
2102 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2103 __invoker __invoker_;
2104
2105 // The policy that describes how to move / copy / destroy __buf_. Never
2106 // null, even if the function is empty.
2107 const __policy* __policy_;
2108
2109 public:
2110 _LIBCPP_INLINE_VISIBILITY
2111 __policy_func() : __policy_(__policy::__create_empty()) {}
2112
2113 template <class _Fp, class _Alloc>
2114 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2115 : __policy_(__policy::__create_empty())
2116 {
2117 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2118 typedef allocator_traits<_Alloc> __alloc_traits;
2119 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2120 _FunAlloc;
2121
2122 if (__function::__not_null(__f))
2123 {
2124 __invoker_ = __invoker::template __create<_Fun>();
2125 __policy_ = __policy::__create<_Fun>();
2126
2127 _FunAlloc __af(__a);
2128 if (__use_small_storage<_Fun>())
2129 {
2130 ::new ((void*)&__buf_.__small)
2131 _Fun(_VSTD::move(__f), _Alloc(__af));
2132 }
2133 else
2134 {
2135 typedef __allocator_destructor<_FunAlloc> _Dp;
2136 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2137 ::new ((void*)__hold.get())
2138 _Fun(_VSTD::move(__f), _Alloc(__af));
2139 __buf_.__large = __hold.release();
2140 }
2141 }
2142 }
2143
Eric Fiselier74ebee62019-06-08 01:31:19 +00002144 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2145 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2146 : __policy_(__policy::__create_empty()) {
2147 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2148
2149 if (__function::__not_null(__f)) {
2150 __invoker_ = __invoker::template __create<_Fun>();
2151 __policy_ = __policy::__create<_Fun>();
2152 if (__use_small_storage<_Fun>()) {
2153 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2154 } else {
2155 __builtin_new_allocator::__holder_t __hold =
2156 __builtin_new_allocator::__allocate_type<_Fun>(1);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002157 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
Eric Fiselier74ebee62019-06-08 01:31:19 +00002158 (void)__hold.release();
2159 }
2160 }
2161 }
2162
Eric Fiselierf2e64362018-12-11 00:14:34 +00002163 _LIBCPP_INLINE_VISIBILITY
2164 __policy_func(const __policy_func& __f)
2165 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2166 __policy_(__f.__policy_)
2167 {
2168 if (__policy_->__clone)
2169 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2170 }
2171
2172 _LIBCPP_INLINE_VISIBILITY
2173 __policy_func(__policy_func&& __f)
2174 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2175 __policy_(__f.__policy_)
2176 {
2177 if (__policy_->__destroy)
2178 {
2179 __f.__policy_ = __policy::__create_empty();
2180 __f.__invoker_ = __invoker();
2181 }
2182 }
2183
2184 _LIBCPP_INLINE_VISIBILITY
2185 ~__policy_func()
2186 {
2187 if (__policy_->__destroy)
2188 __policy_->__destroy(__buf_.__large);
2189 }
2190
2191 _LIBCPP_INLINE_VISIBILITY
2192 __policy_func& operator=(__policy_func&& __f)
2193 {
2194 *this = nullptr;
2195 __buf_ = __f.__buf_;
2196 __invoker_ = __f.__invoker_;
2197 __policy_ = __f.__policy_;
2198 __f.__policy_ = __policy::__create_empty();
2199 __f.__invoker_ = __invoker();
2200 return *this;
2201 }
2202
2203 _LIBCPP_INLINE_VISIBILITY
2204 __policy_func& operator=(nullptr_t)
2205 {
2206 const __policy* __p = __policy_;
2207 __policy_ = __policy::__create_empty();
2208 __invoker_ = __invoker();
2209 if (__p->__destroy)
2210 __p->__destroy(__buf_.__large);
2211 return *this;
2212 }
2213
2214 _LIBCPP_INLINE_VISIBILITY
2215 _Rp operator()(_ArgTypes&&... __args) const
2216 {
2217 return __invoker_.__call_(_VSTD::addressof(__buf_),
2218 _VSTD::forward<_ArgTypes>(__args)...);
2219 }
2220
2221 _LIBCPP_INLINE_VISIBILITY
2222 void swap(__policy_func& __f)
2223 {
2224 _VSTD::swap(__invoker_, __f.__invoker_);
2225 _VSTD::swap(__policy_, __f.__policy_);
2226 _VSTD::swap(__buf_, __f.__buf_);
2227 }
2228
2229 _LIBCPP_INLINE_VISIBILITY
2230 explicit operator bool() const _NOEXCEPT
2231 {
2232 return !__policy_->__is_null;
2233 }
2234
2235#ifndef _LIBCPP_NO_RTTI
2236 _LIBCPP_INLINE_VISIBILITY
2237 const std::type_info& target_type() const _NOEXCEPT
2238 {
2239 return *__policy_->__type_info;
2240 }
2241
2242 template <typename _Tp>
2243 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2244 {
2245 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2246 return nullptr;
2247 if (__policy_->__clone) // Out of line storage.
2248 return reinterpret_cast<const _Tp*>(__buf_.__large);
2249 else
2250 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2251 }
2252#endif // _LIBCPP_NO_RTTI
2253};
2254
Louis Dionne3a632922020-04-23 16:47:52 -04002255#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
Louis Dionnee2391d72020-04-22 13:58:17 -04002256
Louis Dionne91cf4442020-07-31 12:56:36 -04002257extern "C" void *_Block_copy(const void *);
2258extern "C" void _Block_release(const void *);
2259
Louis Dionnee2391d72020-04-22 13:58:17 -04002260template<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)
Louis Dionne91cf4442020-07-31 12:56:36 -04002270 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002271 { }
2272
2273 // [TODO] add && to save on a retain
2274
2275 _LIBCPP_INLINE_VISIBILITY
2276 explicit __func(__block_type __f, const _Alloc& /* unused */)
Louis Dionne91cf4442020-07-31 12:56:36 -04002277 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002278 { }
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 {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002289 ::new ((void*)__p) __func(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002290 }
2291
2292 virtual void destroy() _NOEXCEPT {
2293 if (__f_)
Louis Dionne91cf4442020-07-31 12:56:36 -04002294 _Block_release(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002295 __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) {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05002306 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
Louis Dionnee2391d72020-04-22 13:58:17 -04002307 }
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>,
zoecarver3e722f22020-05-19 17:15:28 -07002341 __invokable<_Fp, _ArgTypes...>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002342 >::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 ||
zoecarver3e722f22020-05-19 17:15:28 -07002348 is_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
Howard Hinnantc834c512011-11-29 18:15:50 +00002349 _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>
zoecarver3e722f22020-05-19 17:15:28 -07002358 using _EnableIfLValueCallable = 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;
zoecarver3e722f22020-05-19 17:15:28 -07002369 template<class _Fp, class = _EnableIfLValueCallable<_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&&);
zoecarver3e722f22020-05-19 17:15:28 -07002383 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_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;
zoecarver3e722f22020-05-19 17:15:28 -07002390 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002391 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{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002521 __f_ = _VSTD::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)
Logan Smith4528b5f2020-05-07 11:54:25 -04003053 return _VSTD::make_pair(__first1, __first1); // Everything matches an empty sequence
Marshall Clowa40686b2018-01-08 19:18:00 +00003054 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
Logan Smith4528b5f2020-05-07 11:54:25 -04003060 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003061 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)
Logan Smith4528b5f2020-05-07 11:54:25 -04003071 return _VSTD::make_pair(__first1, __m1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003072 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Logan Smith4528b5f2020-05-07 11:54:25 -04003073 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003074 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)
Logan Smith4528b5f2020-05-07 11:54:25 -04003095 return _VSTD::make_pair(__first1, __first1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003096 const _D1 __len1 = __last1 - __first1;
3097 if (__len1 < __len2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003098 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003099 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)
Logan Smith4528b5f2020-05-07 11:54:25 -04003106 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003107 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)
Logan Smith4528b5f2020-05-07 11:54:25 -04003117 return _VSTD::make_pair(__first1, __first1 + __len2);
Marshall Clowa40686b2018-01-08 19:18:00 +00003118 ++__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>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003166inline typename _Container::size_type
3167__libcpp_erase_if_container(_Container& __c, _Predicate __pred) {
3168 typename _Container::size_type __old_size = __c.size();
3169
3170 const typename _Container::iterator __last = __c.end();
3171 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
3172 if (__pred(*__iter))
3173 __iter = __c.erase(__iter);
3174 else
3175 ++__iter;
3176 }
3177
3178 return __old_size - __c.size();
Marshall Clow29b53f22018-12-14 18:49:35 +00003179}
3180
Howard Hinnantc51e1022010-05-11 19:42:16 +00003181_LIBCPP_END_NAMESPACE_STD
3182
3183#endif // _LIBCPP_FUNCTIONAL