std_function.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. // Implementation of std::function -*- C++ -*-
  2. // Copyright (C) 2004-2021 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() { return &_M_pod_data[0]; }
  70. const void* _M_access() const { return &_M_pod_data[0]; }
  71. template<typename _Tp>
  72. _Tp&
  73. _M_access()
  74. { return *static_cast<_Tp*>(_M_access()); }
  75. template<typename _Tp>
  76. const _Tp&
  77. _M_access() const
  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. typedef integral_constant<bool, __stored_locally> _Local_storage;
  107. // Retrieve a pointer to the function object
  108. static _Functor*
  109. _M_get_pointer(const _Any_data& __source)
  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. // Clone a location-invariant function object that fits within
  120. // an _Any_data structure.
  121. static void
  122. _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
  123. {
  124. ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
  125. }
  126. // Clone a function object that is not location-invariant or
  127. // that cannot fit into an _Any_data structure.
  128. static void
  129. _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
  130. {
  131. __dest._M_access<_Functor*>() =
  132. new _Functor(*__source._M_access<const _Functor*>());
  133. }
  134. // Destroying a location-invariant object may still require
  135. // destruction.
  136. static void
  137. _M_destroy(_Any_data& __victim, true_type)
  138. {
  139. __victim._M_access<_Functor>().~_Functor();
  140. }
  141. // Destroying an object located on the heap.
  142. static void
  143. _M_destroy(_Any_data& __victim, false_type)
  144. {
  145. delete __victim._M_access<_Functor*>();
  146. }
  147. public:
  148. static bool
  149. _M_manager(_Any_data& __dest, const _Any_data& __source,
  150. _Manager_operation __op)
  151. {
  152. switch (__op)
  153. {
  154. case __get_type_info:
  155. #if __cpp_rtti
  156. __dest._M_access<const type_info*>() = &typeid(_Functor);
  157. #else
  158. __dest._M_access<const type_info*>() = nullptr;
  159. #endif
  160. break;
  161. case __get_functor_ptr:
  162. __dest._M_access<_Functor*>() = _M_get_pointer(__source);
  163. break;
  164. case __clone_functor:
  165. _M_clone(__dest, __source, _Local_storage());
  166. break;
  167. case __destroy_functor:
  168. _M_destroy(__dest, _Local_storage());
  169. break;
  170. }
  171. return false;
  172. }
  173. static void
  174. _M_init_functor(_Any_data& __functor, _Functor&& __f)
  175. { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
  176. template<typename _Signature>
  177. static bool
  178. _M_not_empty_function(const function<_Signature>& __f)
  179. { return static_cast<bool>(__f); }
  180. template<typename _Tp>
  181. static bool
  182. _M_not_empty_function(_Tp* __fp)
  183. { return __fp != nullptr; }
  184. template<typename _Class, typename _Tp>
  185. static bool
  186. _M_not_empty_function(_Tp _Class::* __mp)
  187. { return __mp != nullptr; }
  188. template<typename _Tp>
  189. static bool
  190. _M_not_empty_function(const _Tp&)
  191. { return true; }
  192. private:
  193. static void
  194. _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
  195. { ::new (__functor._M_access()) _Functor(std::move(__f)); }
  196. static void
  197. _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
  198. { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
  199. };
  200. _Function_base() : _M_manager(nullptr) { }
  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. typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
  208. _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. typedef _Function_base::_Base_manager<_Functor> _Base;
  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. };
  246. // Specialization for invalid types
  247. template<>
  248. class _Function_handler<void, void>
  249. {
  250. public:
  251. static bool
  252. _M_manager(_Any_data&, const _Any_data&, _Manager_operation)
  253. { return false; }
  254. };
  255. // Avoids instantiating ill-formed specializations of _Function_handler
  256. // in std::function<_Signature>::target<_Functor>().
  257. // e.g. _Function_handler<Sig, void()> and _Function_handler<Sig, void>
  258. // would be ill-formed.
  259. template<typename _Signature, typename _Functor,
  260. bool __valid = is_object<_Functor>::value>
  261. struct _Target_handler
  262. : _Function_handler<_Signature, typename remove_cv<_Functor>::type>
  263. { };
  264. template<typename _Signature, typename _Functor>
  265. struct _Target_handler<_Signature, _Functor, false>
  266. : _Function_handler<void, void>
  267. { };
  268. /**
  269. * @brief Primary class template for std::function.
  270. * @ingroup functors
  271. *
  272. * Polymorphic function wrapper.
  273. */
  274. template<typename _Res, typename... _ArgTypes>
  275. class function<_Res(_ArgTypes...)>
  276. : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
  277. private _Function_base
  278. {
  279. template<typename _Func,
  280. typename _Res2 = __invoke_result<_Func&, _ArgTypes...>>
  281. struct _Callable
  282. : __is_invocable_impl<_Res2, _Res>::type
  283. { };
  284. // Used so the return type convertibility checks aren't done when
  285. // performing overload resolution for copy construction/assignment.
  286. template<typename _Tp>
  287. struct _Callable<function, _Tp> : false_type { };
  288. template<typename _Cond, typename _Tp>
  289. using _Requires = typename enable_if<_Cond::value, _Tp>::type;
  290. public:
  291. typedef _Res result_type;
  292. // [3.7.2.1] construct/copy/destroy
  293. /**
  294. * @brief Default construct creates an empty function call wrapper.
  295. * @post @c !(bool)*this
  296. */
  297. function() noexcept
  298. : _Function_base() { }
  299. /**
  300. * @brief Creates an empty function call wrapper.
  301. * @post @c !(bool)*this
  302. */
  303. function(nullptr_t) noexcept
  304. : _Function_base() { }
  305. /**
  306. * @brief %Function copy constructor.
  307. * @param __x A %function object with identical call signature.
  308. * @post @c bool(*this) == bool(__x)
  309. *
  310. * The newly-created %function contains a copy of the target of @a
  311. * __x (if it has one).
  312. */
  313. function(const function& __x)
  314. : _Function_base()
  315. {
  316. if (static_cast<bool>(__x))
  317. {
  318. __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
  319. _M_invoker = __x._M_invoker;
  320. _M_manager = __x._M_manager;
  321. }
  322. }
  323. /**
  324. * @brief %Function move constructor.
  325. * @param __x A %function object rvalue with identical call signature.
  326. *
  327. * The newly-created %function contains the target of @a __x
  328. * (if it has one).
  329. */
  330. function(function&& __x) noexcept
  331. : _Function_base()
  332. { __x.swap(*this); }
  333. /**
  334. * @brief Builds a %function that targets a copy of the incoming
  335. * function object.
  336. * @param __f A %function object that is callable with parameters of
  337. * type @c T1, @c T2, ..., @c TN and returns a value convertible
  338. * to @c Res.
  339. *
  340. * The newly-created %function object will target a copy of
  341. * @a __f. If @a __f is @c reference_wrapper<F>, then this function
  342. * object will contain a reference to the function object @c
  343. * __f.get(). If @a __f is a NULL function pointer or NULL
  344. * pointer-to-member, the newly-created object will be empty.
  345. *
  346. * If @a __f is a non-NULL function pointer or an object of type @c
  347. * reference_wrapper<F>, this function will not throw.
  348. */
  349. template<typename _Functor,
  350. typename = _Requires<__not_<is_same<_Functor, function>>, void>,
  351. typename = _Requires<_Callable<_Functor>, void>>
  352. function(_Functor __f)
  353. : _Function_base()
  354. {
  355. typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler;
  356. if (_My_handler::_M_not_empty_function(__f))
  357. {
  358. _My_handler::_M_init_functor(_M_functor, std::move(__f));
  359. _M_invoker = &_My_handler::_M_invoke;
  360. _M_manager = &_My_handler::_M_manager;
  361. }
  362. }
  363. /**
  364. * @brief %Function assignment operator.
  365. * @param __x A %function with identical call signature.
  366. * @post @c (bool)*this == (bool)x
  367. * @returns @c *this
  368. *
  369. * The target of @a __x is copied to @c *this. If @a __x has no
  370. * target, then @c *this will be empty.
  371. *
  372. * If @a __x targets a function pointer or a reference to a function
  373. * object, then this operation will not throw an %exception.
  374. */
  375. function&
  376. operator=(const function& __x)
  377. {
  378. function(__x).swap(*this);
  379. return *this;
  380. }
  381. /**
  382. * @brief %Function move-assignment operator.
  383. * @param __x A %function rvalue with identical call signature.
  384. * @returns @c *this
  385. *
  386. * The target of @a __x is moved 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=(function&& __x) noexcept
  394. {
  395. function(std::move(__x)).swap(*this);
  396. return *this;
  397. }
  398. /**
  399. * @brief %Function assignment to zero.
  400. * @post @c !(bool)*this
  401. * @returns @c *this
  402. *
  403. * The target of @c *this is deallocated, leaving it empty.
  404. */
  405. function&
  406. operator=(nullptr_t) noexcept
  407. {
  408. if (_M_manager)
  409. {
  410. _M_manager(_M_functor, _M_functor, __destroy_functor);
  411. _M_manager = nullptr;
  412. _M_invoker = nullptr;
  413. }
  414. return *this;
  415. }
  416. /**
  417. * @brief %Function assignment to a new target.
  418. * @param __f A %function object that is callable with parameters of
  419. * type @c T1, @c T2, ..., @c TN and returns a value convertible
  420. * to @c Res.
  421. * @return @c *this
  422. *
  423. * This %function object wrapper will target a copy of @a
  424. * __f. If @a __f is @c reference_wrapper<F>, then this function
  425. * object will contain a reference to the function object @c
  426. * __f.get(). If @a __f is a NULL function pointer or NULL
  427. * pointer-to-member, @c this object will be empty.
  428. *
  429. * If @a __f is a non-NULL function pointer or an object of type @c
  430. * reference_wrapper<F>, this function will not throw.
  431. */
  432. template<typename _Functor>
  433. _Requires<_Callable<typename decay<_Functor>::type>, function&>
  434. operator=(_Functor&& __f)
  435. {
  436. function(std::forward<_Functor>(__f)).swap(*this);
  437. return *this;
  438. }
  439. /// @overload
  440. template<typename _Functor>
  441. function&
  442. operator=(reference_wrapper<_Functor> __f) noexcept
  443. {
  444. function(__f).swap(*this);
  445. return *this;
  446. }
  447. // [3.7.2.2] function modifiers
  448. /**
  449. * @brief Swap the targets of two %function objects.
  450. * @param __x A %function with identical call signature.
  451. *
  452. * Swap the targets of @c this function object and @a __f. This
  453. * function will not throw an %exception.
  454. */
  455. void swap(function& __x) noexcept
  456. {
  457. std::swap(_M_functor, __x._M_functor);
  458. std::swap(_M_manager, __x._M_manager);
  459. std::swap(_M_invoker, __x._M_invoker);
  460. }
  461. // [3.7.2.3] function capacity
  462. /**
  463. * @brief Determine if the %function wrapper has a target.
  464. *
  465. * @return @c true when this %function object contains a target,
  466. * or @c false when it is empty.
  467. *
  468. * This function will not throw an %exception.
  469. */
  470. explicit operator bool() const noexcept
  471. { return !_M_empty(); }
  472. // [3.7.2.4] function invocation
  473. /**
  474. * @brief Invokes the function targeted by @c *this.
  475. * @returns the result of the target.
  476. * @throws bad_function_call when @c !(bool)*this
  477. *
  478. * The function call operator invokes the target function object
  479. * stored by @c this.
  480. */
  481. _Res
  482. operator()(_ArgTypes... __args) const
  483. {
  484. if (_M_empty())
  485. __throw_bad_function_call();
  486. return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
  487. }
  488. #if __cpp_rtti
  489. // [3.7.2.5] function target access
  490. /**
  491. * @brief Determine the type of the target of this function object
  492. * wrapper.
  493. *
  494. * @returns the type identifier of the target function object, or
  495. * @c typeid(void) if @c !(bool)*this.
  496. *
  497. * This function will not throw an %exception.
  498. */
  499. const type_info&
  500. target_type() const noexcept
  501. {
  502. if (_M_manager)
  503. {
  504. _Any_data __typeinfo_result;
  505. _M_manager(__typeinfo_result, _M_functor, __get_type_info);
  506. if (auto __ti = __typeinfo_result._M_access<const type_info*>())
  507. return *__ti;
  508. }
  509. return typeid(void);
  510. }
  511. #endif
  512. /**
  513. * @brief Access the stored target function object.
  514. *
  515. * @return Returns a pointer to the stored target function object,
  516. * if @c typeid(_Functor).equals(target_type()); otherwise, a null
  517. * pointer.
  518. *
  519. * This function does not throw exceptions.
  520. *
  521. * @{
  522. */
  523. template<typename _Functor>
  524. _Functor*
  525. target() noexcept
  526. {
  527. const function* __const_this = this;
  528. const _Functor* __func = __const_this->template target<_Functor>();
  529. // If is_function_v<_Functor> is true then const_cast<_Functor*>
  530. // would be ill-formed, so use *const_cast<_Functor**> instead.
  531. return *const_cast<_Functor**>(&__func);
  532. }
  533. template<typename _Functor>
  534. const _Functor*
  535. target() const noexcept
  536. {
  537. if _GLIBCXX17_CONSTEXPR (is_object<_Functor>::value)
  538. {
  539. // For C++11 and C++14 if-constexpr is not used above, so
  540. // _Target_handler avoids ill-formed _Function_handler types.
  541. using _Handler = _Target_handler<_Res(_ArgTypes...), _Functor>;
  542. if (_M_manager == &_Handler::_M_manager
  543. #if __cpp_rtti
  544. || (_M_manager && typeid(_Functor) == target_type())
  545. #endif
  546. )
  547. {
  548. _Any_data __ptr;
  549. _M_manager(__ptr, _M_functor, __get_functor_ptr);
  550. return __ptr._M_access<const _Functor*>();
  551. }
  552. }
  553. return nullptr;
  554. }
  555. /// @}
  556. private:
  557. using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
  558. _Invoker_type _M_invoker;
  559. };
  560. #if __cpp_deduction_guides >= 201606
  561. template<typename>
  562. struct __function_guide_helper
  563. { };
  564. template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
  565. struct __function_guide_helper<
  566. _Res (_Tp::*) (_Args...) noexcept(_Nx)
  567. >
  568. { using type = _Res(_Args...); };
  569. template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
  570. struct __function_guide_helper<
  571. _Res (_Tp::*) (_Args...) & noexcept(_Nx)
  572. >
  573. { using type = _Res(_Args...); };
  574. template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
  575. struct __function_guide_helper<
  576. _Res (_Tp::*) (_Args...) const noexcept(_Nx)
  577. >
  578. { using type = _Res(_Args...); };
  579. template<typename _Res, typename _Tp, bool _Nx, typename... _Args>
  580. struct __function_guide_helper<
  581. _Res (_Tp::*) (_Args...) const & noexcept(_Nx)
  582. >
  583. { using type = _Res(_Args...); };
  584. template<typename _Res, typename... _ArgTypes>
  585. function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
  586. template<typename _Functor, typename _Signature = typename
  587. __function_guide_helper<decltype(&_Functor::operator())>::type>
  588. function(_Functor) -> function<_Signature>;
  589. #endif
  590. // [20.7.15.2.6] null pointer comparisons
  591. /**
  592. * @brief Compares a polymorphic function object wrapper against 0
  593. * (the NULL pointer).
  594. * @returns @c true if the wrapper has no target, @c false otherwise
  595. *
  596. * This function will not throw an %exception.
  597. */
  598. template<typename _Res, typename... _Args>
  599. inline bool
  600. operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
  601. { return !static_cast<bool>(__f); }
  602. #if __cpp_impl_three_way_comparison < 201907L
  603. /// @overload
  604. template<typename _Res, typename... _Args>
  605. inline bool
  606. operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
  607. { return !static_cast<bool>(__f); }
  608. /**
  609. * @brief Compares a polymorphic function object wrapper against 0
  610. * (the NULL pointer).
  611. * @returns @c false if the wrapper has no target, @c true otherwise
  612. *
  613. * This function will not throw an %exception.
  614. */
  615. template<typename _Res, typename... _Args>
  616. inline bool
  617. operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
  618. { return static_cast<bool>(__f); }
  619. /// @overload
  620. template<typename _Res, typename... _Args>
  621. inline bool
  622. operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
  623. { return static_cast<bool>(__f); }
  624. #endif
  625. // [20.7.15.2.7] specialized algorithms
  626. /**
  627. * @brief Swap the targets of two polymorphic function object wrappers.
  628. *
  629. * This function will not throw an %exception.
  630. */
  631. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  632. // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
  633. template<typename _Res, typename... _Args>
  634. inline void
  635. swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
  636. { __x.swap(__y); }
  637. #if __cplusplus >= 201703L
  638. namespace __detail::__variant
  639. {
  640. template<typename> struct _Never_valueless_alt; // see <variant>
  641. // Provide the strong exception-safety guarantee when emplacing a
  642. // function into a variant.
  643. template<typename _Signature>
  644. struct _Never_valueless_alt<std::function<_Signature>>
  645. : std::true_type
  646. { };
  647. } // namespace __detail::__variant
  648. #endif // C++17
  649. _GLIBCXX_END_NAMESPACE_VERSION
  650. } // namespace std
  651. #endif // C++11
  652. #endif // _GLIBCXX_STD_FUNCTION_H