libstdc++
std_function.h
1 // Implementation of std::function -*- C++ -*-
2 
3 // Copyright (C) 2004-2016 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/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 #pragma GCC system_header
34 
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
37 #else
38 
39 #if __cpp_rtti
40 # include <typeinfo>
41 #endif
42 #include <bits/stl_function.h>
43 #include <bits/invoke.h>
44 #include <bits/refwrap.h>
45 #include <bits/functexcept.h>
46 
47 namespace std _GLIBCXX_VISIBILITY(default)
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 
51  /**
52  * Derives from @c unary_function or @c binary_function, or perhaps
53  * nothing, depending on the number of arguments provided. The
54  * primary template is the basis case, which derives nothing.
55  */
56  template<typename _Res, typename... _ArgTypes>
58 
59  /// Derives from @c unary_function, as appropriate.
60  template<typename _Res, typename _T1>
62  : std::unary_function<_T1, _Res> { };
63 
64  /// Derives from @c binary_function, as appropriate.
65  template<typename _Res, typename _T1, typename _T2>
66  struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
67  : std::binary_function<_T1, _T2, _Res> { };
68 
69 
70  /**
71  * @brief Exception class thrown when class template function's
72  * operator() is called with an empty target.
73  * @ingroup exceptions
74  */
76  {
77  public:
78  virtual ~bad_function_call() noexcept;
79 
80  const char* what() const noexcept;
81  };
82 
83  /**
84  * Trait identifying "location-invariant" types, meaning that the
85  * address of the object (or any of its members) will not escape.
86  * Trivially copyable types are location-invariant and users can
87  * specialize this trait for other types.
88  */
89  template<typename _Tp>
91  : is_trivially_copyable<_Tp>::type
92  { };
93 
94  class _Undefined_class;
95 
96  union _Nocopy_types
97  {
98  void* _M_object;
99  const void* _M_const_object;
100  void (*_M_function_pointer)();
101  void (_Undefined_class::*_M_member_pointer)();
102  };
103 
104  union [[gnu::may_alias]] _Any_data
105  {
106  void* _M_access() { return &_M_pod_data[0]; }
107  const void* _M_access() const { return &_M_pod_data[0]; }
108 
109  template<typename _Tp>
110  _Tp&
111  _M_access()
112  { return *static_cast<_Tp*>(_M_access()); }
113 
114  template<typename _Tp>
115  const _Tp&
116  _M_access() const
117  { return *static_cast<const _Tp*>(_M_access()); }
118 
119  _Nocopy_types _M_unused;
120  char _M_pod_data[sizeof(_Nocopy_types)];
121  };
122 
123  enum _Manager_operation
124  {
125  __get_type_info,
126  __get_functor_ptr,
127  __clone_functor,
128  __destroy_functor
129  };
130 
131  // Simple type wrapper that helps avoid annoying const problems
132  // when casting between void pointers and pointers-to-pointers.
133  template<typename _Tp>
134  struct _Simple_type_wrapper
135  {
136  _Simple_type_wrapper(_Tp __value) : __value(__value) { }
137 
138  _Tp __value;
139  };
140 
141  template<typename _Tp>
142  struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
144  { };
145 
146  template<typename _Signature>
147  class function;
148 
149  /// Base class of all polymorphic function object wrappers.
151  {
152  public:
153  static const std::size_t _M_max_size = sizeof(_Nocopy_types);
154  static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
155 
156  template<typename _Functor>
157  class _Base_manager
158  {
159  protected:
160  static const bool __stored_locally =
162  && sizeof(_Functor) <= _M_max_size
163  && __alignof__(_Functor) <= _M_max_align
164  && (_M_max_align % __alignof__(_Functor) == 0));
165 
166  typedef integral_constant<bool, __stored_locally> _Local_storage;
167 
168  // Retrieve a pointer to the function object
169  static _Functor*
170  _M_get_pointer(const _Any_data& __source)
171  {
172  const _Functor* __ptr =
173  __stored_locally? std::__addressof(__source._M_access<_Functor>())
174  /* have stored a pointer */ : __source._M_access<_Functor*>();
175  return const_cast<_Functor*>(__ptr);
176  }
177 
178  // Clone a location-invariant function object that fits within
179  // an _Any_data structure.
180  static void
181  _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
182  {
183  ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
184  }
185 
186  // Clone a function object that is not location-invariant or
187  // that cannot fit into an _Any_data structure.
188  static void
189  _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
190  {
191  __dest._M_access<_Functor*>() =
192  new _Functor(*__source._M_access<_Functor*>());
193  }
194 
195  // Destroying a location-invariant object may still require
196  // destruction.
197  static void
198  _M_destroy(_Any_data& __victim, true_type)
199  {
200  __victim._M_access<_Functor>().~_Functor();
201  }
202 
203  // Destroying an object located on the heap.
204  static void
205  _M_destroy(_Any_data& __victim, false_type)
206  {
207  delete __victim._M_access<_Functor*>();
208  }
209 
210  public:
211  static bool
212  _M_manager(_Any_data& __dest, const _Any_data& __source,
213  _Manager_operation __op)
214  {
215  switch (__op)
216  {
217 #if __cpp_rtti
218  case __get_type_info:
219  __dest._M_access<const type_info*>() = &typeid(_Functor);
220  break;
221 #endif
222  case __get_functor_ptr:
223  __dest._M_access<_Functor*>() = _M_get_pointer(__source);
224  break;
225 
226  case __clone_functor:
227  _M_clone(__dest, __source, _Local_storage());
228  break;
229 
230  case __destroy_functor:
231  _M_destroy(__dest, _Local_storage());
232  break;
233  }
234  return false;
235  }
236 
237  static void
238  _M_init_functor(_Any_data& __functor, _Functor&& __f)
239  { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
240 
241  template<typename _Signature>
242  static bool
243  _M_not_empty_function(const function<_Signature>& __f)
244  { return static_cast<bool>(__f); }
245 
246  template<typename _Tp>
247  static bool
248  _M_not_empty_function(_Tp* __fp)
249  { return __fp != nullptr; }
250 
251  template<typename _Class, typename _Tp>
252  static bool
253  _M_not_empty_function(_Tp _Class::* __mp)
254  { return __mp != nullptr; }
255 
256  template<typename _Tp>
257  static bool
258  _M_not_empty_function(const _Tp&)
259  { return true; }
260 
261  private:
262  static void
263  _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
264  { ::new (__functor._M_access()) _Functor(std::move(__f)); }
265 
266  static void
267  _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
268  { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
269  };
270 
271  template<typename _Functor>
272  class _Ref_manager : public _Base_manager<_Functor*>
273  {
274  typedef _Function_base::_Base_manager<_Functor*> _Base;
275 
276  public:
277  static bool
278  _M_manager(_Any_data& __dest, const _Any_data& __source,
279  _Manager_operation __op)
280  {
281  switch (__op)
282  {
283 #if __cpp_rtti
284  case __get_type_info:
285  __dest._M_access<const type_info*>() = &typeid(_Functor);
286  break;
287 #endif
288  case __get_functor_ptr:
289  __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
291  break;
292 
293  default:
294  _Base::_M_manager(__dest, __source, __op);
295  }
296  return false;
297  }
298 
299  static void
300  _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
301  {
302  _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
303  }
304  };
305 
306  _Function_base() : _M_manager(nullptr) { }
307 
308  ~_Function_base()
309  {
310  if (_M_manager)
311  _M_manager(_M_functor, _M_functor, __destroy_functor);
312  }
313 
314 
315  bool _M_empty() const { return !_M_manager; }
316 
317  typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
318  _Manager_operation);
319 
320  _Any_data _M_functor;
321  _Manager_type _M_manager;
322  };
323 
324  template<typename _Signature, typename _Functor>
325  class _Function_handler;
326 
327  template<typename _Res, typename _Functor, typename... _ArgTypes>
328  class _Function_handler<_Res(_ArgTypes...), _Functor>
329  : public _Function_base::_Base_manager<_Functor>
330  {
331  typedef _Function_base::_Base_manager<_Functor> _Base;
332 
333  public:
334  static _Res
335  _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
336  {
337  return (*_Base::_M_get_pointer(__functor))(
338  std::forward<_ArgTypes>(__args)...);
339  }
340  };
341 
342  template<typename _Functor, typename... _ArgTypes>
343  class _Function_handler<void(_ArgTypes...), _Functor>
344  : public _Function_base::_Base_manager<_Functor>
345  {
346  typedef _Function_base::_Base_manager<_Functor> _Base;
347 
348  public:
349  static void
350  _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
351  {
352  (*_Base::_M_get_pointer(__functor))(
353  std::forward<_ArgTypes>(__args)...);
354  }
355  };
356 
357  template<typename _Res, typename _Functor, typename... _ArgTypes>
358  class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
359  : public _Function_base::_Ref_manager<_Functor>
360  {
361  typedef _Function_base::_Ref_manager<_Functor> _Base;
362 
363  public:
364  static _Res
365  _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
366  {
367  return std::__invoke(**_Base::_M_get_pointer(__functor),
368  std::forward<_ArgTypes>(__args)...);
369  }
370  };
371 
372  template<typename _Functor, typename... _ArgTypes>
373  class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
374  : public _Function_base::_Ref_manager<_Functor>
375  {
376  typedef _Function_base::_Ref_manager<_Functor> _Base;
377 
378  public:
379  static void
380  _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
381  {
382  std::__invoke(**_Base::_M_get_pointer(__functor),
383  std::forward<_ArgTypes>(__args)...);
384  }
385  };
386 
387  template<typename _Class, typename _Member, typename _Res,
388  typename... _ArgTypes>
389  class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
390  : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
391  {
392  typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
393  _Base;
394 
395  public:
396  static _Res
397  _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
398  {
399  return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
400  std::forward<_ArgTypes>(__args)...);
401  }
402  };
403 
404  template<typename _Class, typename _Member, typename... _ArgTypes>
405  class _Function_handler<void(_ArgTypes...), _Member _Class::*>
406  : public _Function_base::_Base_manager<
407  _Simple_type_wrapper< _Member _Class::* > >
408  {
409  typedef _Member _Class::* _Functor;
410  typedef _Simple_type_wrapper<_Functor> _Wrapper;
411  typedef _Function_base::_Base_manager<_Wrapper> _Base;
412 
413  public:
414  static bool
415  _M_manager(_Any_data& __dest, const _Any_data& __source,
416  _Manager_operation __op)
417  {
418  switch (__op)
419  {
420 #if __cpp_rtti
421  case __get_type_info:
422  __dest._M_access<const type_info*>() = &typeid(_Functor);
423  break;
424 #endif
425  case __get_functor_ptr:
426  __dest._M_access<_Functor*>() =
427  &_Base::_M_get_pointer(__source)->__value;
428  break;
429 
430  default:
431  _Base::_M_manager(__dest, __source, __op);
432  }
433  return false;
434  }
435 
436  static void
437  _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
438  {
439  std::__invoke(_Base::_M_get_pointer(__functor)->__value,
440  std::forward<_ArgTypes>(__args)...);
441  }
442  };
443 
444  template<typename _From, typename _To>
445  using __check_func_return_type
446  = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
447 
448  /**
449  * @brief Primary class template for std::function.
450  * @ingroup functors
451  *
452  * Polymorphic function wrapper.
453  */
454  template<typename _Res, typename... _ArgTypes>
455  class function<_Res(_ArgTypes...)>
456  : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
457  private _Function_base
458  {
459  typedef _Res _Signature_type(_ArgTypes...);
460 
461  template<typename _Func,
462  typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
463  struct _Callable : __check_func_return_type<_Res2, _Res> { };
464 
465  // Used so the return type convertibility checks aren't done when
466  // performing overload resolution for copy construction/assignment.
467  template<typename _Tp>
468  struct _Callable<function, _Tp> : false_type { };
469 
470  template<typename _Cond, typename _Tp>
471  using _Requires = typename enable_if<_Cond::value, _Tp>::type;
472 
473  public:
474  typedef _Res result_type;
475 
476  // [3.7.2.1] construct/copy/destroy
477 
478  /**
479  * @brief Default construct creates an empty function call wrapper.
480  * @post @c !(bool)*this
481  */
482  function() noexcept
483  : _Function_base() { }
484 
485  /**
486  * @brief Creates an empty function call wrapper.
487  * @post @c !(bool)*this
488  */
489  function(nullptr_t) noexcept
490  : _Function_base() { }
491 
492  /**
493  * @brief %Function copy constructor.
494  * @param __x A %function object with identical call signature.
495  * @post @c bool(*this) == bool(__x)
496  *
497  * The newly-created %function contains a copy of the target of @a
498  * __x (if it has one).
499  */
500  function(const function& __x);
501 
502  /**
503  * @brief %Function move constructor.
504  * @param __x A %function object rvalue with identical call signature.
505  *
506  * The newly-created %function contains the target of @a __x
507  * (if it has one).
508  */
509  function(function&& __x) : _Function_base()
510  {
511  __x.swap(*this);
512  }
513 
514  /**
515  * @brief Builds a %function that targets a copy of the incoming
516  * function object.
517  * @param __f A %function object that is callable with parameters of
518  * type @c T1, @c T2, ..., @c TN and returns a value convertible
519  * to @c Res.
520  *
521  * The newly-created %function object will target a copy of
522  * @a __f. If @a __f is @c reference_wrapper<F>, then this function
523  * object will contain a reference to the function object @c
524  * __f.get(). If @a __f is a NULL function pointer or NULL
525  * pointer-to-member, the newly-created object will be empty.
526  *
527  * If @a __f is a non-NULL function pointer or an object of type @c
528  * reference_wrapper<F>, this function will not throw.
529  */
530  template<typename _Functor,
531  typename = _Requires<__not_<is_same<_Functor, function>>, void>,
532  typename = _Requires<_Callable<_Functor>, void>>
533  function(_Functor);
534 
535  /**
536  * @brief %Function assignment operator.
537  * @param __x A %function with identical call signature.
538  * @post @c (bool)*this == (bool)x
539  * @returns @c *this
540  *
541  * The target of @a __x is copied to @c *this. If @a __x has no
542  * target, then @c *this will be empty.
543  *
544  * If @a __x targets a function pointer or a reference to a function
545  * object, then this operation will not throw an %exception.
546  */
547  function&
548  operator=(const function& __x)
549  {
550  function(__x).swap(*this);
551  return *this;
552  }
553 
554  /**
555  * @brief %Function move-assignment operator.
556  * @param __x A %function rvalue with identical call signature.
557  * @returns @c *this
558  *
559  * The target of @a __x is moved to @c *this. If @a __x has no
560  * target, then @c *this will be empty.
561  *
562  * If @a __x targets a function pointer or a reference to a function
563  * object, then this operation will not throw an %exception.
564  */
565  function&
566  operator=(function&& __x)
567  {
568  function(std::move(__x)).swap(*this);
569  return *this;
570  }
571 
572  /**
573  * @brief %Function assignment to zero.
574  * @post @c !(bool)*this
575  * @returns @c *this
576  *
577  * The target of @c *this is deallocated, leaving it empty.
578  */
579  function&
580  operator=(nullptr_t) noexcept
581  {
582  if (_M_manager)
583  {
584  _M_manager(_M_functor, _M_functor, __destroy_functor);
585  _M_manager = nullptr;
586  _M_invoker = nullptr;
587  }
588  return *this;
589  }
590 
591  /**
592  * @brief %Function assignment to a new target.
593  * @param __f A %function object that is callable with parameters of
594  * type @c T1, @c T2, ..., @c TN and returns a value convertible
595  * to @c Res.
596  * @return @c *this
597  *
598  * This %function object wrapper will target a copy of @a
599  * __f. If @a __f is @c reference_wrapper<F>, then this function
600  * object will contain a reference to the function object @c
601  * __f.get(). If @a __f is a NULL function pointer or NULL
602  * pointer-to-member, @c this object will be empty.
603  *
604  * If @a __f is a non-NULL function pointer or an object of type @c
605  * reference_wrapper<F>, this function will not throw.
606  */
607  template<typename _Functor>
608  _Requires<_Callable<typename decay<_Functor>::type>, function&>
609  operator=(_Functor&& __f)
610  {
611  function(std::forward<_Functor>(__f)).swap(*this);
612  return *this;
613  }
614 
615  /// @overload
616  template<typename _Functor>
617  function&
619  {
620  function(__f).swap(*this);
621  return *this;
622  }
623 
624  // [3.7.2.2] function modifiers
625 
626  /**
627  * @brief Swap the targets of two %function objects.
628  * @param __x A %function with identical call signature.
629  *
630  * Swap the targets of @c this function object and @a __f. This
631  * function will not throw an %exception.
632  */
633  void swap(function& __x) noexcept
634  {
635  std::swap(_M_functor, __x._M_functor);
636  std::swap(_M_manager, __x._M_manager);
637  std::swap(_M_invoker, __x._M_invoker);
638  }
639 
640  // [3.7.2.3] function capacity
641 
642  /**
643  * @brief Determine if the %function wrapper has a target.
644  *
645  * @return @c true when this %function object contains a target,
646  * or @c false when it is empty.
647  *
648  * This function will not throw an %exception.
649  */
650  explicit operator bool() const noexcept
651  { return !_M_empty(); }
652 
653  // [3.7.2.4] function invocation
654 
655  /**
656  * @brief Invokes the function targeted by @c *this.
657  * @returns the result of the target.
658  * @throws bad_function_call when @c !(bool)*this
659  *
660  * The function call operator invokes the target function object
661  * stored by @c this.
662  */
663  _Res operator()(_ArgTypes... __args) const;
664 
665 #if __cpp_rtti
666  // [3.7.2.5] function target access
667  /**
668  * @brief Determine the type of the target of this function object
669  * wrapper.
670  *
671  * @returns the type identifier of the target function object, or
672  * @c typeid(void) if @c !(bool)*this.
673  *
674  * This function will not throw an %exception.
675  */
676  const type_info& target_type() const noexcept;
677 
678  /**
679  * @brief Access the stored target function object.
680  *
681  * @return Returns a pointer to the stored target function object,
682  * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
683  * pointer.
684  *
685  * This function will not throw an %exception.
686  */
687  template<typename _Functor> _Functor* target() noexcept;
688 
689  /// @overload
690  template<typename _Functor> const _Functor* target() const noexcept;
691 #endif
692 
693  private:
694  using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
695  _Invoker_type _M_invoker;
696  };
697 
698  // Out-of-line member definitions.
699  template<typename _Res, typename... _ArgTypes>
700  function<_Res(_ArgTypes...)>::
701  function(const function& __x)
702  : _Function_base()
703  {
704  if (static_cast<bool>(__x))
705  {
706  __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
707  _M_invoker = __x._M_invoker;
708  _M_manager = __x._M_manager;
709  }
710  }
711 
712  template<typename _Res, typename... _ArgTypes>
713  template<typename _Functor, typename, typename>
714  function<_Res(_ArgTypes...)>::
715  function(_Functor __f)
716  : _Function_base()
717  {
718  typedef _Function_handler<_Signature_type, _Functor> _My_handler;
719 
720  if (_My_handler::_M_not_empty_function(__f))
721  {
722  _My_handler::_M_init_functor(_M_functor, std::move(__f));
723  _M_invoker = &_My_handler::_M_invoke;
724  _M_manager = &_My_handler::_M_manager;
725  }
726  }
727 
728  template<typename _Res, typename... _ArgTypes>
729  _Res
730  function<_Res(_ArgTypes...)>::
731  operator()(_ArgTypes... __args) const
732  {
733  if (_M_empty())
734  __throw_bad_function_call();
735  return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
736  }
737 
738 #if __cpp_rtti
739  template<typename _Res, typename... _ArgTypes>
740  const type_info&
741  function<_Res(_ArgTypes...)>::
742  target_type() const noexcept
743  {
744  if (_M_manager)
745  {
746  _Any_data __typeinfo_result;
747  _M_manager(__typeinfo_result, _M_functor, __get_type_info);
748  return *__typeinfo_result._M_access<const type_info*>();
749  }
750  else
751  return typeid(void);
752  }
753 
754  template<typename _Res, typename... _ArgTypes>
755  template<typename _Functor>
756  _Functor*
757  function<_Res(_ArgTypes...)>::
758  target() noexcept
759  {
760  if (typeid(_Functor) == target_type() && _M_manager)
761  {
762  _Any_data __ptr;
763  if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
765  return 0;
766  else
767  return __ptr._M_access<_Functor*>();
768  }
769  else
770  return 0;
771  }
772 
773  template<typename _Res, typename... _ArgTypes>
774  template<typename _Functor>
775  const _Functor*
776  function<_Res(_ArgTypes...)>::
777  target() const noexcept
778  {
779  if (typeid(_Functor) == target_type() && _M_manager)
780  {
781  _Any_data __ptr;
782  _M_manager(__ptr, _M_functor, __get_functor_ptr);
783  return __ptr._M_access<const _Functor*>();
784  }
785  else
786  return 0;
787  }
788 #endif
789 
790  // [20.7.15.2.6] null pointer comparisons
791 
792  /**
793  * @brief Compares a polymorphic function object wrapper against 0
794  * (the NULL pointer).
795  * @returns @c true if the wrapper has no target, @c false otherwise
796  *
797  * This function will not throw an %exception.
798  */
799  template<typename _Res, typename... _Args>
800  inline bool
801  operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
802  { return !static_cast<bool>(__f); }
803 
804  /// @overload
805  template<typename _Res, typename... _Args>
806  inline bool
807  operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
808  { return !static_cast<bool>(__f); }
809 
810  /**
811  * @brief Compares a polymorphic function object wrapper against 0
812  * (the NULL pointer).
813  * @returns @c false if the wrapper has no target, @c true otherwise
814  *
815  * This function will not throw an %exception.
816  */
817  template<typename _Res, typename... _Args>
818  inline bool
819  operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
820  { return static_cast<bool>(__f); }
821 
822  /// @overload
823  template<typename _Res, typename... _Args>
824  inline bool
825  operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
826  { return static_cast<bool>(__f); }
827 
828 
829  // [20.7.15.2.7] specialized algorithms
830 
831  /**
832  * @brief Swap the targets of two polymorphic function object wrappers.
833  *
834  * This function will not throw an %exception.
835  */
836  // _GLIBCXX_RESOLVE_LIB_DEFECTS
837  // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
838  template<typename _Res, typename... _Args>
839  inline void
840  swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
841  { __x.swap(__y); }
842 
843 _GLIBCXX_END_NAMESPACE_VERSION
844 } // namespace std
845 
846 #endif // C++11
847 
848 #endif // _GLIBCXX_STD_FUNCTION_H
is_const
Definition: type_traits:658
_Requires< _Callable< typename decay< _Functor >::type >, function & > operator=(_Functor &&__f)
Function assignment to a new target.
Definition: std_function.h:609
function & operator=(nullptr_t) noexcept
Function assignment to zero.
Definition: std_function.h:580
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:47
ISO C++ entities toplevel namespace is std.
void swap(function &__x) noexcept
Swap the targets of two function objects.
Definition: std_function.h:633
function & operator=(const function &__x)
Function assignment operator.
Definition: std_function.h:548
function & operator=(reference_wrapper< _Functor > __f) noexcept
Definition: std_function.h:618
Exception class thrown when class template function&#39;s operator() is called with an empty target...
Definition: std_function.h:75
function & operator=(function &&__x)
Function move-assignment operator.
Definition: std_function.h:566
Primary class template for reference_wrapper.
Definition: refwrap.h:311
Base class for all library exceptions.
Definition: exception.h:60
integral_constant
Definition: type_traits:69
Base class of all polymorphic function object wrappers.
Definition: std_function.h:150
Part of RTTI.
Definition: typeinfo:88
constexpr result_of< _Callable &&(_Args &&...)>::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_callable< _Callable &&(_Args &&...)>::value)
Invoke a callable object.
Definition: invoke.h:89