blob: bcd74a9ee597200392d24253742b1591bd48b346 [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
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000443// Null pointer comparisons:
444template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000445 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000447template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000448 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000450template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000451 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000452
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000453template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000454 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000456// specialized algorithms:
457template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000458 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459
460template <class T> struct hash;
461
462template <> struct hash<bool>;
463template <> struct hash<char>;
464template <> struct hash<signed char>;
465template <> struct hash<unsigned char>;
466template <> struct hash<char16_t>;
467template <> struct hash<char32_t>;
468template <> struct hash<wchar_t>;
469template <> struct hash<short>;
470template <> struct hash<unsigned short>;
471template <> struct hash<int>;
472template <> struct hash<unsigned int>;
473template <> struct hash<long>;
474template <> struct hash<long long>;
475template <> struct hash<unsigned long>;
476template <> struct hash<unsigned long long>;
477
478template <> struct hash<float>;
479template <> struct hash<double>;
480template <> struct hash<long double>;
481
482template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000483template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000484
485} // std
486
487POLICY: For non-variadic implementations, the number of arguments is limited
488 to 3. It is hoped that the need for non-variadic implementations
489 will be minimal.
490
491*/
492
493#include <__config>
494#include <type_traits>
495#include <typeinfo>
496#include <exception>
497#include <memory>
498#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000499#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000500#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501
502#include <__functional_base>
503
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000504#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000506#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507
508_LIBCPP_BEGIN_NAMESPACE_STD
509
Marshall Clow974bae22013-07-29 14:21:53 +0000510#if _LIBCPP_STD_VER > 11
511template <class _Tp = void>
512#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000513template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000514#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000515struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516{
Marshall Clowd18ee652013-09-28 19:06:12 +0000517 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
518 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519 {return __x + __y;}
520};
521
Marshall Clow974bae22013-07-29 14:21:53 +0000522#if _LIBCPP_STD_VER > 11
523template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000524struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000525{
526 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000527 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
528 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000529 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
530 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
531 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000532 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000533};
534#endif
535
536
537#if _LIBCPP_STD_VER > 11
538template <class _Tp = void>
539#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000541#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000542struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543{
Marshall Clowd18ee652013-09-28 19:06:12 +0000544 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
545 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546 {return __x - __y;}
547};
548
Marshall Clow974bae22013-07-29 14:21:53 +0000549#if _LIBCPP_STD_VER > 11
550template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000551struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000552{
553 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000554 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
555 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000556 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
557 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
558 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000559 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000560};
561#endif
562
563
564#if _LIBCPP_STD_VER > 11
565template <class _Tp = void>
566#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000568#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000569struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570{
Marshall Clowd18ee652013-09-28 19:06:12 +0000571 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
572 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573 {return __x * __y;}
574};
575
Marshall Clow974bae22013-07-29 14:21:53 +0000576#if _LIBCPP_STD_VER > 11
577template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000578struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000579{
580 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000581 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
582 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000583 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
584 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
585 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000586 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000587};
588#endif
589
590
591#if _LIBCPP_STD_VER > 11
592template <class _Tp = void>
593#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000595#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000596struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597{
Marshall Clowd18ee652013-09-28 19:06:12 +0000598 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
599 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600 {return __x / __y;}
601};
602
Marshall Clow974bae22013-07-29 14:21:53 +0000603#if _LIBCPP_STD_VER > 11
604template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000605struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000606{
607 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000608 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
609 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000610 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
611 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
612 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000613 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000614};
615#endif
616
617
618#if _LIBCPP_STD_VER > 11
619template <class _Tp = void>
620#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000622#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000623struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624{
Marshall Clowd18ee652013-09-28 19:06:12 +0000625 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
626 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627 {return __x % __y;}
628};
629
Marshall Clow974bae22013-07-29 14:21:53 +0000630#if _LIBCPP_STD_VER > 11
631template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000632struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000633{
634 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000635 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
636 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000637 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
638 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
639 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000640 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000641};
642#endif
643
644
645#if _LIBCPP_STD_VER > 11
646template <class _Tp = void>
647#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000649#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000650struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651{
Marshall Clowd18ee652013-09-28 19:06:12 +0000652 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
653 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654 {return -__x;}
655};
656
Marshall Clow974bae22013-07-29 14:21:53 +0000657#if _LIBCPP_STD_VER > 11
658template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000659struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000660{
661 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000662 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
663 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000664 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
665 -> decltype (- _VSTD::forward<_Tp>(__x))
666 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000667 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000668};
669#endif
670
671
672#if _LIBCPP_STD_VER > 11
673template <class _Tp = void>
674#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000676#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000677struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678{
Marshall Clowd18ee652013-09-28 19:06:12 +0000679 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
680 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681 {return __x == __y;}
682};
683
Marshall Clow974bae22013-07-29 14:21:53 +0000684#if _LIBCPP_STD_VER > 11
685template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000686struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000687{
Marshall Clowd18ee652013-09-28 19:06:12 +0000688 template <class _T1, class _T2>
689 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000690 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000691 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
692 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
693 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000694 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000695};
696#endif
697
698
699#if _LIBCPP_STD_VER > 11
700template <class _Tp = void>
701#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000703#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000704struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705{
Marshall Clowd18ee652013-09-28 19:06:12 +0000706 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
707 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708 {return __x != __y;}
709};
710
Marshall Clow974bae22013-07-29 14:21:53 +0000711#if _LIBCPP_STD_VER > 11
712template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000713struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000714{
Marshall Clowd18ee652013-09-28 19:06:12 +0000715 template <class _T1, class _T2>
716 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000717 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000718 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
719 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
720 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000721 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000722};
723#endif
724
725
726#if _LIBCPP_STD_VER > 11
727template <class _Tp = void>
728#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000730#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000731struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732{
Marshall Clowd18ee652013-09-28 19:06:12 +0000733 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
734 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 {return __x > __y;}
736};
737
Marshall Clow974bae22013-07-29 14:21:53 +0000738#if _LIBCPP_STD_VER > 11
739template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000740struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000741{
Marshall Clowd18ee652013-09-28 19:06:12 +0000742 template <class _T1, class _T2>
743 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000744 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000745 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
746 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
747 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000748 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000749};
750#endif
751
752
Howard Hinnantb17caf92012-02-21 21:02:58 +0000753// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754
Marshall Clow974bae22013-07-29 14:21:53 +0000755#if _LIBCPP_STD_VER > 11
756template <class _Tp = void>
757#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000759#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000760struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761{
Marshall Clowd18ee652013-09-28 19:06:12 +0000762 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
763 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 {return __x >= __y;}
765};
766
Marshall Clow974bae22013-07-29 14:21:53 +0000767#if _LIBCPP_STD_VER > 11
768template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000769struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000770{
Marshall Clowd18ee652013-09-28 19:06:12 +0000771 template <class _T1, class _T2>
772 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000773 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000774 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
775 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
776 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000777 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000778};
779#endif
780
781
782#if _LIBCPP_STD_VER > 11
783template <class _Tp = void>
784#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000786#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000787struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788{
Marshall Clowd18ee652013-09-28 19:06:12 +0000789 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
790 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791 {return __x <= __y;}
792};
793
Marshall Clow974bae22013-07-29 14:21:53 +0000794#if _LIBCPP_STD_VER > 11
795template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000796struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000797{
Marshall Clowd18ee652013-09-28 19:06:12 +0000798 template <class _T1, class _T2>
799 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000800 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000801 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
802 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
803 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000804 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000805};
806#endif
807
808
809#if _LIBCPP_STD_VER > 11
810template <class _Tp = void>
811#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000813#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000814struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815{
Marshall Clowd18ee652013-09-28 19:06:12 +0000816 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
817 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818 {return __x && __y;}
819};
820
Marshall Clow974bae22013-07-29 14:21:53 +0000821#if _LIBCPP_STD_VER > 11
822template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000823struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000824{
Marshall Clowd18ee652013-09-28 19:06:12 +0000825 template <class _T1, class _T2>
826 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000827 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000828 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
829 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
830 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000831 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000832};
833#endif
834
835
836#if _LIBCPP_STD_VER > 11
837template <class _Tp = void>
838#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000840#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000841struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842{
Marshall Clowd18ee652013-09-28 19:06:12 +0000843 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
844 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845 {return __x || __y;}
846};
847
Marshall Clow974bae22013-07-29 14:21:53 +0000848#if _LIBCPP_STD_VER > 11
849template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000850struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000851{
Marshall Clowd18ee652013-09-28 19:06:12 +0000852 template <class _T1, class _T2>
853 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000854 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000855 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
856 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
857 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000858 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000859};
860#endif
861
862
863#if _LIBCPP_STD_VER > 11
864template <class _Tp = void>
865#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000867#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000868struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869{
Marshall Clowd18ee652013-09-28 19:06:12 +0000870 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
871 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872 {return !__x;}
873};
874
Marshall Clow974bae22013-07-29 14:21:53 +0000875#if _LIBCPP_STD_VER > 11
876template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000877struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000878{
879 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000880 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
881 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000882 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
883 -> decltype (!_VSTD::forward<_Tp>(__x))
884 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000885 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000886};
887#endif
888
889
890#if _LIBCPP_STD_VER > 11
891template <class _Tp = void>
892#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000894#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000895struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896{
Marshall Clowd18ee652013-09-28 19:06:12 +0000897 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
898 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 {return __x & __y;}
900};
901
Marshall Clow974bae22013-07-29 14:21:53 +0000902#if _LIBCPP_STD_VER > 11
903template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000904struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000905{
Marshall Clowd18ee652013-09-28 19:06:12 +0000906 template <class _T1, class _T2>
907 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000908 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000909 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
910 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
911 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000912 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000913};
914#endif
915
916
917#if _LIBCPP_STD_VER > 11
918template <class _Tp = void>
919#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000921#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000922struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923{
Marshall Clowd18ee652013-09-28 19:06:12 +0000924 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
925 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926 {return __x | __y;}
927};
928
Marshall Clow974bae22013-07-29 14:21:53 +0000929#if _LIBCPP_STD_VER > 11
930template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000931struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000932{
Marshall Clowd18ee652013-09-28 19:06:12 +0000933 template <class _T1, class _T2>
934 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000935 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000936 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
937 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
938 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000939 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000940};
941#endif
942
943
944#if _LIBCPP_STD_VER > 11
945template <class _Tp = void>
946#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000948#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000949struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950{
Marshall Clowd18ee652013-09-28 19:06:12 +0000951 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
952 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953 {return __x ^ __y;}
954};
955
Marshall Clow974bae22013-07-29 14:21:53 +0000956#if _LIBCPP_STD_VER > 11
957template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000958struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000959{
Marshall Clowd18ee652013-09-28 19:06:12 +0000960 template <class _T1, class _T2>
961 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000962 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000963 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
964 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
965 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000966 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000967};
968#endif
969
970
971#if _LIBCPP_STD_VER > 11
972template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000973struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000974{
Marshall Clowd18ee652013-09-28 19:06:12 +0000975 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
976 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000977 {return ~__x;}
978};
979
980template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000981struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000982{
983 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000984 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
985 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000986 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
987 -> decltype (~_VSTD::forward<_Tp>(__x))
988 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000989 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000990};
991#endif
992
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000994class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995 : public unary_function<typename _Predicate::argument_type, bool>
996{
997 _Predicate __pred_;
998public:
Marshall Clowd18ee652013-09-28 19:06:12 +0000999 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1000 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001002 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1003 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 {return !__pred_(__x);}
1005};
1006
1007template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001008_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009unary_negate<_Predicate>
1010not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1011
1012template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001013class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 : public binary_function<typename _Predicate::first_argument_type,
1015 typename _Predicate::second_argument_type,
1016 bool>
1017{
1018 _Predicate __pred_;
1019public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001020 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001021 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1022
1023 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1024 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 const typename _Predicate::second_argument_type& __y) const
1026 {return !__pred_(__x, __y);}
1027};
1028
1029template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001030_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031binary_negate<_Predicate>
1032not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1033
Marshall Clow26a027c2017-04-13 18:25:32 +00001034#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001036class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 : public unary_function<typename __Operation::second_argument_type,
1038 typename __Operation::result_type>
1039{
1040protected:
1041 __Operation op;
1042 typename __Operation::first_argument_type value;
1043public:
1044 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1045 const typename __Operation::first_argument_type __y)
1046 : op(__x), value(__y) {}
1047 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1048 (typename __Operation::second_argument_type& __x) const
1049 {return op(value, __x);}
1050 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1051 (const typename __Operation::second_argument_type& __x) const
1052 {return op(value, __x);}
1053};
1054
1055template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001056_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057binder1st<__Operation>
1058bind1st(const __Operation& __op, const _Tp& __x)
1059 {return binder1st<__Operation>(__op, __x);}
1060
1061template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001062class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 : public unary_function<typename __Operation::first_argument_type,
1064 typename __Operation::result_type>
1065{
1066protected:
1067 __Operation op;
1068 typename __Operation::second_argument_type value;
1069public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1072 : op(__x), value(__y) {}
1073 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1074 ( typename __Operation::first_argument_type& __x) const
1075 {return op(__x, value);}
1076 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1077 (const typename __Operation::first_argument_type& __x) const
1078 {return op(__x, value);}
1079};
1080
1081template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001082_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083binder2nd<__Operation>
1084bind2nd(const __Operation& __op, const _Tp& __x)
1085 {return binder2nd<__Operation>(__op, __x);}
1086
1087template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001088class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001089 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090{
1091 _Result (*__f_)(_Arg);
1092public:
1093 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1094 : __f_(__f) {}
1095 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1096 {return __f_(__x);}
1097};
1098
1099template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001100_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101pointer_to_unary_function<_Arg,_Result>
1102ptr_fun(_Result (*__f)(_Arg))
1103 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1104
1105template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001106class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001107 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108{
1109 _Result (*__f_)(_Arg1, _Arg2);
1110public:
1111 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1112 : __f_(__f) {}
1113 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1114 {return __f_(__x, __y);}
1115};
1116
1117template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001118_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119pointer_to_binary_function<_Arg1,_Arg2,_Result>
1120ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1121 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1122
1123template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001124class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1125 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126{
1127 _Sp (_Tp::*__p_)();
1128public:
1129 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1130 : __p_(__p) {}
1131 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1132 {return (__p->*__p_)();}
1133};
1134
1135template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001136class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1137 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138{
1139 _Sp (_Tp::*__p_)(_Ap);
1140public:
1141 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1142 : __p_(__p) {}
1143 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1144 {return (__p->*__p_)(__x);}
1145};
1146
1147template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001148_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149mem_fun_t<_Sp,_Tp>
1150mem_fun(_Sp (_Tp::*__f)())
1151 {return mem_fun_t<_Sp,_Tp>(__f);}
1152
1153template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001154_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155mem_fun1_t<_Sp,_Tp,_Ap>
1156mem_fun(_Sp (_Tp::*__f)(_Ap))
1157 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1158
1159template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001160class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1161 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162{
1163 _Sp (_Tp::*__p_)();
1164public:
1165 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1166 : __p_(__p) {}
1167 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1168 {return (__p.*__p_)();}
1169};
1170
1171template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001172class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1173 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174{
1175 _Sp (_Tp::*__p_)(_Ap);
1176public:
1177 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1178 : __p_(__p) {}
1179 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1180 {return (__p.*__p_)(__x);}
1181};
1182
1183template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001184_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185mem_fun_ref_t<_Sp,_Tp>
1186mem_fun_ref(_Sp (_Tp::*__f)())
1187 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1188
1189template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001190_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191mem_fun1_ref_t<_Sp,_Tp,_Ap>
1192mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1193 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1194
1195template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001196class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1197 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198{
1199 _Sp (_Tp::*__p_)() const;
1200public:
1201 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1202 : __p_(__p) {}
1203 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1204 {return (__p->*__p_)();}
1205};
1206
1207template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001208class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1209 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210{
1211 _Sp (_Tp::*__p_)(_Ap) const;
1212public:
1213 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1214 : __p_(__p) {}
1215 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1216 {return (__p->*__p_)(__x);}
1217};
1218
1219template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001220_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221const_mem_fun_t<_Sp,_Tp>
1222mem_fun(_Sp (_Tp::*__f)() const)
1223 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1224
1225template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001226_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227const_mem_fun1_t<_Sp,_Tp,_Ap>
1228mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1229 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1230
1231template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001232class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1233 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234{
1235 _Sp (_Tp::*__p_)() const;
1236public:
1237 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1238 : __p_(__p) {}
1239 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1240 {return (__p.*__p_)();}
1241};
1242
1243template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001244class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001245 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246{
1247 _Sp (_Tp::*__p_)(_Ap) const;
1248public:
1249 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1250 : __p_(__p) {}
1251 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1252 {return (__p.*__p_)(__x);}
1253};
1254
1255template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001256_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257const_mem_fun_ref_t<_Sp,_Tp>
1258mem_fun_ref(_Sp (_Tp::*__f)() const)
1259 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1260
1261template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001262_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1264mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1265 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001266#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001268////////////////////////////////////////////////////////////////////////////////
1269// MEMFUN
1270//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272template <class _Tp>
1273class __mem_fn
1274 : public __weak_result_type<_Tp>
1275{
1276public:
1277 // types
1278 typedef _Tp type;
1279private:
1280 type __f_;
1281
1282public:
Marshall Clowad8ff212015-10-25 20:12:16 +00001283 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001285#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286 // invoke
1287 template <class... _ArgTypes>
Eric Fiselier2cc48332015-07-22 22:43:27 +00001288 _LIBCPP_INLINE_VISIBILITY
1289 typename __invoke_return<type, _ArgTypes...>::type
1290 operator() (_ArgTypes&&... __args) const {
1291 return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
1292 }
1293#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001294
1295 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001296 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001297 typename __invoke_return0<type, _A0>::type
1298 operator() (_A0& __a0) const {
1299 return __invoke(__f_, __a0);
1300 }
1301
Eric Fiselierce1813a2015-08-26 20:15:02 +00001302 template <class _A0>
1303 _LIBCPP_INLINE_VISIBILITY
1304 typename __invoke_return0<type, _A0 const>::type
1305 operator() (_A0 const& __a0) const {
1306 return __invoke(__f_, __a0);
1307 }
1308
Eric Fiselier2cc48332015-07-22 22:43:27 +00001309 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001310 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001311 typename __invoke_return1<type, _A0, _A1>::type
1312 operator() (_A0& __a0, _A1& __a1) const {
1313 return __invoke(__f_, __a0, __a1);
1314 }
1315
Eric Fiselierce1813a2015-08-26 20:15:02 +00001316 template <class _A0, class _A1>
1317 _LIBCPP_INLINE_VISIBILITY
1318 typename __invoke_return1<type, _A0 const, _A1>::type
1319 operator() (_A0 const& __a0, _A1& __a1) const {
1320 return __invoke(__f_, __a0, __a1);
1321 }
1322
1323 template <class _A0, class _A1>
1324 _LIBCPP_INLINE_VISIBILITY
1325 typename __invoke_return1<type, _A0, _A1 const>::type
1326 operator() (_A0& __a0, _A1 const& __a1) const {
1327 return __invoke(__f_, __a0, __a1);
1328 }
1329
1330 template <class _A0, class _A1>
1331 _LIBCPP_INLINE_VISIBILITY
1332 typename __invoke_return1<type, _A0 const, _A1 const>::type
1333 operator() (_A0 const& __a0, _A1 const& __a1) const {
1334 return __invoke(__f_, __a0, __a1);
1335 }
1336
Eric Fiselier2cc48332015-07-22 22:43:27 +00001337 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001338 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001339 typename __invoke_return2<type, _A0, _A1, _A2>::type
1340 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
1341 return __invoke(__f_, __a0, __a1, __a2);
1342 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001343
1344 template <class _A0, class _A1, class _A2>
1345 _LIBCPP_INLINE_VISIBILITY
1346 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1347 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
1348 return __invoke(__f_, __a0, __a1, __a2);
1349 }
1350
1351 template <class _A0, class _A1, class _A2>
1352 _LIBCPP_INLINE_VISIBILITY
1353 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1354 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
1355 return __invoke(__f_, __a0, __a1, __a2);
1356 }
1357
1358 template <class _A0, class _A1, class _A2>
1359 _LIBCPP_INLINE_VISIBILITY
1360 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1361 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
1362 return __invoke(__f_, __a0, __a1, __a2);
1363 }
1364
1365 template <class _A0, class _A1, class _A2>
1366 _LIBCPP_INLINE_VISIBILITY
1367 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1368 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
1369 return __invoke(__f_, __a0, __a1, __a2);
1370 }
1371
1372 template <class _A0, class _A1, class _A2>
1373 _LIBCPP_INLINE_VISIBILITY
1374 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1375 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
1376 return __invoke(__f_, __a0, __a1, __a2);
1377 }
1378
1379 template <class _A0, class _A1, class _A2>
1380 _LIBCPP_INLINE_VISIBILITY
1381 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1382 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
1383 return __invoke(__f_, __a0, __a1, __a2);
1384 }
1385
1386 template <class _A0, class _A1, class _A2>
1387 _LIBCPP_INLINE_VISIBILITY
1388 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1389 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
1390 return __invoke(__f_, __a0, __a1, __a2);
1391 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001392#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393};
1394
Howard Hinnantc834c512011-11-29 18:15:50 +00001395template<class _Rp, class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001397__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001398mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399{
Howard Hinnantc834c512011-11-29 18:15:50 +00001400 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401}
1402
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001403////////////////////////////////////////////////////////////////////////////////
1404// FUNCTION
1405//==============================================================================
1406
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407// bad_function_call
1408
Howard Hinnant4ff57432010-09-21 22:55:27 +00001409class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 : public exception
1411{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001412#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1413public:
1414 virtual ~bad_function_call() _NOEXCEPT;
1415
1416 virtual const char* what() const _NOEXCEPT;
1417#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418};
1419
Louis Dionne16fe2952018-07-11 23:14:33 +00001420_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001421void __throw_bad_function_call()
1422{
1423#ifndef _LIBCPP_NO_EXCEPTIONS
1424 throw bad_function_call();
1425#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001426 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001427#endif
1428}
1429
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001430template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431
1432namespace __function
1433{
1434
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001435template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436struct __maybe_derive_from_unary_function
1437{
1438};
1439
Howard Hinnantc834c512011-11-29 18:15:50 +00001440template<class _Rp, class _A1>
1441struct __maybe_derive_from_unary_function<_Rp(_A1)>
1442 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443{
1444};
1445
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001446template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447struct __maybe_derive_from_binary_function
1448{
1449};
1450
Howard Hinnantc834c512011-11-29 18:15:50 +00001451template<class _Rp, class _A1, class _A2>
1452struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1453 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454{
1455};
1456
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001457template <class _Fp>
1458_LIBCPP_INLINE_VISIBILITY
1459bool __not_null(_Fp const&) { return true; }
1460
1461template <class _Fp>
1462_LIBCPP_INLINE_VISIBILITY
1463bool __not_null(_Fp* __ptr) { return __ptr; }
1464
1465template <class _Ret, class _Class>
1466_LIBCPP_INLINE_VISIBILITY
1467bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1468
1469template <class _Fp>
1470_LIBCPP_INLINE_VISIBILITY
1471bool __not_null(function<_Fp> const& __f) { return !!__f; }
1472
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001473} // namespace __function
1474
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001475#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001476
1477namespace __function {
1478
Eric Fiselier125798e2018-12-10 18:14:09 +00001479// __alloc_func holds a functor and an allocator.
1480
1481template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001482template <class _Fp, class _FB>
1483class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001484
1485template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1486class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1487{
1488 __compressed_pair<_Fp, _Ap> __f_;
1489
1490 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001491 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1492 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001493
1494 _LIBCPP_INLINE_VISIBILITY
1495 const _Target& __target() const { return __f_.first(); }
1496
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001497 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001498 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001499 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001500
1501 _LIBCPP_INLINE_VISIBILITY
1502 explicit __alloc_func(_Target&& __f)
1503 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1504 _VSTD::forward_as_tuple())
1505 {
1506 }
1507
1508 _LIBCPP_INLINE_VISIBILITY
1509 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1510 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1511 _VSTD::forward_as_tuple(__a))
1512 {
1513 }
1514
1515 _LIBCPP_INLINE_VISIBILITY
1516 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1517 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1518 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1519 {
1520 }
1521
1522 _LIBCPP_INLINE_VISIBILITY
1523 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1524 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1525 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1526 {
1527 }
1528
1529 _LIBCPP_INLINE_VISIBILITY
1530 _Rp operator()(_ArgTypes&&... __arg)
1531 {
1532 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1533 return _Invoker::__call(__f_.first(),
1534 _VSTD::forward<_ArgTypes>(__arg)...);
1535 }
1536
1537 _LIBCPP_INLINE_VISIBILITY
1538 __alloc_func* __clone() const
1539 {
1540 typedef allocator_traits<_Alloc> __alloc_traits;
1541 typedef
1542 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1543 _AA;
1544 _AA __a(__f_.second());
1545 typedef __allocator_destructor<_AA> _Dp;
1546 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1547 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1548 return __hold.release();
1549 }
1550
1551 _LIBCPP_INLINE_VISIBILITY
1552 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001553
1554 static void __destroy_and_delete(__alloc_func* __f) {
1555 typedef allocator_traits<_Alloc> __alloc_traits;
1556 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1557 _FunAlloc;
1558 _FunAlloc __a(__f->__get_allocator());
1559 __f->destroy();
1560 __a.deallocate(__f, 1);
1561 }
1562};
1563
1564template <class _Fp, class _Rp, class... _ArgTypes>
1565class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1566 _Fp __f_;
1567
1568public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001569 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001570
1571 _LIBCPP_INLINE_VISIBILITY
1572 const _Target& __target() const { return __f_; }
1573
1574 _LIBCPP_INLINE_VISIBILITY
1575 explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
1576
1577 _LIBCPP_INLINE_VISIBILITY
1578 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1579
1580 _LIBCPP_INLINE_VISIBILITY
1581 _Rp operator()(_ArgTypes&&... __arg) {
1582 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1583 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1584 }
1585
1586 _LIBCPP_INLINE_VISIBILITY
1587 __default_alloc_func* __clone() const {
1588 __builtin_new_allocator::__holder_t __hold =
1589 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1590 __default_alloc_func* __res =
1591 ::new (__hold.get()) __default_alloc_func(__f_);
1592 (void)__hold.release();
1593 return __res;
1594 }
1595
1596 _LIBCPP_INLINE_VISIBILITY
1597 void destroy() _NOEXCEPT { __f_.~_Target(); }
1598
1599 static void __destroy_and_delete(__default_alloc_func* __f) {
1600 __f->destroy();
1601 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1602 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001603};
1604
1605// __base provides an abstract interface for copyable functors.
1606
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607template<class _Fp> class __base;
1608
Howard Hinnantc834c512011-11-29 18:15:50 +00001609template<class _Rp, class ..._ArgTypes>
1610class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611{
1612 __base(const __base&);
1613 __base& operator=(const __base&);
1614public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001615 _LIBCPP_INLINE_VISIBILITY __base() {}
1616 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617 virtual __base* __clone() const = 0;
1618 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001619 virtual void destroy() _NOEXCEPT = 0;
1620 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001621 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001622#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001623 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1624 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001625#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626};
1627
Eric Fiselier125798e2018-12-10 18:14:09 +00001628// __func implements __base for a given functor type.
1629
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630template<class _FD, class _Alloc, class _FB> class __func;
1631
Howard Hinnantc834c512011-11-29 18:15:50 +00001632template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1633class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1634 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635{
Eric Fiselier125798e2018-12-10 18:14:09 +00001636 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001639 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001640 : __f_(_VSTD::move(__f)) {}
1641
Howard Hinnant4ff57432010-09-21 22:55:27 +00001642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001643 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001644 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001645
1646 _LIBCPP_INLINE_VISIBILITY
1647 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001648 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001649
1650 _LIBCPP_INLINE_VISIBILITY
1651 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001652 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1653
Howard Hinnantc834c512011-11-29 18:15:50 +00001654 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1655 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001656 virtual void destroy() _NOEXCEPT;
1657 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001658 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001659#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001660 virtual const void* target(const type_info&) const _NOEXCEPT;
1661 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001662#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663};
1664
Howard Hinnantc834c512011-11-29 18:15:50 +00001665template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1666__base<_Rp(_ArgTypes...)>*
1667__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001669 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001670 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001671 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001672 typedef __allocator_destructor<_Ap> _Dp;
1673 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001674 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675 return __hold.release();
1676}
1677
Howard Hinnantc834c512011-11-29 18:15:50 +00001678template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679void
Howard Hinnantc834c512011-11-29 18:15:50 +00001680__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681{
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001682 ::new (__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683}
1684
Howard Hinnantc834c512011-11-29 18:15:50 +00001685template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686void
Howard Hinnantc834c512011-11-29 18:15:50 +00001687__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688{
Eric Fiselier125798e2018-12-10 18:14:09 +00001689 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690}
1691
Howard Hinnantc834c512011-11-29 18:15:50 +00001692template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693void
Howard Hinnantc834c512011-11-29 18:15:50 +00001694__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001696 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001697 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001698 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001699 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 __a.deallocate(this, 1);
1701}
1702
Howard Hinnantc834c512011-11-29 18:15:50 +00001703template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1704_Rp
1705__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706{
Eric Fiselier125798e2018-12-10 18:14:09 +00001707 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708}
1709
Howard Hinnant72f73582010-08-11 17:04:31 +00001710#ifndef _LIBCPP_NO_RTTI
1711
Howard Hinnantc834c512011-11-29 18:15:50 +00001712template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001714__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715{
Howard Hinnantc834c512011-11-29 18:15:50 +00001716 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001717 return &__f_.__target();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 return (const void*)0;
1719}
1720
Howard Hinnantc834c512011-11-29 18:15:50 +00001721template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001723__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724{
Howard Hinnantc834c512011-11-29 18:15:50 +00001725 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726}
1727
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001728#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001729
Eric Fiselier125798e2018-12-10 18:14:09 +00001730// __value_func creates a value-type from a __func.
1731
1732template <class _Fp> class __value_func;
1733
1734template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1735{
1736 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1737
1738 typedef __base<_Rp(_ArgTypes...)> __func;
1739 __func* __f_;
1740
1741 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1742 {
1743 return reinterpret_cast<__func*>(p);
1744 }
1745
1746 public:
1747 _LIBCPP_INLINE_VISIBILITY
1748 __value_func() _NOEXCEPT : __f_(0) {}
1749
1750 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001751 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001752 : __f_(0)
1753 {
1754 typedef allocator_traits<_Alloc> __alloc_traits;
1755 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1756 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1757 _FunAlloc;
1758
1759 if (__function::__not_null(__f))
1760 {
1761 _FunAlloc __af(__a);
1762 if (sizeof(_Fun) <= sizeof(__buf_) &&
1763 is_nothrow_copy_constructible<_Fp>::value &&
1764 is_nothrow_copy_constructible<_FunAlloc>::value)
1765 {
1766 __f_ =
1767 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1768 }
1769 else
1770 {
1771 typedef __allocator_destructor<_FunAlloc> _Dp;
1772 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1773 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1774 __f_ = __hold.release();
1775 }
1776 }
1777 }
1778
Eric Fiselier74ebee62019-06-08 01:31:19 +00001779 template <class _Fp,
1780 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1781 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
1782 : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
1783
Eric Fiselier125798e2018-12-10 18:14:09 +00001784 _LIBCPP_INLINE_VISIBILITY
1785 __value_func(const __value_func& __f)
1786 {
1787 if (__f.__f_ == 0)
1788 __f_ = 0;
1789 else if ((void*)__f.__f_ == &__f.__buf_)
1790 {
1791 __f_ = __as_base(&__buf_);
1792 __f.__f_->__clone(__f_);
1793 }
1794 else
1795 __f_ = __f.__f_->__clone();
1796 }
1797
1798 _LIBCPP_INLINE_VISIBILITY
1799 __value_func(__value_func&& __f) _NOEXCEPT
1800 {
1801 if (__f.__f_ == 0)
1802 __f_ = 0;
1803 else if ((void*)__f.__f_ == &__f.__buf_)
1804 {
1805 __f_ = __as_base(&__buf_);
1806 __f.__f_->__clone(__f_);
1807 }
1808 else
1809 {
1810 __f_ = __f.__f_;
1811 __f.__f_ = 0;
1812 }
1813 }
1814
1815 _LIBCPP_INLINE_VISIBILITY
1816 ~__value_func()
1817 {
1818 if ((void*)__f_ == &__buf_)
1819 __f_->destroy();
1820 else if (__f_)
1821 __f_->destroy_deallocate();
1822 }
1823
1824 _LIBCPP_INLINE_VISIBILITY
1825 __value_func& operator=(__value_func&& __f)
1826 {
1827 *this = nullptr;
1828 if (__f.__f_ == 0)
1829 __f_ = 0;
1830 else if ((void*)__f.__f_ == &__f.__buf_)
1831 {
1832 __f_ = __as_base(&__buf_);
1833 __f.__f_->__clone(__f_);
1834 }
1835 else
1836 {
1837 __f_ = __f.__f_;
1838 __f.__f_ = 0;
1839 }
1840 return *this;
1841 }
1842
1843 _LIBCPP_INLINE_VISIBILITY
1844 __value_func& operator=(nullptr_t)
1845 {
1846 __func* __f = __f_;
1847 __f_ = 0;
1848 if ((void*)__f == &__buf_)
1849 __f->destroy();
1850 else if (__f)
1851 __f->destroy_deallocate();
1852 return *this;
1853 }
1854
1855 _LIBCPP_INLINE_VISIBILITY
1856 _Rp operator()(_ArgTypes&&... __args) const
1857 {
1858 if (__f_ == 0)
1859 __throw_bad_function_call();
1860 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1861 }
1862
1863 _LIBCPP_INLINE_VISIBILITY
1864 void swap(__value_func& __f) _NOEXCEPT
1865 {
1866 if (&__f == this)
1867 return;
1868 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1869 {
1870 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1871 __func* __t = __as_base(&__tempbuf);
1872 __f_->__clone(__t);
1873 __f_->destroy();
1874 __f_ = 0;
1875 __f.__f_->__clone(__as_base(&__buf_));
1876 __f.__f_->destroy();
1877 __f.__f_ = 0;
1878 __f_ = __as_base(&__buf_);
1879 __t->__clone(__as_base(&__f.__buf_));
1880 __t->destroy();
1881 __f.__f_ = __as_base(&__f.__buf_);
1882 }
1883 else if ((void*)__f_ == &__buf_)
1884 {
1885 __f_->__clone(__as_base(&__f.__buf_));
1886 __f_->destroy();
1887 __f_ = __f.__f_;
1888 __f.__f_ = __as_base(&__f.__buf_);
1889 }
1890 else if ((void*)__f.__f_ == &__f.__buf_)
1891 {
1892 __f.__f_->__clone(__as_base(&__buf_));
1893 __f.__f_->destroy();
1894 __f.__f_ = __f_;
1895 __f_ = __as_base(&__buf_);
1896 }
1897 else
1898 _VSTD::swap(__f_, __f.__f_);
1899 }
1900
1901 _LIBCPP_INLINE_VISIBILITY
1902 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != 0; }
1903
1904#ifndef _LIBCPP_NO_RTTI
1905 _LIBCPP_INLINE_VISIBILITY
1906 const std::type_info& target_type() const _NOEXCEPT
1907 {
1908 if (__f_ == 0)
1909 return typeid(void);
1910 return __f_->target_type();
1911 }
1912
1913 template <typename _Tp>
1914 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1915 {
1916 if (__f_ == 0)
1917 return 0;
1918 return (const _Tp*)__f_->target(typeid(_Tp));
1919 }
1920#endif // _LIBCPP_NO_RTTI
1921};
1922
Eric Fiselierf2e64362018-12-11 00:14:34 +00001923// Storage for a functor object, to be used with __policy to manage copy and
1924// destruction.
1925union __policy_storage
1926{
1927 mutable char __small[sizeof(void*) * 2];
1928 void* __large;
1929};
1930
1931// True if _Fun can safely be held in __policy_storage.__small.
1932template <typename _Fun>
1933struct __use_small_storage
1934 : public _VSTD::integral_constant<
1935 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001936 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Eric Fiselierf2e64362018-12-11 00:14:34 +00001937 _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1938 _VSTD::is_trivially_destructible<_Fun>::value> {};
1939
1940// Policy contains information about how to copy, destroy, and move the
1941// underlying functor. You can think of it as a vtable of sorts.
1942struct __policy
1943{
1944 // Used to copy or destroy __large values. null for trivial objects.
1945 void* (*const __clone)(const void*);
1946 void (*const __destroy)(void*);
1947
1948 // True if this is the null policy (no value).
1949 const bool __is_null;
1950
1951 // The target type. May be null if RTTI is disabled.
1952 const std::type_info* const __type_info;
1953
1954 // Returns a pointer to a static policy object suitable for the functor
1955 // type.
1956 template <typename _Fun>
1957 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1958 {
1959 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1960 }
1961
1962 _LIBCPP_INLINE_VISIBILITY
1963 static const __policy* __create_empty()
1964 {
1965 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1966 true,
1967#ifndef _LIBCPP_NO_RTTI
1968 &typeid(void)
1969#else
1970 nullptr
1971#endif
1972 };
1973 return &__policy_;
1974 }
1975
1976 private:
1977 template <typename _Fun> static void* __large_clone(const void* __s)
1978 {
1979 const _Fun* __f = static_cast<const _Fun*>(__s);
1980 return __f->__clone();
1981 }
1982
Eric Fiselier74ebee62019-06-08 01:31:19 +00001983 template <typename _Fun>
1984 static void __large_destroy(void* __s) {
1985 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00001986 }
1987
1988 template <typename _Fun>
1989 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00001990 __choose_policy(/* is_small = */ false_type) {
1991 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
1992 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00001993#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00001994 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00001995#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00001996 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00001997#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00001998 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00001999 return &__policy_;
2000 }
2001
2002 template <typename _Fun>
2003 _LIBCPP_INLINE_VISIBILITY static const __policy*
2004 __choose_policy(/* is_small = */ true_type)
2005 {
2006 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2007 nullptr, nullptr, false,
2008#ifndef _LIBCPP_NO_RTTI
2009 &typeid(typename _Fun::_Target)
2010#else
2011 nullptr
2012#endif
2013 };
2014 return &__policy_;
2015 }
2016};
2017
2018// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2019// faster for types that can be passed in registers.
2020template <typename _Tp>
2021using __fast_forward =
2022 typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2023
2024// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2025
2026template <class _Fp> struct __policy_invoker;
2027
2028template <class _Rp, class... _ArgTypes>
2029struct __policy_invoker<_Rp(_ArgTypes...)>
2030{
2031 typedef _Rp (*__Call)(const __policy_storage*,
2032 __fast_forward<_ArgTypes>...);
2033
2034 __Call __call_;
2035
2036 // Creates an invoker that throws bad_function_call.
2037 _LIBCPP_INLINE_VISIBILITY
2038 __policy_invoker() : __call_(&__call_empty) {}
2039
2040 // Creates an invoker that calls the given instance of __func.
2041 template <typename _Fun>
2042 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2043 {
2044 return __policy_invoker(&__call_impl<_Fun>);
2045 }
2046
2047 private:
2048 _LIBCPP_INLINE_VISIBILITY
2049 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2050
2051 static _Rp __call_empty(const __policy_storage*,
2052 __fast_forward<_ArgTypes>...)
2053 {
2054 __throw_bad_function_call();
2055 }
2056
2057 template <typename _Fun>
2058 static _Rp __call_impl(const __policy_storage* __buf,
2059 __fast_forward<_ArgTypes>... __args)
2060 {
2061 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2062 ? &__buf->__small
2063 : __buf->__large);
2064 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2065 }
2066};
2067
2068// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2069// copyable functor.
2070
2071template <class _Fp> class __policy_func;
2072
2073template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2074{
2075 // Inline storage for small objects.
2076 __policy_storage __buf_;
2077
2078 // Calls the value stored in __buf_. This could technically be part of
2079 // policy, but storing it here eliminates a level of indirection inside
2080 // operator().
2081 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2082 __invoker __invoker_;
2083
2084 // The policy that describes how to move / copy / destroy __buf_. Never
2085 // null, even if the function is empty.
2086 const __policy* __policy_;
2087
2088 public:
2089 _LIBCPP_INLINE_VISIBILITY
2090 __policy_func() : __policy_(__policy::__create_empty()) {}
2091
2092 template <class _Fp, class _Alloc>
2093 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2094 : __policy_(__policy::__create_empty())
2095 {
2096 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2097 typedef allocator_traits<_Alloc> __alloc_traits;
2098 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2099 _FunAlloc;
2100
2101 if (__function::__not_null(__f))
2102 {
2103 __invoker_ = __invoker::template __create<_Fun>();
2104 __policy_ = __policy::__create<_Fun>();
2105
2106 _FunAlloc __af(__a);
2107 if (__use_small_storage<_Fun>())
2108 {
2109 ::new ((void*)&__buf_.__small)
2110 _Fun(_VSTD::move(__f), _Alloc(__af));
2111 }
2112 else
2113 {
2114 typedef __allocator_destructor<_FunAlloc> _Dp;
2115 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2116 ::new ((void*)__hold.get())
2117 _Fun(_VSTD::move(__f), _Alloc(__af));
2118 __buf_.__large = __hold.release();
2119 }
2120 }
2121 }
2122
Eric Fiselier74ebee62019-06-08 01:31:19 +00002123 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2124 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2125 : __policy_(__policy::__create_empty()) {
2126 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2127
2128 if (__function::__not_null(__f)) {
2129 __invoker_ = __invoker::template __create<_Fun>();
2130 __policy_ = __policy::__create<_Fun>();
2131 if (__use_small_storage<_Fun>()) {
2132 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2133 } else {
2134 __builtin_new_allocator::__holder_t __hold =
2135 __builtin_new_allocator::__allocate_type<_Fun>(1);
2136 __buf_.__large = ::new (__hold.get()) _Fun(_VSTD::move(__f));
2137 (void)__hold.release();
2138 }
2139 }
2140 }
2141
Eric Fiselierf2e64362018-12-11 00:14:34 +00002142 _LIBCPP_INLINE_VISIBILITY
2143 __policy_func(const __policy_func& __f)
2144 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2145 __policy_(__f.__policy_)
2146 {
2147 if (__policy_->__clone)
2148 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2149 }
2150
2151 _LIBCPP_INLINE_VISIBILITY
2152 __policy_func(__policy_func&& __f)
2153 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2154 __policy_(__f.__policy_)
2155 {
2156 if (__policy_->__destroy)
2157 {
2158 __f.__policy_ = __policy::__create_empty();
2159 __f.__invoker_ = __invoker();
2160 }
2161 }
2162
2163 _LIBCPP_INLINE_VISIBILITY
2164 ~__policy_func()
2165 {
2166 if (__policy_->__destroy)
2167 __policy_->__destroy(__buf_.__large);
2168 }
2169
2170 _LIBCPP_INLINE_VISIBILITY
2171 __policy_func& operator=(__policy_func&& __f)
2172 {
2173 *this = nullptr;
2174 __buf_ = __f.__buf_;
2175 __invoker_ = __f.__invoker_;
2176 __policy_ = __f.__policy_;
2177 __f.__policy_ = __policy::__create_empty();
2178 __f.__invoker_ = __invoker();
2179 return *this;
2180 }
2181
2182 _LIBCPP_INLINE_VISIBILITY
2183 __policy_func& operator=(nullptr_t)
2184 {
2185 const __policy* __p = __policy_;
2186 __policy_ = __policy::__create_empty();
2187 __invoker_ = __invoker();
2188 if (__p->__destroy)
2189 __p->__destroy(__buf_.__large);
2190 return *this;
2191 }
2192
2193 _LIBCPP_INLINE_VISIBILITY
2194 _Rp operator()(_ArgTypes&&... __args) const
2195 {
2196 return __invoker_.__call_(_VSTD::addressof(__buf_),
2197 _VSTD::forward<_ArgTypes>(__args)...);
2198 }
2199
2200 _LIBCPP_INLINE_VISIBILITY
2201 void swap(__policy_func& __f)
2202 {
2203 _VSTD::swap(__invoker_, __f.__invoker_);
2204 _VSTD::swap(__policy_, __f.__policy_);
2205 _VSTD::swap(__buf_, __f.__buf_);
2206 }
2207
2208 _LIBCPP_INLINE_VISIBILITY
2209 explicit operator bool() const _NOEXCEPT
2210 {
2211 return !__policy_->__is_null;
2212 }
2213
2214#ifndef _LIBCPP_NO_RTTI
2215 _LIBCPP_INLINE_VISIBILITY
2216 const std::type_info& target_type() const _NOEXCEPT
2217 {
2218 return *__policy_->__type_info;
2219 }
2220
2221 template <typename _Tp>
2222 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2223 {
2224 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2225 return nullptr;
2226 if (__policy_->__clone) // Out of line storage.
2227 return reinterpret_cast<const _Tp*>(__buf_.__large);
2228 else
2229 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2230 }
2231#endif // _LIBCPP_NO_RTTI
2232};
2233
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234} // __function
2235
Howard Hinnantc834c512011-11-29 18:15:50 +00002236template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002237class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002238 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2239 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002241#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002242 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002243#else
2244 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2245#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246
Eric Fiselier125798e2018-12-10 18:14:09 +00002247 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002248
Eric Fiselier3906a132019-06-23 20:28:29 +00002249 template <class _Fp, bool = _And<
2250 _IsNotSame<__uncvref_t<_Fp>, function>,
Eric Fiselier43c04f72017-09-10 23:41:20 +00002251 __invokable<_Fp&, _ArgTypes...>
2252 >::value>
2253 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002254 template <class _Fp>
2255 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002256 {
Eric Fiselierea080702015-02-10 16:48:45 +00002257 static const bool value = is_same<void, _Rp>::value ||
Howard Hinnantc834c512011-11-29 18:15:50 +00002258 is_convertible<typename __invoke_of<_Fp&, _ArgTypes...>::type,
2259 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002260 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002261 template <class _Fp>
2262 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002263 {
2264 static const bool value = false;
2265 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002266
2267 template <class _Fp>
2268 using _EnableIfCallable = typename enable_if<__callable<_Fp>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002270 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271
Howard Hinnantf06d9262010-08-20 19:36:46 +00002272 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002273 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002274 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002275 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002276 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002278 function(function&&) _NOEXCEPT;
Eric Fiselier43c04f72017-09-10 23:41:20 +00002279 template<class _Fp, class = _EnableIfCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002280 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281
Marshall Clow3148f422016-10-13 21:06:03 +00002282#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002283 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002284 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002285 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002286 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002287 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002288 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002289 template<class _Alloc>
2290 function(allocator_arg_t, const _Alloc&, const function&);
2291 template<class _Alloc>
2292 function(allocator_arg_t, const _Alloc&, function&&);
Eric Fiselier43c04f72017-09-10 23:41:20 +00002293 template<class _Fp, class _Alloc, class = _EnableIfCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002294 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002295#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296
2297 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002298 function& operator=(function&&) _NOEXCEPT;
2299 function& operator=(nullptr_t) _NOEXCEPT;
Eric Fiselier43c04f72017-09-10 23:41:20 +00002300 template<class _Fp, class = _EnableIfCallable<_Fp>>
2301 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302
2303 ~function();
2304
Howard Hinnantf06d9262010-08-20 19:36:46 +00002305 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002306 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002307
2308#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002309 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002311 void assign(_Fp&& __f, const _Alloc& __a)
2312 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002313#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314
Howard Hinnantf06d9262010-08-20 19:36:46 +00002315 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002316 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002317 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2318 return static_cast<bool>(__f_);
2319 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 // deleted overloads close possible hole in the type system
2322 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002323 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002325 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002327 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002328 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329
Howard Hinnant72f73582010-08-11 17:04:31 +00002330#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002331 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002332 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002333 template <typename _Tp> _Tp* target() _NOEXCEPT;
2334 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002335#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336};
2337
Howard Hinnantc834c512011-11-29 18:15:50 +00002338template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002339function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340
Marshall Clow3148f422016-10-13 21:06:03 +00002341#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002342template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002343template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002344function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002345 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002346#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002347
Eric Fiselier125798e2018-12-10 18:14:09 +00002348template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002349function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002350 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351
Marshall Clow3148f422016-10-13 21:06:03 +00002352#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002353template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002354template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002355function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002356 function&& __f)
2357 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002358#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002359
Eric Fiselier125798e2018-12-10 18:14:09 +00002360template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002361template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002362function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363
Marshall Clow3148f422016-10-13 21:06:03 +00002364#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002365template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002366template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002367function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2368 _Fp __f)
2369 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002370#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002371
Howard Hinnantc834c512011-11-29 18:15:50 +00002372template<class _Rp, class ..._ArgTypes>
2373function<_Rp(_ArgTypes...)>&
2374function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375{
2376 function(__f).swap(*this);
2377 return *this;
2378}
2379
Howard Hinnantc834c512011-11-29 18:15:50 +00002380template<class _Rp, class ..._ArgTypes>
2381function<_Rp(_ArgTypes...)>&
2382function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383{
Eric Fiselier125798e2018-12-10 18:14:09 +00002384 __f_ = std::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002385 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386}
2387
Howard Hinnantc834c512011-11-29 18:15:50 +00002388template<class _Rp, class ..._ArgTypes>
2389function<_Rp(_ArgTypes...)>&
2390function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391{
Eric Fiselier125798e2018-12-10 18:14:09 +00002392 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002393 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394}
2395
Howard Hinnantc834c512011-11-29 18:15:50 +00002396template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002397template <class _Fp, class>
2398function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002399function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400{
Howard Hinnantc834c512011-11-29 18:15:50 +00002401 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402 return *this;
2403}
2404
Howard Hinnantc834c512011-11-29 18:15:50 +00002405template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002406function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002407
Howard Hinnantc834c512011-11-29 18:15:50 +00002408template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409void
Howard Hinnantc834c512011-11-29 18:15:50 +00002410function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411{
Eric Fiselier125798e2018-12-10 18:14:09 +00002412 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413}
2414
Howard Hinnantc834c512011-11-29 18:15:50 +00002415template<class _Rp, class ..._ArgTypes>
2416_Rp
2417function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418{
Eric Fiselier125798e2018-12-10 18:14:09 +00002419 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420}
2421
Howard Hinnant72f73582010-08-11 17:04:31 +00002422#ifndef _LIBCPP_NO_RTTI
2423
Howard Hinnantc834c512011-11-29 18:15:50 +00002424template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002426function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427{
Eric Fiselier125798e2018-12-10 18:14:09 +00002428 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429}
2430
Howard Hinnantc834c512011-11-29 18:15:50 +00002431template<class _Rp, class ..._ArgTypes>
2432template <typename _Tp>
2433_Tp*
2434function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435{
Eric Fiselier125798e2018-12-10 18:14:09 +00002436 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437}
2438
Howard Hinnantc834c512011-11-29 18:15:50 +00002439template<class _Rp, class ..._ArgTypes>
2440template <typename _Tp>
2441const _Tp*
2442function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443{
Eric Fiselier125798e2018-12-10 18:14:09 +00002444 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445}
2446
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002447#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002448
Howard Hinnantc834c512011-11-29 18:15:50 +00002449template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450inline _LIBCPP_INLINE_VISIBILITY
2451bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002452operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453
Howard Hinnantc834c512011-11-29 18:15:50 +00002454template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455inline _LIBCPP_INLINE_VISIBILITY
2456bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002457operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458
Howard Hinnantc834c512011-11-29 18:15:50 +00002459template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460inline _LIBCPP_INLINE_VISIBILITY
2461bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002462operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463
Howard Hinnantc834c512011-11-29 18:15:50 +00002464template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465inline _LIBCPP_INLINE_VISIBILITY
2466bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002467operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468
Howard Hinnantc834c512011-11-29 18:15:50 +00002469template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470inline _LIBCPP_INLINE_VISIBILITY
2471void
Howard Hinnantc834c512011-11-29 18:15:50 +00002472swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473{return __x.swap(__y);}
2474
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002475#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002476
2477#include <__functional_03>
2478
2479#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002480
2481////////////////////////////////////////////////////////////////////////////////
2482// BIND
2483//==============================================================================
2484
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002486template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2488
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002489#if _LIBCPP_STD_VER > 14
2490template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002491_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002492#endif
2493
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002495template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2497
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002498#if _LIBCPP_STD_VER > 14
2499template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002500_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002501#endif
2502
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503namespace placeholders
2504{
2505
Howard Hinnantc834c512011-11-29 18:15:50 +00002506template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002508#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002509_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2510_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2511_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2512_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2513_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2514_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2515_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2516_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2517_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2518_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2519#else
Marshall Clow396b2132018-01-02 19:01:45 +00002520/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2521/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2522/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2523/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2524/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2525/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2526/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2527/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2528/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2529/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002530#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531
2532} // placeholders
2533
Howard Hinnantc834c512011-11-29 18:15:50 +00002534template<int _Np>
2535struct __is_placeholder<placeholders::__ph<_Np> >
2536 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002538
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002539#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002540
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541template <class _Tp, class _Uj>
2542inline _LIBCPP_INLINE_VISIBILITY
2543_Tp&
2544__mu(reference_wrapper<_Tp> __t, _Uj&)
2545{
2546 return __t.get();
2547}
2548
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549template <class _Ti, class ..._Uj, size_t ..._Indx>
2550inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002551typename __invoke_of<_Ti&, _Uj...>::type
2552__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002554 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555}
2556
2557template <class _Ti, class ..._Uj>
2558inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002559typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560<
2561 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002562 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563>::type
2564__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2565{
2566 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
2567 return __mu_expand(__ti, __uj, __indices());
2568}
2569
2570template <bool IsPh, class _Ti, class _Uj>
2571struct __mu_return2 {};
2572
2573template <class _Ti, class _Uj>
2574struct __mu_return2<true, _Ti, _Uj>
2575{
2576 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2577};
2578
2579template <class _Ti, class _Uj>
2580inline _LIBCPP_INLINE_VISIBILITY
2581typename enable_if
2582<
2583 0 < is_placeholder<_Ti>::value,
2584 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2585>::type
2586__mu(_Ti&, _Uj& __uj)
2587{
2588 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002589 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590}
2591
2592template <class _Ti, class _Uj>
2593inline _LIBCPP_INLINE_VISIBILITY
2594typename enable_if
2595<
2596 !is_bind_expression<_Ti>::value &&
2597 is_placeholder<_Ti>::value == 0 &&
2598 !__is_reference_wrapper<_Ti>::value,
2599 _Ti&
2600>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002601__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602{
2603 return __ti;
2604}
2605
Howard Hinnant0415d792011-05-22 15:07:43 +00002606template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2607 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002608struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002610template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002611struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002612{
2613 typedef __nat type;
2614};
2615
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002617struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002619 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620};
2621
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002622template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002623struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2624 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002625{
2626};
2627
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002629struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630{
2631 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2632 _TupleUj>::type&& type;
2633};
2634
2635template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002636struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002637{
2638 typedef typename _Ti::type& type;
2639};
2640
2641template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002642struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643{
2644 typedef _Ti& type;
2645};
2646
2647template <class _Ti, class _TupleUj>
2648struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002649 : public __mu_return_impl<_Ti,
2650 __is_reference_wrapper<_Ti>::value,
2651 is_bind_expression<_Ti>::value,
2652 0 < is_placeholder<_Ti>::value &&
2653 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2654 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655{
2656};
2657
Howard Hinnantc834c512011-11-29 18:15:50 +00002658template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002659struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002660{
2661 static const bool value = false;
2662};
2663
2664template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002665struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002666{
2667 static const bool value = __invokable<_Fp,
2668 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2669};
2670
2671template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002672struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002673{
2674 static const bool value = __invokable<_Fp,
2675 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2676};
2677
2678template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002679 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680struct __bind_return;
2681
Howard Hinnantc834c512011-11-29 18:15:50 +00002682template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002683struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002685 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002687 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002688 typename __mu_return
2689 <
2690 _BoundArgs,
2691 _TupleUj
2692 >::type...
2693 >::type type;
2694};
2695
Howard Hinnantc834c512011-11-29 18:15:50 +00002696template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002697struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002699 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002701 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702 typename __mu_return
2703 <
2704 const _BoundArgs,
2705 _TupleUj
2706 >::type...
2707 >::type type;
2708};
2709
Howard Hinnantc834c512011-11-29 18:15:50 +00002710template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002712typename __bind_return<_Fp, _BoundArgs, _Args>::type
2713__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714 _Args&& __args)
2715{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002716 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717}
2718
Howard Hinnantc834c512011-11-29 18:15:50 +00002719template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002721 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002723protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002724 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002725 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002726private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002727 _Fd __f_;
2728 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729
2730 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2731public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002732 template <class _Gp, class ..._BA,
2733 class = typename enable_if
2734 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002735 is_constructible<_Fd, _Gp>::value &&
2736 !is_same<typename remove_reference<_Gp>::type,
2737 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002738 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002740 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2741 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002742 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743
2744 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002746 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 operator()(_Args&& ...__args)
2748 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002749 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002750 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751 }
2752
2753 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002754 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002755 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756 operator()(_Args&& ...__args) const
2757 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002758 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002759 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760 }
2761};
2762
Howard Hinnantc834c512011-11-29 18:15:50 +00002763template<class _Fp, class ..._BoundArgs>
2764struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765
Howard Hinnantc834c512011-11-29 18:15:50 +00002766template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002768 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769{
Howard Hinnantc834c512011-11-29 18:15:50 +00002770 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002771 typedef typename base::_Fd _Fd;
2772 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002774 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775
Howard Hinnant7091e652011-07-02 18:22:36 +00002776
Howard Hinnantf292a922013-07-01 00:01:51 +00002777 template <class _Gp, class ..._BA,
2778 class = typename enable_if
2779 <
2780 is_constructible<_Fd, _Gp>::value &&
2781 !is_same<typename remove_reference<_Gp>::type,
2782 __bind_r>::value
2783 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002785 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2786 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002787 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788
2789 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002790 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002791 typename enable_if
2792 <
2793 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002794 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002795 result_type
2796 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 operator()(_Args&& ...__args)
2798 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002799 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2800 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801 }
2802
2803 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002805 typename enable_if
2806 <
2807 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002808 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002809 result_type
2810 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 operator()(_Args&& ...__args) const
2812 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002813 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2814 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815 }
2816};
2817
Howard Hinnantc834c512011-11-29 18:15:50 +00002818template<class _Rp, class _Fp, class ..._BoundArgs>
2819struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820
Howard Hinnantc834c512011-11-29 18:15:50 +00002821template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002823__bind<_Fp, _BoundArgs...>
2824bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825{
Howard Hinnantc834c512011-11-29 18:15:50 +00002826 typedef __bind<_Fp, _BoundArgs...> type;
2827 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828}
2829
Howard Hinnantc834c512011-11-29 18:15:50 +00002830template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002832__bind_r<_Rp, _Fp, _BoundArgs...>
2833bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834{
Howard Hinnantc834c512011-11-29 18:15:50 +00002835 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2836 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837}
2838
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002839#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840
Eric Fiselier0d974f12015-07-14 20:16:15 +00002841#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002842
Eric Fiselier0d974f12015-07-14 20:16:15 +00002843template <class _Fn, class ..._Args>
Louis Dionneaf34f122019-04-03 17:54:37 +00002844invoke_result_t<_Fn, _Args...>
Eric Fiselier934f63b2016-06-02 01:25:41 +00002845invoke(_Fn&& __f, _Args&&... __args)
Louis Dionneaf34f122019-04-03 17:54:37 +00002846 noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
Eric Fiselier934f63b2016-06-02 01:25:41 +00002847{
2848 return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
Eric Fiselier0d974f12015-07-14 20:16:15 +00002849}
Eric Fiselier934f63b2016-06-02 01:25:41 +00002850
2851template <class _DecayFunc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002852class _LIBCPP_TEMPLATE_VIS __not_fn_imp {
Eric Fiselier934f63b2016-06-02 01:25:41 +00002853 _DecayFunc __fd;
2854
2855public:
2856 __not_fn_imp() = delete;
2857
2858 template <class ..._Args>
2859 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00002860 auto operator()(_Args&& ...__args) &
Eric Fiselier934f63b2016-06-02 01:25:41 +00002861 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002862 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2863 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00002864
2865 template <class ..._Args>
2866 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00002867 auto operator()(_Args&& ...__args) &&
2868 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002869 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
2870 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00002871
2872 template <class ..._Args>
2873 _LIBCPP_INLINE_VISIBILITY
2874 auto operator()(_Args&& ...__args) const&
Eric Fiselier934f63b2016-06-02 01:25:41 +00002875 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002876 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2877 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00002878
Eric Fiseliere8303a32016-06-27 00:40:41 +00002879
2880 template <class ..._Args>
2881 _LIBCPP_INLINE_VISIBILITY
2882 auto operator()(_Args&& ...__args) const&&
2883 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002884 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
2885 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00002886
Eric Fiselier934f63b2016-06-02 01:25:41 +00002887private:
2888 template <class _RawFunc,
2889 class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
2890 _LIBCPP_INLINE_VISIBILITY
2891 explicit __not_fn_imp(_RawFunc&& __rf)
2892 : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
2893
2894 template <class _RawFunc>
2895 friend inline _LIBCPP_INLINE_VISIBILITY
2896 __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
2897};
2898
2899template <class _RawFunc>
2900inline _LIBCPP_INLINE_VISIBILITY
2901__not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
2902 return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
2903}
2904
Eric Fiselier0d974f12015-07-14 20:16:15 +00002905#endif
2906
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002907// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908
Marshall Clowa40686b2018-01-08 19:18:00 +00002909template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00002910pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00002911__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2912 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
2913 forward_iterator_tag, forward_iterator_tag)
2914{
2915 if (__first2 == __last2)
2916 return make_pair(__first1, __first1); // Everything matches an empty sequence
2917 while (true)
2918 {
2919 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
2920 while (true)
2921 {
2922 if (__first1 == __last1) // return __last1 if no element matches *__first2
2923 return make_pair(__last1, __last1);
2924 if (__pred(*__first1, *__first2))
2925 break;
2926 ++__first1;
2927 }
2928 // *__first1 matches *__first2, now match elements after here
2929 _ForwardIterator1 __m1 = __first1;
2930 _ForwardIterator2 __m2 = __first2;
2931 while (true)
2932 {
2933 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
2934 return make_pair(__first1, __m1);
2935 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
2936 return make_pair(__last1, __last1);
2937 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
2938 {
2939 ++__first1;
2940 break;
2941 } // else there is a match, check next elements
2942 }
2943 }
2944}
2945
2946template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
2947_LIBCPP_CONSTEXPR_AFTER_CXX11
2948pair<_RandomAccessIterator1, _RandomAccessIterator1>
2949__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
2950 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
2951 random_access_iterator_tag, random_access_iterator_tag)
2952{
2953 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
2954 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
2955 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
2956 const _D2 __len2 = __last2 - __first2;
2957 if (__len2 == 0)
2958 return make_pair(__first1, __first1);
2959 const _D1 __len1 = __last1 - __first1;
2960 if (__len1 < __len2)
2961 return make_pair(__last1, __last1);
2962 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
2963
2964 while (true)
2965 {
2966 while (true)
2967 {
2968 if (__first1 == __s)
2969 return make_pair(__last1, __last1);
2970 if (__pred(*__first1, *__first2))
2971 break;
2972 ++__first1;
2973 }
2974
2975 _RandomAccessIterator1 __m1 = __first1;
2976 _RandomAccessIterator2 __m2 = __first2;
2977 while (true)
2978 {
2979 if (++__m2 == __last2)
2980 return make_pair(__first1, __first1 + __len2);
2981 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
2982 if (!__pred(*__m1, *__m2))
2983 {
2984 ++__first1;
2985 break;
2986 }
2987 }
2988 }
2989}
2990
2991#if _LIBCPP_STD_VER > 14
2992
2993// default searcher
2994template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Dimitry Andricfd3633d2018-02-13 17:40:59 +00002995class _LIBCPP_TYPE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00002996public:
2997 _LIBCPP_INLINE_VISIBILITY
Louis Dionne44bcff92018-08-03 22:36:53 +00002998 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00002999 _BinaryPredicate __p = _BinaryPredicate())
3000 : __first_(__f), __last_(__l), __pred_(__p) {}
3001
3002 template <typename _ForwardIterator2>
3003 _LIBCPP_INLINE_VISIBILITY
3004 pair<_ForwardIterator2, _ForwardIterator2>
3005 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3006 {
3007 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
3008 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
3009 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
3010 }
3011
3012private:
3013 _ForwardIterator __first_;
3014 _ForwardIterator __last_;
3015 _BinaryPredicate __pred_;
3016 };
3017
3018#endif // _LIBCPP_STD_VER > 14
3019
Louis Dionnedb1892a2018-12-03 14:03:27 +00003020#if _LIBCPP_STD_VER > 17
3021template <class _Tp>
3022using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3023
3024template <class _Tp>
3025using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3026#endif // > C++17
3027
Marshall Clow29b53f22018-12-14 18:49:35 +00003028template <class _Container, class _Predicate>
3029inline void __libcpp_erase_if_container( _Container& __c, _Predicate __pred)
3030{
3031 for (typename _Container::iterator __iter = __c.begin(), __last = __c.end(); __iter != __last;)
3032 {
3033 if (__pred(*__iter))
3034 __iter = __c.erase(__iter);
3035 else
3036 ++__iter;
3037 }
3038}
3039
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040_LIBCPP_END_NAMESPACE_STD
3041
3042#endif // _LIBCPP_FUNCTIONAL