blob: 8a724f64c9429dd585c6484956d5d01bbec6ce5b [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
Arthur O'Dwyer8fa87942020-12-05 19:37:41 -050045 template<class U>
46 reference_wrapper(U&&);
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
Arthur O'Dwyer8fa87942020-12-05 19:37:41 -050062template <class T>
63 reference_wrapper(T&) -> reference_wrapper<T>;
64
Howard Hinnantf7724cd2011-05-28 17:59:48 +000065template <class T> reference_wrapper<T> ref(T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000066template <class T> void ref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000067template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068
Howard Hinnantf7724cd2011-05-28 17:59:48 +000069template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000070template <class T> void cref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000071template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
Louis Dionnedb1892a2018-12-03 14:03:27 +000073template <class T> struct unwrap_reference; // since C++20
74template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { }; // since C++20
75template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
76template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
77
Marshall Clow974bae22013-07-29 14:21:53 +000078template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000079struct plus : binary_function<T, T, T>
80{
81 T operator()(const T& x, const T& y) const;
82};
83
Marshall Clow974bae22013-07-29 14:21:53 +000084template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000085struct minus : binary_function<T, T, T>
86{
87 T operator()(const T& x, const T& y) const;
88};
89
Marshall Clow974bae22013-07-29 14:21:53 +000090template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000091struct multiplies : binary_function<T, T, T>
92{
93 T operator()(const T& x, const T& y) const;
94};
95
Marshall Clow974bae22013-07-29 14:21:53 +000096template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000097struct divides : binary_function<T, T, T>
98{
99 T operator()(const T& x, const T& y) const;
100};
101
Marshall Clow974bae22013-07-29 14:21:53 +0000102template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103struct modulus : binary_function<T, T, T>
104{
105 T operator()(const T& x, const T& y) const;
106};
107
Marshall Clow974bae22013-07-29 14:21:53 +0000108template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000109struct negate : unary_function<T, T>
110{
111 T operator()(const T& x) const;
112};
113
Marshall Clow974bae22013-07-29 14:21:53 +0000114template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115struct equal_to : binary_function<T, T, bool>
116{
117 bool operator()(const T& x, const T& y) const;
118};
119
Marshall Clow974bae22013-07-29 14:21:53 +0000120template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121struct not_equal_to : binary_function<T, T, bool>
122{
123 bool operator()(const T& x, const T& y) const;
124};
125
Marshall Clow974bae22013-07-29 14:21:53 +0000126template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127struct greater : binary_function<T, T, bool>
128{
129 bool operator()(const T& x, const T& y) const;
130};
131
Marshall Clow974bae22013-07-29 14:21:53 +0000132template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133struct less : binary_function<T, T, bool>
134{
135 bool operator()(const T& x, const T& y) const;
136};
137
Marshall Clow974bae22013-07-29 14:21:53 +0000138template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139struct greater_equal : binary_function<T, T, bool>
140{
141 bool operator()(const T& x, const T& y) const;
142};
143
Marshall Clow974bae22013-07-29 14:21:53 +0000144template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145struct less_equal : binary_function<T, T, bool>
146{
147 bool operator()(const T& x, const T& y) const;
148};
149
Marshall Clow974bae22013-07-29 14:21:53 +0000150template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151struct logical_and : binary_function<T, T, bool>
152{
153 bool operator()(const T& x, const T& y) const;
154};
155
Marshall Clow974bae22013-07-29 14:21:53 +0000156template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157struct logical_or : binary_function<T, T, bool>
158{
159 bool operator()(const T& x, const T& y) const;
160};
161
Marshall Clow974bae22013-07-29 14:21:53 +0000162template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163struct logical_not : unary_function<T, bool>
164{
165 bool operator()(const T& x) const;
166};
167
Marshall Clow974bae22013-07-29 14:21:53 +0000168template <class T> // <class T=void> in C++14
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500169struct bit_and : binary_function<T, T, T>
Marshall Clow974bae22013-07-29 14:21:53 +0000170{
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500171 T operator()(const T& x, const T& y) const;
Marshall Clow974bae22013-07-29 14:21:53 +0000172};
173
174template <class T> // <class T=void> in C++14
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500175struct bit_or : binary_function<T, T, T>
Marshall Clow974bae22013-07-29 14:21:53 +0000176{
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500177 T operator()(const T& x, const T& y) const;
Marshall Clow974bae22013-07-29 14:21:53 +0000178};
179
180template <class T> // <class T=void> in C++14
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500181struct bit_xor : binary_function<T, T, T>
Marshall Clow974bae22013-07-29 14:21:53 +0000182{
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500183 T operator()(const T& x, const T& y) const;
Marshall Clow974bae22013-07-29 14:21:53 +0000184};
185
186template <class T=void> // C++14
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500187struct bit_not : unary_function<T, T>
Marshall Clow974bae22013-07-29 14:21:53 +0000188{
Arthur O'Dwyer0e774452021-03-07 20:22:03 -0500189 T operator()(const T& x) const;
Marshall Clow974bae22013-07-29 14:21:53 +0000190};
191
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000193class unary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 : public unary_function<typename Predicate::argument_type, bool>
195{
196public:
197 explicit unary_negate(const Predicate& pred);
198 bool operator()(const typename Predicate::argument_type& x) const;
199};
200
Louis Dionne481a2662018-09-23 18:35:00 +0000201template <class Predicate> // deprecated in C++17
202unary_negate<Predicate> not1(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203
204template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000205class binary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 : public binary_function<typename Predicate::first_argument_type,
207 typename Predicate::second_argument_type,
208 bool>
209{
210public:
211 explicit binary_negate(const Predicate& pred);
212 bool operator()(const typename Predicate::first_argument_type& x,
213 const typename Predicate::second_argument_type& y) const;
214};
215
Louis Dionne481a2662018-09-23 18:35:00 +0000216template <class Predicate> // deprecated in C++17
217binary_negate<Predicate> not2(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500219template <class F>
220constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
Eric Fiselier934f63b2016-06-02 01:25:41 +0000221
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222template<class T> struct is_bind_expression;
223template<class T> struct is_placeholder;
224
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000225 // See C++14 20.9.9, Function object binders
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000226template <class T> inline constexpr bool is_bind_expression_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000227 = is_bind_expression<T>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000228template <class T> inline constexpr int is_placeholder_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000229 = is_placeholder<T>::value; // C++17
230
231
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000232template<class Fn, class... BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500233 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000234template<class R, class Fn, class... BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500235 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236
Louis Dionneaf34f122019-04-03 17:54:37 +0000237template<class F, class... Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500238 constexpr // constexpr in C++20
Louis Dionneaf34f122019-04-03 17:54:37 +0000239 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
240 noexcept(is_nothrow_invocable_v<F, Args...>);
241
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000242namespace placeholders {
243 // M is the implementation-defined number of placeholders
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 extern unspecified _1;
245 extern unspecified _2;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000246 .
247 .
248 .
Howard Hinnantc834c512011-11-29 18:15:50 +0000249 extern unspecified _Mp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250}
251
252template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000253class binder1st // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 : public unary_function<typename Operation::second_argument_type,
255 typename Operation::result_type>
256{
257protected:
258 Operation op;
259 typename Operation::first_argument_type value;
260public:
261 binder1st(const Operation& x, const typename Operation::first_argument_type y);
262 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
263 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
264};
265
266template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000267binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
269template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000270class binder2nd // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000271 : public unary_function<typename Operation::first_argument_type,
272 typename Operation::result_type>
273{
274protected:
275 Operation op;
276 typename Operation::second_argument_type value;
277public:
278 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
279 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
280 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
281};
282
283template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000284binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285
Marshall Clow26a027c2017-04-13 18:25:32 +0000286template <class Arg, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287class pointer_to_unary_function : public unary_function<Arg, Result>
288{
289public:
290 explicit pointer_to_unary_function(Result (*f)(Arg));
291 Result operator()(Arg x) const;
292};
293
294template <class Arg, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000295pointer_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 +0000296
Marshall Clow26a027c2017-04-13 18:25:32 +0000297template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
299{
300public:
301 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
302 Result operator()(Arg1 x, Arg2 y) const;
303};
304
305template <class Arg1, class Arg2, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000306pointer_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 +0000307
Marshall Clow26a027c2017-04-13 18:25:32 +0000308template<class S, class T> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309class mem_fun_t : public unary_function<T*, S>
310{
311public:
312 explicit mem_fun_t(S (T::*p)());
313 S operator()(T* p) const;
314};
315
316template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000317class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318{
319public:
320 explicit mem_fun1_t(S (T::*p)(A));
321 S operator()(T* p, A x) const;
322};
323
Marshall Clow26a027c2017-04-13 18:25:32 +0000324template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
325template<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 +0000326
327template<class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000328class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329{
330public:
331 explicit mem_fun_ref_t(S (T::*p)());
332 S operator()(T& p) const;
333};
334
335template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000336class 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 +0000337{
338public:
339 explicit mem_fun1_ref_t(S (T::*p)(A));
340 S operator()(T& p, A x) const;
341};
342
Marshall Clow26a027c2017-04-13 18:25:32 +0000343template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
344template<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 +0000345
346template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000347class 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 +0000348{
349public:
350 explicit const_mem_fun_t(S (T::*p)() const);
351 S operator()(const T* p) const;
352};
353
354template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000355class 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 +0000356{
357public:
358 explicit const_mem_fun1_t(S (T::*p)(A) const);
359 S operator()(const T* p, A x) const;
360};
361
Marshall Clow26a027c2017-04-13 18:25:32 +0000362template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
363template <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 +0000364
365template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000366class 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 +0000367{
368public:
369 explicit const_mem_fun_ref_t(S (T::*p)() const);
370 S operator()(const T& p) const;
371};
372
373template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000374class 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 +0000375{
376public:
377 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
378 S operator()(const T& p, A x) const;
379};
380
Marshall Clow26a027c2017-04-13 18:25:32 +0000381template <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
382template <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 +0000383
Arthur O'Dwyer81449d32020-12-25 14:48:39 -0500384template<class R, class T>
385constexpr unspecified mem_fn(R T::*); // constexpr in C++20
Howard Hinnantf06d9262010-08-20 19:36:46 +0000386
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387class bad_function_call
388 : public exception
389{
390};
391
Howard Hinnantf06d9262010-08-20 19:36:46 +0000392template<class> class function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
Howard Hinnantf06d9262010-08-20 19:36:46 +0000394template<class R, class... ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395class function<R(ArgTypes...)>
396 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
397 // ArgTypes contains T1
398 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
399 // ArgTypes contains T1 and T2
400{
401public:
402 typedef R result_type;
403
Howard Hinnantf06d9262010-08-20 19:36:46 +0000404 // construct/copy/destroy:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000405 function() noexcept;
406 function(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000408 function(function&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 template<class F>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410 function(F);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000412 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000414 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000416 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000418 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419 template<class F, Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000420 function(allocator_arg_t, const Alloc&, F); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000423 function& operator=(function&&) noexcept;
Howard Hinnant7b85be02011-05-29 13:53:56 +0000424 function& operator=(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425 template<class F>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000426 function& operator=(F&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427 template<class F>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000428 function& operator=(reference_wrapper<F>) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430 ~function();
431
Howard Hinnantf06d9262010-08-20 19:36:46 +0000432 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000433 void swap(function&) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +0000434 template<class F, class Alloc>
Marshall Clowfc8fd832016-01-25 17:29:55 +0000435 void assign(F&&, const Alloc&); // Removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436
Howard Hinnantf06d9262010-08-20 19:36:46 +0000437 // function capacity:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000438 explicit operator bool() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439
Howard Hinnantf06d9262010-08-20 19:36:46 +0000440 // function invocation:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441 R operator()(ArgTypes...) const;
442
Howard Hinnantf06d9262010-08-20 19:36:46 +0000443 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000444 const std::type_info& target_type() const noexcept;
445 template <typename T> T* target() noexcept;
446 template <typename T> const T* target() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447};
448
Louis Dionne4af49712019-07-18 19:50:56 +0000449// Deduction guides
450template<class R, class ...Args>
451function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
452
453template<class F>
454function(F) -> function<see-below>; // since C++17
455
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000456// Null pointer comparisons:
457template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000458 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000460template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000461 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000462
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000463template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000464 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000465
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000466template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000467 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000469// specialized algorithms:
470template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000471 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000472
473template <class T> struct hash;
474
475template <> struct hash<bool>;
476template <> struct hash<char>;
477template <> struct hash<signed char>;
478template <> struct hash<unsigned char>;
Yuriy Chernyshovfbb87fa2020-12-08 13:39:56 -0500479template <> struct hash<char8_t>; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000480template <> struct hash<char16_t>;
481template <> struct hash<char32_t>;
482template <> struct hash<wchar_t>;
483template <> struct hash<short>;
484template <> struct hash<unsigned short>;
485template <> struct hash<int>;
486template <> struct hash<unsigned int>;
487template <> struct hash<long>;
488template <> struct hash<long long>;
489template <> struct hash<unsigned long>;
490template <> struct hash<unsigned long long>;
491
492template <> struct hash<float>;
493template <> struct hash<double>;
494template <> struct hash<long double>;
495
496template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000497template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000498
499} // std
500
501POLICY: For non-variadic implementations, the number of arguments is limited
502 to 3. It is hoped that the need for non-variadic implementations
503 will be minimal.
504
505*/
506
507#include <__config>
508#include <type_traits>
509#include <typeinfo>
510#include <exception>
511#include <memory>
512#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000513#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000514#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515
516#include <__functional_base>
517
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000518#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000520#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521
522_LIBCPP_BEGIN_NAMESPACE_STD
523
Marshall Clow974bae22013-07-29 14:21:53 +0000524#if _LIBCPP_STD_VER > 11
525template <class _Tp = void>
526#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000528#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000529struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530{
Marshall Clowd18ee652013-09-28 19:06:12 +0000531 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
532 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533 {return __x + __y;}
534};
535
Marshall Clow974bae22013-07-29 14:21:53 +0000536#if _LIBCPP_STD_VER > 11
537template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000538struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000539{
540 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000541 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
542 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000543 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
544 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
545 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000546 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000547};
548#endif
549
550
551#if _LIBCPP_STD_VER > 11
552template <class _Tp = void>
553#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000555#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000556struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557{
Marshall Clowd18ee652013-09-28 19:06:12 +0000558 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
559 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560 {return __x - __y;}
561};
562
Marshall Clow974bae22013-07-29 14:21:53 +0000563#if _LIBCPP_STD_VER > 11
564template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000565struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000566{
567 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000568 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
569 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000570 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
571 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
572 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000573 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000574};
575#endif
576
577
578#if _LIBCPP_STD_VER > 11
579template <class _Tp = void>
580#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000582#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000583struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584{
Marshall Clowd18ee652013-09-28 19:06:12 +0000585 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
586 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587 {return __x * __y;}
588};
589
Marshall Clow974bae22013-07-29 14:21:53 +0000590#if _LIBCPP_STD_VER > 11
591template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000592struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000593{
594 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000595 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
596 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000597 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
598 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
599 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000600 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000601};
602#endif
603
604
605#if _LIBCPP_STD_VER > 11
606template <class _Tp = void>
607#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000609#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000610struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611{
Marshall Clowd18ee652013-09-28 19:06:12 +0000612 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
613 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614 {return __x / __y;}
615};
616
Marshall Clow974bae22013-07-29 14:21:53 +0000617#if _LIBCPP_STD_VER > 11
618template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000619struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000620{
621 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000622 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
623 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000624 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
625 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
626 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000627 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000628};
629#endif
630
631
632#if _LIBCPP_STD_VER > 11
633template <class _Tp = void>
634#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000636#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000637struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638{
Marshall Clowd18ee652013-09-28 19:06:12 +0000639 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
640 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641 {return __x % __y;}
642};
643
Marshall Clow974bae22013-07-29 14:21:53 +0000644#if _LIBCPP_STD_VER > 11
645template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000646struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000647{
648 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000649 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
650 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000651 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
652 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
653 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000654 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000655};
656#endif
657
658
659#if _LIBCPP_STD_VER > 11
660template <class _Tp = void>
661#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000663#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000664struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665{
Marshall Clowd18ee652013-09-28 19:06:12 +0000666 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
667 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668 {return -__x;}
669};
670
Marshall Clow974bae22013-07-29 14:21:53 +0000671#if _LIBCPP_STD_VER > 11
672template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000673struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000674{
675 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000676 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
677 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000678 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
679 -> decltype (- _VSTD::forward<_Tp>(__x))
680 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000681 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000682};
683#endif
684
685
686#if _LIBCPP_STD_VER > 11
687template <class _Tp = void>
688#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000689template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000690#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000691struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692{
Marshall Clowd18ee652013-09-28 19:06:12 +0000693 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
694 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695 {return __x == __y;}
696};
697
Marshall Clow974bae22013-07-29 14:21:53 +0000698#if _LIBCPP_STD_VER > 11
699template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000700struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000701{
Marshall Clowd18ee652013-09-28 19:06:12 +0000702 template <class _T1, class _T2>
703 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000704 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000705 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
706 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
707 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000708 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000709};
710#endif
711
712
713#if _LIBCPP_STD_VER > 11
714template <class _Tp = void>
715#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000717#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000718struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719{
Marshall Clowd18ee652013-09-28 19:06:12 +0000720 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
721 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722 {return __x != __y;}
723};
724
Marshall Clow974bae22013-07-29 14:21:53 +0000725#if _LIBCPP_STD_VER > 11
726template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000727struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000728{
Marshall Clowd18ee652013-09-28 19:06:12 +0000729 template <class _T1, class _T2>
730 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000731 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000732 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
733 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
734 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000735 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000736};
737#endif
738
739
740#if _LIBCPP_STD_VER > 11
741template <class _Tp = void>
742#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000744#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000745struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746{
Marshall Clowd18ee652013-09-28 19:06:12 +0000747 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
748 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 {return __x > __y;}
750};
751
Marshall Clow974bae22013-07-29 14:21:53 +0000752#if _LIBCPP_STD_VER > 11
753template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000754struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000755{
Marshall Clowd18ee652013-09-28 19:06:12 +0000756 template <class _T1, class _T2>
757 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000758 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000759 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
760 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
761 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000762 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000763};
764#endif
765
766
Howard Hinnantb17caf92012-02-21 21:02:58 +0000767// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768
Marshall Clow974bae22013-07-29 14:21:53 +0000769#if _LIBCPP_STD_VER > 11
770template <class _Tp = void>
771#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000773#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000774struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775{
Marshall Clowd18ee652013-09-28 19:06:12 +0000776 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
777 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778 {return __x >= __y;}
779};
780
Marshall Clow974bae22013-07-29 14:21:53 +0000781#if _LIBCPP_STD_VER > 11
782template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000783struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000784{
Marshall Clowd18ee652013-09-28 19:06:12 +0000785 template <class _T1, class _T2>
786 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000787 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000788 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
789 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
790 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000791 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000792};
793#endif
794
795
796#if _LIBCPP_STD_VER > 11
797template <class _Tp = void>
798#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000800#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000801struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802{
Marshall Clowd18ee652013-09-28 19:06:12 +0000803 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
804 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805 {return __x <= __y;}
806};
807
Marshall Clow974bae22013-07-29 14:21:53 +0000808#if _LIBCPP_STD_VER > 11
809template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000810struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000811{
Marshall Clowd18ee652013-09-28 19:06:12 +0000812 template <class _T1, class _T2>
813 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000814 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000815 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
816 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
817 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000818 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000819};
820#endif
821
822
823#if _LIBCPP_STD_VER > 11
824template <class _Tp = void>
825#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000827#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000828struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829{
Marshall Clowd18ee652013-09-28 19:06:12 +0000830 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
831 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 {return __x && __y;}
833};
834
Marshall Clow974bae22013-07-29 14:21:53 +0000835#if _LIBCPP_STD_VER > 11
836template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000837struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000838{
Marshall Clowd18ee652013-09-28 19:06:12 +0000839 template <class _T1, class _T2>
840 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000841 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000842 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
843 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
844 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000845 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000846};
847#endif
848
849
850#if _LIBCPP_STD_VER > 11
851template <class _Tp = void>
852#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000854#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000855struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856{
Marshall Clowd18ee652013-09-28 19:06:12 +0000857 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
858 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 {return __x || __y;}
860};
861
Marshall Clow974bae22013-07-29 14:21:53 +0000862#if _LIBCPP_STD_VER > 11
863template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000864struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000865{
Marshall Clowd18ee652013-09-28 19:06:12 +0000866 template <class _T1, class _T2>
867 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000868 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000869 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
870 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
871 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000872 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000873};
874#endif
875
876
877#if _LIBCPP_STD_VER > 11
878template <class _Tp = void>
879#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000881#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000882struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883{
Marshall Clowd18ee652013-09-28 19:06:12 +0000884 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
885 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886 {return !__x;}
887};
888
Marshall Clow974bae22013-07-29 14:21:53 +0000889#if _LIBCPP_STD_VER > 11
890template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000891struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000892{
893 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000894 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
895 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000896 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
897 -> decltype (!_VSTD::forward<_Tp>(__x))
898 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000899 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000900};
901#endif
902
903
904#if _LIBCPP_STD_VER > 11
905template <class _Tp = void>
906#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000908#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000909struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910{
Marshall Clowd18ee652013-09-28 19:06:12 +0000911 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
912 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913 {return __x & __y;}
914};
915
Marshall Clow974bae22013-07-29 14:21:53 +0000916#if _LIBCPP_STD_VER > 11
917template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000918struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000919{
Marshall Clowd18ee652013-09-28 19:06:12 +0000920 template <class _T1, class _T2>
921 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000922 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000923 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
924 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
925 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000926 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000927};
928#endif
929
930
931#if _LIBCPP_STD_VER > 11
932template <class _Tp = void>
933#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000935#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000936struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937{
Marshall Clowd18ee652013-09-28 19:06:12 +0000938 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
939 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 {return __x | __y;}
941};
942
Marshall Clow974bae22013-07-29 14:21:53 +0000943#if _LIBCPP_STD_VER > 11
944template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000945struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000946{
Marshall Clowd18ee652013-09-28 19:06:12 +0000947 template <class _T1, class _T2>
948 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000949 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000950 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
951 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
952 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000953 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000954};
955#endif
956
957
958#if _LIBCPP_STD_VER > 11
959template <class _Tp = void>
960#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000962#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000963struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964{
Marshall Clowd18ee652013-09-28 19:06:12 +0000965 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
966 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 {return __x ^ __y;}
968};
969
Marshall Clow974bae22013-07-29 14:21:53 +0000970#if _LIBCPP_STD_VER > 11
971template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000972struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000973{
Marshall Clowd18ee652013-09-28 19:06:12 +0000974 template <class _T1, class _T2>
975 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000976 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000977 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
978 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
979 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000980 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000981};
982#endif
983
984
985#if _LIBCPP_STD_VER > 11
986template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000987struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000988{
Marshall Clowd18ee652013-09-28 19:06:12 +0000989 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
990 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000991 {return ~__x;}
992};
993
994template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000995struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000996{
997 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000998 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
999 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +00001000 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
1001 -> decltype (~_VSTD::forward<_Tp>(__x))
1002 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +00001003 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +00001004};
1005#endif
1006
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001008class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009 : public unary_function<typename _Predicate::argument_type, bool>
1010{
1011 _Predicate __pred_;
1012public:
Marshall Clowd18ee652013-09-28 19:06:12 +00001013 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1014 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +00001016 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1017 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 {return !__pred_(__x);}
1019};
1020
1021template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001022_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023unary_negate<_Predicate>
1024not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1025
1026template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001027class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 : public binary_function<typename _Predicate::first_argument_type,
1029 typename _Predicate::second_argument_type,
1030 bool>
1031{
1032 _Predicate __pred_;
1033public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001034 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001035 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1036
1037 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1038 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 const typename _Predicate::second_argument_type& __y) const
1040 {return !__pred_(__x, __y);}
1041};
1042
1043template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001044_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045binary_negate<_Predicate>
1046not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1047
Marshall Clow26a027c2017-04-13 18:25:32 +00001048#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001050class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 : public unary_function<typename __Operation::second_argument_type,
1052 typename __Operation::result_type>
1053{
1054protected:
1055 __Operation op;
1056 typename __Operation::first_argument_type value;
1057public:
1058 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1059 const typename __Operation::first_argument_type __y)
1060 : op(__x), value(__y) {}
1061 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1062 (typename __Operation::second_argument_type& __x) const
1063 {return op(value, __x);}
1064 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1065 (const typename __Operation::second_argument_type& __x) const
1066 {return op(value, __x);}
1067};
1068
1069template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001070_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071binder1st<__Operation>
1072bind1st(const __Operation& __op, const _Tp& __x)
1073 {return binder1st<__Operation>(__op, __x);}
1074
1075template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001076class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 : public unary_function<typename __Operation::first_argument_type,
1078 typename __Operation::result_type>
1079{
1080protected:
1081 __Operation op;
1082 typename __Operation::second_argument_type value;
1083public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1086 : op(__x), value(__y) {}
1087 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1088 ( typename __Operation::first_argument_type& __x) const
1089 {return op(__x, value);}
1090 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1091 (const typename __Operation::first_argument_type& __x) const
1092 {return op(__x, value);}
1093};
1094
1095template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001096_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097binder2nd<__Operation>
1098bind2nd(const __Operation& __op, const _Tp& __x)
1099 {return binder2nd<__Operation>(__op, __x);}
1100
1101template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001102class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001103 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104{
1105 _Result (*__f_)(_Arg);
1106public:
1107 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1108 : __f_(__f) {}
1109 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1110 {return __f_(__x);}
1111};
1112
1113template <class _Arg, 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_unary_function<_Arg,_Result>
1116ptr_fun(_Result (*__f)(_Arg))
1117 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1118
1119template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001120class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001121 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122{
1123 _Result (*__f_)(_Arg1, _Arg2);
1124public:
1125 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1126 : __f_(__f) {}
1127 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1128 {return __f_(__x, __y);}
1129};
1130
1131template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001132_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133pointer_to_binary_function<_Arg1,_Arg2,_Result>
1134ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1135 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1136
1137template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001138class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1139 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140{
1141 _Sp (_Tp::*__p_)();
1142public:
1143 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1144 : __p_(__p) {}
1145 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1146 {return (__p->*__p_)();}
1147};
1148
1149template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001150class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1151 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152{
1153 _Sp (_Tp::*__p_)(_Ap);
1154public:
1155 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1156 : __p_(__p) {}
1157 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1158 {return (__p->*__p_)(__x);}
1159};
1160
1161template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001162_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163mem_fun_t<_Sp,_Tp>
1164mem_fun(_Sp (_Tp::*__f)())
1165 {return mem_fun_t<_Sp,_Tp>(__f);}
1166
1167template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001168_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169mem_fun1_t<_Sp,_Tp,_Ap>
1170mem_fun(_Sp (_Tp::*__f)(_Ap))
1171 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1172
1173template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001174class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1175 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176{
1177 _Sp (_Tp::*__p_)();
1178public:
1179 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1180 : __p_(__p) {}
1181 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1182 {return (__p.*__p_)();}
1183};
1184
1185template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001186class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1187 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188{
1189 _Sp (_Tp::*__p_)(_Ap);
1190public:
1191 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1192 : __p_(__p) {}
1193 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1194 {return (__p.*__p_)(__x);}
1195};
1196
1197template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001198_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199mem_fun_ref_t<_Sp,_Tp>
1200mem_fun_ref(_Sp (_Tp::*__f)())
1201 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1202
1203template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001204_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205mem_fun1_ref_t<_Sp,_Tp,_Ap>
1206mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1207 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1208
1209template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001210class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1211 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212{
1213 _Sp (_Tp::*__p_)() const;
1214public:
1215 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1216 : __p_(__p) {}
1217 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1218 {return (__p->*__p_)();}
1219};
1220
1221template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001222class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1223 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224{
1225 _Sp (_Tp::*__p_)(_Ap) const;
1226public:
1227 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1228 : __p_(__p) {}
1229 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1230 {return (__p->*__p_)(__x);}
1231};
1232
1233template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001234_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235const_mem_fun_t<_Sp,_Tp>
1236mem_fun(_Sp (_Tp::*__f)() const)
1237 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1238
1239template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001240_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241const_mem_fun1_t<_Sp,_Tp,_Ap>
1242mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1243 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1244
1245template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001246class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1247 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248{
1249 _Sp (_Tp::*__p_)() const;
1250public:
1251 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1252 : __p_(__p) {}
1253 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1254 {return (__p.*__p_)();}
1255};
1256
1257template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001258class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001259 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260{
1261 _Sp (_Tp::*__p_)(_Ap) const;
1262public:
1263 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1264 : __p_(__p) {}
1265 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1266 {return (__p.*__p_)(__x);}
1267};
1268
1269template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001270_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271const_mem_fun_ref_t<_Sp,_Tp>
1272mem_fun_ref(_Sp (_Tp::*__f)() const)
1273 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1274
1275template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001276_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1278mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1279 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001280#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001282////////////////////////////////////////////////////////////////////////////////
1283// MEMFUN
1284//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286template <class _Tp>
1287class __mem_fn
1288 : public __weak_result_type<_Tp>
1289{
1290public:
1291 // types
1292 typedef _Tp type;
1293private:
1294 type __f_;
1295
1296public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001297 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1298 __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001300#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301 // invoke
1302 template <class... _ArgTypes>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001303 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier2cc48332015-07-22 22:43:27 +00001304 typename __invoke_return<type, _ArgTypes...>::type
1305 operator() (_ArgTypes&&... __args) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001306 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001307 }
1308#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001309
1310 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001311 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001312 typename __invoke_return0<type, _A0>::type
1313 operator() (_A0& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001314 return _VSTD::__invoke(__f_, __a0);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001315 }
1316
Eric Fiselierce1813a2015-08-26 20:15:02 +00001317 template <class _A0>
1318 _LIBCPP_INLINE_VISIBILITY
1319 typename __invoke_return0<type, _A0 const>::type
1320 operator() (_A0 const& __a0) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001321 return _VSTD::__invoke(__f_, __a0);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001322 }
1323
Eric Fiselier2cc48332015-07-22 22:43:27 +00001324 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001325 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001326 typename __invoke_return1<type, _A0, _A1>::type
1327 operator() (_A0& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001328 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001329 }
1330
Eric Fiselierce1813a2015-08-26 20:15:02 +00001331 template <class _A0, class _A1>
1332 _LIBCPP_INLINE_VISIBILITY
1333 typename __invoke_return1<type, _A0 const, _A1>::type
1334 operator() (_A0 const& __a0, _A1& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001335 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001336 }
1337
1338 template <class _A0, class _A1>
1339 _LIBCPP_INLINE_VISIBILITY
1340 typename __invoke_return1<type, _A0, _A1 const>::type
1341 operator() (_A0& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001342 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001343 }
1344
1345 template <class _A0, class _A1>
1346 _LIBCPP_INLINE_VISIBILITY
1347 typename __invoke_return1<type, _A0 const, _A1 const>::type
1348 operator() (_A0 const& __a0, _A1 const& __a1) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001349 return _VSTD::__invoke(__f_, __a0, __a1);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001350 }
1351
Eric Fiselier2cc48332015-07-22 22:43:27 +00001352 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001353 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001354 typename __invoke_return2<type, _A0, _A1, _A2>::type
1355 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001356 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselier2cc48332015-07-22 22:43:27 +00001357 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001358
1359 template <class _A0, class _A1, class _A2>
1360 _LIBCPP_INLINE_VISIBILITY
1361 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1362 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001363 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001364 }
1365
1366 template <class _A0, class _A1, class _A2>
1367 _LIBCPP_INLINE_VISIBILITY
1368 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1369 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001370 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001371 }
1372
1373 template <class _A0, class _A1, class _A2>
1374 _LIBCPP_INLINE_VISIBILITY
1375 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1376 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001377 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001378 }
1379
1380 template <class _A0, class _A1, class _A2>
1381 _LIBCPP_INLINE_VISIBILITY
1382 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1383 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001384 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001385 }
1386
1387 template <class _A0, class _A1, class _A2>
1388 _LIBCPP_INLINE_VISIBILITY
1389 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1390 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001391 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001392 }
1393
1394 template <class _A0, class _A1, class _A2>
1395 _LIBCPP_INLINE_VISIBILITY
1396 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1397 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001398 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001399 }
1400
1401 template <class _A0, class _A1, class _A2>
1402 _LIBCPP_INLINE_VISIBILITY
1403 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1404 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05001405 return _VSTD::__invoke(__f_, __a0, __a1, __a2);
Eric Fiselierce1813a2015-08-26 20:15:02 +00001406 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001407#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408};
1409
Howard Hinnantc834c512011-11-29 18:15:50 +00001410template<class _Rp, class _Tp>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05001411inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00001412__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001413mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414{
Howard Hinnantc834c512011-11-29 18:15:50 +00001415 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416}
1417
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001418////////////////////////////////////////////////////////////////////////////////
1419// FUNCTION
1420//==============================================================================
1421
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422// bad_function_call
1423
Howard Hinnant4ff57432010-09-21 22:55:27 +00001424class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425 : public exception
1426{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001427#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1428public:
1429 virtual ~bad_function_call() _NOEXCEPT;
1430
1431 virtual const char* what() const _NOEXCEPT;
1432#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433};
1434
Louis Dionne16fe2952018-07-11 23:14:33 +00001435_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001436void __throw_bad_function_call()
1437{
1438#ifndef _LIBCPP_NO_EXCEPTIONS
1439 throw bad_function_call();
1440#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001441 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001442#endif
1443}
1444
Louis Dionne44d1f812020-03-09 11:16:22 -04001445#if defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DISABLE_DEPRECATION_WARNINGS) && __has_attribute(deprecated)
1446# define _LIBCPP_DEPRECATED_CXX03_FUNCTION \
1447 __attribute__((deprecated("Using std::function in C++03 is not supported anymore. Please upgrade to C++11 or later, or use a different type")))
1448#else
1449# define _LIBCPP_DEPRECATED_CXX03_FUNCTION /* nothing */
1450#endif
1451
1452template<class _Fp> class _LIBCPP_DEPRECATED_CXX03_FUNCTION _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453
1454namespace __function
1455{
1456
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001457template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458struct __maybe_derive_from_unary_function
1459{
1460};
1461
Howard Hinnantc834c512011-11-29 18:15:50 +00001462template<class _Rp, class _A1>
1463struct __maybe_derive_from_unary_function<_Rp(_A1)>
1464 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465{
1466};
1467
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001468template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469struct __maybe_derive_from_binary_function
1470{
1471};
1472
Howard Hinnantc834c512011-11-29 18:15:50 +00001473template<class _Rp, class _A1, class _A2>
1474struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1475 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476{
1477};
1478
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001479template <class _Fp>
1480_LIBCPP_INLINE_VISIBILITY
1481bool __not_null(_Fp const&) { return true; }
1482
1483template <class _Fp>
1484_LIBCPP_INLINE_VISIBILITY
1485bool __not_null(_Fp* __ptr) { return __ptr; }
1486
1487template <class _Ret, class _Class>
1488_LIBCPP_INLINE_VISIBILITY
1489bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1490
1491template <class _Fp>
1492_LIBCPP_INLINE_VISIBILITY
1493bool __not_null(function<_Fp> const& __f) { return !!__f; }
1494
Louis Dionnee2391d72020-04-22 13:58:17 -04001495#ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
1496template <class _Rp, class ..._Args>
1497_LIBCPP_INLINE_VISIBILITY
1498bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
1499#endif
1500
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001501} // namespace __function
1502
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001503#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001504
1505namespace __function {
1506
Eric Fiselier125798e2018-12-10 18:14:09 +00001507// __alloc_func holds a functor and an allocator.
1508
1509template <class _Fp, class _Ap, class _FB> class __alloc_func;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001510template <class _Fp, class _FB>
1511class __default_alloc_func;
Eric Fiselier125798e2018-12-10 18:14:09 +00001512
1513template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
1514class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
1515{
1516 __compressed_pair<_Fp, _Ap> __f_;
1517
1518 public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001519 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
1520 typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
Eric Fiselier125798e2018-12-10 18:14:09 +00001521
1522 _LIBCPP_INLINE_VISIBILITY
1523 const _Target& __target() const { return __f_.first(); }
1524
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001525 // WIN32 APIs may define __allocator, so use __get_allocator instead.
Eric Fiselier125798e2018-12-10 18:14:09 +00001526 _LIBCPP_INLINE_VISIBILITY
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001527 const _Alloc& __get_allocator() const { return __f_.second(); }
Eric Fiselier125798e2018-12-10 18:14:09 +00001528
1529 _LIBCPP_INLINE_VISIBILITY
1530 explicit __alloc_func(_Target&& __f)
1531 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1532 _VSTD::forward_as_tuple())
1533 {
1534 }
1535
1536 _LIBCPP_INLINE_VISIBILITY
1537 explicit __alloc_func(const _Target& __f, const _Alloc& __a)
1538 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1539 _VSTD::forward_as_tuple(__a))
1540 {
1541 }
1542
1543 _LIBCPP_INLINE_VISIBILITY
1544 explicit __alloc_func(const _Target& __f, _Alloc&& __a)
1545 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1546 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1547 {
1548 }
1549
1550 _LIBCPP_INLINE_VISIBILITY
1551 explicit __alloc_func(_Target&& __f, _Alloc&& __a)
1552 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1553 _VSTD::forward_as_tuple(_VSTD::move(__a)))
1554 {
1555 }
1556
1557 _LIBCPP_INLINE_VISIBILITY
1558 _Rp operator()(_ArgTypes&&... __arg)
1559 {
1560 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1561 return _Invoker::__call(__f_.first(),
1562 _VSTD::forward<_ArgTypes>(__arg)...);
1563 }
1564
1565 _LIBCPP_INLINE_VISIBILITY
1566 __alloc_func* __clone() const
1567 {
1568 typedef allocator_traits<_Alloc> __alloc_traits;
1569 typedef
1570 typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1571 _AA;
1572 _AA __a(__f_.second());
1573 typedef __allocator_destructor<_AA> _Dp;
1574 unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1575 ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
1576 return __hold.release();
1577 }
1578
1579 _LIBCPP_INLINE_VISIBILITY
1580 void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
Eric Fiselier74ebee62019-06-08 01:31:19 +00001581
1582 static void __destroy_and_delete(__alloc_func* __f) {
1583 typedef allocator_traits<_Alloc> __alloc_traits;
1584 typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
1585 _FunAlloc;
1586 _FunAlloc __a(__f->__get_allocator());
1587 __f->destroy();
1588 __a.deallocate(__f, 1);
1589 }
1590};
1591
1592template <class _Fp, class _Rp, class... _ArgTypes>
1593class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
1594 _Fp __f_;
1595
1596public:
Eric Fiselier4fc82a22019-06-12 02:03:31 +00001597 typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
Eric Fiselier74ebee62019-06-08 01:31:19 +00001598
1599 _LIBCPP_INLINE_VISIBILITY
1600 const _Target& __target() const { return __f_; }
1601
1602 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001603 explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001604
1605 _LIBCPP_INLINE_VISIBILITY
1606 explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
1607
1608 _LIBCPP_INLINE_VISIBILITY
1609 _Rp operator()(_ArgTypes&&... __arg) {
1610 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1611 return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
1612 }
1613
1614 _LIBCPP_INLINE_VISIBILITY
1615 __default_alloc_func* __clone() const {
1616 __builtin_new_allocator::__holder_t __hold =
1617 __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
1618 __default_alloc_func* __res =
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001619 ::new ((void*)__hold.get()) __default_alloc_func(__f_);
Eric Fiselier74ebee62019-06-08 01:31:19 +00001620 (void)__hold.release();
1621 return __res;
1622 }
1623
1624 _LIBCPP_INLINE_VISIBILITY
1625 void destroy() _NOEXCEPT { __f_.~_Target(); }
1626
1627 static void __destroy_and_delete(__default_alloc_func* __f) {
1628 __f->destroy();
1629 __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
1630 }
Eric Fiselier125798e2018-12-10 18:14:09 +00001631};
1632
1633// __base provides an abstract interface for copyable functors.
1634
Yunlian Jiang06c6f272020-03-18 17:06:18 -04001635template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636
Howard Hinnantc834c512011-11-29 18:15:50 +00001637template<class _Rp, class ..._ArgTypes>
1638class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639{
1640 __base(const __base&);
1641 __base& operator=(const __base&);
1642public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001643 _LIBCPP_INLINE_VISIBILITY __base() {}
1644 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645 virtual __base* __clone() const = 0;
1646 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001647 virtual void destroy() _NOEXCEPT = 0;
1648 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001649 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001650#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001651 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1652 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001653#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654};
1655
Eric Fiselier125798e2018-12-10 18:14:09 +00001656// __func implements __base for a given functor type.
1657
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658template<class _FD, class _Alloc, class _FB> class __func;
1659
Howard Hinnantc834c512011-11-29 18:15:50 +00001660template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1661class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1662 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663{
Eric Fiselier125798e2018-12-10 18:14:09 +00001664 __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001667 explicit __func(_Fp&& __f)
Eric Fiselier125798e2018-12-10 18:14:09 +00001668 : __f_(_VSTD::move(__f)) {}
1669
Howard Hinnant4ff57432010-09-21 22:55:27 +00001670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001671 explicit __func(const _Fp& __f, const _Alloc& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001672 : __f_(__f, __a) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001673
1674 _LIBCPP_INLINE_VISIBILITY
1675 explicit __func(const _Fp& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001676 : __f_(__f, _VSTD::move(__a)) {}
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001677
1678 _LIBCPP_INLINE_VISIBILITY
1679 explicit __func(_Fp&& __f, _Alloc&& __a)
Eric Fiselier125798e2018-12-10 18:14:09 +00001680 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
1681
Howard Hinnantc834c512011-11-29 18:15:50 +00001682 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1683 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001684 virtual void destroy() _NOEXCEPT;
1685 virtual void destroy_deallocate() _NOEXCEPT;
Eric Fiselier125798e2018-12-10 18:14:09 +00001686 virtual _Rp operator()(_ArgTypes&&... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001687#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001688 virtual const void* target(const type_info&) const _NOEXCEPT;
1689 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001690#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691};
1692
Howard Hinnantc834c512011-11-29 18:15:50 +00001693template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1694__base<_Rp(_ArgTypes...)>*
1695__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001697 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001698 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001699 _Ap __a(__f_.__get_allocator());
Howard Hinnantc834c512011-11-29 18:15:50 +00001700 typedef __allocator_destructor<_Ap> _Dp;
1701 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier125798e2018-12-10 18:14:09 +00001702 ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 return __hold.release();
1704}
1705
Howard Hinnantc834c512011-11-29 18:15:50 +00001706template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707void
Howard Hinnantc834c512011-11-29 18:15:50 +00001708__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709{
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05001710 ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711}
1712
Howard Hinnantc834c512011-11-29 18:15:50 +00001713template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714void
Howard Hinnantc834c512011-11-29 18:15:50 +00001715__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716{
Eric Fiselier125798e2018-12-10 18:14:09 +00001717 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718}
1719
Howard Hinnantc834c512011-11-29 18:15:50 +00001720template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721void
Howard Hinnantc834c512011-11-29 18:15:50 +00001722__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001724 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001725 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Thomas Andersonf88da8d2019-01-29 23:19:45 +00001726 _Ap __a(__f_.__get_allocator());
Eric Fiselier125798e2018-12-10 18:14:09 +00001727 __f_.destroy();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728 __a.deallocate(this, 1);
1729}
1730
Howard Hinnantc834c512011-11-29 18:15:50 +00001731template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1732_Rp
1733__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734{
Eric Fiselier125798e2018-12-10 18:14:09 +00001735 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736}
1737
Howard Hinnant72f73582010-08-11 17:04:31 +00001738#ifndef _LIBCPP_NO_RTTI
1739
Howard Hinnantc834c512011-11-29 18:15:50 +00001740template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001742__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743{
Howard Hinnantc834c512011-11-29 18:15:50 +00001744 if (__ti == typeid(_Fp))
Eric Fiselier125798e2018-12-10 18:14:09 +00001745 return &__f_.__target();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001746 return nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747}
1748
Howard Hinnantc834c512011-11-29 18:15:50 +00001749template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001751__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752{
Howard Hinnantc834c512011-11-29 18:15:50 +00001753 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754}
1755
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001756#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001757
Eric Fiselier125798e2018-12-10 18:14:09 +00001758// __value_func creates a value-type from a __func.
1759
1760template <class _Fp> class __value_func;
1761
1762template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
1763{
1764 typename aligned_storage<3 * sizeof(void*)>::type __buf_;
1765
1766 typedef __base<_Rp(_ArgTypes...)> __func;
1767 __func* __f_;
1768
1769 _LIBCPP_NO_CFI static __func* __as_base(void* p)
1770 {
1771 return reinterpret_cast<__func*>(p);
1772 }
1773
1774 public:
1775 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001776 __value_func() _NOEXCEPT : __f_(nullptr) {}
Eric Fiselier125798e2018-12-10 18:14:09 +00001777
1778 template <class _Fp, class _Alloc>
Eric Fiselier74ebee62019-06-08 01:31:19 +00001779 _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001780 : __f_(nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001781 {
1782 typedef allocator_traits<_Alloc> __alloc_traits;
1783 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
1784 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
1785 _FunAlloc;
1786
1787 if (__function::__not_null(__f))
1788 {
1789 _FunAlloc __af(__a);
1790 if (sizeof(_Fun) <= sizeof(__buf_) &&
1791 is_nothrow_copy_constructible<_Fp>::value &&
1792 is_nothrow_copy_constructible<_FunAlloc>::value)
1793 {
1794 __f_ =
1795 ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
1796 }
1797 else
1798 {
1799 typedef __allocator_destructor<_FunAlloc> _Dp;
1800 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
1801 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
1802 __f_ = __hold.release();
1803 }
1804 }
1805 }
1806
Eric Fiselier74ebee62019-06-08 01:31:19 +00001807 template <class _Fp,
1808 class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
1809 _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001810 : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
Eric Fiselier74ebee62019-06-08 01:31:19 +00001811
Eric Fiselier125798e2018-12-10 18:14:09 +00001812 _LIBCPP_INLINE_VISIBILITY
1813 __value_func(const __value_func& __f)
1814 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001815 if (__f.__f_ == nullptr)
1816 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001817 else if ((void*)__f.__f_ == &__f.__buf_)
1818 {
1819 __f_ = __as_base(&__buf_);
1820 __f.__f_->__clone(__f_);
1821 }
1822 else
1823 __f_ = __f.__f_->__clone();
1824 }
1825
1826 _LIBCPP_INLINE_VISIBILITY
1827 __value_func(__value_func&& __f) _NOEXCEPT
1828 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001829 if (__f.__f_ == nullptr)
1830 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001831 else if ((void*)__f.__f_ == &__f.__buf_)
1832 {
1833 __f_ = __as_base(&__buf_);
1834 __f.__f_->__clone(__f_);
1835 }
1836 else
1837 {
1838 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001839 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001840 }
1841 }
1842
1843 _LIBCPP_INLINE_VISIBILITY
1844 ~__value_func()
1845 {
1846 if ((void*)__f_ == &__buf_)
1847 __f_->destroy();
1848 else if (__f_)
1849 __f_->destroy_deallocate();
1850 }
1851
1852 _LIBCPP_INLINE_VISIBILITY
1853 __value_func& operator=(__value_func&& __f)
1854 {
1855 *this = nullptr;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001856 if (__f.__f_ == nullptr)
1857 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001858 else if ((void*)__f.__f_ == &__f.__buf_)
1859 {
1860 __f_ = __as_base(&__buf_);
1861 __f.__f_->__clone(__f_);
1862 }
1863 else
1864 {
1865 __f_ = __f.__f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001866 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001867 }
1868 return *this;
1869 }
1870
1871 _LIBCPP_INLINE_VISIBILITY
1872 __value_func& operator=(nullptr_t)
1873 {
1874 __func* __f = __f_;
Bruce Mitchener170d8972020-11-24 12:53:53 -05001875 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001876 if ((void*)__f == &__buf_)
1877 __f->destroy();
1878 else if (__f)
1879 __f->destroy_deallocate();
1880 return *this;
1881 }
1882
1883 _LIBCPP_INLINE_VISIBILITY
1884 _Rp operator()(_ArgTypes&&... __args) const
1885 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001886 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001887 __throw_bad_function_call();
1888 return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
1889 }
1890
1891 _LIBCPP_INLINE_VISIBILITY
1892 void swap(__value_func& __f) _NOEXCEPT
1893 {
1894 if (&__f == this)
1895 return;
1896 if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
1897 {
1898 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1899 __func* __t = __as_base(&__tempbuf);
1900 __f_->__clone(__t);
1901 __f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001902 __f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001903 __f.__f_->__clone(__as_base(&__buf_));
1904 __f.__f_->destroy();
Bruce Mitchener170d8972020-11-24 12:53:53 -05001905 __f.__f_ = nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001906 __f_ = __as_base(&__buf_);
1907 __t->__clone(__as_base(&__f.__buf_));
1908 __t->destroy();
1909 __f.__f_ = __as_base(&__f.__buf_);
1910 }
1911 else if ((void*)__f_ == &__buf_)
1912 {
1913 __f_->__clone(__as_base(&__f.__buf_));
1914 __f_->destroy();
1915 __f_ = __f.__f_;
1916 __f.__f_ = __as_base(&__f.__buf_);
1917 }
1918 else if ((void*)__f.__f_ == &__f.__buf_)
1919 {
1920 __f.__f_->__clone(__as_base(&__buf_));
1921 __f.__f_->destroy();
1922 __f.__f_ = __f_;
1923 __f_ = __as_base(&__buf_);
1924 }
1925 else
1926 _VSTD::swap(__f_, __f.__f_);
1927 }
1928
1929 _LIBCPP_INLINE_VISIBILITY
Bruce Mitchener170d8972020-11-24 12:53:53 -05001930 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
Eric Fiselier125798e2018-12-10 18:14:09 +00001931
1932#ifndef _LIBCPP_NO_RTTI
1933 _LIBCPP_INLINE_VISIBILITY
1934 const std::type_info& target_type() const _NOEXCEPT
1935 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001936 if (__f_ == nullptr)
Eric Fiselier125798e2018-12-10 18:14:09 +00001937 return typeid(void);
1938 return __f_->target_type();
1939 }
1940
1941 template <typename _Tp>
1942 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
1943 {
Bruce Mitchener170d8972020-11-24 12:53:53 -05001944 if (__f_ == nullptr)
1945 return nullptr;
Eric Fiselier125798e2018-12-10 18:14:09 +00001946 return (const _Tp*)__f_->target(typeid(_Tp));
1947 }
1948#endif // _LIBCPP_NO_RTTI
1949};
1950
Eric Fiselierf2e64362018-12-11 00:14:34 +00001951// Storage for a functor object, to be used with __policy to manage copy and
1952// destruction.
1953union __policy_storage
1954{
1955 mutable char __small[sizeof(void*) * 2];
1956 void* __large;
1957};
1958
1959// True if _Fun can safely be held in __policy_storage.__small.
1960template <typename _Fun>
1961struct __use_small_storage
1962 : public _VSTD::integral_constant<
1963 bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
Eric Fiselier0f0ed9d2019-01-16 01:51:12 +00001964 _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
Eric Fiselierf2e64362018-12-11 00:14:34 +00001965 _VSTD::is_trivially_copy_constructible<_Fun>::value &&
1966 _VSTD::is_trivially_destructible<_Fun>::value> {};
1967
1968// Policy contains information about how to copy, destroy, and move the
1969// underlying functor. You can think of it as a vtable of sorts.
1970struct __policy
1971{
1972 // Used to copy or destroy __large values. null for trivial objects.
1973 void* (*const __clone)(const void*);
1974 void (*const __destroy)(void*);
1975
1976 // True if this is the null policy (no value).
1977 const bool __is_null;
1978
1979 // The target type. May be null if RTTI is disabled.
1980 const std::type_info* const __type_info;
1981
1982 // Returns a pointer to a static policy object suitable for the functor
1983 // type.
1984 template <typename _Fun>
1985 _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
1986 {
1987 return __choose_policy<_Fun>(__use_small_storage<_Fun>());
1988 }
1989
1990 _LIBCPP_INLINE_VISIBILITY
1991 static const __policy* __create_empty()
1992 {
1993 static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
1994 true,
1995#ifndef _LIBCPP_NO_RTTI
1996 &typeid(void)
1997#else
1998 nullptr
1999#endif
2000 };
2001 return &__policy_;
2002 }
2003
2004 private:
2005 template <typename _Fun> static void* __large_clone(const void* __s)
2006 {
2007 const _Fun* __f = static_cast<const _Fun*>(__s);
2008 return __f->__clone();
2009 }
2010
Eric Fiselier74ebee62019-06-08 01:31:19 +00002011 template <typename _Fun>
2012 static void __large_destroy(void* __s) {
2013 _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
Eric Fiselierf2e64362018-12-11 00:14:34 +00002014 }
2015
2016 template <typename _Fun>
2017 _LIBCPP_INLINE_VISIBILITY static const __policy*
Eric Fiselier74ebee62019-06-08 01:31:19 +00002018 __choose_policy(/* is_small = */ false_type) {
2019 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2020 &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
Eric Fiselierf2e64362018-12-11 00:14:34 +00002021#ifndef _LIBCPP_NO_RTTI
Eric Fiselier74ebee62019-06-08 01:31:19 +00002022 &typeid(typename _Fun::_Target)
Eric Fiselierf2e64362018-12-11 00:14:34 +00002023#else
Eric Fiselier74ebee62019-06-08 01:31:19 +00002024 nullptr
Eric Fiselierf2e64362018-12-11 00:14:34 +00002025#endif
Eric Fiselier74ebee62019-06-08 01:31:19 +00002026 };
Eric Fiselierf2e64362018-12-11 00:14:34 +00002027 return &__policy_;
2028 }
2029
2030 template <typename _Fun>
2031 _LIBCPP_INLINE_VISIBILITY static const __policy*
2032 __choose_policy(/* is_small = */ true_type)
2033 {
2034 static const _LIBCPP_CONSTEXPR __policy __policy_ = {
2035 nullptr, nullptr, false,
2036#ifndef _LIBCPP_NO_RTTI
2037 &typeid(typename _Fun::_Target)
2038#else
2039 nullptr
2040#endif
2041 };
2042 return &__policy_;
2043 }
2044};
2045
2046// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
2047// faster for types that can be passed in registers.
2048template <typename _Tp>
2049using __fast_forward =
2050 typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
2051
2052// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
2053
2054template <class _Fp> struct __policy_invoker;
2055
2056template <class _Rp, class... _ArgTypes>
2057struct __policy_invoker<_Rp(_ArgTypes...)>
2058{
2059 typedef _Rp (*__Call)(const __policy_storage*,
2060 __fast_forward<_ArgTypes>...);
2061
2062 __Call __call_;
2063
2064 // Creates an invoker that throws bad_function_call.
2065 _LIBCPP_INLINE_VISIBILITY
2066 __policy_invoker() : __call_(&__call_empty) {}
2067
2068 // Creates an invoker that calls the given instance of __func.
2069 template <typename _Fun>
2070 _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
2071 {
2072 return __policy_invoker(&__call_impl<_Fun>);
2073 }
2074
2075 private:
2076 _LIBCPP_INLINE_VISIBILITY
2077 explicit __policy_invoker(__Call __c) : __call_(__c) {}
2078
2079 static _Rp __call_empty(const __policy_storage*,
2080 __fast_forward<_ArgTypes>...)
2081 {
2082 __throw_bad_function_call();
2083 }
2084
2085 template <typename _Fun>
2086 static _Rp __call_impl(const __policy_storage* __buf,
2087 __fast_forward<_ArgTypes>... __args)
2088 {
2089 _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
2090 ? &__buf->__small
2091 : __buf->__large);
2092 return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
2093 }
2094};
2095
2096// __policy_func uses a __policy and __policy_invoker to create a type-erased,
2097// copyable functor.
2098
2099template <class _Fp> class __policy_func;
2100
2101template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
2102{
2103 // Inline storage for small objects.
2104 __policy_storage __buf_;
2105
2106 // Calls the value stored in __buf_. This could technically be part of
2107 // policy, but storing it here eliminates a level of indirection inside
2108 // operator().
2109 typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
2110 __invoker __invoker_;
2111
2112 // The policy that describes how to move / copy / destroy __buf_. Never
2113 // null, even if the function is empty.
2114 const __policy* __policy_;
2115
2116 public:
2117 _LIBCPP_INLINE_VISIBILITY
2118 __policy_func() : __policy_(__policy::__create_empty()) {}
2119
2120 template <class _Fp, class _Alloc>
2121 _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
2122 : __policy_(__policy::__create_empty())
2123 {
2124 typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
2125 typedef allocator_traits<_Alloc> __alloc_traits;
2126 typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
2127 _FunAlloc;
2128
2129 if (__function::__not_null(__f))
2130 {
2131 __invoker_ = __invoker::template __create<_Fun>();
2132 __policy_ = __policy::__create<_Fun>();
2133
2134 _FunAlloc __af(__a);
2135 if (__use_small_storage<_Fun>())
2136 {
2137 ::new ((void*)&__buf_.__small)
2138 _Fun(_VSTD::move(__f), _Alloc(__af));
2139 }
2140 else
2141 {
2142 typedef __allocator_destructor<_FunAlloc> _Dp;
2143 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
2144 ::new ((void*)__hold.get())
2145 _Fun(_VSTD::move(__f), _Alloc(__af));
2146 __buf_.__large = __hold.release();
2147 }
2148 }
2149 }
2150
Eric Fiselier74ebee62019-06-08 01:31:19 +00002151 template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
2152 _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
2153 : __policy_(__policy::__create_empty()) {
2154 typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
2155
2156 if (__function::__not_null(__f)) {
2157 __invoker_ = __invoker::template __create<_Fun>();
2158 __policy_ = __policy::__create<_Fun>();
2159 if (__use_small_storage<_Fun>()) {
2160 ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
2161 } else {
2162 __builtin_new_allocator::__holder_t __hold =
2163 __builtin_new_allocator::__allocate_type<_Fun>(1);
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002164 __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
Eric Fiselier74ebee62019-06-08 01:31:19 +00002165 (void)__hold.release();
2166 }
2167 }
2168 }
2169
Eric Fiselierf2e64362018-12-11 00:14:34 +00002170 _LIBCPP_INLINE_VISIBILITY
2171 __policy_func(const __policy_func& __f)
2172 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2173 __policy_(__f.__policy_)
2174 {
2175 if (__policy_->__clone)
2176 __buf_.__large = __policy_->__clone(__f.__buf_.__large);
2177 }
2178
2179 _LIBCPP_INLINE_VISIBILITY
2180 __policy_func(__policy_func&& __f)
2181 : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
2182 __policy_(__f.__policy_)
2183 {
2184 if (__policy_->__destroy)
2185 {
2186 __f.__policy_ = __policy::__create_empty();
2187 __f.__invoker_ = __invoker();
2188 }
2189 }
2190
2191 _LIBCPP_INLINE_VISIBILITY
2192 ~__policy_func()
2193 {
2194 if (__policy_->__destroy)
2195 __policy_->__destroy(__buf_.__large);
2196 }
2197
2198 _LIBCPP_INLINE_VISIBILITY
2199 __policy_func& operator=(__policy_func&& __f)
2200 {
2201 *this = nullptr;
2202 __buf_ = __f.__buf_;
2203 __invoker_ = __f.__invoker_;
2204 __policy_ = __f.__policy_;
2205 __f.__policy_ = __policy::__create_empty();
2206 __f.__invoker_ = __invoker();
2207 return *this;
2208 }
2209
2210 _LIBCPP_INLINE_VISIBILITY
2211 __policy_func& operator=(nullptr_t)
2212 {
2213 const __policy* __p = __policy_;
2214 __policy_ = __policy::__create_empty();
2215 __invoker_ = __invoker();
2216 if (__p->__destroy)
2217 __p->__destroy(__buf_.__large);
2218 return *this;
2219 }
2220
2221 _LIBCPP_INLINE_VISIBILITY
2222 _Rp operator()(_ArgTypes&&... __args) const
2223 {
2224 return __invoker_.__call_(_VSTD::addressof(__buf_),
2225 _VSTD::forward<_ArgTypes>(__args)...);
2226 }
2227
2228 _LIBCPP_INLINE_VISIBILITY
2229 void swap(__policy_func& __f)
2230 {
2231 _VSTD::swap(__invoker_, __f.__invoker_);
2232 _VSTD::swap(__policy_, __f.__policy_);
2233 _VSTD::swap(__buf_, __f.__buf_);
2234 }
2235
2236 _LIBCPP_INLINE_VISIBILITY
2237 explicit operator bool() const _NOEXCEPT
2238 {
2239 return !__policy_->__is_null;
2240 }
2241
2242#ifndef _LIBCPP_NO_RTTI
2243 _LIBCPP_INLINE_VISIBILITY
2244 const std::type_info& target_type() const _NOEXCEPT
2245 {
2246 return *__policy_->__type_info;
2247 }
2248
2249 template <typename _Tp>
2250 _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
2251 {
2252 if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
2253 return nullptr;
2254 if (__policy_->__clone) // Out of line storage.
2255 return reinterpret_cast<const _Tp*>(__buf_.__large);
2256 else
2257 return reinterpret_cast<const _Tp*>(&__buf_.__small);
2258 }
2259#endif // _LIBCPP_NO_RTTI
2260};
2261
Louis Dionne3a632922020-04-23 16:47:52 -04002262#if defined(_LIBCPP_HAS_BLOCKS_RUNTIME) && !defined(_LIBCPP_HAS_OBJC_ARC)
Louis Dionnee2391d72020-04-22 13:58:17 -04002263
Louis Dionne91cf4442020-07-31 12:56:36 -04002264extern "C" void *_Block_copy(const void *);
2265extern "C" void _Block_release(const void *);
2266
Louis Dionnee2391d72020-04-22 13:58:17 -04002267template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
2268class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
2269 : public __base<_Rp(_ArgTypes...)>
2270{
2271 typedef _Rp1(^__block_type)(_ArgTypes1...);
2272 __block_type __f_;
2273
2274public:
2275 _LIBCPP_INLINE_VISIBILITY
2276 explicit __func(__block_type const& __f)
Louis Dionne91cf4442020-07-31 12:56:36 -04002277 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002278 { }
2279
2280 // [TODO] add && to save on a retain
2281
2282 _LIBCPP_INLINE_VISIBILITY
2283 explicit __func(__block_type __f, const _Alloc& /* unused */)
Louis Dionne91cf4442020-07-31 12:56:36 -04002284 : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
Louis Dionnee2391d72020-04-22 13:58:17 -04002285 { }
2286
2287 virtual __base<_Rp(_ArgTypes...)>* __clone() const {
2288 _LIBCPP_ASSERT(false,
2289 "Block pointers are just pointers, so they should always fit into "
2290 "std::function's small buffer optimization. This function should "
2291 "never be invoked.");
2292 return nullptr;
2293 }
2294
2295 virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -05002296 ::new ((void*)__p) __func(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002297 }
2298
2299 virtual void destroy() _NOEXCEPT {
2300 if (__f_)
Louis Dionne91cf4442020-07-31 12:56:36 -04002301 _Block_release(__f_);
Louis Dionnee2391d72020-04-22 13:58:17 -04002302 __f_ = 0;
2303 }
2304
2305 virtual void destroy_deallocate() _NOEXCEPT {
2306 _LIBCPP_ASSERT(false,
2307 "Block pointers are just pointers, so they should always fit into "
2308 "std::function's small buffer optimization. This function should "
2309 "never be invoked.");
2310 }
2311
2312 virtual _Rp operator()(_ArgTypes&& ... __arg) {
Arthur O'Dwyer6814ff72020-12-08 16:15:01 -05002313 return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
Louis Dionnee2391d72020-04-22 13:58:17 -04002314 }
2315
2316#ifndef _LIBCPP_NO_RTTI
2317 virtual const void* target(type_info const& __ti) const _NOEXCEPT {
2318 if (__ti == typeid(__func::__block_type))
2319 return &__f_;
2320 return (const void*)nullptr;
2321 }
2322
2323 virtual const std::type_info& target_type() const _NOEXCEPT {
2324 return typeid(__func::__block_type);
2325 }
2326#endif // _LIBCPP_NO_RTTI
2327};
2328
2329#endif // _LIBCPP_HAS_EXTENSION_BLOCKS && !_LIBCPP_HAS_OBJC_ARC
2330
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331} // __function
2332
Howard Hinnantc834c512011-11-29 18:15:50 +00002333template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002334class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00002335 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
2336 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337{
Eric Fiselierf2e64362018-12-11 00:14:34 +00002338#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
Eric Fiselier125798e2018-12-10 18:14:09 +00002339 typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
Eric Fiselierf2e64362018-12-11 00:14:34 +00002340#else
2341 typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
2342#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343
Eric Fiselier125798e2018-12-10 18:14:09 +00002344 __func __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00002345
Eric Fiselier3906a132019-06-23 20:28:29 +00002346 template <class _Fp, bool = _And<
2347 _IsNotSame<__uncvref_t<_Fp>, function>,
zoecarver3e722f22020-05-19 17:15:28 -07002348 __invokable<_Fp, _ArgTypes...>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002349 >::value>
2350 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00002351 template <class _Fp>
2352 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00002353 {
Arthur O'Dwyer4ba8e3d2021-01-11 16:29:17 -05002354 static const bool value = is_void<_Rp>::value ||
2355 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
2356 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00002357 };
Howard Hinnantc834c512011-11-29 18:15:50 +00002358 template <class _Fp>
2359 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00002360 {
2361 static const bool value = false;
2362 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00002363
2364 template <class _Fp>
zoecarver3e722f22020-05-19 17:15:28 -07002365 using _EnableIfLValueCallable = typename enable_if<__callable<_Fp&>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002367 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368
Howard Hinnantf06d9262010-08-20 19:36:46 +00002369 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002370 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002371 function() _NOEXCEPT { }
Howard Hinnant4ff57432010-09-21 22:55:27 +00002372 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002373 function(nullptr_t) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002375 function(function&&) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002376 template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002377 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378
Marshall Clow3148f422016-10-13 21:06:03 +00002379#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00002380 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002381 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002382 function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002383 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002384 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002385 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00002386 template<class _Alloc>
2387 function(allocator_arg_t, const _Alloc&, const function&);
2388 template<class _Alloc>
2389 function(allocator_arg_t, const _Alloc&, function&&);
zoecarver3e722f22020-05-19 17:15:28 -07002390 template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002391 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00002392#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393
2394 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002395 function& operator=(function&&) _NOEXCEPT;
2396 function& operator=(nullptr_t) _NOEXCEPT;
zoecarver3e722f22020-05-19 17:15:28 -07002397 template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002398 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399
2400 ~function();
2401
Howard Hinnantf06d9262010-08-20 19:36:46 +00002402 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002403 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00002404
2405#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002406 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002408 void assign(_Fp&& __f, const _Alloc& __a)
2409 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00002410#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002411
Howard Hinnantf06d9262010-08-20 19:36:46 +00002412 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00002413 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier125798e2018-12-10 18:14:09 +00002414 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2415 return static_cast<bool>(__f_);
2416 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 // deleted overloads close possible hole in the type system
2419 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002420 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00002422 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00002424 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00002425 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426
Howard Hinnant72f73582010-08-11 17:04:31 +00002427#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00002428 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00002429 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00002430 template <typename _Tp> _Tp* target() _NOEXCEPT;
2431 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002432#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433};
2434
Louis Dionne4af49712019-07-18 19:50:56 +00002435#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2436template<class _Rp, class ..._Ap>
2437function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2438
2439template<class _Fp>
2440struct __strip_signature;
2441
2442template<class _Rp, class _Gp, class ..._Ap>
2443struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2444template<class _Rp, class _Gp, class ..._Ap>
2445struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2446template<class _Rp, class _Gp, class ..._Ap>
2447struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2448template<class _Rp, class _Gp, class ..._Ap>
2449struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2450
2451template<class _Rp, class _Gp, class ..._Ap>
2452struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2453template<class _Rp, class _Gp, class ..._Ap>
2454struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2455template<class _Rp, class _Gp, class ..._Ap>
2456struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2457template<class _Rp, class _Gp, class ..._Ap>
2458struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2459
2460template<class _Rp, class _Gp, class ..._Ap>
2461struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2462template<class _Rp, class _Gp, class ..._Ap>
2463struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2464template<class _Rp, class _Gp, class ..._Ap>
2465struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2466template<class _Rp, class _Gp, class ..._Ap>
2467struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2468
2469template<class _Rp, class _Gp, class ..._Ap>
2470struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2471template<class _Rp, class _Gp, class ..._Ap>
2472struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2473template<class _Rp, class _Gp, class ..._Ap>
2474struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2475template<class _Rp, class _Gp, class ..._Ap>
2476struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2477
2478template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2479function(_Fp) -> function<_Stripped>;
2480#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2481
Howard Hinnantc834c512011-11-29 18:15:50 +00002482template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002483function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484
Marshall Clow3148f422016-10-13 21:06:03 +00002485#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002486template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002487template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002488function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002489 const function& __f) : __f_(__f.__f_) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002490#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002491
Eric Fiselier125798e2018-12-10 18:14:09 +00002492template <class _Rp, class... _ArgTypes>
Howard Hinnantc834c512011-11-29 18:15:50 +00002493function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Eric Fiselier125798e2018-12-10 18:14:09 +00002494 : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495
Marshall Clow3148f422016-10-13 21:06:03 +00002496#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00002497template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00002498template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00002499function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Eric Fiselier125798e2018-12-10 18:14:09 +00002500 function&& __f)
2501 : __f_(_VSTD::move(__f.__f_)) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002502#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002503
Eric Fiselier125798e2018-12-10 18:14:09 +00002504template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002505template <class _Fp, class>
Eric Fiselier74ebee62019-06-08 01:31:19 +00002506function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507
Marshall Clow3148f422016-10-13 21:06:03 +00002508#if _LIBCPP_STD_VER <= 14
Eric Fiselier125798e2018-12-10 18:14:09 +00002509template <class _Rp, class... _ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00002510template <class _Fp, class _Alloc, class>
Eric Fiselier125798e2018-12-10 18:14:09 +00002511function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
2512 _Fp __f)
2513 : __f_(_VSTD::move(__f), __a) {}
Marshall Clow3148f422016-10-13 21:06:03 +00002514#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00002515
Howard Hinnantc834c512011-11-29 18:15:50 +00002516template<class _Rp, class ..._ArgTypes>
2517function<_Rp(_ArgTypes...)>&
2518function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519{
2520 function(__f).swap(*this);
2521 return *this;
2522}
2523
Howard Hinnantc834c512011-11-29 18:15:50 +00002524template<class _Rp, class ..._ArgTypes>
2525function<_Rp(_ArgTypes...)>&
2526function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527{
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002528 __f_ = _VSTD::move(__f.__f_);
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002529 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530}
2531
Howard Hinnantc834c512011-11-29 18:15:50 +00002532template<class _Rp, class ..._ArgTypes>
2533function<_Rp(_ArgTypes...)>&
2534function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535{
Eric Fiselier125798e2018-12-10 18:14:09 +00002536 __f_ = nullptr;
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002537 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538}
2539
Howard Hinnantc834c512011-11-29 18:15:50 +00002540template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00002541template <class _Fp, class>
2542function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00002543function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544{
Howard Hinnantc834c512011-11-29 18:15:50 +00002545 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546 return *this;
2547}
2548
Howard Hinnantc834c512011-11-29 18:15:50 +00002549template<class _Rp, class ..._ArgTypes>
Eric Fiselier125798e2018-12-10 18:14:09 +00002550function<_Rp(_ArgTypes...)>::~function() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551
Howard Hinnantc834c512011-11-29 18:15:50 +00002552template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553void
Howard Hinnantc834c512011-11-29 18:15:50 +00002554function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555{
Eric Fiselier125798e2018-12-10 18:14:09 +00002556 __f_.swap(__f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557}
2558
Howard Hinnantc834c512011-11-29 18:15:50 +00002559template<class _Rp, class ..._ArgTypes>
2560_Rp
2561function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562{
Eric Fiselier125798e2018-12-10 18:14:09 +00002563 return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564}
2565
Howard Hinnant72f73582010-08-11 17:04:31 +00002566#ifndef _LIBCPP_NO_RTTI
2567
Howard Hinnantc834c512011-11-29 18:15:50 +00002568template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00002570function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571{
Eric Fiselier125798e2018-12-10 18:14:09 +00002572 return __f_.target_type();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573}
2574
Howard Hinnantc834c512011-11-29 18:15:50 +00002575template<class _Rp, class ..._ArgTypes>
2576template <typename _Tp>
2577_Tp*
2578function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579{
Eric Fiselier125798e2018-12-10 18:14:09 +00002580 return (_Tp*)(__f_.template target<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581}
2582
Howard Hinnantc834c512011-11-29 18:15:50 +00002583template<class _Rp, class ..._ArgTypes>
2584template <typename _Tp>
2585const _Tp*
2586function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587{
Eric Fiselier125798e2018-12-10 18:14:09 +00002588 return __f_.template target<_Tp>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002589}
2590
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002591#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00002592
Howard Hinnantc834c512011-11-29 18:15:50 +00002593template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594inline _LIBCPP_INLINE_VISIBILITY
2595bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002596operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597
Howard Hinnantc834c512011-11-29 18:15:50 +00002598template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599inline _LIBCPP_INLINE_VISIBILITY
2600bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002601operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602
Howard Hinnantc834c512011-11-29 18:15:50 +00002603template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604inline _LIBCPP_INLINE_VISIBILITY
2605bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002606operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607
Howard Hinnantc834c512011-11-29 18:15:50 +00002608template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609inline _LIBCPP_INLINE_VISIBILITY
2610bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002611operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612
Howard Hinnantc834c512011-11-29 18:15:50 +00002613template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614inline _LIBCPP_INLINE_VISIBILITY
2615void
Howard Hinnantc834c512011-11-29 18:15:50 +00002616swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617{return __x.swap(__y);}
2618
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002619#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00002620
2621#include <__functional_03>
2622
2623#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002624
2625////////////////////////////////////////////////////////////////////////////////
2626// BIND
2627//==============================================================================
2628
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002630template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
2632
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002633#if _LIBCPP_STD_VER > 14
2634template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002635_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002636#endif
2637
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002639template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2641
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002642#if _LIBCPP_STD_VER > 14
2643template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002644_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002645#endif
2646
Howard Hinnantc51e1022010-05-11 19:42:16 +00002647namespace placeholders
2648{
2649
Howard Hinnantc834c512011-11-29 18:15:50 +00002650template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002652#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002653_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2654_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2655_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2656_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2657_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2658_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2659_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2660_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2661_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2662_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2663#else
Marshall Clow396b2132018-01-02 19:01:45 +00002664/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2665/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2666/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2667/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2668/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2669/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2670/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2671/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2672/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2673/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002674#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675
2676} // placeholders
2677
Howard Hinnantc834c512011-11-29 18:15:50 +00002678template<int _Np>
2679struct __is_placeholder<placeholders::__ph<_Np> >
2680 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002682
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002683#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002684
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685template <class _Tp, class _Uj>
2686inline _LIBCPP_INLINE_VISIBILITY
2687_Tp&
2688__mu(reference_wrapper<_Tp> __t, _Uj&)
2689{
2690 return __t.get();
2691}
2692
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693template <class _Ti, class ..._Uj, size_t ..._Indx>
2694inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002695typename __invoke_of<_Ti&, _Uj...>::type
2696__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002698 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699}
2700
2701template <class _Ti, class ..._Uj>
2702inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00002703typename _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704<
2705 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002706 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707>::type
2708__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2709{
2710 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
Arthur O'Dwyer34465da2020-12-15 19:32:29 -05002711 return _VSTD::__mu_expand(__ti, __uj, __indices());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712}
2713
2714template <bool IsPh, class _Ti, class _Uj>
2715struct __mu_return2 {};
2716
2717template <class _Ti, class _Uj>
2718struct __mu_return2<true, _Ti, _Uj>
2719{
2720 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2721};
2722
2723template <class _Ti, class _Uj>
2724inline _LIBCPP_INLINE_VISIBILITY
2725typename enable_if
2726<
2727 0 < is_placeholder<_Ti>::value,
2728 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2729>::type
2730__mu(_Ti&, _Uj& __uj)
2731{
2732 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002733 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734}
2735
2736template <class _Ti, class _Uj>
2737inline _LIBCPP_INLINE_VISIBILITY
2738typename enable_if
2739<
2740 !is_bind_expression<_Ti>::value &&
2741 is_placeholder<_Ti>::value == 0 &&
2742 !__is_reference_wrapper<_Ti>::value,
2743 _Ti&
2744>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002745__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746{
2747 return __ti;
2748}
2749
Howard Hinnant0415d792011-05-22 15:07:43 +00002750template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2751 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002752struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002754template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002755struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002756{
2757 typedef __nat type;
2758};
2759
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002761struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002763 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764};
2765
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002766template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002767struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2768 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002769{
2770};
2771
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002773struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774{
2775 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2776 _TupleUj>::type&& type;
2777};
2778
2779template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002780struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002781{
2782 typedef typename _Ti::type& type;
2783};
2784
2785template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002786struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787{
2788 typedef _Ti& type;
2789};
2790
2791template <class _Ti, class _TupleUj>
2792struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002793 : public __mu_return_impl<_Ti,
2794 __is_reference_wrapper<_Ti>::value,
2795 is_bind_expression<_Ti>::value,
2796 0 < is_placeholder<_Ti>::value &&
2797 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2798 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799{
2800};
2801
Howard Hinnantc834c512011-11-29 18:15:50 +00002802template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002803struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002804{
2805 static const bool value = false;
2806};
2807
2808template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002809struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002810{
2811 static const bool value = __invokable<_Fp,
2812 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2813};
2814
2815template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002816struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002817{
2818 static const bool value = __invokable<_Fp,
2819 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2820};
2821
2822template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002823 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824struct __bind_return;
2825
Howard Hinnantc834c512011-11-29 18:15:50 +00002826template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002827struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002829 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002831 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 typename __mu_return
2833 <
2834 _BoundArgs,
2835 _TupleUj
2836 >::type...
2837 >::type type;
2838};
2839
Howard Hinnantc834c512011-11-29 18:15:50 +00002840template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002841struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002843 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002845 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 typename __mu_return
2847 <
2848 const _BoundArgs,
2849 _TupleUj
2850 >::type...
2851 >::type type;
2852};
2853
Howard Hinnantc834c512011-11-29 18:15:50 +00002854template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002856typename __bind_return<_Fp, _BoundArgs, _Args>::type
2857__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 _Args&& __args)
2859{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002860 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002861}
2862
Howard Hinnantc834c512011-11-29 18:15:50 +00002863template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002865 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002867protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002868 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002869 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002870private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002871 _Fd __f_;
2872 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873
2874 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2875public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002876 template <class _Gp, class ..._BA,
2877 class = typename enable_if
2878 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002879 is_constructible<_Fd, _Gp>::value &&
2880 !is_same<typename remove_reference<_Gp>::type,
2881 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002882 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002883 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002884 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2885 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002886 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887
2888 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002889 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002890 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891 operator()(_Args&& ...__args)
2892 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002893 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002894 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002895 }
2896
2897 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002898 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002899 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900 operator()(_Args&& ...__args) const
2901 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002902 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002903 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 }
2905};
2906
Howard Hinnantc834c512011-11-29 18:15:50 +00002907template<class _Fp, class ..._BoundArgs>
2908struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909
Howard Hinnantc834c512011-11-29 18:15:50 +00002910template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002912 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913{
Howard Hinnantc834c512011-11-29 18:15:50 +00002914 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002915 typedef typename base::_Fd _Fd;
2916 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002918 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919
Howard Hinnant7091e652011-07-02 18:22:36 +00002920
Howard Hinnantf292a922013-07-01 00:01:51 +00002921 template <class _Gp, class ..._BA,
2922 class = typename enable_if
2923 <
2924 is_constructible<_Fd, _Gp>::value &&
2925 !is_same<typename remove_reference<_Gp>::type,
2926 __bind_r>::value
2927 >::type>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002928 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002929 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2930 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002931 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932
2933 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002934 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002935 typename enable_if
2936 <
2937 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002938 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002939 result_type
2940 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 operator()(_Args&& ...__args)
2942 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002943 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2944 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 }
2946
2947 template <class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002948 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002949 typename enable_if
2950 <
2951 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002952 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002953 result_type
2954 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955 operator()(_Args&& ...__args) const
2956 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002957 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2958 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002959 }
2960};
2961
Howard Hinnantc834c512011-11-29 18:15:50 +00002962template<class _Rp, class _Fp, class ..._BoundArgs>
2963struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964
Howard Hinnantc834c512011-11-29 18:15:50 +00002965template<class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002966inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002967__bind<_Fp, _BoundArgs...>
2968bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969{
Howard Hinnantc834c512011-11-29 18:15:50 +00002970 typedef __bind<_Fp, _BoundArgs...> type;
2971 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972}
2973
Howard Hinnantc834c512011-11-29 18:15:50 +00002974template<class _Rp, class _Fp, class ..._BoundArgs>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002975inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc834c512011-11-29 18:15:50 +00002976__bind_r<_Rp, _Fp, _BoundArgs...>
2977bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978{
Howard Hinnantc834c512011-11-29 18:15:50 +00002979 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2980 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981}
2982
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002983#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984
Eric Fiselier0d974f12015-07-14 20:16:15 +00002985#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002986
Eric Fiselier0d974f12015-07-14 20:16:15 +00002987template <class _Fn, class ..._Args>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05002988_LIBCPP_CONSTEXPR_AFTER_CXX17 invoke_result_t<_Fn, _Args...>
Eric Fiselier934f63b2016-06-02 01:25:41 +00002989invoke(_Fn&& __f, _Args&&... __args)
Louis Dionneaf34f122019-04-03 17:54:37 +00002990 noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
Eric Fiselier934f63b2016-06-02 01:25:41 +00002991{
2992 return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
Eric Fiselier0d974f12015-07-14 20:16:15 +00002993}
Eric Fiselier934f63b2016-06-02 01:25:41 +00002994
zoecarver3595cac2021-03-02 16:17:22 -08002995template<class _Op, class _Tuple,
2996 class _Idxs = typename __make_tuple_indices<tuple_size<_Tuple>::value>::type>
2997struct __perfect_forward_impl;
Eric Fiselier934f63b2016-06-02 01:25:41 +00002998
zoecarver3595cac2021-03-02 16:17:22 -08002999template<class _Op, class... _Bound, size_t... _Idxs>
3000struct __perfect_forward_impl<_Op, __tuple_types<_Bound...>, __tuple_indices<_Idxs...>>
3001{
3002 tuple<_Bound...> __bound_;
Eric Fiselier934f63b2016-06-02 01:25:41 +00003003
zoecarver3595cac2021-03-02 16:17:22 -08003004 template<class... _Args>
3005 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &
3006 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3007 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3008 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003009
zoecarver3595cac2021-03-02 16:17:22 -08003010 template<class... _Args>
3011 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&
3012 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...)))
3013 -> decltype( _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...))
3014 {return _Op::__call(_VSTD::get<_Idxs>(__bound_)..., _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003015
zoecarver3595cac2021-03-02 16:17:22 -08003016 template<class... _Args>
3017 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) &&
3018 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3019 _VSTD::forward<_Args>(__args)...)))
3020 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3021 _VSTD::forward<_Args>(__args)...))
3022 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3023 _VSTD::forward<_Args>(__args)...);}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003024
zoecarver3595cac2021-03-02 16:17:22 -08003025 template<class... _Args>
3026 _LIBCPP_INLINE_VISIBILITY constexpr auto operator()(_Args&&... __args) const&&
3027 noexcept(noexcept(_Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3028 _VSTD::forward<_Args>(__args)...)))
3029 -> decltype( _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3030 _VSTD::forward<_Args>(__args)...))
3031 {return _Op::__call(_VSTD::get<_Idxs>(_VSTD::move(__bound_))...,
3032 _VSTD::forward<_Args>(__args)...);}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003033
zoecarver3595cac2021-03-02 16:17:22 -08003034 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3035 class = _EnableIf<is_copy_constructible_v<_Fn>>>
3036 constexpr __perfect_forward_impl(__perfect_forward_impl const& __other)
3037 : __bound_(__other.__bound_) {}
Eric Fiseliere8303a32016-06-27 00:40:41 +00003038
zoecarver3595cac2021-03-02 16:17:22 -08003039 template<class _Fn = typename tuple_element<0, tuple<_Bound...>>::type,
3040 class = _EnableIf<is_move_constructible_v<_Fn>>>
3041 constexpr __perfect_forward_impl(__perfect_forward_impl && __other)
3042 : __bound_(_VSTD::move(__other.__bound_)) {}
Eric Fiselier934f63b2016-06-02 01:25:41 +00003043
zoecarver3595cac2021-03-02 16:17:22 -08003044 template<class... _BoundArgs>
3045 explicit constexpr __perfect_forward_impl(_BoundArgs&&... __bound) :
3046 __bound_(_VSTD::forward<_BoundArgs>(__bound)...) { }
Eric Fiselier934f63b2016-06-02 01:25:41 +00003047};
3048
zoecarver3595cac2021-03-02 16:17:22 -08003049template<class _Op, class... _Args>
3050using __perfect_forward =
3051 __perfect_forward_impl<_Op, __tuple_types<decay_t<_Args>...>>;
3052
3053struct __not_fn_op
3054{
3055 template<class... _Args>
3056 static _LIBCPP_CONSTEXPR_AFTER_CXX17 auto __call(_Args&&... __args)
3057 noexcept(noexcept(!_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3058 -> decltype( !_VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3059 { return !_VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3060};
3061
3062template<class _Fn,
3063 class = _EnableIf<is_constructible_v<decay_t<_Fn>, _Fn> &&
3064 is_move_constructible_v<_Fn>>>
3065_LIBCPP_CONSTEXPR_AFTER_CXX17 auto not_fn(_Fn&& __f)
3066{
3067 return __perfect_forward<__not_fn_op, _Fn>(_VSTD::forward<_Fn>(__f));
Eric Fiselier934f63b2016-06-02 01:25:41 +00003068}
3069
zoecarver3595cac2021-03-02 16:17:22 -08003070#endif // _LIBCPP_STD_VER > 14
3071
3072#if _LIBCPP_STD_VER > 17
3073
3074struct __bind_front_op
3075{
3076 template<class... _Args>
3077 constexpr static auto __call(_Args&&... __args)
3078 noexcept(noexcept(_VSTD::invoke(_VSTD::forward<_Args>(__args)...)))
3079 -> decltype( _VSTD::invoke(_VSTD::forward<_Args>(__args)...))
3080 { return _VSTD::invoke(_VSTD::forward<_Args>(__args)...); }
3081};
3082
3083template<class _Fn, class... _Args,
3084 class = _EnableIf<conjunction<is_constructible<decay_t<_Fn>, _Fn>,
3085 is_move_constructible<decay_t<_Fn>>,
3086 is_constructible<decay_t<_Args>, _Args>...,
3087 is_move_constructible<decay_t<_Args>>...
3088 >::value>>
3089constexpr auto bind_front(_Fn&& __f, _Args&&... __args)
3090{
3091 return __perfect_forward<__bind_front_op, _Fn, _Args...>(_VSTD::forward<_Fn>(__f),
3092 _VSTD::forward<_Args>(__args)...);
3093}
3094
3095#endif // _LIBCPP_STD_VER > 17
Eric Fiselier0d974f12015-07-14 20:16:15 +00003096
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003097// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098
Marshall Clowa40686b2018-01-08 19:18:00 +00003099template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00003100pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00003101__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3102 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
3103 forward_iterator_tag, forward_iterator_tag)
3104{
3105 if (__first2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003106 return _VSTD::make_pair(__first1, __first1); // Everything matches an empty sequence
Marshall Clowa40686b2018-01-08 19:18:00 +00003107 while (true)
3108 {
3109 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
3110 while (true)
3111 {
3112 if (__first1 == __last1) // return __last1 if no element matches *__first2
Logan Smith4528b5f2020-05-07 11:54:25 -04003113 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003114 if (__pred(*__first1, *__first2))
3115 break;
3116 ++__first1;
3117 }
3118 // *__first1 matches *__first2, now match elements after here
3119 _ForwardIterator1 __m1 = __first1;
3120 _ForwardIterator2 __m2 = __first2;
3121 while (true)
3122 {
3123 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Logan Smith4528b5f2020-05-07 11:54:25 -04003124 return _VSTD::make_pair(__first1, __m1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003125 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Logan Smith4528b5f2020-05-07 11:54:25 -04003126 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003127 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
3128 {
3129 ++__first1;
3130 break;
3131 } // else there is a match, check next elements
3132 }
3133 }
3134}
3135
3136template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
3137_LIBCPP_CONSTEXPR_AFTER_CXX11
3138pair<_RandomAccessIterator1, _RandomAccessIterator1>
3139__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
3140 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
3141 random_access_iterator_tag, random_access_iterator_tag)
3142{
3143 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
3144 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
3145 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
3146 const _D2 __len2 = __last2 - __first2;
3147 if (__len2 == 0)
Logan Smith4528b5f2020-05-07 11:54:25 -04003148 return _VSTD::make_pair(__first1, __first1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003149 const _D1 __len1 = __last1 - __first1;
3150 if (__len1 < __len2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003151 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003152 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
3153
3154 while (true)
3155 {
3156 while (true)
3157 {
3158 if (__first1 == __s)
Logan Smith4528b5f2020-05-07 11:54:25 -04003159 return _VSTD::make_pair(__last1, __last1);
Marshall Clowa40686b2018-01-08 19:18:00 +00003160 if (__pred(*__first1, *__first2))
3161 break;
3162 ++__first1;
3163 }
3164
3165 _RandomAccessIterator1 __m1 = __first1;
3166 _RandomAccessIterator2 __m2 = __first2;
3167 while (true)
3168 {
3169 if (++__m2 == __last2)
Logan Smith4528b5f2020-05-07 11:54:25 -04003170 return _VSTD::make_pair(__first1, __first1 + __len2);
Marshall Clowa40686b2018-01-08 19:18:00 +00003171 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
3172 if (!__pred(*__m1, *__m2))
3173 {
3174 ++__first1;
3175 break;
3176 }
3177 }
3178 }
3179}
3180
3181#if _LIBCPP_STD_VER > 14
3182
3183// default searcher
3184template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Dimitry Andricfd3633d2018-02-13 17:40:59 +00003185class _LIBCPP_TYPE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00003186public:
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003187 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Louis Dionne44bcff92018-08-03 22:36:53 +00003188 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00003189 _BinaryPredicate __p = _BinaryPredicate())
3190 : __first_(__f), __last_(__l), __pred_(__p) {}
3191
3192 template <typename _ForwardIterator2>
Arthur O'Dwyer81449d32020-12-25 14:48:39 -05003193 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00003194 pair<_ForwardIterator2, _ForwardIterator2>
3195 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
3196 {
3197 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
3198 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
3199 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
3200 }
3201
3202private:
3203 _ForwardIterator __first_;
3204 _ForwardIterator __last_;
3205 _BinaryPredicate __pred_;
Eric Fiselier80663c52019-08-04 07:13:43 +00003206 };
Marshall Clowa40686b2018-01-08 19:18:00 +00003207
3208#endif // _LIBCPP_STD_VER > 14
3209
Louis Dionnedb1892a2018-12-03 14:03:27 +00003210#if _LIBCPP_STD_VER > 17
3211template <class _Tp>
3212using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
3213
3214template <class _Tp>
3215using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
3216#endif // > C++17
3217
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218_LIBCPP_END_NAMESPACE_STD
3219
3220#endif // _LIBCPP_FUNCTIONAL