__functional_03 revision 288943
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
36276792Sdim       operator() () const
37227825Stheraven       {
38227825Stheraven           return __invoke(__f_);
39227825Stheraven       }
40227825Stheraven
41227825Stheraven    template <class _A0>
42227825Stheraven       typename __invoke_return0<type, _A0>::type
43276792Sdim          operator() (_A0& __a0) const
44227825Stheraven          {
45227825Stheraven              return __invoke(__f_, __a0);
46227825Stheraven          }
47227825Stheraven
48227825Stheraven    template <class _A0, class _A1>
49227825Stheraven       typename __invoke_return1<type, _A0, _A1>::type
50276792Sdim          operator() (_A0& __a0, _A1& __a1) const
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
57276792Sdim          operator() (_A0& __a0, _A1& __a1, _A2& __a2) const
58227825Stheraven          {
59227825Stheraven              return __invoke(__f_, __a0, __a1, __a2);
60227825Stheraven          }
61227825Stheraven};
62227825Stheraven
63232924Stheraventemplate<class _Rp, class _Tp>
64227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
65232924Stheraven__mem_fn<_Rp _Tp::*>
66232924Stheravenmem_fn(_Rp _Tp::* __pm)
67227825Stheraven{
68232924Stheraven    return __mem_fn<_Rp _Tp::*>(__pm);
69227825Stheraven}
70227825Stheraven
71232924Stheraventemplate<class _Rp, class _Tp>
72227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
73232924Stheraven__mem_fn<_Rp (_Tp::*)()>
74232924Stheravenmem_fn(_Rp (_Tp::* __pm)())
75227825Stheraven{
76232924Stheraven    return __mem_fn<_Rp (_Tp::*)()>(__pm);
77227825Stheraven}
78227825Stheraven
79232924Stheraventemplate<class _Rp, class _Tp, class _A0>
80227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
81232924Stheraven__mem_fn<_Rp (_Tp::*)(_A0)>
82232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0))
83227825Stheraven{
84232924Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0)>(__pm);
85227825Stheraven}
86227825Stheraven
87232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
88227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
89232924Stheraven__mem_fn<_Rp (_Tp::*)(_A0, _A1)>
90232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1))
91227825Stheraven{
92232924Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0, _A1)>(__pm);
93227825Stheraven}
94227825Stheraven
95232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
96227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
97232924Stheraven__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>
98232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2))
99227825Stheraven{
100232924Stheraven    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2)>(__pm);
101227825Stheraven}
102227825Stheraven
103232924Stheraventemplate<class _Rp, class _Tp>
104227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
105261272Sdim__mem_fn<_Rp (_Tp::*)() const>
106232924Stheravenmem_fn(_Rp (_Tp::* __pm)() const)
107227825Stheraven{
108261272Sdim    return __mem_fn<_Rp (_Tp::*)() const>(__pm);
109227825Stheraven}
110227825Stheraven
111232924Stheraventemplate<class _Rp, class _Tp, class _A0>
112227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
113261272Sdim__mem_fn<_Rp (_Tp::*)(_A0) const>
114232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) const)
115227825Stheraven{
116261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) const>(__pm);
117227825Stheraven}
118227825Stheraven
119232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
120227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
121261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) const>
122232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const)
123227825Stheraven{
124261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const>(__pm);
125227825Stheraven}
126227825Stheraven
127232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
128227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
129261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>
130232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const)
131227825Stheraven{
132261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const>(__pm);
133227825Stheraven}
134227825Stheraven
135232924Stheraventemplate<class _Rp, class _Tp>
136227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
137261272Sdim__mem_fn<_Rp (_Tp::*)() volatile>
138232924Stheravenmem_fn(_Rp (_Tp::* __pm)() volatile)
139227825Stheraven{
140261272Sdim    return __mem_fn<_Rp (_Tp::*)() volatile>(__pm);
141227825Stheraven}
142227825Stheraven
143232924Stheraventemplate<class _Rp, class _Tp, class _A0>
144227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
145261272Sdim__mem_fn<_Rp (_Tp::*)(_A0) volatile>
146232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) volatile)
147227825Stheraven{
148261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) volatile>(__pm);
149227825Stheraven}
150227825Stheraven
151232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
152227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
153261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>
154232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) volatile)
155227825Stheraven{
156261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) volatile>(__pm);
157227825Stheraven}
158227825Stheraven
159232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
160227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
161261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>
162232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) volatile)
163227825Stheraven{
164261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) volatile>(__pm);
165227825Stheraven}
166227825Stheraven
167232924Stheraventemplate<class _Rp, class _Tp>
168227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
169261272Sdim__mem_fn<_Rp (_Tp::*)() const volatile>
170232924Stheravenmem_fn(_Rp (_Tp::* __pm)() const volatile)
171227825Stheraven{
172261272Sdim    return __mem_fn<_Rp (_Tp::*)() const volatile>(__pm);
173227825Stheraven}
174227825Stheraven
175232924Stheraventemplate<class _Rp, class _Tp, class _A0>
176227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
177261272Sdim__mem_fn<_Rp (_Tp::*)(_A0) const volatile>
178232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0) const volatile)
179227825Stheraven{
180261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0) const volatile>(__pm);
181227825Stheraven}
182227825Stheraven
183232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1>
184227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
185261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>
186232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1) const volatile)
187227825Stheraven{
188261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1) const volatile>(__pm);
189227825Stheraven}
190227825Stheraven
191232924Stheraventemplate<class _Rp, class _Tp, class _A0, class _A1, class _A2>
192227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
193261272Sdim__mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>
194232924Stheravenmem_fn(_Rp (_Tp::* __pm)(_A0, _A1, _A2) const volatile)
195227825Stheraven{
196261272Sdim    return __mem_fn<_Rp (_Tp::*)(_A0, _A1, _A2) const volatile>(__pm);
197227825Stheraven}
198227825Stheraven
199227825Stheraven// bad_function_call
200227825Stheraven
201227825Stheravenclass _LIBCPP_EXCEPTION_ABI bad_function_call
202227825Stheraven    : public exception
203227825Stheraven{
204227825Stheraven};
205227825Stheraven
206261272Sdimtemplate<class _Fp> class _LIBCPP_TYPE_VIS_ONLY function; // undefined
207227825Stheraven
208227825Stheravennamespace __function
209227825Stheraven{
210227825Stheraven
211232924Stheraventemplate<class _Fp>
212227825Stheravenstruct __maybe_derive_from_unary_function
213227825Stheraven{
214227825Stheraven};
215227825Stheraven
216232924Stheraventemplate<class _Rp, class _A1>
217232924Stheravenstruct __maybe_derive_from_unary_function<_Rp(_A1)>
218232924Stheraven    : public unary_function<_A1, _Rp>
219227825Stheraven{
220227825Stheraven};
221227825Stheraven
222232924Stheraventemplate<class _Fp>
223227825Stheravenstruct __maybe_derive_from_binary_function
224227825Stheraven{
225227825Stheraven};
226227825Stheraven
227232924Stheraventemplate<class _Rp, class _A1, class _A2>
228232924Stheravenstruct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
229232924Stheraven    : public binary_function<_A1, _A2, _Rp>
230227825Stheraven{
231227825Stheraven};
232227825Stheraven
233227825Stheraventemplate<class _Fp> class __base;
234227825Stheraven
235232924Stheraventemplate<class _Rp>
236232924Stheravenclass __base<_Rp()>
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;
247232924Stheraven    virtual _Rp 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
254232924Stheraventemplate<class _Rp, class _A0>
255232924Stheravenclass __base<_Rp(_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;
266232924Stheraven    virtual _Rp 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
273232924Stheraventemplate<class _Rp, class _A0, class _A1>
274232924Stheravenclass __base<_Rp(_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;
285232924Stheraven    virtual _Rp 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
292232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
293232924Stheravenclass __base<_Rp(_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;
304232924Stheraven    virtual _Rp 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
313232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
314232924Stheravenclass __func<_Fp, _Alloc, _Rp()>
315232924Stheraven    : public  __base<_Rp()>
316227825Stheraven{
317232924Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
318227825Stheravenpublic:
319232924Stheraven    explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
320232924Stheraven    explicit __func(_Fp __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
321232924Stheraven    virtual __base<_Rp()>* __clone() const;
322232924Stheraven    virtual void __clone(__base<_Rp()>*) const;
323227825Stheraven    virtual void destroy();
324227825Stheraven    virtual void destroy_deallocate();
325232924Stheraven    virtual _Rp 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
332232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
333232924Stheraven__base<_Rp()>*
334232924Stheraven__func<_Fp, _Alloc, _Rp()>::__clone() const
335227825Stheraven{
336288943Sdim    typedef allocator_traits<_Alloc> __alloc_traits;
337288943Sdim    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
338232924Stheraven    _Ap __a(__f_.second());
339232924Stheraven    typedef __allocator_destructor<_Ap> _Dp;
340232924Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
341227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
342227825Stheraven    return __hold.release();
343227825Stheraven}
344227825Stheraven
345232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
346227825Stheravenvoid
347232924Stheraven__func<_Fp, _Alloc, _Rp()>::__clone(__base<_Rp()>* __p) const
348227825Stheraven{
349227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
350227825Stheraven}
351227825Stheraven
352232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
353227825Stheravenvoid
354232924Stheraven__func<_Fp, _Alloc, _Rp()>::destroy()
355227825Stheraven{
356232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
357227825Stheraven}
358227825Stheraven
359232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
360227825Stheravenvoid
361232924Stheraven__func<_Fp, _Alloc, _Rp()>::destroy_deallocate()
362227825Stheraven{
363288943Sdim    typedef allocator_traits<_Alloc> __alloc_traits;
364288943Sdim    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
365232924Stheraven    _Ap __a(__f_.second());
366232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
367227825Stheraven    __a.deallocate(this, 1);
368227825Stheraven}
369227825Stheraven
370232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
371232924Stheraven_Rp
372232924Stheraven__func<_Fp, _Alloc, _Rp()>::operator()()
373227825Stheraven{
374288943Sdim    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
375288943Sdim    return _Invoker::__call(__f_.first());
376227825Stheraven}
377227825Stheraven
378227825Stheraven#ifndef _LIBCPP_NO_RTTI
379227825Stheraven
380232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
381227825Stheravenconst void*
382232924Stheraven__func<_Fp, _Alloc, _Rp()>::target(const type_info& __ti) const
383227825Stheraven{
384232924Stheraven    if (__ti == typeid(_Fp))
385227825Stheraven        return &__f_.first();
386227825Stheraven    return (const void*)0;
387227825Stheraven}
388227825Stheraven
389232924Stheraventemplate<class _Fp, class _Alloc, class _Rp>
390227825Stheravenconst std::type_info&
391232924Stheraven__func<_Fp, _Alloc, _Rp()>::target_type() const
392227825Stheraven{
393232924Stheraven    return typeid(_Fp);
394227825Stheraven}
395227825Stheraven
396227825Stheraven#endif  // _LIBCPP_NO_RTTI
397227825Stheraven
398232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
399232924Stheravenclass __func<_Fp, _Alloc, _Rp(_A0)>
400232924Stheraven    : public  __base<_Rp(_A0)>
401227825Stheraven{
402232924Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
403227825Stheravenpublic:
404232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
405232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
406227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
407232924Stheraven    virtual __base<_Rp(_A0)>* __clone() const;
408232924Stheraven    virtual void __clone(__base<_Rp(_A0)>*) const;
409227825Stheraven    virtual void destroy();
410227825Stheraven    virtual void destroy_deallocate();
411232924Stheraven    virtual _Rp operator()(_A0);
412227825Stheraven#ifndef _LIBCPP_NO_RTTI
413227825Stheraven    virtual const void* target(const type_info&) const;
414227825Stheraven    virtual const std::type_info& target_type() const;
415227825Stheraven#endif  // _LIBCPP_NO_RTTI
416227825Stheraven};
417227825Stheraven
418232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
419232924Stheraven__base<_Rp(_A0)>*
420232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::__clone() const
421227825Stheraven{
422288943Sdim    typedef allocator_traits<_Alloc> __alloc_traits;
423288943Sdim    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
424232924Stheraven    _Ap __a(__f_.second());
425232924Stheraven    typedef __allocator_destructor<_Ap> _Dp;
426232924Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
427227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
428227825Stheraven    return __hold.release();
429227825Stheraven}
430227825Stheraven
431232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
432227825Stheravenvoid
433232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::__clone(__base<_Rp(_A0)>* __p) const
434227825Stheraven{
435227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
436227825Stheraven}
437227825Stheraven
438232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
439227825Stheravenvoid
440232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::destroy()
441227825Stheraven{
442232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
443227825Stheraven}
444227825Stheraven
445232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
446227825Stheravenvoid
447232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::destroy_deallocate()
448227825Stheraven{
449288943Sdim    typedef allocator_traits<_Alloc> __alloc_traits;
450288943Sdim    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
451232924Stheraven    _Ap __a(__f_.second());
452232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
453227825Stheraven    __a.deallocate(this, 1);
454227825Stheraven}
455227825Stheraven
456232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
457232924Stheraven_Rp
458232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::operator()(_A0 __a0)
459227825Stheraven{
460288943Sdim    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
461288943Sdim    return _Invoker::__call(__f_.first(), __a0);
462227825Stheraven}
463227825Stheraven
464227825Stheraven#ifndef _LIBCPP_NO_RTTI
465227825Stheraven
466232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
467227825Stheravenconst void*
468232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::target(const type_info& __ti) const
469227825Stheraven{
470232924Stheraven    if (__ti == typeid(_Fp))
471227825Stheraven        return &__f_.first();
472227825Stheraven    return (const void*)0;
473227825Stheraven}
474227825Stheraven
475232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0>
476227825Stheravenconst std::type_info&
477232924Stheraven__func<_Fp, _Alloc, _Rp(_A0)>::target_type() const
478227825Stheraven{
479232924Stheraven    return typeid(_Fp);
480227825Stheraven}
481227825Stheraven
482227825Stheraven#endif  // _LIBCPP_NO_RTTI
483227825Stheraven
484232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
485232924Stheravenclass __func<_Fp, _Alloc, _Rp(_A0, _A1)>
486232924Stheraven    : public  __base<_Rp(_A0, _A1)>
487227825Stheraven{
488232924Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
489227825Stheravenpublic:
490232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
491232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
492227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
493232924Stheraven    virtual __base<_Rp(_A0, _A1)>* __clone() const;
494232924Stheraven    virtual void __clone(__base<_Rp(_A0, _A1)>*) const;
495227825Stheraven    virtual void destroy();
496227825Stheraven    virtual void destroy_deallocate();
497232924Stheraven    virtual _Rp operator()(_A0, _A1);
498227825Stheraven#ifndef _LIBCPP_NO_RTTI
499227825Stheraven    virtual const void* target(const type_info&) const;
500227825Stheraven    virtual const std::type_info& target_type() const;
501227825Stheraven#endif  // _LIBCPP_NO_RTTI
502227825Stheraven};
503227825Stheraven
504232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
505232924Stheraven__base<_Rp(_A0, _A1)>*
506232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone() const
507227825Stheraven{
508288943Sdim    typedef allocator_traits<_Alloc> __alloc_traits;
509288943Sdim    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
510232924Stheraven    _Ap __a(__f_.second());
511232924Stheraven    typedef __allocator_destructor<_Ap> _Dp;
512232924Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
513227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
514227825Stheraven    return __hold.release();
515227825Stheraven}
516227825Stheraven
517232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
518227825Stheravenvoid
519232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::__clone(__base<_Rp(_A0, _A1)>* __p) const
520227825Stheraven{
521227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
522227825Stheraven}
523227825Stheraven
524232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
525227825Stheravenvoid
526232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy()
527227825Stheraven{
528232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
529227825Stheraven}
530227825Stheraven
531232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
532227825Stheravenvoid
533232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::destroy_deallocate()
534227825Stheraven{
535288943Sdim    typedef allocator_traits<_Alloc> __alloc_traits;
536288943Sdim    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
537232924Stheraven    _Ap __a(__f_.second());
538232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
539227825Stheraven    __a.deallocate(this, 1);
540227825Stheraven}
541227825Stheraven
542232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
543232924Stheraven_Rp
544232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1)
545227825Stheraven{
546288943Sdim    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
547288943Sdim    return _Invoker::__call(__f_.first(), __a0, __a1);
548227825Stheraven}
549227825Stheraven
550227825Stheraven#ifndef _LIBCPP_NO_RTTI
551227825Stheraven
552232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
553227825Stheravenconst void*
554232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target(const type_info& __ti) const
555227825Stheraven{
556232924Stheraven    if (__ti == typeid(_Fp))
557227825Stheraven        return &__f_.first();
558227825Stheraven    return (const void*)0;
559227825Stheraven}
560227825Stheraven
561232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1>
562227825Stheravenconst std::type_info&
563232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1)>::target_type() const
564227825Stheraven{
565232924Stheraven    return typeid(_Fp);
566227825Stheraven}
567227825Stheraven
568227825Stheraven#endif  // _LIBCPP_NO_RTTI
569227825Stheraven
570232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
571232924Stheravenclass __func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>
572232924Stheraven    : public  __base<_Rp(_A0, _A1, _A2)>
573227825Stheraven{
574232924Stheraven    __compressed_pair<_Fp, _Alloc> __f_;
575227825Stheravenpublic:
576232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f) : __f_(_VSTD::move(__f)) {}
577232924Stheraven    _LIBCPP_INLINE_VISIBILITY explicit __func(_Fp __f, _Alloc __a)
578227825Stheraven        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
579232924Stheraven    virtual __base<_Rp(_A0, _A1, _A2)>* __clone() const;
580232924Stheraven    virtual void __clone(__base<_Rp(_A0, _A1, _A2)>*) const;
581227825Stheraven    virtual void destroy();
582227825Stheraven    virtual void destroy_deallocate();
583232924Stheraven    virtual _Rp operator()(_A0, _A1, _A2);
584227825Stheraven#ifndef _LIBCPP_NO_RTTI
585227825Stheraven    virtual const void* target(const type_info&) const;
586227825Stheraven    virtual const std::type_info& target_type() const;
587227825Stheraven#endif  // _LIBCPP_NO_RTTI
588227825Stheraven};
589227825Stheraven
590232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
591232924Stheraven__base<_Rp(_A0, _A1, _A2)>*
592232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone() const
593227825Stheraven{
594288943Sdim    typedef allocator_traits<_Alloc> __alloc_traits;
595288943Sdim    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
596232924Stheraven    _Ap __a(__f_.second());
597232924Stheraven    typedef __allocator_destructor<_Ap> _Dp;
598232924Stheraven    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
599227825Stheraven    ::new (__hold.get()) __func(__f_.first(), _Alloc(__a));
600227825Stheraven    return __hold.release();
601227825Stheraven}
602227825Stheraven
603232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
604227825Stheravenvoid
605232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::__clone(__base<_Rp(_A0, _A1, _A2)>* __p) const
606227825Stheraven{
607227825Stheraven    ::new (__p) __func(__f_.first(), __f_.second());
608227825Stheraven}
609227825Stheraven
610232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
611227825Stheravenvoid
612232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy()
613227825Stheraven{
614232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
615227825Stheraven}
616227825Stheraven
617232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
618227825Stheravenvoid
619232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::destroy_deallocate()
620227825Stheraven{
621288943Sdim    typedef allocator_traits<_Alloc> __alloc_traits;
622288943Sdim    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
623232924Stheraven    _Ap __a(__f_.second());
624232924Stheraven    __f_.~__compressed_pair<_Fp, _Alloc>();
625227825Stheraven    __a.deallocate(this, 1);
626227825Stheraven}
627227825Stheraven
628232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
629232924Stheraven_Rp
630232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2)
631227825Stheraven{
632288943Sdim    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
633288943Sdim    return _Invoker::__call(__f_.first(), __a0, __a1, __a2);
634227825Stheraven}
635227825Stheraven
636227825Stheraven#ifndef _LIBCPP_NO_RTTI
637227825Stheraven
638232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
639227825Stheravenconst void*
640232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target(const type_info& __ti) const
641227825Stheraven{
642232924Stheraven    if (__ti == typeid(_Fp))
643227825Stheraven        return &__f_.first();
644227825Stheraven    return (const void*)0;
645227825Stheraven}
646227825Stheraven
647232924Stheraventemplate<class _Fp, class _Alloc, class _Rp, class _A0, class _A1, class _A2>
648227825Stheravenconst std::type_info&
649232924Stheraven__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)>::target_type() const
650227825Stheraven{
651232924Stheraven    return typeid(_Fp);
652227825Stheraven}
653227825Stheraven
654227825Stheraven#endif  // _LIBCPP_NO_RTTI
655227825Stheraven
656227825Stheraven}  // __function
657227825Stheraven
658232924Stheraventemplate<class _Rp>
659261272Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp()>
660227825Stheraven{
661232924Stheraven    typedef __function::__base<_Rp()> __base;
662227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
663227825Stheraven    __base* __f_;
664227825Stheraven
665232924Stheraven    template <class _Fp>
666276792Sdim        _LIBCPP_INLINE_VISIBILITY
667232924Stheraven        static bool __not_null(const _Fp&) {return true;}
668227825Stheraven    template <class _R2>
669276792Sdim        _LIBCPP_INLINE_VISIBILITY
670276792Sdim        static bool __not_null(_R2 (*__p)()) {return __p;}
671276792Sdim    template <class _R2>
672276792Sdim        _LIBCPP_INLINE_VISIBILITY
673276792Sdim        static bool __not_null(const function<_R2()>& __p) {return __p;}
674227825Stheravenpublic:
675232924Stheraven    typedef _Rp result_type;
676227825Stheraven
677227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
678227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
679227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
680227825Stheraven    function(const function&);
681232924Stheraven    template<class _Fp>
682232924Stheraven      function(_Fp,
683232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
684227825Stheraven
685227825Stheraven    template<class _Alloc>
686227825Stheraven      _LIBCPP_INLINE_VISIBILITY
687227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
688227825Stheraven    template<class _Alloc>
689227825Stheraven      _LIBCPP_INLINE_VISIBILITY
690227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
691227825Stheraven    template<class _Alloc>
692227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
693232924Stheraven    template<class _Fp, class _Alloc>
694232924Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
695232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
696227825Stheraven
697227825Stheraven    function& operator=(const function&);
698227825Stheraven    function& operator=(nullptr_t);
699232924Stheraven    template<class _Fp>
700227825Stheraven      typename enable_if
701227825Stheraven      <
702232924Stheraven        !is_integral<_Fp>::value,
703227825Stheraven        function&
704227825Stheraven      >::type
705232924Stheraven      operator=(_Fp);
706227825Stheraven
707227825Stheraven    ~function();
708227825Stheraven
709227825Stheraven    // 20.7.16.2.2, function modifiers:
710227825Stheraven    void swap(function&);
711232924Stheraven    template<class _Fp, class _Alloc>
712227825Stheraven      _LIBCPP_INLINE_VISIBILITY
713232924Stheraven      void assign(_Fp __f, const _Alloc& __a)
714227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
715227825Stheraven
716227825Stheraven    // 20.7.16.2.3, function capacity:
717227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
718227825Stheraven
719227825Stheravenprivate:
720227825Stheraven    // deleted overloads close possible hole in the type system
721227825Stheraven    template<class _R2>
722227825Stheraven      bool operator==(const function<_R2()>&) const;// = delete;
723227825Stheraven    template<class _R2>
724227825Stheraven      bool operator!=(const function<_R2()>&) const;// = delete;
725227825Stheravenpublic:
726227825Stheraven    // 20.7.16.2.4, function invocation:
727232924Stheraven    _Rp operator()() const;
728227825Stheraven
729227825Stheraven#ifndef _LIBCPP_NO_RTTI
730227825Stheraven    // 20.7.16.2.5, function target access:
731227825Stheraven    const std::type_info& target_type() const;
732232924Stheraven    template <typename _Tp> _Tp* target();
733232924Stheraven    template <typename _Tp> const _Tp* target() const;
734227825Stheraven#endif  // _LIBCPP_NO_RTTI
735227825Stheraven};
736227825Stheraven
737232924Stheraventemplate<class _Rp>
738232924Stheravenfunction<_Rp()>::function(const function& __f)
739227825Stheraven{
740227825Stheraven    if (__f.__f_ == 0)
741227825Stheraven        __f_ = 0;
742227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
743227825Stheraven    {
744227825Stheraven        __f_ = (__base*)&__buf_;
745227825Stheraven        __f.__f_->__clone(__f_);
746227825Stheraven    }
747227825Stheraven    else
748227825Stheraven        __f_ = __f.__f_->__clone();
749227825Stheraven}
750227825Stheraven
751232924Stheraventemplate<class _Rp>
752227825Stheraventemplate<class _Alloc>
753232924Stheravenfunction<_Rp()>::function(allocator_arg_t, const _Alloc&, const function& __f)
754227825Stheraven{
755227825Stheraven    if (__f.__f_ == 0)
756227825Stheraven        __f_ = 0;
757227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
758227825Stheraven    {
759227825Stheraven        __f_ = (__base*)&__buf_;
760227825Stheraven        __f.__f_->__clone(__f_);
761227825Stheraven    }
762227825Stheraven    else
763227825Stheraven        __f_ = __f.__f_->__clone();
764227825Stheraven}
765227825Stheraven
766232924Stheraventemplate<class _Rp>
767232924Stheraventemplate <class _Fp>
768232924Stheravenfunction<_Rp()>::function(_Fp __f,
769232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
770227825Stheraven    : __f_(0)
771227825Stheraven{
772227825Stheraven    if (__not_null(__f))
773227825Stheraven    {
774232924Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp()> _FF;
775227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
776227825Stheraven        {
777227825Stheraven            __f_ = (__base*)&__buf_;
778227825Stheraven            ::new (__f_) _FF(__f);
779227825Stheraven        }
780227825Stheraven        else
781227825Stheraven        {
782232924Stheraven            typedef allocator<_FF> _Ap;
783232924Stheraven            _Ap __a;
784232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
785232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
786232924Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
787227825Stheraven            __f_ = __hold.release();
788227825Stheraven        }
789227825Stheraven    }
790227825Stheraven}
791227825Stheraven
792232924Stheraventemplate<class _Rp>
793232924Stheraventemplate <class _Fp, class _Alloc>
794232924Stheravenfunction<_Rp()>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
795232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
796227825Stheraven    : __f_(0)
797227825Stheraven{
798227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
799227825Stheraven    if (__not_null(__f))
800227825Stheraven    {
801232924Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp()> _FF;
802227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
803227825Stheraven        {
804227825Stheraven            __f_ = (__base*)&__buf_;
805288943Sdim            ::new (__f_) _FF(__f, __a0);
806227825Stheraven        }
807227825Stheraven        else
808227825Stheraven        {
809288943Sdim            typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
810232924Stheraven            _Ap __a(__a0);
811232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
812232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
813227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
814227825Stheraven            __f_ = __hold.release();
815227825Stheraven        }
816227825Stheraven    }
817227825Stheraven}
818227825Stheraven
819232924Stheraventemplate<class _Rp>
820232924Stheravenfunction<_Rp()>&
821232924Stheravenfunction<_Rp()>::operator=(const function& __f)
822227825Stheraven{
823227825Stheraven    function(__f).swap(*this);
824227825Stheraven    return *this;
825227825Stheraven}
826227825Stheraven
827232924Stheraventemplate<class _Rp>
828232924Stheravenfunction<_Rp()>&
829232924Stheravenfunction<_Rp()>::operator=(nullptr_t)
830227825Stheraven{
831227825Stheraven    if (__f_ == (__base*)&__buf_)
832227825Stheraven        __f_->destroy();
833227825Stheraven    else if (__f_)
834227825Stheraven        __f_->destroy_deallocate();
835227825Stheraven    __f_ = 0;
836288943Sdim    return *this;
837227825Stheraven}
838227825Stheraven
839232924Stheraventemplate<class _Rp>
840232924Stheraventemplate <class _Fp>
841227825Stheraventypename enable_if
842227825Stheraven<
843232924Stheraven    !is_integral<_Fp>::value,
844232924Stheraven    function<_Rp()>&
845227825Stheraven>::type
846232924Stheravenfunction<_Rp()>::operator=(_Fp __f)
847227825Stheraven{
848227825Stheraven    function(_VSTD::move(__f)).swap(*this);
849227825Stheraven    return *this;
850227825Stheraven}
851227825Stheraven
852232924Stheraventemplate<class _Rp>
853232924Stheravenfunction<_Rp()>::~function()
854227825Stheraven{
855227825Stheraven    if (__f_ == (__base*)&__buf_)
856227825Stheraven        __f_->destroy();
857227825Stheraven    else if (__f_)
858227825Stheraven        __f_->destroy_deallocate();
859227825Stheraven}
860227825Stheraven
861232924Stheraventemplate<class _Rp>
862227825Stheravenvoid
863232924Stheravenfunction<_Rp()>::swap(function& __f)
864227825Stheraven{
865227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
866227825Stheraven    {
867227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
868227825Stheraven        __base* __t = (__base*)&__tempbuf;
869227825Stheraven        __f_->__clone(__t);
870227825Stheraven        __f_->destroy();
871227825Stheraven        __f_ = 0;
872227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
873227825Stheraven        __f.__f_->destroy();
874227825Stheraven        __f.__f_ = 0;
875227825Stheraven        __f_ = (__base*)&__buf_;
876227825Stheraven        __t->__clone((__base*)&__f.__buf_);
877227825Stheraven        __t->destroy();
878227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
879227825Stheraven    }
880227825Stheraven    else if (__f_ == (__base*)&__buf_)
881227825Stheraven    {
882227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
883227825Stheraven        __f_->destroy();
884227825Stheraven        __f_ = __f.__f_;
885227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
886227825Stheraven    }
887227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
888227825Stheraven    {
889227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
890227825Stheraven        __f.__f_->destroy();
891227825Stheraven        __f.__f_ = __f_;
892227825Stheraven        __f_ = (__base*)&__buf_;
893227825Stheraven    }
894227825Stheraven    else
895227825Stheraven        _VSTD::swap(__f_, __f.__f_);
896227825Stheraven}
897227825Stheraven
898232924Stheraventemplate<class _Rp>
899232924Stheraven_Rp
900232924Stheravenfunction<_Rp()>::operator()() const
901227825Stheraven{
902227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
903227825Stheraven    if (__f_ == 0)
904227825Stheraven        throw bad_function_call();
905227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
906227825Stheraven    return (*__f_)();
907227825Stheraven}
908227825Stheraven
909227825Stheraven#ifndef _LIBCPP_NO_RTTI
910227825Stheraven
911232924Stheraventemplate<class _Rp>
912227825Stheravenconst std::type_info&
913232924Stheravenfunction<_Rp()>::target_type() const
914227825Stheraven{
915227825Stheraven    if (__f_ == 0)
916227825Stheraven        return typeid(void);
917227825Stheraven    return __f_->target_type();
918227825Stheraven}
919227825Stheraven
920232924Stheraventemplate<class _Rp>
921232924Stheraventemplate <typename _Tp>
922232924Stheraven_Tp*
923232924Stheravenfunction<_Rp()>::target()
924227825Stheraven{
925227825Stheraven    if (__f_ == 0)
926232924Stheraven        return (_Tp*)0;
927232924Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
928227825Stheraven}
929227825Stheraven
930232924Stheraventemplate<class _Rp>
931232924Stheraventemplate <typename _Tp>
932232924Stheravenconst _Tp*
933232924Stheravenfunction<_Rp()>::target() const
934227825Stheraven{
935227825Stheraven    if (__f_ == 0)
936232924Stheraven        return (const _Tp*)0;
937232924Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
938227825Stheraven}
939227825Stheraven
940227825Stheraven#endif  // _LIBCPP_NO_RTTI
941227825Stheraven
942232924Stheraventemplate<class _Rp, class _A0>
943261272Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0)>
944232924Stheraven    : public unary_function<_A0, _Rp>
945227825Stheraven{
946232924Stheraven    typedef __function::__base<_Rp(_A0)> __base;
947227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
948227825Stheraven    __base* __f_;
949227825Stheraven
950232924Stheraven    template <class _Fp>
951227825Stheraven        _LIBCPP_INLINE_VISIBILITY
952232924Stheraven        static bool __not_null(const _Fp&) {return true;}
953227825Stheraven    template <class _R2, class _B0>
954227825Stheraven        _LIBCPP_INLINE_VISIBILITY
955227825Stheraven        static bool __not_null(_R2 (*__p)(_B0)) {return __p;}
956232924Stheraven    template <class _R2, class _Cp>
957227825Stheraven        _LIBCPP_INLINE_VISIBILITY
958232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)()) {return __p;}
959232924Stheraven    template <class _R2, class _Cp>
960227825Stheraven        _LIBCPP_INLINE_VISIBILITY
961232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)() const) {return __p;}
962232924Stheraven    template <class _R2, class _Cp>
963227825Stheraven        _LIBCPP_INLINE_VISIBILITY
964232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)() volatile) {return __p;}
965232924Stheraven    template <class _R2, class _Cp>
966227825Stheraven        _LIBCPP_INLINE_VISIBILITY
967232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)() const volatile) {return __p;}
968227825Stheraven    template <class _R2, class _B0>
969227825Stheraven        _LIBCPP_INLINE_VISIBILITY
970276792Sdim        static bool __not_null(const function<_R2(_B0)>& __p) {return __p;}
971227825Stheravenpublic:
972232924Stheraven    typedef _Rp result_type;
973227825Stheraven
974227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
975227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
976227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
977227825Stheraven    function(const function&);
978232924Stheraven    template<class _Fp>
979232924Stheraven      function(_Fp,
980232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
981227825Stheraven
982227825Stheraven    template<class _Alloc>
983227825Stheraven      _LIBCPP_INLINE_VISIBILITY
984227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
985227825Stheraven    template<class _Alloc>
986227825Stheraven      _LIBCPP_INLINE_VISIBILITY
987227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
988227825Stheraven    template<class _Alloc>
989227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
990232924Stheraven    template<class _Fp, class _Alloc>
991232924Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
992232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
993227825Stheraven
994227825Stheraven    function& operator=(const function&);
995227825Stheraven    function& operator=(nullptr_t);
996232924Stheraven    template<class _Fp>
997227825Stheraven      typename enable_if
998227825Stheraven      <
999232924Stheraven        !is_integral<_Fp>::value,
1000227825Stheraven        function&
1001227825Stheraven      >::type
1002232924Stheraven      operator=(_Fp);
1003227825Stheraven
1004227825Stheraven    ~function();
1005227825Stheraven
1006227825Stheraven    // 20.7.16.2.2, function modifiers:
1007227825Stheraven    void swap(function&);
1008232924Stheraven    template<class _Fp, class _Alloc>
1009227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1010232924Stheraven      void assign(_Fp __f, const _Alloc& __a)
1011227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1012227825Stheraven
1013227825Stheraven    // 20.7.16.2.3, function capacity:
1014227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1015227825Stheraven
1016227825Stheravenprivate:
1017227825Stheraven    // deleted overloads close possible hole in the type system
1018227825Stheraven    template<class _R2, class _B0>
1019227825Stheraven      bool operator==(const function<_R2(_B0)>&) const;// = delete;
1020227825Stheraven    template<class _R2, class _B0>
1021227825Stheraven      bool operator!=(const function<_R2(_B0)>&) const;// = delete;
1022227825Stheravenpublic:
1023227825Stheraven    // 20.7.16.2.4, function invocation:
1024232924Stheraven    _Rp operator()(_A0) const;
1025227825Stheraven
1026227825Stheraven#ifndef _LIBCPP_NO_RTTI
1027227825Stheraven    // 20.7.16.2.5, function target access:
1028227825Stheraven    const std::type_info& target_type() const;
1029232924Stheraven    template <typename _Tp> _Tp* target();
1030232924Stheraven    template <typename _Tp> const _Tp* target() const;
1031227825Stheraven#endif  // _LIBCPP_NO_RTTI
1032227825Stheraven};
1033227825Stheraven
1034232924Stheraventemplate<class _Rp, class _A0>
1035232924Stheravenfunction<_Rp(_A0)>::function(const function& __f)
1036227825Stheraven{
1037227825Stheraven    if (__f.__f_ == 0)
1038227825Stheraven        __f_ = 0;
1039227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1040227825Stheraven    {
1041227825Stheraven        __f_ = (__base*)&__buf_;
1042227825Stheraven        __f.__f_->__clone(__f_);
1043227825Stheraven    }
1044227825Stheraven    else
1045227825Stheraven        __f_ = __f.__f_->__clone();
1046227825Stheraven}
1047227825Stheraven
1048232924Stheraventemplate<class _Rp, class _A0>
1049227825Stheraventemplate<class _Alloc>
1050232924Stheravenfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1051227825Stheraven{
1052227825Stheraven    if (__f.__f_ == 0)
1053227825Stheraven        __f_ = 0;
1054227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1055227825Stheraven    {
1056227825Stheraven        __f_ = (__base*)&__buf_;
1057227825Stheraven        __f.__f_->__clone(__f_);
1058227825Stheraven    }
1059227825Stheraven    else
1060227825Stheraven        __f_ = __f.__f_->__clone();
1061227825Stheraven}
1062227825Stheraven
1063232924Stheraventemplate<class _Rp, class _A0>
1064232924Stheraventemplate <class _Fp>
1065232924Stheravenfunction<_Rp(_A0)>::function(_Fp __f,
1066232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1067227825Stheraven    : __f_(0)
1068227825Stheraven{
1069227825Stheraven    if (__not_null(__f))
1070227825Stheraven    {
1071232924Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0)> _FF;
1072227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1073227825Stheraven        {
1074227825Stheraven            __f_ = (__base*)&__buf_;
1075227825Stheraven            ::new (__f_) _FF(__f);
1076227825Stheraven        }
1077227825Stheraven        else
1078227825Stheraven        {
1079232924Stheraven            typedef allocator<_FF> _Ap;
1080232924Stheraven            _Ap __a;
1081232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1082232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1083232924Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1084227825Stheraven            __f_ = __hold.release();
1085227825Stheraven        }
1086227825Stheraven    }
1087227825Stheraven}
1088227825Stheraven
1089232924Stheraventemplate<class _Rp, class _A0>
1090232924Stheraventemplate <class _Fp, class _Alloc>
1091232924Stheravenfunction<_Rp(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1092232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1093227825Stheraven    : __f_(0)
1094227825Stheraven{
1095227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1096227825Stheraven    if (__not_null(__f))
1097227825Stheraven    {
1098232924Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0)> _FF;
1099227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1100227825Stheraven        {
1101227825Stheraven            __f_ = (__base*)&__buf_;
1102288943Sdim            ::new (__f_) _FF(__f, __a0);
1103227825Stheraven        }
1104227825Stheraven        else
1105227825Stheraven        {
1106288943Sdim            typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1107232924Stheraven            _Ap __a(__a0);
1108232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1109232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1110227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1111227825Stheraven            __f_ = __hold.release();
1112227825Stheraven        }
1113227825Stheraven    }
1114227825Stheraven}
1115227825Stheraven
1116232924Stheraventemplate<class _Rp, class _A0>
1117232924Stheravenfunction<_Rp(_A0)>&
1118232924Stheravenfunction<_Rp(_A0)>::operator=(const function& __f)
1119227825Stheraven{
1120227825Stheraven    function(__f).swap(*this);
1121227825Stheraven    return *this;
1122227825Stheraven}
1123227825Stheraven
1124232924Stheraventemplate<class _Rp, class _A0>
1125232924Stheravenfunction<_Rp(_A0)>&
1126232924Stheravenfunction<_Rp(_A0)>::operator=(nullptr_t)
1127227825Stheraven{
1128227825Stheraven    if (__f_ == (__base*)&__buf_)
1129227825Stheraven        __f_->destroy();
1130227825Stheraven    else if (__f_)
1131227825Stheraven        __f_->destroy_deallocate();
1132227825Stheraven    __f_ = 0;
1133288943Sdim    return *this;
1134227825Stheraven}
1135227825Stheraven
1136232924Stheraventemplate<class _Rp, class _A0>
1137232924Stheraventemplate <class _Fp>
1138227825Stheraventypename enable_if
1139227825Stheraven<
1140232924Stheraven    !is_integral<_Fp>::value,
1141232924Stheraven    function<_Rp(_A0)>&
1142227825Stheraven>::type
1143232924Stheravenfunction<_Rp(_A0)>::operator=(_Fp __f)
1144227825Stheraven{
1145227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1146227825Stheraven    return *this;
1147227825Stheraven}
1148227825Stheraven
1149232924Stheraventemplate<class _Rp, class _A0>
1150232924Stheravenfunction<_Rp(_A0)>::~function()
1151227825Stheraven{
1152227825Stheraven    if (__f_ == (__base*)&__buf_)
1153227825Stheraven        __f_->destroy();
1154227825Stheraven    else if (__f_)
1155227825Stheraven        __f_->destroy_deallocate();
1156227825Stheraven}
1157227825Stheraven
1158232924Stheraventemplate<class _Rp, class _A0>
1159227825Stheravenvoid
1160232924Stheravenfunction<_Rp(_A0)>::swap(function& __f)
1161227825Stheraven{
1162227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1163227825Stheraven    {
1164227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1165227825Stheraven        __base* __t = (__base*)&__tempbuf;
1166227825Stheraven        __f_->__clone(__t);
1167227825Stheraven        __f_->destroy();
1168227825Stheraven        __f_ = 0;
1169227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1170227825Stheraven        __f.__f_->destroy();
1171227825Stheraven        __f.__f_ = 0;
1172227825Stheraven        __f_ = (__base*)&__buf_;
1173227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1174227825Stheraven        __t->destroy();
1175227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1176227825Stheraven    }
1177227825Stheraven    else if (__f_ == (__base*)&__buf_)
1178227825Stheraven    {
1179227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1180227825Stheraven        __f_->destroy();
1181227825Stheraven        __f_ = __f.__f_;
1182227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1183227825Stheraven    }
1184227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1185227825Stheraven    {
1186227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1187227825Stheraven        __f.__f_->destroy();
1188227825Stheraven        __f.__f_ = __f_;
1189227825Stheraven        __f_ = (__base*)&__buf_;
1190227825Stheraven    }
1191227825Stheraven    else
1192227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1193227825Stheraven}
1194227825Stheraven
1195232924Stheraventemplate<class _Rp, class _A0>
1196232924Stheraven_Rp
1197232924Stheravenfunction<_Rp(_A0)>::operator()(_A0 __a0) const
1198227825Stheraven{
1199227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1200227825Stheraven    if (__f_ == 0)
1201227825Stheraven        throw bad_function_call();
1202227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1203227825Stheraven    return (*__f_)(__a0);
1204227825Stheraven}
1205227825Stheraven
1206227825Stheraven#ifndef _LIBCPP_NO_RTTI
1207227825Stheraven
1208232924Stheraventemplate<class _Rp, class _A0>
1209227825Stheravenconst std::type_info&
1210232924Stheravenfunction<_Rp(_A0)>::target_type() const
1211227825Stheraven{
1212227825Stheraven    if (__f_ == 0)
1213227825Stheraven        return typeid(void);
1214227825Stheraven    return __f_->target_type();
1215227825Stheraven}
1216227825Stheraven
1217232924Stheraventemplate<class _Rp, class _A0>
1218232924Stheraventemplate <typename _Tp>
1219232924Stheraven_Tp*
1220232924Stheravenfunction<_Rp(_A0)>::target()
1221227825Stheraven{
1222227825Stheraven    if (__f_ == 0)
1223232924Stheraven        return (_Tp*)0;
1224232924Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1225227825Stheraven}
1226227825Stheraven
1227232924Stheraventemplate<class _Rp, class _A0>
1228232924Stheraventemplate <typename _Tp>
1229232924Stheravenconst _Tp*
1230232924Stheravenfunction<_Rp(_A0)>::target() const
1231227825Stheraven{
1232227825Stheraven    if (__f_ == 0)
1233232924Stheraven        return (const _Tp*)0;
1234232924Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1235227825Stheraven}
1236227825Stheraven
1237227825Stheraven#endif  // _LIBCPP_NO_RTTI
1238227825Stheraven
1239232924Stheraventemplate<class _Rp, class _A0, class _A1>
1240261272Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1)>
1241232924Stheraven    : public binary_function<_A0, _A1, _Rp>
1242227825Stheraven{
1243232924Stheraven    typedef __function::__base<_Rp(_A0, _A1)> __base;
1244227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1245227825Stheraven    __base* __f_;
1246227825Stheraven
1247232924Stheraven    template <class _Fp>
1248227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1249232924Stheraven        static bool __not_null(const _Fp&) {return true;}
1250227825Stheraven    template <class _R2, class _B0, class _B1>
1251227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1252227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;}
1253232924Stheraven    template <class _R2, class _Cp, class _B1>
1254227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1255232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1)) {return __p;}
1256232924Stheraven    template <class _R2, class _Cp, class _B1>
1257227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1258232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) const) {return __p;}
1259232924Stheraven    template <class _R2, class _Cp, class _B1>
1260227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1261232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) volatile) {return __p;}
1262232924Stheraven    template <class _R2, class _Cp, class _B1>
1263227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1264232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1) const volatile) {return __p;}
1265227825Stheraven    template <class _R2, class _B0, class _B1>
1266227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1267276792Sdim        static bool __not_null(const function<_R2(_B0, _B1)>& __p) {return __p;}
1268227825Stheravenpublic:
1269232924Stheraven    typedef _Rp result_type;
1270227825Stheraven
1271227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1272227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1273227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1274227825Stheraven    function(const function&);
1275232924Stheraven    template<class _Fp>
1276232924Stheraven      function(_Fp,
1277232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1278227825Stheraven
1279227825Stheraven    template<class _Alloc>
1280227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1281227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1282227825Stheraven    template<class _Alloc>
1283227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1284227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1285227825Stheraven    template<class _Alloc>
1286227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1287232924Stheraven    template<class _Fp, class _Alloc>
1288232924Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1289232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1290227825Stheraven
1291227825Stheraven    function& operator=(const function&);
1292227825Stheraven    function& operator=(nullptr_t);
1293232924Stheraven    template<class _Fp>
1294227825Stheraven      typename enable_if
1295227825Stheraven      <
1296232924Stheraven        !is_integral<_Fp>::value,
1297227825Stheraven        function&
1298227825Stheraven      >::type
1299232924Stheraven      operator=(_Fp);
1300227825Stheraven
1301227825Stheraven    ~function();
1302227825Stheraven
1303227825Stheraven    // 20.7.16.2.2, function modifiers:
1304227825Stheraven    void swap(function&);
1305232924Stheraven    template<class _Fp, class _Alloc>
1306227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1307232924Stheraven      void assign(_Fp __f, const _Alloc& __a)
1308227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1309227825Stheraven
1310227825Stheraven    // 20.7.16.2.3, function capacity:
1311227825Stheraven    operator bool() const {return __f_;}
1312227825Stheraven
1313227825Stheravenprivate:
1314227825Stheraven    // deleted overloads close possible hole in the type system
1315227825Stheraven    template<class _R2, class _B0, class _B1>
1316227825Stheraven      bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete;
1317227825Stheraven    template<class _R2, class _B0, class _B1>
1318227825Stheraven      bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete;
1319227825Stheravenpublic:
1320227825Stheraven    // 20.7.16.2.4, function invocation:
1321232924Stheraven    _Rp operator()(_A0, _A1) const;
1322227825Stheraven
1323227825Stheraven#ifndef _LIBCPP_NO_RTTI
1324227825Stheraven    // 20.7.16.2.5, function target access:
1325227825Stheraven    const std::type_info& target_type() const;
1326232924Stheraven    template <typename _Tp> _Tp* target();
1327232924Stheraven    template <typename _Tp> const _Tp* target() const;
1328227825Stheraven#endif  // _LIBCPP_NO_RTTI
1329227825Stheraven};
1330227825Stheraven
1331232924Stheraventemplate<class _Rp, class _A0, class _A1>
1332232924Stheravenfunction<_Rp(_A0, _A1)>::function(const function& __f)
1333227825Stheraven{
1334227825Stheraven    if (__f.__f_ == 0)
1335227825Stheraven        __f_ = 0;
1336227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1337227825Stheraven    {
1338227825Stheraven        __f_ = (__base*)&__buf_;
1339227825Stheraven        __f.__f_->__clone(__f_);
1340227825Stheraven    }
1341227825Stheraven    else
1342227825Stheraven        __f_ = __f.__f_->__clone();
1343227825Stheraven}
1344227825Stheraven
1345232924Stheraventemplate<class _Rp, class _A0, class _A1>
1346227825Stheraventemplate<class _Alloc>
1347232924Stheravenfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f)
1348227825Stheraven{
1349227825Stheraven    if (__f.__f_ == 0)
1350227825Stheraven        __f_ = 0;
1351227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1352227825Stheraven    {
1353227825Stheraven        __f_ = (__base*)&__buf_;
1354227825Stheraven        __f.__f_->__clone(__f_);
1355227825Stheraven    }
1356227825Stheraven    else
1357227825Stheraven        __f_ = __f.__f_->__clone();
1358227825Stheraven}
1359227825Stheraven
1360232924Stheraventemplate<class _Rp, class _A0, class _A1>
1361232924Stheraventemplate <class _Fp>
1362232924Stheravenfunction<_Rp(_A0, _A1)>::function(_Fp __f,
1363232924Stheraven                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1364227825Stheraven    : __f_(0)
1365227825Stheraven{
1366227825Stheraven    if (__not_null(__f))
1367227825Stheraven    {
1368232924Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1)> _FF;
1369227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1370227825Stheraven        {
1371227825Stheraven            __f_ = (__base*)&__buf_;
1372227825Stheraven            ::new (__f_) _FF(__f);
1373227825Stheraven        }
1374227825Stheraven        else
1375227825Stheraven        {
1376232924Stheraven            typedef allocator<_FF> _Ap;
1377232924Stheraven            _Ap __a;
1378232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1379232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1380232924Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1381227825Stheraven            __f_ = __hold.release();
1382227825Stheraven        }
1383227825Stheraven    }
1384227825Stheraven}
1385227825Stheraven
1386232924Stheraventemplate<class _Rp, class _A0, class _A1>
1387232924Stheraventemplate <class _Fp, class _Alloc>
1388232924Stheravenfunction<_Rp(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1389232924Stheraven                                 typename enable_if<!is_integral<_Fp>::value>::type*)
1390227825Stheraven    : __f_(0)
1391227825Stheraven{
1392227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1393227825Stheraven    if (__not_null(__f))
1394227825Stheraven    {
1395232924Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1)> _FF;
1396227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1397227825Stheraven        {
1398227825Stheraven            __f_ = (__base*)&__buf_;
1399288943Sdim            ::new (__f_) _FF(__f, __a0);
1400227825Stheraven        }
1401227825Stheraven        else
1402227825Stheraven        {
1403288943Sdim            typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1404232924Stheraven            _Ap __a(__a0);
1405232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1406232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1407227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1408227825Stheraven            __f_ = __hold.release();
1409227825Stheraven        }
1410227825Stheraven    }
1411227825Stheraven}
1412227825Stheraven
1413232924Stheraventemplate<class _Rp, class _A0, class _A1>
1414232924Stheravenfunction<_Rp(_A0, _A1)>&
1415232924Stheravenfunction<_Rp(_A0, _A1)>::operator=(const function& __f)
1416227825Stheraven{
1417227825Stheraven    function(__f).swap(*this);
1418227825Stheraven    return *this;
1419227825Stheraven}
1420227825Stheraven
1421232924Stheraventemplate<class _Rp, class _A0, class _A1>
1422232924Stheravenfunction<_Rp(_A0, _A1)>&
1423232924Stheravenfunction<_Rp(_A0, _A1)>::operator=(nullptr_t)
1424227825Stheraven{
1425227825Stheraven    if (__f_ == (__base*)&__buf_)
1426227825Stheraven        __f_->destroy();
1427227825Stheraven    else if (__f_)
1428227825Stheraven        __f_->destroy_deallocate();
1429227825Stheraven    __f_ = 0;
1430288943Sdim    return *this;
1431227825Stheraven}
1432227825Stheraven
1433232924Stheraventemplate<class _Rp, class _A0, class _A1>
1434232924Stheraventemplate <class _Fp>
1435227825Stheraventypename enable_if
1436227825Stheraven<
1437232924Stheraven    !is_integral<_Fp>::value,
1438232924Stheraven    function<_Rp(_A0, _A1)>&
1439227825Stheraven>::type
1440232924Stheravenfunction<_Rp(_A0, _A1)>::operator=(_Fp __f)
1441227825Stheraven{
1442227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1443227825Stheraven    return *this;
1444227825Stheraven}
1445227825Stheraven
1446232924Stheraventemplate<class _Rp, class _A0, class _A1>
1447232924Stheravenfunction<_Rp(_A0, _A1)>::~function()
1448227825Stheraven{
1449227825Stheraven    if (__f_ == (__base*)&__buf_)
1450227825Stheraven        __f_->destroy();
1451227825Stheraven    else if (__f_)
1452227825Stheraven        __f_->destroy_deallocate();
1453227825Stheraven}
1454227825Stheraven
1455232924Stheraventemplate<class _Rp, class _A0, class _A1>
1456227825Stheravenvoid
1457232924Stheravenfunction<_Rp(_A0, _A1)>::swap(function& __f)
1458227825Stheraven{
1459227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1460227825Stheraven    {
1461227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1462227825Stheraven        __base* __t = (__base*)&__tempbuf;
1463227825Stheraven        __f_->__clone(__t);
1464227825Stheraven        __f_->destroy();
1465227825Stheraven        __f_ = 0;
1466227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1467227825Stheraven        __f.__f_->destroy();
1468227825Stheraven        __f.__f_ = 0;
1469227825Stheraven        __f_ = (__base*)&__buf_;
1470227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1471227825Stheraven        __t->destroy();
1472227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1473227825Stheraven    }
1474227825Stheraven    else if (__f_ == (__base*)&__buf_)
1475227825Stheraven    {
1476227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1477227825Stheraven        __f_->destroy();
1478227825Stheraven        __f_ = __f.__f_;
1479227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1480227825Stheraven    }
1481227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1482227825Stheraven    {
1483227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1484227825Stheraven        __f.__f_->destroy();
1485227825Stheraven        __f.__f_ = __f_;
1486227825Stheraven        __f_ = (__base*)&__buf_;
1487227825Stheraven    }
1488227825Stheraven    else
1489227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1490227825Stheraven}
1491227825Stheraven
1492232924Stheraventemplate<class _Rp, class _A0, class _A1>
1493232924Stheraven_Rp
1494232924Stheravenfunction<_Rp(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const
1495227825Stheraven{
1496227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1497227825Stheraven    if (__f_ == 0)
1498227825Stheraven        throw bad_function_call();
1499227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1500227825Stheraven    return (*__f_)(__a0, __a1);
1501227825Stheraven}
1502227825Stheraven
1503227825Stheraven#ifndef _LIBCPP_NO_RTTI
1504227825Stheraven
1505232924Stheraventemplate<class _Rp, class _A0, class _A1>
1506227825Stheravenconst std::type_info&
1507232924Stheravenfunction<_Rp(_A0, _A1)>::target_type() const
1508227825Stheraven{
1509227825Stheraven    if (__f_ == 0)
1510227825Stheraven        return typeid(void);
1511227825Stheraven    return __f_->target_type();
1512227825Stheraven}
1513227825Stheraven
1514232924Stheraventemplate<class _Rp, class _A0, class _A1>
1515232924Stheraventemplate <typename _Tp>
1516232924Stheraven_Tp*
1517232924Stheravenfunction<_Rp(_A0, _A1)>::target()
1518227825Stheraven{
1519227825Stheraven    if (__f_ == 0)
1520232924Stheraven        return (_Tp*)0;
1521232924Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1522227825Stheraven}
1523227825Stheraven
1524232924Stheraventemplate<class _Rp, class _A0, class _A1>
1525232924Stheraventemplate <typename _Tp>
1526232924Stheravenconst _Tp*
1527232924Stheravenfunction<_Rp(_A0, _A1)>::target() const
1528227825Stheraven{
1529227825Stheraven    if (__f_ == 0)
1530232924Stheraven        return (const _Tp*)0;
1531232924Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1532227825Stheraven}
1533227825Stheraven
1534227825Stheraven#endif  // _LIBCPP_NO_RTTI
1535227825Stheraven
1536232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1537261272Sdimclass _LIBCPP_TYPE_VIS_ONLY function<_Rp(_A0, _A1, _A2)>
1538227825Stheraven{
1539232924Stheraven    typedef __function::__base<_Rp(_A0, _A1, _A2)> __base;
1540227825Stheraven    aligned_storage<3*sizeof(void*)>::type __buf_;
1541227825Stheraven    __base* __f_;
1542227825Stheraven
1543232924Stheraven    template <class _Fp>
1544227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1545232924Stheraven        static bool __not_null(const _Fp&) {return true;}
1546227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1547227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1548227825Stheraven        static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;}
1549232924Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1550227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1551232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2)) {return __p;}
1552232924Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1553227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1554232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const) {return __p;}
1555232924Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1556227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1557232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) volatile) {return __p;}
1558232924Stheraven    template <class _R2, class _Cp, class _B1, class _B2>
1559227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1560232924Stheraven        static bool __not_null(_R2 (_Cp::*__p)(_B1, _B2) const volatile) {return __p;}
1561227825Stheraven    template <class _R2, class _B0, class _B1, class _B2>
1562227825Stheraven        _LIBCPP_INLINE_VISIBILITY
1563276792Sdim        static bool __not_null(const function<_R2(_B0, _B1, _B2)>& __p) {return __p;}
1564227825Stheravenpublic:
1565232924Stheraven    typedef _Rp result_type;
1566227825Stheraven
1567227825Stheraven    // 20.7.16.2.1, construct/copy/destroy:
1568227825Stheraven    _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {}
1569227825Stheraven    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
1570227825Stheraven    function(const function&);
1571232924Stheraven    template<class _Fp>
1572232924Stheraven      function(_Fp,
1573232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1574227825Stheraven
1575227825Stheraven    template<class _Alloc>
1576227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1577227825Stheraven      function(allocator_arg_t, const _Alloc&) : __f_(0) {}
1578227825Stheraven    template<class _Alloc>
1579227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1580227825Stheraven      function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {}
1581227825Stheraven    template<class _Alloc>
1582227825Stheraven      function(allocator_arg_t, const _Alloc&, const function&);
1583232924Stheraven    template<class _Fp, class _Alloc>
1584232924Stheraven      function(allocator_arg_t, const _Alloc& __a, _Fp __f,
1585232924Stheraven               typename enable_if<!is_integral<_Fp>::value>::type* = 0);
1586227825Stheraven
1587227825Stheraven    function& operator=(const function&);
1588227825Stheraven    function& operator=(nullptr_t);
1589232924Stheraven    template<class _Fp>
1590227825Stheraven      typename enable_if
1591227825Stheraven      <
1592232924Stheraven        !is_integral<_Fp>::value,
1593227825Stheraven        function&
1594227825Stheraven      >::type
1595232924Stheraven      operator=(_Fp);
1596227825Stheraven
1597227825Stheraven    ~function();
1598227825Stheraven
1599227825Stheraven    // 20.7.16.2.2, function modifiers:
1600227825Stheraven    void swap(function&);
1601232924Stheraven    template<class _Fp, class _Alloc>
1602227825Stheraven      _LIBCPP_INLINE_VISIBILITY
1603232924Stheraven      void assign(_Fp __f, const _Alloc& __a)
1604227825Stheraven        {function(allocator_arg, __a, __f).swap(*this);}
1605227825Stheraven
1606227825Stheraven    // 20.7.16.2.3, function capacity:
1607227825Stheraven    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
1608227825Stheraven
1609227825Stheravenprivate:
1610227825Stheraven    // deleted overloads close possible hole in the type system
1611227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1612227825Stheraven      bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1613227825Stheraven    template<class _R2, class _B0, class _B1, class _B2>
1614227825Stheraven      bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete;
1615227825Stheravenpublic:
1616227825Stheraven    // 20.7.16.2.4, function invocation:
1617232924Stheraven    _Rp operator()(_A0, _A1, _A2) const;
1618227825Stheraven
1619227825Stheraven#ifndef _LIBCPP_NO_RTTI
1620227825Stheraven    // 20.7.16.2.5, function target access:
1621227825Stheraven    const std::type_info& target_type() const;
1622232924Stheraven    template <typename _Tp> _Tp* target();
1623232924Stheraven    template <typename _Tp> const _Tp* target() const;
1624227825Stheraven#endif  // _LIBCPP_NO_RTTI
1625227825Stheraven};
1626227825Stheraven
1627232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1628232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(const function& __f)
1629227825Stheraven{
1630227825Stheraven    if (__f.__f_ == 0)
1631227825Stheraven        __f_ = 0;
1632227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1633227825Stheraven    {
1634227825Stheraven        __f_ = (__base*)&__buf_;
1635227825Stheraven        __f.__f_->__clone(__f_);
1636227825Stheraven    }
1637227825Stheraven    else
1638227825Stheraven        __f_ = __f.__f_->__clone();
1639227825Stheraven}
1640227825Stheraven
1641232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1642227825Stheraventemplate<class _Alloc>
1643232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&,
1644227825Stheraven                                      const function& __f)
1645227825Stheraven{
1646227825Stheraven    if (__f.__f_ == 0)
1647227825Stheraven        __f_ = 0;
1648227825Stheraven    else if (__f.__f_ == (const __base*)&__f.__buf_)
1649227825Stheraven    {
1650227825Stheraven        __f_ = (__base*)&__buf_;
1651227825Stheraven        __f.__f_->__clone(__f_);
1652227825Stheraven    }
1653227825Stheraven    else
1654227825Stheraven        __f_ = __f.__f_->__clone();
1655227825Stheraven}
1656227825Stheraven
1657232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1658232924Stheraventemplate <class _Fp>
1659232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(_Fp __f,
1660232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1661227825Stheraven    : __f_(0)
1662227825Stheraven{
1663227825Stheraven    if (__not_null(__f))
1664227825Stheraven    {
1665232924Stheraven        typedef __function::__func<_Fp, allocator<_Fp>, _Rp(_A0, _A1, _A2)> _FF;
1666227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1667227825Stheraven        {
1668227825Stheraven            __f_ = (__base*)&__buf_;
1669227825Stheraven            ::new (__f_) _FF(__f);
1670227825Stheraven        }
1671227825Stheraven        else
1672227825Stheraven        {
1673232924Stheraven            typedef allocator<_FF> _Ap;
1674232924Stheraven            _Ap __a;
1675232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1676232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1677232924Stheraven            ::new (__hold.get()) _FF(__f, allocator<_Fp>(__a));
1678227825Stheraven            __f_ = __hold.release();
1679227825Stheraven        }
1680227825Stheraven    }
1681227825Stheraven}
1682227825Stheraven
1683232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1684232924Stheraventemplate <class _Fp, class _Alloc>
1685232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _Fp __f,
1686232924Stheraven                                     typename enable_if<!is_integral<_Fp>::value>::type*)
1687227825Stheraven    : __f_(0)
1688227825Stheraven{
1689227825Stheraven    typedef allocator_traits<_Alloc> __alloc_traits;
1690227825Stheraven    if (__not_null(__f))
1691227825Stheraven    {
1692232924Stheraven        typedef __function::__func<_Fp, _Alloc, _Rp(_A0, _A1, _A2)> _FF;
1693227825Stheraven        if (sizeof(_FF) <= sizeof(__buf_))
1694227825Stheraven        {
1695227825Stheraven            __f_ = (__base*)&__buf_;
1696288943Sdim            ::new (__f_) _FF(__f, __a0);
1697227825Stheraven        }
1698227825Stheraven        else
1699227825Stheraven        {
1700288943Sdim            typedef typename __rebind_alloc_helper<__alloc_traits, _FF>::type _Ap;
1701232924Stheraven            _Ap __a(__a0);
1702232924Stheraven            typedef __allocator_destructor<_Ap> _Dp;
1703232924Stheraven            unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1704227825Stheraven            ::new (__hold.get()) _FF(__f, _Alloc(__a));
1705227825Stheraven            __f_ = __hold.release();
1706227825Stheraven        }
1707227825Stheraven    }
1708227825Stheraven}
1709227825Stheraven
1710232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1711232924Stheravenfunction<_Rp(_A0, _A1, _A2)>&
1712232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(const function& __f)
1713227825Stheraven{
1714227825Stheraven    function(__f).swap(*this);
1715227825Stheraven    return *this;
1716227825Stheraven}
1717227825Stheraven
1718232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1719232924Stheravenfunction<_Rp(_A0, _A1, _A2)>&
1720232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(nullptr_t)
1721227825Stheraven{
1722227825Stheraven    if (__f_ == (__base*)&__buf_)
1723227825Stheraven        __f_->destroy();
1724227825Stheraven    else if (__f_)
1725227825Stheraven        __f_->destroy_deallocate();
1726227825Stheraven    __f_ = 0;
1727288943Sdim    return *this;
1728227825Stheraven}
1729227825Stheraven
1730232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1731232924Stheraventemplate <class _Fp>
1732227825Stheraventypename enable_if
1733227825Stheraven<
1734232924Stheraven    !is_integral<_Fp>::value,
1735232924Stheraven    function<_Rp(_A0, _A1, _A2)>&
1736227825Stheraven>::type
1737232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator=(_Fp __f)
1738227825Stheraven{
1739227825Stheraven    function(_VSTD::move(__f)).swap(*this);
1740227825Stheraven    return *this;
1741227825Stheraven}
1742227825Stheraven
1743232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1744232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::~function()
1745227825Stheraven{
1746227825Stheraven    if (__f_ == (__base*)&__buf_)
1747227825Stheraven        __f_->destroy();
1748227825Stheraven    else if (__f_)
1749227825Stheraven        __f_->destroy_deallocate();
1750227825Stheraven}
1751227825Stheraven
1752232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1753227825Stheravenvoid
1754232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::swap(function& __f)
1755227825Stheraven{
1756227825Stheraven    if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1757227825Stheraven    {
1758227825Stheraven        typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1759227825Stheraven        __base* __t = (__base*)&__tempbuf;
1760227825Stheraven        __f_->__clone(__t);
1761227825Stheraven        __f_->destroy();
1762227825Stheraven        __f_ = 0;
1763227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1764227825Stheraven        __f.__f_->destroy();
1765227825Stheraven        __f.__f_ = 0;
1766227825Stheraven        __f_ = (__base*)&__buf_;
1767227825Stheraven        __t->__clone((__base*)&__f.__buf_);
1768227825Stheraven        __t->destroy();
1769227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1770227825Stheraven    }
1771227825Stheraven    else if (__f_ == (__base*)&__buf_)
1772227825Stheraven    {
1773227825Stheraven        __f_->__clone((__base*)&__f.__buf_);
1774227825Stheraven        __f_->destroy();
1775227825Stheraven        __f_ = __f.__f_;
1776227825Stheraven        __f.__f_ = (__base*)&__f.__buf_;
1777227825Stheraven    }
1778227825Stheraven    else if (__f.__f_ == (__base*)&__f.__buf_)
1779227825Stheraven    {
1780227825Stheraven        __f.__f_->__clone((__base*)&__buf_);
1781227825Stheraven        __f.__f_->destroy();
1782227825Stheraven        __f.__f_ = __f_;
1783227825Stheraven        __f_ = (__base*)&__buf_;
1784227825Stheraven    }
1785227825Stheraven    else
1786227825Stheraven        _VSTD::swap(__f_, __f.__f_);
1787227825Stheraven}
1788227825Stheraven
1789232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1790232924Stheraven_Rp
1791232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const
1792227825Stheraven{
1793227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
1794227825Stheraven    if (__f_ == 0)
1795227825Stheraven        throw bad_function_call();
1796227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
1797227825Stheraven    return (*__f_)(__a0, __a1, __a2);
1798227825Stheraven}
1799227825Stheraven
1800227825Stheraven#ifndef _LIBCPP_NO_RTTI
1801227825Stheraven
1802232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1803227825Stheravenconst std::type_info&
1804232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::target_type() const
1805227825Stheraven{
1806227825Stheraven    if (__f_ == 0)
1807227825Stheraven        return typeid(void);
1808227825Stheraven    return __f_->target_type();
1809227825Stheraven}
1810227825Stheraven
1811232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1812232924Stheraventemplate <typename _Tp>
1813232924Stheraven_Tp*
1814232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::target()
1815227825Stheraven{
1816227825Stheraven    if (__f_ == 0)
1817232924Stheraven        return (_Tp*)0;
1818232924Stheraven    return (_Tp*)__f_->target(typeid(_Tp));
1819227825Stheraven}
1820227825Stheraven
1821232924Stheraventemplate<class _Rp, class _A0, class _A1, class _A2>
1822232924Stheraventemplate <typename _Tp>
1823232924Stheravenconst _Tp*
1824232924Stheravenfunction<_Rp(_A0, _A1, _A2)>::target() const
1825227825Stheraven{
1826227825Stheraven    if (__f_ == 0)
1827232924Stheraven        return (const _Tp*)0;
1828232924Stheraven    return (const _Tp*)__f_->target(typeid(_Tp));
1829227825Stheraven}
1830227825Stheraven
1831227825Stheraven#endif  // _LIBCPP_NO_RTTI
1832227825Stheraven
1833232924Stheraventemplate <class _Fp>
1834227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1835227825Stheravenbool
1836232924Stheravenoperator==(const function<_Fp>& __f, nullptr_t) {return !__f;}
1837227825Stheraven
1838232924Stheraventemplate <class _Fp>
1839227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1840227825Stheravenbool
1841232924Stheravenoperator==(nullptr_t, const function<_Fp>& __f) {return !__f;}
1842227825Stheraven
1843232924Stheraventemplate <class _Fp>
1844227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1845227825Stheravenbool
1846232924Stheravenoperator!=(const function<_Fp>& __f, nullptr_t) {return (bool)__f;}
1847227825Stheraven
1848232924Stheraventemplate <class _Fp>
1849227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1850227825Stheravenbool
1851232924Stheravenoperator!=(nullptr_t, const function<_Fp>& __f) {return (bool)__f;}
1852227825Stheraven
1853232924Stheraventemplate <class _Fp>
1854227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1855227825Stheravenvoid
1856232924Stheravenswap(function<_Fp>& __x, function<_Fp>& __y)
1857227825Stheraven{return __x.swap(__y);}
1858227825Stheraven
1859227825Stheraventemplate<class _Tp> struct __is_bind_expression : public false_type {};
1860261272Sdimtemplate<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_bind_expression
1861227825Stheraven    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
1862227825Stheraven
1863227825Stheraventemplate<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
1864261272Sdimtemplate<class _Tp> struct _LIBCPP_TYPE_VIS_ONLY is_placeholder
1865227825Stheraven    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
1866227825Stheraven
1867227825Stheravennamespace placeholders
1868227825Stheraven{
1869227825Stheraven
1870232924Stheraventemplate <int _Np> struct __ph {};
1871227825Stheraven
1872227825Stheravenextern __ph<1>   _1;
1873227825Stheravenextern __ph<2>   _2;
1874227825Stheravenextern __ph<3>   _3;
1875227825Stheravenextern __ph<4>   _4;
1876227825Stheravenextern __ph<5>   _5;
1877227825Stheravenextern __ph<6>   _6;
1878227825Stheravenextern __ph<7>   _7;
1879227825Stheravenextern __ph<8>   _8;
1880227825Stheravenextern __ph<9>   _9;
1881227825Stheravenextern __ph<10> _10;
1882227825Stheraven
1883227825Stheraven}  // placeholders
1884227825Stheraven
1885232924Stheraventemplate<int _Np>
1886232924Stheravenstruct __is_placeholder<placeholders::__ph<_Np> >
1887232924Stheraven    : public integral_constant<int, _Np> {};
1888227825Stheraven
1889227825Stheraventemplate <class _Tp, class _Uj>
1890227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1891227825Stheraven_Tp&
1892227825Stheraven__mu(reference_wrapper<_Tp> __t, _Uj&)
1893227825Stheraven{
1894227825Stheraven    return __t.get();
1895227825Stheraven}
1896227825Stheraven/*
1897227825Stheraventemplate <bool _IsBindExpr, class _Ti, class ..._Uj>
1898227825Stheravenstruct __mu_return1 {};
1899227825Stheraven
1900227825Stheraventemplate <class _Ti, class ..._Uj>
1901227825Stheravenstruct __mu_return1<true, _Ti, _Uj...>
1902227825Stheraven{
1903227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1904227825Stheraven};
1905227825Stheraven
1906227825Stheraventemplate <class _Ti, class ..._Uj, size_t ..._Indx>
1907227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1908227825Stheraventypename __mu_return1<true, _Ti, _Uj...>::type
1909227825Stheraven__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>)
1910227825Stheraven{
1911276792Sdim    __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj))...);
1912227825Stheraven}
1913227825Stheraven
1914227825Stheraventemplate <class _Ti, class ..._Uj>
1915227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1916227825Stheraventypename enable_if
1917227825Stheraven<
1918227825Stheraven    is_bind_expression<_Ti>::value,
1919227825Stheraven    typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type
1920227825Stheraven>::type
1921227825Stheraven__mu(_Ti& __ti, tuple<_Uj...>& __uj)
1922227825Stheraven{
1923227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
1924227825Stheraven    return  __mu_expand(__ti, __uj, __indices());
1925227825Stheraven}
1926227825Stheraven
1927227825Stheraventemplate <bool IsPh, class _Ti, class _Uj>
1928227825Stheravenstruct __mu_return2 {};
1929227825Stheraven
1930227825Stheraventemplate <class _Ti, class _Uj>
1931227825Stheravenstruct __mu_return2<true, _Ti, _Uj>
1932227825Stheraven{
1933227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
1934227825Stheraven};
1935227825Stheraven
1936227825Stheraventemplate <class _Ti, class _Uj>
1937227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1938227825Stheraventypename enable_if
1939227825Stheraven<
1940227825Stheraven    0 < is_placeholder<_Ti>::value,
1941227825Stheraven    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
1942227825Stheraven>::type
1943227825Stheraven__mu(_Ti&, _Uj& __uj)
1944227825Stheraven{
1945227825Stheraven    const size_t _Indx = is_placeholder<_Ti>::value - 1;
1946227825Stheraven    // compiler bug workaround
1947276792Sdim    typename tuple_element<_Indx, _Uj>::type __t = _VSTD::get<_Indx>(__uj);
1948227825Stheraven    return __t;
1949276792Sdim//    return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
1950227825Stheraven}
1951227825Stheraven
1952227825Stheraventemplate <class _Ti, class _Uj>
1953227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1954227825Stheraventypename enable_if
1955227825Stheraven<
1956227825Stheraven    !is_bind_expression<_Ti>::value &&
1957227825Stheraven    is_placeholder<_Ti>::value == 0 &&
1958227825Stheraven    !__is_reference_wrapper<_Ti>::value,
1959227825Stheraven    _Ti&
1960227825Stheraven>::type
1961227825Stheraven__mu(_Ti& __ti, _Uj& __uj)
1962227825Stheraven{
1963227825Stheraven    return __ti;
1964227825Stheraven}
1965227825Stheraven
1966227825Stheraventemplate <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj>
1967227825Stheravenstruct ____mu_return;
1968227825Stheraven
1969227825Stheraventemplate <class _Ti, class ..._Uj>
1970227825Stheravenstruct ____mu_return<_Ti, true, false, tuple<_Uj...> >
1971227825Stheraven{
1972227825Stheraven    typedef typename result_of<_Ti(_Uj...)>::type type;
1973227825Stheraven};
1974227825Stheraven
1975227825Stheraventemplate <class _Ti, class _TupleUj>
1976227825Stheravenstruct ____mu_return<_Ti, false, true, _TupleUj>
1977227825Stheraven{
1978227825Stheraven    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
1979227825Stheraven                                   _TupleUj>::type&& type;
1980227825Stheraven};
1981227825Stheraven
1982227825Stheraventemplate <class _Ti, class _TupleUj>
1983227825Stheravenstruct ____mu_return<_Ti, false, false, _TupleUj>
1984227825Stheraven{
1985227825Stheraven    typedef _Ti& type;
1986227825Stheraven};
1987227825Stheraven
1988227825Stheraventemplate <class _Ti, class _TupleUj>
1989227825Stheravenstruct __mu_return
1990227825Stheraven    : public ____mu_return<_Ti,
1991227825Stheraven                           is_bind_expression<_Ti>::value,
1992227825Stheraven                           0 < is_placeholder<_Ti>::value,
1993227825Stheraven                           _TupleUj>
1994227825Stheraven{
1995227825Stheraven};
1996227825Stheraven
1997227825Stheraventemplate <class _Ti, class _TupleUj>
1998227825Stheravenstruct __mu_return<reference_wrapper<_Ti>, _TupleUj>
1999227825Stheraven{
2000227825Stheraven    typedef _Ti& type;
2001227825Stheraven};
2002227825Stheraven
2003232924Stheraventemplate <class _Fp, class _BoundArgs, class _TupleUj>
2004227825Stheravenstruct __bind_return;
2005227825Stheraven
2006232924Stheraventemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
2007232924Stheravenstruct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
2008227825Stheraven{
2009227825Stheraven    typedef typename __ref_return
2010227825Stheraven    <
2011232924Stheraven        _Fp&,
2012227825Stheraven        typename __mu_return
2013227825Stheraven        <
2014227825Stheraven            _BoundArgs,
2015227825Stheraven            _TupleUj
2016227825Stheraven        >::type...
2017227825Stheraven    >::type type;
2018227825Stheraven};
2019227825Stheraven
2020232924Stheraventemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
2021232924Stheravenstruct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
2022227825Stheraven{
2023227825Stheraven    typedef typename __ref_return
2024227825Stheraven    <
2025232924Stheraven        _Fp&,
2026227825Stheraven        typename __mu_return
2027227825Stheraven        <
2028227825Stheraven            const _BoundArgs,
2029227825Stheraven            _TupleUj
2030227825Stheraven        >::type...
2031227825Stheraven    >::type type;
2032227825Stheraven};
2033227825Stheraven
2034232924Stheraventemplate <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
2035227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2036232924Stheraventypename __bind_return<_Fp, _BoundArgs, _Args>::type
2037232924Stheraven__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
2038227825Stheraven                _Args&& __args)
2039227825Stheraven{
2040276792Sdim    return __invoke(__f, __mu(_VSTD::get<_Indx>(__bound_args), __args)...);
2041227825Stheraven}
2042227825Stheraven
2043232924Stheraventemplate<class _Fp, class ..._BoundArgs>
2044227825Stheravenclass __bind
2045227825Stheraven{
2046232924Stheraven    _Fp __f_;
2047227825Stheraven    tuple<_BoundArgs...> __bound_args_;
2048227825Stheraven
2049227825Stheraven    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
2050227825Stheravenpublic:
2051232924Stheraven    template <class _Gp, class ..._BA>
2052232924Stheraven      explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
2053232924Stheraven        : __f_(_VSTD::forward<_Gp>(__f)),
2054227825Stheraven          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
2055227825Stheraven
2056227825Stheraven    template <class ..._Args>
2057232924Stheraven        typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2058227825Stheraven        operator()(_Args&& ...__args)
2059227825Stheraven        {
2060227825Stheraven            // compiler bug workaround
2061227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2062227825Stheraven                                  tuple<_Args&&...>(__args...));
2063227825Stheraven        }
2064227825Stheraven
2065227825Stheraven    template <class ..._Args>
2066232924Stheraven        typename __bind_return<_Fp, tuple<_BoundArgs...>, tuple<_Args&&...> >::type
2067227825Stheraven        operator()(_Args&& ...__args) const
2068227825Stheraven        {
2069227825Stheraven            return __apply_functor(__f_, __bound_args_, __indices(),
2070227825Stheraven                                   tuple<_Args&&...>(__args...));
2071227825Stheraven        }
2072227825Stheraven};
2073227825Stheraven
2074232924Stheraventemplate<class _Fp, class ..._BoundArgs>
2075232924Stheravenstruct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
2076227825Stheraven
2077232924Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2078227825Stheravenclass __bind_r
2079232924Stheraven    : public __bind<_Fp, _BoundArgs...>
2080227825Stheraven{
2081232924Stheraven    typedef __bind<_Fp, _BoundArgs...> base;
2082227825Stheravenpublic:
2083232924Stheraven    typedef _Rp result_type;
2084227825Stheraven
2085232924Stheraven    template <class _Gp, class ..._BA>
2086232924Stheraven      explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
2087232924Stheraven        : base(_VSTD::forward<_Gp>(__f),
2088227825Stheraven               _VSTD::forward<_BA>(__bound_args)...) {}
2089227825Stheraven
2090227825Stheraven    template <class ..._Args>
2091227825Stheraven        result_type
2092227825Stheraven        operator()(_Args&& ...__args)
2093227825Stheraven        {
2094288943Sdim            typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2095288943Sdim            return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
2096227825Stheraven        }
2097227825Stheraven
2098227825Stheraven    template <class ..._Args>
2099227825Stheraven        result_type
2100227825Stheraven        operator()(_Args&& ...__args) const
2101227825Stheraven        {
2102288943Sdim            typedef __invoke_void_return_wrapper<_Rp> _Invoker;
2103288943Sdim            return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
2104227825Stheraven        }
2105227825Stheraven};
2106227825Stheraven
2107232924Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2108232924Stheravenstruct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
2109227825Stheraven
2110232924Stheraventemplate<class _Fp, class ..._BoundArgs>
2111227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2112232924Stheraven__bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2113232924Stheravenbind(_Fp&& __f, _BoundArgs&&... __bound_args)
2114227825Stheraven{
2115232924Stheraven    typedef __bind<typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2116232924Stheraven    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2117227825Stheraven}
2118227825Stheraven
2119232924Stheraventemplate<class _Rp, class _Fp, class ..._BoundArgs>
2120227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
2121232924Stheraven__bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...>
2122232924Stheravenbind(_Fp&& __f, _BoundArgs&&... __bound_args)
2123227825Stheraven{
2124232924Stheraven    typedef __bind_r<_Rp, typename decay<_Fp>::type, typename decay<_BoundArgs>::type...> type;
2125232924Stheraven    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
2126227825Stheraven}
2127227825Stheraven*/
2128227825Stheraven
2129227825Stheraven#endif  // _LIBCPP_FUNCTIONAL_03
2130