std_function.h 22 KB

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