std_function.h 23 KB

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