blob: 61c87b02c70c30f408533411212dd1779591cd28 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===------------------------ functional ----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL
12#define _LIBCPP_FUNCTIONAL
13
14/*
15 functional synopsis
16
17namespace std
18{
19
20template <class Arg, class Result>
21struct unary_function
22{
23 typedef Arg argument_type;
24 typedef Result result_type;
25};
26
27template <class Arg1, class Arg2, class Result>
28struct binary_function
29{
30 typedef Arg1 first_argument_type;
31 typedef Arg2 second_argument_type;
32 typedef Result result_type;
33};
34
Howard Hinnantf06d9262010-08-20 19:36:46 +000035template <class T>
Howard Hinnantc51e1022010-05-11 19:42:16 +000036class reference_wrapper
37 : public unary_function<T1, R> // if wrapping a unary functor
38 : public binary_function<T1, T2, R> // if wraping a binary functor
39{
40public:
41 // types
42 typedef T type;
43 typedef see below result_type; // Not always defined
44
45 // construct/copy/destroy
Howard Hinnantf7724cd2011-05-28 17:59:48 +000046 reference_wrapper(T&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 reference_wrapper(T&&) = delete; // do not bind to temps
Howard Hinnantf7724cd2011-05-28 17:59:48 +000048 reference_wrapper(const reference_wrapper<T>& x) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
50 // assignment
Howard Hinnantf7724cd2011-05-28 17:59:48 +000051 reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000052
53 // access
Howard Hinnantf7724cd2011-05-28 17:59:48 +000054 operator T& () const noexcept;
55 T& get() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000056
57 // invoke
58 template <class... ArgTypes>
Howard Hinnantc9c775b2013-09-21 17:58:58 +000059 typename result_of<T&(ArgTypes&&...)>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +000060 operator() (ArgTypes&&...) const;
61};
62
Howard Hinnantf7724cd2011-05-28 17:59:48 +000063template <class T> reference_wrapper<T> ref(T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000064template <class T> void ref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000065template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000066
Howard Hinnantf7724cd2011-05-28 17:59:48 +000067template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +000068template <class T> void cref(const T&& t) = delete;
Howard Hinnantf7724cd2011-05-28 17:59:48 +000069template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000070
Marshall Clow974bae22013-07-29 14:21:53 +000071template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000072struct plus : binary_function<T, T, T>
73{
74 T operator()(const T& x, const T& y) const;
75};
76
Marshall Clow974bae22013-07-29 14:21:53 +000077template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000078struct minus : binary_function<T, T, T>
79{
80 T operator()(const T& x, const T& y) const;
81};
82
Marshall Clow974bae22013-07-29 14:21:53 +000083template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000084struct multiplies : binary_function<T, T, T>
85{
86 T operator()(const T& x, const T& y) const;
87};
88
Marshall Clow974bae22013-07-29 14:21:53 +000089template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000090struct divides : binary_function<T, T, T>
91{
92 T operator()(const T& x, const T& y) const;
93};
94
Marshall Clow974bae22013-07-29 14:21:53 +000095template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096struct modulus : binary_function<T, T, T>
97{
98 T operator()(const T& x, const T& y) const;
99};
100
Marshall Clow974bae22013-07-29 14:21:53 +0000101template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102struct negate : unary_function<T, T>
103{
104 T operator()(const T& x) const;
105};
106
Marshall Clow974bae22013-07-29 14:21:53 +0000107template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108struct equal_to : binary_function<T, T, bool>
109{
110 bool operator()(const T& x, const T& y) const;
111};
112
Marshall Clow974bae22013-07-29 14:21:53 +0000113template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114struct not_equal_to : binary_function<T, T, bool>
115{
116 bool operator()(const T& x, const T& y) const;
117};
118
Marshall Clow974bae22013-07-29 14:21:53 +0000119template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120struct greater : binary_function<T, T, bool>
121{
122 bool operator()(const T& x, const T& y) const;
123};
124
Marshall Clow974bae22013-07-29 14:21:53 +0000125template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126struct less : binary_function<T, T, bool>
127{
128 bool operator()(const T& x, const T& y) const;
129};
130
Marshall Clow974bae22013-07-29 14:21:53 +0000131template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132struct greater_equal : binary_function<T, T, bool>
133{
134 bool operator()(const T& x, const T& y) const;
135};
136
Marshall Clow974bae22013-07-29 14:21:53 +0000137template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000138struct less_equal : binary_function<T, T, bool>
139{
140 bool operator()(const T& x, const T& y) const;
141};
142
Marshall Clow974bae22013-07-29 14:21:53 +0000143template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144struct logical_and : binary_function<T, T, bool>
145{
146 bool operator()(const T& x, const T& y) const;
147};
148
Marshall Clow974bae22013-07-29 14:21:53 +0000149template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150struct logical_or : binary_function<T, T, bool>
151{
152 bool operator()(const T& x, const T& y) const;
153};
154
Marshall Clow974bae22013-07-29 14:21:53 +0000155template <class T> // <class T=void> in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156struct logical_not : unary_function<T, bool>
157{
158 bool operator()(const T& x) const;
159};
160
Marshall Clow974bae22013-07-29 14:21:53 +0000161template <class T> // <class T=void> in C++14
162struct bit_and : unary_function<T, bool>
163{
164 bool operator()(const T& x, const T& y) const;
165};
166
167template <class T> // <class T=void> in C++14
168struct bit_or : unary_function<T, bool>
169{
170 bool operator()(const T& x, const T& y) const;
171};
172
173template <class T> // <class T=void> in C++14
174struct bit_xor : unary_function<T, bool>
175{
176 bool operator()(const T& x, const T& y) const;
177};
178
179template <class T=void> // C++14
180struct bit_xor : unary_function<T, bool>
181{
182 bool operator()(const T& x) const;
183};
184
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000186class unary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187 : public unary_function<typename Predicate::argument_type, bool>
188{
189public:
190 explicit unary_negate(const Predicate& pred);
191 bool operator()(const typename Predicate::argument_type& x) const;
192};
193
Louis Dionne481a2662018-09-23 18:35:00 +0000194template <class Predicate> // deprecated in C++17
195unary_negate<Predicate> not1(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196
197template <class Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000198class binary_negate // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199 : public binary_function<typename Predicate::first_argument_type,
200 typename Predicate::second_argument_type,
201 bool>
202{
203public:
204 explicit binary_negate(const Predicate& pred);
205 bool operator()(const typename Predicate::first_argument_type& x,
206 const typename Predicate::second_argument_type& y) const;
207};
208
Louis Dionne481a2662018-09-23 18:35:00 +0000209template <class Predicate> // deprecated in C++17
210binary_negate<Predicate> not2(const Predicate& pred);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211
Eric Fiselier934f63b2016-06-02 01:25:41 +0000212template <class F> unspecified not_fn(F&& f); // C++17
213
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214template<class T> struct is_bind_expression;
215template<class T> struct is_placeholder;
216
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000217 // See C++14 20.9.9, Function object binders
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000218template <class T> inline constexpr bool is_bind_expression_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000219 = is_bind_expression<T>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000220template <class T> inline constexpr int is_placeholder_v
Marshall Clow2d2d7f12016-09-22 00:23:15 +0000221 = is_placeholder<T>::value; // C++17
222
223
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000224template<class Fn, class... BoundArgs>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000225 unspecified bind(Fn&&, BoundArgs&&...);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000226template<class R, class Fn, class... BoundArgs>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000227 unspecified bind(Fn&&, BoundArgs&&...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000229namespace placeholders {
230 // M is the implementation-defined number of placeholders
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 extern unspecified _1;
232 extern unspecified _2;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000233 .
234 .
235 .
Howard Hinnantc834c512011-11-29 18:15:50 +0000236 extern unspecified _Mp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237}
238
239template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000240class binder1st // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241 : public unary_function<typename Operation::second_argument_type,
242 typename Operation::result_type>
243{
244protected:
245 Operation op;
246 typename Operation::first_argument_type value;
247public:
248 binder1st(const Operation& x, const typename Operation::first_argument_type y);
249 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
250 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
251};
252
253template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000254binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255
256template <class Operation>
Marshall Clow26a027c2017-04-13 18:25:32 +0000257class binder2nd // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258 : public unary_function<typename Operation::first_argument_type,
259 typename Operation::result_type>
260{
261protected:
262 Operation op;
263 typename Operation::second_argument_type value;
264public:
265 binder2nd(const Operation& x, const typename Operation::second_argument_type y);
266 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
267 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
268};
269
270template <class Operation, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000271binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272
Marshall Clow26a027c2017-04-13 18:25:32 +0000273template <class Arg, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274class pointer_to_unary_function : public unary_function<Arg, Result>
275{
276public:
277 explicit pointer_to_unary_function(Result (*f)(Arg));
278 Result operator()(Arg x) const;
279};
280
281template <class Arg, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000282pointer_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 +0000283
Marshall Clow26a027c2017-04-13 18:25:32 +0000284template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
286{
287public:
288 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
289 Result operator()(Arg1 x, Arg2 y) const;
290};
291
292template <class Arg1, class Arg2, class Result>
Marshall Clow26a027c2017-04-13 18:25:32 +0000293pointer_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 +0000294
Marshall Clow26a027c2017-04-13 18:25:32 +0000295template<class S, class T> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296class mem_fun_t : public unary_function<T*, S>
297{
298public:
299 explicit mem_fun_t(S (T::*p)());
300 S operator()(T* p) const;
301};
302
303template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000304class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305{
306public:
307 explicit mem_fun1_t(S (T::*p)(A));
308 S operator()(T* p, A x) const;
309};
310
Marshall Clow26a027c2017-04-13 18:25:32 +0000311template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
312template<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 +0000313
314template<class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000315class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316{
317public:
318 explicit mem_fun_ref_t(S (T::*p)());
319 S operator()(T& p) const;
320};
321
322template<class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000323class 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 +0000324{
325public:
326 explicit mem_fun1_ref_t(S (T::*p)(A));
327 S operator()(T& p, A x) const;
328};
329
Marshall Clow26a027c2017-04-13 18:25:32 +0000330template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
331template<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 +0000332
333template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000334class 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 +0000335{
336public:
337 explicit const_mem_fun_t(S (T::*p)() const);
338 S operator()(const T* p) const;
339};
340
341template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000342class 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 +0000343{
344public:
345 explicit const_mem_fun1_t(S (T::*p)(A) const);
346 S operator()(const T* p, A x) const;
347};
348
Marshall Clow26a027c2017-04-13 18:25:32 +0000349template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
350template <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 +0000351
352template <class S, class T>
Marshall Clow26a027c2017-04-13 18:25:32 +0000353class 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 +0000354{
355public:
356 explicit const_mem_fun_ref_t(S (T::*p)() const);
357 S operator()(const T& p) const;
358};
359
360template <class S, class T, class A>
Marshall Clow26a027c2017-04-13 18:25:32 +0000361class 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 +0000362{
363public:
364 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
365 S operator()(const T& p, A x) const;
366};
367
Marshall Clow26a027c2017-04-13 18:25:32 +0000368template <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
369template <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 +0000370
Howard Hinnantf06d9262010-08-20 19:36:46 +0000371template<class R, class T> unspecified mem_fn(R T::*);
Howard Hinnantf06d9262010-08-20 19:36:46 +0000372
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373class bad_function_call
374 : public exception
375{
376};
377
Howard Hinnantf06d9262010-08-20 19:36:46 +0000378template<class> class function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnantf06d9262010-08-20 19:36:46 +0000380template<class R, class... ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381class function<R(ArgTypes...)>
382 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
383 // ArgTypes contains T1
384 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
385 // ArgTypes contains T1 and T2
386{
387public:
388 typedef R result_type;
389
Howard Hinnantf06d9262010-08-20 19:36:46 +0000390 // construct/copy/destroy:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000391 function() noexcept;
392 function(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000394 function(function&&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395 template<class F>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 function(F);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000398 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000400 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000402 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 template<Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000404 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 template<class F, Allocator Alloc>
Marshall Clow3148f422016-10-13 21:06:03 +0000406 function(allocator_arg_t, const Alloc&, F); // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407
408 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000409 function& operator=(function&&) noexcept;
Howard Hinnant7b85be02011-05-29 13:53:56 +0000410 function& operator=(nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 template<class F>
Howard Hinnantf06d9262010-08-20 19:36:46 +0000412 function& operator=(F&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 template<class F>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000414 function& operator=(reference_wrapper<F>) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415
416 ~function();
417
Howard Hinnantf06d9262010-08-20 19:36:46 +0000418 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000419 void swap(function&) noexcept;
Howard Hinnantf06d9262010-08-20 19:36:46 +0000420 template<class F, class Alloc>
Marshall Clowfc8fd832016-01-25 17:29:55 +0000421 void assign(F&&, const Alloc&); // Removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
Howard Hinnantf06d9262010-08-20 19:36:46 +0000423 // function capacity:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000424 explicit operator bool() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425
Howard Hinnantf06d9262010-08-20 19:36:46 +0000426 // function invocation:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427 R operator()(ArgTypes...) const;
428
Howard Hinnantf06d9262010-08-20 19:36:46 +0000429 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000430 const std::type_info& target_type() const noexcept;
431 template <typename T> T* target() noexcept;
432 template <typename T> const T* target() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433};
434
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000435// Null pointer comparisons:
436template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000437 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000439template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000440 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000442template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000443 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000445template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000446 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000448// specialized algorithms:
449template <class R, class ... ArgTypes>
Howard Hinnantf7724cd2011-05-28 17:59:48 +0000450 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000451
452template <class T> struct hash;
453
454template <> struct hash<bool>;
455template <> struct hash<char>;
456template <> struct hash<signed char>;
457template <> struct hash<unsigned char>;
458template <> struct hash<char16_t>;
459template <> struct hash<char32_t>;
460template <> struct hash<wchar_t>;
461template <> struct hash<short>;
462template <> struct hash<unsigned short>;
463template <> struct hash<int>;
464template <> struct hash<unsigned int>;
465template <> struct hash<long>;
466template <> struct hash<long long>;
467template <> struct hash<unsigned long>;
468template <> struct hash<unsigned long long>;
469
470template <> struct hash<float>;
471template <> struct hash<double>;
472template <> struct hash<long double>;
473
474template<class T> struct hash<T*>;
Marshall Clowcc252222017-03-23 06:20:18 +0000475template <> struct hash<nullptr_t>; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000476
477} // std
478
479POLICY: For non-variadic implementations, the number of arguments is limited
480 to 3. It is hoped that the need for non-variadic implementations
481 will be minimal.
482
483*/
484
485#include <__config>
486#include <type_traits>
487#include <typeinfo>
488#include <exception>
489#include <memory>
490#include <tuple>
Eric Fiselier698a97b2017-01-21 00:02:12 +0000491#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000492#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000493
494#include <__functional_base>
495
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000496#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000497#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000498#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499
500_LIBCPP_BEGIN_NAMESPACE_STD
501
Marshall Clow974bae22013-07-29 14:21:53 +0000502#if _LIBCPP_STD_VER > 11
503template <class _Tp = void>
504#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000506#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000507struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508{
Marshall Clowd18ee652013-09-28 19:06:12 +0000509 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
510 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000511 {return __x + __y;}
512};
513
Marshall Clow974bae22013-07-29 14:21:53 +0000514#if _LIBCPP_STD_VER > 11
515template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000516struct _LIBCPP_TEMPLATE_VIS plus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000517{
518 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000519 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
520 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000521 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
522 -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
523 { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000524 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000525};
526#endif
527
528
529#if _LIBCPP_STD_VER > 11
530template <class _Tp = void>
531#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000533#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000534struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535{
Marshall Clowd18ee652013-09-28 19:06:12 +0000536 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
537 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000538 {return __x - __y;}
539};
540
Marshall Clow974bae22013-07-29 14:21:53 +0000541#if _LIBCPP_STD_VER > 11
542template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000543struct _LIBCPP_TEMPLATE_VIS minus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000544{
545 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000546 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
547 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000548 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
549 -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
550 { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000551 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000552};
553#endif
554
555
556#if _LIBCPP_STD_VER > 11
557template <class _Tp = void>
558#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000560#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000561struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562{
Marshall Clowd18ee652013-09-28 19:06:12 +0000563 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
564 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565 {return __x * __y;}
566};
567
Marshall Clow974bae22013-07-29 14:21:53 +0000568#if _LIBCPP_STD_VER > 11
569template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000570struct _LIBCPP_TEMPLATE_VIS multiplies<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000571{
572 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000573 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
574 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000575 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
576 -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
577 { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000578 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000579};
580#endif
581
582
583#if _LIBCPP_STD_VER > 11
584template <class _Tp = void>
585#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000587#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000588struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589{
Marshall Clowd18ee652013-09-28 19:06:12 +0000590 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
591 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592 {return __x / __y;}
593};
594
Marshall Clow974bae22013-07-29 14:21:53 +0000595#if _LIBCPP_STD_VER > 11
596template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000597struct _LIBCPP_TEMPLATE_VIS divides<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000598{
599 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000600 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
601 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000602 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
603 -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
604 { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000605 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000606};
607#endif
608
609
610#if _LIBCPP_STD_VER > 11
611template <class _Tp = void>
612#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000614#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000615struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616{
Marshall Clowd18ee652013-09-28 19:06:12 +0000617 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
618 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619 {return __x % __y;}
620};
621
Marshall Clow974bae22013-07-29 14:21:53 +0000622#if _LIBCPP_STD_VER > 11
623template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000624struct _LIBCPP_TEMPLATE_VIS modulus<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000625{
626 template <class _T1, class _T2>
Marshall Clowd18ee652013-09-28 19:06:12 +0000627 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
628 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000629 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
630 -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
631 { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000632 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000633};
634#endif
635
636
637#if _LIBCPP_STD_VER > 11
638template <class _Tp = void>
639#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000641#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000642struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643{
Marshall Clowd18ee652013-09-28 19:06:12 +0000644 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
645 _Tp operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 {return -__x;}
647};
648
Marshall Clow974bae22013-07-29 14:21:53 +0000649#if _LIBCPP_STD_VER > 11
650template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000651struct _LIBCPP_TEMPLATE_VIS negate<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000652{
653 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000654 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
655 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000656 _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
657 -> decltype (- _VSTD::forward<_Tp>(__x))
658 { return - _VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000659 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000660};
661#endif
662
663
664#if _LIBCPP_STD_VER > 11
665template <class _Tp = void>
666#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000668#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000669struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670{
Marshall Clowd18ee652013-09-28 19:06:12 +0000671 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
672 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 {return __x == __y;}
674};
675
Marshall Clow974bae22013-07-29 14:21:53 +0000676#if _LIBCPP_STD_VER > 11
677template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000678struct _LIBCPP_TEMPLATE_VIS equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000679{
Marshall Clowd18ee652013-09-28 19:06:12 +0000680 template <class _T1, class _T2>
681 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000682 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000683 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
684 -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
685 { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000686 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000687};
688#endif
689
690
691#if _LIBCPP_STD_VER > 11
692template <class _Tp = void>
693#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000695#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000696struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697{
Marshall Clowd18ee652013-09-28 19:06:12 +0000698 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
699 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 {return __x != __y;}
701};
702
Marshall Clow974bae22013-07-29 14:21:53 +0000703#if _LIBCPP_STD_VER > 11
704template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000705struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000706{
Marshall Clowd18ee652013-09-28 19:06:12 +0000707 template <class _T1, class _T2>
708 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000709 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000710 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
711 -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
712 { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000713 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000714};
715#endif
716
717
718#if _LIBCPP_STD_VER > 11
719template <class _Tp = void>
720#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000722#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000723struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724{
Marshall Clowd18ee652013-09-28 19:06:12 +0000725 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
726 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727 {return __x > __y;}
728};
729
Marshall Clow974bae22013-07-29 14:21:53 +0000730#if _LIBCPP_STD_VER > 11
731template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000732struct _LIBCPP_TEMPLATE_VIS greater<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000733{
Marshall Clowd18ee652013-09-28 19:06:12 +0000734 template <class _T1, class _T2>
735 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000736 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000737 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
738 -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
739 { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000740 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000741};
742#endif
743
744
Howard Hinnantb17caf92012-02-21 21:02:58 +0000745// less in <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746
Marshall Clow974bae22013-07-29 14:21:53 +0000747#if _LIBCPP_STD_VER > 11
748template <class _Tp = void>
749#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000751#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000752struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753{
Marshall Clowd18ee652013-09-28 19:06:12 +0000754 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
755 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756 {return __x >= __y;}
757};
758
Marshall Clow974bae22013-07-29 14:21:53 +0000759#if _LIBCPP_STD_VER > 11
760template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000761struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000762{
Marshall Clowd18ee652013-09-28 19:06:12 +0000763 template <class _T1, class _T2>
764 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000765 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000766 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
767 -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
768 { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000769 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000770};
771#endif
772
773
774#if _LIBCPP_STD_VER > 11
775template <class _Tp = void>
776#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000778#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000779struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780{
Marshall Clowd18ee652013-09-28 19:06:12 +0000781 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
782 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783 {return __x <= __y;}
784};
785
Marshall Clow974bae22013-07-29 14:21:53 +0000786#if _LIBCPP_STD_VER > 11
787template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000788struct _LIBCPP_TEMPLATE_VIS less_equal<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000789{
Marshall Clowd18ee652013-09-28 19:06:12 +0000790 template <class _T1, class _T2>
791 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000792 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000793 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
794 -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
795 { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000796 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000797};
798#endif
799
800
801#if _LIBCPP_STD_VER > 11
802template <class _Tp = void>
803#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000805#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000806struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807{
Marshall Clowd18ee652013-09-28 19:06:12 +0000808 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
809 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 {return __x && __y;}
811};
812
Marshall Clow974bae22013-07-29 14:21:53 +0000813#if _LIBCPP_STD_VER > 11
814template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000815struct _LIBCPP_TEMPLATE_VIS logical_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000816{
Marshall Clowd18ee652013-09-28 19:06:12 +0000817 template <class _T1, class _T2>
818 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000819 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000820 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
821 -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
822 { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000823 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000824};
825#endif
826
827
828#if _LIBCPP_STD_VER > 11
829template <class _Tp = void>
830#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000832#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000833struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834{
Marshall Clowd18ee652013-09-28 19:06:12 +0000835 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
836 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837 {return __x || __y;}
838};
839
Marshall Clow974bae22013-07-29 14:21:53 +0000840#if _LIBCPP_STD_VER > 11
841template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000842struct _LIBCPP_TEMPLATE_VIS logical_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000843{
Marshall Clowd18ee652013-09-28 19:06:12 +0000844 template <class _T1, class _T2>
845 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000846 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000847 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
848 -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
849 { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000850 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000851};
852#endif
853
854
855#if _LIBCPP_STD_VER > 11
856template <class _Tp = void>
857#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000858template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000859#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000860struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861{
Marshall Clowd18ee652013-09-28 19:06:12 +0000862 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
863 bool operator()(const _Tp& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864 {return !__x;}
865};
866
Marshall Clow974bae22013-07-29 14:21:53 +0000867#if _LIBCPP_STD_VER > 11
868template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000869struct _LIBCPP_TEMPLATE_VIS logical_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000870{
871 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000872 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
873 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000874 _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
875 -> decltype (!_VSTD::forward<_Tp>(__x))
876 { return !_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000877 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000878};
879#endif
880
881
882#if _LIBCPP_STD_VER > 11
883template <class _Tp = void>
884#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000886#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000887struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888{
Marshall Clowd18ee652013-09-28 19:06:12 +0000889 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
890 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 {return __x & __y;}
892};
893
Marshall Clow974bae22013-07-29 14:21:53 +0000894#if _LIBCPP_STD_VER > 11
895template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000896struct _LIBCPP_TEMPLATE_VIS bit_and<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000897{
Marshall Clowd18ee652013-09-28 19:06:12 +0000898 template <class _T1, class _T2>
899 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000900 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000901 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
902 -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
903 { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000904 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000905};
906#endif
907
908
909#if _LIBCPP_STD_VER > 11
910template <class _Tp = void>
911#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000913#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000914struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915{
Marshall Clowd18ee652013-09-28 19:06:12 +0000916 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
917 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918 {return __x | __y;}
919};
920
Marshall Clow974bae22013-07-29 14:21:53 +0000921#if _LIBCPP_STD_VER > 11
922template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000923struct _LIBCPP_TEMPLATE_VIS bit_or<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000924{
Marshall Clowd18ee652013-09-28 19:06:12 +0000925 template <class _T1, class _T2>
926 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000927 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000928 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
929 -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
930 { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000931 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000932};
933#endif
934
935
936#if _LIBCPP_STD_VER > 11
937template <class _Tp = void>
938#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939template <class _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000940#endif
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000941struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942{
Marshall Clowd18ee652013-09-28 19:06:12 +0000943 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
944 _Tp operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 {return __x ^ __y;}
946};
947
Marshall Clow974bae22013-07-29 14:21:53 +0000948#if _LIBCPP_STD_VER > 11
949template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000950struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000951{
Marshall Clowd18ee652013-09-28 19:06:12 +0000952 template <class _T1, class _T2>
953 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow974bae22013-07-29 14:21:53 +0000954 auto operator()(_T1&& __t, _T2&& __u) const
Marshall Clow012ca342015-02-25 12:20:52 +0000955 _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
956 -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
957 { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
Marshall Clowc0152142013-08-13 01:11:06 +0000958 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000959};
960#endif
961
962
963#if _LIBCPP_STD_VER > 11
964template <class _Tp = void>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000965struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
Marshall Clow974bae22013-07-29 14:21:53 +0000966{
Marshall Clowd18ee652013-09-28 19:06:12 +0000967 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
968 _Tp operator()(const _Tp& __x) const
Marshall Clow974bae22013-07-29 14:21:53 +0000969 {return ~__x;}
970};
971
972template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000973struct _LIBCPP_TEMPLATE_VIS bit_not<void>
Marshall Clow974bae22013-07-29 14:21:53 +0000974{
975 template <class _Tp>
Marshall Clowd18ee652013-09-28 19:06:12 +0000976 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
977 auto operator()(_Tp&& __x) const
Marshall Clow012ca342015-02-25 12:20:52 +0000978 _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
979 -> decltype (~_VSTD::forward<_Tp>(__x))
980 { return ~_VSTD::forward<_Tp>(__x); }
Marshall Clowc0152142013-08-13 01:11:06 +0000981 typedef void is_transparent;
Marshall Clow974bae22013-07-29 14:21:53 +0000982};
983#endif
984
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +0000986class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 : public unary_function<typename _Predicate::argument_type, bool>
988{
989 _Predicate __pred_;
990public:
Marshall Clowd18ee652013-09-28 19:06:12 +0000991 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
992 explicit unary_negate(const _Predicate& __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993 : __pred_(__pred) {}
Marshall Clowd18ee652013-09-28 19:06:12 +0000994 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
995 bool operator()(const typename _Predicate::argument_type& __x) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 {return !__pred_(__x);}
997};
998
999template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001000_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001unary_negate<_Predicate>
1002not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
1003
1004template <class _Predicate>
Louis Dionne481a2662018-09-23 18:35:00 +00001005class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 : public binary_function<typename _Predicate::first_argument_type,
1007 typename _Predicate::second_argument_type,
1008 bool>
1009{
1010 _Predicate __pred_;
1011public:
Louis Dionne44bcff92018-08-03 22:36:53 +00001012 _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowd18ee652013-09-28 19:06:12 +00001013 binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
1014
1015 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1016 bool operator()(const typename _Predicate::first_argument_type& __x,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 const typename _Predicate::second_argument_type& __y) const
1018 {return !__pred_(__x, __y);}
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 +00001023binary_negate<_Predicate>
1024not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
1025
Marshall Clow26a027c2017-04-13 18:25:32 +00001026#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001028class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 : public unary_function<typename __Operation::second_argument_type,
1030 typename __Operation::result_type>
1031{
1032protected:
1033 __Operation op;
1034 typename __Operation::first_argument_type value;
1035public:
1036 _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
1037 const typename __Operation::first_argument_type __y)
1038 : op(__x), value(__y) {}
1039 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1040 (typename __Operation::second_argument_type& __x) const
1041 {return op(value, __x);}
1042 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1043 (const typename __Operation::second_argument_type& __x) const
1044 {return op(value, __x);}
1045};
1046
1047template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001048_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049binder1st<__Operation>
1050bind1st(const __Operation& __op, const _Tp& __x)
1051 {return binder1st<__Operation>(__op, __x);}
1052
1053template <class __Operation>
Louis Dionne481a2662018-09-23 18:35:00 +00001054class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 : public unary_function<typename __Operation::first_argument_type,
1056 typename __Operation::result_type>
1057{
1058protected:
1059 __Operation op;
1060 typename __Operation::second_argument_type value;
1061public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
1064 : op(__x), value(__y) {}
1065 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1066 ( typename __Operation::first_argument_type& __x) const
1067 {return op(__x, value);}
1068 _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
1069 (const typename __Operation::first_argument_type& __x) const
1070 {return op(__x, value);}
1071};
1072
1073template <class __Operation, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001074_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075binder2nd<__Operation>
1076bind2nd(const __Operation& __op, const _Tp& __x)
1077 {return binder2nd<__Operation>(__op, __x);}
1078
1079template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001080class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001081 : public unary_function<_Arg, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082{
1083 _Result (*__f_)(_Arg);
1084public:
1085 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
1086 : __f_(__f) {}
1087 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
1088 {return __f_(__x);}
1089};
1090
1091template <class _Arg, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001092_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093pointer_to_unary_function<_Arg,_Result>
1094ptr_fun(_Result (*__f)(_Arg))
1095 {return pointer_to_unary_function<_Arg,_Result>(__f);}
1096
1097template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001098class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
Howard Hinnant4ff57432010-09-21 22:55:27 +00001099 : public binary_function<_Arg1, _Arg2, _Result>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100{
1101 _Result (*__f_)(_Arg1, _Arg2);
1102public:
1103 _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
1104 : __f_(__f) {}
1105 _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
1106 {return __f_(__x, __y);}
1107};
1108
1109template <class _Arg1, class _Arg2, class _Result>
Louis Dionne481a2662018-09-23 18:35:00 +00001110_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111pointer_to_binary_function<_Arg1,_Arg2,_Result>
1112ptr_fun(_Result (*__f)(_Arg1,_Arg2))
1113 {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
1114
1115template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001116class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
1117 : public unary_function<_Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118{
1119 _Sp (_Tp::*__p_)();
1120public:
1121 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
1122 : __p_(__p) {}
1123 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
1124 {return (__p->*__p_)();}
1125};
1126
1127template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001128class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
1129 : public binary_function<_Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130{
1131 _Sp (_Tp::*__p_)(_Ap);
1132public:
1133 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
1134 : __p_(__p) {}
1135 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
1136 {return (__p->*__p_)(__x);}
1137};
1138
1139template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001140_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141mem_fun_t<_Sp,_Tp>
1142mem_fun(_Sp (_Tp::*__f)())
1143 {return mem_fun_t<_Sp,_Tp>(__f);}
1144
1145template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001146_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147mem_fun1_t<_Sp,_Tp,_Ap>
1148mem_fun(_Sp (_Tp::*__f)(_Ap))
1149 {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1150
1151template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001152class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
1153 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154{
1155 _Sp (_Tp::*__p_)();
1156public:
1157 _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
1158 : __p_(__p) {}
1159 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
1160 {return (__p.*__p_)();}
1161};
1162
1163template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001164class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
1165 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166{
1167 _Sp (_Tp::*__p_)(_Ap);
1168public:
1169 _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
1170 : __p_(__p) {}
1171 _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
1172 {return (__p.*__p_)(__x);}
1173};
1174
1175template<class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001176_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177mem_fun_ref_t<_Sp,_Tp>
1178mem_fun_ref(_Sp (_Tp::*__f)())
1179 {return mem_fun_ref_t<_Sp,_Tp>(__f);}
1180
1181template<class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001182_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183mem_fun1_ref_t<_Sp,_Tp,_Ap>
1184mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
1185 {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
1186
1187template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001188class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
1189 : public unary_function<const _Tp*, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190{
1191 _Sp (_Tp::*__p_)() const;
1192public:
1193 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
1194 : __p_(__p) {}
1195 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
1196 {return (__p->*__p_)();}
1197};
1198
1199template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001200class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
1201 : public binary_function<const _Tp*, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202{
1203 _Sp (_Tp::*__p_)(_Ap) const;
1204public:
1205 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
1206 : __p_(__p) {}
1207 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
1208 {return (__p->*__p_)(__x);}
1209};
1210
1211template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001212_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213const_mem_fun_t<_Sp,_Tp>
1214mem_fun(_Sp (_Tp::*__f)() const)
1215 {return const_mem_fun_t<_Sp,_Tp>(__f);}
1216
1217template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001218_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219const_mem_fun1_t<_Sp,_Tp,_Ap>
1220mem_fun(_Sp (_Tp::*__f)(_Ap) const)
1221 {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
1222
1223template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001224class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
1225 : public unary_function<_Tp, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226{
1227 _Sp (_Tp::*__p_)() const;
1228public:
1229 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
1230 : __p_(__p) {}
1231 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
1232 {return (__p.*__p_)();}
1233};
1234
1235template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001236class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
Howard Hinnant4ff57432010-09-21 22:55:27 +00001237 : public binary_function<_Tp, _Ap, _Sp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238{
1239 _Sp (_Tp::*__p_)(_Ap) const;
1240public:
1241 _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
1242 : __p_(__p) {}
1243 _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
1244 {return (__p.*__p_)(__x);}
1245};
1246
1247template <class _Sp, class _Tp>
Louis Dionne481a2662018-09-23 18:35:00 +00001248_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249const_mem_fun_ref_t<_Sp,_Tp>
1250mem_fun_ref(_Sp (_Tp::*__f)() const)
1251 {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
1252
1253template <class _Sp, class _Tp, class _Ap>
Louis Dionne481a2662018-09-23 18:35:00 +00001254_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
1256mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
1257 {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
Marshall Clow26a027c2017-04-13 18:25:32 +00001258#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001260////////////////////////////////////////////////////////////////////////////////
1261// MEMFUN
1262//==============================================================================
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264template <class _Tp>
1265class __mem_fn
1266 : public __weak_result_type<_Tp>
1267{
1268public:
1269 // types
1270 typedef _Tp type;
1271private:
1272 type __f_;
1273
1274public:
Marshall Clowad8ff212015-10-25 20:12:16 +00001275 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001277#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278 // invoke
1279 template <class... _ArgTypes>
Eric Fiselier2cc48332015-07-22 22:43:27 +00001280 _LIBCPP_INLINE_VISIBILITY
1281 typename __invoke_return<type, _ArgTypes...>::type
1282 operator() (_ArgTypes&&... __args) const {
1283 return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
1284 }
1285#else
Eric Fiselier2cc48332015-07-22 22:43:27 +00001286
1287 template <class _A0>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001288 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001289 typename __invoke_return0<type, _A0>::type
1290 operator() (_A0& __a0) const {
1291 return __invoke(__f_, __a0);
1292 }
1293
Eric Fiselierce1813a2015-08-26 20:15:02 +00001294 template <class _A0>
1295 _LIBCPP_INLINE_VISIBILITY
1296 typename __invoke_return0<type, _A0 const>::type
1297 operator() (_A0 const& __a0) const {
1298 return __invoke(__f_, __a0);
1299 }
1300
Eric Fiselier2cc48332015-07-22 22:43:27 +00001301 template <class _A0, class _A1>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001302 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001303 typename __invoke_return1<type, _A0, _A1>::type
1304 operator() (_A0& __a0, _A1& __a1) const {
1305 return __invoke(__f_, __a0, __a1);
1306 }
1307
Eric Fiselierce1813a2015-08-26 20:15:02 +00001308 template <class _A0, class _A1>
1309 _LIBCPP_INLINE_VISIBILITY
1310 typename __invoke_return1<type, _A0 const, _A1>::type
1311 operator() (_A0 const& __a0, _A1& __a1) const {
1312 return __invoke(__f_, __a0, __a1);
1313 }
1314
1315 template <class _A0, class _A1>
1316 _LIBCPP_INLINE_VISIBILITY
1317 typename __invoke_return1<type, _A0, _A1 const>::type
1318 operator() (_A0& __a0, _A1 const& __a1) const {
1319 return __invoke(__f_, __a0, __a1);
1320 }
1321
1322 template <class _A0, class _A1>
1323 _LIBCPP_INLINE_VISIBILITY
1324 typename __invoke_return1<type, _A0 const, _A1 const>::type
1325 operator() (_A0 const& __a0, _A1 const& __a1) const {
1326 return __invoke(__f_, __a0, __a1);
1327 }
1328
Eric Fiselier2cc48332015-07-22 22:43:27 +00001329 template <class _A0, class _A1, class _A2>
Eric Fiselierce1813a2015-08-26 20:15:02 +00001330 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2cc48332015-07-22 22:43:27 +00001331 typename __invoke_return2<type, _A0, _A1, _A2>::type
1332 operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
1333 return __invoke(__f_, __a0, __a1, __a2);
1334 }
Eric Fiselierce1813a2015-08-26 20:15:02 +00001335
1336 template <class _A0, class _A1, class _A2>
1337 _LIBCPP_INLINE_VISIBILITY
1338 typename __invoke_return2<type, _A0 const, _A1, _A2>::type
1339 operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
1340 return __invoke(__f_, __a0, __a1, __a2);
1341 }
1342
1343 template <class _A0, class _A1, class _A2>
1344 _LIBCPP_INLINE_VISIBILITY
1345 typename __invoke_return2<type, _A0, _A1 const, _A2>::type
1346 operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
1347 return __invoke(__f_, __a0, __a1, __a2);
1348 }
1349
1350 template <class _A0, class _A1, class _A2>
1351 _LIBCPP_INLINE_VISIBILITY
1352 typename __invoke_return2<type, _A0, _A1, _A2 const>::type
1353 operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
1354 return __invoke(__f_, __a0, __a1, __a2);
1355 }
1356
1357 template <class _A0, class _A1, class _A2>
1358 _LIBCPP_INLINE_VISIBILITY
1359 typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
1360 operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
1361 return __invoke(__f_, __a0, __a1, __a2);
1362 }
1363
1364 template <class _A0, class _A1, class _A2>
1365 _LIBCPP_INLINE_VISIBILITY
1366 typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
1367 operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
1368 return __invoke(__f_, __a0, __a1, __a2);
1369 }
1370
1371 template <class _A0, class _A1, class _A2>
1372 _LIBCPP_INLINE_VISIBILITY
1373 typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
1374 operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
1375 return __invoke(__f_, __a0, __a1, __a2);
1376 }
1377
1378 template <class _A0, class _A1, class _A2>
1379 _LIBCPP_INLINE_VISIBILITY
1380 typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
1381 operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
1382 return __invoke(__f_, __a0, __a1, __a2);
1383 }
Eric Fiselier2cc48332015-07-22 22:43:27 +00001384#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385};
1386
Howard Hinnantc834c512011-11-29 18:15:50 +00001387template<class _Rp, class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001389__mem_fn<_Rp _Tp::*>
Marshall Clowad8ff212015-10-25 20:12:16 +00001390mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391{
Howard Hinnantc834c512011-11-29 18:15:50 +00001392 return __mem_fn<_Rp _Tp::*>(__pm);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393}
1394
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001395////////////////////////////////////////////////////////////////////////////////
1396// FUNCTION
1397//==============================================================================
1398
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399// bad_function_call
1400
Howard Hinnant4ff57432010-09-21 22:55:27 +00001401class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402 : public exception
1403{
Shoaib Meenaic130eaf2017-03-28 19:33:31 +00001404#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
1405public:
1406 virtual ~bad_function_call() _NOEXCEPT;
1407
1408 virtual const char* what() const _NOEXCEPT;
1409#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410};
1411
Louis Dionne16fe2952018-07-11 23:14:33 +00001412_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +00001413void __throw_bad_function_call()
1414{
1415#ifndef _LIBCPP_NO_EXCEPTIONS
1416 throw bad_function_call();
1417#else
Louis Dionne44bcff92018-08-03 22:36:53 +00001418 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +00001419#endif
1420}
1421
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001422template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423
1424namespace __function
1425{
1426
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001427template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428struct __maybe_derive_from_unary_function
1429{
1430};
1431
Howard Hinnantc834c512011-11-29 18:15:50 +00001432template<class _Rp, class _A1>
1433struct __maybe_derive_from_unary_function<_Rp(_A1)>
1434 : public unary_function<_A1, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435{
1436};
1437
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001438template<class _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439struct __maybe_derive_from_binary_function
1440{
1441};
1442
Howard Hinnantc834c512011-11-29 18:15:50 +00001443template<class _Rp, class _A1, class _A2>
1444struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
1445 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446{
1447};
1448
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001449template <class _Fp>
1450_LIBCPP_INLINE_VISIBILITY
1451bool __not_null(_Fp const&) { return true; }
1452
1453template <class _Fp>
1454_LIBCPP_INLINE_VISIBILITY
1455bool __not_null(_Fp* __ptr) { return __ptr; }
1456
1457template <class _Ret, class _Class>
1458_LIBCPP_INLINE_VISIBILITY
1459bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
1460
1461template <class _Fp>
1462_LIBCPP_INLINE_VISIBILITY
1463bool __not_null(function<_Fp> const& __f) { return !!__f; }
1464
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001465} // namespace __function
1466
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001467#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001468
1469namespace __function {
1470
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471template<class _Fp> class __base;
1472
Howard Hinnantc834c512011-11-29 18:15:50 +00001473template<class _Rp, class ..._ArgTypes>
1474class __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475{
1476 __base(const __base&);
1477 __base& operator=(const __base&);
1478public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001479 _LIBCPP_INLINE_VISIBILITY __base() {}
1480 _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481 virtual __base* __clone() const = 0;
1482 virtual void __clone(__base*) const = 0;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001483 virtual void destroy() _NOEXCEPT = 0;
1484 virtual void destroy_deallocate() _NOEXCEPT = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00001485 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant72f73582010-08-11 17:04:31 +00001486#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001487 virtual const void* target(const type_info&) const _NOEXCEPT = 0;
1488 virtual const std::type_info& target_type() const _NOEXCEPT = 0;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001489#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490};
1491
1492template<class _FD, class _Alloc, class _FB> class __func;
1493
Howard Hinnantc834c512011-11-29 18:15:50 +00001494template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1495class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1496 : public __base<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497{
Howard Hinnantc834c512011-11-29 18:15:50 +00001498 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499public:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001501 explicit __func(_Fp&& __f)
1502 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1503 _VSTD::forward_as_tuple()) {}
Howard Hinnant4ff57432010-09-21 22:55:27 +00001504 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant4828c4a2012-02-28 19:47:38 +00001505 explicit __func(const _Fp& __f, const _Alloc& __a)
1506 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1507 _VSTD::forward_as_tuple(__a)) {}
1508
1509 _LIBCPP_INLINE_VISIBILITY
1510 explicit __func(const _Fp& __f, _Alloc&& __a)
1511 : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
1512 _VSTD::forward_as_tuple(_VSTD::move(__a))) {}
1513
1514 _LIBCPP_INLINE_VISIBILITY
1515 explicit __func(_Fp&& __f, _Alloc&& __a)
1516 : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
1517 _VSTD::forward_as_tuple(_VSTD::move(__a))) {}
Howard Hinnantc834c512011-11-29 18:15:50 +00001518 virtual __base<_Rp(_ArgTypes...)>* __clone() const;
1519 virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001520 virtual void destroy() _NOEXCEPT;
1521 virtual void destroy_deallocate() _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00001522 virtual _Rp operator()(_ArgTypes&& ... __arg);
Howard Hinnant72f73582010-08-11 17:04:31 +00001523#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001524 virtual const void* target(const type_info&) const _NOEXCEPT;
1525 virtual const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001526#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527};
1528
Howard Hinnantc834c512011-11-29 18:15:50 +00001529template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1530__base<_Rp(_ArgTypes...)>*
1531__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001533 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001534 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnantc834c512011-11-29 18:15:50 +00001535 _Ap __a(__f_.second());
1536 typedef __allocator_destructor<_Ap> _Dp;
1537 unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
1539 return __hold.release();
1540}
1541
Howard Hinnantc834c512011-11-29 18:15:50 +00001542template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543void
Howard Hinnantc834c512011-11-29 18:15:50 +00001544__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545{
1546 ::new (__p) __func(__f_.first(), __f_.second());
1547}
1548
Howard Hinnantc834c512011-11-29 18:15:50 +00001549template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550void
Howard Hinnantc834c512011-11-29 18:15:50 +00001551__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552{
Howard Hinnantc834c512011-11-29 18:15:50 +00001553 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554}
1555
Howard Hinnantc834c512011-11-29 18:15:50 +00001556template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557void
Howard Hinnantc834c512011-11-29 18:15:50 +00001558__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559{
Eric Fiselierb5826ad2015-03-18 22:56:50 +00001560 typedef allocator_traits<_Alloc> __alloc_traits;
Marshall Clow940e01c2015-04-07 05:21:38 +00001561 typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
Howard Hinnantc834c512011-11-29 18:15:50 +00001562 _Ap __a(__f_.second());
1563 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 __a.deallocate(this, 1);
1565}
1566
Howard Hinnantc834c512011-11-29 18:15:50 +00001567template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1568_Rp
1569__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570{
Eric Fiselierea080702015-02-10 16:48:45 +00001571 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
1572 return _Invoker::__call(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573}
1574
Howard Hinnant72f73582010-08-11 17:04:31 +00001575#ifndef _LIBCPP_NO_RTTI
1576
Howard Hinnantc834c512011-11-29 18:15:50 +00001577template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578const void*
Howard Hinnantc834c512011-11-29 18:15:50 +00001579__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580{
Howard Hinnantc834c512011-11-29 18:15:50 +00001581 if (__ti == typeid(_Fp))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 return &__f_.first();
1583 return (const void*)0;
1584}
1585
Howard Hinnantc834c512011-11-29 18:15:50 +00001586template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001588__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589{
Howard Hinnantc834c512011-11-29 18:15:50 +00001590 return typeid(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591}
1592
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001593#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001594
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595} // __function
1596
Howard Hinnantc834c512011-11-29 18:15:50 +00001597template<class _Rp, class ..._ArgTypes>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001598class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
Howard Hinnantc834c512011-11-29 18:15:50 +00001599 : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
1600 public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601{
Howard Hinnantc834c512011-11-29 18:15:50 +00001602 typedef __function::__base<_Rp(_ArgTypes...)> __base;
Howard Hinnant022c7482013-01-21 17:26:55 +00001603 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 __base* __f_;
1605
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001606 _LIBCPP_NO_CFI static __base *__as_base(void *p) {
1607 return reinterpret_cast<__base*>(p);
1608 }
1609
Eric Fiselier43c04f72017-09-10 23:41:20 +00001610 template <class _Fp, bool = __lazy_and<
1611 integral_constant<bool, !is_same<__uncvref_t<_Fp>, function>::value>,
1612 __invokable<_Fp&, _ArgTypes...>
1613 >::value>
1614 struct __callable;
Howard Hinnantc834c512011-11-29 18:15:50 +00001615 template <class _Fp>
1616 struct __callable<_Fp, true>
Howard Hinnant95755932011-05-31 21:45:26 +00001617 {
Eric Fiselierea080702015-02-10 16:48:45 +00001618 static const bool value = is_same<void, _Rp>::value ||
Howard Hinnantc834c512011-11-29 18:15:50 +00001619 is_convertible<typename __invoke_of<_Fp&, _ArgTypes...>::type,
1620 _Rp>::value;
Howard Hinnant95755932011-05-31 21:45:26 +00001621 };
Howard Hinnantc834c512011-11-29 18:15:50 +00001622 template <class _Fp>
1623 struct __callable<_Fp, false>
Howard Hinnant95755932011-05-31 21:45:26 +00001624 {
1625 static const bool value = false;
1626 };
Eric Fiselier43c04f72017-09-10 23:41:20 +00001627
1628 template <class _Fp>
1629 using _EnableIfCallable = typename enable_if<__callable<_Fp>::value>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630public:
Howard Hinnantc834c512011-11-29 18:15:50 +00001631 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632
Howard Hinnantf06d9262010-08-20 19:36:46 +00001633 // construct/copy/destroy:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001634 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001635 function() _NOEXCEPT : __f_(0) {}
Howard Hinnant4ff57432010-09-21 22:55:27 +00001636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001637 function(nullptr_t) _NOEXCEPT : __f_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 function(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001639 function(function&&) _NOEXCEPT;
Eric Fiselier43c04f72017-09-10 23:41:20 +00001640 template<class _Fp, class = _EnableIfCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00001641 function(_Fp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642
Marshall Clow3148f422016-10-13 21:06:03 +00001643#if _LIBCPP_STD_VER <= 14
Howard Hinnantf06d9262010-08-20 19:36:46 +00001644 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00001645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001646 function(allocator_arg_t, const _Alloc&) _NOEXCEPT : __f_(0) {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00001647 template<class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00001648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001649 function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT : __f_(0) {}
Howard Hinnantf06d9262010-08-20 19:36:46 +00001650 template<class _Alloc>
1651 function(allocator_arg_t, const _Alloc&, const function&);
1652 template<class _Alloc>
1653 function(allocator_arg_t, const _Alloc&, function&&);
Eric Fiselier43c04f72017-09-10 23:41:20 +00001654 template<class _Fp, class _Alloc, class = _EnableIfCallable<_Fp>>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00001655 function(allocator_arg_t, const _Alloc& __a, _Fp __f);
Marshall Clow3148f422016-10-13 21:06:03 +00001656#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657
1658 function& operator=(const function&);
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001659 function& operator=(function&&) _NOEXCEPT;
1660 function& operator=(nullptr_t) _NOEXCEPT;
Eric Fiselier43c04f72017-09-10 23:41:20 +00001661 template<class _Fp, class = _EnableIfCallable<_Fp>>
1662 function& operator=(_Fp&&);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663
1664 ~function();
1665
Howard Hinnantf06d9262010-08-20 19:36:46 +00001666 // function modifiers:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001667 void swap(function&) _NOEXCEPT;
Marshall Clowfc8fd832016-01-25 17:29:55 +00001668
1669#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00001670 template<class _Fp, class _Alloc>
Howard Hinnant4ff57432010-09-21 22:55:27 +00001671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001672 void assign(_Fp&& __f, const _Alloc& __a)
1673 {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
Marshall Clowfc8fd832016-01-25 17:29:55 +00001674#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675
Howard Hinnantf06d9262010-08-20 19:36:46 +00001676 // function capacity:
Howard Hinnant4ff57432010-09-21 22:55:27 +00001677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00001678 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return __f_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 // deleted overloads close possible hole in the type system
1681 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00001682 bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683 template<class _R2, class... _ArgTypes2>
Howard Hinnant5e9a1cf2010-09-11 15:33:21 +00001684 bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685public:
Howard Hinnantf06d9262010-08-20 19:36:46 +00001686 // function invocation:
Howard Hinnantc834c512011-11-29 18:15:50 +00001687 _Rp operator()(_ArgTypes...) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688
Howard Hinnant72f73582010-08-11 17:04:31 +00001689#ifndef _LIBCPP_NO_RTTI
Howard Hinnantf06d9262010-08-20 19:36:46 +00001690 // function target access:
Howard Hinnantf7724cd2011-05-28 17:59:48 +00001691 const std::type_info& target_type() const _NOEXCEPT;
Howard Hinnantc834c512011-11-29 18:15:50 +00001692 template <typename _Tp> _Tp* target() _NOEXCEPT;
1693 template <typename _Tp> const _Tp* target() const _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001694#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695};
1696
Howard Hinnantc834c512011-11-29 18:15:50 +00001697template<class _Rp, class ..._ArgTypes>
1698function<_Rp(_ArgTypes...)>::function(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699{
1700 if (__f.__f_ == 0)
1701 __f_ = 0;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001702 else if ((void *)__f.__f_ == &__f.__buf_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001704 __f_ = __as_base(&__buf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 __f.__f_->__clone(__f_);
1706 }
1707 else
1708 __f_ = __f.__f_->__clone();
1709}
1710
Marshall Clow3148f422016-10-13 21:06:03 +00001711#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00001712template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00001713template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00001714function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnantf06d9262010-08-20 19:36:46 +00001715 const function& __f)
1716{
1717 if (__f.__f_ == 0)
1718 __f_ = 0;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001719 else if ((void *)__f.__f_ == &__f.__buf_)
Howard Hinnantf06d9262010-08-20 19:36:46 +00001720 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001721 __f_ = __as_base(&__buf_);
Howard Hinnantf06d9262010-08-20 19:36:46 +00001722 __f.__f_->__clone(__f_);
1723 }
1724 else
1725 __f_ = __f.__f_->__clone();
1726}
Marshall Clow3148f422016-10-13 21:06:03 +00001727#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00001728
Howard Hinnantc834c512011-11-29 18:15:50 +00001729template<class _Rp, class ..._ArgTypes>
1730function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731{
1732 if (__f.__f_ == 0)
1733 __f_ = 0;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001734 else if ((void *)__f.__f_ == &__f.__buf_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001736 __f_ = __as_base(&__buf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 __f.__f_->__clone(__f_);
1738 }
1739 else
1740 {
1741 __f_ = __f.__f_;
1742 __f.__f_ = 0;
1743 }
1744}
1745
Marshall Clow3148f422016-10-13 21:06:03 +00001746#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00001747template<class _Rp, class ..._ArgTypes>
Howard Hinnantf06d9262010-08-20 19:36:46 +00001748template <class _Alloc>
Howard Hinnantc834c512011-11-29 18:15:50 +00001749function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
Howard Hinnantf06d9262010-08-20 19:36:46 +00001750 function&& __f)
1751{
1752 if (__f.__f_ == 0)
1753 __f_ = 0;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001754 else if ((void *)__f.__f_ == &__f.__buf_)
Howard Hinnantf06d9262010-08-20 19:36:46 +00001755 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001756 __f_ = __as_base(&__buf_);
Howard Hinnantf06d9262010-08-20 19:36:46 +00001757 __f.__f_->__clone(__f_);
1758 }
1759 else
1760 {
1761 __f_ = __f.__f_;
1762 __f.__f_ = 0;
1763 }
1764}
Marshall Clow3148f422016-10-13 21:06:03 +00001765#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00001766
Howard Hinnantc834c512011-11-29 18:15:50 +00001767template<class _Rp, class ..._ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00001768template <class _Fp, class>
1769function<_Rp(_ArgTypes...)>::function(_Fp __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770 : __f_(0)
1771{
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001772 if (__function::__not_null(__f))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001774 typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_ArgTypes...)> _FF;
1775 if (sizeof(_FF) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001777 __f_ = ::new((void*)&__buf_) _FF(_VSTD::move(__f));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 }
1779 else
1780 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001781 typedef allocator<_FF> _Ap;
1782 _Ap __a;
1783 typedef __allocator_destructor<_Ap> _Dp;
1784 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1785 ::new (__hold.get()) _FF(_VSTD::move(__f), allocator<_Fp>(__a));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 __f_ = __hold.release();
1787 }
1788 }
1789}
1790
Marshall Clow3148f422016-10-13 21:06:03 +00001791#if _LIBCPP_STD_VER <= 14
Howard Hinnantc834c512011-11-29 18:15:50 +00001792template<class _Rp, class ..._ArgTypes>
Eric Fiselierf3e18cf2016-07-20 05:21:00 +00001793template <class _Fp, class _Alloc, class>
1794function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f)
Howard Hinnantf06d9262010-08-20 19:36:46 +00001795 : __f_(0)
1796{
1797 typedef allocator_traits<_Alloc> __alloc_traits;
Eric Fiseliera584a1e2015-08-18 19:41:51 +00001798 if (__function::__not_null(__f))
Howard Hinnantf06d9262010-08-20 19:36:46 +00001799 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001800 typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _FF;
Marshall Clow940e01c2015-04-07 05:21:38 +00001801 typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
Marshall Clowe2631a22014-04-18 17:23:36 +00001802 _Ap __a(__a0);
Louis Dionne44bcff92018-08-03 22:36:53 +00001803 if (sizeof(_FF) <= sizeof(__buf_) &&
Marshall Clowe2631a22014-04-18 17:23:36 +00001804 is_nothrow_copy_constructible<_Fp>::value && is_nothrow_copy_constructible<_Ap>::value)
Howard Hinnantf06d9262010-08-20 19:36:46 +00001805 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001806 __f_ = ::new((void*)&__buf_) _FF(_VSTD::move(__f), _Alloc(__a));
Howard Hinnantf06d9262010-08-20 19:36:46 +00001807 }
1808 else
1809 {
Howard Hinnantc834c512011-11-29 18:15:50 +00001810 typedef __allocator_destructor<_Ap> _Dp;
1811 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001812 ::new (__hold.get()) _FF(_VSTD::move(__f), _Alloc(__a));
Howard Hinnantf06d9262010-08-20 19:36:46 +00001813 __f_ = __hold.release();
1814 }
1815 }
1816}
Marshall Clow3148f422016-10-13 21:06:03 +00001817#endif
Howard Hinnantf06d9262010-08-20 19:36:46 +00001818
Howard Hinnantc834c512011-11-29 18:15:50 +00001819template<class _Rp, class ..._ArgTypes>
1820function<_Rp(_ArgTypes...)>&
1821function<_Rp(_ArgTypes...)>::operator=(const function& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822{
1823 function(__f).swap(*this);
1824 return *this;
1825}
1826
Howard Hinnantc834c512011-11-29 18:15:50 +00001827template<class _Rp, class ..._ArgTypes>
1828function<_Rp(_ArgTypes...)>&
1829function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830{
Volodymyr Sapsai1f2c57d2018-04-25 23:38:41 +00001831 *this = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 if (__f.__f_ == 0)
1833 __f_ = 0;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001834 else if ((void *)__f.__f_ == &__f.__buf_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001836 __f_ = __as_base(&__buf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 __f.__f_->__clone(__f_);
1838 }
1839 else
1840 {
1841 __f_ = __f.__f_;
1842 __f.__f_ = 0;
1843 }
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00001844 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845}
1846
Howard Hinnantc834c512011-11-29 18:15:50 +00001847template<class _Rp, class ..._ArgTypes>
1848function<_Rp(_ArgTypes...)>&
1849function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850{
Volodymyr Sapsai1f2c57d2018-04-25 23:38:41 +00001851 __base* __t = __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 __f_ = 0;
Volodymyr Sapsai1f2c57d2018-04-25 23:38:41 +00001853 if ((void *)__t == &__buf_)
1854 __t->destroy();
1855 else if (__t)
1856 __t->destroy_deallocate();
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00001857 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858}
1859
Howard Hinnantc834c512011-11-29 18:15:50 +00001860template<class _Rp, class ..._ArgTypes>
Eric Fiselier43c04f72017-09-10 23:41:20 +00001861template <class _Fp, class>
1862function<_Rp(_ArgTypes...)>&
Howard Hinnantc834c512011-11-29 18:15:50 +00001863function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864{
Howard Hinnantc834c512011-11-29 18:15:50 +00001865 function(_VSTD::forward<_Fp>(__f)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 return *this;
1867}
1868
Howard Hinnantc834c512011-11-29 18:15:50 +00001869template<class _Rp, class ..._ArgTypes>
1870function<_Rp(_ArgTypes...)>::~function()
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871{
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001872 if ((void *)__f_ == &__buf_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 __f_->destroy();
1874 else if (__f_)
1875 __f_->destroy_deallocate();
1876}
1877
Howard Hinnantc834c512011-11-29 18:15:50 +00001878template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879void
Howard Hinnantc834c512011-11-29 18:15:50 +00001880function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881{
Eric Fiselierc84b3fd2016-12-29 20:03:55 +00001882 if (_VSTD::addressof(__f) == this)
1883 return;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001884 if ((void *)__f_ == &__buf_ && (void *)__f.__f_ == &__f.__buf_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 {
1886 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001887 __base* __t = __as_base(&__tempbuf);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888 __f_->__clone(__t);
1889 __f_->destroy();
1890 __f_ = 0;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001891 __f.__f_->__clone(__as_base(&__buf_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 __f.__f_->destroy();
1893 __f.__f_ = 0;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001894 __f_ = __as_base(&__buf_);
1895 __t->__clone(__as_base(&__f.__buf_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 __t->destroy();
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001897 __f.__f_ = __as_base(&__f.__buf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898 }
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001899 else if ((void *)__f_ == &__buf_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001901 __f_->__clone(__as_base(&__f.__buf_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902 __f_->destroy();
1903 __f_ = __f.__f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001904 __f.__f_ = __as_base(&__f.__buf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 }
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001906 else if ((void *)__f.__f_ == &__f.__buf_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907 {
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001908 __f.__f_->__clone(__as_base(&__buf_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 __f.__f_->destroy();
1910 __f.__f_ = __f_;
Evgeniy Stepanov076b2372016-02-10 21:53:28 +00001911 __f_ = __as_base(&__buf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912 }
1913 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001914 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915}
1916
Howard Hinnantc834c512011-11-29 18:15:50 +00001917template<class _Rp, class ..._ArgTypes>
1918_Rp
1919function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920{
1921 if (__f_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00001922 __throw_bad_function_call();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001923 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924}
1925
Howard Hinnant72f73582010-08-11 17:04:31 +00001926#ifndef _LIBCPP_NO_RTTI
1927
Howard Hinnantc834c512011-11-29 18:15:50 +00001928template<class _Rp, class ..._ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929const std::type_info&
Howard Hinnantc834c512011-11-29 18:15:50 +00001930function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931{
1932 if (__f_ == 0)
1933 return typeid(void);
1934 return __f_->target_type();
1935}
1936
Howard Hinnantc834c512011-11-29 18:15:50 +00001937template<class _Rp, class ..._ArgTypes>
1938template <typename _Tp>
1939_Tp*
1940function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941{
1942 if (__f_ == 0)
Marshall Clowbeda7142017-06-14 20:00:36 +00001943 return nullptr;
1944 return (_Tp*) const_cast<void *>(__f_->target(typeid(_Tp)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945}
1946
Howard Hinnantc834c512011-11-29 18:15:50 +00001947template<class _Rp, class ..._ArgTypes>
1948template <typename _Tp>
1949const _Tp*
1950function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951{
1952 if (__f_ == 0)
Marshall Clowbeda7142017-06-14 20:00:36 +00001953 return nullptr;
Howard Hinnantc834c512011-11-29 18:15:50 +00001954 return (const _Tp*)__f_->target(typeid(_Tp));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955}
1956
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001957#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00001958
Howard Hinnantc834c512011-11-29 18:15:50 +00001959template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960inline _LIBCPP_INLINE_VISIBILITY
1961bool
Howard Hinnantc834c512011-11-29 18:15:50 +00001962operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963
Howard Hinnantc834c512011-11-29 18:15:50 +00001964template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965inline _LIBCPP_INLINE_VISIBILITY
1966bool
Howard Hinnantc834c512011-11-29 18:15:50 +00001967operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968
Howard Hinnantc834c512011-11-29 18:15:50 +00001969template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970inline _LIBCPP_INLINE_VISIBILITY
1971bool
Howard Hinnantc834c512011-11-29 18:15:50 +00001972operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973
Howard Hinnantc834c512011-11-29 18:15:50 +00001974template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975inline _LIBCPP_INLINE_VISIBILITY
1976bool
Howard Hinnantc834c512011-11-29 18:15:50 +00001977operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978
Howard Hinnantc834c512011-11-29 18:15:50 +00001979template <class _Rp, class... _ArgTypes>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980inline _LIBCPP_INLINE_VISIBILITY
1981void
Howard Hinnantc834c512011-11-29 18:15:50 +00001982swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983{return __x.swap(__y);}
1984
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00001985#else // _LIBCPP_CXX03_LANG
Eric Fiselier2cc48332015-07-22 22:43:27 +00001986
1987#include <__functional_03>
1988
1989#endif
Eric Fiseliera6f61c62015-07-22 04:14:38 +00001990
1991////////////////////////////////////////////////////////////////////////////////
1992// BIND
1993//==============================================================================
1994
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995template<class _Tp> struct __is_bind_expression : public false_type {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001996template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1998
Marshall Clow2d2d7f12016-09-22 00:23:15 +00001999#if _LIBCPP_STD_VER > 14
2000template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002001_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002002#endif
2003
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002005template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
2007
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002008#if _LIBCPP_STD_VER > 14
2009template <class _Tp>
Marshall Clowf1bf62f2018-01-02 17:17:01 +00002010_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
Marshall Clow2d2d7f12016-09-22 00:23:15 +00002011#endif
2012
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013namespace placeholders
2014{
2015
Howard Hinnantc834c512011-11-29 18:15:50 +00002016template <int _Np> struct __ph {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002018#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierb7f51d62016-06-26 21:01:34 +00002019_LIBCPP_FUNC_VIS extern const __ph<1> _1;
2020_LIBCPP_FUNC_VIS extern const __ph<2> _2;
2021_LIBCPP_FUNC_VIS extern const __ph<3> _3;
2022_LIBCPP_FUNC_VIS extern const __ph<4> _4;
2023_LIBCPP_FUNC_VIS extern const __ph<5> _5;
2024_LIBCPP_FUNC_VIS extern const __ph<6> _6;
2025_LIBCPP_FUNC_VIS extern const __ph<7> _7;
2026_LIBCPP_FUNC_VIS extern const __ph<8> _8;
2027_LIBCPP_FUNC_VIS extern const __ph<9> _9;
2028_LIBCPP_FUNC_VIS extern const __ph<10> _10;
2029#else
Marshall Clow396b2132018-01-02 19:01:45 +00002030/* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
2031/* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
2032/* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
2033/* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
2034/* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
2035/* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
2036/* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
2037/* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
2038/* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
2039/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
Louis Dionne5e0eadd2018-08-01 02:08:59 +00002040#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041
2042} // placeholders
2043
Howard Hinnantc834c512011-11-29 18:15:50 +00002044template<int _Np>
2045struct __is_placeholder<placeholders::__ph<_Np> >
2046 : public integral_constant<int, _Np> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002048
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002049#ifndef _LIBCPP_CXX03_LANG
Eric Fiseliera6f61c62015-07-22 04:14:38 +00002050
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051template <class _Tp, class _Uj>
2052inline _LIBCPP_INLINE_VISIBILITY
2053_Tp&
2054__mu(reference_wrapper<_Tp> __t, _Uj&)
2055{
2056 return __t.get();
2057}
2058
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059template <class _Ti, class ..._Uj, size_t ..._Indx>
2060inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002061typename __invoke_of<_Ti&, _Uj...>::type
2062__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063{
Marshall Clow60e4aa72014-06-24 00:46:19 +00002064 return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065}
2066
2067template <class _Ti, class ..._Uj>
2068inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002069typename __lazy_enable_if
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070<
2071 is_bind_expression<_Ti>::value,
Eric Fiselierf3a1cea2014-12-23 05:54:34 +00002072 __invoke_of<_Ti&, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073>::type
2074__mu(_Ti& __ti, tuple<_Uj...>& __uj)
2075{
2076 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
2077 return __mu_expand(__ti, __uj, __indices());
2078}
2079
2080template <bool IsPh, class _Ti, class _Uj>
2081struct __mu_return2 {};
2082
2083template <class _Ti, class _Uj>
2084struct __mu_return2<true, _Ti, _Uj>
2085{
2086 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
2087};
2088
2089template <class _Ti, class _Uj>
2090inline _LIBCPP_INLINE_VISIBILITY
2091typename enable_if
2092<
2093 0 < is_placeholder<_Ti>::value,
2094 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
2095>::type
2096__mu(_Ti&, _Uj& __uj)
2097{
2098 const size_t _Indx = is_placeholder<_Ti>::value - 1;
Marshall Clow60e4aa72014-06-24 00:46:19 +00002099 return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100}
2101
2102template <class _Ti, class _Uj>
2103inline _LIBCPP_INLINE_VISIBILITY
2104typename enable_if
2105<
2106 !is_bind_expression<_Ti>::value &&
2107 is_placeholder<_Ti>::value == 0 &&
2108 !__is_reference_wrapper<_Ti>::value,
2109 _Ti&
2110>::type
Howard Hinnant28b24882011-12-01 20:21:04 +00002111__mu(_Ti& __ti, _Uj&)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112{
2113 return __ti;
2114}
2115
Howard Hinnant0415d792011-05-22 15:07:43 +00002116template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
2117 class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002118struct __mu_return_impl;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002120template <bool _Invokable, class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002121struct __mu_return_invokable // false
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002122{
2123 typedef __nat type;
2124};
2125
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002127struct __mu_return_invokable<true, _Ti, _Uj...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002129 typedef typename __invoke_of<_Ti&, _Uj...>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130};
2131
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002132template <class _Ti, class ..._Uj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002133struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
2134 : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
Howard Hinnant51dae7a2013-06-30 19:48:15 +00002135{
2136};
2137
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002139struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140{
2141 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
2142 _TupleUj>::type&& type;
2143};
2144
2145template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002146struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
Howard Hinnant0415d792011-05-22 15:07:43 +00002147{
2148 typedef typename _Ti::type& type;
2149};
2150
2151template <class _Ti, class _TupleUj>
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002152struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153{
2154 typedef _Ti& type;
2155};
2156
2157template <class _Ti, class _TupleUj>
2158struct __mu_return
Louis Dionnecbbd7b12018-09-23 16:44:50 +00002159 : public __mu_return_impl<_Ti,
2160 __is_reference_wrapper<_Ti>::value,
2161 is_bind_expression<_Ti>::value,
2162 0 < is_placeholder<_Ti>::value &&
2163 is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
2164 _TupleUj>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165{
2166};
2167
Howard Hinnantc834c512011-11-29 18:15:50 +00002168template <class _Fp, class _BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002169struct __is_valid_bind_return
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002170{
2171 static const bool value = false;
2172};
2173
2174template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002175struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002176{
2177 static const bool value = __invokable<_Fp,
2178 typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
2179};
2180
2181template <class _Fp, class ..._BoundArgs, class _TupleUj>
Eric Fiselier99fffba2015-05-19 22:27:18 +00002182struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002183{
2184 static const bool value = __invokable<_Fp,
2185 typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
2186};
2187
2188template <class _Fp, class _BoundArgs, class _TupleUj,
Eric Fiselier99fffba2015-05-19 22:27:18 +00002189 bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190struct __bind_return;
2191
Howard Hinnantc834c512011-11-29 18:15:50 +00002192template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002193struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002195 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002197 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198 typename __mu_return
2199 <
2200 _BoundArgs,
2201 _TupleUj
2202 >::type...
2203 >::type type;
2204};
2205
Howard Hinnantc834c512011-11-29 18:15:50 +00002206template <class _Fp, class ..._BoundArgs, class _TupleUj>
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002207struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208{
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002209 typedef typename __invoke_of
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210 <
Howard Hinnantc834c512011-11-29 18:15:50 +00002211 _Fp&,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 typename __mu_return
2213 <
2214 const _BoundArgs,
2215 _TupleUj
2216 >::type...
2217 >::type type;
2218};
2219
Howard Hinnantc834c512011-11-29 18:15:50 +00002220template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002222typename __bind_return<_Fp, _BoundArgs, _Args>::type
2223__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224 _Args&& __args)
2225{
Eric Fiselier17264eb2017-05-03 21:02:19 +00002226 return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227}
2228
Howard Hinnantc834c512011-11-29 18:15:50 +00002229template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230class __bind
Howard Hinnantc834c512011-11-29 18:15:50 +00002231 : public __weak_result_type<typename decay<_Fp>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232{
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002233protected:
Howard Hinnantc834c512011-11-29 18:15:50 +00002234 typedef typename decay<_Fp>::type _Fd;
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002235 typedef tuple<typename decay<_BoundArgs>::type...> _Td;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002236private:
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002237 _Fd __f_;
2238 _Td __bound_args_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239
2240 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2241public:
Howard Hinnant0337ade2012-05-04 17:21:02 +00002242 template <class _Gp, class ..._BA,
2243 class = typename enable_if
2244 <
Howard Hinnantf292a922013-07-01 00:01:51 +00002245 is_constructible<_Fd, _Gp>::value &&
2246 !is_same<typename remove_reference<_Gp>::type,
2247 __bind>::value
Howard Hinnant0337ade2012-05-04 17:21:02 +00002248 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002250 explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2251 : __f_(_VSTD::forward<_Gp>(__f)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002252 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253
2254 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc1132eb2011-05-19 19:41:47 +00002256 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 operator()(_Args&& ...__args)
2258 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002259 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002260 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 }
2262
2263 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002265 typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 operator()(_Args&& ...__args) const
2267 {
Eric Fiselier17264eb2017-05-03 21:02:19 +00002268 return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002269 tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 }
2271};
2272
Howard Hinnantc834c512011-11-29 18:15:50 +00002273template<class _Fp, class ..._BoundArgs>
2274struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275
Howard Hinnantc834c512011-11-29 18:15:50 +00002276template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277class __bind_r
Howard Hinnantc834c512011-11-29 18:15:50 +00002278 : public __bind<_Fp, _BoundArgs...>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279{
Howard Hinnantc834c512011-11-29 18:15:50 +00002280 typedef __bind<_Fp, _BoundArgs...> base;
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002281 typedef typename base::_Fd _Fd;
2282 typedef typename base::_Td _Td;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283public:
Howard Hinnantc834c512011-11-29 18:15:50 +00002284 typedef _Rp result_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285
Howard Hinnant7091e652011-07-02 18:22:36 +00002286
Howard Hinnantf292a922013-07-01 00:01:51 +00002287 template <class _Gp, class ..._BA,
2288 class = typename enable_if
2289 <
2290 is_constructible<_Fd, _Gp>::value &&
2291 !is_same<typename remove_reference<_Gp>::type,
2292 __bind_r>::value
2293 >::type>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002295 explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2296 : base(_VSTD::forward<_Gp>(__f),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002297 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002298
2299 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002301 typename enable_if
2302 <
2303 is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002304 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002305 result_type
2306 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307 operator()(_Args&& ...__args)
2308 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002309 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2310 return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311 }
2312
2313 template <class ..._Args>
Howard Hinnant4ff57432010-09-21 22:55:27 +00002314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002315 typename enable_if
2316 <
2317 is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002318 result_type>::value || is_void<_Rp>::value,
Howard Hinnantde3a5f02013-02-21 18:16:55 +00002319 result_type
2320 >::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 operator()(_Args&& ...__args) const
2322 {
Eric Fiselier7a8710a2015-07-10 23:29:18 +00002323 typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2324 return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325 }
2326};
2327
Howard Hinnantc834c512011-11-29 18:15:50 +00002328template<class _Rp, class _Fp, class ..._BoundArgs>
2329struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330
Howard Hinnantc834c512011-11-29 18:15:50 +00002331template<class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002332inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002333__bind<_Fp, _BoundArgs...>
2334bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002335{
Howard Hinnantc834c512011-11-29 18:15:50 +00002336 typedef __bind<_Fp, _BoundArgs...> type;
2337 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338}
2339
Howard Hinnantc834c512011-11-29 18:15:50 +00002340template<class _Rp, class _Fp, class ..._BoundArgs>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002342__bind_r<_Rp, _Fp, _BoundArgs...>
2343bind(_Fp&& __f, _BoundArgs&&... __bound_args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344{
Howard Hinnantc834c512011-11-29 18:15:50 +00002345 typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
2346 return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347}
2348
Eric Fiseliera9ae7a62017-04-19 01:28:47 +00002349#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350
Eric Fiselier0d974f12015-07-14 20:16:15 +00002351#if _LIBCPP_STD_VER > 14
Eric Fiselier934f63b2016-06-02 01:25:41 +00002352
Eric Fiselier0d974f12015-07-14 20:16:15 +00002353template <class _Fn, class ..._Args>
2354result_of_t<_Fn&&(_Args&&...)>
Eric Fiselier934f63b2016-06-02 01:25:41 +00002355invoke(_Fn&& __f, _Args&&... __args)
2356 noexcept(noexcept(_VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...)))
2357{
2358 return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
Eric Fiselier0d974f12015-07-14 20:16:15 +00002359}
Eric Fiselier934f63b2016-06-02 01:25:41 +00002360
2361template <class _DecayFunc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002362class _LIBCPP_TEMPLATE_VIS __not_fn_imp {
Eric Fiselier934f63b2016-06-02 01:25:41 +00002363 _DecayFunc __fd;
2364
2365public:
2366 __not_fn_imp() = delete;
2367
2368 template <class ..._Args>
2369 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00002370 auto operator()(_Args&& ...__args) &
Eric Fiselier934f63b2016-06-02 01:25:41 +00002371 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002372 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2373 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00002374
2375 template <class ..._Args>
2376 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliere8303a32016-06-27 00:40:41 +00002377 auto operator()(_Args&& ...__args) &&
2378 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002379 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
2380 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00002381
2382 template <class ..._Args>
2383 _LIBCPP_INLINE_VISIBILITY
2384 auto operator()(_Args&& ...__args) const&
Eric Fiselier934f63b2016-06-02 01:25:41 +00002385 noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002386 -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
2387 { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
Eric Fiselier934f63b2016-06-02 01:25:41 +00002388
Eric Fiseliere8303a32016-06-27 00:40:41 +00002389
2390 template <class ..._Args>
2391 _LIBCPP_INLINE_VISIBILITY
2392 auto operator()(_Args&& ...__args) const&&
2393 noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
Marshall Clowdb7095c2016-10-10 14:37:18 +00002394 -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
2395 { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
Eric Fiseliere8303a32016-06-27 00:40:41 +00002396
Eric Fiselier934f63b2016-06-02 01:25:41 +00002397private:
2398 template <class _RawFunc,
2399 class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
2400 _LIBCPP_INLINE_VISIBILITY
2401 explicit __not_fn_imp(_RawFunc&& __rf)
2402 : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
2403
2404 template <class _RawFunc>
2405 friend inline _LIBCPP_INLINE_VISIBILITY
2406 __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
2407};
2408
2409template <class _RawFunc>
2410inline _LIBCPP_INLINE_VISIBILITY
2411__not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
2412 return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
2413}
2414
Eric Fiselier0d974f12015-07-14 20:16:15 +00002415#endif
2416
Howard Hinnant36b31ae2010-06-03 16:42:57 +00002417// struct hash<T*> in <memory>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418
Marshall Clowa40686b2018-01-08 19:18:00 +00002419template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00002420pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowa40686b2018-01-08 19:18:00 +00002421__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2422 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
2423 forward_iterator_tag, forward_iterator_tag)
2424{
2425 if (__first2 == __last2)
2426 return make_pair(__first1, __first1); // Everything matches an empty sequence
2427 while (true)
2428 {
2429 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
2430 while (true)
2431 {
2432 if (__first1 == __last1) // return __last1 if no element matches *__first2
2433 return make_pair(__last1, __last1);
2434 if (__pred(*__first1, *__first2))
2435 break;
2436 ++__first1;
2437 }
2438 // *__first1 matches *__first2, now match elements after here
2439 _ForwardIterator1 __m1 = __first1;
2440 _ForwardIterator2 __m2 = __first2;
2441 while (true)
2442 {
2443 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
2444 return make_pair(__first1, __m1);
2445 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
2446 return make_pair(__last1, __last1);
2447 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
2448 {
2449 ++__first1;
2450 break;
2451 } // else there is a match, check next elements
2452 }
2453 }
2454}
2455
2456template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
2457_LIBCPP_CONSTEXPR_AFTER_CXX11
2458pair<_RandomAccessIterator1, _RandomAccessIterator1>
2459__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
2460 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
2461 random_access_iterator_tag, random_access_iterator_tag)
2462{
2463 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
2464 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
2465 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
2466 const _D2 __len2 = __last2 - __first2;
2467 if (__len2 == 0)
2468 return make_pair(__first1, __first1);
2469 const _D1 __len1 = __last1 - __first1;
2470 if (__len1 < __len2)
2471 return make_pair(__last1, __last1);
2472 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
2473
2474 while (true)
2475 {
2476 while (true)
2477 {
2478 if (__first1 == __s)
2479 return make_pair(__last1, __last1);
2480 if (__pred(*__first1, *__first2))
2481 break;
2482 ++__first1;
2483 }
2484
2485 _RandomAccessIterator1 __m1 = __first1;
2486 _RandomAccessIterator2 __m2 = __first2;
2487 while (true)
2488 {
2489 if (++__m2 == __last2)
2490 return make_pair(__first1, __first1 + __len2);
2491 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
2492 if (!__pred(*__m1, *__m2))
2493 {
2494 ++__first1;
2495 break;
2496 }
2497 }
2498 }
2499}
2500
2501#if _LIBCPP_STD_VER > 14
2502
2503// default searcher
2504template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Dimitry Andricfd3633d2018-02-13 17:40:59 +00002505class _LIBCPP_TYPE_VIS default_searcher {
Marshall Clowa40686b2018-01-08 19:18:00 +00002506public:
2507 _LIBCPP_INLINE_VISIBILITY
Louis Dionne44bcff92018-08-03 22:36:53 +00002508 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clowa40686b2018-01-08 19:18:00 +00002509 _BinaryPredicate __p = _BinaryPredicate())
2510 : __first_(__f), __last_(__l), __pred_(__p) {}
2511
2512 template <typename _ForwardIterator2>
2513 _LIBCPP_INLINE_VISIBILITY
2514 pair<_ForwardIterator2, _ForwardIterator2>
2515 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
2516 {
2517 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
2518 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
2519 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
2520 }
2521
2522private:
2523 _ForwardIterator __first_;
2524 _ForwardIterator __last_;
2525 _BinaryPredicate __pred_;
2526 };
2527
2528#endif // _LIBCPP_STD_VER > 14
2529
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530_LIBCPP_END_NAMESPACE_STD
2531
2532#endif // _LIBCPP_FUNCTIONAL