blob: 5d30ce2b7502f7b93d5af575901eb3dd7ce3cf2a [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL_03
12#define _LIBCPP_FUNCTIONAL_03
13
14// manual variadic expansion for <functional>
15
Howard Hinnant08e17472011-10-17 20:05:10 +000016#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000017#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000018#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
20template <class _Tp>
21class __mem_fn
22 : public __weak_result_type<_Tp>
23{
24public:
25 // types
26 typedef _Tp type;
27private:
28 type __f_;
29
30public:
31 _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
32
33 // invoke
34
35 typename __invoke_return<type>::type
36 operator() ()
37 {
38 return __invoke(__f_);
39 }
40
41 template <class _A0>
42 typename __invoke_return0<type, _A0>::type
43 operator() (_A0& __a0)
44 {
45 return __invoke(__f_, __a0);
46 }
47
48 template <class _A0, class _A1>
49 typename __invoke_return1<type, _A0, _A1>::type
50 operator() (_A0& __a0, _A1& __a1)
51 {
52 return __invoke(__f_, __a0, __a1);
53 }
54
55 template <class _A0, class _A1, class _A2>
56 typename __invoke_return2<type, _A0, _A1, _A2>::type
57 operator() (_A0& __a0, _A1& __a1, _A2& __a2)
58 {
59 return __invoke(__f_, __a0, __a1, __a2);
60 }
61};
62
63template<class _R, class _T>
64inline _LIBCPP_INLINE_VISIBILITY
65__mem_fn<_R _T::*>
66mem_fn(_R _T::* __pm)
67{
68 return __mem_fn<_R _T::*>(__pm);
69}
70
71template<class _R, class _T>
72inline _LIBCPP_INLINE_VISIBILITY
73__mem_fn<_R (_T::*)()>
74mem_fn(_R (_T::* __pm)())
75{
76 return __mem_fn<_R (_T::*)()>(__pm);
77}
78
79template<class _R, class _T, class _A0>
80inline _LIBCPP_INLINE_VISIBILITY
81__mem_fn<_R (_T::*)(_A0)>
82mem_fn(_R (_T::* __pm)(_A0))
83{
84 return __mem_fn<_R (_T::*)(_A0)>(__pm);
85}
86
87template<class _R, class _T, class _A0, class _A1>
88inline _LIBCPP_INLINE_VISIBILITY
89__mem_fn<_R (_T::*)(_A0, _A1)>
90mem_fn(_R (_T::* __pm)(_A0, _A1))
91{
92 return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
93}
94
95template<class _R, class _T, class _A0, class _A1, class _A2>
96inline _LIBCPP_INLINE_VISIBILITY
97__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
98mem_fn(_R (_T::* __pm)(_A0, _A1, _A2))
99{
100 return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
101}
102
103template<class _R, class _T>
104inline _LIBCPP_INLINE_VISIBILITY
105__mem_fn<_R (_T::*)()>
106mem_fn(_R (_T::* __pm)() const)
107{
108 return __mem_fn<_R (_T::*)()>(__pm);
109}
110
111template<class _R, class _T, class _A0>
112inline _LIBCPP_INLINE_VISIBILITY
113__mem_fn<_R (_T::*)(_A0)>
114mem_fn(_R (_T::* __pm)(_A0) const)
115{
116 return __mem_fn<_R (_T::*)(_A0)>(__pm);
117}
118
119template<class _R, class _T, class _A0, class _A1>
120inline _LIBCPP_INLINE_VISIBILITY
121__mem_fn<_R (_T::*)(_A0, _A1)>
122mem_fn(_R (_T::* __pm)(_A0, _A1) const)
123{
124 return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
125}
126
127template<class _R, class _T, class _A0, class _A1, class _A2>
128inline _LIBCPP_INLINE_VISIBILITY
129__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
130mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const)
131{
132 return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
133}
134
135template<class _R, class _T>
136inline _LIBCPP_INLINE_VISIBILITY
137__mem_fn<_R (_T::*)()>
138mem_fn(_R (_T::* __pm)() volatile)
139{
140 return __mem_fn<_R (_T::*)()>(__pm);
141}
142
143template<class _R, class _T, class _A0>
144inline _LIBCPP_INLINE_VISIBILITY
145__mem_fn<_R (_T::*)(_A0)>
146mem_fn(_R (_T::* __pm)(_A0) volatile)
147{
148 return __mem_fn<_R (_T::*)(_A0)>(__pm);
149}
150
151template<class _R, class _T, class _A0, class _A1>
152inline _LIBCPP_INLINE_VISIBILITY
153__mem_fn<_R (_T::*)(_A0, _A1)>
154mem_fn(_R (_T::* __pm)(_A0, _A1) volatile)
155{
156 return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
157}
158
159template<class _R, class _T, class _A0, class _A1, class _A2>
160inline _LIBCPP_INLINE_VISIBILITY
161__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
162mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) volatile)
163{
164 return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
165}
166
167template<class _R, class _T>
168inline _LIBCPP_INLINE_VISIBILITY
169__mem_fn<_R (_T::*)()>
170mem_fn(_R (_T::* __pm)() const volatile)
171{
172 return __mem_fn<_R (_T::*)()>(__pm);
173}
174
175template<class _R, class _T, class _A0>
176inline _LIBCPP_INLINE_VISIBILITY
177__mem_fn<_R (_T::*)(_A0)>
178mem_fn(_R (_T::* __pm)(_A0) const volatile)
179{
180 return __mem_fn<_R (_T::*)(_A0)>(__pm);
181}
182
183template<class _R, class _T, class _A0, class _A1>
184inline _LIBCPP_INLINE_VISIBILITY
185__mem_fn<_R (_T::*)(_A0, _A1)>
186mem_fn(_R (_T::* __pm)(_A0, _A1) const volatile)
187{
188 return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
189}
190
191template<class _R, class _T, class _A0, class _A1, class _A2>
192inline _LIBCPP_INLINE_VISIBILITY
193__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
194mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const volatile)
195{
196 return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
197}
198
199// bad_function_call
200
Howard Hinnant99acc502010-09-21 17:32:39 +0000201class _LIBCPP_EXCEPTION_ABI bad_function_call
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 : public exception
203{
204};
205
Howard Hinnant99acc502010-09-21 17:32:39 +0000206template<class _Fp> class _LIBCPP_VISIBLE function; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
208namespace __function
209{
210
211template<class _F>
212struct __maybe_derive_from_unary_function
213{
214};
215
216template<class _R, class _A1>
217struct __maybe_derive_from_unary_function<_R(_A1)>
218 : public unary_function<_A1, _R>
219{
220};
221
222template<class _F>
223struct __maybe_derive_from_binary_function
224{
225};
226
227template<class _R, class _A1, class _A2>
228struct __maybe_derive_from_binary_function<_R(_A1, _A2)>
229 : public binary_function<_A1, _A2, _R>
230{
231};
232
233template<class _Fp> class __base;
234
235template<class _R>
236class __base<_R()>
237{
238 __base(const __base&);
239 __base& operator=(const __base&);
240public:
241 __base() {}
242 virtual ~__base() {}
243 virtual __base* __clone() const = 0;
244 virtual void __clone(__base*) const = 0;
245 virtual void destroy() = 0;
246 virtual void destroy_deallocate() = 0;
247 virtual _R operator()() = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000248#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 virtual const void* target(const type_info&) const = 0;
250 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000251#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252};
253
254template<class _R, class _A0>
255class __base<_R(_A0)>
256{
257 __base(const __base&);
258 __base& operator=(const __base&);
259public:
260 __base() {}
261 virtual ~__base() {}
262 virtual __base* __clone() const = 0;
263 virtual void __clone(__base*) const = 0;
264 virtual void destroy() = 0;
265 virtual void destroy_deallocate() = 0;
266 virtual _R operator()(_A0) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000267#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268 virtual const void* target(const type_info&) const = 0;
269 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000270#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271};
272
273template<class _R, class _A0, class _A1>
274class __base<_R(_A0, _A1)>
275{
276 __base(const __base&);
277 __base& operator=(const __base&);
278public:
279 __base() {}
280 virtual ~__base() {}
281 virtual __base* __clone() const = 0;
282 virtual void __clone(__base*) const = 0;
283 virtual void destroy() = 0;
284 virtual void destroy_deallocate() = 0;
285 virtual _R operator()(_A0, _A1) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000286#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000287 virtual const void* target(const type_info&) const = 0;
288 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000289#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290};
291
292template<class _R, class _A0, class _A1, class _A2>
293class __base<_R(_A0, _A1, _A2)>
294{
295 __base(const __base&);
296 __base& operator=(const __base&);
297public:
298 __base() {}
299 virtual ~__base() {}
300 virtual __base* __clone() const = 0;
301 virtual void __clone(__base*) const = 0;
302 virtual void destroy() = 0;
303 virtual void destroy_deallocate() = 0;
304 virtual _R operator()(_A0, _A1, _A2) = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000305#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 virtual const void* target(const type_info&) const = 0;
307 virtual const std::type_info& target_type() const = 0;
Howard Hinnant324bb032010-08-22 00:02:43 +0000308#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309};
310
311template<class _FD, class _Alloc, class _FB> class __func;
312
313template<class _F, class _Alloc, class _R>
314class __func<_F, _Alloc, _R()>
315 : public __base<_R()>
316{
317 __compressed_pair<_F, _Alloc> __f_;
318public:
Howard Hinnant0949eed2011-06-30 21:18:19 +0000319 explicit __func(_F __f) : __f_(_VSTD::move(__f)) {}
320 explicit __func(_F __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321 virtual __base<_R()>* __clone() const;
322 virtual void __clone(__base<_R()>*) const;
323 virtual void destroy();
324 virtual void destroy_deallocate();
325 virtual _R operator()();
Howard Hinnantd4444702010-08-11 17:04:31 +0000326#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327 virtual const void* target(const type_info&) const;
328 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000329#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330};
331
332template<class _F, class _Alloc, class _R>
333__base<_R()>*
334__func<_F, _Alloc, _R()>::__clone() const
335{
336 typedef typename _Alloc::template rebind<__func>::other _A;
337 _A __a(__f_.second());
338 typedef __allocator_destructor<_A> _D;
339 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
340 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
341 return __hold.release();
342}
343
344template<class _F, class _Alloc, class _R>
345void
346__func<_F, _Alloc, _R()>::__clone(__base<_R()>* __p) const
347{
348 ::new (__p) __func(__f_.first(), __f_.second());
349}
350
351template<class _F, class _Alloc, class _R>
352void
353__func<_F, _Alloc, _R()>::destroy()
354{
355 __f_.~__compressed_pair<_F, _Alloc>();
356}
357
358template<class _F, class _Alloc, class _R>
359void
360__func<_F, _Alloc, _R()>::destroy_deallocate()
361{
362 typedef typename _Alloc::template rebind<__func>::other _A;
363 _A __a(__f_.second());
364 __f_.~__compressed_pair<_F, _Alloc>();
365 __a.deallocate(this, 1);
366}
367
368template<class _F, class _Alloc, class _R>
369_R
370__func<_F, _Alloc, _R()>::operator()()
371{
Howard Hinnant72552802010-08-20 19:36:46 +0000372 return __invoke(__f_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373}
374
Howard Hinnantd4444702010-08-11 17:04:31 +0000375#ifndef _LIBCPP_NO_RTTI
376
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377template<class _F, class _Alloc, class _R>
378const void*
379__func<_F, _Alloc, _R()>::target(const type_info& __ti) const
380{
381 if (__ti == typeid(_F))
382 return &__f_.first();
383 return (const void*)0;
384}
385
386template<class _F, class _Alloc, class _R>
387const std::type_info&
388__func<_F, _Alloc, _R()>::target_type() const
389{
390 return typeid(_F);
391}
392
Howard Hinnant324bb032010-08-22 00:02:43 +0000393#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000394
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395template<class _F, class _Alloc, class _R, class _A0>
396class __func<_F, _Alloc, _R(_A0)>
397 : public __base<_R(_A0)>
398{
399 __compressed_pair<_F, _Alloc> __f_;
400public:
Howard Hinnant0949eed2011-06-30 21:18:19 +0000401 _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000402 _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000403 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 virtual __base<_R(_A0)>* __clone() const;
405 virtual void __clone(__base<_R(_A0)>*) const;
406 virtual void destroy();
407 virtual void destroy_deallocate();
408 virtual _R operator()(_A0);
Howard Hinnantd4444702010-08-11 17:04:31 +0000409#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410 virtual const void* target(const type_info&) const;
411 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000412#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000413};
414
415template<class _F, class _Alloc, class _R, class _A0>
416__base<_R(_A0)>*
417__func<_F, _Alloc, _R(_A0)>::__clone() const
418{
419 typedef typename _Alloc::template rebind<__func>::other _A;
420 _A __a(__f_.second());
421 typedef __allocator_destructor<_A> _D;
422 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
423 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
424 return __hold.release();
425}
426
427template<class _F, class _Alloc, class _R, class _A0>
428void
429__func<_F, _Alloc, _R(_A0)>::__clone(__base<_R(_A0)>* __p) const
430{
431 ::new (__p) __func(__f_.first(), __f_.second());
432}
433
434template<class _F, class _Alloc, class _R, class _A0>
435void
436__func<_F, _Alloc, _R(_A0)>::destroy()
437{
438 __f_.~__compressed_pair<_F, _Alloc>();
439}
440
441template<class _F, class _Alloc, class _R, class _A0>
442void
443__func<_F, _Alloc, _R(_A0)>::destroy_deallocate()
444{
445 typedef typename _Alloc::template rebind<__func>::other _A;
446 _A __a(__f_.second());
447 __f_.~__compressed_pair<_F, _Alloc>();
448 __a.deallocate(this, 1);
449}
450
451template<class _F, class _Alloc, class _R, class _A0>
452_R
453__func<_F, _Alloc, _R(_A0)>::operator()(_A0 __a0)
454{
455 return __invoke(__f_.first(), __a0);
456}
457
Howard Hinnantd4444702010-08-11 17:04:31 +0000458#ifndef _LIBCPP_NO_RTTI
459
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000460template<class _F, class _Alloc, class _R, class _A0>
461const void*
462__func<_F, _Alloc, _R(_A0)>::target(const type_info& __ti) const
463{
464 if (__ti == typeid(_F))
465 return &__f_.first();
466 return (const void*)0;
467}
468
469template<class _F, class _Alloc, class _R, class _A0>
470const std::type_info&
471__func<_F, _Alloc, _R(_A0)>::target_type() const
472{
473 return typeid(_F);
474}
475
Howard Hinnant324bb032010-08-22 00:02:43 +0000476#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000477
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478template<class _F, class _Alloc, class _R, class _A0, class _A1>
479class __func<_F, _Alloc, _R(_A0, _A1)>
480 : public __base<_R(_A0, _A1)>
481{
482 __compressed_pair<_F, _Alloc> __f_;
483public:
Howard Hinnant0949eed2011-06-30 21:18:19 +0000484 _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000485 _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000486 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487 virtual __base<_R(_A0, _A1)>* __clone() const;
488 virtual void __clone(__base<_R(_A0, _A1)>*) const;
489 virtual void destroy();
490 virtual void destroy_deallocate();
491 virtual _R operator()(_A0, _A1);
Howard Hinnantd4444702010-08-11 17:04:31 +0000492#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000493 virtual const void* target(const type_info&) const;
494 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000495#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496};
497
498template<class _F, class _Alloc, class _R, class _A0, class _A1>
499__base<_R(_A0, _A1)>*
500__func<_F, _Alloc, _R(_A0, _A1)>::__clone() const
501{
502 typedef typename _Alloc::template rebind<__func>::other _A;
503 _A __a(__f_.second());
504 typedef __allocator_destructor<_A> _D;
505 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
506 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
507 return __hold.release();
508}
509
510template<class _F, class _Alloc, class _R, class _A0, class _A1>
511void
512__func<_F, _Alloc, _R(_A0, _A1)>::__clone(__base<_R(_A0, _A1)>* __p) const
513{
514 ::new (__p) __func(__f_.first(), __f_.second());
515}
516
517template<class _F, class _Alloc, class _R, class _A0, class _A1>
518void
519__func<_F, _Alloc, _R(_A0, _A1)>::destroy()
520{
521 __f_.~__compressed_pair<_F, _Alloc>();
522}
523
524template<class _F, class _Alloc, class _R, class _A0, class _A1>
525void
526__func<_F, _Alloc, _R(_A0, _A1)>::destroy_deallocate()
527{
528 typedef typename _Alloc::template rebind<__func>::other _A;
529 _A __a(__f_.second());
530 __f_.~__compressed_pair<_F, _Alloc>();
531 __a.deallocate(this, 1);
532}
533
534template<class _F, class _Alloc, class _R, class _A0, class _A1>
535_R
536__func<_F, _Alloc, _R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
537{
538 return __invoke(__f_.first(), __a0, __a1);
539}
540
Howard Hinnantd4444702010-08-11 17:04:31 +0000541#ifndef _LIBCPP_NO_RTTI
542
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543template<class _F, class _Alloc, class _R, class _A0, class _A1>
544const void*
545__func<_F, _Alloc, _R(_A0, _A1)>::target(const type_info& __ti) const
546{
547 if (__ti == typeid(_F))
548 return &__f_.first();
549 return (const void*)0;
550}
551
552template<class _F, class _Alloc, class _R, class _A0, class _A1>
553const std::type_info&
554__func<_F, _Alloc, _R(_A0, _A1)>::target_type() const
555{
556 return typeid(_F);
557}
558
Howard Hinnant324bb032010-08-22 00:02:43 +0000559#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000560
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
562class __func<_F, _Alloc, _R(_A0, _A1, _A2)>
563 : public __base<_R(_A0, _A1, _A2)>
564{
565 __compressed_pair<_F, _Alloc> __f_;
566public:
Howard Hinnant0949eed2011-06-30 21:18:19 +0000567 _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant99acc502010-09-21 17:32:39 +0000568 _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000569 : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 virtual __base<_R(_A0, _A1, _A2)>* __clone() const;
571 virtual void __clone(__base<_R(_A0, _A1, _A2)>*) const;
572 virtual void destroy();
573 virtual void destroy_deallocate();
574 virtual _R operator()(_A0, _A1, _A2);
Howard Hinnantd4444702010-08-11 17:04:31 +0000575#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576 virtual const void* target(const type_info&) const;
577 virtual const std::type_info& target_type() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000578#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579};
580
581template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
582__base<_R(_A0, _A1, _A2)>*
583__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone() const
584{
585 typedef typename _Alloc::template rebind<__func>::other _A;
586 _A __a(__f_.second());
587 typedef __allocator_destructor<_A> _D;
588 unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
589 ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
590 return __hold.release();
591}
592
593template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
594void
595__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone(__base<_R(_A0, _A1, _A2)>* __p) const
596{
597 ::new (__p) __func(__f_.first(), __f_.second());
598}
599
600template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
601void
602__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy()
603{
604 __f_.~__compressed_pair<_F, _Alloc>();
605}
606
607template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
608void
609__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy_deallocate()
610{
611 typedef typename _Alloc::template rebind<__func>::other _A;
612 _A __a(__f_.second());
613 __f_.~__compressed_pair<_F, _Alloc>();
614 __a.deallocate(this, 1);
615}
616
617template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
618_R
619__func<_F, _Alloc, _R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
620{
621 return __invoke(__f_.first(), __a0, __a1, __a2);
622}
623
Howard Hinnantd4444702010-08-11 17:04:31 +0000624#ifndef _LIBCPP_NO_RTTI
625
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
627const void*
628__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target(const type_info& __ti) const
629{
630 if (__ti == typeid(_F))
631 return &__f_.first();
632 return (const void*)0;
633}
634
635template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
636const std::type_info&
637__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target_type() const
638{
639 return typeid(_F);
640}
641
Howard Hinnant324bb032010-08-22 00:02:43 +0000642#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000643
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644} // __function
645
646template<class _R>
Howard Hinnant99acc502010-09-21 17:32:39 +0000647class _LIBCPP_VISIBLE function<_R()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648{
649 typedef __function::__base<_R()> __base;
650 aligned_storage<3*sizeof(void*)>::type __buf_;
651 __base* __f_;
652
653 template <class _F>
654 static bool __not_null(const _F&) {return true;}
655 template <class _R2>
656 static bool __not_null(const function<_R()>& __p) {return __p;}
657public:
658 typedef _R result_type;
659
660 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000661 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
662 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663 function(const function&);
664 template<class _F>
665 function(_F,
666 typename enable_if<!is_integral<_F>::value>::type* = 0);
667
Howard Hinnant72552802010-08-20 19:36:46 +0000668 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000670 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
671 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000673 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
674 template<class _Alloc>
675 function(allocator_arg_t, const _Alloc&, const function&);
676 template<class _F, class _Alloc>
677 function(allocator_arg_t, const _Alloc& __a, _F __f,
678 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679
680 function& operator=(const function&);
681 function& operator=(nullptr_t);
682 template<class _F>
683 typename enable_if
684 <
685 !is_integral<_F>::value,
686 function&
687 >::type
688 operator=(_F);
689
690 ~function();
691
692 // 20.7.16.2.2, function modifiers:
693 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +0000694 template<class _F, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000696 void assign(_F __f, const _Alloc& __a)
697 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698
699 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +0000700 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701
702private:
703 // deleted overloads close possible hole in the type system
704 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000705 bool operator==(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706 template<class _R2>
Howard Hinnant99acc502010-09-21 17:32:39 +0000707 bool operator!=(const function<_R2()>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708public:
709 // 20.7.16.2.4, function invocation:
710 _R operator()() const;
711
Howard Hinnantd4444702010-08-11 17:04:31 +0000712#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 // 20.7.16.2.5, function target access:
714 const std::type_info& target_type() const;
715 template <typename _T> _T* target();
716 template <typename _T> const _T* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +0000717#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718};
719
720template<class _R>
721function<_R()>::function(const function& __f)
722{
723 if (__f.__f_ == 0)
724 __f_ = 0;
725 else if (__f.__f_ == (const __base*)&__f.__buf_)
726 {
727 __f_ = (__base*)&__buf_;
728 __f.__f_->__clone(__f_);
729 }
730 else
731 __f_ = __f.__f_->__clone();
732}
733
734template<class _R>
Howard Hinnant72552802010-08-20 19:36:46 +0000735template<class _Alloc>
736function<_R()>::function(allocator_arg_t, const _Alloc&, const function& __f)
737{
738 if (__f.__f_ == 0)
739 __f_ = 0;
740 else if (__f.__f_ == (const __base*)&__f.__buf_)
741 {
742 __f_ = (__base*)&__buf_;
743 __f.__f_->__clone(__f_);
744 }
745 else
746 __f_ = __f.__f_->__clone();
747}
748
749template<class _R>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750template <class _F>
751function<_R()>::function(_F __f,
752 typename enable_if<!is_integral<_F>::value>::type*)
753 : __f_(0)
754{
755 if (__not_null(__f))
756 {
757 typedef __function::__func<_F, allocator<_F>, _R()> _FF;
758 if (sizeof(_FF) <= sizeof(__buf_))
759 {
760 __f_ = (__base*)&__buf_;
761 ::new (__f_) _FF(__f);
762 }
763 else
764 {
765 typedef allocator<_FF> _A;
766 _A __a;
767 typedef __allocator_destructor<_A> _D;
768 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
769 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
770 __f_ = __hold.release();
771 }
772 }
773}
774
775template<class _R>
Howard Hinnant72552802010-08-20 19:36:46 +0000776template <class _F, class _Alloc>
777function<_R()>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
778 typename enable_if<!is_integral<_F>::value>::type*)
779 : __f_(0)
780{
781 typedef allocator_traits<_Alloc> __alloc_traits;
782 if (__not_null(__f))
783 {
784 typedef __function::__func<_F, _Alloc, _R()> _FF;
785 if (sizeof(_FF) <= sizeof(__buf_))
786 {
787 __f_ = (__base*)&__buf_;
788 ::new (__f_) _FF(__f);
789 }
790 else
791 {
792 typedef typename __alloc_traits::template
793#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794 rebind_alloc<_FF>
795#else
796 rebind_alloc<_FF>::other
797#endif
798 _A;
799 _A __a(__a0);
800 typedef __allocator_destructor<_A> _D;
801 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
802 ::new (__hold.get()) _FF(__f, _Alloc(__a));
803 __f_ = __hold.release();
804 }
805 }
806}
807
808template<class _R>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000809function<_R()>&
810function<_R()>::operator=(const function& __f)
811{
812 function(__f).swap(*this);
813 return *this;
814}
815
816template<class _R>
817function<_R()>&
818function<_R()>::operator=(nullptr_t)
819{
820 if (__f_ == (__base*)&__buf_)
821 __f_->destroy();
822 else if (__f_)
823 __f_->destroy_deallocate();
824 __f_ = 0;
825}
826
827template<class _R>
828template <class _F>
829typename enable_if
830<
831 !is_integral<_F>::value,
832 function<_R()>&
833>::type
834function<_R()>::operator=(_F __f)
835{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000836 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837 return *this;
838}
839
840template<class _R>
841function<_R()>::~function()
842{
843 if (__f_ == (__base*)&__buf_)
844 __f_->destroy();
845 else if (__f_)
846 __f_->destroy_deallocate();
847}
848
849template<class _R>
850void
851function<_R()>::swap(function& __f)
852{
853 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
854 {
855 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
856 __base* __t = (__base*)&__tempbuf;
857 __f_->__clone(__t);
858 __f_->destroy();
859 __f_ = 0;
860 __f.__f_->__clone((__base*)&__buf_);
861 __f.__f_->destroy();
862 __f.__f_ = 0;
863 __f_ = (__base*)&__buf_;
864 __t->__clone((__base*)&__f.__buf_);
865 __t->destroy();
866 __f.__f_ = (__base*)&__f.__buf_;
867 }
868 else if (__f_ == (__base*)&__buf_)
869 {
870 __f_->__clone((__base*)&__f.__buf_);
871 __f_->destroy();
872 __f_ = __f.__f_;
873 __f.__f_ = (__base*)&__f.__buf_;
874 }
875 else if (__f.__f_ == (__base*)&__f.__buf_)
876 {
877 __f.__f_->__clone((__base*)&__buf_);
878 __f.__f_->destroy();
879 __f.__f_ = __f_;
880 __f_ = (__base*)&__buf_;
881 }
882 else
Howard Hinnant0949eed2011-06-30 21:18:19 +0000883 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884}
885
886template<class _R>
887_R
888function<_R()>::operator()() const
889{
Howard Hinnantd4444702010-08-11 17:04:31 +0000890#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891 if (__f_ == 0)
892 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +0000893#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894 return (*__f_)();
895}
896
Howard Hinnantd4444702010-08-11 17:04:31 +0000897#ifndef _LIBCPP_NO_RTTI
898
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899template<class _R>
900const std::type_info&
901function<_R()>::target_type() const
902{
903 if (__f_ == 0)
904 return typeid(void);
905 return __f_->target_type();
906}
907
908template<class _R>
909template <typename _T>
910_T*
911function<_R()>::target()
912{
913 if (__f_ == 0)
914 return (_T*)0;
915 return (_T*)__f_->target(typeid(_T));
916}
917
918template<class _R>
919template <typename _T>
920const _T*
921function<_R()>::target() const
922{
923 if (__f_ == 0)
924 return (const _T*)0;
925 return (const _T*)__f_->target(typeid(_T));
926}
927
Howard Hinnant324bb032010-08-22 00:02:43 +0000928#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +0000929
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930template<class _R, class _A0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000931class _LIBCPP_VISIBLE function<_R(_A0)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932 : public unary_function<_A0, _R>
933{
934 typedef __function::__base<_R(_A0)> __base;
935 aligned_storage<3*sizeof(void*)>::type __buf_;
936 __base* __f_;
937
938 template <class _F>
Howard Hinnant99acc502010-09-21 17:32:39 +0000939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 static bool __not_null(const _F&) {return true;}
941 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943 static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
944 template <class _R2, class _C>
Howard Hinnant99acc502010-09-21 17:32:39 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946 static bool __not_null(_R2 (_C::*__p)()) {return __p;}
947 template <class _R2, class _C>
Howard Hinnant99acc502010-09-21 17:32:39 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949 static bool __not_null(_R2 (_C::*__p)() const) {return __p;}
950 template <class _R2, class _C>
Howard Hinnant99acc502010-09-21 17:32:39 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000952 static bool __not_null(_R2 (_C::*__p)() volatile) {return __p;}
953 template <class _R2, class _C>
Howard Hinnant99acc502010-09-21 17:32:39 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955 static bool __not_null(_R2 (_C::*__p)() const volatile) {return __p;}
956 template <class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958 static bool __not_null(const function<_R(_B0)>& __p) {return __p;}
959public:
960 typedef _R result_type;
961
962 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +0000963 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
964 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 function(const function&);
966 template<class _F>
967 function(_F,
968 typename enable_if<!is_integral<_F>::value>::type* = 0);
969
Howard Hinnant72552802010-08-20 19:36:46 +0000970 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000972 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
973 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000975 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
976 template<class _Alloc>
977 function(allocator_arg_t, const _Alloc&, const function&);
978 template<class _F, class _Alloc>
979 function(allocator_arg_t, const _Alloc& __a, _F __f,
980 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981
982 function& operator=(const function&);
983 function& operator=(nullptr_t);
984 template<class _F>
985 typename enable_if
986 <
987 !is_integral<_F>::value,
988 function&
989 >::type
990 operator=(_F);
991
992 ~function();
993
994 // 20.7.16.2.2, function modifiers:
995 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +0000996 template<class _F, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +0000997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +0000998 void assign(_F __f, const _Alloc& __a)
999 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000
1001 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001002 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003
1004private:
1005 // deleted overloads close possible hole in the type system
1006 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001007 bool operator==(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008 template<class _R2, class _B0>
Howard Hinnant99acc502010-09-21 17:32:39 +00001009 bool operator!=(const function<_R2(_B0)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010public:
1011 // 20.7.16.2.4, function invocation:
1012 _R operator()(_A0) const;
1013
Howard Hinnantd4444702010-08-11 17:04:31 +00001014#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 // 20.7.16.2.5, function target access:
1016 const std::type_info& target_type() const;
1017 template <typename _T> _T* target();
1018 template <typename _T> const _T* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001019#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020};
1021
1022template<class _R, class _A0>
1023function<_R(_A0)>::function(const function& __f)
1024{
1025 if (__f.__f_ == 0)
1026 __f_ = 0;
1027 else if (__f.__f_ == (const __base*)&__f.__buf_)
1028 {
1029 __f_ = (__base*)&__buf_;
1030 __f.__f_->__clone(__f_);
1031 }
1032 else
1033 __f_ = __f.__f_->__clone();
1034}
1035
1036template<class _R, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001037template<class _Alloc>
1038function<_R(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1039{
1040 if (__f.__f_ == 0)
1041 __f_ = 0;
1042 else if (__f.__f_ == (const __base*)&__f.__buf_)
1043 {
1044 __f_ = (__base*)&__buf_;
1045 __f.__f_->__clone(__f_);
1046 }
1047 else
1048 __f_ = __f.__f_->__clone();
1049}
1050
1051template<class _R, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052template <class _F>
1053function<_R(_A0)>::function(_F __f,
1054 typename enable_if<!is_integral<_F>::value>::type*)
1055 : __f_(0)
1056{
1057 if (__not_null(__f))
1058 {
1059 typedef __function::__func<_F, allocator<_F>, _R(_A0)> _FF;
1060 if (sizeof(_FF) <= sizeof(__buf_))
1061 {
1062 __f_ = (__base*)&__buf_;
1063 ::new (__f_) _FF(__f);
1064 }
1065 else
1066 {
1067 typedef allocator<_FF> _A;
1068 _A __a;
1069 typedef __allocator_destructor<_A> _D;
1070 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1071 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1072 __f_ = __hold.release();
1073 }
1074 }
1075}
1076
1077template<class _R, class _A0>
Howard Hinnant72552802010-08-20 19:36:46 +00001078template <class _F, class _Alloc>
1079function<_R(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1080 typename enable_if<!is_integral<_F>::value>::type*)
1081 : __f_(0)
1082{
1083 typedef allocator_traits<_Alloc> __alloc_traits;
1084 if (__not_null(__f))
1085 {
1086 typedef __function::__func<_F, _Alloc, _R(_A0)> _FF;
1087 if (sizeof(_FF) <= sizeof(__buf_))
1088 {
1089 __f_ = (__base*)&__buf_;
1090 ::new (__f_) _FF(__f);
1091 }
1092 else
1093 {
1094 typedef typename __alloc_traits::template
1095#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1096 rebind_alloc<_FF>
1097#else
1098 rebind_alloc<_FF>::other
1099#endif
1100 _A;
1101 _A __a(__a0);
1102 typedef __allocator_destructor<_A> _D;
1103 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1104 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1105 __f_ = __hold.release();
1106 }
1107 }
1108}
1109
1110template<class _R, class _A0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111function<_R(_A0)>&
1112function<_R(_A0)>::operator=(const function& __f)
1113{
1114 function(__f).swap(*this);
1115 return *this;
1116}
1117
1118template<class _R, class _A0>
1119function<_R(_A0)>&
1120function<_R(_A0)>::operator=(nullptr_t)
1121{
1122 if (__f_ == (__base*)&__buf_)
1123 __f_->destroy();
1124 else if (__f_)
1125 __f_->destroy_deallocate();
1126 __f_ = 0;
1127}
1128
1129template<class _R, class _A0>
1130template <class _F>
1131typename enable_if
1132<
1133 !is_integral<_F>::value,
1134 function<_R(_A0)>&
1135>::type
1136function<_R(_A0)>::operator=(_F __f)
1137{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001138 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139 return *this;
1140}
1141
1142template<class _R, class _A0>
1143function<_R(_A0)>::~function()
1144{
1145 if (__f_ == (__base*)&__buf_)
1146 __f_->destroy();
1147 else if (__f_)
1148 __f_->destroy_deallocate();
1149}
1150
1151template<class _R, class _A0>
1152void
1153function<_R(_A0)>::swap(function& __f)
1154{
1155 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1156 {
1157 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1158 __base* __t = (__base*)&__tempbuf;
1159 __f_->__clone(__t);
1160 __f_->destroy();
1161 __f_ = 0;
1162 __f.__f_->__clone((__base*)&__buf_);
1163 __f.__f_->destroy();
1164 __f.__f_ = 0;
1165 __f_ = (__base*)&__buf_;
1166 __t->__clone((__base*)&__f.__buf_);
1167 __t->destroy();
1168 __f.__f_ = (__base*)&__f.__buf_;
1169 }
1170 else if (__f_ == (__base*)&__buf_)
1171 {
1172 __f_->__clone((__base*)&__f.__buf_);
1173 __f_->destroy();
1174 __f_ = __f.__f_;
1175 __f.__f_ = (__base*)&__f.__buf_;
1176 }
1177 else if (__f.__f_ == (__base*)&__f.__buf_)
1178 {
1179 __f.__f_->__clone((__base*)&__buf_);
1180 __f.__f_->destroy();
1181 __f.__f_ = __f_;
1182 __f_ = (__base*)&__buf_;
1183 }
1184 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001185 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186}
1187
1188template<class _R, class _A0>
1189_R
1190function<_R(_A0)>::operator()(_A0 __a0) const
1191{
Howard Hinnantd4444702010-08-11 17:04:31 +00001192#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 if (__f_ == 0)
1194 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001195#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196 return (*__f_)(__a0);
1197}
1198
Howard Hinnantd4444702010-08-11 17:04:31 +00001199#ifndef _LIBCPP_NO_RTTI
1200
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201template<class _R, class _A0>
1202const std::type_info&
1203function<_R(_A0)>::target_type() const
1204{
1205 if (__f_ == 0)
1206 return typeid(void);
1207 return __f_->target_type();
1208}
1209
1210template<class _R, class _A0>
1211template <typename _T>
1212_T*
1213function<_R(_A0)>::target()
1214{
1215 if (__f_ == 0)
1216 return (_T*)0;
1217 return (_T*)__f_->target(typeid(_T));
1218}
1219
1220template<class _R, class _A0>
1221template <typename _T>
1222const _T*
1223function<_R(_A0)>::target() const
1224{
1225 if (__f_ == 0)
1226 return (const _T*)0;
1227 return (const _T*)__f_->target(typeid(_T));
1228}
1229
Howard Hinnant324bb032010-08-22 00:02:43 +00001230#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001231
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232template<class _R, class _A0, class _A1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001233class _LIBCPP_VISIBLE function<_R(_A0, _A1)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234 : public binary_function<_A0, _A1, _R>
1235{
1236 typedef __function::__base<_R(_A0, _A1)> __base;
1237 aligned_storage<3*sizeof(void*)>::type __buf_;
1238 __base* __f_;
1239
1240 template <class _F>
Howard Hinnant99acc502010-09-21 17:32:39 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242 static bool __not_null(const _F&) {return true;}
1243 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1246 template <class _R2, class _C, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248 static bool __not_null(_R2 (_C::*__p)(_B1)) {return __p;}
1249 template <class _R2, class _C, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001251 static bool __not_null(_R2 (_C::*__p)(_B1) const) {return __p;}
1252 template <class _R2, class _C, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254 static bool __not_null(_R2 (_C::*__p)(_B1) volatile) {return __p;}
1255 template <class _R2, class _C, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001257 static bool __not_null(_R2 (_C::*__p)(_B1) const volatile) {return __p;}
1258 template <class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260 static bool __not_null(const function<_R(_B0, _B1)>& __p) {return __p;}
1261public:
1262 typedef _R result_type;
1263
1264 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001265 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1266 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267 function(const function&);
1268 template<class _F>
1269 function(_F,
1270 typename enable_if<!is_integral<_F>::value>::type* = 0);
1271
Howard Hinnant72552802010-08-20 19:36:46 +00001272 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001274 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1275 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001277 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1278 template<class _Alloc>
1279 function(allocator_arg_t, const _Alloc&, const function&);
1280 template<class _F, class _Alloc>
1281 function(allocator_arg_t, const _Alloc& __a, _F __f,
1282 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283
1284 function& operator=(const function&);
1285 function& operator=(nullptr_t);
1286 template<class _F>
1287 typename enable_if
1288 <
1289 !is_integral<_F>::value,
1290 function&
1291 >::type
1292 operator=(_F);
1293
1294 ~function();
1295
1296 // 20.7.16.2.2, function modifiers:
1297 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +00001298 template<class _F, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001300 void assign(_F __f, const _Alloc& __a)
1301 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302
1303 // 20.7.16.2.3, function capacity:
1304 operator bool() const {return __f_;}
1305
1306private:
1307 // deleted overloads close possible hole in the type system
1308 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001309 bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 template<class _R2, class _B0, class _B1>
Howard Hinnant99acc502010-09-21 17:32:39 +00001311 bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312public:
1313 // 20.7.16.2.4, function invocation:
1314 _R operator()(_A0, _A1) const;
1315
Howard Hinnantd4444702010-08-11 17:04:31 +00001316#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 // 20.7.16.2.5, function target access:
1318 const std::type_info& target_type() const;
1319 template <typename _T> _T* target();
1320 template <typename _T> const _T* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001321#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322};
1323
1324template<class _R, class _A0, class _A1>
1325function<_R(_A0, _A1)>::function(const function& __f)
1326{
1327 if (__f.__f_ == 0)
1328 __f_ = 0;
1329 else if (__f.__f_ == (const __base*)&__f.__buf_)
1330 {
1331 __f_ = (__base*)&__buf_;
1332 __f.__f_->__clone(__f_);
1333 }
1334 else
1335 __f_ = __f.__f_->__clone();
1336}
1337
1338template<class _R, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001339template<class _Alloc>
1340function<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1341{
1342 if (__f.__f_ == 0)
1343 __f_ = 0;
1344 else if (__f.__f_ == (const __base*)&__f.__buf_)
1345 {
1346 __f_ = (__base*)&__buf_;
1347 __f.__f_->__clone(__f_);
1348 }
1349 else
1350 __f_ = __f.__f_->__clone();
1351}
1352
1353template<class _R, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354template <class _F>
1355function<_R(_A0, _A1)>::function(_F __f,
Howard Hinnant72552802010-08-20 19:36:46 +00001356 typename enable_if<!is_integral<_F>::value>::type*)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 : __f_(0)
1358{
1359 if (__not_null(__f))
1360 {
1361 typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1)> _FF;
1362 if (sizeof(_FF) <= sizeof(__buf_))
1363 {
1364 __f_ = (__base*)&__buf_;
1365 ::new (__f_) _FF(__f);
1366 }
1367 else
1368 {
1369 typedef allocator<_FF> _A;
1370 _A __a;
1371 typedef __allocator_destructor<_A> _D;
1372 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1373 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1374 __f_ = __hold.release();
1375 }
1376 }
1377}
1378
1379template<class _R, class _A0, class _A1>
Howard Hinnant72552802010-08-20 19:36:46 +00001380template <class _F, class _Alloc>
1381function<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1382 typename enable_if<!is_integral<_F>::value>::type*)
1383 : __f_(0)
1384{
1385 typedef allocator_traits<_Alloc> __alloc_traits;
1386 if (__not_null(__f))
1387 {
1388 typedef __function::__func<_F, _Alloc, _R(_A0, _A1)> _FF;
1389 if (sizeof(_FF) <= sizeof(__buf_))
1390 {
1391 __f_ = (__base*)&__buf_;
1392 ::new (__f_) _FF(__f);
1393 }
1394 else
1395 {
1396 typedef typename __alloc_traits::template
1397#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1398 rebind_alloc<_FF>
1399#else
1400 rebind_alloc<_FF>::other
1401#endif
1402 _A;
1403 _A __a(__a0);
1404 typedef __allocator_destructor<_A> _D;
1405 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1406 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1407 __f_ = __hold.release();
1408 }
1409 }
1410}
1411
1412template<class _R, class _A0, class _A1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413function<_R(_A0, _A1)>&
1414function<_R(_A0, _A1)>::operator=(const function& __f)
1415{
1416 function(__f).swap(*this);
1417 return *this;
1418}
1419
1420template<class _R, class _A0, class _A1>
1421function<_R(_A0, _A1)>&
1422function<_R(_A0, _A1)>::operator=(nullptr_t)
1423{
1424 if (__f_ == (__base*)&__buf_)
1425 __f_->destroy();
1426 else if (__f_)
1427 __f_->destroy_deallocate();
1428 __f_ = 0;
1429}
1430
1431template<class _R, class _A0, class _A1>
1432template <class _F>
1433typename enable_if
1434<
1435 !is_integral<_F>::value,
1436 function<_R(_A0, _A1)>&
1437>::type
1438function<_R(_A0, _A1)>::operator=(_F __f)
1439{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001440 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 return *this;
1442}
1443
1444template<class _R, class _A0, class _A1>
1445function<_R(_A0, _A1)>::~function()
1446{
1447 if (__f_ == (__base*)&__buf_)
1448 __f_->destroy();
1449 else if (__f_)
1450 __f_->destroy_deallocate();
1451}
1452
1453template<class _R, class _A0, class _A1>
1454void
1455function<_R(_A0, _A1)>::swap(function& __f)
1456{
1457 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1458 {
1459 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1460 __base* __t = (__base*)&__tempbuf;
1461 __f_->__clone(__t);
1462 __f_->destroy();
1463 __f_ = 0;
1464 __f.__f_->__clone((__base*)&__buf_);
1465 __f.__f_->destroy();
1466 __f.__f_ = 0;
1467 __f_ = (__base*)&__buf_;
1468 __t->__clone((__base*)&__f.__buf_);
1469 __t->destroy();
1470 __f.__f_ = (__base*)&__f.__buf_;
1471 }
1472 else if (__f_ == (__base*)&__buf_)
1473 {
1474 __f_->__clone((__base*)&__f.__buf_);
1475 __f_->destroy();
1476 __f_ = __f.__f_;
1477 __f.__f_ = (__base*)&__f.__buf_;
1478 }
1479 else if (__f.__f_ == (__base*)&__f.__buf_)
1480 {
1481 __f.__f_->__clone((__base*)&__buf_);
1482 __f.__f_->destroy();
1483 __f.__f_ = __f_;
1484 __f_ = (__base*)&__buf_;
1485 }
1486 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001487 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488}
1489
1490template<class _R, class _A0, class _A1>
1491_R
1492function<_R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1493{
Howard Hinnantd4444702010-08-11 17:04:31 +00001494#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495 if (__f_ == 0)
1496 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001497#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 return (*__f_)(__a0, __a1);
1499}
1500
Howard Hinnantd4444702010-08-11 17:04:31 +00001501#ifndef _LIBCPP_NO_RTTI
1502
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503template<class _R, class _A0, class _A1>
1504const std::type_info&
1505function<_R(_A0, _A1)>::target_type() const
1506{
1507 if (__f_ == 0)
1508 return typeid(void);
1509 return __f_->target_type();
1510}
1511
1512template<class _R, class _A0, class _A1>
1513template <typename _T>
1514_T*
1515function<_R(_A0, _A1)>::target()
1516{
1517 if (__f_ == 0)
1518 return (_T*)0;
1519 return (_T*)__f_->target(typeid(_T));
1520}
1521
1522template<class _R, class _A0, class _A1>
1523template <typename _T>
1524const _T*
1525function<_R(_A0, _A1)>::target() const
1526{
1527 if (__f_ == 0)
1528 return (const _T*)0;
1529 return (const _T*)__f_->target(typeid(_T));
1530}
1531
Howard Hinnant324bb032010-08-22 00:02:43 +00001532#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001533
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534template<class _R, class _A0, class _A1, class _A2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001535class _LIBCPP_VISIBLE function<_R(_A0, _A1, _A2)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536{
1537 typedef __function::__base<_R(_A0, _A1, _A2)> __base;
1538 aligned_storage<3*sizeof(void*)>::type __buf_;
1539 __base* __f_;
1540
1541 template <class _F>
Howard Hinnant99acc502010-09-21 17:32:39 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 static bool __not_null(const _F&) {return true;}
1544 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1547 template <class _R2, class _C, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001549 static bool __not_null(_R2 (_C::*__p)(_B1, _B2)) {return __p;}
1550 template <class _R2, class _C, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const) {return __p;}
1553 template <class _R2, class _C, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) volatile) {return __p;}
1556 template <class _R2, class _C, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const volatile) {return __p;}
1559 template <class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 static bool __not_null(const function<_R(_B0, _B1, _B2)>& __p) {return __p;}
1562public:
1563 typedef _R result_type;
1564
1565 // 20.7.16.2.1, construct/copy/destroy:
Howard Hinnant99acc502010-09-21 17:32:39 +00001566 _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1567 _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568 function(const function&);
1569 template<class _F>
1570 function(_F,
1571 typename enable_if<!is_integral<_F>::value>::type* = 0);
1572
Howard Hinnant72552802010-08-20 19:36:46 +00001573 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001575 function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1576 template<class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001578 function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1579 template<class _Alloc>
1580 function(allocator_arg_t, const _Alloc&, const function&);
1581 template<class _F, class _Alloc>
1582 function(allocator_arg_t, const _Alloc& __a, _F __f,
1583 typename enable_if<!is_integral<_F>::value>::type* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584
1585 function& operator=(const function&);
1586 function& operator=(nullptr_t);
1587 template<class _F>
1588 typename enable_if
1589 <
1590 !is_integral<_F>::value,
1591 function&
1592 >::type
1593 operator=(_F);
1594
1595 ~function();
1596
1597 // 20.7.16.2.2, function modifiers:
1598 void swap(function&);
Howard Hinnant72552802010-08-20 19:36:46 +00001599 template<class _F, class _Alloc>
Howard Hinnant99acc502010-09-21 17:32:39 +00001600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant72552802010-08-20 19:36:46 +00001601 void assign(_F __f, const _Alloc& __a)
1602 {function(allocator_arg, __a, __f).swap(*this);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603
1604 // 20.7.16.2.3, function capacity:
Howard Hinnant99acc502010-09-21 17:32:39 +00001605 _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606
1607private:
1608 // deleted overloads close possible hole in the type system
1609 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001610 bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 template<class _R2, class _B0, class _B1, class _B2>
Howard Hinnant99acc502010-09-21 17:32:39 +00001612 bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613public:
1614 // 20.7.16.2.4, function invocation:
1615 _R operator()(_A0, _A1, _A2) const;
1616
Howard Hinnantd4444702010-08-11 17:04:31 +00001617#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 // 20.7.16.2.5, function target access:
1619 const std::type_info& target_type() const;
1620 template <typename _T> _T* target();
1621 template <typename _T> const _T* target() const;
Howard Hinnant324bb032010-08-22 00:02:43 +00001622#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623};
1624
1625template<class _R, class _A0, class _A1, class _A2>
1626function<_R(_A0, _A1, _A2)>::function(const function& __f)
1627{
1628 if (__f.__f_ == 0)
1629 __f_ = 0;
1630 else if (__f.__f_ == (const __base*)&__f.__buf_)
1631 {
1632 __f_ = (__base*)&__buf_;
1633 __f.__f_->__clone(__f_);
1634 }
1635 else
1636 __f_ = __f.__f_->__clone();
1637}
1638
1639template<class _R, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001640template<class _Alloc>
1641function<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
1642 const function& __f)
1643{
1644 if (__f.__f_ == 0)
1645 __f_ = 0;
1646 else if (__f.__f_ == (const __base*)&__f.__buf_)
1647 {
1648 __f_ = (__base*)&__buf_;
1649 __f.__f_->__clone(__f_);
1650 }
1651 else
1652 __f_ = __f.__f_->__clone();
1653}
1654
1655template<class _R, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656template <class _F>
1657function<_R(_A0, _A1, _A2)>::function(_F __f,
1658 typename enable_if<!is_integral<_F>::value>::type*)
1659 : __f_(0)
1660{
1661 if (__not_null(__f))
1662 {
1663 typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1, _A2)> _FF;
1664 if (sizeof(_FF) <= sizeof(__buf_))
1665 {
1666 __f_ = (__base*)&__buf_;
1667 ::new (__f_) _FF(__f);
1668 }
1669 else
1670 {
1671 typedef allocator<_FF> _A;
1672 _A __a;
1673 typedef __allocator_destructor<_A> _D;
1674 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1675 ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1676 __f_ = __hold.release();
1677 }
1678 }
1679}
1680
1681template<class _R, class _A0, class _A1, class _A2>
Howard Hinnant72552802010-08-20 19:36:46 +00001682template <class _F, class _Alloc>
1683function<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1684 typename enable_if<!is_integral<_F>::value>::type*)
1685 : __f_(0)
1686{
1687 typedef allocator_traits<_Alloc> __alloc_traits;
1688 if (__not_null(__f))
1689 {
1690 typedef __function::__func<_F, _Alloc, _R(_A0, _A1, _A2)> _FF;
1691 if (sizeof(_FF) <= sizeof(__buf_))
1692 {
1693 __f_ = (__base*)&__buf_;
1694 ::new (__f_) _FF(__f);
1695 }
1696 else
1697 {
1698 typedef typename __alloc_traits::template
1699#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1700 rebind_alloc<_FF>
1701#else
1702 rebind_alloc<_FF>::other
1703#endif
1704 _A;
1705 _A __a(__a0);
1706 typedef __allocator_destructor<_A> _D;
1707 unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1708 ::new (__hold.get()) _FF(__f, _Alloc(__a));
1709 __f_ = __hold.release();
1710 }
1711 }
1712}
1713
1714template<class _R, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715function<_R(_A0, _A1, _A2)>&
1716function<_R(_A0, _A1, _A2)>::operator=(const function& __f)
1717{
1718 function(__f).swap(*this);
1719 return *this;
1720}
1721
1722template<class _R, class _A0, class _A1, class _A2>
1723function<_R(_A0, _A1, _A2)>&
1724function<_R(_A0, _A1, _A2)>::operator=(nullptr_t)
1725{
1726 if (__f_ == (__base*)&__buf_)
1727 __f_->destroy();
1728 else if (__f_)
1729 __f_->destroy_deallocate();
1730 __f_ = 0;
1731}
1732
1733template<class _R, class _A0, class _A1, class _A2>
1734template <class _F>
1735typename enable_if
1736<
1737 !is_integral<_F>::value,
1738 function<_R(_A0, _A1, _A2)>&
1739>::type
1740function<_R(_A0, _A1, _A2)>::operator=(_F __f)
1741{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001742 function(_VSTD::move(__f)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 return *this;
1744}
1745
1746template<class _R, class _A0, class _A1, class _A2>
1747function<_R(_A0, _A1, _A2)>::~function()
1748{
1749 if (__f_ == (__base*)&__buf_)
1750 __f_->destroy();
1751 else if (__f_)
1752 __f_->destroy_deallocate();
1753}
1754
1755template<class _R, class _A0, class _A1, class _A2>
1756void
1757function<_R(_A0, _A1, _A2)>::swap(function& __f)
1758{
1759 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1760 {
1761 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1762 __base* __t = (__base*)&__tempbuf;
1763 __f_->__clone(__t);
1764 __f_->destroy();
1765 __f_ = 0;
1766 __f.__f_->__clone((__base*)&__buf_);
1767 __f.__f_->destroy();
1768 __f.__f_ = 0;
1769 __f_ = (__base*)&__buf_;
1770 __t->__clone((__base*)&__f.__buf_);
1771 __t->destroy();
1772 __f.__f_ = (__base*)&__f.__buf_;
1773 }
1774 else if (__f_ == (__base*)&__buf_)
1775 {
1776 __f_->__clone((__base*)&__f.__buf_);
1777 __f_->destroy();
1778 __f_ = __f.__f_;
1779 __f.__f_ = (__base*)&__f.__buf_;
1780 }
1781 else if (__f.__f_ == (__base*)&__f.__buf_)
1782 {
1783 __f.__f_->__clone((__base*)&__buf_);
1784 __f.__f_->destroy();
1785 __f.__f_ = __f_;
1786 __f_ = (__base*)&__buf_;
1787 }
1788 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00001789 _VSTD::swap(__f_, __f.__f_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790}
1791
1792template<class _R, class _A0, class _A1, class _A2>
1793_R
1794function<_R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1795{
Howard Hinnantd4444702010-08-11 17:04:31 +00001796#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 if (__f_ == 0)
1798 throw bad_function_call();
Howard Hinnant324bb032010-08-22 00:02:43 +00001799#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 return (*__f_)(__a0, __a1, __a2);
1801}
1802
Howard Hinnantd4444702010-08-11 17:04:31 +00001803#ifndef _LIBCPP_NO_RTTI
1804
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805template<class _R, class _A0, class _A1, class _A2>
1806const std::type_info&
1807function<_R(_A0, _A1, _A2)>::target_type() const
1808{
1809 if (__f_ == 0)
1810 return typeid(void);
1811 return __f_->target_type();
1812}
1813
1814template<class _R, class _A0, class _A1, class _A2>
1815template <typename _T>
1816_T*
1817function<_R(_A0, _A1, _A2)>::target()
1818{
1819 if (__f_ == 0)
1820 return (_T*)0;
1821 return (_T*)__f_->target(typeid(_T));
1822}
1823
1824template<class _R, class _A0, class _A1, class _A2>
1825template <typename _T>
1826const _T*
1827function<_R(_A0, _A1, _A2)>::target() const
1828{
1829 if (__f_ == 0)
1830 return (const _T*)0;
1831 return (const _T*)__f_->target(typeid(_T));
1832}
1833
Howard Hinnant324bb032010-08-22 00:02:43 +00001834#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00001835
Howard Hinnant324bb032010-08-22 00:02:43 +00001836template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837inline _LIBCPP_INLINE_VISIBILITY
1838bool
1839operator==(const function<_F>& __f, nullptr_t) {return !__f;}
1840
Howard Hinnant324bb032010-08-22 00:02:43 +00001841template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842inline _LIBCPP_INLINE_VISIBILITY
1843bool
1844operator==(nullptr_t, const function<_F>& __f) {return !__f;}
1845
Howard Hinnant324bb032010-08-22 00:02:43 +00001846template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847inline _LIBCPP_INLINE_VISIBILITY
1848bool
1849operator!=(const function<_F>& __f, nullptr_t) {return (bool)__f;}
1850
Howard Hinnant324bb032010-08-22 00:02:43 +00001851template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852inline _LIBCPP_INLINE_VISIBILITY
1853bool
1854operator!=(nullptr_t, const function<_F>& __f) {return (bool)__f;}
1855
Howard Hinnant324bb032010-08-22 00:02:43 +00001856template <class _F>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857inline _LIBCPP_INLINE_VISIBILITY
1858void
1859swap(function<_F>& __x, function<_F>& __y)
1860{return __x.swap(__y);}
1861
1862template<class _Tp> struct __is_bind_expression : public false_type {};
Howard Hinnant99acc502010-09-21 17:32:39 +00001863template<class _Tp> struct _LIBCPP_VISIBLE is_bind_expression
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1865
1866template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
Howard Hinnant99acc502010-09-21 17:32:39 +00001867template<class _Tp> struct _LIBCPP_VISIBLE is_placeholder
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868 : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1869
1870namespace placeholders
1871{
1872
1873template <int _N> struct __ph {};
1874
1875extern __ph<1> _1;
1876extern __ph<2> _2;
1877extern __ph<3> _3;
1878extern __ph<4> _4;
1879extern __ph<5> _5;
1880extern __ph<6> _6;
1881extern __ph<7> _7;
1882extern __ph<8> _8;
1883extern __ph<9> _9;
1884extern __ph<10> _10;
1885
1886} // placeholders
1887
1888template<int _N>
1889struct __is_placeholder<placeholders::__ph<_N> >
1890 : public integral_constant<int, _N> {};
1891
1892template <class _Tp, class _Uj>
1893inline _LIBCPP_INLINE_VISIBILITY
1894_Tp&
1895__mu(reference_wrapper<_Tp> __t, _Uj&)
1896{
1897 return __t.get();
1898}
1899/*
1900template <bool _IsBindExpr, class _Ti, class ..._Uj>
1901struct __mu_return1 {};
1902
1903template <class _Ti, class ..._Uj>
1904struct __mu_return1<true, _Ti, _Uj...>
1905{
1906 typedef typename result_of<_Ti(_Uj...)>::type type;
1907};
1908
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909template <class _Ti, class ..._Uj, size_t ..._Indx>
1910inline _LIBCPP_INLINE_VISIBILITY
1911typename __mu_return1<true, _Ti, _Uj...>::type
1912__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1913{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001914 __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915}
1916
1917template <class _Ti, class ..._Uj>
1918inline _LIBCPP_INLINE_VISIBILITY
1919typename enable_if
1920<
1921 is_bind_expression<_Ti>::value,
1922 typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1923>::type
1924__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1925{
1926 typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1927 return __mu_expand(__ti, __uj, __indices());
1928}
1929
1930template <bool IsPh, class _Ti, class _Uj>
1931struct __mu_return2 {};
1932
1933template <class _Ti, class _Uj>
1934struct __mu_return2<true, _Ti, _Uj>
1935{
1936 typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1937};
1938
1939template <class _Ti, class _Uj>
1940inline _LIBCPP_INLINE_VISIBILITY
1941typename enable_if
1942<
1943 0 < is_placeholder<_Ti>::value,
1944 typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1945>::type
1946__mu(_Ti&, _Uj& __uj)
1947{
1948 const size_t _Indx = is_placeholder<_Ti>::value - 1;
1949 // compiler bug workaround
1950 typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1951 return __t;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001952// return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953}
1954
1955template <class _Ti, class _Uj>
1956inline _LIBCPP_INLINE_VISIBILITY
1957typename enable_if
1958<
1959 !is_bind_expression<_Ti>::value &&
1960 is_placeholder<_Ti>::value == 0 &&
1961 !__is_reference_wrapper<_Ti>::value,
1962 _Ti&
1963>::type
1964__mu(_Ti& __ti, _Uj& __uj)
1965{
1966 return __ti;
1967}
1968
1969template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1970struct ____mu_return;
1971
1972template <class _Ti, class ..._Uj>
1973struct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1974{
1975 typedef typename result_of<_Ti(_Uj...)>::type type;
1976};
1977
1978template <class _Ti, class _TupleUj>
1979struct ____mu_return<_Ti, false, true, _TupleUj>
1980{
1981 typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1982 _TupleUj>::type&& type;
1983};
1984
1985template <class _Ti, class _TupleUj>
1986struct ____mu_return<_Ti, false, false, _TupleUj>
1987{
1988 typedef _Ti& type;
1989};
1990
1991template <class _Ti, class _TupleUj>
1992struct __mu_return
1993 : public ____mu_return<_Ti,
1994 is_bind_expression<_Ti>::value,
1995 0 < is_placeholder<_Ti>::value,
1996 _TupleUj>
1997{
1998};
1999
2000template <class _Ti, class _TupleUj>
2001struct __mu_return<reference_wrapper<_Ti>, _TupleUj>
2002{
2003 typedef _Ti& type;
2004};
2005
2006template <class _F, class _BoundArgs, class _TupleUj>
2007struct __bind_return;
2008
2009template <class _F, class ..._BoundArgs, class _TupleUj>
2010struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
2011{
2012 typedef typename __ref_return
2013 <
2014 _F&,
2015 typename __mu_return
2016 <
2017 _BoundArgs,
2018 _TupleUj
2019 >::type...
2020 >::type type;
2021};
2022
2023template <class _F, class ..._BoundArgs, class _TupleUj>
2024struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
2025{
2026 typedef typename __ref_return
2027 <
2028 _F&,
2029 typename __mu_return
2030 <
2031 const _BoundArgs,
2032 _TupleUj
2033 >::type...
2034 >::type type;
2035};
2036
2037template <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
2038inline _LIBCPP_INLINE_VISIBILITY
2039typename __bind_return<_F, _BoundArgs, _Args>::type
2040__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2041 _Args&& __args)
2042{
2043 return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
2044}
2045
Howard Hinnant324bb032010-08-22 00:02:43 +00002046template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047class __bind
2048{
2049 _F __f_;
2050 tuple<_BoundArgs...> __bound_args_;
2051
2052 typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2053public:
2054 template <class _G, class ..._BA>
2055 explicit __bind(_G&& __f, _BA&& ...__bound_args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002056 : __f_(_VSTD::forward<_G>(__f)),
2057 __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058
2059 template <class ..._Args>
2060 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2061 operator()(_Args&& ...__args)
2062 {
2063 // compiler bug workaround
Howard Hinnant324bb032010-08-22 00:02:43 +00002064 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065 tuple<_Args&&...>(__args...));
2066 }
2067
2068 template <class ..._Args>
2069 typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2070 operator()(_Args&& ...__args) const
2071 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002072 return __apply_functor(__f_, __bound_args_, __indices(),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073 tuple<_Args&&...>(__args...));
2074 }
2075};
2076
Howard Hinnant324bb032010-08-22 00:02:43 +00002077template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
2079
Howard Hinnant324bb032010-08-22 00:02:43 +00002080template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081class __bind_r
2082 : public __bind<_F, _BoundArgs...>
2083{
2084 typedef __bind<_F, _BoundArgs...> base;
2085public:
2086 typedef _R result_type;
2087
2088 template <class _G, class ..._BA>
2089 explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002090 : base(_VSTD::forward<_G>(__f),
2091 _VSTD::forward<_BA>(__bound_args)...) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092
2093 template <class ..._Args>
2094 result_type
2095 operator()(_Args&& ...__args)
2096 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002097 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098 }
2099
2100 template <class ..._Args>
2101 result_type
2102 operator()(_Args&& ...__args) const
2103 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002104 return base::operator()(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105 }
2106};
2107
Howard Hinnant324bb032010-08-22 00:02:43 +00002108template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
2110
Howard Hinnant324bb032010-08-22 00:02:43 +00002111template<class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112inline _LIBCPP_INLINE_VISIBILITY
2113__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
2114bind(_F&& __f, _BoundArgs&&... __bound_args)
2115{
2116 typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002117 return type(_VSTD::forward<_F>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118}
2119
Howard Hinnant324bb032010-08-22 00:02:43 +00002120template<class _R, class _F, class ..._BoundArgs>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121inline _LIBCPP_INLINE_VISIBILITY
2122__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
2123bind(_F&& __f, _BoundArgs&&... __bound_args)
2124{
2125 typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002126 return type(_VSTD::forward<_F>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127}
2128*/
2129
Howard Hinnant324bb032010-08-22 00:02:43 +00002130#endif // _LIBCPP_FUNCTIONAL_03