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   template<typename _From, typename _To>
00363     using __check_func_return_type
00364       = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
00365 
00366   /**
00367    *  @brief Primary class template for std::function.
00368    *  @ingroup functors
00369    *
00370    *  Polymorphic function wrapper.
00371    */
00372   template<typename _Res, typename... _ArgTypes>
00373     class function<_Res(_ArgTypes...)>
00374     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
00375       private _Function_base
00376     {
00377       template<typename _Func,
00378                typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
00379         struct _Callable : __check_func_return_type<_Res2, _Res> { };
00380 
00381       // Used so the return type convertibility checks aren't done when
00382       // performing overload resolution for copy construction/assignment.
00383       template<typename _Tp>
00384         struct _Callable<function, _Tp> : false_type { };
00385 
00386       template<typename _Cond, typename _Tp>
00387         using _Requires = typename enable_if<_Cond::value, _Tp>::type;
00388 
00389     public:
00390       typedef _Res result_type;
00391 
00392       // [3.7.2.1] construct/copy/destroy
00393 
00394       /**
00395        *  @brief Default construct creates an empty function call wrapper.
00396        *  @post @c !(bool)*this
00397        */
00398       function() noexcept
00399       : _Function_base() { }
00400 
00401       /**
00402        *  @brief Creates an empty function call wrapper.
00403        *  @post @c !(bool)*this
00404        */
00405       function(nullptr_t) noexcept
00406       : _Function_base() { }
00407 
00408       /**
00409        *  @brief %Function copy constructor.
00410        *  @param __x A %function object with identical call signature.
00411        *  @post @c bool(*this) == bool(__x)
00412        *
00413        *  The newly-created %function contains a copy of the target of @a
00414        *  __x (if it has one).
00415        */
00416       function(const function& __x);
00417 
00418       /**
00419        *  @brief %Function move constructor.
00420        *  @param __x A %function object rvalue with identical call signature.
00421        *
00422        *  The newly-created %function contains the target of @a __x
00423        *  (if it has one).
00424        */
00425       function(function&& __x) noexcept : _Function_base()
00426       {
00427         __x.swap(*this);
00428       }
00429 
00430       /**
00431        *  @brief Builds a %function that targets a copy of the incoming
00432        *  function object.
00433        *  @param __f A %function object that is callable with parameters of
00434        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
00435        *  to @c Res.
00436        *
00437        *  The newly-created %function object will target a copy of
00438        *  @a __f. If @a __f is @c reference_wrapper<F>, then this function
00439        *  object will contain a reference to the function object @c
00440        *  __f.get(). If @a __f is a NULL function pointer or NULL
00441        *  pointer-to-member, the newly-created object will be empty.
00442        *
00443        *  If @a __f is a non-NULL function pointer or an object of type @c
00444        *  reference_wrapper<F>, this function will not throw.
00445        */
00446       template<typename _Functor,
00447                typename = _Requires<__not_<is_same<_Functor, function>>, void>,
00448                typename = _Requires<_Callable<_Functor>, void>>
00449         function(_Functor);
00450 
00451       /**
00452        *  @brief %Function assignment operator.
00453        *  @param __x A %function with identical call signature.
00454        *  @post @c (bool)*this == (bool)x
00455        *  @returns @c *this
00456        *
00457        *  The target of @a __x is copied to @c *this. If @a __x has no
00458        *  target, then @c *this will be empty.
00459        *
00460        *  If @a __x targets a function pointer or a reference to a function
00461        *  object, then this operation will not throw an %exception.
00462        */
00463       function&
00464       operator=(const function& __x)
00465       {
00466         function(__x).swap(*this);
00467         return *this;
00468       }
00469 
00470       /**
00471        *  @brief %Function move-assignment operator.
00472        *  @param __x A %function rvalue with identical call signature.
00473        *  @returns @c *this
00474        *
00475        *  The target of @a __x is moved to @c *this. If @a __x has no
00476        *  target, then @c *this will be empty.
00477        *
00478        *  If @a __x targets a function pointer or a reference to a function
00479        *  object, then this operation will not throw an %exception.
00480        */
00481       function&
00482       operator=(function&& __x) noexcept
00483       {
00484         function(std::move(__x)).swap(*this);
00485         return *this;
00486       }
00487 
00488       /**
00489        *  @brief %Function assignment to zero.
00490        *  @post @c !(bool)*this
00491        *  @returns @c *this
00492        *
00493        *  The target of @c *this is deallocated, leaving it empty.
00494        */
00495       function&
00496       operator=(nullptr_t) noexcept
00497       {
00498         if (_M_manager)
00499           {
00500             _M_manager(_M_functor, _M_functor, __destroy_functor);
00501             _M_manager = nullptr;
00502             _M_invoker = nullptr;
00503           }
00504         return *this;
00505       }
00506 
00507       /**
00508        *  @brief %Function assignment to a new target.
00509        *  @param __f A %function object that is callable with parameters of
00510        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
00511        *  to @c Res.
00512        *  @return @c *this
00513        *
00514        *  This  %function object wrapper will target a copy of @a
00515        *  __f. If @a __f is @c reference_wrapper<F>, then this function
00516        *  object will contain a reference to the function object @c
00517        *  __f.get(). If @a __f is a NULL function pointer or NULL
00518        *  pointer-to-member, @c this object will be empty.
00519        *
00520        *  If @a __f is a non-NULL function pointer or an object of type @c
00521        *  reference_wrapper<F>, this function will not throw.
00522        */
00523       template<typename _Functor>
00524         _Requires<_Callable<typename decay<_Functor>::type>, function&>
00525         operator=(_Functor&& __f)
00526         {
00527           function(std::forward<_Functor>(__f)).swap(*this);
00528           return *this;
00529         }
00530 
00531       /// @overload
00532       template<typename _Functor>
00533         function&
00534         operator=(reference_wrapper<_Functor> __f) noexcept
00535         {
00536           function(__f).swap(*this);
00537           return *this;
00538         }
00539 
00540       // [3.7.2.2] function modifiers
00541 
00542       /**
00543        *  @brief Swap the targets of two %function objects.
00544        *  @param __x A %function with identical call signature.
00545        *
00546        *  Swap the targets of @c this function object and @a __f. This
00547        *  function will not throw an %exception.
00548        */
00549       void swap(function& __x) noexcept
00550       {
00551         std::swap(_M_functor, __x._M_functor);
00552         std::swap(_M_manager, __x._M_manager);
00553         std::swap(_M_invoker, __x._M_invoker);
00554       }
00555 
00556       // [3.7.2.3] function capacity
00557 
00558       /**
00559        *  @brief Determine if the %function wrapper has a target.
00560        *
00561        *  @return @c true when this %function object contains a target,
00562        *  or @c false when it is empty.
00563        *
00564        *  This function will not throw an %exception.
00565        */
00566       explicit operator bool() const noexcept
00567       { return !_M_empty(); }
00568 
00569       // [3.7.2.4] function invocation
00570 
00571       /**
00572        *  @brief Invokes the function targeted by @c *this.
00573        *  @returns the result of the target.
00574        *  @throws bad_function_call when @c !(bool)*this
00575        *
00576        *  The function call operator invokes the target function object
00577        *  stored by @c this.
00578        */
00579       _Res operator()(_ArgTypes... __args) const;
00580 
00581 #if __cpp_rtti
00582       // [3.7.2.5] function target access
00583       /**
00584        *  @brief Determine the type of the target of this function object
00585        *  wrapper.
00586        *
00587        *  @returns the type identifier of the target function object, or
00588        *  @c typeid(void) if @c !(bool)*this.
00589        *
00590        *  This function will not throw an %exception.
00591        */
00592       const type_info& target_type() const noexcept;
00593 
00594       /**
00595        *  @brief Access the stored target function object.
00596        *
00597        *  @return Returns a pointer to the stored target function object,
00598        *  if @c typeid(_Functor).equals(target_type()); otherwise, a NULL
00599        *  pointer.
00600        *
00601        * This function does not throw exceptions.
00602        *
00603        * @{
00604        */
00605       template<typename _Functor>       _Functor* target() noexcept;
00606 
00607       template<typename _Functor> const _Functor* target() const noexcept;
00608       // @}
00609 #endif
00610 
00611     private:
00612       using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
00613       _Invoker_type _M_invoker;
00614   };
00615 
00616 #if __cpp_deduction_guides >= 201606
00617   template<typename>
00618     struct __function_guide_helper
00619     { };
00620 
00621   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00622     struct __function_guide_helper<
00623       _Res (_Tp::*) (_Args...) noexcept(_Nx)
00624     >
00625     { using type = _Res(_Args...); };
00626 
00627   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00628     struct __function_guide_helper<
00629       _Res (_Tp::*) (_Args...) & noexcept(_Nx)
00630     >
00631     { using type = _Res(_Args...); };
00632 
00633   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00634     struct __function_guide_helper<
00635       _Res (_Tp::*) (_Args...) const noexcept(_Nx)
00636     >
00637     { using type = _Res(_Args...); };
00638 
00639   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00640     struct __function_guide_helper<
00641       _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
00642     >
00643     { using type = _Res(_Args...); };
00644 
00645   template<typename _Res, typename... _ArgTypes>
00646     function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
00647 
00648   template<typename _Functor, typename _Signature = typename
00649            __function_guide_helper<decltype(&_Functor::operator())>::type>
00650     function(_Functor) -> function<_Signature>;
00651 #endif
00652 
00653   // Out-of-line member definitions.
00654   template<typename _Res, typename... _ArgTypes>
00655     function<_Res(_ArgTypes...)>::
00656     function(const function& __x)
00657     : _Function_base()
00658     {
00659       if (static_cast<bool>(__x))
00660         {
00661           __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
00662           _M_invoker = __x._M_invoker;
00663           _M_manager = __x._M_manager;
00664         }
00665     }
00666 
00667   template<typename _Res, typename... _ArgTypes>
00668     template<typename _Functor, typename, typename>
00669       function<_Res(_ArgTypes...)>::
00670       function(_Functor __f)
00671       : _Function_base()
00672       {
00673         typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
00674 
00675         if (_My_handler::_M_not_empty_function(__f))
00676           {
00677             _My_handler::_M_init_functor(_M_functor, std::move(__f));
00678             _M_invoker = &_My_handler::_M_invoke;
00679             _M_manager = &_My_handler::_M_manager;
00680           }
00681       }
00682 
00683   template<typename _Res, typename... _ArgTypes>
00684     _Res
00685     function<_Res(_ArgTypes...)>::
00686     operator()(_ArgTypes... __args) const
00687     {
00688       if (_M_empty())
00689         __throw_bad_function_call();
00690       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
00691     }
00692 
00693 #if __cpp_rtti
00694   template<typename _Res, typename... _ArgTypes>
00695     const type_info&
00696     function<_Res(_ArgTypes...)>::
00697     target_type() const noexcept
00698     {
00699       if (_M_manager)
00700         {
00701           _Any_data __typeinfo_result;
00702           _M_manager(__typeinfo_result, _M_functor, __get_type_info);
00703           return *__typeinfo_result._M_access<const type_info*>();
00704         }
00705       else
00706         return typeid(void);
00707     }
00708 
00709   template<typename _Res, typename... _ArgTypes>
00710     template<typename _Functor>
00711       _Functor*
00712       function<_Res(_ArgTypes...)>::
00713       target() noexcept
00714       {
00715         const function* __const_this = this;
00716         const _Functor* __func = __const_this->template target<_Functor>();
00717         return const_cast<_Functor*>(__func);
00718       }
00719 
00720   template<typename _Res, typename... _ArgTypes>
00721     template<typename _Functor>
00722       const _Functor*
00723       function<_Res(_ArgTypes...)>::
00724       target() const noexcept
00725       {
00726         if (typeid(_Functor) == target_type() && _M_manager)
00727           {
00728             _Any_data __ptr;
00729             _M_manager(__ptr, _M_functor, __get_functor_ptr);
00730             return __ptr._M_access<const _Functor*>();
00731           }
00732         else
00733           return nullptr;
00734       }
00735 #endif
00736 
00737   // [20.7.15.2.6] null pointer comparisons
00738 
00739   /**
00740    *  @brief Compares a polymorphic function object wrapper against 0
00741    *  (the NULL pointer).
00742    *  @returns @c true if the wrapper has no target, @c false otherwise
00743    *
00744    *  This function will not throw an %exception.
00745    */
00746   template<typename _Res, typename... _Args>
00747     inline bool
00748     operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
00749     { return !static_cast<bool>(__f); }
00750 
00751   /// @overload
00752   template<typename _Res, typename... _Args>
00753     inline bool
00754     operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
00755     { return !static_cast<bool>(__f); }
00756 
00757   /**
00758    *  @brief Compares a polymorphic function object wrapper against 0
00759    *  (the NULL pointer).
00760    *  @returns @c false if the wrapper has no target, @c true otherwise
00761    *
00762    *  This function will not throw an %exception.
00763    */
00764   template<typename _Res, typename... _Args>
00765     inline bool
00766     operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
00767     { return static_cast<bool>(__f); }
00768 
00769   /// @overload
00770   template<typename _Res, typename... _Args>
00771     inline bool
00772     operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
00773     { return static_cast<bool>(__f); }
00774 
00775 
00776   // [20.7.15.2.7] specialized algorithms
00777 
00778   /**
00779    *  @brief Swap the targets of two polymorphic function object wrappers.
00780    *
00781    *  This function will not throw an %exception.
00782    */
00783   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00784   // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
00785   template<typename _Res, typename... _Args>
00786     inline void
00787     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
00788     { __x.swap(__y); }
00789 
00790 #if __cplusplus >= 201703L
00791   namespace __detail::__variant
00792   {
00793     template<typename> struct _Never_valueless_alt; // see <variant>
00794 
00795     // Provide the strong exception-safety guarantee when emplacing a
00796     // function into a variant.
00797     template<typename _Signature>
00798       struct _Never_valueless_alt<std::function<_Signature>>
00799       : std::true_type
00800       { };
00801   }  // namespace __detail::__variant
00802 #endif // C++17
00803 
00804 _GLIBCXX_END_NAMESPACE_VERSION
00805 } // namespace std
00806 
00807 #endif // C++11
00808 #endif // _GLIBCXX_STD_FUNCTION_H