__functional_03 revision 227825
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===----------------------------------------------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_FUNCTIONAL_03
12227825Stheraven#define _LIBCPP_FUNCTIONAL_03
13227825Stheraven
14227825Stheraven// manual variadic expansion for <functional>
15227825Stheraven
16227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17227825Stheraven#pragma GCC system_header
18227825Stheraven#endif
19227825Stheraven
20227825Stheraventemplate <class _Tp>
21227825Stheravenclass __mem_fn
22227825Stheraven    : public __weak_result_type<_Tp>
23227825Stheraven{
24227825Stheravenpublic:
25227825Stheraven    // types
26227825Stheraven    typedef _Tp type;
27227825Stheravenprivate:
28227825Stheraven    type __f_;
29227825Stheraven
30227825Stheravenpublic:
31227825Stheraven    _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {}
32227825Stheraven
33227825Stheraven    // invoke
34227825Stheraven
35227825Stheraven    typename __invoke_return<type>::type
36227825Stheraven       operator() ()
37227825Stheraven       {
38227825Stheraven           return __invoke(__f_);
39227825Stheraven       }
40227825Stheraven
41227825Stheraven    template <class _A0>
42227825Stheraven       typename __invoke_return0<type, _A0>::type
43227825Stheraven          operator() (_A0& __a0)
44227825Stheraven          {
45227825Stheraven              return __invoke(__f_, __a0);
46227825Stheraven          }
47227825Stheraven
48227825Stheraven    template <class _A0, class _A1>
49227825Stheraven       typename __invoke_return1<type, _A0, _A1>::type
50227825Stheraven          operator() (_A0& __a0, _A1& __a1)
51227825Stheraven          {
52227825Stheraven              return __invoke(__f_, __a0, __a1);
53227825Stheraven          }
54227825Stheraven
55227825Stheraven    template <class _A0, class _A1, class _A2>
56227825Stheraven       typename __invoke_return2<type, _A0, _A1, _A2>::type
57227825Stheraven          operator() (_A0& __a0, _A1& __a1, _A2& __a2)
58227825Stheraven          {
59227825Stheraven              return __invoke(__f_, __a0, __a1, __a2);
60227825Stheraven          }
61227825Stheraven};
62227825Stheraven
63227825Stheraventemplate<class _R, class _T>
64227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
65227825Stheraven__mem_fn<_R _T::*>
66227825Stheravenmem_fn(_R _T::* __pm)
67227825Stheraven{
68227825Stheraven    return __mem_fn<_R _T::*>(__pm);
69227825Stheraven}
70227825Stheraven
71227825Stheraventemplate<class _R, class _T>
72227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
73227825Stheraven__mem_fn<_R (_T::*)()>
74227825Stheravenmem_fn(_R (_T::* __pm)())
75227825Stheraven{
76227825Stheraven    return __mem_fn<_R (_T::*)()>(__pm);
77227825Stheraven}
78227825Stheraven
79227825Stheraventemplate<class _R, class _T, class _A0>
80227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
81227825Stheraven__mem_fn<_R (_T::*)(_A0)>
82227825Stheravenmem_fn(_R (_T::* __pm)(_A0))
83227825Stheraven{
84227825Stheraven    return __mem_fn<_R (_T::*)(_A0)>(__pm);
85227825Stheraven}
86227825Stheraven
87227825Stheraventemplate<class _R, class _T, class _A0, class _A1>
88227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
89227825Stheraven__mem_fn<_R (_T::*)(_A0, _A1)>
90227825Stheravenmem_fn(_R (_T::* __pm)(_A0, _A1))
91227825Stheraven{
92227825Stheraven    return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
93227825Stheraven}
94227825Stheraven
95227825Stheraventemplate<class _R, class _T, class _A0, class _A1, class _A2>
96227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
97227825Stheraven__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
98227825Stheravenmem_fn(_R (_T::* __pm)(_A0, _A1, _A2))
99227825Stheraven{
100227825Stheraven    return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
101227825Stheraven}
102227825Stheraven
103227825Stheraventemplate<class _R, class _T>
104227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
105227825Stheraven__mem_fn<_R (_T::*)()>
106227825Stheravenmem_fn(_R (_T::* __pm)() const)
107227825Stheraven{
108227825Stheraven    return __mem_fn<_R (_T::*)()>(__pm);
109227825Stheraven}
110227825Stheraven
111227825Stheraventemplate<class _R, class _T, class _A0>
112227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
113227825Stheraven__mem_fn<_R (_T::*)(_A0)>
114227825Stheravenmem_fn(_R (_T::* __pm)(_A0) const)
115227825Stheraven{
116227825Stheraven    return __mem_fn<_R (_T::*)(_A0)>(__pm);
117227825Stheraven}
118227825Stheraven
119227825Stheraventemplate<class _R, class _T, class _A0, class _A1>
120227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
121227825Stheraven__mem_fn<_R (_T::*)(_A0, _A1)>
122227825Stheravenmem_fn(_R (_T::* __pm)(_A0, _A1) const)
123227825Stheraven{
124227825Stheraven    return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
125227825Stheraven}
126227825Stheraven
127227825Stheraventemplate<class _R, class _T, class _A0, class _A1, class _A2>
128227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
129227825Stheraven__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
130227825Stheravenmem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const)
131227825Stheraven{
132227825Stheraven    return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
133227825Stheraven}
134227825Stheraven
135227825Stheraventemplate<class _R, class _T>
136227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
137227825Stheraven__mem_fn<_R (_T::*)()>
138227825Stheravenmem_fn(_R (_T::* __pm)() volatile)
139227825Stheraven{
140227825Stheraven    return __mem_fn<_R (_T::*)()>(__pm);
141227825Stheraven}
142227825Stheraven
143227825Stheraventemplate<class _R, class _T, class _A0>
144227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
145227825Stheraven__mem_fn<_R (_T::*)(_A0)>
146227825Stheravenmem_fn(_R (_T::* __pm)(_A0) volatile)
147227825Stheraven{
148227825Stheraven    return __mem_fn<_R (_T::*)(_A0)>(__pm);
149227825Stheraven}
150227825Stheraven
151227825Stheraventemplate<class _R, class _T, class _A0, class _A1>
152227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
153227825Stheraven__mem_fn<_R (_T::*)(_A0, _A1)>
154227825Stheravenmem_fn(_R (_T::* __pm)(_A0, _A1) volatile)
155227825Stheraven{
156227825Stheraven    return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
157227825Stheraven}
158227825Stheraven
159227825Stheraventemplate<class _R, class _T, class _A0, class _A1, class _A2>
160227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
161227825Stheraven__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
162227825Stheravenmem_fn(_R (_T::* __pm)(_A0, _A1, _A2) volatile)
163227825Stheraven{
164227825Stheraven    return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
165227825Stheraven}
166227825Stheraven
167227825Stheraventemplate<class _R, class _T>
168227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
169227825Stheraven__mem_fn<_R (_T::*)()>
170227825Stheravenmem_fn(_R (_T::* __pm)() const volatile)
171227825Stheraven{
172227825Stheraven    return __mem_fn<_R (_T::*)()>(__pm);
173227825Stheraven}
174227825Stheraven
175227825Stheraventemplate<class _R, class _T, class _A0>
176227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
177227825Stheraven__mem_fn<_R (_T::*)(_A0)>
178227825Stheravenmem_fn(_R (_T::* __pm)(_A0) const volatile)
179227825Stheraven{
180227825Stheraven    return __mem_fn<_R (_T::*)(_A0)>(__pm);
181227825Stheraven}
182227825Stheraven
183227825Stheraventemplate<class _R, class _T, class _A0, class _A1>
184227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
185227825Stheraven__mem_fn<_R (_T::*)(_A0, _A1)>
186227825Stheravenmem_fn(_R (_T::* __pm)(_A0, _A1) const volatile)
187227825Stheraven{
188227825Stheraven    return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm);
189227825Stheraven}
190227825Stheraven
191227825Stheraventemplate<class _R, class _T, class _A0, class _A1, class _A2>
192227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
193227825Stheraven__mem_fn<_R (_T::*)(_A0, _A1, _A2)>
194227825Stheravenmem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const volatile)
195227825Stheraven{
196227825Stheraven    return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm);
197227825Stheraven}
198227825Stheraven
199227825Stheraven// bad_function_call
200227825Stheraven
201227825Stheravenclass _LIBCPP_EXCEPTION_ABI bad_function_call
202227825Stheraven    : public exception
203227825Stheraven{
204227825Stheraven};
205227825Stheraven
206227825Stheraventemplate<class _Fp> class _LIBCPP_VISIBLE function; // undefined
207227825Stheraven
208227825Stheravennamespace __function
209227825Stheraven{
210227825Stheraven
211227825Stheraventemplate<class _F>
212227825Stheravenstruct __maybe_derive_from_unary_function
213227825Stheraven{
214227825Stheraven};
215227825Stheraven
216227825Stheraventemplate<class _R, class _A1>
217227825Stheravenstruct __maybe_derive_from_unary_function<_R(_A1)>
218227825Stheraven    : public unary_function<_A1, _R>
219227825Stheraven{
220227825Stheraven};
221227825Stheraven
222227825Stheraventemplate<class _F>
223227825Stheravenstruct __maybe_derive_from_binary_function
224227825Stheraven{
225227825Stheraven};
226227825Stheraven
227227825Stheraventemplate<class _R, class _A1, class _A2>
228227825Stheravenstruct __maybe_derive_from_binary_function<_R(_A1, _A2)>
229227825Stheraven    : public binary_function<_A1, _A2, _R>
230227825Stheraven{
231227825Stheraven};
232227825Stheraven
233227825Stheraventemplate<class _Fp> class __base;
234227825Stheraven
235227825Stheraventemplate<class _R>
236227825Stheravenclass __base<_R()>
237227825Stheraven{
238227825Stheraven    __base(const __base&);
239227825Stheraven    __base& operator=(const __base&);
240227825Stheravenpublic:
241227825Stheraven    __base() {}
242227825Stheraven    virtual ~__base() {}
243227825Stheraven    virtual __base* __clone() const = 0;
244227825Stheraven    virtual void __clone(__base*) const = 0;
245227825Stheraven    virtual void destroy() = 0;
246227825Stheraven    virtual void destroy_deallocate() = 0;
247227825Stheraven    virtual _R operator()() = 0;
248227825Stheraven#ifndef _LIBCPP_NO_RTTI
249227825Stheraven    virtual const void* target(const type_info&) const = 0;
250227825Stheraven    virtual const std::type_info& target_type() const = 0;
251227825Stheraven#endif  // _LIBCPP_NO_RTTI
252227825Stheraven};
253227825Stheraven
254227825Stheraventemplate<class _R, class _A0>
255227825Stheravenclass __base<_R(_A0)>
256227825Stheraven{
257227825Stheraven    __base(const __base&);
258227825Stheraven    __base& operator=(const __base&);
259227825Stheravenpublic:
260227825Stheraven    __base() {}
261227825Stheraven    virtual ~__base() {}
262227825Stheraven    virtual __base* __clone() const = 0;
263227825Stheraven    virtual void __clone(__base*) const = 0;
264227825Stheraven    virtual void destroy() = 0;
265227825Stheraven    virtual void destroy_deallocate() = 0;
266227825Stheraven    virtual _R operator()(_A0) = 0;
267227825Stheraven#ifndef _LIBCPP_NO_RTTI
268227825Stheraven    virtual const void* target(const type_info&) const = 0;
269227825Stheraven    virtual const std::type_info& target_type() const = 0;
270227825Stheraven#endif  // _LIBCPP_NO_RTTI
271227825Stheraven};
272227825Stheraven
273227825Stheraventemplate<class _R, class _A0, class _A1>
274227825Stheravenclass __base<_R(_A0, _A1)>
275227825Stheraven{
276227825Stheraven    __base(const __base&);
277227825Stheraven    __base& operator=(const __base&);
278227825Stheravenpublic:
279227825Stheraven    __base() {}
280227825Stheraven    virtual ~__base() {}
281227825Stheraven    virtual __base* __clone() const = 0;
282227825Stheraven    virtual void __clone(__base*) const = 0;
283227825Stheraven    virtual void destroy() = 0;
284227825Stheraven    virtual void destroy_deallocate() = 0;
285227825Stheraven    virtual _R operator()(_A0, _A1) = 0;
286227825Stheraven#ifndef _LIBCPP_NO_RTTI
287227825Stheraven    virtual const void* target(const type_info&) const = 0;
288227825Stheraven    virtual const std::type_info& target_type() const = 0;
289227825Stheraven#endif  // _LIBCPP_NO_RTTI
290227825Stheraven};
291227825Stheraven
292227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
293227825Stheravenclass __base<_R(_A0, _A1, _A2)>
294227825Stheraven{
295227825Stheraven    __base(const __base&);
296227825Stheraven    __base& operator=(const __base&);
297227825Stheravenpublic:
298227825Stheraven    __base() {}
299227825Stheraven    virtual ~__base() {}
300227825Stheraven    virtual __base* __clone() const = 0;
301227825Stheraven    virtual void __clone(__base*) const = 0;
302227825Stheraven    virtual void destroy() = 0;
303227825Stheraven    virtual void destroy_deallocate() = 0;
304227825Stheraven    virtual _R operator()(_A0, _A1, _A2) = 0;
305227825Stheraven#ifndef _LIBCPP_NO_RTTI
306227825Stheraven    virtual const void* target(const type_info&) const = 0;
307227825Stheraven    virtual const std::type_info& target_type() const = 0;
308227825Stheraven#endif  // _LIBCPP_NO_RTTI
309227825Stheraven};
310227825Stheraven
311227825Stheraventemplate<class _FD, class _Alloc, class _FB> class __func;
312227825Stheraven
313227825Stheraventemplate<class _F, class _Alloc, class _R>
314227825Stheravenclass __func<_F, _Alloc, _R()>
315227825Stheraven    : public  __base<_R()>
316227825Stheraven{
317227825Stheraven    __compressed_pair<_F, _Alloc> __f_;
318227825Stheravenpublic:
319227825Stheraven    explicit __func(_F __f) : __f_(_VSTD::move(__f)) {}
320227825Stheraven    explicit __func(_F __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
321227825Stheraven    virtual __base<_R()>* __clone() const;
322227825Stheraven    virtual void __clone(__base<_R()>*) const;
323227825Stheraven    virtual void destroy();
324227825Stheraven    virtual void destroy_deallocate();
325227825Stheraven    virtual _R operator()();
326227825Stheraven#ifndef _LIBCPP_NO_RTTI
327227825Stheraven    virtual const void* target(const type_info&) const;
328227825Stheraven    virtual const std::type_info& target_type() const;
329227825Stheraven#endif  // _LIBCPP_NO_RTTI
330227825Stheraven};
331227825Stheraven
332227825Stheraventemplate<class _F, class _Alloc, class _R>
333227825Stheraven__base<_R()>*
334227825Stheraven__func<_F, _Alloc, _R()>::__clone() const
335227825Stheraven{
336227825Stheraven    typedef typename _Alloc::template rebind<__func>::other _A;
337227825Stheraven    _A __a(__f_.second());
338227825Stheraven    typedef __allocator_destructor<_A> _D;
339227825Stheraven    unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
340227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
341227825Stheraven    return __hold.release();
342227825Stheraven}
343227825Stheraven
344227825Stheraventemplate<class _F, class _Alloc, class _R>
345227825Stheravenvoid
346227825Stheraven__func<_F, _Alloc, _R()>::__clone(__base<_R()>* __p) const
347227825Stheraven{
348227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
349227825Stheraven}
350227825Stheraven
351227825Stheraventemplate<class _F, class _Alloc, class _R>
352227825Stheravenvoid
353227825Stheraven__func<_F, _Alloc, _R()>::destroy()
354227825Stheraven{
355227825Stheraven    __f_.~__compressed_pair<_F, _Alloc>();
356227825Stheraven}
357227825Stheraven
358227825Stheraventemplate<class _F, class _Alloc, class _R>
359227825Stheravenvoid
360227825Stheraven__func<_F, _Alloc, _R()>::destroy_deallocate()
361227825Stheraven{
362227825Stheraven    typedef typename _Alloc::template rebind<__func>::other _A;
363227825Stheraven    _A __a(__f_.second());
364227825Stheraven    __f_.~__compressed_pair<_F, _Alloc>();
365227825Stheraven    __a.deallocate(this, 1);
366227825Stheraven}
367227825Stheraven
368227825Stheraventemplate<class _F, class _Alloc, class _R>
369227825Stheraven_R
370227825Stheraven__func<_F, _Alloc, _R()>::operator()()
371227825Stheraven{
372227825Stheraven    return __invoke(__f_.first());
373227825Stheraven}
374227825Stheraven
375227825Stheraven#ifndef _LIBCPP_NO_RTTI
376227825Stheraven
377227825Stheraventemplate<class _F, class _Alloc, class _R>
378227825Stheravenconst void*
379227825Stheraven__func<_F, _Alloc, _R()>::target(const type_info& __ti) const
380227825Stheraven{
381227825Stheraven    if (__ti == typeid(_F))
382227825Stheraven        return &__f_.first();
383227825Stheraven    return (const void*)0;
384227825Stheraven}
385227825Stheraven
386227825Stheraventemplate<class _F, class _Alloc, class _R>
387227825Stheravenconst std::type_info&
388227825Stheraven__func<_F, _Alloc, _R()>::target_type() const
389227825Stheraven{
390227825Stheraven    return typeid(_F);
391227825Stheraven}
392227825Stheraven
393227825Stheraven#endif  // _LIBCPP_NO_RTTI
394227825Stheraven
395227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0>
396227825Stheravenclass __func<_F, _Alloc, _R(_A0)>
397227825Stheraven    : public  __base<_R(_A0)>
398227825Stheraven{
399227825Stheraven    __compressed_pair<_F, _Alloc> __f_;
400227825Stheravenpublic:
401227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {}
402227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a)
403227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
404227825Stheraven    virtual __base<_R(_A0)>* __clone() const;
405227825Stheraven    virtual void __clone(__base<_R(_A0)>*) const;
406227825Stheraven    virtual void destroy();
407227825Stheraven    virtual void destroy_deallocate();
408227825Stheraven    virtual _R operator()(_A0);
409227825Stheraven#ifndef _LIBCPP_NO_RTTI
410227825Stheraven    virtual const void* target(const type_info&) const;
411227825Stheraven    virtual const std::type_info& target_type() const;
412227825Stheraven#endif  // _LIBCPP_NO_RTTI
413227825Stheraven};
414227825Stheraven
415227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0>
416227825Stheraven__base<_R(_A0)>*
417227825Stheraven__func<_F, _Alloc, _R(_A0)>::__clone() const
418227825Stheraven{
419227825Stheraven    typedef typename _Alloc::template rebind<__func>::other _A;
420227825Stheraven    _A __a(__f_.second());
421227825Stheraven    typedef __allocator_destructor<_A> _D;
422227825Stheraven    unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
423227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
424227825Stheraven    return __hold.release();
425227825Stheraven}
426227825Stheraven
427227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0>
428227825Stheravenvoid
429227825Stheraven__func<_F, _Alloc, _R(_A0)>::__clone(__base<_R(_A0)>* __p) const
430227825Stheraven{
431227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
432227825Stheraven}
433227825Stheraven
434227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0>
435227825Stheravenvoid
436227825Stheraven__func<_F, _Alloc, _R(_A0)>::destroy()
437227825Stheraven{
438227825Stheraven    __f_.~__compressed_pair<_F, _Alloc>();
439227825Stheraven}
440227825Stheraven
441227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0>
442227825Stheravenvoid
443227825Stheraven__func<_F, _Alloc, _R(_A0)>::destroy_deallocate()
444227825Stheraven{
445227825Stheraven    typedef typename _Alloc::template rebind<__func>::other _A;
446227825Stheraven    _A __a(__f_.second());
447227825Stheraven    __f_.~__compressed_pair<_F, _Alloc>();
448227825Stheraven    __a.deallocate(this, 1);
449227825Stheraven}
450227825Stheraven
451227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0>
452227825Stheraven_R
453227825Stheraven__func<_F, _Alloc, _R(_A0)>::operator()(_A0 __a0)
454227825Stheraven{
455227825Stheraven    return __invoke(__f_.first(), __a0);
456227825Stheraven}
457227825Stheraven
458227825Stheraven#ifndef _LIBCPP_NO_RTTI
459227825Stheraven
460227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0>
461227825Stheravenconst void*
462227825Stheraven__func<_F, _Alloc, _R(_A0)>::target(const type_info& __ti) const
463227825Stheraven{
464227825Stheraven    if (__ti == typeid(_F))
465227825Stheraven        return &__f_.first();
466227825Stheraven    return (const void*)0;
467227825Stheraven}
468227825Stheraven
469227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0>
470227825Stheravenconst std::type_info&
471227825Stheraven__func<_F, _Alloc, _R(_A0)>::target_type() const
472227825Stheraven{
473227825Stheraven    return typeid(_F);
474227825Stheraven}
475227825Stheraven
476227825Stheraven#endif  // _LIBCPP_NO_RTTI
477227825Stheraven
478227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1>
479227825Stheravenclass __func<_F, _Alloc, _R(_A0, _A1)>
480227825Stheraven    : public  __base<_R(_A0, _A1)>
481227825Stheraven{
482227825Stheraven    __compressed_pair<_F, _Alloc> __f_;
483227825Stheravenpublic:
484227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {}
485227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a)
486227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
487227825Stheraven    virtual __base<_R(_A0, _A1)>* __clone() const;
488227825Stheraven    virtual void __clone(__base<_R(_A0, _A1)>*) const;
489227825Stheraven    virtual void destroy();
490227825Stheraven    virtual void destroy_deallocate();
491227825Stheraven    virtual _R operator()(_A0, _A1);
492227825Stheraven#ifndef _LIBCPP_NO_RTTI
493227825Stheraven    virtual const void* target(const type_info&) const;
494227825Stheraven    virtual const std::type_info& target_type() const;
495227825Stheraven#endif  // _LIBCPP_NO_RTTI
496227825Stheraven};
497227825Stheraven
498227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1>
499227825Stheraven__base<_R(_A0, _A1)>*
500227825Stheraven__func<_F, _Alloc, _R(_A0, _A1)>::__clone() const
501227825Stheraven{
502227825Stheraven    typedef typename _Alloc::template rebind<__func>::other _A;
503227825Stheraven    _A __a(__f_.second());
504227825Stheraven    typedef __allocator_destructor<_A> _D;
505227825Stheraven    unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
506227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
507227825Stheraven    return __hold.release();
508227825Stheraven}
509227825Stheraven
510227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1>
511227825Stheravenvoid
512227825Stheraven__func<_F, _Alloc, _R(_A0, _A1)>::__clone(__base<_R(_A0, _A1)>* __p) const
513227825Stheraven{
514227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
515227825Stheraven}
516227825Stheraven
517227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1>
518227825Stheravenvoid
519227825Stheraven__func<_F, _Alloc, _R(_A0, _A1)>::destroy()
520227825Stheraven{
521227825Stheraven    __f_.~__compressed_pair<_F, _Alloc>();
522227825Stheraven}
523227825Stheraven
524227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1>
525227825Stheravenvoid
526227825Stheraven__func<_F, _Alloc, _R(_A0, _A1)>::destroy_deallocate()
527227825Stheraven{
528227825Stheraven    typedef typename _Alloc::template rebind<__func>::other _A;
529227825Stheraven    _A __a(__f_.second());
530227825Stheraven    __f_.~__compressed_pair<_F, _Alloc>();
531227825Stheraven    __a.deallocate(this, 1);
532227825Stheraven}
533227825Stheraven
534227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1>
535227825Stheraven_R
536227825Stheraven__func<_F, _Alloc, _R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
537227825Stheraven{
538227825Stheraven    return __invoke(__f_.first(), __a0, __a1);
539227825Stheraven}
540227825Stheraven
541227825Stheraven#ifndef _LIBCPP_NO_RTTI
542227825Stheraven
543227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1>
544227825Stheravenconst void*
545227825Stheraven__func<_F, _Alloc, _R(_A0, _A1)>::target(const type_info& __ti) const
546227825Stheraven{
547227825Stheraven    if (__ti == typeid(_F))
548227825Stheraven        return &__f_.first();
549227825Stheraven    return (const void*)0;
550227825Stheraven}
551227825Stheraven
552227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1>
553227825Stheravenconst std::type_info&
554227825Stheraven__func<_F, _Alloc, _R(_A0, _A1)>::target_type() const
555227825Stheraven{
556227825Stheraven    return typeid(_F);
557227825Stheraven}
558227825Stheraven
559227825Stheraven#endif  // _LIBCPP_NO_RTTI
560227825Stheraven
561227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
562227825Stheravenclass __func<_F, _Alloc, _R(_A0, _A1, _A2)>
563227825Stheraven    : public  __base<_R(_A0, _A1, _A2)>
564227825Stheraven{
565227825Stheraven    __compressed_pair<_F, _Alloc> __f_;
566227825Stheravenpublic:
567227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {}
568227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a)
569227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
570227825Stheraven    virtual __base<_R(_A0, _A1, _A2)>* __clone() const;
571227825Stheraven    virtual void __clone(__base<_R(_A0, _A1, _A2)>*) const;
572227825Stheraven    virtual void destroy();
573227825Stheraven    virtual void destroy_deallocate();
574227825Stheraven    virtual _R operator()(_A0, _A1, _A2);
575227825Stheraven#ifndef _LIBCPP_NO_RTTI
576227825Stheraven    virtual const void* target(const type_info&) const;
577227825Stheraven    virtual const std::type_info& target_type() const;
578227825Stheraven#endif  // _LIBCPP_NO_RTTI
579227825Stheraven};
580227825Stheraven
581227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
582227825Stheraven__base<_R(_A0, _A1, _A2)>*
583227825Stheraven__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone() const
584227825Stheraven{
585227825Stheraven    typedef typename _Alloc::template rebind<__func>::other _A;
586227825Stheraven    _A __a(__f_.second());
587227825Stheraven    typedef __allocator_destructor<_A> _D;
588227825Stheraven    unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1));
589227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
590227825Stheraven    return __hold.release();
591227825Stheraven}
592227825Stheraven
593227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
594227825Stheravenvoid
595227825Stheraven__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone(__base<_R(_A0, _A1, _A2)>* __p) const
596227825Stheraven{
597227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
598227825Stheraven}
599227825Stheraven
600227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
601227825Stheravenvoid
602227825Stheraven__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy()
603227825Stheraven{
604227825Stheraven    __f_.~__compressed_pair<_F, _Alloc>();
605227825Stheraven}
606227825Stheraven
607227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
608227825Stheravenvoid
609227825Stheraven__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy_deallocate()
610227825Stheraven{
611227825Stheraven    typedef typename _Alloc::template rebind<__func>::other _A;
612227825Stheraven    _A __a(__f_.second());
613227825Stheraven    __f_.~__compressed_pair<_F, _Alloc>();
614227825Stheraven    __a.deallocate(this, 1);
615227825Stheraven}
616227825Stheraven
617227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
618227825Stheraven_R
619227825Stheraven__func<_F, _Alloc, _R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
620227825Stheraven{
621227825Stheraven    return __invoke(__f_.first(), __a0, __a1, __a2);
622227825Stheraven}
623227825Stheraven
624227825Stheraven#ifndef _LIBCPP_NO_RTTI
625227825Stheraven
626227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
627227825Stheravenconst void*
628227825Stheraven__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target(const type_info& __ti) const
629227825Stheraven{
630227825Stheraven    if (__ti == typeid(_F))
631227825Stheraven        return &__f_.first();
632227825Stheraven    return (const void*)0;
633227825Stheraven}
634227825Stheraven
635227825Stheraventemplate<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2>
636227825Stheravenconst std::type_info&
637227825Stheraven__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target_type() const
638227825Stheraven{
639227825Stheraven    return typeid(_F);
640227825Stheraven}
641227825Stheraven
642227825Stheraven#endif  // _LIBCPP_NO_RTTI
643227825Stheraven
644227825Stheraven}  // __function
645227825Stheraven
646227825Stheraventemplate<class _R>
647227825Stheravenclass _LIBCPP_VISIBLE function<_R()>
648227825Stheraven{
649227825Stheraven    typedef __function::__base<_R()> __base;
650227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
651227825Stheraven    __base* __f_;
652227825Stheraven
653227825Stheraven    template <class _F>
654227825Stheraven        static bool __not_null(const _F&) {return true;}
655227825Stheraven    template <class _R2>
656227825Stheraven        static bool __not_null(const function<_R()>& __p) {return __p;}
657227825Stheravenpublic:
658227825Stheraven    typedef _R result_type;
659227825Stheraven
660227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
661227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
662227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
663227825Stheraven    function(const function&);
664227825Stheraven    template<class _F>
665227825Stheraven      function(_F,
666227825Stheraven               typename enable_if<!is_integral<_F>::value>::type* = 0);
667227825Stheraven
668227825Stheraven    template<class _Alloc>
669227825Stheraven      _LIBCPP_INLINE_VISIBILITY
670227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
671227825Stheraven    template<class _Alloc>
672227825Stheraven      _LIBCPP_INLINE_VISIBILITY
673227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
674227825Stheraven    template<class _Alloc>
675227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
676227825Stheraven    template<class _F, class _Alloc>
677227825Stheraven      function(allocator_arg_t, const _Alloc& __a, _F __f,
678227825Stheraven               typename enable_if<!is_integral<_F>::value>::type* = 0);
679227825Stheraven
680227825Stheraven    function& operator=(const function&);
681227825Stheraven    function& operator=(nullptr_t);
682227825Stheraven    template<class _F>
683227825Stheraven      typename enable_if
684227825Stheraven      <
685227825Stheraven        !is_integral<_F>::value,
686227825Stheraven        function&
687227825Stheraven      >::type
688227825Stheraven      operator=(_F);
689227825Stheraven
690227825Stheraven    ~function();
691227825Stheraven
692227825Stheraven    // 20.7.16.2.2, function modifiers:
693227825Stheraven    void swap(function&);
694227825Stheraven    template<class _F, class _Alloc>
695227825Stheraven      _LIBCPP_INLINE_VISIBILITY
696227825Stheraven      void assign(_F __f, const _Alloc& __a)
697227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
698227825Stheraven
699227825Stheraven    // 20.7.16.2.3, function capacity:
700227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
701227825Stheraven
702227825Stheravenprivate:
703227825Stheraven    // deleted overloads close possible hole in the type system
704227825Stheraven    template<class _R2>
705227825Stheraven      bool operator==(const function<_R2()>&) const;// = delete;
706227825Stheraven    template<class _R2>
707227825Stheraven      bool operator!=(const function<_R2()>&) const;// = delete;
708227825Stheravenpublic:
709227825Stheraven    // 20.7.16.2.4, function invocation:
710227825Stheraven    _R operator()() const;
711227825Stheraven
712227825Stheraven#ifndef _LIBCPP_NO_RTTI
713227825Stheraven    // 20.7.16.2.5, function target access:
714227825Stheraven    const std::type_info& target_type() const;
715227825Stheraven    template <typename _T> _T* target();
716227825Stheraven    template <typename _T> const _T* target() const;
717227825Stheraven#endif  // _LIBCPP_NO_RTTI
718227825Stheraven};
719227825Stheraven
720227825Stheraventemplate<class _R>
721227825Stheravenfunction<_R()>::function(const function& __f)
722227825Stheraven{
723227825Stheraven    if (__f.__f_ == 0)
724227825Stheraven        __f_ = 0;
725227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
726227825Stheraven    {
727227825Stheraven        __f_ = (__base*)&__buf_;
728227825Stheraven        __f.__f_->__clone(__f_);
729227825Stheraven    }
730227825Stheraven    else
731227825Stheraven        __f_ = __f.__f_->__clone();
732227825Stheraven}
733227825Stheraven
734227825Stheraventemplate<class _R>
735227825Stheraventemplate<class _Alloc>
736227825Stheravenfunction<_R()>::function(allocator_arg_t, const _Alloc&, const function& __f)
737227825Stheraven{
738227825Stheraven    if (__f.__f_ == 0)
739227825Stheraven        __f_ = 0;
740227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
741227825Stheraven    {
742227825Stheraven        __f_ = (__base*)&__buf_;
743227825Stheraven        __f.__f_->__clone(__f_);
744227825Stheraven    }
745227825Stheraven    else
746227825Stheraven        __f_ = __f.__f_->__clone();
747227825Stheraven}
748227825Stheraven
749227825Stheraventemplate<class _R>
750227825Stheraventemplate <class _F>
751227825Stheravenfunction<_R()>::function(_F __f,
752227825Stheraven                                     typename enable_if<!is_integral<_F>::value>::type*)
753227825Stheraven    : __f_(0)
754227825Stheraven{
755227825Stheraven    if (__not_null(__f))
756227825Stheraven    {
757227825Stheraven        typedef __function::__func<_F, allocator<_F>, _R()> _FF;
758227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
759227825Stheraven        {
760227825Stheraven            __f_ = (__base*)&__buf_;
761227825Stheraven            ::new (__f_) _FF(__f);
762227825Stheraven        }
763227825Stheraven        else
764227825Stheraven        {
765227825Stheraven            typedef allocator<_FF> _A;
766227825Stheraven            _A __a;
767227825Stheraven            typedef __allocator_destructor<_A> _D;
768227825Stheraven            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
769227825Stheraven            ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
770227825Stheraven            __f_ = __hold.release();
771227825Stheraven        }
772227825Stheraven    }
773227825Stheraven}
774227825Stheraven
775227825Stheraventemplate<class _R>
776227825Stheraventemplate <class _F, class _Alloc>
777227825Stheravenfunction<_R()>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
778227825Stheraven                                     typename enable_if<!is_integral<_F>::value>::type*)
779227825Stheraven    : __f_(0)
780227825Stheraven{
781227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
782227825Stheraven    if (__not_null(__f))
783227825Stheraven    {
784227825Stheraven        typedef __function::__func<_F, _Alloc, _R()> _FF;
785227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
786227825Stheraven        {
787227825Stheraven            __f_ = (__base*)&__buf_;
788227825Stheraven            ::new (__f_) _FF(__f);
789227825Stheraven        }
790227825Stheraven        else
791227825Stheraven        {
792227825Stheraven            typedef typename __alloc_traits::template
793227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794227825Stheraven                rebind_alloc<_FF>
795227825Stheraven#else
796227825Stheraven                rebind_alloc<_FF>::other
797227825Stheraven#endif
798227825Stheraven                                                         _A;
799227825Stheraven            _A __a(__a0);
800227825Stheraven            typedef __allocator_destructor<_A> _D;
801227825Stheraven            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
802227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
803227825Stheraven            __f_ = __hold.release();
804227825Stheraven        }
805227825Stheraven    }
806227825Stheraven}
807227825Stheraven
808227825Stheraventemplate<class _R>
809227825Stheravenfunction<_R()>&
810227825Stheravenfunction<_R()>::operator=(const function& __f)
811227825Stheraven{
812227825Stheraven    function(__f).swap(*this);
813227825Stheraven    return *this;
814227825Stheraven}
815227825Stheraven
816227825Stheraventemplate<class _R>
817227825Stheravenfunction<_R()>&
818227825Stheravenfunction<_R()>::operator=(nullptr_t)
819227825Stheraven{
820227825Stheraven    if (__f_ == (__base*)&__buf_)
821227825Stheraven        __f_->destroy();
822227825Stheraven    else if (__f_)
823227825Stheraven        __f_->destroy_deallocate();
824227825Stheraven    __f_ = 0;
825227825Stheraven}
826227825Stheraven
827227825Stheraventemplate<class _R>
828227825Stheraventemplate <class _F>
829227825Stheraventypename enable_if
830227825Stheraven<
831227825Stheraven    !is_integral<_F>::value,
832227825Stheraven    function<_R()>&
833227825Stheraven>::type
834227825Stheravenfunction<_R()>::operator=(_F __f)
835227825Stheraven{
836227825Stheraven    function(_VSTD::move(__f)).swap(*this);
837227825Stheraven    return *this;
838227825Stheraven}
839227825Stheraven
840227825Stheraventemplate<class _R>
841227825Stheravenfunction<_R()>::~function()
842227825Stheraven{
843227825Stheraven    if (__f_ == (__base*)&__buf_)
844227825Stheraven        __f_->destroy();
845227825Stheraven    else if (__f_)
846227825Stheraven        __f_->destroy_deallocate();
847227825Stheraven}
848227825Stheraven
849227825Stheraventemplate<class _R>
850227825Stheravenvoid
851227825Stheravenfunction<_R()>::swap(function& __f)
852227825Stheraven{
853227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
854227825Stheraven    {
855227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
856227825Stheraven        __base* __t = (__base*)&__tempbuf;
857227825Stheraven        __f_->__clone(__t);
858227825Stheraven        __f_->destroy();
859227825Stheraven        __f_ = 0;
860227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
861227825Stheraven        __f.__f_->destroy();
862227825Stheraven        __f.__f_ = 0;
863227825Stheraven        __f_ = (__base*)&__buf_;
864227825Stheraven        __t->__clone((__base*)&__f.__buf_);
865227825Stheraven        __t->destroy();
866227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
867227825Stheraven    }
868227825Stheraven    else if (__f_ == (__base*)&__buf_)
869227825Stheraven    {
870227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
871227825Stheraven        __f_->destroy();
872227825Stheraven        __f_ = __f.__f_;
873227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
874227825Stheraven    }
875227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
876227825Stheraven    {
877227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
878227825Stheraven        __f.__f_->destroy();
879227825Stheraven        __f.__f_ = __f_;
880227825Stheraven        __f_ = (__base*)&__buf_;
881227825Stheraven    }
882227825Stheraven    else
883227825Stheraven        _VSTD::swap(__f_, __f.__f_);
884227825Stheraven}
885227825Stheraven
886227825Stheraventemplate<class _R>
887227825Stheraven_R
888227825Stheravenfunction<_R()>::operator()() const
889227825Stheraven{
890227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
891227825Stheraven    if (__f_ == 0)
892227825Stheraven        throw bad_function_call();
893227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
894227825Stheraven    return (*__f_)();
895227825Stheraven}
896227825Stheraven
897227825Stheraven#ifndef _LIBCPP_NO_RTTI
898227825Stheraven
899227825Stheraventemplate<class _R>
900227825Stheravenconst std::type_info&
901227825Stheravenfunction<_R()>::target_type() const
902227825Stheraven{
903227825Stheraven    if (__f_ == 0)
904227825Stheraven        return typeid(void);
905227825Stheraven    return __f_->target_type();
906227825Stheraven}
907227825Stheraven
908227825Stheraventemplate<class _R>
909227825Stheraventemplate <typename _T>
910227825Stheraven_T*
911227825Stheravenfunction<_R()>::target()
912227825Stheraven{
913227825Stheraven    if (__f_ == 0)
914227825Stheraven        return (_T*)0;
915227825Stheraven    return (_T*)__f_->target(typeid(_T));
916227825Stheraven}
917227825Stheraven
918227825Stheraventemplate<class _R>
919227825Stheraventemplate <typename _T>
920227825Stheravenconst _T*
921227825Stheravenfunction<_R()>::target() const
922227825Stheraven{
923227825Stheraven    if (__f_ == 0)
924227825Stheraven        return (const _T*)0;
925227825Stheraven    return (const _T*)__f_->target(typeid(_T));
926227825Stheraven}
927227825Stheraven
928227825Stheraven#endif  // _LIBCPP_NO_RTTI
929227825Stheraven
930227825Stheraventemplate<class _R, class _A0>
931227825Stheravenclass _LIBCPP_VISIBLE function<_R(_A0)>
932227825Stheraven    : public unary_function<_A0, _R>
933227825Stheraven{
934227825Stheraven    typedef __function::__base<_R(_A0)> __base;
935227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
936227825Stheraven    __base* __f_;
937227825Stheraven
938227825Stheraven    template <class _F>
939227825Stheraven        _LIBCPP_INLINE_VISIBILITY
940227825Stheraven        static bool __not_null(const _F&) {return true;}
941227825Stheraven    template <class _R2, class _B0>
942227825Stheraven        _LIBCPP_INLINE_VISIBILITY
943227825Stheraven        static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
944227825Stheraven    template <class _R2, class _C>
945227825Stheraven        _LIBCPP_INLINE_VISIBILITY
946227825Stheraven        static bool __not_null(_R2 (_C::*__p)()) {return __p;}
947227825Stheraven    template <class _R2, class _C>
948227825Stheraven        _LIBCPP_INLINE_VISIBILITY
949227825Stheraven        static bool __not_null(_R2 (_C::*__p)() const) {return __p;}
950227825Stheraven    template <class _R2, class _C>
951227825Stheraven        _LIBCPP_INLINE_VISIBILITY
952227825Stheraven        static bool __not_null(_R2 (_C::*__p)() volatile) {return __p;}
953227825Stheraven    template <class _R2, class _C>
954227825Stheraven        _LIBCPP_INLINE_VISIBILITY
955227825Stheraven        static bool __not_null(_R2 (_C::*__p)() const volatile) {return __p;}
956227825Stheraven    template <class _R2, class _B0>
957227825Stheraven        _LIBCPP_INLINE_VISIBILITY
958227825Stheraven        static bool __not_null(const function<_R(_B0)>& __p) {return __p;}
959227825Stheravenpublic:
960227825Stheraven    typedef _R result_type;
961227825Stheraven
962227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
963227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
964227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
965227825Stheraven    function(const function&);
966227825Stheraven    template<class _F>
967227825Stheraven      function(_F,
968227825Stheraven               typename enable_if<!is_integral<_F>::value>::type* = 0);
969227825Stheraven
970227825Stheraven    template<class _Alloc>
971227825Stheraven      _LIBCPP_INLINE_VISIBILITY
972227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
973227825Stheraven    template<class _Alloc>
974227825Stheraven      _LIBCPP_INLINE_VISIBILITY
975227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
976227825Stheraven    template<class _Alloc>
977227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
978227825Stheraven    template<class _F, class _Alloc>
979227825Stheraven      function(allocator_arg_t, const _Alloc& __a, _F __f,
980227825Stheraven               typename enable_if<!is_integral<_F>::value>::type* = 0);
981227825Stheraven
982227825Stheraven    function& operator=(const function&);
983227825Stheraven    function& operator=(nullptr_t);
984227825Stheraven    template<class _F>
985227825Stheraven      typename enable_if
986227825Stheraven      <
987227825Stheraven        !is_integral<_F>::value,
988227825Stheraven        function&
989227825Stheraven      >::type
990227825Stheraven      operator=(_F);
991227825Stheraven
992227825Stheraven    ~function();
993227825Stheraven
994227825Stheraven    // 20.7.16.2.2, function modifiers:
995227825Stheraven    void swap(function&);
996227825Stheraven    template<class _F, class _Alloc>
997227825Stheraven      _LIBCPP_INLINE_VISIBILITY
998227825Stheraven      void assign(_F __f, const _Alloc& __a)
999227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1000227825Stheraven
1001227825Stheraven    // 20.7.16.2.3, function capacity:
1002227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1003227825Stheraven
1004227825Stheravenprivate:
1005227825Stheraven    // deleted overloads close possible hole in the type system
1006227825Stheraven    template<class _R2, class _B0>
1007227825Stheraven      bool operator==(const function<_R2(_B0)>&) const;// = delete;
1008227825Stheraven    template<class _R2, class _B0>
1009227825Stheraven      bool operator!=(const function<_R2(_B0)>&) const;// = delete;
1010227825Stheravenpublic:
1011227825Stheraven    // 20.7.16.2.4, function invocation:
1012227825Stheraven    _R operator()(_A0) const;
1013227825Stheraven
1014227825Stheraven#ifndef _LIBCPP_NO_RTTI
1015227825Stheraven    // 20.7.16.2.5, function target access:
1016227825Stheraven    const std::type_info& target_type() const;
1017227825Stheraven    template <typename _T> _T* target();
1018227825Stheraven    template <typename _T> const _T* target() const;
1019227825Stheraven#endif  // _LIBCPP_NO_RTTI
1020227825Stheraven};
1021227825Stheraven
1022227825Stheraventemplate<class _R, class _A0>
1023227825Stheravenfunction<_R(_A0)>::function(const function& __f)
1024227825Stheraven{
1025227825Stheraven    if (__f.__f_ == 0)
1026227825Stheraven        __f_ = 0;
1027227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1028227825Stheraven    {
1029227825Stheraven        __f_ = (__base*)&__buf_;
1030227825Stheraven        __f.__f_->__clone(__f_);
1031227825Stheraven    }
1032227825Stheraven    else
1033227825Stheraven        __f_ = __f.__f_->__clone();
1034227825Stheraven}
1035227825Stheraven
1036227825Stheraventemplate<class _R, class _A0>
1037227825Stheraventemplate<class _Alloc>
1038227825Stheravenfunction<_R(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1039227825Stheraven{
1040227825Stheraven    if (__f.__f_ == 0)
1041227825Stheraven        __f_ = 0;
1042227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1043227825Stheraven    {
1044227825Stheraven        __f_ = (__base*)&__buf_;
1045227825Stheraven        __f.__f_->__clone(__f_);
1046227825Stheraven    }
1047227825Stheraven    else
1048227825Stheraven        __f_ = __f.__f_->__clone();
1049227825Stheraven}
1050227825Stheraven
1051227825Stheraventemplate<class _R, class _A0>
1052227825Stheraventemplate <class _F>
1053227825Stheravenfunction<_R(_A0)>::function(_F __f,
1054227825Stheraven                                     typename enable_if<!is_integral<_F>::value>::type*)
1055227825Stheraven    : __f_(0)
1056227825Stheraven{
1057227825Stheraven    if (__not_null(__f))
1058227825Stheraven    {
1059227825Stheraven        typedef __function::__func<_F, allocator<_F>, _R(_A0)> _FF;
1060227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1061227825Stheraven        {
1062227825Stheraven            __f_ = (__base*)&__buf_;
1063227825Stheraven            ::new (__f_) _FF(__f);
1064227825Stheraven        }
1065227825Stheraven        else
1066227825Stheraven        {
1067227825Stheraven            typedef allocator<_FF> _A;
1068227825Stheraven            _A __a;
1069227825Stheraven            typedef __allocator_destructor<_A> _D;
1070227825Stheraven            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1071227825Stheraven            ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1072227825Stheraven            __f_ = __hold.release();
1073227825Stheraven        }
1074227825Stheraven    }
1075227825Stheraven}
1076227825Stheraven
1077227825Stheraventemplate<class _R, class _A0>
1078227825Stheraventemplate <class _F, class _Alloc>
1079227825Stheravenfunction<_R(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1080227825Stheraven                                     typename enable_if<!is_integral<_F>::value>::type*)
1081227825Stheraven    : __f_(0)
1082227825Stheraven{
1083227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1084227825Stheraven    if (__not_null(__f))
1085227825Stheraven    {
1086227825Stheraven        typedef __function::__func<_F, _Alloc, _R(_A0)> _FF;
1087227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1088227825Stheraven        {
1089227825Stheraven            __f_ = (__base*)&__buf_;
1090227825Stheraven            ::new (__f_) _FF(__f);
1091227825Stheraven        }
1092227825Stheraven        else
1093227825Stheraven        {
1094227825Stheraven            typedef typename __alloc_traits::template
1095227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1096227825Stheraven                rebind_alloc<_FF>
1097227825Stheraven#else
1098227825Stheraven                rebind_alloc<_FF>::other
1099227825Stheraven#endif
1100227825Stheraven                                                         _A;
1101227825Stheraven            _A __a(__a0);
1102227825Stheraven            typedef __allocator_destructor<_A> _D;
1103227825Stheraven            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1104227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1105227825Stheraven            __f_ = __hold.release();
1106227825Stheraven        }
1107227825Stheraven    }
1108227825Stheraven}
1109227825Stheraven
1110227825Stheraventemplate<class _R, class _A0>
1111227825Stheravenfunction<_R(_A0)>&
1112227825Stheravenfunction<_R(_A0)>::operator=(const function& __f)
1113227825Stheraven{
1114227825Stheraven    function(__f).swap(*this);
1115227825Stheraven    return *this;
1116227825Stheraven}
1117227825Stheraven
1118227825Stheraventemplate<class _R, class _A0>
1119227825Stheravenfunction<_R(_A0)>&
1120227825Stheravenfunction<_R(_A0)>::operator=(nullptr_t)
1121227825Stheraven{
1122227825Stheraven    if (__f_ == (__base*)&__buf_)
1123227825Stheraven        __f_->destroy();
1124227825Stheraven    else if (__f_)
1125227825Stheraven        __f_->destroy_deallocate();
1126227825Stheraven    __f_ = 0;
1127227825Stheraven}
1128227825Stheraven
1129227825Stheraventemplate<class _R, class _A0>
1130227825Stheraventemplate <class _F>
1131227825Stheraventypename enable_if
1132227825Stheraven<
1133227825Stheraven    !is_integral<_F>::value,
1134227825Stheraven    function<_R(_A0)>&
1135227825Stheraven>::type
1136227825Stheravenfunction<_R(_A0)>::operator=(_F __f)
1137227825Stheraven{
1138227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1139227825Stheraven    return *this;
1140227825Stheraven}
1141227825Stheraven
1142227825Stheraventemplate<class _R, class _A0>
1143227825Stheravenfunction<_R(_A0)>::~function()
1144227825Stheraven{
1145227825Stheraven    if (__f_ == (__base*)&__buf_)
1146227825Stheraven        __f_->destroy();
1147227825Stheraven    else if (__f_)
1148227825Stheraven        __f_->destroy_deallocate();
1149227825Stheraven}
1150227825Stheraven
1151227825Stheraventemplate<class _R, class _A0>
1152227825Stheravenvoid
1153227825Stheravenfunction<_R(_A0)>::swap(function& __f)
1154227825Stheraven{
1155227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1156227825Stheraven    {
1157227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1158227825Stheraven        __base* __t = (__base*)&__tempbuf;
1159227825Stheraven        __f_->__clone(__t);
1160227825Stheraven        __f_->destroy();
1161227825Stheraven        __f_ = 0;
1162227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1163227825Stheraven        __f.__f_->destroy();
1164227825Stheraven        __f.__f_ = 0;
1165227825Stheraven        __f_ = (__base*)&__buf_;
1166227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1167227825Stheraven        __t->destroy();
1168227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1169227825Stheraven    }
1170227825Stheraven    else if (__f_ == (__base*)&__buf_)
1171227825Stheraven    {
1172227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1173227825Stheraven        __f_->destroy();
1174227825Stheraven        __f_ = __f.__f_;
1175227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1176227825Stheraven    }
1177227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1178227825Stheraven    {
1179227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1180227825Stheraven        __f.__f_->destroy();
1181227825Stheraven        __f.__f_ = __f_;
1182227825Stheraven        __f_ = (__base*)&__buf_;
1183227825Stheraven    }
1184227825Stheraven    else
1185227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1186227825Stheraven}
1187227825Stheraven
1188227825Stheraventemplate<class _R, class _A0>
1189227825Stheraven_R
1190227825Stheravenfunction<_R(_A0)>::operator()(_A0 __a0) const
1191227825Stheraven{
1192227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1193227825Stheraven    if (__f_ == 0)
1194227825Stheraven        throw bad_function_call();
1195227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1196227825Stheraven    return (*__f_)(__a0);
1197227825Stheraven}
1198227825Stheraven
1199227825Stheraven#ifndef _LIBCPP_NO_RTTI
1200227825Stheraven
1201227825Stheraventemplate<class _R, class _A0>
1202227825Stheravenconst std::type_info&
1203227825Stheravenfunction<_R(_A0)>::target_type() const
1204227825Stheraven{
1205227825Stheraven    if (__f_ == 0)
1206227825Stheraven        return typeid(void);
1207227825Stheraven    return __f_->target_type();
1208227825Stheraven}
1209227825Stheraven
1210227825Stheraventemplate<class _R, class _A0>
1211227825Stheraventemplate <typename _T>
1212227825Stheraven_T*
1213227825Stheravenfunction<_R(_A0)>::target()
1214227825Stheraven{
1215227825Stheraven    if (__f_ == 0)
1216227825Stheraven        return (_T*)0;
1217227825Stheraven    return (_T*)__f_->target(typeid(_T));
1218227825Stheraven}
1219227825Stheraven
1220227825Stheraventemplate<class _R, class _A0>
1221227825Stheraventemplate <typename _T>
1222227825Stheravenconst _T*
1223227825Stheravenfunction<_R(_A0)>::target() const
1224227825Stheraven{
1225227825Stheraven    if (__f_ == 0)
1226227825Stheraven        return (const _T*)0;
1227227825Stheraven    return (const _T*)__f_->target(typeid(_T));
1228227825Stheraven}
1229227825Stheraven
1230227825Stheraven#endif  // _LIBCPP_NO_RTTI
1231227825Stheraven
1232227825Stheraventemplate<class _R, class _A0, class _A1>
1233227825Stheravenclass _LIBCPP_VISIBLE function<_R(_A0, _A1)>
1234227825Stheraven    : public binary_function<_A0, _A1, _R>
1235227825Stheraven{
1236227825Stheraven    typedef __function::__base<_R(_A0, _A1)> __base;
1237227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1238227825Stheraven    __base* __f_;
1239227825Stheraven
1240227825Stheraven    template <class _F>
1241227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1242227825Stheraven        static bool __not_null(const _F&) {return true;}
1243227825Stheraven    template <class _R2, class _B0, class _B1>
1244227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1245227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1246227825Stheraven    template <class _R2, class _C, class _B1>
1247227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1248227825Stheraven        static bool __not_null(_R2 (_C::*__p)(_B1)) {return __p;}
1249227825Stheraven    template <class _R2, class _C, class _B1>
1250227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1251227825Stheraven        static bool __not_null(_R2 (_C::*__p)(_B1) const) {return __p;}
1252227825Stheraven    template <class _R2, class _C, class _B1>
1253227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1254227825Stheraven        static bool __not_null(_R2 (_C::*__p)(_B1) volatile) {return __p;}
1255227825Stheraven    template <class _R2, class _C, class _B1>
1256227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1257227825Stheraven        static bool __not_null(_R2 (_C::*__p)(_B1) const volatile) {return __p;}
1258227825Stheraven    template <class _R2, class _B0, class _B1>
1259227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1260227825Stheraven        static bool __not_null(const function<_R(_B0, _B1)>& __p) {return __p;}
1261227825Stheravenpublic:
1262227825Stheraven    typedef _R result_type;
1263227825Stheraven
1264227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1265227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1266227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1267227825Stheraven    function(const function&);
1268227825Stheraven    template<class _F>
1269227825Stheraven      function(_F,
1270227825Stheraven               typename enable_if<!is_integral<_F>::value>::type* = 0);
1271227825Stheraven
1272227825Stheraven    template<class _Alloc>
1273227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1274227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1275227825Stheraven    template<class _Alloc>
1276227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1277227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1278227825Stheraven    template<class _Alloc>
1279227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1280227825Stheraven    template<class _F, class _Alloc>
1281227825Stheraven      function(allocator_arg_t, const _Alloc& __a, _F __f,
1282227825Stheraven               typename enable_if<!is_integral<_F>::value>::type* = 0);
1283227825Stheraven
1284227825Stheraven    function& operator=(const function&);
1285227825Stheraven    function& operator=(nullptr_t);
1286227825Stheraven    template<class _F>
1287227825Stheraven      typename enable_if
1288227825Stheraven      <
1289227825Stheraven        !is_integral<_F>::value,
1290227825Stheraven        function&
1291227825Stheraven      >::type
1292227825Stheraven      operator=(_F);
1293227825Stheraven
1294227825Stheraven    ~function();
1295227825Stheraven
1296227825Stheraven    // 20.7.16.2.2, function modifiers:
1297227825Stheraven    void swap(function&);
1298227825Stheraven    template<class _F, class _Alloc>
1299227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1300227825Stheraven      void assign(_F __f, const _Alloc& __a)
1301227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1302227825Stheraven
1303227825Stheraven    // 20.7.16.2.3, function capacity:
1304227825Stheraven    operator bool() const {return __f_;}
1305227825Stheraven
1306227825Stheravenprivate:
1307227825Stheraven    // deleted overloads close possible hole in the type system
1308227825Stheraven    template<class _R2, class _B0, class _B1>
1309227825Stheraven      bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
1310227825Stheraven    template<class _R2, class _B0, class _B1>
1311227825Stheraven      bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
1312227825Stheravenpublic:
1313227825Stheraven    // 20.7.16.2.4, function invocation:
1314227825Stheraven    _R operator()(_A0, _A1) const;
1315227825Stheraven
1316227825Stheraven#ifndef _LIBCPP_NO_RTTI
1317227825Stheraven    // 20.7.16.2.5, function target access:
1318227825Stheraven    const std::type_info& target_type() const;
1319227825Stheraven    template <typename _T> _T* target();
1320227825Stheraven    template <typename _T> const _T* target() const;
1321227825Stheraven#endif  // _LIBCPP_NO_RTTI
1322227825Stheraven};
1323227825Stheraven
1324227825Stheraventemplate<class _R, class _A0, class _A1>
1325227825Stheravenfunction<_R(_A0, _A1)>::function(const function& __f)
1326227825Stheraven{
1327227825Stheraven    if (__f.__f_ == 0)
1328227825Stheraven        __f_ = 0;
1329227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1330227825Stheraven    {
1331227825Stheraven        __f_ = (__base*)&__buf_;
1332227825Stheraven        __f.__f_->__clone(__f_);
1333227825Stheraven    }
1334227825Stheraven    else
1335227825Stheraven        __f_ = __f.__f_->__clone();
1336227825Stheraven}
1337227825Stheraven
1338227825Stheraventemplate<class _R, class _A0, class _A1>
1339227825Stheraventemplate<class _Alloc>
1340227825Stheravenfunction<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1341227825Stheraven{
1342227825Stheraven    if (__f.__f_ == 0)
1343227825Stheraven        __f_ = 0;
1344227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1345227825Stheraven    {
1346227825Stheraven        __f_ = (__base*)&__buf_;
1347227825Stheraven        __f.__f_->__clone(__f_);
1348227825Stheraven    }
1349227825Stheraven    else
1350227825Stheraven        __f_ = __f.__f_->__clone();
1351227825Stheraven}
1352227825Stheraven
1353227825Stheraventemplate<class _R, class _A0, class _A1>
1354227825Stheraventemplate <class _F>
1355227825Stheravenfunction<_R(_A0, _A1)>::function(_F __f,
1356227825Stheraven                                 typename enable_if<!is_integral<_F>::value>::type*)
1357227825Stheraven    : __f_(0)
1358227825Stheraven{
1359227825Stheraven    if (__not_null(__f))
1360227825Stheraven    {
1361227825Stheraven        typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1)> _FF;
1362227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1363227825Stheraven        {
1364227825Stheraven            __f_ = (__base*)&__buf_;
1365227825Stheraven            ::new (__f_) _FF(__f);
1366227825Stheraven        }
1367227825Stheraven        else
1368227825Stheraven        {
1369227825Stheraven            typedef allocator<_FF> _A;
1370227825Stheraven            _A __a;
1371227825Stheraven            typedef __allocator_destructor<_A> _D;
1372227825Stheraven            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1373227825Stheraven            ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1374227825Stheraven            __f_ = __hold.release();
1375227825Stheraven        }
1376227825Stheraven    }
1377227825Stheraven}
1378227825Stheraven
1379227825Stheraventemplate<class _R, class _A0, class _A1>
1380227825Stheraventemplate <class _F, class _Alloc>
1381227825Stheravenfunction<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1382227825Stheraven                                 typename enable_if<!is_integral<_F>::value>::type*)
1383227825Stheraven    : __f_(0)
1384227825Stheraven{
1385227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1386227825Stheraven    if (__not_null(__f))
1387227825Stheraven    {
1388227825Stheraven        typedef __function::__func<_F, _Alloc, _R(_A0, _A1)> _FF;
1389227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1390227825Stheraven        {
1391227825Stheraven            __f_ = (__base*)&__buf_;
1392227825Stheraven            ::new (__f_) _FF(__f);
1393227825Stheraven        }
1394227825Stheraven        else
1395227825Stheraven        {
1396227825Stheraven            typedef typename __alloc_traits::template
1397227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1398227825Stheraven                rebind_alloc<_FF>
1399227825Stheraven#else
1400227825Stheraven                rebind_alloc<_FF>::other
1401227825Stheraven#endif
1402227825Stheraven                                                         _A;
1403227825Stheraven            _A __a(__a0);
1404227825Stheraven            typedef __allocator_destructor<_A> _D;
1405227825Stheraven            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1406227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1407227825Stheraven            __f_ = __hold.release();
1408227825Stheraven        }
1409227825Stheraven    }
1410227825Stheraven}
1411227825Stheraven
1412227825Stheraventemplate<class _R, class _A0, class _A1>
1413227825Stheravenfunction<_R(_A0, _A1)>&
1414227825Stheravenfunction<_R(_A0, _A1)>::operator=(const function& __f)
1415227825Stheraven{
1416227825Stheraven    function(__f).swap(*this);
1417227825Stheraven    return *this;
1418227825Stheraven}
1419227825Stheraven
1420227825Stheraventemplate<class _R, class _A0, class _A1>
1421227825Stheravenfunction<_R(_A0, _A1)>&
1422227825Stheravenfunction<_R(_A0, _A1)>::operator=(nullptr_t)
1423227825Stheraven{
1424227825Stheraven    if (__f_ == (__base*)&__buf_)
1425227825Stheraven        __f_->destroy();
1426227825Stheraven    else if (__f_)
1427227825Stheraven        __f_->destroy_deallocate();
1428227825Stheraven    __f_ = 0;
1429227825Stheraven}
1430227825Stheraven
1431227825Stheraventemplate<class _R, class _A0, class _A1>
1432227825Stheraventemplate <class _F>
1433227825Stheraventypename enable_if
1434227825Stheraven<
1435227825Stheraven    !is_integral<_F>::value,
1436227825Stheraven    function<_R(_A0, _A1)>&
1437227825Stheraven>::type
1438227825Stheravenfunction<_R(_A0, _A1)>::operator=(_F __f)
1439227825Stheraven{
1440227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1441227825Stheraven    return *this;
1442227825Stheraven}
1443227825Stheraven
1444227825Stheraventemplate<class _R, class _A0, class _A1>
1445227825Stheravenfunction<_R(_A0, _A1)>::~function()
1446227825Stheraven{
1447227825Stheraven    if (__f_ == (__base*)&__buf_)
1448227825Stheraven        __f_->destroy();
1449227825Stheraven    else if (__f_)
1450227825Stheraven        __f_->destroy_deallocate();
1451227825Stheraven}
1452227825Stheraven
1453227825Stheraventemplate<class _R, class _A0, class _A1>
1454227825Stheravenvoid
1455227825Stheravenfunction<_R(_A0, _A1)>::swap(function& __f)
1456227825Stheraven{
1457227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1458227825Stheraven    {
1459227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1460227825Stheraven        __base* __t = (__base*)&__tempbuf;
1461227825Stheraven        __f_->__clone(__t);
1462227825Stheraven        __f_->destroy();
1463227825Stheraven        __f_ = 0;
1464227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1465227825Stheraven        __f.__f_->destroy();
1466227825Stheraven        __f.__f_ = 0;
1467227825Stheraven        __f_ = (__base*)&__buf_;
1468227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1469227825Stheraven        __t->destroy();
1470227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1471227825Stheraven    }
1472227825Stheraven    else if (__f_ == (__base*)&__buf_)
1473227825Stheraven    {
1474227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1475227825Stheraven        __f_->destroy();
1476227825Stheraven        __f_ = __f.__f_;
1477227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1478227825Stheraven    }
1479227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1480227825Stheraven    {
1481227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1482227825Stheraven        __f.__f_->destroy();
1483227825Stheraven        __f.__f_ = __f_;
1484227825Stheraven        __f_ = (__base*)&__buf_;
1485227825Stheraven    }
1486227825Stheraven    else
1487227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1488227825Stheraven}
1489227825Stheraven
1490227825Stheraventemplate<class _R, class _A0, class _A1>
1491227825Stheraven_R
1492227825Stheravenfunction<_R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1493227825Stheraven{
1494227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1495227825Stheraven    if (__f_ == 0)
1496227825Stheraven        throw bad_function_call();
1497227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1498227825Stheraven    return (*__f_)(__a0, __a1);
1499227825Stheraven}
1500227825Stheraven
1501227825Stheraven#ifndef _LIBCPP_NO_RTTI
1502227825Stheraven
1503227825Stheraventemplate<class _R, class _A0, class _A1>
1504227825Stheravenconst std::type_info&
1505227825Stheravenfunction<_R(_A0, _A1)>::target_type() const
1506227825Stheraven{
1507227825Stheraven    if (__f_ == 0)
1508227825Stheraven        return typeid(void);
1509227825Stheraven    return __f_->target_type();
1510227825Stheraven}
1511227825Stheraven
1512227825Stheraventemplate<class _R, class _A0, class _A1>
1513227825Stheraventemplate <typename _T>
1514227825Stheraven_T*
1515227825Stheravenfunction<_R(_A0, _A1)>::target()
1516227825Stheraven{
1517227825Stheraven    if (__f_ == 0)
1518227825Stheraven        return (_T*)0;
1519227825Stheraven    return (_T*)__f_->target(typeid(_T));
1520227825Stheraven}
1521227825Stheraven
1522227825Stheraventemplate<class _R, class _A0, class _A1>
1523227825Stheraventemplate <typename _T>
1524227825Stheravenconst _T*
1525227825Stheravenfunction<_R(_A0, _A1)>::target() const
1526227825Stheraven{
1527227825Stheraven    if (__f_ == 0)
1528227825Stheraven        return (const _T*)0;
1529227825Stheraven    return (const _T*)__f_->target(typeid(_T));
1530227825Stheraven}
1531227825Stheraven
1532227825Stheraven#endif  // _LIBCPP_NO_RTTI
1533227825Stheraven
1534227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1535227825Stheravenclass _LIBCPP_VISIBLE function<_R(_A0, _A1, _A2)>
1536227825Stheraven{
1537227825Stheraven    typedef __function::__base<_R(_A0, _A1, _A2)> __base;
1538227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1539227825Stheraven    __base* __f_;
1540227825Stheraven
1541227825Stheraven    template <class _F>
1542227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1543227825Stheraven        static bool __not_null(const _F&) {return true;}
1544227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1545227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1546227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1547227825Stheraven    template <class _R2, class _C, class _B1, class _B2>
1548227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1549227825Stheraven        static bool __not_null(_R2 (_C::*__p)(_B1, _B2)) {return __p;}
1550227825Stheraven    template <class _R2, class _C, class _B1, class _B2>
1551227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1552227825Stheraven        static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const) {return __p;}
1553227825Stheraven    template <class _R2, class _C, class _B1, class _B2>
1554227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1555227825Stheraven        static bool __not_null(_R2 (_C::*__p)(_B1, _B2) volatile) {return __p;}
1556227825Stheraven    template <class _R2, class _C, class _B1, class _B2>
1557227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1558227825Stheraven        static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const volatile) {return __p;}
1559227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1560227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1561227825Stheraven        static bool __not_null(const function<_R(_B0, _B1, _B2)>& __p) {return __p;}
1562227825Stheravenpublic:
1563227825Stheraven    typedef _R result_type;
1564227825Stheraven
1565227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1566227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1567227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1568227825Stheraven    function(const function&);
1569227825Stheraven    template<class _F>
1570227825Stheraven      function(_F,
1571227825Stheraven               typename enable_if<!is_integral<_F>::value>::type* = 0);
1572227825Stheraven
1573227825Stheraven    template<class _Alloc>
1574227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1575227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1576227825Stheraven    template<class _Alloc>
1577227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1578227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1579227825Stheraven    template<class _Alloc>
1580227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1581227825Stheraven    template<class _F, class _Alloc>
1582227825Stheraven      function(allocator_arg_t, const _Alloc& __a, _F __f,
1583227825Stheraven               typename enable_if<!is_integral<_F>::value>::type* = 0);
1584227825Stheraven
1585227825Stheraven    function& operator=(const function&);
1586227825Stheraven    function& operator=(nullptr_t);
1587227825Stheraven    template<class _F>
1588227825Stheraven      typename enable_if
1589227825Stheraven      <
1590227825Stheraven        !is_integral<_F>::value,
1591227825Stheraven        function&
1592227825Stheraven      >::type
1593227825Stheraven      operator=(_F);
1594227825Stheraven
1595227825Stheraven    ~function();
1596227825Stheraven
1597227825Stheraven    // 20.7.16.2.2, function modifiers:
1598227825Stheraven    void swap(function&);
1599227825Stheraven    template<class _F, class _Alloc>
1600227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1601227825Stheraven      void assign(_F __f, const _Alloc& __a)
1602227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1603227825Stheraven
1604227825Stheraven    // 20.7.16.2.3, function capacity:
1605227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1606227825Stheraven
1607227825Stheravenprivate:
1608227825Stheraven    // deleted overloads close possible hole in the type system
1609227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1610227825Stheraven      bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1611227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1612227825Stheraven      bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1613227825Stheravenpublic:
1614227825Stheraven    // 20.7.16.2.4, function invocation:
1615227825Stheraven    _R operator()(_A0, _A1, _A2) const;
1616227825Stheraven
1617227825Stheraven#ifndef _LIBCPP_NO_RTTI
1618227825Stheraven    // 20.7.16.2.5, function target access:
1619227825Stheraven    const std::type_info& target_type() const;
1620227825Stheraven    template <typename _T> _T* target();
1621227825Stheraven    template <typename _T> const _T* target() const;
1622227825Stheraven#endif  // _LIBCPP_NO_RTTI
1623227825Stheraven};
1624227825Stheraven
1625227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1626227825Stheravenfunction<_R(_A0, _A1, _A2)>::function(const function& __f)
1627227825Stheraven{
1628227825Stheraven    if (__f.__f_ == 0)
1629227825Stheraven        __f_ = 0;
1630227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1631227825Stheraven    {
1632227825Stheraven        __f_ = (__base*)&__buf_;
1633227825Stheraven        __f.__f_->__clone(__f_);
1634227825Stheraven    }
1635227825Stheraven    else
1636227825Stheraven        __f_ = __f.__f_->__clone();
1637227825Stheraven}
1638227825Stheraven
1639227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1640227825Stheraventemplate<class _Alloc>
1641227825Stheravenfunction<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
1642227825Stheraven                                      const function& __f)
1643227825Stheraven{
1644227825Stheraven    if (__f.__f_ == 0)
1645227825Stheraven        __f_ = 0;
1646227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1647227825Stheraven    {
1648227825Stheraven        __f_ = (__base*)&__buf_;
1649227825Stheraven        __f.__f_->__clone(__f_);
1650227825Stheraven    }
1651227825Stheraven    else
1652227825Stheraven        __f_ = __f.__f_->__clone();
1653227825Stheraven}
1654227825Stheraven
1655227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1656227825Stheraventemplate <class _F>
1657227825Stheravenfunction<_R(_A0, _A1, _A2)>::function(_F __f,
1658227825Stheraven                                     typename enable_if<!is_integral<_F>::value>::type*)
1659227825Stheraven    : __f_(0)
1660227825Stheraven{
1661227825Stheraven    if (__not_null(__f))
1662227825Stheraven    {
1663227825Stheraven        typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1, _A2)> _FF;
1664227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1665227825Stheraven        {
1666227825Stheraven            __f_ = (__base*)&__buf_;
1667227825Stheraven            ::new (__f_) _FF(__f);
1668227825Stheraven        }
1669227825Stheraven        else
1670227825Stheraven        {
1671227825Stheraven            typedef allocator<_FF> _A;
1672227825Stheraven            _A __a;
1673227825Stheraven            typedef __allocator_destructor<_A> _D;
1674227825Stheraven            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1675227825Stheraven            ::new (__hold.get()) _FF(__f, allocator<_F>(__a));
1676227825Stheraven            __f_ = __hold.release();
1677227825Stheraven        }
1678227825Stheraven    }
1679227825Stheraven}
1680227825Stheraven
1681227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1682227825Stheraventemplate <class _F, class _Alloc>
1683227825Stheravenfunction<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _F __f,
1684227825Stheraven                                     typename enable_if<!is_integral<_F>::value>::type*)
1685227825Stheraven    : __f_(0)
1686227825Stheraven{
1687227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1688227825Stheraven    if (__not_null(__f))
1689227825Stheraven    {
1690227825Stheraven        typedef __function::__func<_F, _Alloc, _R(_A0, _A1, _A2)> _FF;
1691227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1692227825Stheraven        {
1693227825Stheraven            __f_ = (__base*)&__buf_;
1694227825Stheraven            ::new (__f_) _FF(__f);
1695227825Stheraven        }
1696227825Stheraven        else
1697227825Stheraven        {
1698227825Stheraven            typedef typename __alloc_traits::template
1699227825Stheraven#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1700227825Stheraven                rebind_alloc<_FF>
1701227825Stheraven#else
1702227825Stheraven                rebind_alloc<_FF>::other
1703227825Stheraven#endif
1704227825Stheraven                                                         _A;
1705227825Stheraven            _A __a(__a0);
1706227825Stheraven            typedef __allocator_destructor<_A> _D;
1707227825Stheraven            unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1));
1708227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1709227825Stheraven            __f_ = __hold.release();
1710227825Stheraven        }
1711227825Stheraven    }
1712227825Stheraven}
1713227825Stheraven
1714227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1715227825Stheravenfunction<_R(_A0, _A1, _A2)>&
1716227825Stheravenfunction<_R(_A0, _A1, _A2)>::operator=(const function& __f)
1717227825Stheraven{
1718227825Stheraven    function(__f).swap(*this);
1719227825Stheraven    return *this;
1720227825Stheraven}
1721227825Stheraven
1722227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1723227825Stheravenfunction<_R(_A0, _A1, _A2)>&
1724227825Stheravenfunction<_R(_A0, _A1, _A2)>::operator=(nullptr_t)
1725227825Stheraven{
1726227825Stheraven    if (__f_ == (__base*)&__buf_)
1727227825Stheraven        __f_->destroy();
1728227825Stheraven    else if (__f_)
1729227825Stheraven        __f_->destroy_deallocate();
1730227825Stheraven    __f_ = 0;
1731227825Stheraven}
1732227825Stheraven
1733227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1734227825Stheraventemplate <class _F>
1735227825Stheraventypename enable_if
1736227825Stheraven<
1737227825Stheraven    !is_integral<_F>::value,
1738227825Stheraven    function<_R(_A0, _A1, _A2)>&
1739227825Stheraven>::type
1740227825Stheravenfunction<_R(_A0, _A1, _A2)>::operator=(_F __f)
1741227825Stheraven{
1742227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1743227825Stheraven    return *this;
1744227825Stheraven}
1745227825Stheraven
1746227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1747227825Stheravenfunction<_R(_A0, _A1, _A2)>::~function()
1748227825Stheraven{
1749227825Stheraven    if (__f_ == (__base*)&__buf_)
1750227825Stheraven        __f_->destroy();
1751227825Stheraven    else if (__f_)
1752227825Stheraven        __f_->destroy_deallocate();
1753227825Stheraven}
1754227825Stheraven
1755227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1756227825Stheravenvoid
1757227825Stheravenfunction<_R(_A0, _A1, _A2)>::swap(function& __f)
1758227825Stheraven{
1759227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1760227825Stheraven    {
1761227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1762227825Stheraven        __base* __t = (__base*)&__tempbuf;
1763227825Stheraven        __f_->__clone(__t);
1764227825Stheraven        __f_->destroy();
1765227825Stheraven        __f_ = 0;
1766227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1767227825Stheraven        __f.__f_->destroy();
1768227825Stheraven        __f.__f_ = 0;
1769227825Stheraven        __f_ = (__base*)&__buf_;
1770227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1771227825Stheraven        __t->destroy();
1772227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1773227825Stheraven    }
1774227825Stheraven    else if (__f_ == (__base*)&__buf_)
1775227825Stheraven    {
1776227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1777227825Stheraven        __f_->destroy();
1778227825Stheraven        __f_ = __f.__f_;
1779227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1780227825Stheraven    }
1781227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1782227825Stheraven    {
1783227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1784227825Stheraven        __f.__f_->destroy();
1785227825Stheraven        __f.__f_ = __f_;
1786227825Stheraven        __f_ = (__base*)&__buf_;
1787227825Stheraven    }
1788227825Stheraven    else
1789227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1790227825Stheraven}
1791227825Stheraven
1792227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1793227825Stheraven_R
1794227825Stheravenfunction<_R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1795227825Stheraven{
1796227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1797227825Stheraven    if (__f_ == 0)
1798227825Stheraven        throw bad_function_call();
1799227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1800227825Stheraven    return (*__f_)(__a0, __a1, __a2);
1801227825Stheraven}
1802227825Stheraven
1803227825Stheraven#ifndef _LIBCPP_NO_RTTI
1804227825Stheraven
1805227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1806227825Stheravenconst std::type_info&
1807227825Stheravenfunction<_R(_A0, _A1, _A2)>::target_type() const
1808227825Stheraven{
1809227825Stheraven    if (__f_ == 0)
1810227825Stheraven        return typeid(void);
1811227825Stheraven    return __f_->target_type();
1812227825Stheraven}
1813227825Stheraven
1814227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1815227825Stheraventemplate <typename _T>
1816227825Stheraven_T*
1817227825Stheravenfunction<_R(_A0, _A1, _A2)>::target()
1818227825Stheraven{
1819227825Stheraven    if (__f_ == 0)
1820227825Stheraven        return (_T*)0;
1821227825Stheraven    return (_T*)__f_->target(typeid(_T));
1822227825Stheraven}
1823227825Stheraven
1824227825Stheraventemplate<class _R, class _A0, class _A1, class _A2>
1825227825Stheraventemplate <typename _T>
1826227825Stheravenconst _T*
1827227825Stheravenfunction<_R(_A0, _A1, _A2)>::target() const
1828227825Stheraven{
1829227825Stheraven    if (__f_ == 0)
1830227825Stheraven        return (const _T*)0;
1831227825Stheraven    return (const _T*)__f_->target(typeid(_T));
1832227825Stheraven}
1833227825Stheraven
1834227825Stheraven#endif  // _LIBCPP_NO_RTTI
1835227825Stheraven
1836227825Stheraventemplate <class _F>
1837227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1838227825Stheravenbool
1839227825Stheravenoperator==(const function<_F>& __f, nullptr_t) {return !__f;}
1840227825Stheraven
1841227825Stheraventemplate <class _F>
1842227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1843227825Stheravenbool
1844227825Stheravenoperator==(nullptr_t, const function<_F>& __f) {return !__f;}
1845227825Stheraven
1846227825Stheraventemplate <class _F>
1847227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1848227825Stheravenbool
1849227825Stheravenoperator!=(const function<_F>& __f, nullptr_t) {return (bool)__f;}
1850227825Stheraven
1851227825Stheraventemplate <class _F>
1852227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1853227825Stheravenbool
1854227825Stheravenoperator!=(nullptr_t, const function<_F>& __f) {return (bool)__f;}
1855227825Stheraven
1856227825Stheraventemplate <class _F>
1857227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1858227825Stheravenvoid
1859227825Stheravenswap(function<_F>& __x, function<_F>& __y)
1860227825Stheraven{return __x.swap(__y);}
1861227825Stheraven
1862227825Stheraventemplate<class _Tp> struct __is_bind_expression : public false_type {};
1863227825Stheraventemplate<class _Tp> struct _LIBCPP_VISIBLE is_bind_expression
1864227825Stheraven    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1865227825Stheraven
1866227825Stheraventemplate<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1867227825Stheraventemplate<class _Tp> struct _LIBCPP_VISIBLE is_placeholder
1868227825Stheraven    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1869227825Stheraven
1870227825Stheravennamespace placeholders
1871227825Stheraven{
1872227825Stheraven
1873227825Stheraventemplate <int _N> struct __ph {};
1874227825Stheraven
1875227825Stheravenextern __ph<1>   _1;
1876227825Stheravenextern __ph<2>   _2;
1877227825Stheravenextern __ph<3>   _3;
1878227825Stheravenextern __ph<4>   _4;
1879227825Stheravenextern __ph<5>   _5;
1880227825Stheravenextern __ph<6>   _6;
1881227825Stheravenextern __ph<7>   _7;
1882227825Stheravenextern __ph<8>   _8;
1883227825Stheravenextern __ph<9>   _9;
1884227825Stheravenextern __ph<10> _10;
1885227825Stheraven
1886227825Stheraven}  // placeholders
1887227825Stheraven
1888227825Stheraventemplate<int _N>
1889227825Stheravenstruct __is_placeholder<placeholders::__ph<_N> >
1890227825Stheraven    : public integral_constant<int, _N> {};
1891227825Stheraven
1892227825Stheraventemplate <class _Tp, class _Uj>
1893227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1894227825Stheraven_Tp&
1895227825Stheraven__mu(reference_wrapper<_Tp> __t, _Uj&)
1896227825Stheraven{
1897227825Stheraven    return __t.get();
1898227825Stheraven}
1899227825Stheraven/*
1900227825Stheraventemplate <bool _IsBindExpr, class _Ti, class ..._Uj>
1901227825Stheravenstruct __mu_return1 {};
1902227825Stheraven
1903227825Stheraventemplate <class _Ti, class ..._Uj>
1904227825Stheravenstruct __mu_return1<true, _Ti, _Uj...>
1905227825Stheraven{
1906227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1907227825Stheraven};
1908227825Stheraven
1909227825Stheraventemplate <class _Ti, class ..._Uj, size_t ..._Indx>
1910227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1911227825Stheraventypename __mu_return1<true, _Ti, _Uj...>::type
1912227825Stheraven__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1913227825Stheraven{
1914227825Stheraven    __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...);
1915227825Stheraven}
1916227825Stheraven
1917227825Stheraventemplate <class _Ti, class ..._Uj>
1918227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1919227825Stheraventypename enable_if
1920227825Stheraven<
1921227825Stheraven    is_bind_expression<_Ti>::value,
1922227825Stheraven    typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1923227825Stheraven>::type
1924227825Stheraven__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1925227825Stheraven{
1926227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1927227825Stheraven    return  __mu_expand(__ti, __uj, __indices());
1928227825Stheraven}
1929227825Stheraven
1930227825Stheraventemplate <bool IsPh, class _Ti, class _Uj>
1931227825Stheravenstruct __mu_return2 {};
1932227825Stheraven
1933227825Stheraventemplate <class _Ti, class _Uj>
1934227825Stheravenstruct __mu_return2<true, _Ti, _Uj>
1935227825Stheraven{
1936227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1937227825Stheraven};
1938227825Stheraven
1939227825Stheraventemplate <class _Ti, class _Uj>
1940227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1941227825Stheraventypename enable_if
1942227825Stheraven<
1943227825Stheraven    0 < is_placeholder<_Ti>::value,
1944227825Stheraven    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1945227825Stheraven>::type
1946227825Stheraven__mu(_Ti&, _Uj& __uj)
1947227825Stheraven{
1948227825Stheraven    const size_t _Indx = is_placeholder<_Ti>::value - 1;
1949227825Stheraven    // compiler bug workaround
1950227825Stheraven    typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj);
1951227825Stheraven    return __t;
1952227825Stheraven//    return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj));
1953227825Stheraven}
1954227825Stheraven
1955227825Stheraventemplate <class _Ti, class _Uj>
1956227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1957227825Stheraventypename enable_if
1958227825Stheraven<
1959227825Stheraven    !is_bind_expression<_Ti>::value &&
1960227825Stheraven    is_placeholder<_Ti>::value == 0 &&
1961227825Stheraven    !__is_reference_wrapper<_Ti>::value,
1962227825Stheraven    _Ti&
1963227825Stheraven>::type
1964227825Stheraven__mu(_Ti& __ti, _Uj& __uj)
1965227825Stheraven{
1966227825Stheraven    return __ti;
1967227825Stheraven}
1968227825Stheraven
1969227825Stheraventemplate <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1970227825Stheravenstruct ____mu_return;
1971227825Stheraven
1972227825Stheraventemplate <class _Ti, class ..._Uj>
1973227825Stheravenstruct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1974227825Stheraven{
1975227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1976227825Stheraven};
1977227825Stheraven
1978227825Stheraventemplate <class _Ti, class _TupleUj>
1979227825Stheravenstruct ____mu_return<_Ti, false, true, _TupleUj>
1980227825Stheraven{
1981227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1982227825Stheraven                                   _TupleUj>::type&& type;
1983227825Stheraven};
1984227825Stheraven
1985227825Stheraventemplate <class _Ti, class _TupleUj>
1986227825Stheravenstruct ____mu_return<_Ti, false, false, _TupleUj>
1987227825Stheraven{
1988227825Stheraven    typedef _Ti& type;
1989227825Stheraven};
1990227825Stheraven
1991227825Stheraventemplate <class _Ti, class _TupleUj>
1992227825Stheravenstruct __mu_return
1993227825Stheraven    : public ____mu_return<_Ti,
1994227825Stheraven                           is_bind_expression<_Ti>::value,
1995227825Stheraven                           0 < is_placeholder<_Ti>::value,
1996227825Stheraven                           _TupleUj>
1997227825Stheraven{
1998227825Stheraven};
1999227825Stheraven
2000227825Stheraventemplate <class _Ti, class _TupleUj>
2001227825Stheravenstruct __mu_return<reference_wrapper<_Ti>, _TupleUj>
2002227825Stheraven{
2003227825Stheraven    typedef _Ti& type;
2004227825Stheraven};
2005227825Stheraven
2006227825Stheraventemplate <class _F, class _BoundArgs, class _TupleUj>
2007227825Stheravenstruct __bind_return;
2008227825Stheraven
2009227825Stheraventemplate <class _F, class ..._BoundArgs, class _TupleUj>
2010227825Stheravenstruct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj>
2011227825Stheraven{
2012227825Stheraven    typedef typename __ref_return
2013227825Stheraven    <
2014227825Stheraven        _F&,
2015227825Stheraven        typename __mu_return
2016227825Stheraven        <
2017227825Stheraven            _BoundArgs,
2018227825Stheraven            _TupleUj
2019227825Stheraven        >::type...
2020227825Stheraven    >::type type;
2021227825Stheraven};
2022227825Stheraven
2023227825Stheraventemplate <class _F, class ..._BoundArgs, class _TupleUj>
2024227825Stheravenstruct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj>
2025227825Stheraven{
2026227825Stheraven    typedef typename __ref_return
2027227825Stheraven    <
2028227825Stheraven        _F&,
2029227825Stheraven        typename __mu_return
2030227825Stheraven        <
2031227825Stheraven            const _BoundArgs,
2032227825Stheraven            _TupleUj
2033227825Stheraven        >::type...
2034227825Stheraven    >::type type;
2035227825Stheraven};
2036227825Stheraven
2037227825Stheraventemplate <class _F, class _BoundArgs, size_t ..._Indx, class _Args>
2038227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2039227825Stheraventypename __bind_return<_F, _BoundArgs, _Args>::type
2040227825Stheraven__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2041227825Stheraven                _Args&& __args)
2042227825Stheraven{
2043227825Stheraven    return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
2044227825Stheraven}
2045227825Stheraven
2046227825Stheraventemplate<class _F, class ..._BoundArgs>
2047227825Stheravenclass __bind
2048227825Stheraven{
2049227825Stheraven    _F __f_;
2050227825Stheraven    tuple<_BoundArgs...> __bound_args_;
2051227825Stheraven
2052227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2053227825Stheravenpublic:
2054227825Stheraven    template <class _G, class ..._BA>
2055227825Stheraven      explicit __bind(_G&& __f, _BA&& ...__bound_args)
2056227825Stheraven        : __f_(_VSTD::forward<_G>(__f)),
2057227825Stheraven          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
2058227825Stheraven
2059227825Stheraven    template <class ..._Args>
2060227825Stheraven        typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2061227825Stheraven        operator()(_Args&& ...__args)
2062227825Stheraven        {
2063227825Stheraven            // compiler bug workaround
2064227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2065227825Stheraven                                  tuple<_Args&&...>(__args...));
2066227825Stheraven        }
2067227825Stheraven
2068227825Stheraven    template <class ..._Args>
2069227825Stheraven        typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2070227825Stheraven        operator()(_Args&& ...__args) const
2071227825Stheraven        {
2072227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2073227825Stheraven                                   tuple<_Args&&...>(__args...));
2074227825Stheraven        }
2075227825Stheraven};
2076227825Stheraven
2077227825Stheraventemplate<class _F, class ..._BoundArgs>
2078227825Stheravenstruct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {};
2079227825Stheraven
2080227825Stheraventemplate<class _R, class _F, class ..._BoundArgs>
2081227825Stheravenclass __bind_r
2082227825Stheraven    : public __bind<_F, _BoundArgs...>
2083227825Stheraven{
2084227825Stheraven    typedef __bind<_F, _BoundArgs...> base;
2085227825Stheravenpublic:
2086227825Stheraven    typedef _R result_type;
2087227825Stheraven
2088227825Stheraven    template <class _G, class ..._BA>
2089227825Stheraven      explicit __bind_r(_G&& __f, _BA&& ...__bound_args)
2090227825Stheraven        : base(_VSTD::forward<_G>(__f),
2091227825Stheraven               _VSTD::forward<_BA>(__bound_args)...) {}
2092227825Stheraven
2093227825Stheraven    template <class ..._Args>
2094227825Stheraven        result_type
2095227825Stheraven        operator()(_Args&& ...__args)
2096227825Stheraven        {
2097227825Stheraven            return base::operator()(_VSTD::forward<_Args>(__args)...);
2098227825Stheraven        }
2099227825Stheraven
2100227825Stheraven    template <class ..._Args>
2101227825Stheraven        result_type
2102227825Stheraven        operator()(_Args&& ...__args) const
2103227825Stheraven        {
2104227825Stheraven            return base::operator()(_VSTD::forward<_Args>(__args)...);
2105227825Stheraven        }
2106227825Stheraven};
2107227825Stheraven
2108227825Stheraventemplate<class _R, class _F, class ..._BoundArgs>
2109227825Stheravenstruct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {};
2110227825Stheraven
2111227825Stheraventemplate<class _F, class ..._BoundArgs>
2112227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2113227825Stheraven__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...>
2114227825Stheravenbind(_F&& __f, _BoundArgs&&... __bound_args)
2115227825Stheraven{
2116227825Stheraven    typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
2117227825Stheraven    return type(_VSTD::forward<_F>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2118227825Stheraven}
2119227825Stheraven
2120227825Stheraventemplate<class _R, class _F, class ..._BoundArgs>
2121227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2122227825Stheraven__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...>
2123227825Stheravenbind(_F&& __f, _BoundArgs&&... __bound_args)
2124227825Stheraven{
2125227825Stheraven    typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type;
2126227825Stheraven    return type(_VSTD::forward<_F>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2127227825Stheraven}
2128227825Stheraven*/
2129227825Stheraven
2130227825Stheraven#endif  // _LIBCPP_FUNCTIONAL_03
2131