std_function.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. // Implementation of std::function -*- C++ -*-
  2. // Copyright (C) 2004-2018 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 std::size_t _M_max_size = sizeof(_Nocopy_types);
  110. static const std::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. const _Functor* __ptr =
  126. __stored_locally? std::__addressof(__source._M_access<_Functor>())
  127. /* have stored a pointer */ : __source._M_access<_Functor*>();
  128. return const_cast<_Functor*>(__ptr);
  129. }
  130. // Clone a location-invariant function object that fits within
  131. // an _Any_data structure.
  132. static void
  133. _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
  134. {
  135. ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
  136. }
  137. // Clone a function object that is not location-invariant or
  138. // that cannot fit into an _Any_data structure.
  139. static void
  140. _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
  141. {
  142. __dest._M_access<_Functor*>() =
  143. new _Functor(*__source._M_access<_Functor*>());
  144. }
  145. // Destroying a location-invariant object may still require
  146. // destruction.
  147. static void
  148. _M_destroy(_Any_data& __victim, true_type)
  149. {
  150. __victim._M_access<_Functor>().~_Functor();
  151. }
  152. // Destroying an object located on the heap.
  153. static void
  154. _M_destroy(_Any_data& __victim, false_type)
  155. {
  156. delete __victim._M_access<_Functor*>();
  157. }
  158. public:
  159. static bool
  160. _M_manager(_Any_data& __dest, const _Any_data& __source,
  161. _Manager_operation __op)
  162. {
  163. switch (__op)
  164. {
  165. #if __cpp_rtti
  166. case __get_type_info:
  167. __dest._M_access<const type_info*>() = &typeid(_Functor);
  168. break;
  169. #endif
  170. case __get_functor_ptr:
  171. __dest._M_access<_Functor*>() = _M_get_pointer(__source);
  172. break;
  173. case __clone_functor:
  174. _M_clone(__dest, __source, _Local_storage());
  175. break;
  176. case __destroy_functor:
  177. _M_destroy(__dest, _Local_storage());
  178. break;
  179. }
  180. return false;
  181. }
  182. static void
  183. _M_init_functor(_Any_data& __functor, _Functor&& __f)
  184. { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
  185. template<typename _Signature>
  186. static bool
  187. _M_not_empty_function(const function<_Signature>& __f)
  188. { return static_cast<bool>(__f); }
  189. template<typename _Tp>
  190. static bool
  191. _M_not_empty_function(_Tp* __fp)
  192. { return __fp != nullptr; }
  193. template<typename _Class, typename _Tp>
  194. static bool
  195. _M_not_empty_function(_Tp _Class::* __mp)
  196. { return __mp != nullptr; }
  197. template<typename _Tp>
  198. static bool
  199. _M_not_empty_function(const _Tp&)
  200. { return true; }
  201. private:
  202. static void
  203. _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
  204. { ::new (__functor._M_access()) _Functor(std::move(__f)); }
  205. static void
  206. _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
  207. { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
  208. };
  209. _Function_base() : _M_manager(nullptr) { }
  210. ~_Function_base()
  211. {
  212. if (_M_manager)
  213. _M_manager(_M_functor, _M_functor, __destroy_functor);
  214. }
  215. bool _M_empty() const { return !_M_manager; }
  216. typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
  217. _Manager_operation);
  218. _Any_data _M_functor;
  219. _Manager_type _M_manager;
  220. };
  221. template<typename _Signature, typename _Functor>
  222. class _Function_handler;
  223. template<typename _Res, typename _Functor, typename... _ArgTypes>
  224. class _Function_handler<_Res(_ArgTypes...), _Functor>
  225. : public _Function_base::_Base_manager<_Functor>
  226. {
  227. typedef _Function_base::_Base_manager<_Functor> _Base;
  228. public:
  229. static _Res
  230. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  231. {
  232. return (*_Base::_M_get_pointer(__functor))(
  233. std::forward<_ArgTypes>(__args)...);
  234. }
  235. };
  236. template<typename _Functor, typename... _ArgTypes>
  237. class _Function_handler<void(_ArgTypes...), _Functor>
  238. : public _Function_base::_Base_manager<_Functor>
  239. {
  240. typedef _Function_base::_Base_manager<_Functor> _Base;
  241. public:
  242. static void
  243. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  244. {
  245. (*_Base::_M_get_pointer(__functor))(
  246. std::forward<_ArgTypes>(__args)...);
  247. }
  248. };
  249. template<typename _Class, typename _Member, typename _Res,
  250. typename... _ArgTypes>
  251. class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
  252. : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
  253. {
  254. typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
  255. _Base;
  256. public:
  257. static _Res
  258. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  259. {
  260. return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
  261. std::forward<_ArgTypes>(__args)...);
  262. }
  263. };
  264. template<typename _Class, typename _Member, typename... _ArgTypes>
  265. class _Function_handler<void(_ArgTypes...), _Member _Class::*>
  266. : public _Function_base::_Base_manager<
  267. _Simple_type_wrapper< _Member _Class::* > >
  268. {
  269. typedef _Member _Class::* _Functor;
  270. typedef _Simple_type_wrapper<_Functor> _Wrapper;
  271. typedef _Function_base::_Base_manager<_Wrapper> _Base;
  272. public:
  273. static bool
  274. _M_manager(_Any_data& __dest, const _Any_data& __source,
  275. _Manager_operation __op)
  276. {
  277. switch (__op)
  278. {
  279. #if __cpp_rtti
  280. case __get_type_info:
  281. __dest._M_access<const type_info*>() = &typeid(_Functor);
  282. break;
  283. #endif
  284. case __get_functor_ptr:
  285. __dest._M_access<_Functor*>() =
  286. &_Base::_M_get_pointer(__source)->__value;
  287. break;
  288. default:
  289. _Base::_M_manager(__dest, __source, __op);
  290. }
  291. return false;
  292. }
  293. static void
  294. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  295. {
  296. std::__invoke(_Base::_M_get_pointer(__functor)->__value,
  297. std::forward<_ArgTypes>(__args)...);
  298. }
  299. };
  300. template<typename _From, typename _To>
  301. using __check_func_return_type
  302. = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
  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 = typename result_of<_Func&(_ArgTypes...)>::type>
  316. struct _Callable : __check_func_return_type<_Res2, _Res> { };
  317. // Used so the return type convertibility checks aren't done when
  318. // performing overload resolution for copy construction/assignment.
  319. template<typename _Tp>
  320. struct _Callable<function, _Tp> : false_type { };
  321. template<typename _Cond, typename _Tp>
  322. using _Requires = typename enable_if<_Cond::value, _Tp>::type;
  323. public:
  324. typedef _Res result_type;
  325. // [3.7.2.1] construct/copy/destroy
  326. /**
  327. * @brief Default construct creates an empty function call wrapper.
  328. * @post @c !(bool)*this
  329. */
  330. function() noexcept
  331. : _Function_base() { }
  332. /**
  333. * @brief Creates an empty function call wrapper.
  334. * @post @c !(bool)*this
  335. */
  336. function(nullptr_t) noexcept
  337. : _Function_base() { }
  338. /**
  339. * @brief %Function copy constructor.
  340. * @param __x A %function object with identical call signature.
  341. * @post @c bool(*this) == bool(__x)
  342. *
  343. * The newly-created %function contains a copy of the target of @a
  344. * __x (if it has one).
  345. */
  346. function(const function& __x);
  347. /**
  348. * @brief %Function move constructor.
  349. * @param __x A %function object rvalue with identical call signature.
  350. *
  351. * The newly-created %function contains the target of @a __x
  352. * (if it has one).
  353. */
  354. function(function&& __x) noexcept : _Function_base()
  355. {
  356. __x.swap(*this);
  357. }
  358. /**
  359. * @brief Builds a %function that targets a copy of the incoming
  360. * function object.
  361. * @param __f A %function object that is callable with parameters of
  362. * type @c T1, @c T2, ..., @c TN and returns a value convertible
  363. * to @c Res.
  364. *
  365. * The newly-created %function object will target a copy of
  366. * @a __f. If @a __f is @c reference_wrapper<F>, then this function
  367. * object will contain a reference to the function object @c
  368. * __f.get(). If @a __f is a NULL function pointer or NULL
  369. * pointer-to-member, the newly-created object will be empty.
  370. *
  371. * If @a __f is a non-NULL function pointer or an object of type @c
  372. * reference_wrapper<F>, this function will not throw.
  373. */
  374. template<typename _Functor,
  375. typename = _Requires<__not_<is_same<_Functor, function>>, void>,
  376. typename = _Requires<_Callable<_Functor>, void>>
  377. function(_Functor);
  378. /**
  379. * @brief %Function assignment operator.
  380. * @param __x A %function with identical call signature.
  381. * @post @c (bool)*this == (bool)x
  382. * @returns @c *this
  383. *
  384. * The target of @a __x is copied to @c *this. If @a __x has no
  385. * target, then @c *this will be empty.
  386. *
  387. * If @a __x targets a function pointer or a reference to a function
  388. * object, then this operation will not throw an %exception.
  389. */
  390. function&
  391. operator=(const function& __x)
  392. {
  393. function(__x).swap(*this);
  394. return *this;
  395. }
  396. /**
  397. * @brief %Function move-assignment operator.
  398. * @param __x A %function rvalue with identical call signature.
  399. * @returns @c *this
  400. *
  401. * The target of @a __x is moved to @c *this. If @a __x has no
  402. * target, then @c *this will be empty.
  403. *
  404. * If @a __x targets a function pointer or a reference to a function
  405. * object, then this operation will not throw an %exception.
  406. */
  407. function&
  408. operator=(function&& __x) noexcept
  409. {
  410. function(std::move(__x)).swap(*this);
  411. return *this;
  412. }
  413. /**
  414. * @brief %Function assignment to zero.
  415. * @post @c !(bool)*this
  416. * @returns @c *this
  417. *
  418. * The target of @c *this is deallocated, leaving it empty.
  419. */
  420. function&
  421. operator=(nullptr_t) noexcept
  422. {
  423. if (_M_manager)
  424. {
  425. _M_manager(_M_functor, _M_functor, __destroy_functor);
  426. _M_manager = nullptr;
  427. _M_invoker = nullptr;
  428. }
  429. return *this;
  430. }
  431. /**
  432. * @brief %Function assignment to a new target.
  433. * @param __f A %function object that is callable with parameters of
  434. * type @c T1, @c T2, ..., @c TN and returns a value convertible
  435. * to @c Res.
  436. * @return @c *this
  437. *
  438. * This %function object wrapper will target a copy of @a
  439. * __f. If @a __f is @c reference_wrapper<F>, then this function
  440. * object will contain a reference to the function object @c
  441. * __f.get(). If @a __f is a NULL function pointer or NULL
  442. * pointer-to-member, @c this object will be empty.
  443. *
  444. * If @a __f is a non-NULL function pointer or an object of type @c
  445. * reference_wrapper<F>, this function will not throw.
  446. */
  447. template<typename _Functor>
  448. _Requires<_Callable<typename decay<_Functor>::type>, function&>
  449. operator=(_Functor&& __f)
  450. {
  451. function(std::forward<_Functor>(__f)).swap(*this);
  452. return *this;
  453. }
  454. /// @overload
  455. template<typename _Functor>
  456. function&
  457. operator=(reference_wrapper<_Functor> __f) noexcept
  458. {
  459. function(__f).swap(*this);
  460. return *this;
  461. }
  462. // [3.7.2.2] function modifiers
  463. /**
  464. * @brief Swap the targets of two %function objects.
  465. * @param __x A %function with identical call signature.
  466. *
  467. * Swap the targets of @c this function object and @a __f. This
  468. * function will not throw an %exception.
  469. */
  470. void swap(function& __x) noexcept
  471. {
  472. std::swap(_M_functor, __x._M_functor);
  473. std::swap(_M_manager, __x._M_manager);
  474. std::swap(_M_invoker, __x._M_invoker);
  475. }
  476. // [3.7.2.3] function capacity
  477. /**
  478. * @brief Determine if the %function wrapper has a target.
  479. *
  480. * @return @c true when this %function object contains a target,
  481. * or @c false when it is empty.
  482. *
  483. * This function will not throw an %exception.
  484. */
  485. explicit operator bool() const noexcept
  486. { return !_M_empty(); }
  487. // [3.7.2.4] function invocation
  488. /**
  489. * @brief Invokes the function targeted by @c *this.
  490. * @returns the result of the target.
  491. * @throws bad_function_call when @c !(bool)*this
  492. *
  493. * The function call operator invokes the target function object
  494. * stored by @c this.
  495. */
  496. _Res operator()(_ArgTypes... __args) const;
  497. #if __cpp_rtti
  498. // [3.7.2.5] function target access
  499. /**
  500. * @brief Determine the type of the target of this function object
  501. * wrapper.
  502. *
  503. * @returns the type identifier of the target function object, or
  504. * @c typeid(void) if @c !(bool)*this.
  505. *
  506. * This function will not throw an %exception.
  507. */
  508. const type_info& target_type() const noexcept;
  509. /**
  510. * @brief Access the stored target function object.
  511. *
  512. * @return Returns a pointer to the stored target function object,
  513. * if @c typeid(_Functor).equals(target_type()); otherwise, a NULL
  514. * pointer.
  515. *
  516. * This function does not throw exceptions.
  517. *
  518. * @{
  519. */
  520. template<typename _Functor> _Functor* target() noexcept;
  521. template<typename _Functor> const _Functor* target() const noexcept;
  522. // @}
  523. #endif
  524. private:
  525. using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
  526. _Invoker_type _M_invoker;
  527. };
  528. #if __cpp_deduction_guides >= 201606
  529. template<typename>
  530. struct __function_guide_helper
  531. { };
  532. template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
  533. struct __function_guide_helper<
  534. _Res (_Tp::*) (_Args...) noexcept(_Nx)
  535. >
  536. { using type = _Res(_Args...); };
  537. template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
  538. struct __function_guide_helper<
  539. _Res (_Tp::*) (_Args...) & noexcept(_Nx)
  540. >
  541. { using type = _Res(_Args...); };
  542. template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
  543. struct __function_guide_helper<
  544. _Res (_Tp::*) (_Args...) const noexcept(_Nx)
  545. >
  546. { using type = _Res(_Args...); };
  547. template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
  548. struct __function_guide_helper<
  549. _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
  550. >
  551. { using type = _Res(_Args...); };
  552. template<typename _Res, typename... _ArgTypes>
  553. function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
  554. template<typename _Functor, typename _Signature = typename
  555. __function_guide_helper<decltype(&_Functor::operator())>::type>
  556. function(_Functor) -> function<_Signature>;
  557. #endif
  558. // Out-of-line member definitions.
  559. template<typename _Res, typename... _ArgTypes>
  560. function<_Res(_ArgTypes...)>::
  561. function(const function& __x)
  562. : _Function_base()
  563. {
  564. if (static_cast<bool>(__x))
  565. {
  566. __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
  567. _M_invoker = __x._M_invoker;
  568. _M_manager = __x._M_manager;
  569. }
  570. }
  571. template<typename _Res, typename... _ArgTypes>
  572. template<typename _Functor, typename, typename>
  573. function<_Res(_ArgTypes...)>::
  574. function(_Functor __f)
  575. : _Function_base()
  576. {
  577. typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
  578. if (_My_handler::_M_not_empty_function(__f))
  579. {
  580. _My_handler::_M_init_functor(_M_functor, std::move(__f));
  581. _M_invoker = &_My_handler::_M_invoke;
  582. _M_manager = &_My_handler::_M_manager;
  583. }
  584. }
  585. template<typename _Res, typename... _ArgTypes>
  586. _Res
  587. function<_Res(_ArgTypes...)>::
  588. operator()(_ArgTypes... __args) const
  589. {
  590. if (_M_empty())
  591. __throw_bad_function_call();
  592. return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
  593. }
  594. #if __cpp_rtti
  595. template<typename _Res, typename... _ArgTypes>
  596. const type_info&
  597. function<_Res(_ArgTypes...)>::
  598. target_type() const noexcept
  599. {
  600. if (_M_manager)
  601. {
  602. _Any_data __typeinfo_result;
  603. _M_manager(__typeinfo_result, _M_functor, __get_type_info);
  604. return *__typeinfo_result._M_access<const type_info*>();
  605. }
  606. else
  607. return typeid(void);
  608. }
  609. template<typename _Res, typename... _ArgTypes>
  610. template<typename _Functor>
  611. _Functor*
  612. function<_Res(_ArgTypes...)>::
  613. target() noexcept
  614. {
  615. const function* __const_this = this;
  616. const _Functor* __func = __const_this->template target<_Functor>();
  617. return const_cast<_Functor*>(__func);
  618. }
  619. template<typename _Res, typename... _ArgTypes>
  620. template<typename _Functor>
  621. const _Functor*
  622. function<_Res(_ArgTypes...)>::
  623. target() const noexcept
  624. {
  625. if (typeid(_Functor) == target_type() && _M_manager)
  626. {
  627. _Any_data __ptr;
  628. _M_manager(__ptr, _M_functor, __get_functor_ptr);
  629. return __ptr._M_access<const _Functor*>();
  630. }
  631. else
  632. return nullptr;
  633. }
  634. #endif
  635. // [20.7.15.2.6] null pointer comparisons
  636. /**
  637. * @brief Compares a polymorphic function object wrapper against 0
  638. * (the NULL pointer).
  639. * @returns @c true if the wrapper has no target, @c false otherwise
  640. *
  641. * This function will not throw an %exception.
  642. */
  643. template<typename _Res, typename... _Args>
  644. inline bool
  645. operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
  646. { return !static_cast<bool>(__f); }
  647. /// @overload
  648. template<typename _Res, typename... _Args>
  649. inline bool
  650. operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
  651. { return !static_cast<bool>(__f); }
  652. /**
  653. * @brief Compares a polymorphic function object wrapper against 0
  654. * (the NULL pointer).
  655. * @returns @c false if the wrapper has no target, @c true otherwise
  656. *
  657. * This function will not throw an %exception.
  658. */
  659. template<typename _Res, typename... _Args>
  660. inline bool
  661. operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
  662. { return static_cast<bool>(__f); }
  663. /// @overload
  664. template<typename _Res, typename... _Args>
  665. inline bool
  666. operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
  667. { return static_cast<bool>(__f); }
  668. // [20.7.15.2.7] specialized algorithms
  669. /**
  670. * @brief Swap the targets of two polymorphic function object wrappers.
  671. *
  672. * This function will not throw an %exception.
  673. */
  674. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  675. // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
  676. template<typename _Res, typename... _Args>
  677. inline void
  678. swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
  679. { __x.swap(__y); }
  680. _GLIBCXX_END_NAMESPACE_VERSION
  681. } // namespace std
  682. #endif // C++11
  683. #endif // _GLIBCXX_STD_FUNCTION_H