blob: 67baa5bd4b77e26183f7297bc88ad837f251e87a [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
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500216template <class F>
217constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
Eric Fiselier934f63b2016-06-02 01:25:41 +0000218
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219template<class T> struct is_bind_expression;
220template<class T> struct is_placeholder;
221
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000222 // See C++14 20.9.9, Function object binders
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000223template <class T> inline constexpr bool is_bind_expression_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000224 = is_bind_expression<T>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000225template <class T> inline constexpr int is_placeholder_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000226 = is_placeholder<T>::value; // C++17
227
228
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000229template<class Fn, class... BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500230 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000231template<class R, class Fn, class... BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500232 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233
Louis Dionneaf34f122019-04-03 17:54:37 +0000234template<class F, class... Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500235 constexpr // constexpr in C++20
Louis Dionneaf34f122019-04-03 17:54:37 +0000236 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
237 noexcept(is_nothrow_invocable_v<F, Args...>);
238
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000239namespace placeholders {
240 // M is the implementation-defined number of placeholders
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241 extern unspecified _1;
242 extern unspecified _2;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000243 .
244 .
245 .
Howard Hinnantc834c512011-11-29 18:15:50 +0000246 extern unspecified _Mp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247}
248
249template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000250class binder1st // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251 : public unary_function<typename Operation::second_argument_type,
252 typename Operation::result_type>
253{
254protected:
255 Operation op;
256 typename Operation::first_argument_type value;
257public:
258 binder1st(const Operation& x, const typename Operation::first_argument_type y);
259 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
260 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
261};
262
263template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000264binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265
266template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000267class binder2nd // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268 : public unary_function<typename Operation::first_argument_type,
269 typename Operation::result_type>
270{
271protected:
272 Operation op;
273 typename Operation::second_argument_type value;
274public:
275 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
276 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
277 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
278};
279
280template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000281binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282
Marshall Clow26a027c2017-04-13 18:25:32 +0000283template <class Arg, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284class pointer_to_unary_function : public unary_function<Arg, Result>
285{
286public:
287 explicit pointer_to_unary_function(Result (*f)(Arg));
288 Result operator()(Arg x) const;
289};
290
291template <class Arg, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000292pointer_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 +0000293
Marshall Clow26a027c2017-04-13 18:25:32 +0000294template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
296{
297public:
298 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
299 Result operator()(Arg1 x, Arg2 y) const;
300};
301
302template <class Arg1, class Arg2, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000303pointer_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 +0000304
Marshall Clow26a027c2017-04-13 18:25:32 +0000305template<class S, class T> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306class mem_fun_t : public unary_function<T*, S>
307{
308public:
309 explicit mem_fun_t(S (T::*p)());
310 S operator()(T* p) const;
311};
312
313template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000314class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315{
316public:
317 explicit mem_fun1_t(S (T::*p)(A));
318 S operator()(T* p, A x) const;
319};
320
Marshall Clow26a027c2017-04-13 18:25:32 +0000321template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
322template<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 +0000323
324template<class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000325class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326{
327public:
328 explicit mem_fun_ref_t(S (T::*p)());
329 S operator()(T& p) const;
330};
331
332template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000333class 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 +0000334{
335public:
336 explicit mem_fun1_ref_t(S (T::*p)(A));
337 S operator()(T& p, A x) const;
338};
339
Marshall Clow26a027c2017-04-13 18:25:32 +0000340template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
341template<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 +0000342
343template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000344class 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 +0000345{
346public:
347 explicit const_mem_fun_t(S (T::*p)() const);
348 S operator()(const T* p) const;
349};
350
351template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000352class 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 +0000353{
354public:
355 explicit const_mem_fun1_t(S (T::*p)(A) const);
356 S operator()(const T* p, A x) const;
357};
358
Marshall Clow26a027c2017-04-13 18:25:32 +0000359template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
360template <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 +0000361
362template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000363class 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 +0000364{
365public:
366 explicit const_mem_fun_ref_t(S (T::*p)() const);
367 S operator()(const T& p) const;
368};
369
370template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000371class 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 +0000372{
373public:
374 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
375 S operator()(const T& p, A x) const;
376};
377
Marshall Clow26a027c2017-04-13 18:25:32 +0000378template <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
379template <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 +0000380
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500381template<class R, class T>
382constexpr unspecified mem_fn(R T::*); // constexpr in C++20
Howard Hinnantf06d9262010-08-20 19:36:46 +0000383
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384class bad_function_call
385 : public exception
386{
387};
388
Howard Hinnantf06d9262010-08-20 19:36:46 +0000389template<class> class function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390
Howard Hinnantf06d9262010-08-20 19:36:46 +0000391template<class R, class... ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392class function<R(ArgTypes...)>
393 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
394 // ArgTypes contains T1
395 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
396 // ArgTypes contains T1 and T2
397{
398public:
399 typedef R result_type;
400
Howard Hinnantf06d9262010-08-20 19:36:46 +0000401 // construct/copy/destroy:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000402 function() noexcept;
403 function(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000405 function(function&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406 template<class F>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 function(F);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000409 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000411 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000413 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000415 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416 template<class F, Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000417 function(allocator_arg_t, const Alloc&, F); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418
419 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000420 function& operator=(function&&) noexcept;
Howard Hinnant7b85be02011-05-29 13:53:56 +0000421 function& operator=(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422 template<class F>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000423 function& operator=(F&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424 template<class F>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000425 function& operator=(reference_wrapper<F>) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427 ~function();
428
Howard Hinnantf06d9262010-08-20 19:36:46 +0000429 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000430 void swap(function&) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +0000431 template<class F, class Alloc>
Marshall Clowfc8fd832016-01-25 17:29:55 +0000432 void assign(F&&, const Alloc&); // Removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433
Howard Hinnantf06d9262010-08-20 19:36:46 +0000434 // function capacity:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000435 explicit operator bool() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436
Howard Hinnantf06d9262010-08-20 19:36:46 +0000437 // function invocation:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438 R operator()(ArgTypes...) const;
439
Howard Hinnantf06d9262010-08-20 19:36:46 +0000440 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000441 const std::type_info& target_type() const noexcept;
442 template <typename T> T* target() noexcept;
443 template <typename T> const T* target() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444};
445
Louis Dionne4af49712019-07-18 19:50:56 +0000446// Deduction guides
447template<class R, class ...Args>
448function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
449
450template<class F>
451function(F) -> function<see-below>; // since C++17
452
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000453// Null pointer comparisons:
454template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000455 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000457template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000458 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000460template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000461 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000462
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000463template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000464 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000465
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000466// specialized algorithms:
467template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000468 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469
470template <class T> struct hash;
471
472template <> struct hash<bool>;
473template <> struct hash<char>;
474template <> struct hash<signed char>;
475template <> struct hash<unsigned char>;
Yuriy Chernyshovfbb87fa2020-12-08 13:39:56 -0500476template <> struct hash<char8_t>; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000477template <> struct hash<char16_t>;
478template <> struct hash<char32_t>;
479template <> struct hash<wchar_t>;
480template <> struct hash<short>;
481template <> struct hash<unsigned short>;
482template <> struct hash<int>;
483template <> struct hash<unsigned int>;
484template <> struct hash<long>;
485template <> struct hash<long long>;
486template <> struct hash<unsigned long>;
487template <> struct hash<unsigned long long>;
488
489template <> struct hash<float>;
490template <> struct hash<double>;
491template <> struct hash<long double>;
492
493template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000494template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000495
496} // std
497
498POLICY: For non-variadic implementations, the number of arguments is limited
499 to 3. It is hoped that the need for non-variadic implementations
500 will be minimal.
501
502*/
503
504#include <__config>
505#include <type_traits>
506#include <typeinfo>
507#include <exception>
508#include <memory>
509#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000510#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000511#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512
513#include <__functional_base>
514
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000515#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000517#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518
519_LIBCPP_BEGIN_NAMESPACE_STD
520
Marshall Clow974bae22013-07-29 14:21:53 +0000521#if _LIBCPP_STD_VER > 11
522template <class _Tp = void>
523#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000525#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000526struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527{
Marshall Clowd18ee652013-09-28 19:06:12 +0000528 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
529 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530 {return __x + __y;}
531};
532
Marshall Clow974bae22013-07-29 14:21:53 +0000533#if _LIBCPP_STD_VER > 11
534template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000535struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000536{
537 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000538 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
539 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000540 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
541 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
542 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000543 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000544};
545#endif
546
547
548#if _LIBCPP_STD_VER > 11
549template <class _Tp = void>
550#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000552#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000553struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554{
Marshall Clowd18ee652013-09-28 19:06:12 +0000555 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
556 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557 {return __x - __y;}
558};
559
Marshall Clow974bae22013-07-29 14:21:53 +0000560#if _LIBCPP_STD_VER > 11
561template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000562struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000563{
564 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000565 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
566 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000567 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
568 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
569 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000570 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000571};
572#endif
573
574
575#if _LIBCPP_STD_VER > 11
576template <class _Tp = void>
577#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000579#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000580struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581{
Marshall Clowd18ee652013-09-28 19:06:12 +0000582 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
583 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 {return __x * __y;}
585};
586
Marshall Clow974bae22013-07-29 14:21:53 +0000587#if _LIBCPP_STD_VER > 11
588template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000589struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000590{
591 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000592 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
593 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000594 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
595 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
596 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000597 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000598};
599#endif
600
601
602#if _LIBCPP_STD_VER > 11
603template <class _Tp = void>
604#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000606#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000607struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608{
Marshall Clowd18ee652013-09-28 19:06:12 +0000609 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
610 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611 {return __x / __y;}
612};
613
Marshall Clow974bae22013-07-29 14:21:53 +0000614#if _LIBCPP_STD_VER > 11
615template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000616struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000617{
618 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000619 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
620 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000621 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
622 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
623 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000624 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000625};
626#endif
627
628
629#if _LIBCPP_STD_VER > 11
630template <class _Tp = void>
631#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000633#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000634struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635{
Marshall Clowd18ee652013-09-28 19:06:12 +0000636 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
637 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638 {return __x % __y;}
639};
640
Marshall Clow974bae22013-07-29 14:21:53 +0000641#if _LIBCPP_STD_VER > 11
642template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000643struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000644{
645 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000646 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
647 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000648 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
649 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
650 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000651 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000652};
653#endif
654
655
656#if _LIBCPP_STD_VER > 11
657template <class _Tp = void>
658#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000660#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000661struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662{
Marshall Clowd18ee652013-09-28 19:06:12 +0000663 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
664 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665 {return -__x;}
666};
667
Marshall Clow974bae22013-07-29 14:21:53 +0000668#if _LIBCPP_STD_VER > 11
669template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000670struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000671{
672 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000673 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
674 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000675 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
676 -> decltype (- _VSTD::forward<_Tp>(__x))
677 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000678 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000679};
680#endif
681
682
683#if _LIBCPP_STD_VER > 11
684template <class _Tp = void>
685#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000687#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000688struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689{
Marshall Clowd18ee652013-09-28 19:06:12 +0000690 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
691 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 {return __x == __y;}
693};
694
Marshall Clow974bae22013-07-29 14:21:53 +0000695#if _LIBCPP_STD_VER > 11
696template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000697struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000698{
Marshall Clowd18ee652013-09-28 19:06:12 +0000699 template <class _T1, class _T2>
700 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000701 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000702 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
703 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
704 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000705 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000706};
707#endif
708
709
710#if _LIBCPP_STD_VER > 11
711template <class _Tp = void>
712#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000714#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000715struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716{
Marshall Clowd18ee652013-09-28 19:06:12 +0000717 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
718 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719 {return __x != __y;}
720};
721
Marshall Clow974bae22013-07-29 14:21:53 +0000722#if _LIBCPP_STD_VER > 11
723template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000724struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000725{
Marshall Clowd18ee652013-09-28 19:06:12 +0000726 template <class _T1, class _T2>
727 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000728 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000729 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
730 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
731 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000732 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000733};
734#endif
735
736
737#if _LIBCPP_STD_VER > 11
738template <class _Tp = void>
739#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000741#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000742struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743{
Marshall Clowd18ee652013-09-28 19:06:12 +0000744 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
745 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 {return __x > __y;}
747};
748
Marshall Clow974bae22013-07-29 14:21:53 +0000749#if _LIBCPP_STD_VER > 11
750template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000751struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000752{
Marshall Clowd18ee652013-09-28 19:06:12 +0000753 template <class _T1, class _T2>
754 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000755 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000756 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
757 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
758 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000759 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000760};
761#endif
762
763
Howard Hinnantb17caf92012-02-21 21:02:58 +0000764// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765
Marshall Clow974bae22013-07-29 14:21:53 +0000766#if _LIBCPP_STD_VER > 11
767template <class _Tp = void>
768#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000770#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000771struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772{
Marshall Clowd18ee652013-09-28 19:06:12 +0000773 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
774 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 {return __x >= __y;}
776};
777
Marshall Clow974bae22013-07-29 14:21:53 +0000778#if _LIBCPP_STD_VER > 11
779template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000780struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000781{
Marshall Clowd18ee652013-09-28 19:06:12 +0000782 template <class _T1, class _T2>
783 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000784 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000785 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
786 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
787 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000788 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000789};
790#endif
791
792
793#if _LIBCPP_STD_VER > 11
794template <class _Tp = void>
795#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000797#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000798struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799{
Marshall Clowd18ee652013-09-28 19:06:12 +0000800 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
801 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802 {return __x <= __y;}
803};
804
Marshall Clow974bae22013-07-29 14:21:53 +0000805#if _LIBCPP_STD_VER > 11
806template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000807struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000808{
Marshall Clowd18ee652013-09-28 19:06:12 +0000809 template <class _T1, class _T2>
810 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000811 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000812 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
813 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
814 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000815 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000816};
817#endif
818
819
820#if _LIBCPP_STD_VER > 11
821template <class _Tp = void>
822#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000824#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000825struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826{
Marshall Clowd18ee652013-09-28 19:06:12 +0000827 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
828 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829 {return __x && __y;}
830};
831
Marshall Clow974bae22013-07-29 14:21:53 +0000832#if _LIBCPP_STD_VER > 11
833template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000834struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000835{
Marshall Clowd18ee652013-09-28 19:06:12 +0000836 template <class _T1, class _T2>
837 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000838 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000839 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
840 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
841 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000842 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000843};
844#endif
845
846
847#if _LIBCPP_STD_VER > 11
848template <class _Tp = void>
849#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000851#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000852struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853{
Marshall Clowd18ee652013-09-28 19:06:12 +0000854 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
855 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 {return __x || __y;}
857};
858
Marshall Clow974bae22013-07-29 14:21:53 +0000859#if _LIBCPP_STD_VER > 11
860template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000861struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000862{
Marshall Clowd18ee652013-09-28 19:06:12 +0000863 template <class _T1, class _T2>
864 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000865 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000866 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
867 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
868 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000869 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000870};
871#endif
872
873
874#if _LIBCPP_STD_VER > 11
875template <class _Tp = void>
876#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000878#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000879struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880{
Marshall Clowd18ee652013-09-28 19:06:12 +0000881 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
882 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 {return !__x;}
884};
885
Marshall Clow974bae22013-07-29 14:21:53 +0000886#if _LIBCPP_STD_VER > 11
887template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000888struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000889{
890 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000891 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
892 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000893 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
894 -> decltype (!_VSTD::forward<_Tp>(__x))
895 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000896 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000897};
898#endif
899
900
901#if _LIBCPP_STD_VER > 11
902template <class _Tp = void>
903#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000905#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000906struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907{
Marshall Clowd18ee652013-09-28 19:06:12 +0000908 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
909 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 {return __x & __y;}
911};
912
Marshall Clow974bae22013-07-29 14:21:53 +0000913#if _LIBCPP_STD_VER > 11
914template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000915struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000916{
Marshall Clowd18ee652013-09-28 19:06:12 +0000917 template <class _T1, class _T2>
918 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000919 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000920 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
921 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
922 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000923 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000924};
925#endif
926
927
928#if _LIBCPP_STD_VER > 11
929template <class _Tp = void>
930#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000932#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000933struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934{
Marshall Clowd18ee652013-09-28 19:06:12 +0000935 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
936 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937 {return __x | __y;}
938};
939
Marshall Clow974bae22013-07-29 14:21:53 +0000940#if _LIBCPP_STD_VER > 11
941template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000942struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000943{
Marshall Clowd18ee652013-09-28 19:06:12 +0000944 template <class _T1, class _T2>
945 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000946 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000947 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
948 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
949 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000950 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000951};
952#endif
953
954
955#if _LIBCPP_STD_VER > 11
956template <class _Tp = void>
957#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000959#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000960struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961{
Marshall Clowd18ee652013-09-28 19:06:12 +0000962 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
963 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964 {return __x ^ __y;}
965};
966
Marshall Clow974bae22013-07-29 14:21:53 +0000967#if _LIBCPP_STD_VER > 11
968template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000969struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000970{
Marshall Clowd18ee652013-09-28 19:06:12 +0000971 template <class _T1, class _T2>
972 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000973 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000974 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
975 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
976 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000977 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000978};
979#endif
980
981
982#if _LIBCPP_STD_VER > 11
983template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000984struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000985{
Marshall Clowd18ee652013-09-28 19:06:12 +0000986 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
987 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000988 {return ~__x;}
989};
990
991template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000992struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000993{
994 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000995 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
996 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000997 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
998 -> decltype (~_VSTD::forward<_Tp>(__x))
999 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +00001000 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +00001001};
1002#endif
1003
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001005class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 : public unary_function<typename _Predicate::argument_type, bool>
1007{
1008 _Predicate __pred_;
1009public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001010 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1011 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001013 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1014 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 {return !__pred_(__x);}
1016};
1017
1018template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001019_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020unary_negate<_Predicate>
1021not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1022
1023template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001024class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 : public binary_function<typename _Predicate::first_argument_type,
1026 typename _Predicate::second_argument_type,
1027 bool>
1028{
1029 _Predicate __pred_;
1030public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001031 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001032 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1033
1034 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1035 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 const typename _Predicate::second_argument_type& __y) const
1037 {return !__pred_(__x, __y);}
1038};
1039
1040template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001041_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042binary_negate<_Predicate>
1043not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1044
Marshall Clow26a027c2017-04-13 18:25:32 +00001045#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001047class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 : public unary_function<typename __Operation::second_argument_type,
1049 typename __Operation::result_type>
1050{
1051protected:
1052 __Operation op;
1053 typename __Operation::first_argument_type value;
1054public:
1055 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1056 const typename __Operation::first_argument_type __y)
1057 : op(__x), value(__y) {}
1058 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1059 (typename __Operation::second_argument_type& __x) const
1060 {return op(value, __x);}
1061 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1062 (const typename __Operation::second_argument_type& __x) const
1063 {return op(value, __x);}
1064};
1065
1066template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001067_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068binder1st<__Operation>
1069bind1st(const __Operation& __op, const _Tp& __x)
1070 {return binder1st<__Operation>(__op, __x);}
1071
1072template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001073class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 : public unary_function<typename __Operation::first_argument_type,
1075 typename __Operation::result_type>
1076{
1077protected:
1078 __Operation op;
1079 typename __Operation::second_argument_type value;
1080public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1083 : op(__x), value(__y) {}
1084 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1085 ( typename __Operation::first_argument_type& __x) const
1086 {return op(__x, value);}
1087 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1088 (const typename __Operation::first_argument_type& __x) const
1089 {return op(__x, value);}
1090};
1091
1092template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001093_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094binder2nd<__Operation>
1095bind2nd(const __Operation& __op, const _Tp& __x)
1096 {return binder2nd<__Operation>(__op, __x);}
1097
1098template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001099class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001100 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101{
1102 _Result (*__f_)(_Arg);
1103public:
1104 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1105 : __f_(__f) {}
1106 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1107 {return __f_(__x);}
1108};
1109
1110template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001111_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112pointer_to_unary_function<_Arg,_Result>
1113ptr_fun(_Result (*__f)(_Arg))
1114 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1115
1116template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001117class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001118 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119{
1120 _Result (*__f_)(_Arg1, _Arg2);
1121public:
1122 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1123 : __f_(__f) {}
1124 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1125 {return __f_(__x, __y);}
1126};
1127
1128template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001129_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130pointer_to_binary_function<_Arg1,_Arg2,_Result>
1131ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1132 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1133
1134template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001135class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1136 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137{
1138 _Sp (_Tp::*__p_)();
1139public:
1140 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1141 : __p_(__p) {}
1142 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1143 {return (__p->*__p_)();}
1144};
1145
1146template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001147class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1148 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149{
1150 _Sp (_Tp::*__p_)(_Ap);
1151public:
1152 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1153 : __p_(__p) {}
1154 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1155 {return (__p->*__p_)(__x);}
1156};
1157
1158template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001159_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160mem_fun_t<_Sp,_Tp>
1161mem_fun(_Sp (_Tp::*__f)())
1162 {return mem_fun_t<_Sp,_Tp>(__f);}
1163
1164template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001165_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166mem_fun1_t<_Sp,_Tp,_Ap>
1167mem_fun(_Sp (_Tp::*__f)(_Ap))
1168 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1169
1170template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001171class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1172 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173{
1174 _Sp (_Tp::*__p_)();
1175public:
1176 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1177 : __p_(__p) {}
1178 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1179 {return (__p.*__p_)();}
1180};
1181
1182template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001183class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1184 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185{
1186 _Sp (_Tp::*__p_)(_Ap);
1187public:
1188 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1189 : __p_(__p) {}
1190 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1191 {return (__p.*__p_)(__x);}
1192};
1193
1194template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001195_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196mem_fun_ref_t<_Sp,_Tp>
1197mem_fun_ref(_Sp (_Tp::*__f)())
1198 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1199
1200template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001201_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202mem_fun1_ref_t<_Sp,_Tp,_Ap>
1203mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1204 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1205
1206template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001207class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1208 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209{
1210 _Sp (_Tp::*__p_)() const;
1211public:
1212 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1213 : __p_(__p) {}
1214 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1215 {return (__p->*__p_)();}
1216};
1217
1218template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001219class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1220 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221{
1222 _Sp (_Tp::*__p_)(_Ap) const;
1223public:
1224 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1225 : __p_(__p) {}
1226 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1227 {return (__p->*__p_)(__x);}
1228};
1229
1230template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001231_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232const_mem_fun_t<_Sp,_Tp>
1233mem_fun(_Sp (_Tp::*__f)() const)
1234 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1235
1236template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001237_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238const_mem_fun1_t<_Sp,_Tp,_Ap>
1239mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1240 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1241
1242template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001243class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1244 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245{
1246 _Sp (_Tp::*__p_)() const;
1247public:
1248 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1249 : __p_(__p) {}
1250 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1251 {return (__p.*__p_)();}
1252};
1253
1254template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001255class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001256 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001257{
1258 _Sp (_Tp::*__p_)(_Ap) const;
1259public:
1260 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1261 : __p_(__p) {}
1262 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1263 {return (__p.*__p_)(__x);}
1264};
1265
1266template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001267_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268const_mem_fun_ref_t<_Sp,_Tp>
1269mem_fun_ref(_Sp (_Tp::*__f)() const)
1270 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1271
1272template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001273_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1275mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1276 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001277#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001279////////////////////////////////////////////////////////////////////////////////
1280// MEMFUN
1281//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283template <class _Tp>
1284class __mem_fn
1285 : public __weak_result_type<_Tp>
1286{
1287public:
1288 // types
1289 typedef _Tp type;
1290private:
1291 type __f_;
1292
1293public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001294 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1295 __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001297#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298 // invoke
1299 template <class... _ArgTypes>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001300 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier2cc48332015-07-22 22:43:27 +00001301 typename __invoke_return<type, _ArgTypes...>::type
1302 operator() (_ArgTypes&&... __args) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001303 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001304 }
1305#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001306
1307 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001308 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001309 typename __invoke_return0<type, _A0>::type
1310 operator() (_A0& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001311 return _VSTD::__invoke(__f_, __a0);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001312 }
1313
Eric Fiselierce1813a2015-08-26 20:15:02 +00001314 template <class _A0>
1315 _LIBCPP_INLINE_VISIBILITY
1316 typename __invoke_return0<type, _A0 const>::type
1317 operator() (_A0 const& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001318 return _VSTD::__invoke(__f_, __a0);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001319 }
1320
Eric Fiselier2cc48332015-07-22 22:43:27 +00001321 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001322 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001323 typename __invoke_return1<type, _A0, _A1>::type
1324 operator() (_A0& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001325 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001326 }
1327
Eric Fiselierce1813a2015-08-26 20:15:02 +00001328 template <class _A0, class _A1>
1329 _LIBCPP_INLINE_VISIBILITY
1330 typename __invoke_return1<type, _A0 const, _A1>::type
1331 operator() (_A0 const& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001332 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001333 }
1334
1335 template <class _A0, class _A1>
1336 _LIBCPP_INLINE_VISIBILITY
1337 typename __invoke_return1<type, _A0, _A1 const>::type
1338 operator() (_A0& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001339 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001340 }
1341
1342 template <class _A0, class _A1>
1343 _LIBCPP_INLINE_VISIBILITY
1344 typename __invoke_return1<type, _A0 const, _A1 const>::type
1345 operator() (_A0 const& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001346 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001347 }
1348
Eric Fiselier2cc48332015-07-22 22:43:27 +00001349 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001350 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001351 typename __invoke_return2<type, _A0, _A1, _A2>::type
1352 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001353 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001354 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001355
1356 template <class _A0, class _A1, class _A2>
1357 _LIBCPP_INLINE_VISIBILITY
1358 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1359 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001360 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001361 }
1362
1363 template <class _A0, class _A1, class _A2>
1364 _LIBCPP_INLINE_VISIBILITY
1365 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1366 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001367 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001368 }
1369
1370 template <class _A0, class _A1, class _A2>
1371 _LIBCPP_INLINE_VISIBILITY
1372 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1373 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001374 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001375 }
1376
1377 template <class _A0, class _A1, class _A2>
1378 _LIBCPP_INLINE_VISIBILITY
1379 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1380 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001381 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001382 }
1383
1384 template <class _A0, class _A1, class _A2>
1385 _LIBCPP_INLINE_VISIBILITY
1386 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1387 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001388 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001389 }
1390
1391 template <class _A0, class _A1, class _A2>
1392 _LIBCPP_INLINE_VISIBILITY
1393 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1394 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001395 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001396 }
1397
1398 template <class _A0, class _A1, class _A2>
1399 _LIBCPP_INLINE_VISIBILITY
1400 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1401 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001402 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001403 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001404#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405};
1406
Howard Hinnantc834c512011-11-29 18:15:50 +00001407template<class _Rp, class _Tp>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001408inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00001409__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001410mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411{
Howard Hinnantc834c512011-11-29 18:15:50 +00001412 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413}
1414
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001415////////////////////////////////////////////////////////////////////////////////
1416// FUNCTION
1417//==============================================================================
1418
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419// bad_function_call
1420
Howard Hinnant4ff57432010-09-21 22:55:27 +00001421class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 : public exception
1423{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001424#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1425public:
1426 virtual ~bad_function_call() _NOEXCEPT;
1427
1428 virtual const char* what() const _NOEXCEPT;
1429#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430};
1431
Louis Dionne16fe2952018-07-11 23:14:33 +00001432_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001433void __throw_bad_function_call()
1434{
1435#ifndef _LIBCPP_NO_EXCEPTIONS
1436 throw bad_function_call();
1437#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001438 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001439#endif
1440}
1441
Louis Dionne44d1f812020-03-09 11:16:22 -04001442#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1443# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1444 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1445#else
1446# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1447#endif
1448
1449template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450
1451namespace __function
1452{
1453
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001454template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455struct __maybe_derive_from_unary_function
1456{
1457};
1458
Howard Hinnantc834c512011-11-29 18:15:50 +00001459template<class _Rp, class _A1>
1460struct __maybe_derive_from_unary_function<_Rp(_A1)>
1461 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462{
1463};
1464
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001465template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466struct __maybe_derive_from_binary_function
1467{
1468};
1469
Howard Hinnantc834c512011-11-29 18:15:50 +00001470template<class _Rp, class _A1, class _A2>
1471struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1472 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473{
1474};
1475
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001476template <class _Fp>
1477_LIBCPP_INLINE_VISIBILITY
1478bool __not_null(_Fp const&) { return true; }
1479
1480template <class _Fp>
1481_LIBCPP_INLINE_VISIBILITY
1482bool __not_null(_Fp* __ptr) { return __ptr; }
1483
1484template <class _Ret, class _Class>
1485_LIBCPP_INLINE_VISIBILITY
1486bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1487
1488template <class _Fp>
1489_LIBCPP_INLINE_VISIBILITY
1490bool __not_null(function<_Fp> const& __f) { return !!__f; }
1491
Louis Dionnee2391d72020-04-22 13:58:17 -04001492#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1493template <class _Rp, class ..._Args>
1494_LIBCPP_INLINE_VISIBILITY
1495bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1496#endif
1497
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001498} // namespace __function
1499
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001500#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001501
1502namespace __function {
1503
Eric Fiselier125798e2018-12-10 18:14:09 +00001504// __alloc_func holds a functor and an allocator.
1505
1506template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001507template <class _Fp, class _FB>
1508class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001509
1510template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1511class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1512{
1513 __compressed_pair<_Fp, _Ap> __f_;
1514
1515 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001516 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1517 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001518
1519 _LIBCPP_INLINE_VISIBILITY
1520 const _Target& __target() const { return __f_.first(); }
1521
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001522 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001523 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001524 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001525
1526 _LIBCPP_INLINE_VISIBILITY
1527 explicit __alloc_func(_Target&& __f)
1528 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1529 _VSTD::forward_as_tuple())
1530 {
1531 }
1532
1533 _LIBCPP_INLINE_VISIBILITY
1534 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1535 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1536 _VSTD::forward_as_tuple(__a))
1537 {
1538 }
1539
1540 _LIBCPP_INLINE_VISIBILITY
1541 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1542 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1543 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1544 {
1545 }
1546
1547 _LIBCPP_INLINE_VISIBILITY
1548 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1549 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1550 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1551 {
1552 }
1553
1554 _LIBCPP_INLINE_VISIBILITY
1555 _Rp operator()(_ArgTypes&&... __arg)
1556 {
1557 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1558 return _Invoker::__call(__f_.first(),
1559 _VSTD::forward<_ArgTypes>(__arg)...);
1560 }
1561
1562 _LIBCPP_INLINE_VISIBILITY
1563 __alloc_func* __clone() const
1564 {
1565 typedef allocator_traits<_Alloc> __alloc_traits;
1566 typedef
1567 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1568 _AA;
1569 _AA __a(__f_.second());
1570 typedef __allocator_destructor<_AA> _Dp;
1571 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1572 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1573 return __hold.release();
1574 }
1575
1576 _LIBCPP_INLINE_VISIBILITY
1577 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001578
1579 static void __destroy_and_delete(__alloc_func* __f) {
1580 typedef allocator_traits<_Alloc> __alloc_traits;
1581 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1582 _FunAlloc;
1583 _FunAlloc __a(__f->__get_allocator());
1584 __f->destroy();
1585 __a.deallocate(__f, 1);
1586 }
1587};
1588
1589template <class _Fp, class _Rp, class... _ArgTypes>
1590class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1591 _Fp __f_;
1592
1593public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001594 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001595
1596 _LIBCPP_INLINE_VISIBILITY
1597 const _Target& __target() const { return __f_; }
1598
1599 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001600 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001601
1602 _LIBCPP_INLINE_VISIBILITY
1603 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1604
1605 _LIBCPP_INLINE_VISIBILITY
1606 _Rp operator()(_ArgTypes&&... __arg) {
1607 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1608 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1609 }
1610
1611 _LIBCPP_INLINE_VISIBILITY
1612 __default_alloc_func* __clone() const {
1613 __builtin_new_allocator::__holder_t __hold =
1614 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1615 __default_alloc_func* __res =
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001616 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00001617 (void)__hold.release();
1618 return __res;
1619 }
1620
1621 _LIBCPP_INLINE_VISIBILITY
1622 void destroy() _NOEXCEPT { __f_.~_Target(); }
1623
1624 static void __destroy_and_delete(__default_alloc_func* __f) {
1625 __f->destroy();
1626 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1627 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001628};
1629
1630// __base provides an abstract interface for copyable functors.
1631
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001632template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633
Howard Hinnantc834c512011-11-29 18:15:50 +00001634template<class _Rp, class ..._ArgTypes>
1635class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636{
1637 __base(const __base&);
1638 __base& operator=(const __base&);
1639public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001640 _LIBCPP_INLINE_VISIBILITY __base() {}
1641 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 virtual __base* __clone() const = 0;
1643 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001644 virtual void destroy() _NOEXCEPT = 0;
1645 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001646 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001647#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001648 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1649 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001650#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651};
1652
Eric Fiselier125798e2018-12-10 18:14:09 +00001653// __func implements __base for a given functor type.
1654
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655template<class _FD, class _Alloc, class _FB> class __func;
1656
Howard Hinnantc834c512011-11-29 18:15:50 +00001657template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1658class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1659 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660{
Eric Fiselier125798e2018-12-10 18:14:09 +00001661 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001664 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001665 : __f_(_VSTD::move(__f)) {}
1666
Howard Hinnant4ff57432010-09-21 22:55:27 +00001667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001668 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001669 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001670
1671 _LIBCPP_INLINE_VISIBILITY
1672 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001673 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001674
1675 _LIBCPP_INLINE_VISIBILITY
1676 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001677 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1678
Howard Hinnantc834c512011-11-29 18:15:50 +00001679 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1680 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001681 virtual void destroy() _NOEXCEPT;
1682 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001683 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001684#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001685 virtual const void* target(const type_info&) const _NOEXCEPT;
1686 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001687#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688};
1689
Howard Hinnantc834c512011-11-29 18:15:50 +00001690template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1691__base<_Rp(_ArgTypes...)>*
1692__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001694 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001695 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001696 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001697 typedef __allocator_destructor<_Ap> _Dp;
1698 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001699 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 return __hold.release();
1701}
1702
Howard Hinnantc834c512011-11-29 18:15:50 +00001703template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704void
Howard Hinnantc834c512011-11-29 18:15:50 +00001705__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706{
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001707 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708}
1709
Howard Hinnantc834c512011-11-29 18:15:50 +00001710template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711void
Howard Hinnantc834c512011-11-29 18:15:50 +00001712__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713{
Eric Fiselier125798e2018-12-10 18:14:09 +00001714 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715}
1716
Howard Hinnantc834c512011-11-29 18:15:50 +00001717template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718void
Howard Hinnantc834c512011-11-29 18:15:50 +00001719__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001721 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001722 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001723 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001724 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725 __a.deallocate(this, 1);
1726}
1727
Howard Hinnantc834c512011-11-29 18:15:50 +00001728template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1729_Rp
1730__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731{
Eric Fiselier125798e2018-12-10 18:14:09 +00001732 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733}
1734
Howard Hinnant72f73582010-08-11 17:04:31 +00001735#ifndef _LIBCPP_NO_RTTI
1736
Howard Hinnantc834c512011-11-29 18:15:50 +00001737template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001739__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740{
Howard Hinnantc834c512011-11-29 18:15:50 +00001741 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001742 return &__f_.__target();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001743 return nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744}
1745
Howard Hinnantc834c512011-11-29 18:15:50 +00001746template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001748__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749{
Howard Hinnantc834c512011-11-29 18:15:50 +00001750 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751}
1752
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001753#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001754
Eric Fiselier125798e2018-12-10 18:14:09 +00001755// __value_func creates a value-type from a __func.
1756
1757template <class _Fp> class __value_func;
1758
1759template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1760{
1761 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1762
1763 typedef __base<_Rp(_ArgTypes...)> __func;
1764 __func* __f_;
1765
1766 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1767 {
1768 return reinterpret_cast<__func*>(p);
1769 }
1770
1771 public:
1772 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001773 __value_func() _NOEXCEPT : __f_(nullptr) {}
Eric Fiselier125798e2018-12-10 18:14:09 +00001774
1775 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001776 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001777 : __f_(nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001778 {
1779 typedef allocator_traits<_Alloc> __alloc_traits;
1780 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1781 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1782 _FunAlloc;
1783
1784 if (__function::__not_null(__f))
1785 {
1786 _FunAlloc __af(__a);
1787 if (sizeof(_Fun) <= sizeof(__buf_) &&
1788 is_nothrow_copy_constructible<_Fp>::value &&
1789 is_nothrow_copy_constructible<_FunAlloc>::value)
1790 {
1791 __f_ =
1792 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1793 }
1794 else
1795 {
1796 typedef __allocator_destructor<_FunAlloc> _Dp;
1797 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1798 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1799 __f_ = __hold.release();
1800 }
1801 }
1802 }
1803
Eric Fiselier74ebee62019-06-08 01:31:19 +00001804 template <class _Fp,
1805 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1806 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001807 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001808
Eric Fiselier125798e2018-12-10 18:14:09 +00001809 _LIBCPP_INLINE_VISIBILITY
1810 __value_func(const __value_func& __f)
1811 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001812 if (__f.__f_ == nullptr)
1813 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001814 else if ((void*)__f.__f_ == &__f.__buf_)
1815 {
1816 __f_ = __as_base(&__buf_);
1817 __f.__f_->__clone(__f_);
1818 }
1819 else
1820 __f_ = __f.__f_->__clone();
1821 }
1822
1823 _LIBCPP_INLINE_VISIBILITY
1824 __value_func(__value_func&& __f) _NOEXCEPT
1825 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001826 if (__f.__f_ == nullptr)
1827 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001828 else if ((void*)__f.__f_ == &__f.__buf_)
1829 {
1830 __f_ = __as_base(&__buf_);
1831 __f.__f_->__clone(__f_);
1832 }
1833 else
1834 {
1835 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001836 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001837 }
1838 }
1839
1840 _LIBCPP_INLINE_VISIBILITY
1841 ~__value_func()
1842 {
1843 if ((void*)__f_ == &__buf_)
1844 __f_->destroy();
1845 else if (__f_)
1846 __f_->destroy_deallocate();
1847 }
1848
1849 _LIBCPP_INLINE_VISIBILITY
1850 __value_func& operator=(__value_func&& __f)
1851 {
1852 *this = nullptr;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001853 if (__f.__f_ == nullptr)
1854 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001855 else if ((void*)__f.__f_ == &__f.__buf_)
1856 {
1857 __f_ = __as_base(&__buf_);
1858 __f.__f_->__clone(__f_);
1859 }
1860 else
1861 {
1862 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001863 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001864 }
1865 return *this;
1866 }
1867
1868 _LIBCPP_INLINE_VISIBILITY
1869 __value_func& operator=(nullptr_t)
1870 {
1871 __func* __f = __f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001872 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001873 if ((void*)__f == &__buf_)
1874 __f->destroy();
1875 else if (__f)
1876 __f->destroy_deallocate();
1877 return *this;
1878 }
1879
1880 _LIBCPP_INLINE_VISIBILITY
1881 _Rp operator()(_ArgTypes&&... __args) const
1882 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001883 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001884 __throw_bad_function_call();
1885 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1886 }
1887
1888 _LIBCPP_INLINE_VISIBILITY
1889 void swap(__value_func& __f) _NOEXCEPT
1890 {
1891 if (&__f == this)
1892 return;
1893 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1894 {
1895 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1896 __func* __t = __as_base(&__tempbuf);
1897 __f_->__clone(__t);
1898 __f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001899 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001900 __f.__f_->__clone(__as_base(&__buf_));
1901 __f.__f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001902 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001903 __f_ = __as_base(&__buf_);
1904 __t->__clone(__as_base(&__f.__buf_));
1905 __t->destroy();
1906 __f.__f_ = __as_base(&__f.__buf_);
1907 }
1908 else if ((void*)__f_ == &__buf_)
1909 {
1910 __f_->__clone(__as_base(&__f.__buf_));
1911 __f_->destroy();
1912 __f_ = __f.__f_;
1913 __f.__f_ = __as_base(&__f.__buf_);
1914 }
1915 else if ((void*)__f.__f_ == &__f.__buf_)
1916 {
1917 __f.__f_->__clone(__as_base(&__buf_));
1918 __f.__f_->destroy();
1919 __f.__f_ = __f_;
1920 __f_ = __as_base(&__buf_);
1921 }
1922 else
1923 _VSTD::swap(__f_, __f.__f_);
1924 }
1925
1926 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001927 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
Eric Fiselier125798e2018-12-10 18:14:09 +00001928
1929#ifndef _LIBCPP_NO_RTTI
1930 _LIBCPP_INLINE_VISIBILITY
1931 const std::type_info& target_type() const _NOEXCEPT
1932 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001933 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001934 return typeid(void);
1935 return __f_->target_type();
1936 }
1937
1938 template <typename _Tp>
1939 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1940 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001941 if (__f_ == nullptr)
1942 return nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001943 return (const _Tp*)__f_->target(typeid(_Tp));
1944 }
1945#endif // _LIBCPP_NO_RTTI
1946};
1947
Eric Fiselierf2e64362018-12-11 00:14:34 +00001948// Storage for a functor object, to be used with __policy to manage copy and
1949// destruction.
1950union __policy_storage
1951{
1952 mutable char __small[sizeof(void*) * 2];
1953 void* __large;
1954};
1955
1956// True if _Fun can safely be held in __policy_storage.__small.
1957template <typename _Fun>
1958struct __use_small_storage
1959 : public _VSTD::integral_constant<
1960 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001961 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Eric Fiselierf2e64362018-12-11 00:14:34 +00001962 _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1963 _VSTD::is_trivially_destructible<_Fun>::value> {};
1964
1965// Policy contains information about how to copy, destroy, and move the
1966// underlying functor. You can think of it as a vtable of sorts.
1967struct __policy
1968{
1969 // Used to copy or destroy __large values. null for trivial objects.
1970 void* (*const __clone)(const void*);
1971 void (*const __destroy)(void*);
1972
1973 // True if this is the null policy (no value).
1974 const bool __is_null;
1975
1976 // The target type. May be null if RTTI is disabled.
1977 const std::type_info* const __type_info;
1978
1979 // Returns a pointer to a static policy object suitable for the functor
1980 // type.
1981 template <typename _Fun>
1982 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1983 {
1984 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1985 }
1986
1987 _LIBCPP_INLINE_VISIBILITY
1988 static const __policy* __create_empty()
1989 {
1990 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1991 true,
1992#ifndef _LIBCPP_NO_RTTI
1993 &typeid(void)
1994#else
1995 nullptr
1996#endif
1997 };
1998 return &__policy_;
1999 }
2000
2001 private:
2002 template <typename _Fun> static void* __large_clone(const void* __s)
2003 {
2004 const _Fun* __f = static_cast<const _Fun*>(__s);
2005 return __f->__clone();
2006 }
2007
Eric Fiselier74ebee62019-06-08 01:31:19 +00002008 template <typename _Fun>
2009 static void __large_destroy(void* __s) {
2010 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002011 }
2012
2013 template <typename _Fun>
2014 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002015 __choose_policy(/* is_small = */ false_type) {
2016 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2017 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002018#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002019 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002020#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002021 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002022#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002023 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002024 return &__policy_;
2025 }
2026
2027 template <typename _Fun>
2028 _LIBCPP_INLINE_VISIBILITY static const __policy*
2029 __choose_policy(/* is_small = */ true_type)
2030 {
2031 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2032 nullptr, nullptr, false,
2033#ifndef _LIBCPP_NO_RTTI
2034 &typeid(typename _Fun::_Target)
2035#else
2036 nullptr
2037#endif
2038 };
2039 return &__policy_;
2040 }
2041};
2042
2043// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2044// faster for types that can be passed in registers.
2045template <typename _Tp>
2046using __fast_forward =
2047 typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2048
2049// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2050
2051template <class _Fp> struct __policy_invoker;
2052
2053template <class _Rp, class... _ArgTypes>
2054struct __policy_invoker<_Rp(_ArgTypes...)>
2055{
2056 typedef _Rp (*__Call)(const __policy_storage*,
2057 __fast_forward<_ArgTypes>...);
2058
2059 __Call __call_;
2060
2061 // Creates an invoker that throws bad_function_call.
2062 _LIBCPP_INLINE_VISIBILITY
2063 __policy_invoker() : __call_(&__call_empty) {}
2064
2065 // Creates an invoker that calls the given instance of __func.
2066 template <typename _Fun>
2067 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2068 {
2069 return __policy_invoker(&__call_impl<_Fun>);
2070 }
2071
2072 private:
2073 _LIBCPP_INLINE_VISIBILITY
2074 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2075
2076 static _Rp __call_empty(const __policy_storage*,
2077 __fast_forward<_ArgTypes>...)
2078 {
2079 __throw_bad_function_call();
2080 }
2081
2082 template <typename _Fun>
2083 static _Rp __call_impl(const __policy_storage* __buf,
2084 __fast_forward<_ArgTypes>... __args)
2085 {
2086 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2087 ? &__buf->__small
2088 : __buf->__large);
2089 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2090 }
2091};
2092
2093// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2094// copyable functor.
2095
2096template <class _Fp> class __policy_func;
2097
2098template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2099{
2100 // Inline storage for small objects.
2101 __policy_storage __buf_;
2102
2103 // Calls the value stored in __buf_. This could technically be part of
2104 // policy, but storing it here eliminates a level of indirection inside
2105 // operator().
2106 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2107 __invoker __invoker_;
2108
2109 // The policy that describes how to move / copy / destroy __buf_. Never
2110 // null, even if the function is empty.
2111 const __policy* __policy_;
2112
2113 public:
2114 _LIBCPP_INLINE_VISIBILITY
2115 __policy_func() : __policy_(__policy::__create_empty()) {}
2116
2117 template <class _Fp, class _Alloc>
2118 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2119 : __policy_(__policy::__create_empty())
2120 {
2121 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2122 typedef allocator_traits<_Alloc> __alloc_traits;
2123 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2124 _FunAlloc;
2125
2126 if (__function::__not_null(__f))
2127 {
2128 __invoker_ = __invoker::template __create<_Fun>();
2129 __policy_ = __policy::__create<_Fun>();
2130
2131 _FunAlloc __af(__a);
2132 if (__use_small_storage<_Fun>())
2133 {
2134 ::new ((void*)&__buf_.__small)
2135 _Fun(_VSTD::move(__f), _Alloc(__af));
2136 }
2137 else
2138 {
2139 typedef __allocator_destructor<_FunAlloc> _Dp;
2140 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2141 ::new ((void*)__hold.get())
2142 _Fun(_VSTD::move(__f), _Alloc(__af));
2143 __buf_.__large = __hold.release();
2144 }
2145 }
2146 }
2147
Eric Fiselier74ebee62019-06-08 01:31:19 +00002148 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2149 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2150 : __policy_(__policy::__create_empty()) {
2151 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2152
2153 if (__function::__not_null(__f)) {
2154 __invoker_ = __invoker::template __create<_Fun>();
2155 __policy_ = __policy::__create<_Fun>();
2156 if (__use_small_storage<_Fun>()) {
2157 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2158 } else {
2159 __builtin_new_allocator::__holder_t __hold =
2160 __builtin_new_allocator::__allocate_type<_Fun>(1);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002161 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
Eric Fiselier74ebee62019-06-08 01:31:19 +00002162 (void)__hold.release();
2163 }
2164 }
2165 }
2166
Eric Fiselierf2e64362018-12-11 00:14:34 +00002167 _LIBCPP_INLINE_VISIBILITY
2168 __policy_func(const __policy_func& __f)
2169 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2170 __policy_(__f.__policy_)
2171 {
2172 if (__policy_->__clone)
2173 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2174 }
2175
2176 _LIBCPP_INLINE_VISIBILITY
2177 __policy_func(__policy_func&& __f)
2178 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2179 __policy_(__f.__policy_)
2180 {
2181 if (__policy_->__destroy)
2182 {
2183 __f.__policy_ = __policy::__create_empty();
2184 __f.__invoker_ = __invoker();
2185 }
2186 }
2187
2188 _LIBCPP_INLINE_VISIBILITY
2189 ~__policy_func()
2190 {
2191 if (__policy_->__destroy)
2192 __policy_->__destroy(__buf_.__large);
2193 }
2194
2195 _LIBCPP_INLINE_VISIBILITY
2196 __policy_func& operator=(__policy_func&& __f)
2197 {
2198 *this = nullptr;
2199 __buf_ = __f.__buf_;
2200 __invoker_ = __f.__invoker_;
2201 __policy_ = __f.__policy_;
2202 __f.__policy_ = __policy::__create_empty();
2203 __f.__invoker_ = __invoker();
2204 return *this;
2205 }
2206
2207 _LIBCPP_INLINE_VISIBILITY
2208 __policy_func& operator=(nullptr_t)
2209 {
2210 const __policy* __p = __policy_;
2211 __policy_ = __policy::__create_empty();
2212 __invoker_ = __invoker();
2213 if (__p->__destroy)
2214 __p->__destroy(__buf_.__large);
2215 return *this;
2216 }
2217
2218 _LIBCPP_INLINE_VISIBILITY
2219 _Rp operator()(_ArgTypes&&... __args) const
2220 {
2221 return __invoker_.__call_(_VSTD::addressof(__buf_),
2222 _VSTD::forward<_ArgTypes>(__args)...);
2223 }
2224
2225 _LIBCPP_INLINE_VISIBILITY
2226 void swap(__policy_func& __f)
2227 {
2228 _VSTD::swap(__invoker_, __f.__invoker_);
2229 _VSTD::swap(__policy_, __f.__policy_);
2230 _VSTD::swap(__buf_, __f.__buf_);
2231 }
2232
2233 _LIBCPP_INLINE_VISIBILITY
2234 explicit operator bool() const _NOEXCEPT
2235 {
2236 return !__policy_->__is_null;
2237 }
2238
2239#ifndef _LIBCPP_NO_RTTI
2240 _LIBCPP_INLINE_VISIBILITY
2241 const std::type_info& target_type() const _NOEXCEPT
2242 {
2243 return *__policy_->__type_info;
2244 }
2245
2246 template <typename _Tp>
2247 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2248 {
2249 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2250 return nullptr;
2251 if (__policy_->__clone) // Out of line storage.
2252 return reinterpret_cast<const _Tp*>(__buf_.__large);
2253 else
2254 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2255 }
2256#endif // _LIBCPP_NO_RTTI
2257};
2258
Louis Dionne3a632922020-04-23 16:47:52 -04002259#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
Louis Dionnee2391d72020-04-22 13:58:17 -04002260
Louis Dionne91cf4442020-07-31 12:56:36 -04002261extern "C" void *_Block_copy(const void *);
2262extern "C" void _Block_release(const void *);
2263
Louis Dionnee2391d72020-04-22 13:58:17 -04002264template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2265class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2266 : public __base<_Rp(_ArgTypes...)>
2267{
2268 typedef _Rp1(^__block_type)(_ArgTypes1...);
2269 __block_type __f_;
2270
2271public:
2272 _LIBCPP_INLINE_VISIBILITY
2273 explicit __func(__block_type const& __f)
Louis Dionne91cf4442020-07-31 12:56:36 -04002274 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002275 { }
2276
2277 // [TODO] add && to save on a retain
2278
2279 _LIBCPP_INLINE_VISIBILITY
2280 explicit __func(__block_type __f, const _Alloc& /* unused */)
Louis Dionne91cf4442020-07-31 12:56:36 -04002281 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002282 { }
2283
2284 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2285 _LIBCPP_ASSERT(false,
2286 "Block pointers are just pointers, so they should always fit into "
2287 "std::function's small buffer optimization. This function should "
2288 "never be invoked.");
2289 return nullptr;
2290 }
2291
2292 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002293 ::new ((void*)__p) __func(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002294 }
2295
2296 virtual void destroy() _NOEXCEPT {
2297 if (__f_)
Louis Dionne91cf4442020-07-31 12:56:36 -04002298 _Block_release(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002299 __f_ = 0;
2300 }
2301
2302 virtual void destroy_deallocate() _NOEXCEPT {
2303 _LIBCPP_ASSERT(false,
2304 "Block pointers are just pointers, so they should always fit into "
2305 "std::function's small buffer optimization. This function should "
2306 "never be invoked.");
2307 }
2308
2309 virtual _Rp operator()(_ArgTypes&& ... __arg) {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05002310 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
Louis Dionnee2391d72020-04-22 13:58:17 -04002311 }
2312
2313#ifndef _LIBCPP_NO_RTTI
2314 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2315 if (__ti == typeid(__func::__block_type))
2316 return &__f_;
2317 return (const void*)nullptr;
2318 }
2319
2320 virtual const std::type_info& target_type() const _NOEXCEPT {
2321 return typeid(__func::__block_type);
2322 }
2323#endif // _LIBCPP_NO_RTTI
2324};
2325
2326#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
2327
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328} // __function
2329
Howard Hinnantc834c512011-11-29 18:15:50 +00002330template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002331class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002332 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2333 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002335#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002336 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002337#else
2338 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2339#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340
Eric Fiselier125798e2018-12-10 18:14:09 +00002341 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002342
Eric Fiselier3906a132019-06-23 20:28:29 +00002343 template <class _Fp, bool = _And<
2344 _IsNotSame<__uncvref_t<_Fp>, function>,
zoecarver3e722f22020-05-19 17:15:28 -07002345 __invokable<_Fp, _ArgTypes...>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002346 >::value>
2347 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002348 template <class _Fp>
2349 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002350 {
Arthur O'Dwyer4ba8e3d2021-01-11 16:29:17 -05002351 static const bool value = is_void<_Rp>::value ||
2352 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
2353 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002354 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002355 template <class _Fp>
2356 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002357 {
2358 static const bool value = false;
2359 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002360
2361 template <class _Fp>
zoecarver3e722f22020-05-19 17:15:28 -07002362 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002364 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365
Howard Hinnantf06d9262010-08-20 19:36:46 +00002366 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002367 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002368 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002369 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002370 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002372 function(function&&) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002373 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002374 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375
Marshall Clow3148f422016-10-13 21:06:03 +00002376#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002377 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002378 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002379 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002380 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002381 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002382 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002383 template<class _Alloc>
2384 function(allocator_arg_t, const _Alloc&, const function&);
2385 template<class _Alloc>
2386 function(allocator_arg_t, const _Alloc&, function&&);
zoecarver3e722f22020-05-19 17:15:28 -07002387 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002388 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002389#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390
2391 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002392 function& operator=(function&&) _NOEXCEPT;
2393 function& operator=(nullptr_t) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002394 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002395 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396
2397 ~function();
2398
Howard Hinnantf06d9262010-08-20 19:36:46 +00002399 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002400 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002401
2402#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002403 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002405 void assign(_Fp&& __f, const _Alloc& __a)
2406 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002407#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002408
Howard Hinnantf06d9262010-08-20 19:36:46 +00002409 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002410 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002411 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2412 return static_cast<bool>(__f_);
2413 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415 // deleted overloads close possible hole in the type system
2416 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002417 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002419 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002421 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002422 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423
Howard Hinnant72f73582010-08-11 17:04:31 +00002424#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002425 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002426 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002427 template <typename _Tp> _Tp* target() _NOEXCEPT;
2428 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002429#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430};
2431
Louis Dionne4af49712019-07-18 19:50:56 +00002432#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2433template<class _Rp, class ..._Ap>
2434function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2435
2436template<class _Fp>
2437struct __strip_signature;
2438
2439template<class _Rp, class _Gp, class ..._Ap>
2440struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2441template<class _Rp, class _Gp, class ..._Ap>
2442struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2443template<class _Rp, class _Gp, class ..._Ap>
2444struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2445template<class _Rp, class _Gp, class ..._Ap>
2446struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2447
2448template<class _Rp, class _Gp, class ..._Ap>
2449struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2450template<class _Rp, class _Gp, class ..._Ap>
2451struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2452template<class _Rp, class _Gp, class ..._Ap>
2453struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2454template<class _Rp, class _Gp, class ..._Ap>
2455struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2456
2457template<class _Rp, class _Gp, class ..._Ap>
2458struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2459template<class _Rp, class _Gp, class ..._Ap>
2460struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2461template<class _Rp, class _Gp, class ..._Ap>
2462struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2463template<class _Rp, class _Gp, class ..._Ap>
2464struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2465
2466template<class _Rp, class _Gp, class ..._Ap>
2467struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2468template<class _Rp, class _Gp, class ..._Ap>
2469struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2470template<class _Rp, class _Gp, class ..._Ap>
2471struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2472template<class _Rp, class _Gp, class ..._Ap>
2473struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2474
2475template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2476function(_Fp) -> function<_Stripped>;
2477#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2478
Howard Hinnantc834c512011-11-29 18:15:50 +00002479template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002480function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481
Marshall Clow3148f422016-10-13 21:06:03 +00002482#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002483template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002484template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002485function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002486 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002487#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002488
Eric Fiselier125798e2018-12-10 18:14:09 +00002489template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002490function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002491 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492
Marshall Clow3148f422016-10-13 21:06:03 +00002493#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002494template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002495template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002496function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002497 function&& __f)
2498 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002499#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002500
Eric Fiselier125798e2018-12-10 18:14:09 +00002501template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002502template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002503function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504
Marshall Clow3148f422016-10-13 21:06:03 +00002505#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002506template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002507template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002508function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2509 _Fp __f)
2510 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002511#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002512
Howard Hinnantc834c512011-11-29 18:15:50 +00002513template<class _Rp, class ..._ArgTypes>
2514function<_Rp(_ArgTypes...)>&
2515function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516{
2517 function(__f).swap(*this);
2518 return *this;
2519}
2520
Howard Hinnantc834c512011-11-29 18:15:50 +00002521template<class _Rp, class ..._ArgTypes>
2522function<_Rp(_ArgTypes...)>&
2523function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002525 __f_ = _VSTD::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002526 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527}
2528
Howard Hinnantc834c512011-11-29 18:15:50 +00002529template<class _Rp, class ..._ArgTypes>
2530function<_Rp(_ArgTypes...)>&
2531function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532{
Eric Fiselier125798e2018-12-10 18:14:09 +00002533 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002534 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535}
2536
Howard Hinnantc834c512011-11-29 18:15:50 +00002537template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002538template <class _Fp, class>
2539function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002540function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541{
Howard Hinnantc834c512011-11-29 18:15:50 +00002542 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543 return *this;
2544}
2545
Howard Hinnantc834c512011-11-29 18:15:50 +00002546template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002547function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548
Howard Hinnantc834c512011-11-29 18:15:50 +00002549template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550void
Howard Hinnantc834c512011-11-29 18:15:50 +00002551function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552{
Eric Fiselier125798e2018-12-10 18:14:09 +00002553 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554}
2555
Howard Hinnantc834c512011-11-29 18:15:50 +00002556template<class _Rp, class ..._ArgTypes>
2557_Rp
2558function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559{
Eric Fiselier125798e2018-12-10 18:14:09 +00002560 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561}
2562
Howard Hinnant72f73582010-08-11 17:04:31 +00002563#ifndef _LIBCPP_NO_RTTI
2564
Howard Hinnantc834c512011-11-29 18:15:50 +00002565template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002567function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568{
Eric Fiselier125798e2018-12-10 18:14:09 +00002569 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570}
2571
Howard Hinnantc834c512011-11-29 18:15:50 +00002572template<class _Rp, class ..._ArgTypes>
2573template <typename _Tp>
2574_Tp*
2575function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002576{
Eric Fiselier125798e2018-12-10 18:14:09 +00002577 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578}
2579
Howard Hinnantc834c512011-11-29 18:15:50 +00002580template<class _Rp, class ..._ArgTypes>
2581template <typename _Tp>
2582const _Tp*
2583function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584{
Eric Fiselier125798e2018-12-10 18:14:09 +00002585 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586}
2587
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002588#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002589
Howard Hinnantc834c512011-11-29 18:15:50 +00002590template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591inline _LIBCPP_INLINE_VISIBILITY
2592bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002593operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594
Howard Hinnantc834c512011-11-29 18:15:50 +00002595template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596inline _LIBCPP_INLINE_VISIBILITY
2597bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002598operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599
Howard Hinnantc834c512011-11-29 18:15:50 +00002600template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601inline _LIBCPP_INLINE_VISIBILITY
2602bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002603operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604
Howard Hinnantc834c512011-11-29 18:15:50 +00002605template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606inline _LIBCPP_INLINE_VISIBILITY
2607bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002608operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609
Howard Hinnantc834c512011-11-29 18:15:50 +00002610template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611inline _LIBCPP_INLINE_VISIBILITY
2612void
Howard Hinnantc834c512011-11-29 18:15:50 +00002613swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614{return __x.swap(__y);}
2615
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002616#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002617
2618#include <__functional_03>
2619
2620#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002621
2622////////////////////////////////////////////////////////////////////////////////
2623// BIND
2624//==============================================================================
2625
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002627template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2629
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002630#if _LIBCPP_STD_VER > 14
2631template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002632_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002633#endif
2634
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002636template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2638
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002639#if _LIBCPP_STD_VER > 14
2640template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002641_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002642#endif
2643
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644namespace placeholders
2645{
2646
Howard Hinnantc834c512011-11-29 18:15:50 +00002647template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002649#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002650_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2651_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2652_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2653_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2654_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2655_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2656_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2657_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2658_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2659_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2660#else
Marshall Clow396b2132018-01-02 19:01:45 +00002661/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2662/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2663/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2664/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2665/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2666/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2667/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2668/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2669/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2670/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002671#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672
2673} // placeholders
2674
Howard Hinnantc834c512011-11-29 18:15:50 +00002675template<int _Np>
2676struct __is_placeholder<placeholders::__ph<_Np> >
2677 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002679
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002680#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002681
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682template <class _Tp, class _Uj>
2683inline _LIBCPP_INLINE_VISIBILITY
2684_Tp&
2685__mu(reference_wrapper<_Tp> __t, _Uj&)
2686{
2687 return __t.get();
2688}
2689
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690template <class _Ti, class ..._Uj, size_t ..._Indx>
2691inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002692typename __invoke_of<_Ti&, _Uj...>::type
2693__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002695 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696}
2697
2698template <class _Ti, class ..._Uj>
2699inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002700typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701<
2702 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002703 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704>::type
2705__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2706{
2707 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002708 return _VSTD::__mu_expand(__ti, __uj, __indices());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002709}
2710
2711template <bool IsPh, class _Ti, class _Uj>
2712struct __mu_return2 {};
2713
2714template <class _Ti, class _Uj>
2715struct __mu_return2<true, _Ti, _Uj>
2716{
2717 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2718};
2719
2720template <class _Ti, class _Uj>
2721inline _LIBCPP_INLINE_VISIBILITY
2722typename enable_if
2723<
2724 0 < is_placeholder<_Ti>::value,
2725 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2726>::type
2727__mu(_Ti&, _Uj& __uj)
2728{
2729 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002730 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731}
2732
2733template <class _Ti, class _Uj>
2734inline _LIBCPP_INLINE_VISIBILITY
2735typename enable_if
2736<
2737 !is_bind_expression<_Ti>::value &&
2738 is_placeholder<_Ti>::value == 0 &&
2739 !__is_reference_wrapper<_Ti>::value,
2740 _Ti&
2741>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002742__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743{
2744 return __ti;
2745}
2746
Howard Hinnant0415d792011-05-22 15:07:43 +00002747template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2748 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002749struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002751template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002752struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002753{
2754 typedef __nat type;
2755};
2756
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002758struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002760 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761};
2762
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002763template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002764struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2765 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002766{
2767};
2768
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002770struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771{
2772 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2773 _TupleUj>::type&& type;
2774};
2775
2776template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002777struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002778{
2779 typedef typename _Ti::type& type;
2780};
2781
2782template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002783struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784{
2785 typedef _Ti& type;
2786};
2787
2788template <class _Ti, class _TupleUj>
2789struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002790 : public __mu_return_impl<_Ti,
2791 __is_reference_wrapper<_Ti>::value,
2792 is_bind_expression<_Ti>::value,
2793 0 < is_placeholder<_Ti>::value &&
2794 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2795 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796{
2797};
2798
Howard Hinnantc834c512011-11-29 18:15:50 +00002799template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002800struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002801{
2802 static const bool value = false;
2803};
2804
2805template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002806struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002807{
2808 static const bool value = __invokable<_Fp,
2809 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2810};
2811
2812template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002813struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002814{
2815 static const bool value = __invokable<_Fp,
2816 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2817};
2818
2819template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002820 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821struct __bind_return;
2822
Howard Hinnantc834c512011-11-29 18:15:50 +00002823template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002824struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002826 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002828 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 typename __mu_return
2830 <
2831 _BoundArgs,
2832 _TupleUj
2833 >::type...
2834 >::type type;
2835};
2836
Howard Hinnantc834c512011-11-29 18:15:50 +00002837template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002838struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002840 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002842 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 typename __mu_return
2844 <
2845 const _BoundArgs,
2846 _TupleUj
2847 >::type...
2848 >::type type;
2849};
2850
Howard Hinnantc834c512011-11-29 18:15:50 +00002851template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002853typename __bind_return<_Fp, _BoundArgs, _Args>::type
2854__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 _Args&& __args)
2856{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002857 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858}
2859
Howard Hinnantc834c512011-11-29 18:15:50 +00002860template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002862 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002864protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002865 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002866 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002867private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002868 _Fd __f_;
2869 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870
2871 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2872public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002873 template <class _Gp, class ..._BA,
2874 class = typename enable_if
2875 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002876 is_constructible<_Fd, _Gp>::value &&
2877 !is_same<typename remove_reference<_Gp>::type,
2878 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002879 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002880 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002881 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2882 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002883 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884
2885 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002886 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002887 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 operator()(_Args&& ...__args)
2889 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002890 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002891 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 }
2893
2894 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002895 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002896 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897 operator()(_Args&& ...__args) const
2898 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002899 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002900 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901 }
2902};
2903
Howard Hinnantc834c512011-11-29 18:15:50 +00002904template<class _Fp, class ..._BoundArgs>
2905struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906
Howard Hinnantc834c512011-11-29 18:15:50 +00002907template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002909 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910{
Howard Hinnantc834c512011-11-29 18:15:50 +00002911 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002912 typedef typename base::_Fd _Fd;
2913 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002915 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916
Howard Hinnant7091e652011-07-02 18:22:36 +00002917
Howard Hinnantf292a922013-07-01 00:01:51 +00002918 template <class _Gp, class ..._BA,
2919 class = typename enable_if
2920 <
2921 is_constructible<_Fd, _Gp>::value &&
2922 !is_same<typename remove_reference<_Gp>::type,
2923 __bind_r>::value
2924 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002925 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002926 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2927 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002928 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002929
2930 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002931 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002932 typename enable_if
2933 <
2934 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002935 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002936 result_type
2937 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938 operator()(_Args&& ...__args)
2939 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002940 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2941 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942 }
2943
2944 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002945 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002946 typename enable_if
2947 <
2948 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002949 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002950 result_type
2951 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952 operator()(_Args&& ...__args) const
2953 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002954 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2955 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956 }
2957};
2958
Howard Hinnantc834c512011-11-29 18:15:50 +00002959template<class _Rp, class _Fp, class ..._BoundArgs>
2960struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961
Howard Hinnantc834c512011-11-29 18:15:50 +00002962template<class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002963inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002964__bind<_Fp, _BoundArgs...>
2965bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966{
Howard Hinnantc834c512011-11-29 18:15:50 +00002967 typedef __bind<_Fp, _BoundArgs...> type;
2968 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969}
2970
Howard Hinnantc834c512011-11-29 18:15:50 +00002971template<class _Rp, class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002972inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002973__bind_r<_Rp, _Fp, _BoundArgs...>
2974bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975{
Howard Hinnantc834c512011-11-29 18:15:50 +00002976 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2977 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978}
2979
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002980#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981
Eric Fiselier0d974f12015-07-14 20:16:15 +00002982#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002983
Eric Fiselier0d974f12015-07-14 20:16:15 +00002984template <class _Fn, class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002985_LIBCPP_CONSTEXPR_AFTER_CXX17 invoke_result_t<_Fn, _Args...>
Eric Fiselier934f63b2016-06-02 01:25:41 +00002986invoke(_Fn&& __f, _Args&&... __args)
Louis Dionneaf34f122019-04-03 17:54:37 +00002987 noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
Eric Fiselier934f63b2016-06-02 01:25:41 +00002988{
2989 return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
Eric Fiselier0d974f12015-07-14 20:16:15 +00002990}
Eric Fiselier934f63b2016-06-02 01:25:41 +00002991
2992template <class _DecayFunc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002993class _LIBCPP_TEMPLATE_VIS __not_fn_imp {
Eric Fiselier934f63b2016-06-02 01:25:41 +00002994 _DecayFunc __fd;
2995
2996public:
2997 __not_fn_imp() = delete;
2998
2999 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003000 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere8303a32016-06-27 00:40:41 +00003001 auto operator()(_Args&& ...__args) &
Eric Fiselier934f63b2016-06-02 01:25:41 +00003002 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003003 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
3004 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003005
3006 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003007 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere8303a32016-06-27 00:40:41 +00003008 auto operator()(_Args&& ...__args) &&
3009 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003010 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
3011 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00003012
3013 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003014 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere8303a32016-06-27 00:40:41 +00003015 auto operator()(_Args&& ...__args) const&
Eric Fiselier934f63b2016-06-02 01:25:41 +00003016 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003017 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
3018 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003019
Eric Fiseliere8303a32016-06-27 00:40:41 +00003020
3021 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003022 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiseliere8303a32016-06-27 00:40:41 +00003023 auto operator()(_Args&& ...__args) const&&
3024 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00003025 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
3026 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00003027
Eric Fiselier934f63b2016-06-02 01:25:41 +00003028private:
3029 template <class _RawFunc,
3030 class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003031 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier934f63b2016-06-02 01:25:41 +00003032 explicit __not_fn_imp(_RawFunc&& __rf)
3033 : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
3034
3035 template <class _RawFunc>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003036 friend inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier934f63b2016-06-02 01:25:41 +00003037 __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
3038};
3039
3040template <class _RawFunc>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003041inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier934f63b2016-06-02 01:25:41 +00003042__not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
3043 return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
3044}
3045
Eric Fiselier0d974f12015-07-14 20:16:15 +00003046#endif
3047
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003048// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049
Marshall Clowa40686b2018-01-08 19:18:00 +00003050template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00003051pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00003052__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3053 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
3054 forward_iterator_tag, forward_iterator_tag)
3055{
3056 if (__first2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003057 return _VSTD::make_pair(__first1, __first1); // Everything matches an empty sequence
Marshall Clowa40686b2018-01-08 19:18:00 +00003058 while (true)
3059 {
3060 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
3061 while (true)
3062 {
3063 if (__first1 == __last1) // return __last1 if no element matches *__first2
Logan Smith4528b5f2020-05-07 11:54:25 -04003064 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003065 if (__pred(*__first1, *__first2))
3066 break;
3067 ++__first1;
3068 }
3069 // *__first1 matches *__first2, now match elements after here
3070 _ForwardIterator1 __m1 = __first1;
3071 _ForwardIterator2 __m2 = __first2;
3072 while (true)
3073 {
3074 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Logan Smith4528b5f2020-05-07 11:54:25 -04003075 return _VSTD::make_pair(__first1, __m1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003076 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Logan Smith4528b5f2020-05-07 11:54:25 -04003077 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003078 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
3079 {
3080 ++__first1;
3081 break;
3082 } // else there is a match, check next elements
3083 }
3084 }
3085}
3086
3087template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
3088_LIBCPP_CONSTEXPR_AFTER_CXX11
3089pair<_RandomAccessIterator1, _RandomAccessIterator1>
3090__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
3091 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
3092 random_access_iterator_tag, random_access_iterator_tag)
3093{
3094 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
3095 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
3096 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
3097 const _D2 __len2 = __last2 - __first2;
3098 if (__len2 == 0)
Logan Smith4528b5f2020-05-07 11:54:25 -04003099 return _VSTD::make_pair(__first1, __first1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003100 const _D1 __len1 = __last1 - __first1;
3101 if (__len1 < __len2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003102 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003103 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
3104
3105 while (true)
3106 {
3107 while (true)
3108 {
3109 if (__first1 == __s)
Logan Smith4528b5f2020-05-07 11:54:25 -04003110 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003111 if (__pred(*__first1, *__first2))
3112 break;
3113 ++__first1;
3114 }
3115
3116 _RandomAccessIterator1 __m1 = __first1;
3117 _RandomAccessIterator2 __m2 = __first2;
3118 while (true)
3119 {
3120 if (++__m2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003121 return _VSTD::make_pair(__first1, __first1 + __len2);
Marshall Clowa40686b2018-01-08 19:18:00 +00003122 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
3123 if (!__pred(*__m1, *__m2))
3124 {
3125 ++__first1;
3126 break;
3127 }
3128 }
3129 }
3130}
3131
3132#if _LIBCPP_STD_VER > 14
3133
3134// default searcher
3135template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Dimitry Andricfd3633d2018-02-13 17:40:59 +00003136class _LIBCPP_TYPE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00003137public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003138 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne44bcff92018-08-03 22:36:53 +00003139 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00003140 _BinaryPredicate __p = _BinaryPredicate())
3141 : __first_(__f), __last_(__l), __pred_(__p) {}
3142
3143 template <typename _ForwardIterator2>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003144 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00003145 pair<_ForwardIterator2, _ForwardIterator2>
3146 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3147 {
3148 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
3149 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
3150 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
3151 }
3152
3153private:
3154 _ForwardIterator __first_;
3155 _ForwardIterator __last_;
3156 _BinaryPredicate __pred_;
Eric Fiselier80663c52019-08-04 07:13:43 +00003157 };
Marshall Clowa40686b2018-01-08 19:18:00 +00003158
3159#endif // _LIBCPP_STD_VER > 14
3160
Louis Dionnedb1892a2018-12-03 14:03:27 +00003161#if _LIBCPP_STD_VER > 17
3162template <class _Tp>
3163using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3164
3165template <class _Tp>
3166using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3167#endif // > C++17
3168
Marshall Clow29b53f22018-12-14 18:49:35 +00003169template <class _Container, class _Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +02003170inline typename _Container::size_type
3171__libcpp_erase_if_container(_Container& __c, _Predicate __pred) {
3172 typename _Container::size_type __old_size = __c.size();
3173
3174 const typename _Container::iterator __last = __c.end();
3175 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
3176 if (__pred(*__iter))
3177 __iter = __c.erase(__iter);
3178 else
3179 ++__iter;
3180 }
3181
3182 return __old_size - __c.size();
Marshall Clow29b53f22018-12-14 18:49:35 +00003183}
3184
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185_LIBCPP_END_NAMESPACE_STD
3186
3187#endif // _LIBCPP_FUNCTIONAL