libstdc++
std_function.h
Go to the documentation of this file.
1 // Implementation of std::function -*- C++ -*-
2 
3 // Copyright (C) 2004-2025 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file include/bits/std_function.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{functional}
28  */
29 
30 #ifndef _GLIBCXX_STD_FUNCTION_H
31 #define _GLIBCXX_STD_FUNCTION_H 1
32 
33 #ifdef _GLIBCXX_SYSHDR
34 #pragma GCC system_header
35 #endif
36 
37 #if __cplusplus < 201103L
38 # include <bits/c++0x_warning.h>
39 #else
40 
41 #include <new> // placement new
42 #include <typeinfo> // typeid
43 #include <bits/invoke.h> // __invoke_r
44 #include <bits/refwrap.h> // ref wrapper, _Maybe_unary_or_binary_function
45 #include <bits/functexcept.h> // __throw_bad_function_call
46 
47 namespace std _GLIBCXX_VISIBILITY(default)
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 
51  /**
52  * @brief Exception class thrown when class template function's
53  * operator() is called with an empty target.
54  * @ingroup exceptions
55  */
57  {
58  public:
59  virtual ~bad_function_call() noexcept;
60 
61  const char* what() const noexcept;
62  };
63 
64  /**
65  * Trait identifying "location-invariant" types, meaning that the
66  * address of the object (or any of its members) will not escape.
67  * Trivially copyable types are location-invariant and users can
68  * specialize this trait for other types.
69  */
70  template<typename _Tp>
72  : is_trivially_copyable<_Tp>::type
73  { };
74 
75  class _Undefined_class;
76 
77  union _Nocopy_types
78  {
79  void* _M_object;
80  const void* _M_const_object;
81  void (*_M_function_pointer)();
82  void (_Undefined_class::*_M_member_pointer)();
83  };
84 
85  union [[gnu::may_alias]] _Any_data
86  {
87  void* _M_access() noexcept { return &_M_pod_data[0]; }
88  const void* _M_access() const noexcept { return &_M_pod_data[0]; }
89 
90  template<typename _Tp>
91  _Tp&
92  _M_access() noexcept
93  { return *static_cast<_Tp*>(_M_access()); }
94 
95  template<typename _Tp>
96  const _Tp&
97  _M_access() const noexcept
98  { return *static_cast<const _Tp*>(_M_access()); }
99 
100  _Nocopy_types _M_unused;
101  char _M_pod_data[sizeof(_Nocopy_types)];
102  };
103 
104  enum _Manager_operation
105  {
106  __get_type_info,
107  __get_functor_ptr,
108  __clone_functor,
109  __destroy_functor
110  };
111 
112  template<typename _Signature>
113  class function;
114 
115  /// Base class of all polymorphic function object wrappers.
117  {
118  public:
119  static const size_t _M_max_size = sizeof(_Nocopy_types);
120  static const size_t _M_max_align = __alignof__(_Nocopy_types);
121 
122  template<typename _Functor>
123  class _Base_manager
124  {
125  protected:
126  static const bool __stored_locally =
128  && sizeof(_Functor) <= _M_max_size
129  && __alignof__(_Functor) <= _M_max_align
130  && (_M_max_align % __alignof__(_Functor) == 0));
131 
132  using _Local_storage = integral_constant<bool, __stored_locally>;
133 
134  // Retrieve a pointer to the function object
135  static _Functor*
136  _M_get_pointer(const _Any_data& __source) noexcept
137  {
138  if _GLIBCXX17_CONSTEXPR (__stored_locally)
139  {
140  const _Functor& __f = __source._M_access<_Functor>();
141  return const_cast<_Functor*>(std::__addressof(__f));
142  }
143  else // have stored a pointer
144  return __source._M_access<_Functor*>();
145  }
146 
147  private:
148  // Construct a location-invariant function object that fits within
149  // an _Any_data structure.
150  template<typename _Fn>
151  static void
152  _M_create(_Any_data& __dest, _Fn&& __f, true_type)
153  {
154  ::new (__dest._M_access()) _Functor(std::forward<_Fn>(__f));
155  }
156 
157  // Construct a function object on the heap and store a pointer.
158  template<typename _Fn>
159  static void
160  _M_create(_Any_data& __dest, _Fn&& __f, false_type)
161  {
162  __dest._M_access<_Functor*>()
163  = new _Functor(std::forward<_Fn>(__f));
164  }
165 
166  // Destroy an object stored in the internal buffer.
167  static void
168  _M_destroy(_Any_data& __victim, true_type)
169  {
170  __victim._M_access<_Functor>().~_Functor();
171  }
172 
173  // Destroy an object located on the heap.
174  static void
175  _M_destroy(_Any_data& __victim, false_type)
176  {
177  delete __victim._M_access<_Functor*>();
178  }
179 
180  public:
181  static bool
182  _M_manager(_Any_data& __dest, const _Any_data& __source,
183  _Manager_operation __op)
184  {
185  switch (__op)
186  {
187  case __get_type_info:
188 #if __cpp_rtti
189  __dest._M_access<const type_info*>() = &typeid(_Functor);
190 #else
191  __dest._M_access<const type_info*>() = nullptr;
192 #endif
193  break;
194 
195  case __get_functor_ptr:
196  __dest._M_access<_Functor*>() = _M_get_pointer(__source);
197  break;
198 
199  case __clone_functor:
200  _M_init_functor(__dest,
201  *const_cast<const _Functor*>(_M_get_pointer(__source)));
202  break;
203 
204  case __destroy_functor:
205  _M_destroy(__dest, _Local_storage());
206  break;
207  }
208  return false;
209  }
210 
211  template<typename _Fn>
212  static void
213  _M_init_functor(_Any_data& __functor, _Fn&& __f)
214  noexcept(__and_<_Local_storage,
216  {
217  _M_create(__functor, std::forward<_Fn>(__f), _Local_storage());
218  }
219 
220  template<typename _Signature>
221  static bool
222  _M_not_empty_function(const function<_Signature>& __f) noexcept
223  { return static_cast<bool>(__f); }
224 
225  template<typename _Tp>
226  static bool
227  _M_not_empty_function(_Tp* __fp) noexcept
228  { return __fp != nullptr; }
229 
230  template<typename _Class, typename _Tp>
231  static bool
232  _M_not_empty_function(_Tp _Class::* __mp) noexcept
233  { return __mp != nullptr; }
234 
235  template<typename _Tp>
236  static bool
237  _M_not_empty_function(const _Tp&) noexcept
238  { return true; }
239  };
240 
241  _Function_base() = default;
242 
243  ~_Function_base()
244  {
245  if (_M_manager)
246  _M_manager(_M_functor, _M_functor, __destroy_functor);
247  }
248 
249  bool _M_empty() const { return !_M_manager; }
250 
251  using _Manager_type
252  = bool (*)(_Any_data&, const _Any_data&, _Manager_operation);
253 
254  _Any_data _M_functor{};
255  _Manager_type _M_manager{};
256  };
257 
258  template<typename _Signature, typename _Functor>
259  class _Function_handler;
260 
261  template<typename _Res, typename _Functor, typename... _ArgTypes>
262  class _Function_handler<_Res(_ArgTypes...), _Functor>
263  : public _Function_base::_Base_manager<_Functor>
264  {
265  using _Base = _Function_base::_Base_manager<_Functor>;
266 
267  public:
268  static bool
269  _M_manager(_Any_data& __dest, const _Any_data& __source,
270  _Manager_operation __op)
271  {
272  switch (__op)
273  {
274 #if __cpp_rtti
275  case __get_type_info:
276  __dest._M_access<const type_info*>() = &typeid(_Functor);
277  break;
278 #endif
279  case __get_functor_ptr:
280  __dest._M_access<_Functor*>() = _Base::_M_get_pointer(__source);
281  break;
282 
283  default:
284  _Base::_M_manager(__dest, __source, __op);
285  }
286  return false;
287  }
288 
289  static _Res
290  _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
291  {
292  return std::__invoke_r<_Res>(*_Base::_M_get_pointer(__functor),
293  std::forward<_ArgTypes>(__args)...);
294  }
295 
296  template<typename _Fn>
297  static constexpr bool
298  _S_nothrow_init() noexcept
299  {
300  return __and_<typename _Base::_Local_storage,
301  is_nothrow_constructible<_Functor, _Fn>>::value;
302  }
303  };
304 
305  // Specialization for invalid types
306  template<>
307  class _Function_handler<void, void>
308  {
309  public:
310  static bool
311  _M_manager(_Any_data&, const _Any_data&, _Manager_operation)
312  { return false; }
313  };
314 
315  // Avoids instantiating ill-formed specializations of _Function_handler
316  // in std::function<_Signature>::target<_Functor>().
317  // e.g. _Function_handler<Sig, void()> and _Function_handler<Sig, void>
318  // would be ill-formed.
319  template<typename _Signature, typename _Functor,
320  bool __valid = is_object<_Functor>::value>
321  struct _Target_handler
322  : _Function_handler<_Signature, typename remove_cv<_Functor>::type>
323  { };
324 
325  template<typename _Signature, typename _Functor>
326  struct _Target_handler<_Signature, _Functor, false>
327  : _Function_handler<void, void>
328  { };
329 
330  /**
331  * @brief Polymorphic function wrapper.
332  * @ingroup functors
333  * @since C++11
334  */
335  template<typename _Res, typename... _ArgTypes>
336  class function<_Res(_ArgTypes...)>
337  : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
338  private _Function_base
339  {
340  // Equivalent to std::decay_t except that it produces an invalid type
341  // if the decayed type is the current specialization of std::function.
342  template<typename _Func,
343  bool _Self = is_same<__remove_cvref_t<_Func>, function>::value>
344  using _Decay_t
345  = typename __enable_if_t<!_Self, decay<_Func>>::type;
346 
347  template<typename _Func,
348  typename _DFunc = _Decay_t<_Func>,
349  typename _Res2 = __invoke_result<_DFunc&, _ArgTypes...>>
350  struct _Callable
351  : __is_invocable_impl<_Res2, _Res>::type
352  { };
353 
354  template<typename _Cond, typename _Tp = void>
355  using _Requires = __enable_if_t<_Cond::value, _Tp>;
356 
357  template<typename _Functor>
358  using _Handler
359  = _Function_handler<_Res(_ArgTypes...), __decay_t<_Functor>>;
360 
361  public:
362  typedef _Res result_type;
363 
364  // [3.7.2.1] construct/copy/destroy
365 
366  /**
367  * @brief Default construct creates an empty function call wrapper.
368  * @post `!(bool)*this`
369  */
370  function() noexcept
371  : _Function_base() { }
372 
373  /**
374  * @brief Creates an empty function call wrapper.
375  * @post @c !(bool)*this
376  */
377  function(nullptr_t) noexcept
378  : _Function_base() { }
379 
380  /**
381  * @brief %Function copy constructor.
382  * @param __x A %function object with identical call signature.
383  * @post `bool(*this) == bool(__x)`
384  *
385  * The newly-created %function contains a copy of the target of
386  * `__x` (if it has one).
387  */
388  function(const function& __x)
389  : _Function_base()
390  {
391  if (static_cast<bool>(__x))
392  {
393  __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
394  _M_invoker = __x._M_invoker;
395  _M_manager = __x._M_manager;
396  }
397  }
398 
399  /**
400  * @brief %Function move constructor.
401  * @param __x A %function object rvalue with identical call signature.
402  *
403  * The newly-created %function contains the target of `__x`
404  * (if it has one).
405  */
406  function(function&& __x) noexcept
407  : _Function_base(), _M_invoker(__x._M_invoker)
408  {
409  if (static_cast<bool>(__x))
410  {
411  _M_functor = __x._M_functor;
412  _M_manager = __x._M_manager;
413  __x._M_manager = nullptr;
414  __x._M_invoker = nullptr;
415  }
416  }
417 
418  /**
419  * @brief Builds a %function that targets a copy of the incoming
420  * function object.
421  * @param __f A %function object that is callable with parameters of
422  * type `ArgTypes...` and returns a value convertible to `Res`.
423  *
424  * The newly-created %function object will target a copy of
425  * `__f`. If `__f` is `reference_wrapper<F>`, then this function
426  * object will contain a reference to the function object `__f.get()`.
427  * If `__f` is a null function pointer, null pointer-to-member, or
428  * empty `std::function`, the newly-created object will be empty.
429  *
430  * If `__f` is a non-null function pointer or an object of type
431  * `reference_wrapper<F>`, this function will not throw.
432  */
433  // _GLIBCXX_RESOLVE_LIB_DEFECTS
434  // 2774. std::function construction vs assignment
435  template<typename _Functor,
436  typename _Constraints = _Requires<_Callable<_Functor>>>
437  function(_Functor&& __f)
438  noexcept(_Handler<_Functor>::template _S_nothrow_init<_Functor>())
439  : _Function_base()
440  {
441  static_assert(is_copy_constructible<__decay_t<_Functor>>::value,
442  "std::function target must be copy-constructible");
443  static_assert(is_constructible<__decay_t<_Functor>, _Functor>::value,
444  "std::function target must be constructible from the "
445  "constructor argument");
446 
447  using _My_handler = _Handler<_Functor>;
448 
449  if (_My_handler::_M_not_empty_function(__f))
450  {
451  _My_handler::_M_init_functor(_M_functor,
452  std::forward<_Functor>(__f));
453  _M_invoker = &_My_handler::_M_invoke;
454  _M_manager = &_My_handler::_M_manager;
455  }
456  }
457 
458  /**
459  * @brief Function assignment operator.
460  * @param __x A %function with identical call signature.
461  * @post `(bool)*this == (bool)x`
462  * @returns `*this`
463  *
464  * The target of `__x` is copied to `*this`. If `__x` has no
465  * target, then `*this` will be empty.
466  *
467  * If `__x` targets a function pointer or a reference to a function
468  * object, then this operation will not throw an exception.
469  */
470  function&
471  operator=(const function& __x)
472  {
473  function(__x).swap(*this);
474  return *this;
475  }
476 
477  /**
478  * @brief Function move-assignment operator.
479  * @param __x A %function rvalue with identical call signature.
480  * @returns `*this`
481  *
482  * The target of `__x` is moved to `*this`. If `__x` has no
483  * target, then `*this` will be empty.
484  *
485  * If `__x` targets a function pointer or a reference to a function
486  * object, then this operation will not throw an exception.
487  */
488  function&
489  operator=(function&& __x) noexcept
490  {
491  function(std::move(__x)).swap(*this);
492  return *this;
493  }
494 
495  /**
496  * @brief Function assignment to empty.
497  * @post `!(bool)*this`
498  * @returns `*this`
499  *
500  * The target of `*this` is deallocated, leaving it empty.
501  */
502  function&
503  operator=(nullptr_t) noexcept
504  {
505  if (_M_manager)
506  {
507  _M_manager(_M_functor, _M_functor, __destroy_functor);
508  _M_manager = nullptr;
509  _M_invoker = nullptr;
510  }
511  return *this;
512  }
513 
514  /**
515  * @brief Function assignment to a new target.
516  * @param __f A function object that is callable with parameters of
517  * type `_ArgTypes...` and returns a value convertible
518  * to `_Res`.
519  * @return `*this`
520  * @since C++11
521  *
522  * This function object wrapper will target a copy of `__f`. If `__f`
523  * is `reference_wrapper<F>`, then this function object will contain
524  * a reference to the function object `__f.get()`. If `__f` is a null
525  * function pointer or null pointer-to-member, this object will be
526  * empty.
527  *
528  * If `__f` is a non-null function pointer or an object of type
529  * `reference_wrapper<F>`, this function will not throw.
530  */
531  template<typename _Functor>
532  _Requires<_Callable<_Functor>, function&>
533  operator=(_Functor&& __f)
534  noexcept(_Handler<_Functor>::template _S_nothrow_init<_Functor>())
535  {
536  function(std::forward<_Functor>(__f)).swap(*this);
537  return *this;
538  }
539 
540  /// @overload
541  template<typename _Functor>
542  function&
544  {
545  function(__f).swap(*this);
546  return *this;
547  }
548 
549  // [3.7.2.2] function modifiers
550 
551  /**
552  * @brief Swap the targets of two %function objects.
553  * @param __x A %function with identical call signature.
554  *
555  * Swap the targets of `this` function object and `__f`.
556  * This function will not throw exceptions.
557  */
558  void swap(function& __x) noexcept
559  {
560  std::swap(_M_functor, __x._M_functor);
561  std::swap(_M_manager, __x._M_manager);
562  std::swap(_M_invoker, __x._M_invoker);
563  }
564 
565  // [3.7.2.3] function capacity
566 
567  /**
568  * @brief Determine if the %function wrapper has a target.
569  *
570  * @return `true` when this function object contains a target,
571  * or `false` when it is empty.
572  *
573  * This function will not throw exceptions.
574  */
575  explicit operator bool() const noexcept
576  { return !_M_empty(); }
577 
578  // [3.7.2.4] function invocation
579 
580  /**
581  * @brief Invokes the function targeted by `*this`.
582  * @returns the result of the target.
583  * @throws `bad_function_call` when `!(bool)*this`
584  *
585  * The function call operator invokes the target function object
586  * stored by `this`.
587  */
588  _Res
589  operator()(_ArgTypes... __args) const
590  {
591  if (_M_empty())
592  __throw_bad_function_call();
593  return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
594  }
595 
596 #if __cpp_rtti
597  // [3.7.2.5] function target access
598  /**
599  * @brief Determine the type of the target of this function object
600  * wrapper.
601  *
602  * @returns the type identifier of the target function object, or
603  * `typeid(void)` if `!(bool)*this`.
604  *
605  * This function will not throw exceptions.
606  */
607  const type_info&
608  target_type() const noexcept
609  {
610  if (_M_manager)
611  {
612  _Any_data __typeinfo_result;
613  _M_manager(__typeinfo_result, _M_functor, __get_type_info);
614  if (auto __ti = __typeinfo_result._M_access<const type_info*>())
615  return *__ti;
616  }
617  return typeid(void);
618  }
619 #endif
620 
621  /**
622  * @brief Access the stored target function object.
623  *
624  * @return Returns a pointer to the stored target function object,
625  * if `typeid(_Functor).equals(target_type())`; otherwise, a null
626  * pointer.
627  *
628  * This function does not throw exceptions.
629  *
630  * @{
631  */
632  template<typename _Functor>
633  _Functor*
634  target() noexcept
635  {
636  const function* __const_this = this;
637  const _Functor* __func = __const_this->template target<_Functor>();
638  // If is_function_v<_Functor> is true then const_cast<_Functor*>
639  // would be ill-formed, so use *const_cast<_Functor**> instead.
640  return *const_cast<_Functor**>(&__func);
641  }
642 
643  template<typename _Functor>
644  const _Functor*
645  target() const noexcept
646  {
647  if _GLIBCXX17_CONSTEXPR (is_object<_Functor>::value)
648  {
649  // For C++11 and C++14 if-constexpr is not used above, so
650  // _Target_handler avoids ill-formed _Function_handler types.
651  using _Handler = _Target_handler<_Res(_ArgTypes...), _Functor>;
652 
653  if (_M_manager == &_Handler::_M_manager
654 #if __cpp_rtti
655  || (_M_manager && typeid(_Functor) == target_type())
656 #endif
657  )
658  {
659  _Any_data __ptr;
660  _M_manager(__ptr, _M_functor, __get_functor_ptr);
661  return __ptr._M_access<const _Functor*>();
662  }
663  }
664  return nullptr;
665  }
666  /// @}
667 
668  private:
669  using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
670  _Invoker_type _M_invoker = nullptr;
671  };
672 
673 #if __cpp_deduction_guides >= 201606
674  template<typename>
675  struct __function_guide_helper
676  { };
677 
678  template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
679  struct __function_guide_helper<
680  _Res (_Tp::*) (_Args...) noexcept(_Nx)
681  >
682  { using type = _Res(_Args...); };
683 
684  template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
685  struct __function_guide_helper<
686  _Res (_Tp::*) (_Args...) & noexcept(_Nx)
687  >
688  { using type = _Res(_Args...); };
689 
690  template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
691  struct __function_guide_helper<
692  _Res (_Tp::*) (_Args...) const noexcept(_Nx)
693  >
694  { using type = _Res(_Args...); };
695 
696  template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
697  struct __function_guide_helper<
698  _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
699  >
700  { using type = _Res(_Args...); };
701 
702 #if __cpp_explicit_this_parameter >= 202110L
703  // _GLIBCXX_RESOLVE_LIB_DEFECTS
704  // 3617. function/packaged_task deduction guides and deducing this
705  template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
706  struct __function_guide_helper<_Res (*) (_Tp, _Args...) noexcept(_Nx)>
707  { using type = _Res(_Args...); };
708 #endif
709 
710 #if __cpp_static_call_operator >= 202207L && __cpp_concepts >= 202002L
711  template<typename _StaticCallOp>
712  struct __function_guide_static_helper
713  { };
714 
715  template<typename _Res, bool _Nx, typename... _Args>
716  struct __function_guide_static_helper<_Res (*) (_Args...) noexcept(_Nx)>
717  { using type = _Res(_Args...); };
718 
719  template<typename _Fn, typename _Op>
720  using __function_guide_t = typename __conditional_t<
721  requires (_Fn& __f) { (void) __f.operator(); },
722  __function_guide_static_helper<_Op>,
723  __function_guide_helper<_Op>>::type;
724 #else
725  template<typename _Fn, typename _Op>
726  using __function_guide_t = typename __function_guide_helper<_Op>::type;
727 #endif
728 
729  template<typename _Res, typename... _ArgTypes>
730  function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
731 
732  template<typename _Fn, typename _Signature
733  = __function_guide_t<_Fn, decltype(&_Fn::operator())>>
734  function(_Fn) -> function<_Signature>;
735 #endif
736 
737  // [20.7.15.2.6] null pointer comparisons
738 
739  /**
740  * @brief Test whether a polymorphic function object wrapper is empty.
741  * @returns `true` if the wrapper has no target, `false` otherwise
742  *
743  * This function will not throw exceptions.
744  */
745  template<typename _Res, typename... _Args>
746  inline bool
747  operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
748  { return !static_cast<bool>(__f); }
749 
750 #if __cpp_impl_three_way_comparison < 201907L
751  /// @overload
752  template<typename _Res, typename... _Args>
753  inline bool
754  operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
755  { return !static_cast<bool>(__f); }
756 
757  /**
758  * @brief Test whether a polymorphic function object wrapper is non-empty.
759  * @returns `false` if the wrapper has no target, `true` otherwise
760  *
761  * This function will not throw exceptions.
762  */
763  template<typename _Res, typename... _Args>
764  inline bool
765  operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
766  { return static_cast<bool>(__f); }
767 
768  /// @overload
769  template<typename _Res, typename... _Args>
770  inline bool
771  operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
772  { return static_cast<bool>(__f); }
773 #endif
774 
775  // [20.7.15.2.7] specialized algorithms
776 
777  /**
778  * @brief Swap the targets of two polymorphic function object wrappers.
779  *
780  * This function will not throw exceptions.
781  */
782  // _GLIBCXX_RESOLVE_LIB_DEFECTS
783  // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
784  template<typename _Res, typename... _Args>
785  inline void
786  swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
787  { __x.swap(__y); }
788 
789 #if __cplusplus >= 201703L
790  namespace __detail::__variant
791  {
792  template<typename> struct _Never_valueless_alt; // see <variant>
793 
794  // Provide the strong exception-safety guarantee when emplacing a
795  // function into a variant.
796  template<typename _Signature>
797  struct _Never_valueless_alt<std::function<_Signature>>
799  { };
800  } // namespace __detail::__variant
801 #endif // C++17
802 
803 _GLIBCXX_END_NAMESPACE_VERSION
804 } // namespace std
805 
806 #endif // C++11
807 #endif // _GLIBCXX_STD_FUNCTION_H
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:116
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:119
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:52
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:138
ISO C++ entities toplevel namespace is std.
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y) noexcept
Swap the targets of two polymorphic function object wrappers.
Definition: std_function.h:786
bool operator==(const function< _Res(_Args...)> &__f, nullptr_t) noexcept
Test whether a polymorphic function object wrapper is empty.
Definition: std_function.h:747
Part of RTTI.
Definition: typeinfo:94
Primary class template for reference_wrapper.
Definition: refwrap.h:316
integral_constant
Definition: type_traits:93
is_object
Definition: type_traits:757
is_same
Definition: type_traits:1540
is_trivially_copyable
Definition: type_traits:909
is_constructible
Definition: type_traits:1158
is_copy_constructible
Definition: type_traits:1194
is_nothrow_constructible
Definition: type_traits:1236
Base class for all library exceptions.
Definition: exception.h:62
Exception class thrown when class template function's operator() is called with an empty target.
Definition: std_function.h:57
const char * what() const noexcept
Base class of all polymorphic function object wrappers.
Definition: std_function.h:117
function & operator=(function &&__x) noexcept
Function move-assignment operator.
Definition: std_function.h:489
function & operator=(nullptr_t) noexcept
Function assignment to empty.
Definition: std_function.h:503
function & operator=(const function &__x)
Function assignment operator.
Definition: std_function.h:471
const type_info & target_type() const noexcept
Determine the type of the target of this function object wrapper.
Definition: std_function.h:608
_Res operator()(_ArgTypes... __args) const
Invokes the function targeted by *this.
Definition: std_function.h:589
void swap(function &__x) noexcept
Swap the targets of two function objects.
Definition: std_function.h:558
_Requires< _Callable< _Functor >, function & > operator=(_Functor &&__f) noexcept(_Handler< _Functor >::template _S_nothrow_init< _Functor >())
Function assignment to a new target.
Definition: std_function.h:533
const _Functor * target() const noexcept
Access the stored target function object.
Definition: std_function.h:645
_Functor * target() noexcept
Access the stored target function object.
Definition: std_function.h:634
function & operator=(reference_wrapper< _Functor > __f) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: std_function.h:543