• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___FUNCTIONAL_FUNCTION_H
11 #define _LIBCPP___FUNCTIONAL_FUNCTION_H
12 
13 #include <__assert>
14 #include <__config>
15 #include <__exception/exception.h>
16 #include <__functional/binary_function.h>
17 #include <__functional/invoke.h>
18 #include <__functional/unary_function.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__memory/addressof.h>
21 #include <__memory/allocator.h>
22 #include <__memory/allocator_destructor.h>
23 #include <__memory/allocator_traits.h>
24 #include <__memory/builtin_new_allocator.h>
25 #include <__memory/compressed_pair.h>
26 #include <__memory/unique_ptr.h>
27 #include <__type_traits/aligned_storage.h>
28 #include <__type_traits/decay.h>
29 #include <__type_traits/is_core_convertible.h>
30 #include <__type_traits/is_scalar.h>
31 #include <__type_traits/is_trivially_copy_constructible.h>
32 #include <__type_traits/is_trivially_destructible.h>
33 #include <__type_traits/is_void.h>
34 #include <__type_traits/strip_signature.h>
35 #include <__utility/forward.h>
36 #include <__utility/move.h>
37 #include <__utility/piecewise_construct.h>
38 #include <__utility/swap.h>
39 #include <__verbose_abort>
40 #include <new>
41 #include <tuple>
42 #include <typeinfo>
43 
44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45 #  pragma GCC system_header
46 #endif
47 
48 #ifndef _LIBCPP_CXX03_LANG
49 
50 _LIBCPP_BEGIN_NAMESPACE_STD
51 
52 // bad_function_call
53 
54 _LIBCPP_DIAGNOSTIC_PUSH
55 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wweak-vtables")
56 class _LIBCPP_EXPORTED_FROM_ABI bad_function_call
57     : public exception
58 {
59 public:
60     _LIBCPP_HIDE_FROM_ABI bad_function_call() _NOEXCEPT = default;
61     _LIBCPP_HIDE_FROM_ABI bad_function_call(const bad_function_call&) _NOEXCEPT = default;
62     _LIBCPP_HIDE_FROM_ABI bad_function_call& operator=(const bad_function_call&) _NOEXCEPT = default;
63 // Note that when a key function is not used, every translation unit that uses
64 // bad_function_call will end up containing a weak definition of the vtable and
65 // typeinfo.
66 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
67     ~bad_function_call() _NOEXCEPT override;
68 #else
~bad_function_call()69     _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~bad_function_call() _NOEXCEPT override {}
70 #endif
71 
72 #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE
73     const char* what() const _NOEXCEPT override;
74 #endif
75 };
76 _LIBCPP_DIAGNOSTIC_POP
77 
78 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
__throw_bad_function_call()79 void __throw_bad_function_call()
80 {
81 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
82     throw bad_function_call();
83 #else
84     _LIBCPP_VERBOSE_ABORT("bad_function_call was thrown in -fno-exceptions mode");
85 #endif
86 }
87 
88 template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
89 
90 namespace __function
91 {
92 
93 template<class _Rp>
94 struct __maybe_derive_from_unary_function
95 {
96 };
97 
98 template<class _Rp, class _A1>
99 struct __maybe_derive_from_unary_function<_Rp(_A1)>
100     : public __unary_function<_A1, _Rp>
101 {
102 };
103 
104 template<class _Rp>
105 struct __maybe_derive_from_binary_function
106 {
107 };
108 
109 template<class _Rp, class _A1, class _A2>
110 struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
111     : public __binary_function<_A1, _A2, _Rp>
112 {
113 };
114 
115 template <class _Fp>
116 _LIBCPP_INLINE_VISIBILITY
117 bool __not_null(_Fp const&) { return true; }
118 
119 template <class _Fp>
120 _LIBCPP_INLINE_VISIBILITY
121 bool __not_null(_Fp* __ptr) { return __ptr; }
122 
123 template <class _Ret, class _Class>
124 _LIBCPP_INLINE_VISIBILITY
125 bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
126 
127 template <class _Fp>
128 _LIBCPP_INLINE_VISIBILITY
129 bool __not_null(function<_Fp> const& __f) { return !!__f; }
130 
131 #ifdef _LIBCPP_HAS_EXTENSION_BLOCKS
132 template <class _Rp, class ..._Args>
133 _LIBCPP_INLINE_VISIBILITY
134 bool __not_null(_Rp (^__p)(_Args...)) { return __p; }
135 #endif
136 
137 } // namespace __function
138 
139 namespace __function {
140 
141 // __alloc_func holds a functor and an allocator.
142 
143 template <class _Fp, class _Ap, class _FB> class __alloc_func;
144 template <class _Fp, class _FB>
145 class __default_alloc_func;
146 
147 template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
148 class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
149 {
150     __compressed_pair<_Fp, _Ap> __f_;
151 
152   public:
153     typedef _LIBCPP_NODEBUG _Fp _Target;
154     typedef _LIBCPP_NODEBUG _Ap _Alloc;
155 
156     _LIBCPP_INLINE_VISIBILITY
157     const _Target& __target() const { return __f_.first(); }
158 
159     // WIN32 APIs may define __allocator, so use __get_allocator instead.
160     _LIBCPP_INLINE_VISIBILITY
161     const _Alloc& __get_allocator() const { return __f_.second(); }
162 
163     _LIBCPP_INLINE_VISIBILITY
164     explicit __alloc_func(_Target&& __f)
165         : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
166                _VSTD::forward_as_tuple())
167     {
168     }
169 
170     _LIBCPP_INLINE_VISIBILITY
171     explicit __alloc_func(const _Target& __f, const _Alloc& __a)
172         : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
173                _VSTD::forward_as_tuple(__a))
174     {
175     }
176 
177     _LIBCPP_INLINE_VISIBILITY
178     explicit __alloc_func(const _Target& __f, _Alloc&& __a)
179         : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
180                _VSTD::forward_as_tuple(_VSTD::move(__a)))
181     {
182     }
183 
184     _LIBCPP_INLINE_VISIBILITY
185     explicit __alloc_func(_Target&& __f, _Alloc&& __a)
186         : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
187                _VSTD::forward_as_tuple(_VSTD::move(__a)))
188     {
189     }
190 
191     _LIBCPP_INLINE_VISIBILITY
192     _Rp operator()(_ArgTypes&&... __arg)
193     {
194         typedef __invoke_void_return_wrapper<_Rp> _Invoker;
195         return _Invoker::__call(__f_.first(),
196                                 _VSTD::forward<_ArgTypes>(__arg)...);
197     }
198 
199     _LIBCPP_INLINE_VISIBILITY
200     __alloc_func* __clone() const
201     {
202         typedef allocator_traits<_Alloc> __alloc_traits;
203         typedef __rebind_alloc<__alloc_traits, __alloc_func> _AA;
204         _AA __a(__f_.second());
205         typedef __allocator_destructor<_AA> _Dp;
206         unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
207         ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
208         return __hold.release();
209     }
210 
211     _LIBCPP_INLINE_VISIBILITY
212     void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
213 
214     _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__alloc_func* __f) {
215       typedef allocator_traits<_Alloc> __alloc_traits;
216       typedef __rebind_alloc<__alloc_traits, __alloc_func> _FunAlloc;
217       _FunAlloc __a(__f->__get_allocator());
218       __f->destroy();
219       __a.deallocate(__f, 1);
220     }
221 };
222 
223 template <class _Fp, class _Rp, class... _ArgTypes>
224 class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
225   _Fp __f_;
226 
227 public:
228   typedef _LIBCPP_NODEBUG _Fp _Target;
229 
230   _LIBCPP_INLINE_VISIBILITY
231   const _Target& __target() const { return __f_; }
232 
233   _LIBCPP_INLINE_VISIBILITY
234   explicit __default_alloc_func(_Target&& __f) : __f_(_VSTD::move(__f)) {}
235 
236   _LIBCPP_INLINE_VISIBILITY
237   explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
238 
239   _LIBCPP_INLINE_VISIBILITY
240   _Rp operator()(_ArgTypes&&... __arg) {
241     typedef __invoke_void_return_wrapper<_Rp> _Invoker;
242     return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
243   }
244 
245   _LIBCPP_INLINE_VISIBILITY
246   __default_alloc_func* __clone() const {
247       __builtin_new_allocator::__holder_t __hold =
248         __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
249     __default_alloc_func* __res =
250         ::new ((void*)__hold.get()) __default_alloc_func(__f_);
251     (void)__hold.release();
252     return __res;
253   }
254 
255   _LIBCPP_INLINE_VISIBILITY
256   void destroy() _NOEXCEPT { __f_.~_Target(); }
257 
258   _LIBCPP_HIDE_FROM_ABI static void __destroy_and_delete(__default_alloc_func* __f) {
259     __f->destroy();
260       __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
261   }
262 };
263 
264 // __base provides an abstract interface for copyable functors.
265 
266 template<class _Fp> class _LIBCPP_TEMPLATE_VIS __base;
267 
268 template<class _Rp, class ..._ArgTypes>
269 class __base<_Rp(_ArgTypes...)>
270 {
271     __base(const __base&);
272     __base& operator=(const __base&);
273 public:
274     _LIBCPP_INLINE_VISIBILITY __base() {}
275     _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__base() {}
276     virtual __base* __clone() const = 0;
277     virtual void __clone(__base*) const = 0;
278     virtual void destroy() _NOEXCEPT = 0;
279     virtual void destroy_deallocate() _NOEXCEPT = 0;
280     virtual _Rp operator()(_ArgTypes&& ...) = 0;
281 #ifndef _LIBCPP_HAS_NO_RTTI
282     virtual const void* target(const type_info&) const _NOEXCEPT = 0;
283     virtual const std::type_info& target_type() const _NOEXCEPT = 0;
284 #endif // _LIBCPP_HAS_NO_RTTI
285 };
286 
287 // __func implements __base for a given functor type.
288 
289 template<class _FD, class _Alloc, class _FB> class __func;
290 
291 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
292 class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
293     : public  __base<_Rp(_ArgTypes...)>
294 {
295     __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
296 public:
297     _LIBCPP_INLINE_VISIBILITY
298     explicit __func(_Fp&& __f)
299         : __f_(_VSTD::move(__f)) {}
300 
301     _LIBCPP_INLINE_VISIBILITY
302     explicit __func(const _Fp& __f, const _Alloc& __a)
303         : __f_(__f, __a) {}
304 
305     _LIBCPP_INLINE_VISIBILITY
306     explicit __func(const _Fp& __f, _Alloc&& __a)
307         : __f_(__f, _VSTD::move(__a)) {}
308 
309     _LIBCPP_INLINE_VISIBILITY
310     explicit __func(_Fp&& __f, _Alloc&& __a)
311         : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
312 
313     _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual __base<_Rp(_ArgTypes...)>* __clone() const;
314     _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
315     _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy() _NOEXCEPT;
316     _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate() _NOEXCEPT;
317     _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __arg);
318 #ifndef _LIBCPP_HAS_NO_RTTI
319     _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const void* target(const type_info&) const _NOEXCEPT;
320     _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual const std::type_info& target_type() const _NOEXCEPT;
321 #endif // _LIBCPP_HAS_NO_RTTI
322 };
323 
324 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
325 __base<_Rp(_ArgTypes...)>*
326 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
327 {
328     typedef allocator_traits<_Alloc> __alloc_traits;
329     typedef __rebind_alloc<__alloc_traits, __func> _Ap;
330     _Ap __a(__f_.__get_allocator());
331     typedef __allocator_destructor<_Ap> _Dp;
332     unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
333     ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
334     return __hold.release();
335 }
336 
337 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
338 void
339 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
340 {
341     ::new ((void*)__p) __func(__f_.__target(), __f_.__get_allocator());
342 }
343 
344 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
345 void
346 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
347 {
348     __f_.destroy();
349 }
350 
351 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
352 void
353 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
354 {
355     typedef allocator_traits<_Alloc> __alloc_traits;
356     typedef __rebind_alloc<__alloc_traits, __func> _Ap;
357     _Ap __a(__f_.__get_allocator());
358     __f_.destroy();
359     __a.deallocate(this, 1);
360 }
361 
362 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
363 _Rp
364 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
365 {
366     return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
367 }
368 
369 #ifndef _LIBCPP_HAS_NO_RTTI
370 
371 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
372 const void*
373 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
374 {
375     if (__ti == typeid(_Fp))
376         return _VSTD::addressof(__f_.__target());
377     return nullptr;
378 }
379 
380 template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
381 const std::type_info&
382 __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
383 {
384     return typeid(_Fp);
385 }
386 
387 #endif // _LIBCPP_HAS_NO_RTTI
388 
389 // __value_func creates a value-type from a __func.
390 
391 template <class _Fp> class __value_func;
392 
393 template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
394 {
395     _LIBCPP_SUPPRESS_DEPRECATED_PUSH
396     typename aligned_storage<3 * sizeof(void*)>::type __buf_;
397     _LIBCPP_SUPPRESS_DEPRECATED_POP
398 
399     typedef __base<_Rp(_ArgTypes...)> __func;
400     __func* __f_;
401 
402     _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI static __func* __as_base(void* __p)
403     {
404         return reinterpret_cast<__func*>(__p);
405     }
406 
407   public:
408     _LIBCPP_INLINE_VISIBILITY
409     __value_func() _NOEXCEPT : __f_(nullptr) {}
410 
411     template <class _Fp, class _Alloc>
412     _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
413         : __f_(nullptr)
414     {
415         typedef allocator_traits<_Alloc> __alloc_traits;
416         typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
417         typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
418 
419         if (__function::__not_null(__f))
420         {
421             _FunAlloc __af(__a);
422             if (sizeof(_Fun) <= sizeof(__buf_) &&
423                 is_nothrow_copy_constructible<_Fp>::value &&
424                 is_nothrow_copy_constructible<_FunAlloc>::value)
425             {
426                 __f_ =
427                     ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
428             }
429             else
430             {
431                 typedef __allocator_destructor<_FunAlloc> _Dp;
432                 unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
433                 ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
434                 __f_ = __hold.release();
435             }
436         }
437     }
438 
439     template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __value_func>::value, int> = 0>
440     _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
441         : __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
442 
443     _LIBCPP_INLINE_VISIBILITY
444     __value_func(const __value_func& __f)
445     {
446         if (__f.__f_ == nullptr)
447             __f_ = nullptr;
448         else if ((void*)__f.__f_ == &__f.__buf_)
449         {
450             __f_ = __as_base(&__buf_);
451             __f.__f_->__clone(__f_);
452         }
453         else
454             __f_ = __f.__f_->__clone();
455     }
456 
457     _LIBCPP_INLINE_VISIBILITY
458     __value_func(__value_func&& __f) _NOEXCEPT
459     {
460         if (__f.__f_ == nullptr)
461             __f_ = nullptr;
462         else if ((void*)__f.__f_ == &__f.__buf_)
463         {
464             __f_ = __as_base(&__buf_);
465             __f.__f_->__clone(__f_);
466         }
467         else
468         {
469             __f_ = __f.__f_;
470             __f.__f_ = nullptr;
471         }
472     }
473 
474     _LIBCPP_INLINE_VISIBILITY
475     ~__value_func()
476     {
477         if ((void*)__f_ == &__buf_)
478             __f_->destroy();
479         else if (__f_)
480             __f_->destroy_deallocate();
481     }
482 
483     _LIBCPP_INLINE_VISIBILITY
484     __value_func& operator=(__value_func&& __f)
485     {
486         *this = nullptr;
487         if (__f.__f_ == nullptr)
488             __f_ = nullptr;
489         else if ((void*)__f.__f_ == &__f.__buf_)
490         {
491             __f_ = __as_base(&__buf_);
492             __f.__f_->__clone(__f_);
493         }
494         else
495         {
496             __f_ = __f.__f_;
497             __f.__f_ = nullptr;
498         }
499         return *this;
500     }
501 
502     _LIBCPP_INLINE_VISIBILITY
503     __value_func& operator=(nullptr_t)
504     {
505         __func* __f = __f_;
506         __f_ = nullptr;
507         if ((void*)__f == &__buf_)
508             __f->destroy();
509         else if (__f)
510             __f->destroy_deallocate();
511         return *this;
512     }
513 
514     _LIBCPP_INLINE_VISIBILITY
515     _Rp operator()(_ArgTypes&&... __args) const
516     {
517         if (__f_ == nullptr)
518             __throw_bad_function_call();
519         return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
520     }
521 
522     _LIBCPP_INLINE_VISIBILITY
523     void swap(__value_func& __f) _NOEXCEPT
524     {
525         if (&__f == this)
526             return;
527         if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
528         {
529             _LIBCPP_SUPPRESS_DEPRECATED_PUSH
530             typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
531             _LIBCPP_SUPPRESS_DEPRECATED_POP
532             __func* __t = __as_base(&__tempbuf);
533             __f_->__clone(__t);
534             __f_->destroy();
535             __f_ = nullptr;
536             __f.__f_->__clone(__as_base(&__buf_));
537             __f.__f_->destroy();
538             __f.__f_ = nullptr;
539             __f_ = __as_base(&__buf_);
540             __t->__clone(__as_base(&__f.__buf_));
541             __t->destroy();
542             __f.__f_ = __as_base(&__f.__buf_);
543         }
544         else if ((void*)__f_ == &__buf_)
545         {
546             __f_->__clone(__as_base(&__f.__buf_));
547             __f_->destroy();
548             __f_ = __f.__f_;
549             __f.__f_ = __as_base(&__f.__buf_);
550         }
551         else if ((void*)__f.__f_ == &__f.__buf_)
552         {
553             __f.__f_->__clone(__as_base(&__buf_));
554             __f.__f_->destroy();
555             __f.__f_ = __f_;
556             __f_ = __as_base(&__buf_);
557         }
558         else
559             _VSTD::swap(__f_, __f.__f_);
560     }
561 
562     _LIBCPP_INLINE_VISIBILITY
563     explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
564 
565 #ifndef _LIBCPP_HAS_NO_RTTI
566     _LIBCPP_INLINE_VISIBILITY
567     const std::type_info& target_type() const _NOEXCEPT
568     {
569         if (__f_ == nullptr)
570             return typeid(void);
571         return __f_->target_type();
572     }
573 
574     template <typename _Tp>
575     _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
576     {
577         if (__f_ == nullptr)
578             return nullptr;
579         return (const _Tp*)__f_->target(typeid(_Tp));
580     }
581 #endif // _LIBCPP_HAS_NO_RTTI
582 };
583 
584 // Storage for a functor object, to be used with __policy to manage copy and
585 // destruction.
586 union __policy_storage
587 {
588     mutable char __small[sizeof(void*) * 2];
589     void* __large;
590 };
591 
592 // True if _Fun can safely be held in __policy_storage.__small.
593 template <typename _Fun>
594 struct __use_small_storage
595     : public integral_constant<
596           bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
597                     _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
598                     is_trivially_copy_constructible<_Fun>::value &&
599                     is_trivially_destructible<_Fun>::value> {};
600 
601 // Policy contains information about how to copy, destroy, and move the
602 // underlying functor. You can think of it as a vtable of sorts.
603 struct __policy
604 {
605     // Used to copy or destroy __large values. null for trivial objects.
606     void* (*const __clone)(const void*);
607     void (*const __destroy)(void*);
608 
609     // True if this is the null policy (no value).
610     const bool __is_null;
611 
612     // The target type. May be null if RTTI is disabled.
613     const std::type_info* const __type_info;
614 
615     // Returns a pointer to a static policy object suitable for the functor
616     // type.
617     template <typename _Fun>
618     _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
619     {
620         return __choose_policy<_Fun>(__use_small_storage<_Fun>());
621     }
622 
623     _LIBCPP_INLINE_VISIBILITY
624     static const __policy* __create_empty()
625     {
626         static const _LIBCPP_CONSTEXPR __policy __policy = {nullptr, nullptr,
627                                                             true,
628 #ifndef _LIBCPP_HAS_NO_RTTI
629                                                             &typeid(void)
630 #else
631                                                             nullptr
632 #endif
633         };
634         return &__policy;
635     }
636 
637   private:
638     template <typename _Fun>
639     _LIBCPP_HIDE_FROM_ABI static void* __large_clone(const void* __s)
640     {
641         const _Fun* __f = static_cast<const _Fun*>(__s);
642         return __f->__clone();
643     }
644 
645     template <typename _Fun>
646     _LIBCPP_HIDE_FROM_ABI static void __large_destroy(void* __s) {
647       _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
648     }
649 
650     template <typename _Fun>
651     _LIBCPP_INLINE_VISIBILITY static const __policy*
652     __choose_policy(/* is_small = */ false_type) {
653       static const _LIBCPP_CONSTEXPR __policy __policy = {
654           &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
655 #ifndef _LIBCPP_HAS_NO_RTTI
656           &typeid(typename _Fun::_Target)
657 #else
658           nullptr
659 #endif
660       };
661         return &__policy;
662     }
663 
664     template <typename _Fun>
665     _LIBCPP_INLINE_VISIBILITY static const __policy*
666         __choose_policy(/* is_small = */ true_type)
667     {
668         static const _LIBCPP_CONSTEXPR __policy __policy = {
669             nullptr, nullptr, false,
670 #ifndef _LIBCPP_HAS_NO_RTTI
671             &typeid(typename _Fun::_Target)
672 #else
673             nullptr
674 #endif
675         };
676         return &__policy;
677     }
678 };
679 
680 // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
681 // faster for types that can be passed in registers.
682 template <typename _Tp>
683 using __fast_forward = __conditional_t<is_scalar<_Tp>::value, _Tp, _Tp&&>;
684 
685 // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
686 
687 template <class _Fp> struct __policy_invoker;
688 
689 template <class _Rp, class... _ArgTypes>
690 struct __policy_invoker<_Rp(_ArgTypes...)>
691 {
692     typedef _Rp (*__Call)(const __policy_storage*,
693                           __fast_forward<_ArgTypes>...);
694 
695     __Call __call_;
696 
697     // Creates an invoker that throws bad_function_call.
698     _LIBCPP_INLINE_VISIBILITY
699     __policy_invoker() : __call_(&__call_empty) {}
700 
701     // Creates an invoker that calls the given instance of __func.
702     template <typename _Fun>
703     _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
704     {
705         return __policy_invoker(&__call_impl<_Fun>);
706     }
707 
708   private:
709     _LIBCPP_INLINE_VISIBILITY
710     explicit __policy_invoker(__Call __c) : __call_(__c) {}
711 
712     _LIBCPP_HIDE_FROM_ABI static _Rp __call_empty(const __policy_storage*,
713                             __fast_forward<_ArgTypes>...)
714     {
715         __throw_bad_function_call();
716     }
717 
718     template <typename _Fun>
719     _LIBCPP_HIDE_FROM_ABI static _Rp __call_impl(const __policy_storage* __buf,
720                            __fast_forward<_ArgTypes>... __args)
721     {
722         _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
723                                                 ? &__buf->__small
724                                                 : __buf->__large);
725         return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
726     }
727 };
728 
729 // __policy_func uses a __policy and __policy_invoker to create a type-erased,
730 // copyable functor.
731 
732 template <class _Fp> class __policy_func;
733 
734 template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
735 {
736     // Inline storage for small objects.
737     __policy_storage __buf_;
738 
739     // Calls the value stored in __buf_. This could technically be part of
740     // policy, but storing it here eliminates a level of indirection inside
741     // operator().
742     typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
743     __invoker __invoker_;
744 
745     // The policy that describes how to move / copy / destroy __buf_. Never
746     // null, even if the function is empty.
747     const __policy* __policy_;
748 
749   public:
750     _LIBCPP_INLINE_VISIBILITY
751     __policy_func() : __policy_(__policy::__create_empty()) {}
752 
753     template <class _Fp, class _Alloc>
754     _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
755         : __policy_(__policy::__create_empty())
756     {
757         typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
758         typedef allocator_traits<_Alloc> __alloc_traits;
759         typedef __rebind_alloc<__alloc_traits, _Fun> _FunAlloc;
760 
761         if (__function::__not_null(__f))
762         {
763             __invoker_ = __invoker::template __create<_Fun>();
764             __policy_ = __policy::__create<_Fun>();
765 
766             _FunAlloc __af(__a);
767             if (__use_small_storage<_Fun>())
768             {
769                 ::new ((void*)&__buf_.__small)
770                     _Fun(_VSTD::move(__f), _Alloc(__af));
771             }
772             else
773             {
774                 typedef __allocator_destructor<_FunAlloc> _Dp;
775                 unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
776                 ::new ((void*)__hold.get())
777                     _Fun(_VSTD::move(__f), _Alloc(__af));
778                 __buf_.__large = __hold.release();
779             }
780         }
781     }
782 
783     template <class _Fp, __enable_if_t<!is_same<__decay_t<_Fp>, __policy_func>::value, int> = 0>
784     _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
785         : __policy_(__policy::__create_empty()) {
786       typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
787 
788       if (__function::__not_null(__f)) {
789         __invoker_ = __invoker::template __create<_Fun>();
790         __policy_ = __policy::__create<_Fun>();
791         if (__use_small_storage<_Fun>()) {
792           ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
793         } else {
794           __builtin_new_allocator::__holder_t __hold =
795               __builtin_new_allocator::__allocate_type<_Fun>(1);
796           __buf_.__large = ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f));
797           (void)__hold.release();
798         }
799       }
800     }
801 
802     _LIBCPP_INLINE_VISIBILITY
803     __policy_func(const __policy_func& __f)
804         : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
805           __policy_(__f.__policy_)
806     {
807         if (__policy_->__clone)
808             __buf_.__large = __policy_->__clone(__f.__buf_.__large);
809     }
810 
811     _LIBCPP_INLINE_VISIBILITY
812     __policy_func(__policy_func&& __f)
813         : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
814           __policy_(__f.__policy_)
815     {
816         if (__policy_->__destroy)
817         {
818             __f.__policy_ = __policy::__create_empty();
819             __f.__invoker_ = __invoker();
820         }
821     }
822 
823     _LIBCPP_INLINE_VISIBILITY
824     ~__policy_func()
825     {
826         if (__policy_->__destroy)
827             __policy_->__destroy(__buf_.__large);
828     }
829 
830     _LIBCPP_INLINE_VISIBILITY
831     __policy_func& operator=(__policy_func&& __f)
832     {
833         *this = nullptr;
834         __buf_ = __f.__buf_;
835         __invoker_ = __f.__invoker_;
836         __policy_ = __f.__policy_;
837         __f.__policy_ = __policy::__create_empty();
838         __f.__invoker_ = __invoker();
839         return *this;
840     }
841 
842     _LIBCPP_INLINE_VISIBILITY
843     __policy_func& operator=(nullptr_t)
844     {
845         const __policy* __p = __policy_;
846         __policy_ = __policy::__create_empty();
847         __invoker_ = __invoker();
848         if (__p->__destroy)
849             __p->__destroy(__buf_.__large);
850         return *this;
851     }
852 
853     _LIBCPP_INLINE_VISIBILITY
854     _Rp operator()(_ArgTypes&&... __args) const
855     {
856         return __invoker_.__call_(_VSTD::addressof(__buf_),
857                                   _VSTD::forward<_ArgTypes>(__args)...);
858     }
859 
860     _LIBCPP_INLINE_VISIBILITY
861     void swap(__policy_func& __f)
862     {
863         _VSTD::swap(__invoker_, __f.__invoker_);
864         _VSTD::swap(__policy_, __f.__policy_);
865         _VSTD::swap(__buf_, __f.__buf_);
866     }
867 
868     _LIBCPP_INLINE_VISIBILITY
869     explicit operator bool() const _NOEXCEPT
870     {
871         return !__policy_->__is_null;
872     }
873 
874 #ifndef _LIBCPP_HAS_NO_RTTI
875     _LIBCPP_INLINE_VISIBILITY
876     const std::type_info& target_type() const _NOEXCEPT
877     {
878         return *__policy_->__type_info;
879     }
880 
881     template <typename _Tp>
882     _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
883     {
884         if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
885             return nullptr;
886         if (__policy_->__clone) // Out of line storage.
887             return reinterpret_cast<const _Tp*>(__buf_.__large);
888         else
889             return reinterpret_cast<const _Tp*>(&__buf_.__small);
890     }
891 #endif // _LIBCPP_HAS_NO_RTTI
892 };
893 
894 #if defined(_LIBCPP_HAS_BLOCKS_RUNTIME)
895 
896 extern "C" void *_Block_copy(const void *);
897 extern "C" void _Block_release(const void *);
898 
899 template<class _Rp1, class ..._ArgTypes1, class _Alloc, class _Rp, class ..._ArgTypes>
900 class __func<_Rp1(^)(_ArgTypes1...), _Alloc, _Rp(_ArgTypes...)>
901     : public  __base<_Rp(_ArgTypes...)>
902 {
903     typedef _Rp1(^__block_type)(_ArgTypes1...);
904     __block_type __f_;
905 
906 public:
907     _LIBCPP_INLINE_VISIBILITY
908     explicit __func(__block_type const& __f)
909 #ifdef _LIBCPP_HAS_OBJC_ARC
910         : __f_(__f)
911 #else
912         : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
913 #endif
914     { }
915 
916     // [TODO] add && to save on a retain
917 
918     _LIBCPP_INLINE_VISIBILITY
919     explicit __func(__block_type __f, const _Alloc& /* unused */)
920 #ifdef _LIBCPP_HAS_OBJC_ARC
921         : __f_(__f)
922 #else
923         : __f_(reinterpret_cast<__block_type>(__f ? _Block_copy(__f) : nullptr))
924 #endif
925     { }
926 
927     virtual __base<_Rp(_ArgTypes...)>* __clone() const {
928         _LIBCPP_ASSERT_INTERNAL(false,
929             "Block pointers are just pointers, so they should always fit into "
930             "std::function's small buffer optimization. This function should "
931             "never be invoked.");
932         return nullptr;
933     }
934 
935     virtual void __clone(__base<_Rp(_ArgTypes...)>* __p) const {
936         ::new ((void*)__p) __func(__f_);
937     }
938 
939     virtual void destroy() _NOEXCEPT {
940 #ifndef _LIBCPP_HAS_OBJC_ARC
941         if (__f_)
942             _Block_release(__f_);
943 #endif
944         __f_ = 0;
945     }
946 
947     virtual void destroy_deallocate() _NOEXCEPT {
948         _LIBCPP_ASSERT_INTERNAL(false,
949             "Block pointers are just pointers, so they should always fit into "
950             "std::function's small buffer optimization. This function should "
951             "never be invoked.");
952     }
953 
954     virtual _Rp operator()(_ArgTypes&& ... __arg) {
955         return _VSTD::__invoke(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
956     }
957 
958 #ifndef _LIBCPP_HAS_NO_RTTI
959     virtual const void* target(type_info const& __ti) const _NOEXCEPT {
960         if (__ti == typeid(__func::__block_type))
961             return &__f_;
962         return (const void*)nullptr;
963     }
964 
965     virtual const std::type_info& target_type() const _NOEXCEPT {
966         return typeid(__func::__block_type);
967     }
968 #endif // _LIBCPP_HAS_NO_RTTI
969 };
970 
971 #endif // _LIBCPP_HAS_EXTENSION_BLOCKS
972 
973 } // namespace __function
974 
975 template<class _Rp, class ..._ArgTypes>
976 class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
977     : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
978       public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
979 {
980 #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
981     typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
982 #else
983     typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
984 #endif
985 
986     __func __f_;
987 
988     template <class _Fp, bool = _And<
989         _IsNotSame<__remove_cvref_t<_Fp>, function>,
990         __invokable<_Fp, _ArgTypes...>
991     >::value>
992     struct __callable;
993     template <class _Fp>
994         struct __callable<_Fp, true>
995         {
996             static const bool value = is_void<_Rp>::value ||
997                 __is_core_convertible<typename __invoke_of<_Fp, _ArgTypes...>::type,
998                                       _Rp>::value;
999         };
1000     template <class _Fp>
1001         struct __callable<_Fp, false>
1002         {
1003             static const bool value = false;
1004         };
1005 
1006   template <class _Fp>
1007   using _EnableIfLValueCallable = __enable_if_t<__callable<_Fp&>::value>;
1008 public:
1009     typedef _Rp result_type;
1010 
1011     // construct/copy/destroy:
1012     _LIBCPP_INLINE_VISIBILITY
1013     function() _NOEXCEPT { }
1014     _LIBCPP_INLINE_VISIBILITY
1015     _LIBCPP_HIDE_FROM_ABI function(nullptr_t) _NOEXCEPT {}
1016     _LIBCPP_HIDE_FROM_ABI function(const function&);
1017     _LIBCPP_HIDE_FROM_ABI function(function&&) _NOEXCEPT;
1018     template<class _Fp, class = _EnableIfLValueCallable<_Fp>>
1019     _LIBCPP_HIDE_FROM_ABI function(_Fp);
1020 
1021 #if _LIBCPP_STD_VER <= 14
1022     template<class _Alloc>
1023       _LIBCPP_INLINE_VISIBILITY
1024       function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
1025     template<class _Alloc>
1026       _LIBCPP_INLINE_VISIBILITY
1027       function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
1028     template<class _Alloc>
1029     _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, const function&);
1030     template<class _Alloc>
1031     _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc&, function&&);
1032     template<class _Fp, class _Alloc, class = _EnableIfLValueCallable<_Fp>>
1033     _LIBCPP_HIDE_FROM_ABI function(allocator_arg_t, const _Alloc& __a, _Fp __f);
1034 #endif
1035 
1036     _LIBCPP_HIDE_FROM_ABI function& operator=(const function&);
1037     _LIBCPP_HIDE_FROM_ABI function& operator=(function&&) _NOEXCEPT;
1038     _LIBCPP_HIDE_FROM_ABI function& operator=(nullptr_t) _NOEXCEPT;
1039     template<class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>
1040     _LIBCPP_HIDE_FROM_ABI function& operator=(_Fp&&);
1041 
1042     _LIBCPP_HIDE_FROM_ABI ~function();
1043 
1044     // function modifiers:
1045     _LIBCPP_HIDE_FROM_ABI void swap(function&) _NOEXCEPT;
1046 
1047 #if _LIBCPP_STD_VER <= 14
1048     template<class _Fp, class _Alloc>
1049       _LIBCPP_INLINE_VISIBILITY
1050       void assign(_Fp&& __f, const _Alloc& __a)
1051         {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
1052 #endif
1053 
1054     // function capacity:
1055     _LIBCPP_INLINE_VISIBILITY
1056     explicit operator bool() const _NOEXCEPT {
1057       return static_cast<bool>(__f_);
1058     }
1059 
1060     // deleted overloads close possible hole in the type system
1061     template<class _R2, class... _ArgTypes2>
1062       bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
1063 #if _LIBCPP_STD_VER <= 17
1064     template<class _R2, class... _ArgTypes2>
1065       bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
1066 #endif
1067 public:
1068     // function invocation:
1069     _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
1070 
1071 #ifndef _LIBCPP_HAS_NO_RTTI
1072     // function target access:
1073     _LIBCPP_HIDE_FROM_ABI const std::type_info& target_type() const _NOEXCEPT;
1074     template <typename _Tp>
1075     _LIBCPP_HIDE_FROM_ABI _Tp* target() _NOEXCEPT;
1076     template <typename _Tp>
1077     _LIBCPP_HIDE_FROM_ABI const _Tp* target() const _NOEXCEPT;
1078 #endif // _LIBCPP_HAS_NO_RTTI
1079 };
1080 
1081 #if _LIBCPP_STD_VER >= 17
1082 template<class _Rp, class ..._Ap>
1083 function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
1084 
1085 template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
1086 function(_Fp) -> function<_Stripped>;
1087 #endif // _LIBCPP_STD_VER >= 17
1088 
1089 template<class _Rp, class ..._ArgTypes>
1090 function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
1091 
1092 #if _LIBCPP_STD_VER <= 14
1093 template<class _Rp, class ..._ArgTypes>
1094 template <class _Alloc>
1095 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1096                                      const function& __f) : __f_(__f.__f_) {}
1097 #endif
1098 
1099 template <class _Rp, class... _ArgTypes>
1100 function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
1101     : __f_(_VSTD::move(__f.__f_)) {}
1102 
1103 #if _LIBCPP_STD_VER <= 14
1104 template<class _Rp, class ..._ArgTypes>
1105 template <class _Alloc>
1106 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
1107                                       function&& __f)
1108     : __f_(_VSTD::move(__f.__f_)) {}
1109 #endif
1110 
1111 template <class _Rp, class... _ArgTypes>
1112 template <class _Fp, class>
1113 function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
1114 
1115 #if _LIBCPP_STD_VER <= 14
1116 template <class _Rp, class... _ArgTypes>
1117 template <class _Fp, class _Alloc, class>
1118 function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
1119                                       _Fp __f)
1120     : __f_(_VSTD::move(__f), __a) {}
1121 #endif
1122 
1123 template<class _Rp, class ..._ArgTypes>
1124 function<_Rp(_ArgTypes...)>&
1125 function<_Rp(_ArgTypes...)>::operator=(const function& __f)
1126 {
1127     function(__f).swap(*this);
1128     return *this;
1129 }
1130 
1131 template<class _Rp, class ..._ArgTypes>
1132 function<_Rp(_ArgTypes...)>&
1133 function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
1134 {
1135     __f_ = _VSTD::move(__f.__f_);
1136     return *this;
1137 }
1138 
1139 template<class _Rp, class ..._ArgTypes>
1140 function<_Rp(_ArgTypes...)>&
1141 function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
1142 {
1143     __f_ = nullptr;
1144     return *this;
1145 }
1146 
1147 template<class _Rp, class ..._ArgTypes>
1148 template <class _Fp, class>
1149 function<_Rp(_ArgTypes...)>&
1150 function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
1151 {
1152     function(_VSTD::forward<_Fp>(__f)).swap(*this);
1153     return *this;
1154 }
1155 
1156 template<class _Rp, class ..._ArgTypes>
1157 function<_Rp(_ArgTypes...)>::~function() {}
1158 
1159 template<class _Rp, class ..._ArgTypes>
1160 void
1161 function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
1162 {
1163     __f_.swap(__f.__f_);
1164 }
1165 
1166 template<class _Rp, class ..._ArgTypes>
1167 _Rp
1168 function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
1169 {
1170     return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
1171 }
1172 
1173 #ifndef _LIBCPP_HAS_NO_RTTI
1174 
1175 template<class _Rp, class ..._ArgTypes>
1176 const std::type_info&
1177 function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
1178 {
1179     return __f_.target_type();
1180 }
1181 
1182 template<class _Rp, class ..._ArgTypes>
1183 template <typename _Tp>
1184 _Tp*
1185 function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
1186 {
1187     return (_Tp*)(__f_.template target<_Tp>());
1188 }
1189 
1190 template<class _Rp, class ..._ArgTypes>
1191 template <typename _Tp>
1192 const _Tp*
1193 function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
1194 {
1195     return __f_.template target<_Tp>();
1196 }
1197 
1198 #endif // _LIBCPP_HAS_NO_RTTI
1199 
1200 template <class _Rp, class... _ArgTypes>
1201 inline _LIBCPP_INLINE_VISIBILITY
1202 bool
1203 operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
1204 
1205 #if _LIBCPP_STD_VER <= 17
1206 
1207 template <class _Rp, class... _ArgTypes>
1208 inline _LIBCPP_INLINE_VISIBILITY
1209 bool
1210 operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
1211 
1212 template <class _Rp, class... _ArgTypes>
1213 inline _LIBCPP_INLINE_VISIBILITY
1214 bool
1215 operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
1216 
1217 template <class _Rp, class... _ArgTypes>
1218 inline _LIBCPP_INLINE_VISIBILITY
1219 bool
1220 operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
1221 
1222 #endif // _LIBCPP_STD_VER <= 17
1223 
1224 template <class _Rp, class... _ArgTypes>
1225 inline _LIBCPP_INLINE_VISIBILITY
1226 void
1227 swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
1228 {return __x.swap(__y);}
1229 
1230 _LIBCPP_END_NAMESPACE_STD
1231 
1232 #endif // _LIBCPP_CXX03_LANG
1233 
1234 #endif // _LIBCPP___FUNCTIONAL_FUNCTION_H
1235