libstdc++
std_function.h
Go to the documentation of this file.
00001 // Implementation of std::function -*- C++ -*-
00002 
00003 // Copyright (C) 2004-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/bits/std_function.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{functional}
00028  */
00029 
00030 #ifndef _GLIBCXX_STD_FUNCTION_H
00031 #define _GLIBCXX_STD_FUNCTION_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #if __cplusplus < 201103L
00036 # include <bits/c++0x_warning.h>
00037 #else
00038 
00039 #if __cpp_rtti
00040 # include <typeinfo>
00041 #endif
00042 #include <bits/stl_function.h>
00043 #include <bits/invoke.h>
00044 #include <bits/refwrap.h>
00045 #include <bits/functexcept.h>
00046 
00047 namespace std _GLIBCXX_VISIBILITY(default)
00048 {
00049 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00050 
00051   /**
00052    *  @brief Exception class thrown when class template function's
00053    *  operator() is called with an empty target.
00054    *  @ingroup exceptions
00055    */
00056   class bad_function_call : public std::exception
00057   {
00058   public:
00059     virtual ~bad_function_call() noexcept;
00060 
00061     const char* what() const noexcept;
00062   };
00063 
00064   /**
00065    *  Trait identifying "location-invariant" types, meaning that the
00066    *  address of the object (or any of its members) will not escape.
00067    *  Trivially copyable types are location-invariant and users can
00068    *  specialize this trait for other types.
00069    */
00070   template<typename _Tp>
00071     struct __is_location_invariant
00072     : is_trivially_copyable<_Tp>::type
00073     { };
00074 
00075   class _Undefined_class;
00076 
00077   union _Nocopy_types
00078   {
00079     void*       _M_object;
00080     const void* _M_const_object;
00081     void (*_M_function_pointer)();
00082     void (_Undefined_class::*_M_member_pointer)();
00083   };
00084 
00085   union [[gnu::may_alias]] _Any_data
00086   {
00087     void*       _M_access()       { return &_M_pod_data[0]; }
00088     const void* _M_access() const { return &_M_pod_data[0]; }
00089 
00090     template<typename _Tp>
00091       _Tp&
00092       _M_access()
00093       { return *static_cast<_Tp*>(_M_access()); }
00094 
00095     template<typename _Tp>
00096       const _Tp&
00097       _M_access() const
00098       { return *static_cast<const _Tp*>(_M_access()); }
00099 
00100     _Nocopy_types _M_unused;
00101     char _M_pod_data[sizeof(_Nocopy_types)];
00102   };
00103 
00104   enum _Manager_operation
00105   {
00106     __get_type_info,
00107     __get_functor_ptr,
00108     __clone_functor,
00109     __destroy_functor
00110   };
00111 
00112   // Simple type wrapper that helps avoid annoying const problems
00113   // when casting between void pointers and pointers-to-pointers.
00114   template<typename _Tp>
00115     struct _Simple_type_wrapper
00116     {
00117       _Simple_type_wrapper(_Tp __value) : __value(__value) { }
00118 
00119       _Tp __value;
00120     };
00121 
00122   template<typename _Tp>
00123     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
00124     : __is_location_invariant<_Tp>
00125     { };
00126 
00127   template<typename _Signature>
00128     class function;
00129 
00130   /// Base class of all polymorphic function object wrappers.
00131   class _Function_base
00132   {
00133   public:
00134     static const size_t _M_max_size = sizeof(_Nocopy_types);
00135     static const size_t _M_max_align = __alignof__(_Nocopy_types);
00136 
00137     template<typename _Functor>
00138       class _Base_manager
00139       {
00140       protected:
00141         static const bool __stored_locally =
00142         (__is_location_invariant<_Functor>::value
00143          && sizeof(_Functor) <= _M_max_size
00144          && __alignof__(_Functor) <= _M_max_align
00145          && (_M_max_align % __alignof__(_Functor) == 0));
00146 
00147         typedef integral_constant<bool, __stored_locally> _Local_storage;
00148 
00149         // Retrieve a pointer to the function object
00150         static _Functor*
00151         _M_get_pointer(const _Any_data& __source)
00152         {
00153           if _GLIBCXX17_CONSTEXPR (__stored_locally)
00154             {
00155               const _Functor& __f = __source._M_access<_Functor>();
00156               return const_cast<_Functor*>(std::__addressof(__f));
00157             }
00158           else // have stored a pointer
00159             return __source._M_access<_Functor*>();
00160         }
00161 
00162         // Clone a location-invariant function object that fits within
00163         // an _Any_data structure.
00164         static void
00165         _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
00166         {
00167           ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
00168         }
00169 
00170         // Clone a function object that is not location-invariant or
00171         // that cannot fit into an _Any_data structure.
00172         static void
00173         _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
00174         {
00175           __dest._M_access<_Functor*>() =
00176             new _Functor(*__source._M_access<const _Functor*>());
00177         }
00178 
00179         // Destroying a location-invariant object may still require
00180         // destruction.
00181         static void
00182         _M_destroy(_Any_data& __victim, true_type)
00183         {
00184           __victim._M_access<_Functor>().~_Functor();
00185         }
00186 
00187         // Destroying an object located on the heap.
00188         static void
00189         _M_destroy(_Any_data& __victim, false_type)
00190         {
00191           delete __victim._M_access<_Functor*>();
00192         }
00193 
00194       public:
00195         static bool
00196         _M_manager(_Any_data& __dest, const _Any_data& __source,
00197                    _Manager_operation __op)
00198         {
00199           switch (__op)
00200             {
00201 #if __cpp_rtti
00202             case __get_type_info:
00203               __dest._M_access<const type_info*>() = &typeid(_Functor);
00204               break;
00205 #endif
00206             case __get_functor_ptr:
00207               __dest._M_access<_Functor*>() = _M_get_pointer(__source);
00208               break;
00209 
00210             case __clone_functor:
00211               _M_clone(__dest, __source, _Local_storage());
00212               break;
00213 
00214             case __destroy_functor:
00215               _M_destroy(__dest, _Local_storage());
00216               break;
00217             }
00218           return false;
00219         }
00220 
00221         static void
00222         _M_init_functor(_Any_data& __functor, _Functor&& __f)
00223         { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
00224 
00225         template<typename _Signature>
00226           static bool
00227           _M_not_empty_function(const function<_Signature>& __f)
00228           { return static_cast<bool>(__f); }
00229 
00230         template<typename _Tp>
00231           static bool
00232           _M_not_empty_function(_Tp* __fp)
00233           { return __fp != nullptr; }
00234 
00235         template<typename _Class, typename _Tp>
00236           static bool
00237           _M_not_empty_function(_Tp _Class::* __mp)
00238           { return __mp != nullptr; }
00239 
00240         template<typename _Tp>
00241           static bool
00242           _M_not_empty_function(const _Tp&)
00243           { return true; }
00244 
00245       private:
00246         static void
00247         _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
00248         { ::new (__functor._M_access()) _Functor(std::move(__f)); }
00249 
00250         static void
00251         _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
00252         { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
00253       };
00254 
00255     _Function_base() : _M_manager(nullptr) { }
00256 
00257     ~_Function_base()
00258     {
00259       if (_M_manager)
00260         _M_manager(_M_functor, _M_functor, __destroy_functor);
00261     }
00262 
00263     bool _M_empty() const { return !_M_manager; }
00264 
00265     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
00266                                   _Manager_operation);
00267 
00268     _Any_data     _M_functor;
00269     _Manager_type _M_manager;
00270   };
00271 
00272   template<typename _Signature, typename _Functor>
00273     class _Function_handler;
00274 
00275   template<typename _Res, typename _Functor, typename... _ArgTypes>
00276     class _Function_handler<_Res(_ArgTypes...), _Functor>
00277     : public _Function_base::_Base_manager<_Functor>
00278     {
00279       typedef _Function_base::_Base_manager<_Functor> _Base;
00280 
00281     public:
00282       static _Res
00283       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
00284       {
00285         return (*_Base::_M_get_pointer(__functor))(
00286             std::forward<_ArgTypes>(__args)...);
00287       }
00288     };
00289 
00290   template<typename _Functor, typename... _ArgTypes>
00291     class _Function_handler<void(_ArgTypes...), _Functor>
00292     : public _Function_base::_Base_manager<_Functor>
00293     {
00294       typedef _Function_base::_Base_manager<_Functor> _Base;
00295 
00296      public:
00297       static void
00298       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
00299       {
00300         (*_Base::_M_get_pointer(__functor))(
00301             std::forward<_ArgTypes>(__args)...);
00302       }
00303     };
00304 
00305   template<typename _Class, typename _Member, typename _Res,
00306            typename... _ArgTypes>
00307     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
00308     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
00309     {
00310       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
00311         _Base;
00312 
00313      public:
00314       static _Res
00315       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
00316       {
00317         return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
00318                              std::forward<_ArgTypes>(__args)...);
00319       }
00320     };
00321 
00322   template<typename _Class, typename _Member, typename... _ArgTypes>
00323     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
00324     : public _Function_base::_Base_manager<
00325                  _Simple_type_wrapper< _Member _Class::* > >
00326     {
00327       typedef _Member _Class::* _Functor;
00328       typedef _Simple_type_wrapper<_Functor> _Wrapper;
00329       typedef _Function_base::_Base_manager<_Wrapper> _Base;
00330 
00331     public:
00332       static bool
00333       _M_manager(_Any_data& __dest, const _Any_data& __source,
00334                  _Manager_operation __op)
00335       {
00336         switch (__op)
00337           {
00338 #if __cpp_rtti
00339           case __get_type_info:
00340             __dest._M_access<const type_info*>() = &typeid(_Functor);
00341             break;
00342 #endif
00343           case __get_functor_ptr:
00344             __dest._M_access<_Functor*>() =
00345               &_Base::_M_get_pointer(__source)->__value;
00346             break;
00347 
00348           default:
00349             _Base::_M_manager(__dest, __source, __op);
00350           }
00351         return false;
00352       }
00353 
00354       static void
00355       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
00356       {
00357         std::__invoke(_Base::_M_get_pointer(__functor)->__value,
00358                       std::forward<_ArgTypes>(__args)...);
00359       }
00360     };
00361 
00362   /**
00363    *  @brief Primary class template for std::function.
00364    *  @ingroup functors
00365    *
00366    *  Polymorphic function wrapper.
00367    */
00368   template<typename _Res, typename... _ArgTypes>
00369     class function<_Res(_ArgTypes...)>
00370     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
00371       private _Function_base
00372     {
00373       template<typename _Func,
00374                typename _Res2 = __invoke_result<_Func&, _ArgTypes...>>
00375         struct _Callable
00376         : __is_invocable_impl<_Res2, _Res>::type
00377         { };
00378 
00379       // Used so the return type convertibility checks aren't done when
00380       // performing overload resolution for copy construction/assignment.
00381       template<typename _Tp>
00382         struct _Callable<function, _Tp> : false_type { };
00383 
00384       template<typename _Cond, typename _Tp>
00385         using _Requires = typename enable_if<_Cond::value, _Tp>::type;
00386 
00387     public:
00388       typedef _Res result_type;
00389 
00390       // [3.7.2.1] construct/copy/destroy
00391 
00392       /**
00393        *  @brief Default construct creates an empty function call wrapper.
00394        *  @post @c !(bool)*this
00395        */
00396       function() noexcept
00397       : _Function_base() { }
00398 
00399       /**
00400        *  @brief Creates an empty function call wrapper.
00401        *  @post @c !(bool)*this
00402        */
00403       function(nullptr_t) noexcept
00404       : _Function_base() { }
00405 
00406       /**
00407        *  @brief %Function copy constructor.
00408        *  @param __x A %function object with identical call signature.
00409        *  @post @c bool(*this) == bool(__x)
00410        *
00411        *  The newly-created %function contains a copy of the target of @a
00412        *  __x (if it has one).
00413        */
00414       function(const function& __x);
00415 
00416       /**
00417        *  @brief %Function move constructor.
00418        *  @param __x A %function object rvalue with identical call signature.
00419        *
00420        *  The newly-created %function contains the target of @a __x
00421        *  (if it has one).
00422        */
00423       function(function&& __x) noexcept : _Function_base()
00424       {
00425         __x.swap(*this);
00426       }
00427 
00428       /**
00429        *  @brief Builds a %function that targets a copy of the incoming
00430        *  function object.
00431        *  @param __f A %function object that is callable with parameters of
00432        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
00433        *  to @c Res.
00434        *
00435        *  The newly-created %function object will target a copy of
00436        *  @a __f. If @a __f is @c reference_wrapper<F>, then this function
00437        *  object will contain a reference to the function object @c
00438        *  __f.get(). If @a __f is a NULL function pointer or NULL
00439        *  pointer-to-member, the newly-created object will be empty.
00440        *
00441        *  If @a __f is a non-NULL function pointer or an object of type @c
00442        *  reference_wrapper<F>, this function will not throw.
00443        */
00444       template<typename _Functor,
00445                typename = _Requires<__not_<is_same<_Functor, function>>, void>,
00446                typename = _Requires<_Callable<_Functor>, void>>
00447         function(_Functor);
00448 
00449       /**
00450        *  @brief %Function assignment operator.
00451        *  @param __x A %function with identical call signature.
00452        *  @post @c (bool)*this == (bool)x
00453        *  @returns @c *this
00454        *
00455        *  The target of @a __x is copied to @c *this. If @a __x has no
00456        *  target, then @c *this will be empty.
00457        *
00458        *  If @a __x targets a function pointer or a reference to a function
00459        *  object, then this operation will not throw an %exception.
00460        */
00461       function&
00462       operator=(const function& __x)
00463       {
00464         function(__x).swap(*this);
00465         return *this;
00466       }
00467 
00468       /**
00469        *  @brief %Function move-assignment operator.
00470        *  @param __x A %function rvalue with identical call signature.
00471        *  @returns @c *this
00472        *
00473        *  The target of @a __x is moved to @c *this. If @a __x has no
00474        *  target, then @c *this will be empty.
00475        *
00476        *  If @a __x targets a function pointer or a reference to a function
00477        *  object, then this operation will not throw an %exception.
00478        */
00479       function&
00480       operator=(function&& __x) noexcept
00481       {
00482         function(std::move(__x)).swap(*this);
00483         return *this;
00484       }
00485 
00486       /**
00487        *  @brief %Function assignment to zero.
00488        *  @post @c !(bool)*this
00489        *  @returns @c *this
00490        *
00491        *  The target of @c *this is deallocated, leaving it empty.
00492        */
00493       function&
00494       operator=(nullptr_t) noexcept
00495       {
00496         if (_M_manager)
00497           {
00498             _M_manager(_M_functor, _M_functor, __destroy_functor);
00499             _M_manager = nullptr;
00500             _M_invoker = nullptr;
00501           }
00502         return *this;
00503       }
00504 
00505       /**
00506        *  @brief %Function assignment to a new target.
00507        *  @param __f A %function object that is callable with parameters of
00508        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
00509        *  to @c Res.
00510        *  @return @c *this
00511        *
00512        *  This  %function object wrapper will target a copy of @a
00513        *  __f. If @a __f is @c reference_wrapper<F>, then this function
00514        *  object will contain a reference to the function object @c
00515        *  __f.get(). If @a __f is a NULL function pointer or NULL
00516        *  pointer-to-member, @c this object will be empty.
00517        *
00518        *  If @a __f is a non-NULL function pointer or an object of type @c
00519        *  reference_wrapper<F>, this function will not throw.
00520        */
00521       template<typename _Functor>
00522         _Requires<_Callable<typename decay<_Functor>::type>, function&>
00523         operator=(_Functor&& __f)
00524         {
00525           function(std::forward<_Functor>(__f)).swap(*this);
00526           return *this;
00527         }
00528 
00529       /// @overload
00530       template<typename _Functor>
00531         function&
00532         operator=(reference_wrapper<_Functor> __f) noexcept
00533         {
00534           function(__f).swap(*this);
00535           return *this;
00536         }
00537 
00538       // [3.7.2.2] function modifiers
00539 
00540       /**
00541        *  @brief Swap the targets of two %function objects.
00542        *  @param __x A %function with identical call signature.
00543        *
00544        *  Swap the targets of @c this function object and @a __f. This
00545        *  function will not throw an %exception.
00546        */
00547       void swap(function& __x) noexcept
00548       {
00549         std::swap(_M_functor, __x._M_functor);
00550         std::swap(_M_manager, __x._M_manager);
00551         std::swap(_M_invoker, __x._M_invoker);
00552       }
00553 
00554       // [3.7.2.3] function capacity
00555 
00556       /**
00557        *  @brief Determine if the %function wrapper has a target.
00558        *
00559        *  @return @c true when this %function object contains a target,
00560        *  or @c false when it is empty.
00561        *
00562        *  This function will not throw an %exception.
00563        */
00564       explicit operator bool() const noexcept
00565       { return !_M_empty(); }
00566 
00567       // [3.7.2.4] function invocation
00568 
00569       /**
00570        *  @brief Invokes the function targeted by @c *this.
00571        *  @returns the result of the target.
00572        *  @throws bad_function_call when @c !(bool)*this
00573        *
00574        *  The function call operator invokes the target function object
00575        *  stored by @c this.
00576        */
00577       _Res operator()(_ArgTypes... __args) const;
00578 
00579 #if __cpp_rtti
00580       // [3.7.2.5] function target access
00581       /**
00582        *  @brief Determine the type of the target of this function object
00583        *  wrapper.
00584        *
00585        *  @returns the type identifier of the target function object, or
00586        *  @c typeid(void) if @c !(bool)*this.
00587        *
00588        *  This function will not throw an %exception.
00589        */
00590       const type_info& target_type() const noexcept;
00591 
00592       /**
00593        *  @brief Access the stored target function object.
00594        *
00595        *  @return Returns a pointer to the stored target function object,
00596        *  if @c typeid(_Functor).equals(target_type()); otherwise, a NULL
00597        *  pointer.
00598        *
00599        * This function does not throw exceptions.
00600        *
00601        * @{
00602        */
00603       template<typename _Functor>       _Functor* target() noexcept;
00604 
00605       template<typename _Functor> const _Functor* target() const noexcept;
00606       // @}
00607 #endif
00608 
00609     private:
00610       using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
00611       _Invoker_type _M_invoker;
00612   };
00613 
00614 #if __cpp_deduction_guides >= 201606
00615   template<typename>
00616     struct __function_guide_helper
00617     { };
00618 
00619   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00620     struct __function_guide_helper<
00621       _Res (_Tp::*) (_Args...) noexcept(_Nx)
00622     >
00623     { using type = _Res(_Args...); };
00624 
00625   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00626     struct __function_guide_helper<
00627       _Res (_Tp::*) (_Args...) & noexcept(_Nx)
00628     >
00629     { using type = _Res(_Args...); };
00630 
00631   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00632     struct __function_guide_helper<
00633       _Res (_Tp::*) (_Args...) const noexcept(_Nx)
00634     >
00635     { using type = _Res(_Args...); };
00636 
00637   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00638     struct __function_guide_helper<
00639       _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
00640     >
00641     { using type = _Res(_Args...); };
00642 
00643   template<typename _Res, typename... _ArgTypes>
00644     function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
00645 
00646   template<typename _Functor, typename _Signature = typename
00647            __function_guide_helper<decltype(&_Functor::operator())>::type>
00648     function(_Functor) -> function<_Signature>;
00649 #endif
00650 
00651   // Out-of-line member definitions.
00652   template<typename _Res, typename... _ArgTypes>
00653     function<_Res(_ArgTypes...)>::
00654     function(const function& __x)
00655     : _Function_base()
00656     {
00657       if (static_cast<bool>(__x))
00658         {
00659           __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
00660           _M_invoker = __x._M_invoker;
00661           _M_manager = __x._M_manager;
00662         }
00663     }
00664 
00665   template<typename _Res, typename... _ArgTypes>
00666     template<typename _Functor, typename, typename>
00667       function<_Res(_ArgTypes...)>::
00668       function(_Functor __f)
00669       : _Function_base()
00670       {
00671         typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
00672 
00673         if (_My_handler::_M_not_empty_function(__f))
00674           {
00675             _My_handler::_M_init_functor(_M_functor, std::move(__f));
00676             _M_invoker = &_My_handler::_M_invoke;
00677             _M_manager = &_My_handler::_M_manager;
00678           }
00679       }
00680 
00681   template<typename _Res, typename... _ArgTypes>
00682     _Res
00683     function<_Res(_ArgTypes...)>::
00684     operator()(_ArgTypes... __args) const
00685     {
00686       if (_M_empty())
00687         __throw_bad_function_call();
00688       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
00689     }
00690 
00691 #if __cpp_rtti
00692   template<typename _Res, typename... _ArgTypes>
00693     const type_info&
00694     function<_Res(_ArgTypes...)>::
00695     target_type() const noexcept
00696     {
00697       if (_M_manager)
00698         {
00699           _Any_data __typeinfo_result;
00700           _M_manager(__typeinfo_result, _M_functor, __get_type_info);
00701           return *__typeinfo_result._M_access<const type_info*>();
00702         }
00703       else
00704         return typeid(void);
00705     }
00706 
00707   template<typename _Res, typename... _ArgTypes>
00708     template<typename _Functor>
00709       _Functor*
00710       function<_Res(_ArgTypes...)>::
00711       target() noexcept
00712       {
00713         const function* __const_this = this;
00714         const _Functor* __func = __const_this->template target<_Functor>();
00715         return const_cast<_Functor*>(__func);
00716       }
00717 
00718   template<typename _Res, typename... _ArgTypes>
00719     template<typename _Functor>
00720       const _Functor*
00721       function<_Res(_ArgTypes...)>::
00722       target() const noexcept
00723       {
00724         if (typeid(_Functor) == target_type() && _M_manager)
00725           {
00726             _Any_data __ptr;
00727             _M_manager(__ptr, _M_functor, __get_functor_ptr);
00728             return __ptr._M_access<const _Functor*>();
00729           }
00730         else
00731           return nullptr;
00732       }
00733 #endif
00734 
00735   // [20.7.15.2.6] null pointer comparisons
00736 
00737   /**
00738    *  @brief Compares a polymorphic function object wrapper against 0
00739    *  (the NULL pointer).
00740    *  @returns @c true if the wrapper has no target, @c false otherwise
00741    *
00742    *  This function will not throw an %exception.
00743    */
00744   template<typename _Res, typename... _Args>
00745     inline bool
00746     operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
00747     { return !static_cast<bool>(__f); }
00748 
00749   /// @overload
00750   template<typename _Res, typename... _Args>
00751     inline bool
00752     operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
00753     { return !static_cast<bool>(__f); }
00754 
00755   /**
00756    *  @brief Compares a polymorphic function object wrapper against 0
00757    *  (the NULL pointer).
00758    *  @returns @c false if the wrapper has no target, @c true otherwise
00759    *
00760    *  This function will not throw an %exception.
00761    */
00762   template<typename _Res, typename... _Args>
00763     inline bool
00764     operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
00765     { return static_cast<bool>(__f); }
00766 
00767   /// @overload
00768   template<typename _Res, typename... _Args>
00769     inline bool
00770     operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
00771     { return static_cast<bool>(__f); }
00772 
00773 
00774   // [20.7.15.2.7] specialized algorithms
00775 
00776   /**
00777    *  @brief Swap the targets of two polymorphic function object wrappers.
00778    *
00779    *  This function will not throw an %exception.
00780    */
00781   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00782   // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
00783   template<typename _Res, typename... _Args>
00784     inline void
00785     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
00786     { __x.swap(__y); }
00787 
00788 #if __cplusplus >= 201703L
00789   namespace __detail::__variant
00790   {
00791     template<typename> struct _Never_valueless_alt; // see <variant>
00792 
00793     // Provide the strong exception-safety guarantee when emplacing a
00794     // function into a variant.
00795     template<typename _Signature>
00796       struct _Never_valueless_alt<std::function<_Signature>>
00797       : std::true_type
00798       { };
00799   }  // namespace __detail::__variant
00800 #endif // C++17
00801 
00802 _GLIBCXX_END_NAMESPACE_VERSION
00803 } // namespace std
00804 
00805 #endif // C++11
00806 #endif // _GLIBCXX_STD_FUNCTION_H