blob: 086610e61902eb6de9ddcfb8d04d09a4d770904d [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------ functional ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FUNCTIONAL
11#define _LIBCPP_FUNCTIONAL
12
13/*
14 functional synopsis
15
16namespace std
17{
18
19template <class Arg, class Result>
20struct unary_function
21{
22 typedef Arg argument_type;
23 typedef Result result_type;
24};
25
26template <class Arg1, class Arg2, class Result>
27struct binary_function
28{
29 typedef Arg1 first_argument_type;
30 typedef Arg2 second_argument_type;
31 typedef Result result_type;
32};
33
Howard Hinnantf06d9262010-08-20 19:36:46 +000034template <class T>
Howard Hinnantc51e1022010-05-11 19:42:16 +000035class reference_wrapper
36 : public unary_function<T1, R> // if wrapping a unary functor
37 : public binary_function<T1, T2, R> // if wraping a binary functor
38{
39public:
40 // types
41 typedef T type;
42 typedef see below result_type; // Not always defined
43
44 // construct/copy/destroy
Howard Hinnantf7724cd2011-05-28 17:59:48 +000045 reference_wrapper(T&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000046 reference_wrapper(T&&) = delete; // do not bind to temps
Howard Hinnantf7724cd2011-05-28 17:59:48 +000047 reference_wrapper(const reference_wrapper<T>& x) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000048
49 // assignment
Howard Hinnantf7724cd2011-05-28 17:59:48 +000050 reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000051
52 // access
Howard Hinnantf7724cd2011-05-28 17:59:48 +000053 operator T& () const noexcept;
54 T& get() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 // invoke
57 template <class... ArgTypes>
Howard Hinnantc9c775b2013-09-21 17:58:58 +000058 typename result_of<T&(ArgTypes&&...)>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +000059 operator() (ArgTypes&&...) const;
60};
61
Howard Hinnantf7724cd2011-05-28 17:59:48 +000062template <class T> reference_wrapper<T> ref(T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000063template <class T> void ref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000064template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000065
Howard Hinnantf7724cd2011-05-28 17:59:48 +000066template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000067template <class T> void cref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000068template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000069
Louis Dionnedb1892a2018-12-03 14:03:27 +000070template <class T> struct unwrap_reference; // since C++20
71template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { }; // since C++20
72template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
73template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
74
Marshall Clow974bae22013-07-29 14:21:53 +000075template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000076struct plus : binary_function<T, T, T>
77{
78 T operator()(const T& x, const T& y) const;
79};
80
Marshall Clow974bae22013-07-29 14:21:53 +000081template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000082struct minus : binary_function<T, T, T>
83{
84 T operator()(const T& x, const T& y) const;
85};
86
Marshall Clow974bae22013-07-29 14:21:53 +000087template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000088struct multiplies : binary_function<T, T, T>
89{
90 T operator()(const T& x, const T& y) const;
91};
92
Marshall Clow974bae22013-07-29 14:21:53 +000093template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000094struct divides : binary_function<T, T, T>
95{
96 T operator()(const T& x, const T& y) const;
97};
98
Marshall Clow974bae22013-07-29 14:21:53 +000099template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100struct modulus : binary_function<T, T, T>
101{
102 T operator()(const T& x, const T& y) const;
103};
104
Marshall Clow974bae22013-07-29 14:21:53 +0000105template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106struct negate : unary_function<T, T>
107{
108 T operator()(const T& x) const;
109};
110
Marshall Clow974bae22013-07-29 14:21:53 +0000111template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112struct equal_to : binary_function<T, T, bool>
113{
114 bool operator()(const T& x, const T& y) const;
115};
116
Marshall Clow974bae22013-07-29 14:21:53 +0000117template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118struct not_equal_to : binary_function<T, T, bool>
119{
120 bool operator()(const T& x, const T& y) const;
121};
122
Marshall Clow974bae22013-07-29 14:21:53 +0000123template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124struct greater : binary_function<T, T, bool>
125{
126 bool operator()(const T& x, const T& y) const;
127};
128
Marshall Clow974bae22013-07-29 14:21:53 +0000129template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130struct less : binary_function<T, T, bool>
131{
132 bool operator()(const T& x, const T& y) const;
133};
134
Marshall Clow974bae22013-07-29 14:21:53 +0000135template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136struct greater_equal : binary_function<T, T, bool>
137{
138 bool operator()(const T& x, const T& y) const;
139};
140
Marshall Clow974bae22013-07-29 14:21:53 +0000141template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142struct less_equal : binary_function<T, T, bool>
143{
144 bool operator()(const T& x, const T& y) const;
145};
146
Marshall Clow974bae22013-07-29 14:21:53 +0000147template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148struct logical_and : binary_function<T, T, bool>
149{
150 bool operator()(const T& x, const T& y) const;
151};
152
Marshall Clow974bae22013-07-29 14:21:53 +0000153template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154struct logical_or : binary_function<T, T, bool>
155{
156 bool operator()(const T& x, const T& y) const;
157};
158
Marshall Clow974bae22013-07-29 14:21:53 +0000159template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160struct logical_not : unary_function<T, bool>
161{
162 bool operator()(const T& x) const;
163};
164
Marshall Clow974bae22013-07-29 14:21:53 +0000165template <class T> // <class T=void> in C++14
166struct bit_and : unary_function<T, bool>
167{
168 bool operator()(const T& x, const T& y) const;
169};
170
171template <class T> // <class T=void> in C++14
172struct bit_or : unary_function<T, bool>
173{
174 bool operator()(const T& x, const T& y) const;
175};
176
177template <class T> // <class T=void> in C++14
178struct bit_xor : unary_function<T, bool>
179{
180 bool operator()(const T& x, const T& y) const;
181};
182
183template <class T=void> // C++14
184struct bit_xor : unary_function<T, bool>
185{
186 bool operator()(const T& x) const;
187};
188
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000190class unary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 : public unary_function<typename Predicate::argument_type, bool>
192{
193public:
194 explicit unary_negate(const Predicate& pred);
195 bool operator()(const typename Predicate::argument_type& x) const;
196};
197
Louis Dionne481a2662018-09-23 18:35:00 +0000198template <class Predicate> // deprecated in C++17
199unary_negate<Predicate> not1(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
201template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000202class binary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203 : public binary_function<typename Predicate::first_argument_type,
204 typename Predicate::second_argument_type,
205 bool>
206{
207public:
208 explicit binary_negate(const Predicate& pred);
209 bool operator()(const typename Predicate::first_argument_type& x,
210 const typename Predicate::second_argument_type& y) const;
211};
212
Louis Dionne481a2662018-09-23 18:35:00 +0000213template <class Predicate> // deprecated in C++17
214binary_negate<Predicate> not2(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
Eric Fiselier934f63b2016-06-02 01:25:41 +0000216template <class F> unspecified not_fn(F&& f); // C++17
217
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218template<class T> struct is_bind_expression;
219template<class T> struct is_placeholder;
220
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000221 // See C++14 20.9.9, Function object binders
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000222template <class T> inline constexpr bool is_bind_expression_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000223 = is_bind_expression<T>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000224template <class T> inline constexpr int is_placeholder_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000225 = is_placeholder<T>::value; // C++17
226
227
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000228template<class Fn, class... BoundArgs>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000229 unspecified bind(Fn&&, BoundArgs&&...);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000230template<class R, class Fn, class... BoundArgs>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000231 unspecified bind(Fn&&, BoundArgs&&...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000233namespace placeholders {
234 // M is the implementation-defined number of placeholders
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 extern unspecified _1;
236 extern unspecified _2;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000237 .
238 .
239 .
Howard Hinnantc834c512011-11-29 18:15:50 +0000240 extern unspecified _Mp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241}
242
243template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000244class binder1st // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245 : public unary_function<typename Operation::second_argument_type,
246 typename Operation::result_type>
247{
248protected:
249 Operation op;
250 typename Operation::first_argument_type value;
251public:
252 binder1st(const Operation& x, const typename Operation::first_argument_type y);
253 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
254 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
255};
256
257template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000258binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
260template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000261class binder2nd // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262 : public unary_function<typename Operation::first_argument_type,
263 typename Operation::result_type>
264{
265protected:
266 Operation op;
267 typename Operation::second_argument_type value;
268public:
269 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
270 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
271 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
272};
273
274template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000275binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276
Marshall Clow26a027c2017-04-13 18:25:32 +0000277template <class Arg, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278class pointer_to_unary_function : public unary_function<Arg, Result>
279{
280public:
281 explicit pointer_to_unary_function(Result (*f)(Arg));
282 Result operator()(Arg x) const;
283};
284
285template <class Arg, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000286pointer_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 +0000287
Marshall Clow26a027c2017-04-13 18:25:32 +0000288template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
290{
291public:
292 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
293 Result operator()(Arg1 x, Arg2 y) const;
294};
295
296template <class Arg1, class Arg2, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000297pointer_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 +0000298
Marshall Clow26a027c2017-04-13 18:25:32 +0000299template<class S, class T> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300class mem_fun_t : public unary_function<T*, S>
301{
302public:
303 explicit mem_fun_t(S (T::*p)());
304 S operator()(T* p) const;
305};
306
307template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000308class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309{
310public:
311 explicit mem_fun1_t(S (T::*p)(A));
312 S operator()(T* p, A x) const;
313};
314
Marshall Clow26a027c2017-04-13 18:25:32 +0000315template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
316template<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 +0000317
318template<class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000319class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320{
321public:
322 explicit mem_fun_ref_t(S (T::*p)());
323 S operator()(T& p) const;
324};
325
326template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000327class 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 +0000328{
329public:
330 explicit mem_fun1_ref_t(S (T::*p)(A));
331 S operator()(T& p, A x) const;
332};
333
Marshall Clow26a027c2017-04-13 18:25:32 +0000334template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
335template<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 +0000336
337template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000338class 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 +0000339{
340public:
341 explicit const_mem_fun_t(S (T::*p)() const);
342 S operator()(const T* p) const;
343};
344
345template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000346class 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 +0000347{
348public:
349 explicit const_mem_fun1_t(S (T::*p)(A) const);
350 S operator()(const T* p, A x) const;
351};
352
Marshall Clow26a027c2017-04-13 18:25:32 +0000353template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
354template <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 +0000355
356template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000357class 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 +0000358{
359public:
360 explicit const_mem_fun_ref_t(S (T::*p)() const);
361 S operator()(const T& p) const;
362};
363
364template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000365class 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 +0000366{
367public:
368 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
369 S operator()(const T& p, A x) const;
370};
371
Marshall Clow26a027c2017-04-13 18:25:32 +0000372template <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
373template <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 +0000374
Howard Hinnantf06d9262010-08-20 19:36:46 +0000375template<class R, class T> unspecified mem_fn(R T::*);
Howard Hinnantf06d9262010-08-20 19:36:46 +0000376
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377class bad_function_call
378 : public exception
379{
380};
381
Howard Hinnantf06d9262010-08-20 19:36:46 +0000382template<class> class function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
Howard Hinnantf06d9262010-08-20 19:36:46 +0000384template<class R, class... ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385class function<R(ArgTypes...)>
386 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
387 // ArgTypes contains T1
388 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
389 // ArgTypes contains T1 and T2
390{
391public:
392 typedef R result_type;
393
Howard Hinnantf06d9262010-08-20 19:36:46 +0000394 // construct/copy/destroy:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000395 function() noexcept;
396 function(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000398 function(function&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 template<class F>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400 function(F);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000402 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000404 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000406 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000408 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 template<class F, Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000410 function(allocator_arg_t, const Alloc&, F); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000413 function& operator=(function&&) noexcept;
Howard Hinnant7b85be02011-05-29 13:53:56 +0000414 function& operator=(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 template<class F>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000416 function& operator=(F&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417 template<class F>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000418 function& operator=(reference_wrapper<F>) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420 ~function();
421
Howard Hinnantf06d9262010-08-20 19:36:46 +0000422 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000423 void swap(function&) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +0000424 template<class F, class Alloc>
Marshall Clowfc8fd832016-01-25 17:29:55 +0000425 void assign(F&&, const Alloc&); // Removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
Howard Hinnantf06d9262010-08-20 19:36:46 +0000427 // function capacity:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000428 explicit operator bool() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
Howard Hinnantf06d9262010-08-20 19:36:46 +0000430 // function invocation:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431 R operator()(ArgTypes...) const;
432
Howard Hinnantf06d9262010-08-20 19:36:46 +0000433 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000434 const std::type_info& target_type() const noexcept;
435 template <typename T> T* target() noexcept;
436 template <typename T> const T* target() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437};
438
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000439// Null pointer comparisons:
440template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000441 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000443template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000444 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000446template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000447 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000449template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000450 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000452// specialized algorithms:
453template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000454 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455
456template <class T> struct hash;
457
458template <> struct hash<bool>;
459template <> struct hash<char>;
460template <> struct hash<signed char>;
461template <> struct hash<unsigned char>;
462template <> struct hash<char16_t>;
463template <> struct hash<char32_t>;
464template <> struct hash<wchar_t>;
465template <> struct hash<short>;
466template <> struct hash<unsigned short>;
467template <> struct hash<int>;
468template <> struct hash<unsigned int>;
469template <> struct hash<long>;
470template <> struct hash<long long>;
471template <> struct hash<unsigned long>;
472template <> struct hash<unsigned long long>;
473
474template <> struct hash<float>;
475template <> struct hash<double>;
476template <> struct hash<long double>;
477
478template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000479template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000480
481} // std
482
483POLICY: For non-variadic implementations, the number of arguments is limited
484 to 3. It is hoped that the need for non-variadic implementations
485 will be minimal.
486
487*/
488
489#include <__config>
490#include <type_traits>
491#include <typeinfo>
492#include <exception>
493#include <memory>
494#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000495#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000496#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000497
498#include <__functional_base>
499
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000500#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000502#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000503
504_LIBCPP_BEGIN_NAMESPACE_STD
505
Marshall Clow974bae22013-07-29 14:21:53 +0000506#if _LIBCPP_STD_VER > 11
507template <class _Tp = void>
508#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000509template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000510#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000511struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512{
Marshall Clowd18ee652013-09-28 19:06:12 +0000513 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
514 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515 {return __x + __y;}
516};
517
Marshall Clow974bae22013-07-29 14:21:53 +0000518#if _LIBCPP_STD_VER > 11
519template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000520struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000521{
522 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000523 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
524 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000525 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
526 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
527 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000528 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000529};
530#endif
531
532
533#if _LIBCPP_STD_VER > 11
534template <class _Tp = void>
535#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000537#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000538struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539{
Marshall Clowd18ee652013-09-28 19:06:12 +0000540 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
541 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542 {return __x - __y;}
543};
544
Marshall Clow974bae22013-07-29 14:21:53 +0000545#if _LIBCPP_STD_VER > 11
546template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000547struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000548{
549 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000550 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
551 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000552 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
553 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
554 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000555 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000556};
557#endif
558
559
560#if _LIBCPP_STD_VER > 11
561template <class _Tp = void>
562#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000564#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000565struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566{
Marshall Clowd18ee652013-09-28 19:06:12 +0000567 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
568 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 {return __x * __y;}
570};
571
Marshall Clow974bae22013-07-29 14:21:53 +0000572#if _LIBCPP_STD_VER > 11
573template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000574struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000575{
576 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000577 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
578 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000579 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
580 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
581 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000582 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000583};
584#endif
585
586
587#if _LIBCPP_STD_VER > 11
588template <class _Tp = void>
589#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000591#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000592struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593{
Marshall Clowd18ee652013-09-28 19:06:12 +0000594 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
595 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596 {return __x / __y;}
597};
598
Marshall Clow974bae22013-07-29 14:21:53 +0000599#if _LIBCPP_STD_VER > 11
600template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000601struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000602{
603 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000604 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
605 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000606 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
607 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
608 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000609 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000610};
611#endif
612
613
614#if _LIBCPP_STD_VER > 11
615template <class _Tp = void>
616#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000617template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000618#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000619struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620{
Marshall Clowd18ee652013-09-28 19:06:12 +0000621 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
622 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623 {return __x % __y;}
624};
625
Marshall Clow974bae22013-07-29 14:21:53 +0000626#if _LIBCPP_STD_VER > 11
627template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000628struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000629{
630 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000631 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
632 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000633 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
634 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
635 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000636 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000637};
638#endif
639
640
641#if _LIBCPP_STD_VER > 11
642template <class _Tp = void>
643#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000645#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000646struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000647{
Marshall Clowd18ee652013-09-28 19:06:12 +0000648 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
649 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 {return -__x;}
651};
652
Marshall Clow974bae22013-07-29 14:21:53 +0000653#if _LIBCPP_STD_VER > 11
654template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000655struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000656{
657 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000658 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
659 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000660 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
661 -> decltype (- _VSTD::forward<_Tp>(__x))
662 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000663 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000664};
665#endif
666
667
668#if _LIBCPP_STD_VER > 11
669template <class _Tp = void>
670#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000672#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000673struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674{
Marshall Clowd18ee652013-09-28 19:06:12 +0000675 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
676 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677 {return __x == __y;}
678};
679
Marshall Clow974bae22013-07-29 14:21:53 +0000680#if _LIBCPP_STD_VER > 11
681template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000682struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000683{
Marshall Clowd18ee652013-09-28 19:06:12 +0000684 template <class _T1, class _T2>
685 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000686 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000687 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
688 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
689 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000690 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000691};
692#endif
693
694
695#if _LIBCPP_STD_VER > 11
696template <class _Tp = void>
697#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000699#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000700struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000701{
Marshall Clowd18ee652013-09-28 19:06:12 +0000702 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
703 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704 {return __x != __y;}
705};
706
Marshall Clow974bae22013-07-29 14:21:53 +0000707#if _LIBCPP_STD_VER > 11
708template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000709struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000710{
Marshall Clowd18ee652013-09-28 19:06:12 +0000711 template <class _T1, class _T2>
712 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000713 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000714 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
715 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
716 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000717 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000718};
719#endif
720
721
722#if _LIBCPP_STD_VER > 11
723template <class _Tp = void>
724#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000726#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000727struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728{
Marshall Clowd18ee652013-09-28 19:06:12 +0000729 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
730 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 {return __x > __y;}
732};
733
Marshall Clow974bae22013-07-29 14:21:53 +0000734#if _LIBCPP_STD_VER > 11
735template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000736struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000737{
Marshall Clowd18ee652013-09-28 19:06:12 +0000738 template <class _T1, class _T2>
739 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000740 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000741 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
742 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
743 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000744 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000745};
746#endif
747
748
Howard Hinnantb17caf92012-02-21 21:02:58 +0000749// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750
Marshall Clow974bae22013-07-29 14:21:53 +0000751#if _LIBCPP_STD_VER > 11
752template <class _Tp = void>
753#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000755#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000756struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757{
Marshall Clowd18ee652013-09-28 19:06:12 +0000758 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
759 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760 {return __x >= __y;}
761};
762
Marshall Clow974bae22013-07-29 14:21:53 +0000763#if _LIBCPP_STD_VER > 11
764template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000765struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000766{
Marshall Clowd18ee652013-09-28 19:06:12 +0000767 template <class _T1, class _T2>
768 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000769 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000770 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
771 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
772 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000773 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000774};
775#endif
776
777
778#if _LIBCPP_STD_VER > 11
779template <class _Tp = void>
780#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000782#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000783struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784{
Marshall Clowd18ee652013-09-28 19:06:12 +0000785 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
786 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787 {return __x <= __y;}
788};
789
Marshall Clow974bae22013-07-29 14:21:53 +0000790#if _LIBCPP_STD_VER > 11
791template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000792struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000793{
Marshall Clowd18ee652013-09-28 19:06:12 +0000794 template <class _T1, class _T2>
795 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000796 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000797 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
798 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
799 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000800 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000801};
802#endif
803
804
805#if _LIBCPP_STD_VER > 11
806template <class _Tp = void>
807#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000809#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000810struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811{
Marshall Clowd18ee652013-09-28 19:06:12 +0000812 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
813 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 {return __x && __y;}
815};
816
Marshall Clow974bae22013-07-29 14:21:53 +0000817#if _LIBCPP_STD_VER > 11
818template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000819struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000820{
Marshall Clowd18ee652013-09-28 19:06:12 +0000821 template <class _T1, class _T2>
822 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000823 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000824 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
825 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
826 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000827 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000828};
829#endif
830
831
832#if _LIBCPP_STD_VER > 11
833template <class _Tp = void>
834#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000836#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000837struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838{
Marshall Clowd18ee652013-09-28 19:06:12 +0000839 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
840 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841 {return __x || __y;}
842};
843
Marshall Clow974bae22013-07-29 14:21:53 +0000844#if _LIBCPP_STD_VER > 11
845template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000846struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000847{
Marshall Clowd18ee652013-09-28 19:06:12 +0000848 template <class _T1, class _T2>
849 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000850 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000851 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
852 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
853 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000854 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000855};
856#endif
857
858
859#if _LIBCPP_STD_VER > 11
860template <class _Tp = void>
861#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000863#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000864struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865{
Marshall Clowd18ee652013-09-28 19:06:12 +0000866 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
867 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868 {return !__x;}
869};
870
Marshall Clow974bae22013-07-29 14:21:53 +0000871#if _LIBCPP_STD_VER > 11
872template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000873struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000874{
875 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000876 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
877 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000878 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
879 -> decltype (!_VSTD::forward<_Tp>(__x))
880 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000881 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000882};
883#endif
884
885
886#if _LIBCPP_STD_VER > 11
887template <class _Tp = void>
888#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000890#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000891struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892{
Marshall Clowd18ee652013-09-28 19:06:12 +0000893 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
894 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000895 {return __x & __y;}
896};
897
Marshall Clow974bae22013-07-29 14:21:53 +0000898#if _LIBCPP_STD_VER > 11
899template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000900struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000901{
Marshall Clowd18ee652013-09-28 19:06:12 +0000902 template <class _T1, class _T2>
903 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000904 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000905 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
906 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
907 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000908 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000909};
910#endif
911
912
913#if _LIBCPP_STD_VER > 11
914template <class _Tp = void>
915#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000917#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000918struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919{
Marshall Clowd18ee652013-09-28 19:06:12 +0000920 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
921 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922 {return __x | __y;}
923};
924
Marshall Clow974bae22013-07-29 14:21:53 +0000925#if _LIBCPP_STD_VER > 11
926template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000927struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000928{
Marshall Clowd18ee652013-09-28 19:06:12 +0000929 template <class _T1, class _T2>
930 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000931 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000932 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
933 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
934 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000935 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000936};
937#endif
938
939
940#if _LIBCPP_STD_VER > 11
941template <class _Tp = void>
942#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000943template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000944#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000945struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946{
Marshall Clowd18ee652013-09-28 19:06:12 +0000947 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
948 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949 {return __x ^ __y;}
950};
951
Marshall Clow974bae22013-07-29 14:21:53 +0000952#if _LIBCPP_STD_VER > 11
953template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000954struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000955{
Marshall Clowd18ee652013-09-28 19:06:12 +0000956 template <class _T1, class _T2>
957 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000958 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000959 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
960 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
961 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000962 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000963};
964#endif
965
966
967#if _LIBCPP_STD_VER > 11
968template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000969struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000970{
Marshall Clowd18ee652013-09-28 19:06:12 +0000971 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
972 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000973 {return ~__x;}
974};
975
976template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000977struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000978{
979 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000980 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
981 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000982 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
983 -> decltype (~_VSTD::forward<_Tp>(__x))
984 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000985 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000986};
987#endif
988
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000990class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 : public unary_function<typename _Predicate::argument_type, bool>
992{
993 _Predicate __pred_;
994public:
Marshall Clowd18ee652013-09-28 19:06:12 +0000995 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
996 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +0000998 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
999 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000 {return !__pred_(__x);}
1001};
1002
1003template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001004_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005unary_negate<_Predicate>
1006not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1007
1008template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001009class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 : public binary_function<typename _Predicate::first_argument_type,
1011 typename _Predicate::second_argument_type,
1012 bool>
1013{
1014 _Predicate __pred_;
1015public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001016 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001017 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1018
1019 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1020 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021 const typename _Predicate::second_argument_type& __y) const
1022 {return !__pred_(__x, __y);}
1023};
1024
1025template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001026_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027binary_negate<_Predicate>
1028not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1029
Marshall Clow26a027c2017-04-13 18:25:32 +00001030#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001032class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 : public unary_function<typename __Operation::second_argument_type,
1034 typename __Operation::result_type>
1035{
1036protected:
1037 __Operation op;
1038 typename __Operation::first_argument_type value;
1039public:
1040 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1041 const typename __Operation::first_argument_type __y)
1042 : op(__x), value(__y) {}
1043 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1044 (typename __Operation::second_argument_type& __x) const
1045 {return op(value, __x);}
1046 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1047 (const typename __Operation::second_argument_type& __x) const
1048 {return op(value, __x);}
1049};
1050
1051template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001052_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053binder1st<__Operation>
1054bind1st(const __Operation& __op, const _Tp& __x)
1055 {return binder1st<__Operation>(__op, __x);}
1056
1057template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001058class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059 : public unary_function<typename __Operation::first_argument_type,
1060 typename __Operation::result_type>
1061{
1062protected:
1063 __Operation op;
1064 typename __Operation::second_argument_type value;
1065public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1068 : op(__x), value(__y) {}
1069 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1070 ( typename __Operation::first_argument_type& __x) const
1071 {return op(__x, value);}
1072 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1073 (const typename __Operation::first_argument_type& __x) const
1074 {return op(__x, value);}
1075};
1076
1077template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001078_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079binder2nd<__Operation>
1080bind2nd(const __Operation& __op, const _Tp& __x)
1081 {return binder2nd<__Operation>(__op, __x);}
1082
1083template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001084class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001085 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086{
1087 _Result (*__f_)(_Arg);
1088public:
1089 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1090 : __f_(__f) {}
1091 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1092 {return __f_(__x);}
1093};
1094
1095template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001096_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097pointer_to_unary_function<_Arg,_Result>
1098ptr_fun(_Result (*__f)(_Arg))
1099 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1100
1101template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001102class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001103 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104{
1105 _Result (*__f_)(_Arg1, _Arg2);
1106public:
1107 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1108 : __f_(__f) {}
1109 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1110 {return __f_(__x, __y);}
1111};
1112
1113template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001114_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115pointer_to_binary_function<_Arg1,_Arg2,_Result>
1116ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1117 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1118
1119template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001120class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1121 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122{
1123 _Sp (_Tp::*__p_)();
1124public:
1125 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1126 : __p_(__p) {}
1127 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1128 {return (__p->*__p_)();}
1129};
1130
1131template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001132class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1133 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134{
1135 _Sp (_Tp::*__p_)(_Ap);
1136public:
1137 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1138 : __p_(__p) {}
1139 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1140 {return (__p->*__p_)(__x);}
1141};
1142
1143template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001144_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145mem_fun_t<_Sp,_Tp>
1146mem_fun(_Sp (_Tp::*__f)())
1147 {return mem_fun_t<_Sp,_Tp>(__f);}
1148
1149template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001150_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151mem_fun1_t<_Sp,_Tp,_Ap>
1152mem_fun(_Sp (_Tp::*__f)(_Ap))
1153 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1154
1155template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001156class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1157 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158{
1159 _Sp (_Tp::*__p_)();
1160public:
1161 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1162 : __p_(__p) {}
1163 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1164 {return (__p.*__p_)();}
1165};
1166
1167template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001168class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1169 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170{
1171 _Sp (_Tp::*__p_)(_Ap);
1172public:
1173 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1174 : __p_(__p) {}
1175 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1176 {return (__p.*__p_)(__x);}
1177};
1178
1179template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001180_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181mem_fun_ref_t<_Sp,_Tp>
1182mem_fun_ref(_Sp (_Tp::*__f)())
1183 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1184
1185template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001186_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187mem_fun1_ref_t<_Sp,_Tp,_Ap>
1188mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1189 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1190
1191template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001192class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1193 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194{
1195 _Sp (_Tp::*__p_)() const;
1196public:
1197 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1198 : __p_(__p) {}
1199 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1200 {return (__p->*__p_)();}
1201};
1202
1203template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001204class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1205 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206{
1207 _Sp (_Tp::*__p_)(_Ap) const;
1208public:
1209 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1210 : __p_(__p) {}
1211 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1212 {return (__p->*__p_)(__x);}
1213};
1214
1215template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001216_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217const_mem_fun_t<_Sp,_Tp>
1218mem_fun(_Sp (_Tp::*__f)() const)
1219 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1220
1221template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001222_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223const_mem_fun1_t<_Sp,_Tp,_Ap>
1224mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1225 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1226
1227template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001228class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1229 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230{
1231 _Sp (_Tp::*__p_)() const;
1232public:
1233 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1234 : __p_(__p) {}
1235 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1236 {return (__p.*__p_)();}
1237};
1238
1239template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001240class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001241 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242{
1243 _Sp (_Tp::*__p_)(_Ap) const;
1244public:
1245 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1246 : __p_(__p) {}
1247 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1248 {return (__p.*__p_)(__x);}
1249};
1250
1251template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001252_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253const_mem_fun_ref_t<_Sp,_Tp>
1254mem_fun_ref(_Sp (_Tp::*__f)() const)
1255 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1256
1257template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001258_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1260mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1261 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001262#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001264////////////////////////////////////////////////////////////////////////////////
1265// MEMFUN
1266//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268template <class _Tp>
1269class __mem_fn
1270 : public __weak_result_type<_Tp>
1271{
1272public:
1273 // types
1274 typedef _Tp type;
1275private:
1276 type __f_;
1277
1278public:
Marshall Clowad8ff212015-10-25 20:12:16 +00001279 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001281#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282 // invoke
1283 template <class... _ArgTypes>
Eric Fiselier2cc48332015-07-22 22:43:27 +00001284 _LIBCPP_INLINE_VISIBILITY
1285 typename __invoke_return<type, _ArgTypes...>::type
1286 operator() (_ArgTypes&&... __args) const {
1287 return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
1288 }
1289#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001290
1291 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001292 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001293 typename __invoke_return0<type, _A0>::type
1294 operator() (_A0& __a0) const {
1295 return __invoke(__f_, __a0);
1296 }
1297
Eric Fiselierce1813a2015-08-26 20:15:02 +00001298 template <class _A0>
1299 _LIBCPP_INLINE_VISIBILITY
1300 typename __invoke_return0<type, _A0 const>::type
1301 operator() (_A0 const& __a0) const {
1302 return __invoke(__f_, __a0);
1303 }
1304
Eric Fiselier2cc48332015-07-22 22:43:27 +00001305 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001306 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001307 typename __invoke_return1<type, _A0, _A1>::type
1308 operator() (_A0& __a0, _A1& __a1) const {
1309 return __invoke(__f_, __a0, __a1);
1310 }
1311
Eric Fiselierce1813a2015-08-26 20:15:02 +00001312 template <class _A0, class _A1>
1313 _LIBCPP_INLINE_VISIBILITY
1314 typename __invoke_return1<type, _A0 const, _A1>::type
1315 operator() (_A0 const& __a0, _A1& __a1) const {
1316 return __invoke(__f_, __a0, __a1);
1317 }
1318
1319 template <class _A0, class _A1>
1320 _LIBCPP_INLINE_VISIBILITY
1321 typename __invoke_return1<type, _A0, _A1 const>::type
1322 operator() (_A0& __a0, _A1 const& __a1) const {
1323 return __invoke(__f_, __a0, __a1);
1324 }
1325
1326 template <class _A0, class _A1>
1327 _LIBCPP_INLINE_VISIBILITY
1328 typename __invoke_return1<type, _A0 const, _A1 const>::type
1329 operator() (_A0 const& __a0, _A1 const& __a1) const {
1330 return __invoke(__f_, __a0, __a1);
1331 }
1332
Eric Fiselier2cc48332015-07-22 22:43:27 +00001333 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001334 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001335 typename __invoke_return2<type, _A0, _A1, _A2>::type
1336 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
1337 return __invoke(__f_, __a0, __a1, __a2);
1338 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001339
1340 template <class _A0, class _A1, class _A2>
1341 _LIBCPP_INLINE_VISIBILITY
1342 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1343 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
1344 return __invoke(__f_, __a0, __a1, __a2);
1345 }
1346
1347 template <class _A0, class _A1, class _A2>
1348 _LIBCPP_INLINE_VISIBILITY
1349 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1350 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
1351 return __invoke(__f_, __a0, __a1, __a2);
1352 }
1353
1354 template <class _A0, class _A1, class _A2>
1355 _LIBCPP_INLINE_VISIBILITY
1356 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1357 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
1358 return __invoke(__f_, __a0, __a1, __a2);
1359 }
1360
1361 template <class _A0, class _A1, class _A2>
1362 _LIBCPP_INLINE_VISIBILITY
1363 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1364 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
1365 return __invoke(__f_, __a0, __a1, __a2);
1366 }
1367
1368 template <class _A0, class _A1, class _A2>
1369 _LIBCPP_INLINE_VISIBILITY
1370 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1371 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
1372 return __invoke(__f_, __a0, __a1, __a2);
1373 }
1374
1375 template <class _A0, class _A1, class _A2>
1376 _LIBCPP_INLINE_VISIBILITY
1377 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1378 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
1379 return __invoke(__f_, __a0, __a1, __a2);
1380 }
1381
1382 template <class _A0, class _A1, class _A2>
1383 _LIBCPP_INLINE_VISIBILITY
1384 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1385 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
1386 return __invoke(__f_, __a0, __a1, __a2);
1387 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001388#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389};
1390
Howard Hinnantc834c512011-11-29 18:15:50 +00001391template<class _Rp, class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001393__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001394mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395{
Howard Hinnantc834c512011-11-29 18:15:50 +00001396 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397}
1398
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001399////////////////////////////////////////////////////////////////////////////////
1400// FUNCTION
1401//==============================================================================
1402
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403// bad_function_call
1404
Howard Hinnant4ff57432010-09-21 22:55:27 +00001405class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 : public exception
1407{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001408#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1409public:
1410 virtual ~bad_function_call() _NOEXCEPT;
1411
1412 virtual const char* what() const _NOEXCEPT;
1413#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414};
1415
Louis Dionne16fe2952018-07-11 23:14:33 +00001416_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001417void __throw_bad_function_call()
1418{
1419#ifndef _LIBCPP_NO_EXCEPTIONS
1420 throw bad_function_call();
1421#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001422 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001423#endif
1424}
1425
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001426template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001427
1428namespace __function
1429{
1430
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001431template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432struct __maybe_derive_from_unary_function
1433{
1434};
1435
Howard Hinnantc834c512011-11-29 18:15:50 +00001436template<class _Rp, class _A1>
1437struct __maybe_derive_from_unary_function<_Rp(_A1)>
1438 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439{
1440};
1441
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001442template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443struct __maybe_derive_from_binary_function
1444{
1445};
1446
Howard Hinnantc834c512011-11-29 18:15:50 +00001447template<class _Rp, class _A1, class _A2>
1448struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1449 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450{
1451};
1452
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001453template <class _Fp>
1454_LIBCPP_INLINE_VISIBILITY
1455bool __not_null(_Fp const&) { return true; }
1456
1457template <class _Fp>
1458_LIBCPP_INLINE_VISIBILITY
1459bool __not_null(_Fp* __ptr) { return __ptr; }
1460
1461template <class _Ret, class _Class>
1462_LIBCPP_INLINE_VISIBILITY
1463bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1464
1465template <class _Fp>
1466_LIBCPP_INLINE_VISIBILITY
1467bool __not_null(function<_Fp> const& __f) { return !!__f; }
1468
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001469} // namespace __function
1470
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001471#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001472
1473namespace __function {
1474
Eric Fiselier125798e2018-12-10 18:14:09 +00001475// __alloc_func holds a functor and an allocator.
1476
1477template <class _Fp, class _Ap, class _FB> class __alloc_func;
1478
1479template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1480class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1481{
1482 __compressed_pair<_Fp, _Ap> __f_;
1483
1484 public:
1485 typedef _Fp _Target;
1486 typedef _Ap _Alloc;
1487
1488 _LIBCPP_INLINE_VISIBILITY
1489 const _Target& __target() const { return __f_.first(); }
1490
1491 _LIBCPP_INLINE_VISIBILITY
1492 const _Alloc& __allocator() const { return __f_.second(); }
1493
1494 _LIBCPP_INLINE_VISIBILITY
1495 explicit __alloc_func(_Target&& __f)
1496 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1497 _VSTD::forward_as_tuple())
1498 {
1499 }
1500
1501 _LIBCPP_INLINE_VISIBILITY
1502 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1503 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1504 _VSTD::forward_as_tuple(__a))
1505 {
1506 }
1507
1508 _LIBCPP_INLINE_VISIBILITY
1509 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1510 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1511 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1512 {
1513 }
1514
1515 _LIBCPP_INLINE_VISIBILITY
1516 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1517 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1518 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1519 {
1520 }
1521
1522 _LIBCPP_INLINE_VISIBILITY
1523 _Rp operator()(_ArgTypes&&... __arg)
1524 {
1525 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1526 return _Invoker::__call(__f_.first(),
1527 _VSTD::forward<_ArgTypes>(__arg)...);
1528 }
1529
1530 _LIBCPP_INLINE_VISIBILITY
1531 __alloc_func* __clone() const
1532 {
1533 typedef allocator_traits<_Alloc> __alloc_traits;
1534 typedef
1535 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1536 _AA;
1537 _AA __a(__f_.second());
1538 typedef __allocator_destructor<_AA> _Dp;
1539 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1540 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1541 return __hold.release();
1542 }
1543
1544 _LIBCPP_INLINE_VISIBILITY
1545 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
1546};
1547
1548// __base provides an abstract interface for copyable functors.
1549
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550template<class _Fp> class __base;
1551
Howard Hinnantc834c512011-11-29 18:15:50 +00001552template<class _Rp, class ..._ArgTypes>
1553class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554{
1555 __base(const __base&);
1556 __base& operator=(const __base&);
1557public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001558 _LIBCPP_INLINE_VISIBILITY __base() {}
1559 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 virtual __base* __clone() const = 0;
1561 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001562 virtual void destroy() _NOEXCEPT = 0;
1563 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001564 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001565#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001566 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1567 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001568#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569};
1570
Eric Fiselier125798e2018-12-10 18:14:09 +00001571// __func implements __base for a given functor type.
1572
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573template<class _FD, class _Alloc, class _FB> class __func;
1574
Howard Hinnantc834c512011-11-29 18:15:50 +00001575template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1576class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1577 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578{
Eric Fiselier125798e2018-12-10 18:14:09 +00001579 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001582 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001583 : __f_(_VSTD::move(__f)) {}
1584
Howard Hinnant4ff57432010-09-21 22:55:27 +00001585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001586 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001587 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001588
1589 _LIBCPP_INLINE_VISIBILITY
1590 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001591 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001592
1593 _LIBCPP_INLINE_VISIBILITY
1594 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001595 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1596
Howard Hinnantc834c512011-11-29 18:15:50 +00001597 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1598 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001599 virtual void destroy() _NOEXCEPT;
1600 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001601 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001602#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001603 virtual const void* target(const type_info&) const _NOEXCEPT;
1604 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001605#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606};
1607
Howard Hinnantc834c512011-11-29 18:15:50 +00001608template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1609__base<_Rp(_ArgTypes...)>*
1610__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001612 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001613 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Eric Fiselier125798e2018-12-10 18:14:09 +00001614 _Ap __a(__f_.__allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001615 typedef __allocator_destructor<_Ap> _Dp;
1616 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001617 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618 return __hold.release();
1619}
1620
Howard Hinnantc834c512011-11-29 18:15:50 +00001621template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622void
Howard Hinnantc834c512011-11-29 18:15:50 +00001623__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624{
Eric Fiselier125798e2018-12-10 18:14:09 +00001625 ::new (__p) __func(__f_.__target(), __f_.__allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626}
1627
Howard Hinnantc834c512011-11-29 18:15:50 +00001628template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629void
Howard Hinnantc834c512011-11-29 18:15:50 +00001630__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631{
Eric Fiselier125798e2018-12-10 18:14:09 +00001632 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633}
1634
Howard Hinnantc834c512011-11-29 18:15:50 +00001635template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636void
Howard Hinnantc834c512011-11-29 18:15:50 +00001637__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001639 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001640 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Eric Fiselier125798e2018-12-10 18:14:09 +00001641 _Ap __a(__f_.__allocator());
1642 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643 __a.deallocate(this, 1);
1644}
1645
Howard Hinnantc834c512011-11-29 18:15:50 +00001646template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1647_Rp
1648__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649{
Eric Fiselier125798e2018-12-10 18:14:09 +00001650 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001651}
1652
Howard Hinnant72f73582010-08-11 17:04:31 +00001653#ifndef _LIBCPP_NO_RTTI
1654
Howard Hinnantc834c512011-11-29 18:15:50 +00001655template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001657__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658{
Howard Hinnantc834c512011-11-29 18:15:50 +00001659 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001660 return &__f_.__target();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661 return (const void*)0;
1662}
1663
Howard Hinnantc834c512011-11-29 18:15:50 +00001664template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001666__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667{
Howard Hinnantc834c512011-11-29 18:15:50 +00001668 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669}
1670
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001671#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001672
Eric Fiselier125798e2018-12-10 18:14:09 +00001673// __value_func creates a value-type from a __func.
1674
1675template <class _Fp> class __value_func;
1676
1677template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1678{
1679 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1680
1681 typedef __base<_Rp(_ArgTypes...)> __func;
1682 __func* __f_;
1683
1684 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1685 {
1686 return reinterpret_cast<__func*>(p);
1687 }
1688
1689 public:
1690 _LIBCPP_INLINE_VISIBILITY
1691 __value_func() _NOEXCEPT : __f_(0) {}
1692
1693 template <class _Fp, class _Alloc>
1694 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc __a)
1695 : __f_(0)
1696 {
1697 typedef allocator_traits<_Alloc> __alloc_traits;
1698 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1699 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1700 _FunAlloc;
1701
1702 if (__function::__not_null(__f))
1703 {
1704 _FunAlloc __af(__a);
1705 if (sizeof(_Fun) <= sizeof(__buf_) &&
1706 is_nothrow_copy_constructible<_Fp>::value &&
1707 is_nothrow_copy_constructible<_FunAlloc>::value)
1708 {
1709 __f_ =
1710 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1711 }
1712 else
1713 {
1714 typedef __allocator_destructor<_FunAlloc> _Dp;
1715 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1716 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1717 __f_ = __hold.release();
1718 }
1719 }
1720 }
1721
1722 _LIBCPP_INLINE_VISIBILITY
1723 __value_func(const __value_func& __f)
1724 {
1725 if (__f.__f_ == 0)
1726 __f_ = 0;
1727 else if ((void*)__f.__f_ == &__f.__buf_)
1728 {
1729 __f_ = __as_base(&__buf_);
1730 __f.__f_->__clone(__f_);
1731 }
1732 else
1733 __f_ = __f.__f_->__clone();
1734 }
1735
1736 _LIBCPP_INLINE_VISIBILITY
1737 __value_func(__value_func&& __f) _NOEXCEPT
1738 {
1739 if (__f.__f_ == 0)
1740 __f_ = 0;
1741 else if ((void*)__f.__f_ == &__f.__buf_)
1742 {
1743 __f_ = __as_base(&__buf_);
1744 __f.__f_->__clone(__f_);
1745 }
1746 else
1747 {
1748 __f_ = __f.__f_;
1749 __f.__f_ = 0;
1750 }
1751 }
1752
1753 _LIBCPP_INLINE_VISIBILITY
1754 ~__value_func()
1755 {
1756 if ((void*)__f_ == &__buf_)
1757 __f_->destroy();
1758 else if (__f_)
1759 __f_->destroy_deallocate();
1760 }
1761
1762 _LIBCPP_INLINE_VISIBILITY
1763 __value_func& operator=(__value_func&& __f)
1764 {
1765 *this = nullptr;
1766 if (__f.__f_ == 0)
1767 __f_ = 0;
1768 else if ((void*)__f.__f_ == &__f.__buf_)
1769 {
1770 __f_ = __as_base(&__buf_);
1771 __f.__f_->__clone(__f_);
1772 }
1773 else
1774 {
1775 __f_ = __f.__f_;
1776 __f.__f_ = 0;
1777 }
1778 return *this;
1779 }
1780
1781 _LIBCPP_INLINE_VISIBILITY
1782 __value_func& operator=(nullptr_t)
1783 {
1784 __func* __f = __f_;
1785 __f_ = 0;
1786 if ((void*)__f == &__buf_)
1787 __f->destroy();
1788 else if (__f)
1789 __f->destroy_deallocate();
1790 return *this;
1791 }
1792
1793 _LIBCPP_INLINE_VISIBILITY
1794 _Rp operator()(_ArgTypes&&... __args) const
1795 {
1796 if (__f_ == 0)
1797 __throw_bad_function_call();
1798 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1799 }
1800
1801 _LIBCPP_INLINE_VISIBILITY
1802 void swap(__value_func& __f) _NOEXCEPT
1803 {
1804 if (&__f == this)
1805 return;
1806 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1807 {
1808 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1809 __func* __t = __as_base(&__tempbuf);
1810 __f_->__clone(__t);
1811 __f_->destroy();
1812 __f_ = 0;
1813 __f.__f_->__clone(__as_base(&__buf_));
1814 __f.__f_->destroy();
1815 __f.__f_ = 0;
1816 __f_ = __as_base(&__buf_);
1817 __t->__clone(__as_base(&__f.__buf_));
1818 __t->destroy();
1819 __f.__f_ = __as_base(&__f.__buf_);
1820 }
1821 else if ((void*)__f_ == &__buf_)
1822 {
1823 __f_->__clone(__as_base(&__f.__buf_));
1824 __f_->destroy();
1825 __f_ = __f.__f_;
1826 __f.__f_ = __as_base(&__f.__buf_);
1827 }
1828 else if ((void*)__f.__f_ == &__f.__buf_)
1829 {
1830 __f.__f_->__clone(__as_base(&__buf_));
1831 __f.__f_->destroy();
1832 __f.__f_ = __f_;
1833 __f_ = __as_base(&__buf_);
1834 }
1835 else
1836 _VSTD::swap(__f_, __f.__f_);
1837 }
1838
1839 _LIBCPP_INLINE_VISIBILITY
1840 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != 0; }
1841
1842#ifndef _LIBCPP_NO_RTTI
1843 _LIBCPP_INLINE_VISIBILITY
1844 const std::type_info& target_type() const _NOEXCEPT
1845 {
1846 if (__f_ == 0)
1847 return typeid(void);
1848 return __f_->target_type();
1849 }
1850
1851 template <typename _Tp>
1852 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1853 {
1854 if (__f_ == 0)
1855 return 0;
1856 return (const _Tp*)__f_->target(typeid(_Tp));
1857 }
1858#endif // _LIBCPP_NO_RTTI
1859};
1860
Eric Fiselierf2e64362018-12-11 00:14:34 +00001861// Storage for a functor object, to be used with __policy to manage copy and
1862// destruction.
1863union __policy_storage
1864{
1865 mutable char __small[sizeof(void*) * 2];
1866 void* __large;
1867};
1868
1869// True if _Fun can safely be held in __policy_storage.__small.
1870template <typename _Fun>
1871struct __use_small_storage
1872 : public _VSTD::integral_constant<
1873 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001874 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Eric Fiselierf2e64362018-12-11 00:14:34 +00001875 _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1876 _VSTD::is_trivially_destructible<_Fun>::value> {};
1877
1878// Policy contains information about how to copy, destroy, and move the
1879// underlying functor. You can think of it as a vtable of sorts.
1880struct __policy
1881{
1882 // Used to copy or destroy __large values. null for trivial objects.
1883 void* (*const __clone)(const void*);
1884 void (*const __destroy)(void*);
1885
1886 // True if this is the null policy (no value).
1887 const bool __is_null;
1888
1889 // The target type. May be null if RTTI is disabled.
1890 const std::type_info* const __type_info;
1891
1892 // Returns a pointer to a static policy object suitable for the functor
1893 // type.
1894 template <typename _Fun>
1895 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1896 {
1897 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1898 }
1899
1900 _LIBCPP_INLINE_VISIBILITY
1901 static const __policy* __create_empty()
1902 {
1903 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1904 true,
1905#ifndef _LIBCPP_NO_RTTI
1906 &typeid(void)
1907#else
1908 nullptr
1909#endif
1910 };
1911 return &__policy_;
1912 }
1913
1914 private:
1915 template <typename _Fun> static void* __large_clone(const void* __s)
1916 {
1917 const _Fun* __f = static_cast<const _Fun*>(__s);
1918 return __f->__clone();
1919 }
1920
1921 template <typename _Fun> static void __large_destroy(void* __s)
1922 {
1923 typedef allocator_traits<typename _Fun::_Alloc> __alloc_traits;
1924 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1925 _FunAlloc;
1926 _Fun* __f = static_cast<_Fun*>(__s);
1927 _FunAlloc __a(__f->__allocator());
1928 __f->destroy();
1929 __a.deallocate(__f, 1);
1930 }
1931
1932 template <typename _Fun>
1933 _LIBCPP_INLINE_VISIBILITY static const __policy*
1934 __choose_policy(/* is_small = */ false_type)
1935 {
1936 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
1937 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
1938#ifndef _LIBCPP_NO_RTTI
1939 &typeid(typename _Fun::_Target)
1940#else
1941 nullptr
1942#endif
1943 };
1944 return &__policy_;
1945 }
1946
1947 template <typename _Fun>
1948 _LIBCPP_INLINE_VISIBILITY static const __policy*
1949 __choose_policy(/* is_small = */ true_type)
1950 {
1951 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
1952 nullptr, nullptr, false,
1953#ifndef _LIBCPP_NO_RTTI
1954 &typeid(typename _Fun::_Target)
1955#else
1956 nullptr
1957#endif
1958 };
1959 return &__policy_;
1960 }
1961};
1962
1963// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
1964// faster for types that can be passed in registers.
1965template <typename _Tp>
1966using __fast_forward =
1967 typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
1968
1969// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
1970
1971template <class _Fp> struct __policy_invoker;
1972
1973template <class _Rp, class... _ArgTypes>
1974struct __policy_invoker<_Rp(_ArgTypes...)>
1975{
1976 typedef _Rp (*__Call)(const __policy_storage*,
1977 __fast_forward<_ArgTypes>...);
1978
1979 __Call __call_;
1980
1981 // Creates an invoker that throws bad_function_call.
1982 _LIBCPP_INLINE_VISIBILITY
1983 __policy_invoker() : __call_(&__call_empty) {}
1984
1985 // Creates an invoker that calls the given instance of __func.
1986 template <typename _Fun>
1987 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
1988 {
1989 return __policy_invoker(&__call_impl<_Fun>);
1990 }
1991
1992 private:
1993 _LIBCPP_INLINE_VISIBILITY
1994 explicit __policy_invoker(__Call __c) : __call_(__c) {}
1995
1996 static _Rp __call_empty(const __policy_storage*,
1997 __fast_forward<_ArgTypes>...)
1998 {
1999 __throw_bad_function_call();
2000 }
2001
2002 template <typename _Fun>
2003 static _Rp __call_impl(const __policy_storage* __buf,
2004 __fast_forward<_ArgTypes>... __args)
2005 {
2006 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2007 ? &__buf->__small
2008 : __buf->__large);
2009 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2010 }
2011};
2012
2013// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2014// copyable functor.
2015
2016template <class _Fp> class __policy_func;
2017
2018template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2019{
2020 // Inline storage for small objects.
2021 __policy_storage __buf_;
2022
2023 // Calls the value stored in __buf_. This could technically be part of
2024 // policy, but storing it here eliminates a level of indirection inside
2025 // operator().
2026 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2027 __invoker __invoker_;
2028
2029 // The policy that describes how to move / copy / destroy __buf_. Never
2030 // null, even if the function is empty.
2031 const __policy* __policy_;
2032
2033 public:
2034 _LIBCPP_INLINE_VISIBILITY
2035 __policy_func() : __policy_(__policy::__create_empty()) {}
2036
2037 template <class _Fp, class _Alloc>
2038 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2039 : __policy_(__policy::__create_empty())
2040 {
2041 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2042 typedef allocator_traits<_Alloc> __alloc_traits;
2043 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2044 _FunAlloc;
2045
2046 if (__function::__not_null(__f))
2047 {
2048 __invoker_ = __invoker::template __create<_Fun>();
2049 __policy_ = __policy::__create<_Fun>();
2050
2051 _FunAlloc __af(__a);
2052 if (__use_small_storage<_Fun>())
2053 {
2054 ::new ((void*)&__buf_.__small)
2055 _Fun(_VSTD::move(__f), _Alloc(__af));
2056 }
2057 else
2058 {
2059 typedef __allocator_destructor<_FunAlloc> _Dp;
2060 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2061 ::new ((void*)__hold.get())
2062 _Fun(_VSTD::move(__f), _Alloc(__af));
2063 __buf_.__large = __hold.release();
2064 }
2065 }
2066 }
2067
2068 _LIBCPP_INLINE_VISIBILITY
2069 __policy_func(const __policy_func& __f)
2070 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2071 __policy_(__f.__policy_)
2072 {
2073 if (__policy_->__clone)
2074 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2075 }
2076
2077 _LIBCPP_INLINE_VISIBILITY
2078 __policy_func(__policy_func&& __f)
2079 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2080 __policy_(__f.__policy_)
2081 {
2082 if (__policy_->__destroy)
2083 {
2084 __f.__policy_ = __policy::__create_empty();
2085 __f.__invoker_ = __invoker();
2086 }
2087 }
2088
2089 _LIBCPP_INLINE_VISIBILITY
2090 ~__policy_func()
2091 {
2092 if (__policy_->__destroy)
2093 __policy_->__destroy(__buf_.__large);
2094 }
2095
2096 _LIBCPP_INLINE_VISIBILITY
2097 __policy_func& operator=(__policy_func&& __f)
2098 {
2099 *this = nullptr;
2100 __buf_ = __f.__buf_;
2101 __invoker_ = __f.__invoker_;
2102 __policy_ = __f.__policy_;
2103 __f.__policy_ = __policy::__create_empty();
2104 __f.__invoker_ = __invoker();
2105 return *this;
2106 }
2107
2108 _LIBCPP_INLINE_VISIBILITY
2109 __policy_func& operator=(nullptr_t)
2110 {
2111 const __policy* __p = __policy_;
2112 __policy_ = __policy::__create_empty();
2113 __invoker_ = __invoker();
2114 if (__p->__destroy)
2115 __p->__destroy(__buf_.__large);
2116 return *this;
2117 }
2118
2119 _LIBCPP_INLINE_VISIBILITY
2120 _Rp operator()(_ArgTypes&&... __args) const
2121 {
2122 return __invoker_.__call_(_VSTD::addressof(__buf_),
2123 _VSTD::forward<_ArgTypes>(__args)...);
2124 }
2125
2126 _LIBCPP_INLINE_VISIBILITY
2127 void swap(__policy_func& __f)
2128 {
2129 _VSTD::swap(__invoker_, __f.__invoker_);
2130 _VSTD::swap(__policy_, __f.__policy_);
2131 _VSTD::swap(__buf_, __f.__buf_);
2132 }
2133
2134 _LIBCPP_INLINE_VISIBILITY
2135 explicit operator bool() const _NOEXCEPT
2136 {
2137 return !__policy_->__is_null;
2138 }
2139
2140#ifndef _LIBCPP_NO_RTTI
2141 _LIBCPP_INLINE_VISIBILITY
2142 const std::type_info& target_type() const _NOEXCEPT
2143 {
2144 return *__policy_->__type_info;
2145 }
2146
2147 template <typename _Tp>
2148 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2149 {
2150 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2151 return nullptr;
2152 if (__policy_->__clone) // Out of line storage.
2153 return reinterpret_cast<const _Tp*>(__buf_.__large);
2154 else
2155 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2156 }
2157#endif // _LIBCPP_NO_RTTI
2158};
2159
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160} // __function
2161
Howard Hinnantc834c512011-11-29 18:15:50 +00002162template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002163class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002164 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2165 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002167#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002168 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002169#else
2170 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2171#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172
Eric Fiselier125798e2018-12-10 18:14:09 +00002173 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002174
Eric Fiselier43c04f72017-09-10 23:41:20 +00002175 template <class _Fp, bool = __lazy_and<
2176 integral_constant<bool, !is_same<__uncvref_t<_Fp>, function>::value>,
2177 __invokable<_Fp&, _ArgTypes...>
2178 >::value>
2179 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002180 template <class _Fp>
2181 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002182 {
Eric Fiselierea080702015-02-10 16:48:45 +00002183 static const bool value = is_same<void, _Rp>::value ||
Howard Hinnantc834c512011-11-29 18:15:50 +00002184 is_convertible<typename __invoke_of<_Fp&, _ArgTypes...>::type,
2185 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002186 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002187 template <class _Fp>
2188 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002189 {
2190 static const bool value = false;
2191 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002192
2193 template <class _Fp>
2194 using _EnableIfCallable = typename enable_if<__callable<_Fp>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002196 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197
Howard Hinnantf06d9262010-08-20 19:36:46 +00002198 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002199 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002200 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002201 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002202 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002204 function(function&&) _NOEXCEPT;
Eric Fiselier43c04f72017-09-10 23:41:20 +00002205 template<class _Fp, class = _EnableIfCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002206 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207
Marshall Clow3148f422016-10-13 21:06:03 +00002208#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002209 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002210 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002211 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002212 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002213 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002214 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002215 template<class _Alloc>
2216 function(allocator_arg_t, const _Alloc&, const function&);
2217 template<class _Alloc>
2218 function(allocator_arg_t, const _Alloc&, function&&);
Eric Fiselier43c04f72017-09-10 23:41:20 +00002219 template<class _Fp, class _Alloc, class = _EnableIfCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002220 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002221#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222
2223 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002224 function& operator=(function&&) _NOEXCEPT;
2225 function& operator=(nullptr_t) _NOEXCEPT;
Eric Fiselier43c04f72017-09-10 23:41:20 +00002226 template<class _Fp, class = _EnableIfCallable<_Fp>>
2227 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228
2229 ~function();
2230
Howard Hinnantf06d9262010-08-20 19:36:46 +00002231 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002232 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002233
2234#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002235 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002237 void assign(_Fp&& __f, const _Alloc& __a)
2238 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002239#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240
Howard Hinnantf06d9262010-08-20 19:36:46 +00002241 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002242 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002243 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2244 return static_cast<bool>(__f_);
2245 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 // deleted overloads close possible hole in the type system
2248 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002249 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002251 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002253 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002254 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255
Howard Hinnant72f73582010-08-11 17:04:31 +00002256#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002257 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002258 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002259 template <typename _Tp> _Tp* target() _NOEXCEPT;
2260 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002261#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262};
2263
Howard Hinnantc834c512011-11-29 18:15:50 +00002264template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002265function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266
Marshall Clow3148f422016-10-13 21:06:03 +00002267#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002268template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002269template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002270function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002271 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002272#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002273
Eric Fiselier125798e2018-12-10 18:14:09 +00002274template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002275function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002276 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277
Marshall Clow3148f422016-10-13 21:06:03 +00002278#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002279template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002280template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002281function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002282 function&& __f)
2283 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002284#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002285
Eric Fiselier125798e2018-12-10 18:14:09 +00002286template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002287template <class _Fp, class>
2288function<_Rp(_ArgTypes...)>::function(_Fp __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00002289 : __f_(_VSTD::move(__f), allocator<_Fp>()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290
Marshall Clow3148f422016-10-13 21:06:03 +00002291#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002292template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002293template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002294function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2295 _Fp __f)
2296 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002297#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002298
Howard Hinnantc834c512011-11-29 18:15:50 +00002299template<class _Rp, class ..._ArgTypes>
2300function<_Rp(_ArgTypes...)>&
2301function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302{
2303 function(__f).swap(*this);
2304 return *this;
2305}
2306
Howard Hinnantc834c512011-11-29 18:15:50 +00002307template<class _Rp, class ..._ArgTypes>
2308function<_Rp(_ArgTypes...)>&
2309function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310{
Eric Fiselier125798e2018-12-10 18:14:09 +00002311 __f_ = std::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002312 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313}
2314
Howard Hinnantc834c512011-11-29 18:15:50 +00002315template<class _Rp, class ..._ArgTypes>
2316function<_Rp(_ArgTypes...)>&
2317function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318{
Eric Fiselier125798e2018-12-10 18:14:09 +00002319 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002320 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321}
2322
Howard Hinnantc834c512011-11-29 18:15:50 +00002323template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002324template <class _Fp, class>
2325function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002326function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327{
Howard Hinnantc834c512011-11-29 18:15:50 +00002328 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329 return *this;
2330}
2331
Howard Hinnantc834c512011-11-29 18:15:50 +00002332template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002333function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334
Howard Hinnantc834c512011-11-29 18:15:50 +00002335template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336void
Howard Hinnantc834c512011-11-29 18:15:50 +00002337function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338{
Eric Fiselier125798e2018-12-10 18:14:09 +00002339 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340}
2341
Howard Hinnantc834c512011-11-29 18:15:50 +00002342template<class _Rp, class ..._ArgTypes>
2343_Rp
2344function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345{
Eric Fiselier125798e2018-12-10 18:14:09 +00002346 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347}
2348
Howard Hinnant72f73582010-08-11 17:04:31 +00002349#ifndef _LIBCPP_NO_RTTI
2350
Howard Hinnantc834c512011-11-29 18:15:50 +00002351template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002353function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354{
Eric Fiselier125798e2018-12-10 18:14:09 +00002355 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356}
2357
Howard Hinnantc834c512011-11-29 18:15:50 +00002358template<class _Rp, class ..._ArgTypes>
2359template <typename _Tp>
2360_Tp*
2361function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362{
Eric Fiselier125798e2018-12-10 18:14:09 +00002363 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364}
2365
Howard Hinnantc834c512011-11-29 18:15:50 +00002366template<class _Rp, class ..._ArgTypes>
2367template <typename _Tp>
2368const _Tp*
2369function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370{
Eric Fiselier125798e2018-12-10 18:14:09 +00002371 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372}
2373
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002374#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002375
Howard Hinnantc834c512011-11-29 18:15:50 +00002376template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002377inline _LIBCPP_INLINE_VISIBILITY
2378bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002379operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380
Howard Hinnantc834c512011-11-29 18:15:50 +00002381template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382inline _LIBCPP_INLINE_VISIBILITY
2383bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002384operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385
Howard Hinnantc834c512011-11-29 18:15:50 +00002386template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387inline _LIBCPP_INLINE_VISIBILITY
2388bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002389operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390
Howard Hinnantc834c512011-11-29 18:15:50 +00002391template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392inline _LIBCPP_INLINE_VISIBILITY
2393bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002394operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395
Howard Hinnantc834c512011-11-29 18:15:50 +00002396template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397inline _LIBCPP_INLINE_VISIBILITY
2398void
Howard Hinnantc834c512011-11-29 18:15:50 +00002399swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400{return __x.swap(__y);}
2401
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002402#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002403
2404#include <__functional_03>
2405
2406#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002407
2408////////////////////////////////////////////////////////////////////////////////
2409// BIND
2410//==============================================================================
2411
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002413template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002414 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2415
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002416#if _LIBCPP_STD_VER > 14
2417template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002418_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002419#endif
2420
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002422template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2424
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002425#if _LIBCPP_STD_VER > 14
2426template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002427_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002428#endif
2429
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430namespace placeholders
2431{
2432
Howard Hinnantc834c512011-11-29 18:15:50 +00002433template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002435#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002436_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2437_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2438_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2439_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2440_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2441_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2442_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2443_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2444_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2445_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2446#else
Marshall Clow396b2132018-01-02 19:01:45 +00002447/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2448/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2449/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2450/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2451/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2452/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2453/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2454/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2455/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2456/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002457#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458
2459} // placeholders
2460
Howard Hinnantc834c512011-11-29 18:15:50 +00002461template<int _Np>
2462struct __is_placeholder<placeholders::__ph<_Np> >
2463 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002465
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002466#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002467
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468template <class _Tp, class _Uj>
2469inline _LIBCPP_INLINE_VISIBILITY
2470_Tp&
2471__mu(reference_wrapper<_Tp> __t, _Uj&)
2472{
2473 return __t.get();
2474}
2475
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476template <class _Ti, class ..._Uj, size_t ..._Indx>
2477inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002478typename __invoke_of<_Ti&, _Uj...>::type
2479__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002481 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482}
2483
2484template <class _Ti, class ..._Uj>
2485inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002486typename __lazy_enable_if
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487<
2488 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002489 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490>::type
2491__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2492{
2493 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
2494 return __mu_expand(__ti, __uj, __indices());
2495}
2496
2497template <bool IsPh, class _Ti, class _Uj>
2498struct __mu_return2 {};
2499
2500template <class _Ti, class _Uj>
2501struct __mu_return2<true, _Ti, _Uj>
2502{
2503 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2504};
2505
2506template <class _Ti, class _Uj>
2507inline _LIBCPP_INLINE_VISIBILITY
2508typename enable_if
2509<
2510 0 < is_placeholder<_Ti>::value,
2511 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2512>::type
2513__mu(_Ti&, _Uj& __uj)
2514{
2515 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002516 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517}
2518
2519template <class _Ti, class _Uj>
2520inline _LIBCPP_INLINE_VISIBILITY
2521typename enable_if
2522<
2523 !is_bind_expression<_Ti>::value &&
2524 is_placeholder<_Ti>::value == 0 &&
2525 !__is_reference_wrapper<_Ti>::value,
2526 _Ti&
2527>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002528__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529{
2530 return __ti;
2531}
2532
Howard Hinnant0415d792011-05-22 15:07:43 +00002533template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2534 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002535struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002537template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002538struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002539{
2540 typedef __nat type;
2541};
2542
Howard Hinnantc51e1022010-05-11 19:42:16 +00002543template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002544struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002546 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547};
2548
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002549template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002550struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2551 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002552{
2553};
2554
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002556struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557{
2558 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2559 _TupleUj>::type&& type;
2560};
2561
2562template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002563struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002564{
2565 typedef typename _Ti::type& type;
2566};
2567
2568template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002569struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570{
2571 typedef _Ti& type;
2572};
2573
2574template <class _Ti, class _TupleUj>
2575struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002576 : public __mu_return_impl<_Ti,
2577 __is_reference_wrapper<_Ti>::value,
2578 is_bind_expression<_Ti>::value,
2579 0 < is_placeholder<_Ti>::value &&
2580 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2581 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002582{
2583};
2584
Howard Hinnantc834c512011-11-29 18:15:50 +00002585template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002586struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002587{
2588 static const bool value = false;
2589};
2590
2591template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002592struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002593{
2594 static const bool value = __invokable<_Fp,
2595 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2596};
2597
2598template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002599struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002600{
2601 static const bool value = __invokable<_Fp,
2602 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2603};
2604
2605template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002606 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607struct __bind_return;
2608
Howard Hinnantc834c512011-11-29 18:15:50 +00002609template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002610struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002612 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002614 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615 typename __mu_return
2616 <
2617 _BoundArgs,
2618 _TupleUj
2619 >::type...
2620 >::type type;
2621};
2622
Howard Hinnantc834c512011-11-29 18:15:50 +00002623template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002624struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002626 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002628 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629 typename __mu_return
2630 <
2631 const _BoundArgs,
2632 _TupleUj
2633 >::type...
2634 >::type type;
2635};
2636
Howard Hinnantc834c512011-11-29 18:15:50 +00002637template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002639typename __bind_return<_Fp, _BoundArgs, _Args>::type
2640__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641 _Args&& __args)
2642{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002643 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644}
2645
Howard Hinnantc834c512011-11-29 18:15:50 +00002646template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002647class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002648 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002650protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002651 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002652 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002653private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002654 _Fd __f_;
2655 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656
2657 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2658public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002659 template <class _Gp, class ..._BA,
2660 class = typename enable_if
2661 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002662 is_constructible<_Fd, _Gp>::value &&
2663 !is_same<typename remove_reference<_Gp>::type,
2664 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002665 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002667 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2668 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002669 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670
2671 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002673 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002674 operator()(_Args&& ...__args)
2675 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002676 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002677 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 }
2679
2680 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002682 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683 operator()(_Args&& ...__args) const
2684 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002685 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002686 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 }
2688};
2689
Howard Hinnantc834c512011-11-29 18:15:50 +00002690template<class _Fp, class ..._BoundArgs>
2691struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692
Howard Hinnantc834c512011-11-29 18:15:50 +00002693template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002695 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696{
Howard Hinnantc834c512011-11-29 18:15:50 +00002697 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002698 typedef typename base::_Fd _Fd;
2699 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002701 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702
Howard Hinnant7091e652011-07-02 18:22:36 +00002703
Howard Hinnantf292a922013-07-01 00:01:51 +00002704 template <class _Gp, class ..._BA,
2705 class = typename enable_if
2706 <
2707 is_constructible<_Fd, _Gp>::value &&
2708 !is_same<typename remove_reference<_Gp>::type,
2709 __bind_r>::value
2710 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002712 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2713 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002714 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715
2716 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002718 typename enable_if
2719 <
2720 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002721 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002722 result_type
2723 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724 operator()(_Args&& ...__args)
2725 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002726 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2727 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 }
2729
2730 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002732 typename enable_if
2733 <
2734 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002735 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002736 result_type
2737 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 operator()(_Args&& ...__args) const
2739 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002740 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2741 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 }
2743};
2744
Howard Hinnantc834c512011-11-29 18:15:50 +00002745template<class _Rp, class _Fp, class ..._BoundArgs>
2746struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747
Howard Hinnantc834c512011-11-29 18:15:50 +00002748template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002749inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002750__bind<_Fp, _BoundArgs...>
2751bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752{
Howard Hinnantc834c512011-11-29 18:15:50 +00002753 typedef __bind<_Fp, _BoundArgs...> type;
2754 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755}
2756
Howard Hinnantc834c512011-11-29 18:15:50 +00002757template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002759__bind_r<_Rp, _Fp, _BoundArgs...>
2760bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761{
Howard Hinnantc834c512011-11-29 18:15:50 +00002762 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2763 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764}
2765
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002766#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767
Eric Fiselier0d974f12015-07-14 20:16:15 +00002768#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002769
Eric Fiselier0d974f12015-07-14 20:16:15 +00002770template <class _Fn, class ..._Args>
2771result_of_t<_Fn&&(_Args&&...)>
Eric Fiselier934f63b2016-06-02 01:25:41 +00002772invoke(_Fn&& __f, _Args&&... __args)
2773 noexcept(noexcept(_VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...)))
2774{
2775 return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
Eric Fiselier0d974f12015-07-14 20:16:15 +00002776}
Eric Fiselier934f63b2016-06-02 01:25:41 +00002777
2778template <class _DecayFunc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002779class _LIBCPP_TEMPLATE_VIS __not_fn_imp {
Eric Fiselier934f63b2016-06-02 01:25:41 +00002780 _DecayFunc __fd;
2781
2782public:
2783 __not_fn_imp() = delete;
2784
2785 template <class ..._Args>
2786 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00002787 auto operator()(_Args&& ...__args) &
Eric Fiselier934f63b2016-06-02 01:25:41 +00002788 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002789 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2790 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00002791
2792 template <class ..._Args>
2793 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00002794 auto operator()(_Args&& ...__args) &&
2795 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002796 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
2797 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00002798
2799 template <class ..._Args>
2800 _LIBCPP_INLINE_VISIBILITY
2801 auto operator()(_Args&& ...__args) const&
Eric Fiselier934f63b2016-06-02 01:25:41 +00002802 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002803 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2804 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00002805
Eric Fiseliere8303a32016-06-27 00:40:41 +00002806
2807 template <class ..._Args>
2808 _LIBCPP_INLINE_VISIBILITY
2809 auto operator()(_Args&& ...__args) const&&
2810 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002811 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
2812 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00002813
Eric Fiselier934f63b2016-06-02 01:25:41 +00002814private:
2815 template <class _RawFunc,
2816 class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
2817 _LIBCPP_INLINE_VISIBILITY
2818 explicit __not_fn_imp(_RawFunc&& __rf)
2819 : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
2820
2821 template <class _RawFunc>
2822 friend inline _LIBCPP_INLINE_VISIBILITY
2823 __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
2824};
2825
2826template <class _RawFunc>
2827inline _LIBCPP_INLINE_VISIBILITY
2828__not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
2829 return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
2830}
2831
Eric Fiselier0d974f12015-07-14 20:16:15 +00002832#endif
2833
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002834// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835
Marshall Clowa40686b2018-01-08 19:18:00 +00002836template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00002837pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00002838__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2839 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
2840 forward_iterator_tag, forward_iterator_tag)
2841{
2842 if (__first2 == __last2)
2843 return make_pair(__first1, __first1); // Everything matches an empty sequence
2844 while (true)
2845 {
2846 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
2847 while (true)
2848 {
2849 if (__first1 == __last1) // return __last1 if no element matches *__first2
2850 return make_pair(__last1, __last1);
2851 if (__pred(*__first1, *__first2))
2852 break;
2853 ++__first1;
2854 }
2855 // *__first1 matches *__first2, now match elements after here
2856 _ForwardIterator1 __m1 = __first1;
2857 _ForwardIterator2 __m2 = __first2;
2858 while (true)
2859 {
2860 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
2861 return make_pair(__first1, __m1);
2862 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
2863 return make_pair(__last1, __last1);
2864 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
2865 {
2866 ++__first1;
2867 break;
2868 } // else there is a match, check next elements
2869 }
2870 }
2871}
2872
2873template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
2874_LIBCPP_CONSTEXPR_AFTER_CXX11
2875pair<_RandomAccessIterator1, _RandomAccessIterator1>
2876__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
2877 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
2878 random_access_iterator_tag, random_access_iterator_tag)
2879{
2880 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
2881 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
2882 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
2883 const _D2 __len2 = __last2 - __first2;
2884 if (__len2 == 0)
2885 return make_pair(__first1, __first1);
2886 const _D1 __len1 = __last1 - __first1;
2887 if (__len1 < __len2)
2888 return make_pair(__last1, __last1);
2889 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
2890
2891 while (true)
2892 {
2893 while (true)
2894 {
2895 if (__first1 == __s)
2896 return make_pair(__last1, __last1);
2897 if (__pred(*__first1, *__first2))
2898 break;
2899 ++__first1;
2900 }
2901
2902 _RandomAccessIterator1 __m1 = __first1;
2903 _RandomAccessIterator2 __m2 = __first2;
2904 while (true)
2905 {
2906 if (++__m2 == __last2)
2907 return make_pair(__first1, __first1 + __len2);
2908 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
2909 if (!__pred(*__m1, *__m2))
2910 {
2911 ++__first1;
2912 break;
2913 }
2914 }
2915 }
2916}
2917
2918#if _LIBCPP_STD_VER > 14
2919
2920// default searcher
2921template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Dimitry Andricfd3633d2018-02-13 17:40:59 +00002922class _LIBCPP_TYPE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00002923public:
2924 _LIBCPP_INLINE_VISIBILITY
Louis Dionne44bcff92018-08-03 22:36:53 +00002925 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00002926 _BinaryPredicate __p = _BinaryPredicate())
2927 : __first_(__f), __last_(__l), __pred_(__p) {}
2928
2929 template <typename _ForwardIterator2>
2930 _LIBCPP_INLINE_VISIBILITY
2931 pair<_ForwardIterator2, _ForwardIterator2>
2932 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
2933 {
2934 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
2935 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
2936 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
2937 }
2938
2939private:
2940 _ForwardIterator __first_;
2941 _ForwardIterator __last_;
2942 _BinaryPredicate __pred_;
2943 };
2944
2945#endif // _LIBCPP_STD_VER > 14
2946
Louis Dionnedb1892a2018-12-03 14:03:27 +00002947#if _LIBCPP_STD_VER > 17
2948template <class _Tp>
2949using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
2950
2951template <class _Tp>
2952using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
2953#endif // > C++17
2954
Marshall Clow29b53f22018-12-14 18:49:35 +00002955template <class _Container, class _Predicate>
2956inline void __libcpp_erase_if_container( _Container& __c, _Predicate __pred)
2957{
2958 for (typename _Container::iterator __iter = __c.begin(), __last = __c.end(); __iter != __last;)
2959 {
2960 if (__pred(*__iter))
2961 __iter = __c.erase(__iter);
2962 else
2963 ++__iter;
2964 }
2965}
2966
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967_LIBCPP_END_NAMESPACE_STD
2968
2969#endif // _LIBCPP_FUNCTIONAL