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