libstdc++
std_function.h
Go to the documentation of this file.
00001 // Implementation of std::function -*- C++ -*-
00002 
00003 // Copyright (C) 2004-2018 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 std::size_t _M_max_size = sizeof(_Nocopy_types);
00135     static const std::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           const _Functor* __ptr =
00154             __stored_locally? std::__addressof(__source._M_access<_Functor>())
00155             /* have stored a pointer */ : __source._M_access<_Functor*>();
00156           return const_cast<_Functor*>(__ptr);
00157         }
00158 
00159         // Clone a location-invariant function object that fits within
00160         // an _Any_data structure.
00161         static void
00162         _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
00163         {
00164           ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
00165         }
00166 
00167         // Clone a function object that is not location-invariant or
00168         // that cannot fit into an _Any_data structure.
00169         static void
00170         _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
00171         {
00172           __dest._M_access<_Functor*>() =
00173             new _Functor(*__source._M_access<_Functor*>());
00174         }
00175 
00176         // Destroying a location-invariant object may still require
00177         // destruction.
00178         static void
00179         _M_destroy(_Any_data& __victim, true_type)
00180         {
00181           __victim._M_access<_Functor>().~_Functor();
00182         }
00183 
00184         // Destroying an object located on the heap.
00185         static void
00186         _M_destroy(_Any_data& __victim, false_type)
00187         {
00188           delete __victim._M_access<_Functor*>();
00189         }
00190 
00191       public:
00192         static bool
00193         _M_manager(_Any_data& __dest, const _Any_data& __source,
00194                    _Manager_operation __op)
00195         {
00196           switch (__op)
00197             {
00198 #if __cpp_rtti
00199             case __get_type_info:
00200               __dest._M_access<const type_info*>() = &typeid(_Functor);
00201               break;
00202 #endif
00203             case __get_functor_ptr:
00204               __dest._M_access<_Functor*>() = _M_get_pointer(__source);
00205               break;
00206 
00207             case __clone_functor:
00208               _M_clone(__dest, __source, _Local_storage());
00209               break;
00210 
00211             case __destroy_functor:
00212               _M_destroy(__dest, _Local_storage());
00213               break;
00214             }
00215           return false;
00216         }
00217 
00218         static void
00219         _M_init_functor(_Any_data& __functor, _Functor&& __f)
00220         { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
00221 
00222         template<typename _Signature>
00223           static bool
00224           _M_not_empty_function(const function<_Signature>& __f)
00225           { return static_cast<bool>(__f); }
00226 
00227         template<typename _Tp>
00228           static bool
00229           _M_not_empty_function(_Tp* __fp)
00230           { return __fp != nullptr; }
00231 
00232         template<typename _Class, typename _Tp>
00233           static bool
00234           _M_not_empty_function(_Tp _Class::* __mp)
00235           { return __mp != nullptr; }
00236 
00237         template<typename _Tp>
00238           static bool
00239           _M_not_empty_function(const _Tp&)
00240           { return true; }
00241 
00242       private:
00243         static void
00244         _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
00245         { ::new (__functor._M_access()) _Functor(std::move(__f)); }
00246 
00247         static void
00248         _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
00249         { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
00250       };
00251 
00252     _Function_base() : _M_manager(nullptr) { }
00253 
00254     ~_Function_base()
00255     {
00256       if (_M_manager)
00257         _M_manager(_M_functor, _M_functor, __destroy_functor);
00258     }
00259 
00260     bool _M_empty() const { return !_M_manager; }
00261 
00262     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
00263                                   _Manager_operation);
00264 
00265     _Any_data     _M_functor;
00266     _Manager_type _M_manager;
00267   };
00268 
00269   template<typename _Signature, typename _Functor>
00270     class _Function_handler;
00271 
00272   template<typename _Res, typename _Functor, typename... _ArgTypes>
00273     class _Function_handler<_Res(_ArgTypes...), _Functor>
00274     : public _Function_base::_Base_manager<_Functor>
00275     {
00276       typedef _Function_base::_Base_manager<_Functor> _Base;
00277 
00278     public:
00279       static _Res
00280       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
00281       {
00282         return (*_Base::_M_get_pointer(__functor))(
00283             std::forward<_ArgTypes>(__args)...);
00284       }
00285     };
00286 
00287   template<typename _Functor, typename... _ArgTypes>
00288     class _Function_handler<void(_ArgTypes...), _Functor>
00289     : public _Function_base::_Base_manager<_Functor>
00290     {
00291       typedef _Function_base::_Base_manager<_Functor> _Base;
00292 
00293      public:
00294       static void
00295       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
00296       {
00297         (*_Base::_M_get_pointer(__functor))(
00298             std::forward<_ArgTypes>(__args)...);
00299       }
00300     };
00301 
00302   template<typename _Class, typename _Member, typename _Res,
00303            typename... _ArgTypes>
00304     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
00305     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
00306     {
00307       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
00308         _Base;
00309 
00310      public:
00311       static _Res
00312       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
00313       {
00314         return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
00315                              std::forward<_ArgTypes>(__args)...);
00316       }
00317     };
00318 
00319   template<typename _Class, typename _Member, typename... _ArgTypes>
00320     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
00321     : public _Function_base::_Base_manager<
00322                  _Simple_type_wrapper< _Member _Class::* > >
00323     {
00324       typedef _Member _Class::* _Functor;
00325       typedef _Simple_type_wrapper<_Functor> _Wrapper;
00326       typedef _Function_base::_Base_manager<_Wrapper> _Base;
00327 
00328     public:
00329       static bool
00330       _M_manager(_Any_data& __dest, const _Any_data& __source,
00331                  _Manager_operation __op)
00332       {
00333         switch (__op)
00334           {
00335 #if __cpp_rtti
00336           case __get_type_info:
00337             __dest._M_access<const type_info*>() = &typeid(_Functor);
00338             break;
00339 #endif
00340           case __get_functor_ptr:
00341             __dest._M_access<_Functor*>() =
00342               &_Base::_M_get_pointer(__source)->__value;
00343             break;
00344 
00345           default:
00346             _Base::_M_manager(__dest, __source, __op);
00347           }
00348         return false;
00349       }
00350 
00351       static void
00352       _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
00353       {
00354         std::__invoke(_Base::_M_get_pointer(__functor)->__value,
00355                       std::forward<_ArgTypes>(__args)...);
00356       }
00357     };
00358 
00359   template<typename _From, typename _To>
00360     using __check_func_return_type
00361       = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
00362 
00363   /**
00364    *  @brief Primary class template for std::function.
00365    *  @ingroup functors
00366    *
00367    *  Polymorphic function wrapper.
00368    */
00369   template<typename _Res, typename... _ArgTypes>
00370     class function<_Res(_ArgTypes...)>
00371     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
00372       private _Function_base
00373     {
00374       template<typename _Func,
00375                typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
00376         struct _Callable : __check_func_return_type<_Res2, _Res> { };
00377 
00378       // Used so the return type convertibility checks aren't done when
00379       // performing overload resolution for copy construction/assignment.
00380       template<typename _Tp>
00381         struct _Callable<function, _Tp> : false_type { };
00382 
00383       template<typename _Cond, typename _Tp>
00384         using _Requires = typename enable_if<_Cond::value, _Tp>::type;
00385 
00386     public:
00387       typedef _Res result_type;
00388 
00389       // [3.7.2.1] construct/copy/destroy
00390 
00391       /**
00392        *  @brief Default construct creates an empty function call wrapper.
00393        *  @post @c !(bool)*this
00394        */
00395       function() noexcept
00396       : _Function_base() { }
00397 
00398       /**
00399        *  @brief Creates an empty function call wrapper.
00400        *  @post @c !(bool)*this
00401        */
00402       function(nullptr_t) noexcept
00403       : _Function_base() { }
00404 
00405       /**
00406        *  @brief %Function copy constructor.
00407        *  @param __x A %function object with identical call signature.
00408        *  @post @c bool(*this) == bool(__x)
00409        *
00410        *  The newly-created %function contains a copy of the target of @a
00411        *  __x (if it has one).
00412        */
00413       function(const function& __x);
00414 
00415       /**
00416        *  @brief %Function move constructor.
00417        *  @param __x A %function object rvalue with identical call signature.
00418        *
00419        *  The newly-created %function contains the target of @a __x
00420        *  (if it has one).
00421        */
00422       function(function&& __x) noexcept : _Function_base()
00423       {
00424         __x.swap(*this);
00425       }
00426 
00427       /**
00428        *  @brief Builds a %function that targets a copy of the incoming
00429        *  function object.
00430        *  @param __f A %function object that is callable with parameters of
00431        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
00432        *  to @c Res.
00433        *
00434        *  The newly-created %function object will target a copy of
00435        *  @a __f. If @a __f is @c reference_wrapper<F>, then this function
00436        *  object will contain a reference to the function object @c
00437        *  __f.get(). If @a __f is a NULL function pointer or NULL
00438        *  pointer-to-member, the newly-created object will be empty.
00439        *
00440        *  If @a __f is a non-NULL function pointer or an object of type @c
00441        *  reference_wrapper<F>, this function will not throw.
00442        */
00443       template<typename _Functor,
00444                typename = _Requires<__not_<is_same<_Functor, function>>, void>,
00445                typename = _Requires<_Callable<_Functor>, void>>
00446         function(_Functor);
00447 
00448       /**
00449        *  @brief %Function assignment operator.
00450        *  @param __x A %function with identical call signature.
00451        *  @post @c (bool)*this == (bool)x
00452        *  @returns @c *this
00453        *
00454        *  The target of @a __x is copied to @c *this. If @a __x has no
00455        *  target, then @c *this will be empty.
00456        *
00457        *  If @a __x targets a function pointer or a reference to a function
00458        *  object, then this operation will not throw an %exception.
00459        */
00460       function&
00461       operator=(const function& __x)
00462       {
00463         function(__x).swap(*this);
00464         return *this;
00465       }
00466 
00467       /**
00468        *  @brief %Function move-assignment operator.
00469        *  @param __x A %function rvalue with identical call signature.
00470        *  @returns @c *this
00471        *
00472        *  The target of @a __x is moved to @c *this. If @a __x has no
00473        *  target, then @c *this will be empty.
00474        *
00475        *  If @a __x targets a function pointer or a reference to a function
00476        *  object, then this operation will not throw an %exception.
00477        */
00478       function&
00479       operator=(function&& __x) noexcept
00480       {
00481         function(std::move(__x)).swap(*this);
00482         return *this;
00483       }
00484 
00485       /**
00486        *  @brief %Function assignment to zero.
00487        *  @post @c !(bool)*this
00488        *  @returns @c *this
00489        *
00490        *  The target of @c *this is deallocated, leaving it empty.
00491        */
00492       function&
00493       operator=(nullptr_t) noexcept
00494       {
00495         if (_M_manager)
00496           {
00497             _M_manager(_M_functor, _M_functor, __destroy_functor);
00498             _M_manager = nullptr;
00499             _M_invoker = nullptr;
00500           }
00501         return *this;
00502       }
00503 
00504       /**
00505        *  @brief %Function assignment to a new target.
00506        *  @param __f A %function object that is callable with parameters of
00507        *  type @c T1, @c T2, ..., @c TN and returns a value convertible
00508        *  to @c Res.
00509        *  @return @c *this
00510        *
00511        *  This  %function object wrapper will target a copy of @a
00512        *  __f. If @a __f is @c reference_wrapper<F>, then this function
00513        *  object will contain a reference to the function object @c
00514        *  __f.get(). If @a __f is a NULL function pointer or NULL
00515        *  pointer-to-member, @c this object will be empty.
00516        *
00517        *  If @a __f is a non-NULL function pointer or an object of type @c
00518        *  reference_wrapper<F>, this function will not throw.
00519        */
00520       template<typename _Functor>
00521         _Requires<_Callable<typename decay<_Functor>::type>, function&>
00522         operator=(_Functor&& __f)
00523         {
00524           function(std::forward<_Functor>(__f)).swap(*this);
00525           return *this;
00526         }
00527 
00528       /// @overload
00529       template<typename _Functor>
00530         function&
00531         operator=(reference_wrapper<_Functor> __f) noexcept
00532         {
00533           function(__f).swap(*this);
00534           return *this;
00535         }
00536 
00537       // [3.7.2.2] function modifiers
00538 
00539       /**
00540        *  @brief Swap the targets of two %function objects.
00541        *  @param __x A %function with identical call signature.
00542        *
00543        *  Swap the targets of @c this function object and @a __f. This
00544        *  function will not throw an %exception.
00545        */
00546       void swap(function& __x) noexcept
00547       {
00548         std::swap(_M_functor, __x._M_functor);
00549         std::swap(_M_manager, __x._M_manager);
00550         std::swap(_M_invoker, __x._M_invoker);
00551       }
00552 
00553       // [3.7.2.3] function capacity
00554 
00555       /**
00556        *  @brief Determine if the %function wrapper has a target.
00557        *
00558        *  @return @c true when this %function object contains a target,
00559        *  or @c false when it is empty.
00560        *
00561        *  This function will not throw an %exception.
00562        */
00563       explicit operator bool() const noexcept
00564       { return !_M_empty(); }
00565 
00566       // [3.7.2.4] function invocation
00567 
00568       /**
00569        *  @brief Invokes the function targeted by @c *this.
00570        *  @returns the result of the target.
00571        *  @throws bad_function_call when @c !(bool)*this
00572        *
00573        *  The function call operator invokes the target function object
00574        *  stored by @c this.
00575        */
00576       _Res operator()(_ArgTypes... __args) const;
00577 
00578 #if __cpp_rtti
00579       // [3.7.2.5] function target access
00580       /**
00581        *  @brief Determine the type of the target of this function object
00582        *  wrapper.
00583        *
00584        *  @returns the type identifier of the target function object, or
00585        *  @c typeid(void) if @c !(bool)*this.
00586        *
00587        *  This function will not throw an %exception.
00588        */
00589       const type_info& target_type() const noexcept;
00590 
00591       /**
00592        *  @brief Access the stored target function object.
00593        *
00594        *  @return Returns a pointer to the stored target function object,
00595        *  if @c typeid(_Functor).equals(target_type()); otherwise, a NULL
00596        *  pointer.
00597        *
00598        * This function does not throw exceptions.
00599        *
00600        * @{
00601        */
00602       template<typename _Functor>       _Functor* target() noexcept;
00603 
00604       template<typename _Functor> const _Functor* target() const noexcept;
00605       // @}
00606 #endif
00607 
00608     private:
00609       using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
00610       _Invoker_type _M_invoker;
00611   };
00612 
00613 #if __cpp_deduction_guides >= 201606
00614   template<typename>
00615     struct __function_guide_helper
00616     { };
00617 
00618   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00619     struct __function_guide_helper<
00620       _Res (_Tp::*) (_Args...) noexcept(_Nx)
00621     >
00622     { using type = _Res(_Args...); };
00623 
00624   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00625     struct __function_guide_helper<
00626       _Res (_Tp::*) (_Args...) & noexcept(_Nx)
00627     >
00628     { using type = _Res(_Args...); };
00629 
00630   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00631     struct __function_guide_helper<
00632       _Res (_Tp::*) (_Args...) const noexcept(_Nx)
00633     >
00634     { using type = _Res(_Args...); };
00635 
00636   template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
00637     struct __function_guide_helper<
00638       _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
00639     >
00640     { using type = _Res(_Args...); };
00641 
00642   template<typename _Res, typename... _ArgTypes>
00643     function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
00644 
00645   template<typename _Functor, typename _Signature = typename
00646            __function_guide_helper<decltype(&_Functor::operator())>::type>
00647     function(_Functor) -> function<_Signature>;
00648 #endif
00649 
00650   // Out-of-line member definitions.
00651   template<typename _Res, typename... _ArgTypes>
00652     function<_Res(_ArgTypes...)>::
00653     function(const function& __x)
00654     : _Function_base()
00655     {
00656       if (static_cast<bool>(__x))
00657         {
00658           __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
00659           _M_invoker = __x._M_invoker;
00660           _M_manager = __x._M_manager;
00661         }
00662     }
00663 
00664   template<typename _Res, typename... _ArgTypes>
00665     template<typename _Functor, typename, typename>
00666       function<_Res(_ArgTypes...)>::
00667       function(_Functor __f)
00668       : _Function_base()
00669       {
00670         typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
00671 
00672         if (_My_handler::_M_not_empty_function(__f))
00673           {
00674             _My_handler::_M_init_functor(_M_functor, std::move(__f));
00675             _M_invoker = &_My_handler::_M_invoke;
00676             _M_manager = &_My_handler::_M_manager;
00677           }
00678       }
00679 
00680   template<typename _Res, typename... _ArgTypes>
00681     _Res
00682     function<_Res(_ArgTypes...)>::
00683     operator()(_ArgTypes... __args) const
00684     {
00685       if (_M_empty())
00686         __throw_bad_function_call();
00687       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
00688     }
00689 
00690 #if __cpp_rtti
00691   template<typename _Res, typename... _ArgTypes>
00692     const type_info&
00693     function<_Res(_ArgTypes...)>::
00694     target_type() const noexcept
00695     {
00696       if (_M_manager)
00697         {
00698           _Any_data __typeinfo_result;
00699           _M_manager(__typeinfo_result, _M_functor, __get_type_info);
00700           return *__typeinfo_result._M_access<const type_info*>();
00701         }
00702       else
00703         return typeid(void);
00704     }
00705 
00706   template<typename _Res, typename... _ArgTypes>
00707     template<typename _Functor>
00708       _Functor*
00709       function<_Res(_ArgTypes...)>::
00710       target() noexcept
00711       {
00712         const function* __const_this = this;
00713         const _Functor* __func = __const_this->template target<_Functor>();
00714         return const_cast<_Functor*>(__func);
00715       }
00716 
00717   template<typename _Res, typename... _ArgTypes>
00718     template<typename _Functor>
00719       const _Functor*
00720       function<_Res(_ArgTypes...)>::
00721       target() const noexcept
00722       {
00723         if (typeid(_Functor) == target_type() && _M_manager)
00724           {
00725             _Any_data __ptr;
00726             _M_manager(__ptr, _M_functor, __get_functor_ptr);
00727             return __ptr._M_access<const _Functor*>();
00728           }
00729         else
00730           return nullptr;
00731       }
00732 #endif
00733 
00734   // [20.7.15.2.6] null pointer comparisons
00735 
00736   /**
00737    *  @brief Compares a polymorphic function object wrapper against 0
00738    *  (the NULL pointer).
00739    *  @returns @c true if the wrapper has no target, @c false otherwise
00740    *
00741    *  This function will not throw an %exception.
00742    */
00743   template<typename _Res, typename... _Args>
00744     inline bool
00745     operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
00746     { return !static_cast<bool>(__f); }
00747 
00748   /// @overload
00749   template<typename _Res, typename... _Args>
00750     inline bool
00751     operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
00752     { return !static_cast<bool>(__f); }
00753 
00754   /**
00755    *  @brief Compares a polymorphic function object wrapper against 0
00756    *  (the NULL pointer).
00757    *  @returns @c false if the wrapper has no target, @c true otherwise
00758    *
00759    *  This function will not throw an %exception.
00760    */
00761   template<typename _Res, typename... _Args>
00762     inline bool
00763     operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
00764     { return static_cast<bool>(__f); }
00765 
00766   /// @overload
00767   template<typename _Res, typename... _Args>
00768     inline bool
00769     operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
00770     { return static_cast<bool>(__f); }
00771 
00772 
00773   // [20.7.15.2.7] specialized algorithms
00774 
00775   /**
00776    *  @brief Swap the targets of two polymorphic function object wrappers.
00777    *
00778    *  This function will not throw an %exception.
00779    */
00780   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00781   // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
00782   template<typename _Res, typename... _Args>
00783     inline void
00784     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
00785     { __x.swap(__y); }
00786 
00787 _GLIBCXX_END_NAMESPACE_VERSION
00788 } // namespace std
00789 
00790 #endif // C++11
00791 
00792 #endif // _GLIBCXX_STD_FUNCTION_H