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