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