functional 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  1. // <functional> -*- C++ -*-
  2. // Copyright (C) 2001-2016 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. /*
  21. * Copyright (c) 1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. *
  32. */
  33. /** @file include/functional
  34. * This is a Standard C++ Library header.
  35. */
  36. #ifndef _GLIBCXX_FUNCTIONAL
  37. #define _GLIBCXX_FUNCTIONAL 1
  38. #pragma GCC system_header
  39. #include <bits/c++config.h>
  40. #include <bits/stl_function.h>
  41. #if __cplusplus >= 201103L
  42. #include <typeinfo>
  43. #include <new>
  44. #include <tuple>
  45. #include <type_traits>
  46. #include <bits/functexcept.h>
  47. #include <bits/functional_hash.h>
  48. namespace std _GLIBCXX_VISIBILITY(default)
  49. {
  50. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  51. template<typename _MemberPointer>
  52. class _Mem_fn;
  53. template<typename _Tp, typename _Class>
  54. _Mem_fn<_Tp _Class::*>
  55. mem_fn(_Tp _Class::*) noexcept;
  56. /// If we have found a result_type, extract it.
  57. template<typename _Functor, typename = __void_t<>>
  58. struct _Maybe_get_result_type
  59. { };
  60. template<typename _Functor>
  61. struct _Maybe_get_result_type<_Functor,
  62. __void_t<typename _Functor::result_type>>
  63. { typedef typename _Functor::result_type result_type; };
  64. /**
  65. * Base class for any function object that has a weak result type, as
  66. * defined in 20.8.2 [func.require] of C++11.
  67. */
  68. template<typename _Functor>
  69. struct _Weak_result_type_impl
  70. : _Maybe_get_result_type<_Functor>
  71. { };
  72. /// Retrieve the result type for a function type.
  73. template<typename _Res, typename... _ArgTypes>
  74. struct _Weak_result_type_impl<_Res(_ArgTypes...)>
  75. { typedef _Res result_type; };
  76. template<typename _Res, typename... _ArgTypes>
  77. struct _Weak_result_type_impl<_Res(_ArgTypes......)>
  78. { typedef _Res result_type; };
  79. template<typename _Res, typename... _ArgTypes>
  80. struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
  81. { typedef _Res result_type; };
  82. template<typename _Res, typename... _ArgTypes>
  83. struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
  84. { typedef _Res result_type; };
  85. template<typename _Res, typename... _ArgTypes>
  86. struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
  87. { typedef _Res result_type; };
  88. template<typename _Res, typename... _ArgTypes>
  89. struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
  90. { typedef _Res result_type; };
  91. template<typename _Res, typename... _ArgTypes>
  92. struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
  93. { typedef _Res result_type; };
  94. template<typename _Res, typename... _ArgTypes>
  95. struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
  96. { typedef _Res result_type; };
  97. /// Retrieve the result type for a function reference.
  98. template<typename _Res, typename... _ArgTypes>
  99. struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
  100. { typedef _Res result_type; };
  101. template<typename _Res, typename... _ArgTypes>
  102. struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
  103. { typedef _Res result_type; };
  104. /// Retrieve the result type for a function pointer.
  105. template<typename _Res, typename... _ArgTypes>
  106. struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
  107. { typedef _Res result_type; };
  108. template<typename _Res, typename... _ArgTypes>
  109. struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
  110. { typedef _Res result_type; };
  111. /// Retrieve result type for a member function pointer.
  112. template<typename _Res, typename _Class, typename... _ArgTypes>
  113. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
  114. { typedef _Res result_type; };
  115. template<typename _Res, typename _Class, typename... _ArgTypes>
  116. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
  117. { typedef _Res result_type; };
  118. /// Retrieve result type for a const member function pointer.
  119. template<typename _Res, typename _Class, typename... _ArgTypes>
  120. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
  121. { typedef _Res result_type; };
  122. template<typename _Res, typename _Class, typename... _ArgTypes>
  123. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
  124. { typedef _Res result_type; };
  125. /// Retrieve result type for a volatile member function pointer.
  126. template<typename _Res, typename _Class, typename... _ArgTypes>
  127. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
  128. { typedef _Res result_type; };
  129. template<typename _Res, typename _Class, typename... _ArgTypes>
  130. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
  131. { typedef _Res result_type; };
  132. /// Retrieve result type for a const volatile member function pointer.
  133. template<typename _Res, typename _Class, typename... _ArgTypes>
  134. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
  135. const volatile>
  136. { typedef _Res result_type; };
  137. template<typename _Res, typename _Class, typename... _ArgTypes>
  138. struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
  139. const volatile>
  140. { typedef _Res result_type; };
  141. /**
  142. * Strip top-level cv-qualifiers from the function object and let
  143. * _Weak_result_type_impl perform the real work.
  144. */
  145. template<typename _Functor>
  146. struct _Weak_result_type
  147. : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
  148. { };
  149. template<typename _Tp, typename _Up = typename decay<_Tp>::type>
  150. struct _Unwrap
  151. {
  152. using type = _Tp&&;
  153. // Equivalent to std::forward<_Tp>
  154. static constexpr _Tp&&
  155. _S_fwd(_Tp& __t) noexcept { return static_cast<_Tp&&>(__t); }
  156. };
  157. template<typename _Tp, typename _Up>
  158. struct _Unwrap<_Tp, reference_wrapper<_Up>>
  159. {
  160. using type = _Up&;
  161. // Get an lvalue-reference from a reference_wrapper.
  162. static _Up&
  163. _S_fwd(const _Tp& __t) noexcept { __t.get(); }
  164. };
  165. // Used by __invoke_impl instead of std::forward<_Tp> so that a
  166. // reference_wrapper is converted to an lvalue-reference.
  167. template<typename _Tp>
  168. inline typename _Unwrap<_Tp>::type
  169. __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
  170. { return _Unwrap<_Tp>::_S_fwd(__t); }
  171. template<typename _Res, typename _Fn, typename... _Args>
  172. inline _Res
  173. __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
  174. noexcept(noexcept(std::forward<_Fn>(__f)(std::forward<_Args>(__args)...)))
  175. { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
  176. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  177. inline _Res
  178. __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
  179. _Args&&... __args)
  180. noexcept(noexcept(
  181. (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...)))
  182. { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
  183. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  184. inline _Res
  185. __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
  186. _Args&&... __args)
  187. noexcept(noexcept(
  188. ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...)))
  189. {
  190. return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
  191. }
  192. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  193. inline _Res
  194. __invoke_impl(__invoke_memobj_ref, _MemFun&& __f, _Tp&& __t)
  195. noexcept(noexcept(__invfwd<_Tp>(__t).*__f))
  196. { return __invfwd<_Tp>(__t).*__f; }
  197. template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  198. inline _Res
  199. __invoke_impl(__invoke_memobj_deref, _MemFun&& __f, _Tp&& __t,
  200. _Args&&... __args)
  201. noexcept(noexcept((*std::forward<_Tp>(__t)).*__f))
  202. { return (*std::forward<_Tp>(__t)).*__f; }
  203. /// Invoke a callable object.
  204. template<typename _Callable, typename... _Args>
  205. inline typename result_of<_Callable&&(_Args&&...)>::type
  206. __invoke(_Callable&& __fn, _Args&&... __args)
  207. {
  208. using __result_of = result_of<_Callable&&(_Args&&...)>;
  209. using __type = typename __result_of::type;
  210. using __tag = typename __result_of::__invoke_type;
  211. return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
  212. std::forward<_Args>(__args)...);
  213. }
  214. #if __cplusplus > 201402L
  215. # define __cpp_lib_invoke 201411
  216. /// Invoke a callable object.
  217. template<typename _Callable, typename... _Args>
  218. inline result_of_t<_Callable&&(_Args&&...)>
  219. invoke(_Callable&& __fn, _Args&&... __args)
  220. {
  221. return std::__invoke(std::forward<_Callable>(__fn),
  222. std::forward<_Args>(__args)...);
  223. }
  224. #endif
  225. /**
  226. * Knowing which of unary_function and binary_function _Tp derives
  227. * from, derives from the same and ensures that reference_wrapper
  228. * will have a weak result type. See cases below.
  229. */
  230. template<bool _Unary, bool _Binary, typename _Tp>
  231. struct _Reference_wrapper_base_impl;
  232. // None of the nested argument types.
  233. template<typename _Tp>
  234. struct _Reference_wrapper_base_impl<false, false, _Tp>
  235. : _Weak_result_type<_Tp>
  236. { };
  237. // Nested argument_type only.
  238. template<typename _Tp>
  239. struct _Reference_wrapper_base_impl<true, false, _Tp>
  240. : _Weak_result_type<_Tp>
  241. {
  242. typedef typename _Tp::argument_type argument_type;
  243. };
  244. // Nested first_argument_type and second_argument_type only.
  245. template<typename _Tp>
  246. struct _Reference_wrapper_base_impl<false, true, _Tp>
  247. : _Weak_result_type<_Tp>
  248. {
  249. typedef typename _Tp::first_argument_type first_argument_type;
  250. typedef typename _Tp::second_argument_type second_argument_type;
  251. };
  252. // All the nested argument types.
  253. template<typename _Tp>
  254. struct _Reference_wrapper_base_impl<true, true, _Tp>
  255. : _Weak_result_type<_Tp>
  256. {
  257. typedef typename _Tp::argument_type argument_type;
  258. typedef typename _Tp::first_argument_type first_argument_type;
  259. typedef typename _Tp::second_argument_type second_argument_type;
  260. };
  261. _GLIBCXX_HAS_NESTED_TYPE(argument_type)
  262. _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
  263. _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
  264. /**
  265. * Derives from unary_function or binary_function when it
  266. * can. Specializations handle all of the easy cases. The primary
  267. * template determines what to do with a class type, which may
  268. * derive from both unary_function and binary_function.
  269. */
  270. template<typename _Tp>
  271. struct _Reference_wrapper_base
  272. : _Reference_wrapper_base_impl<
  273. __has_argument_type<_Tp>::value,
  274. __has_first_argument_type<_Tp>::value
  275. && __has_second_argument_type<_Tp>::value,
  276. _Tp>
  277. { };
  278. // - a function type (unary)
  279. template<typename _Res, typename _T1>
  280. struct _Reference_wrapper_base<_Res(_T1)>
  281. : unary_function<_T1, _Res>
  282. { };
  283. template<typename _Res, typename _T1>
  284. struct _Reference_wrapper_base<_Res(_T1) const>
  285. : unary_function<_T1, _Res>
  286. { };
  287. template<typename _Res, typename _T1>
  288. struct _Reference_wrapper_base<_Res(_T1) volatile>
  289. : unary_function<_T1, _Res>
  290. { };
  291. template<typename _Res, typename _T1>
  292. struct _Reference_wrapper_base<_Res(_T1) const volatile>
  293. : unary_function<_T1, _Res>
  294. { };
  295. // - a function type (binary)
  296. template<typename _Res, typename _T1, typename _T2>
  297. struct _Reference_wrapper_base<_Res(_T1, _T2)>
  298. : binary_function<_T1, _T2, _Res>
  299. { };
  300. template<typename _Res, typename _T1, typename _T2>
  301. struct _Reference_wrapper_base<_Res(_T1, _T2) const>
  302. : binary_function<_T1, _T2, _Res>
  303. { };
  304. template<typename _Res, typename _T1, typename _T2>
  305. struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
  306. : binary_function<_T1, _T2, _Res>
  307. { };
  308. template<typename _Res, typename _T1, typename _T2>
  309. struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
  310. : binary_function<_T1, _T2, _Res>
  311. { };
  312. // - a function pointer type (unary)
  313. template<typename _Res, typename _T1>
  314. struct _Reference_wrapper_base<_Res(*)(_T1)>
  315. : unary_function<_T1, _Res>
  316. { };
  317. // - a function pointer type (binary)
  318. template<typename _Res, typename _T1, typename _T2>
  319. struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
  320. : binary_function<_T1, _T2, _Res>
  321. { };
  322. // - a pointer to member function type (unary, no qualifiers)
  323. template<typename _Res, typename _T1>
  324. struct _Reference_wrapper_base<_Res (_T1::*)()>
  325. : unary_function<_T1*, _Res>
  326. { };
  327. // - a pointer to member function type (binary, no qualifiers)
  328. template<typename _Res, typename _T1, typename _T2>
  329. struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
  330. : binary_function<_T1*, _T2, _Res>
  331. { };
  332. // - a pointer to member function type (unary, const)
  333. template<typename _Res, typename _T1>
  334. struct _Reference_wrapper_base<_Res (_T1::*)() const>
  335. : unary_function<const _T1*, _Res>
  336. { };
  337. // - a pointer to member function type (binary, const)
  338. template<typename _Res, typename _T1, typename _T2>
  339. struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
  340. : binary_function<const _T1*, _T2, _Res>
  341. { };
  342. // - a pointer to member function type (unary, volatile)
  343. template<typename _Res, typename _T1>
  344. struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
  345. : unary_function<volatile _T1*, _Res>
  346. { };
  347. // - a pointer to member function type (binary, volatile)
  348. template<typename _Res, typename _T1, typename _T2>
  349. struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
  350. : binary_function<volatile _T1*, _T2, _Res>
  351. { };
  352. // - a pointer to member function type (unary, const volatile)
  353. template<typename _Res, typename _T1>
  354. struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
  355. : unary_function<const volatile _T1*, _Res>
  356. { };
  357. // - a pointer to member function type (binary, const volatile)
  358. template<typename _Res, typename _T1, typename _T2>
  359. struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
  360. : binary_function<const volatile _T1*, _T2, _Res>
  361. { };
  362. /**
  363. * @brief Primary class template for reference_wrapper.
  364. * @ingroup functors
  365. * @{
  366. */
  367. template<typename _Tp>
  368. class reference_wrapper
  369. : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
  370. {
  371. _Tp* _M_data;
  372. public:
  373. typedef _Tp type;
  374. reference_wrapper(_Tp& __indata) noexcept
  375. : _M_data(std::__addressof(__indata))
  376. { }
  377. reference_wrapper(_Tp&&) = delete;
  378. reference_wrapper(const reference_wrapper&) = default;
  379. reference_wrapper&
  380. operator=(const reference_wrapper&) = default;
  381. operator _Tp&() const noexcept
  382. { return this->get(); }
  383. _Tp&
  384. get() const noexcept
  385. { return *_M_data; }
  386. template<typename... _Args>
  387. typename result_of<_Tp&(_Args&&...)>::type
  388. operator()(_Args&&... __args) const
  389. {
  390. return std::__invoke(get(), std::forward<_Args>(__args)...);
  391. }
  392. };
  393. /// Denotes a reference should be taken to a variable.
  394. template<typename _Tp>
  395. inline reference_wrapper<_Tp>
  396. ref(_Tp& __t) noexcept
  397. { return reference_wrapper<_Tp>(__t); }
  398. /// Denotes a const reference should be taken to a variable.
  399. template<typename _Tp>
  400. inline reference_wrapper<const _Tp>
  401. cref(const _Tp& __t) noexcept
  402. { return reference_wrapper<const _Tp>(__t); }
  403. template<typename _Tp>
  404. void ref(const _Tp&&) = delete;
  405. template<typename _Tp>
  406. void cref(const _Tp&&) = delete;
  407. /// Partial specialization.
  408. template<typename _Tp>
  409. inline reference_wrapper<_Tp>
  410. ref(reference_wrapper<_Tp> __t) noexcept
  411. { return ref(__t.get()); }
  412. /// Partial specialization.
  413. template<typename _Tp>
  414. inline reference_wrapper<const _Tp>
  415. cref(reference_wrapper<_Tp> __t) noexcept
  416. { return cref(__t.get()); }
  417. // @} group functors
  418. template<typename... _Types>
  419. struct _Pack : integral_constant<size_t, sizeof...(_Types)>
  420. { };
  421. template<typename _From, typename _To, bool = _From::value == _To::value>
  422. struct _AllConvertible : false_type
  423. { };
  424. template<typename... _From, typename... _To>
  425. struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
  426. : __and_<is_convertible<_From, _To>...>
  427. { };
  428. template<typename _Tp1, typename _Tp2>
  429. using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
  430. typename std::decay<_Tp2>::type>>;
  431. /**
  432. * Derives from @c unary_function or @c binary_function, or perhaps
  433. * nothing, depending on the number of arguments provided. The
  434. * primary template is the basis case, which derives nothing.
  435. */
  436. template<typename _Res, typename... _ArgTypes>
  437. struct _Maybe_unary_or_binary_function { };
  438. /// Derives from @c unary_function, as appropriate.
  439. template<typename _Res, typename _T1>
  440. struct _Maybe_unary_or_binary_function<_Res, _T1>
  441. : std::unary_function<_T1, _Res> { };
  442. /// Derives from @c binary_function, as appropriate.
  443. template<typename _Res, typename _T1, typename _T2>
  444. struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
  445. : std::binary_function<_T1, _T2, _Res> { };
  446. template<typename _Signature>
  447. struct _Mem_fn_traits;
  448. template<typename _Res, typename _Class, typename... _ArgTypes>
  449. struct _Mem_fn_traits_base
  450. {
  451. using __result_type = _Res;
  452. using __maybe_type
  453. = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
  454. using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
  455. };
  456. #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
  457. template<typename _Res, typename _Class, typename... _ArgTypes> \
  458. struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
  459. : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
  460. { \
  461. using __vararg = false_type; \
  462. }; \
  463. template<typename _Res, typename _Class, typename... _ArgTypes> \
  464. struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
  465. : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
  466. { \
  467. using __vararg = true_type; \
  468. };
  469. #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
  470. _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
  471. _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
  472. _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
  473. _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
  474. _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
  475. _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
  476. _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
  477. #undef _GLIBCXX_MEM_FN_TRAITS
  478. #undef _GLIBCXX_MEM_FN_TRAITS2
  479. template<typename _MemFunPtr,
  480. bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
  481. class _Mem_fn_base
  482. : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
  483. {
  484. using _Traits = _Mem_fn_traits<_MemFunPtr>;
  485. using _Arity = typename _Traits::__arity;
  486. using _Varargs = typename _Traits::__vararg;
  487. template<typename _Func, typename... _BoundArgs>
  488. friend struct _Bind_check_arity;
  489. _MemFunPtr _M_pmf;
  490. public:
  491. using result_type = typename _Traits::__result_type;
  492. explicit constexpr
  493. _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
  494. template<typename... _Args>
  495. auto
  496. operator()(_Args&&... __args) const
  497. noexcept(noexcept(
  498. std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
  499. -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
  500. { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
  501. };
  502. // Partial specialization for member object pointers.
  503. template<typename _MemObjPtr>
  504. class _Mem_fn_base<_MemObjPtr, false>
  505. {
  506. using _Arity = integral_constant<size_t, 0>;
  507. using _Varargs = false_type;
  508. template<typename _Func, typename... _BoundArgs>
  509. friend struct _Bind_check_arity;
  510. _MemObjPtr _M_pm;
  511. public:
  512. explicit constexpr
  513. _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
  514. template<typename _Tp>
  515. auto
  516. operator()(_Tp&& __obj) const
  517. noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
  518. -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
  519. { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
  520. };
  521. template<typename _Res, typename _Class>
  522. struct _Mem_fn<_Res _Class::*>
  523. : _Mem_fn_base<_Res _Class::*>
  524. {
  525. using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
  526. };
  527. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  528. // 2048. Unnecessary mem_fn overloads
  529. /**
  530. * @brief Returns a function object that forwards to the member
  531. * pointer @a pm.
  532. * @ingroup functors
  533. */
  534. template<typename _Tp, typename _Class>
  535. inline _Mem_fn<_Tp _Class::*>
  536. mem_fn(_Tp _Class::* __pm) noexcept
  537. {
  538. return _Mem_fn<_Tp _Class::*>(__pm);
  539. }
  540. /**
  541. * @brief Determines if the given type _Tp is a function object that
  542. * should be treated as a subexpression when evaluating calls to
  543. * function objects returned by bind().
  544. *
  545. * C++11 [func.bind.isbind].
  546. * @ingroup binders
  547. */
  548. template<typename _Tp>
  549. struct is_bind_expression
  550. : public false_type { };
  551. /**
  552. * @brief Determines if the given type _Tp is a placeholder in a
  553. * bind() expression and, if so, which placeholder it is.
  554. *
  555. * C++11 [func.bind.isplace].
  556. * @ingroup binders
  557. */
  558. template<typename _Tp>
  559. struct is_placeholder
  560. : public integral_constant<int, 0>
  561. { };
  562. /** @brief The type of placeholder objects defined by libstdc++.
  563. * @ingroup binders
  564. */
  565. template<int _Num> struct _Placeholder { };
  566. _GLIBCXX_END_NAMESPACE_VERSION
  567. /** @namespace std::placeholders
  568. * @brief ISO C++11 entities sub-namespace for functional.
  569. * @ingroup binders
  570. */
  571. namespace placeholders
  572. {
  573. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  574. /* Define a large number of placeholders. There is no way to
  575. * simplify this with variadic templates, because we're introducing
  576. * unique names for each.
  577. */
  578. extern const _Placeholder<1> _1;
  579. extern const _Placeholder<2> _2;
  580. extern const _Placeholder<3> _3;
  581. extern const _Placeholder<4> _4;
  582. extern const _Placeholder<5> _5;
  583. extern const _Placeholder<6> _6;
  584. extern const _Placeholder<7> _7;
  585. extern const _Placeholder<8> _8;
  586. extern const _Placeholder<9> _9;
  587. extern const _Placeholder<10> _10;
  588. extern const _Placeholder<11> _11;
  589. extern const _Placeholder<12> _12;
  590. extern const _Placeholder<13> _13;
  591. extern const _Placeholder<14> _14;
  592. extern const _Placeholder<15> _15;
  593. extern const _Placeholder<16> _16;
  594. extern const _Placeholder<17> _17;
  595. extern const _Placeholder<18> _18;
  596. extern const _Placeholder<19> _19;
  597. extern const _Placeholder<20> _20;
  598. extern const _Placeholder<21> _21;
  599. extern const _Placeholder<22> _22;
  600. extern const _Placeholder<23> _23;
  601. extern const _Placeholder<24> _24;
  602. extern const _Placeholder<25> _25;
  603. extern const _Placeholder<26> _26;
  604. extern const _Placeholder<27> _27;
  605. extern const _Placeholder<28> _28;
  606. extern const _Placeholder<29> _29;
  607. _GLIBCXX_END_NAMESPACE_VERSION
  608. }
  609. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  610. /**
  611. * Partial specialization of is_placeholder that provides the placeholder
  612. * number for the placeholder objects defined by libstdc++.
  613. * @ingroup binders
  614. */
  615. template<int _Num>
  616. struct is_placeholder<_Placeholder<_Num> >
  617. : public integral_constant<int, _Num>
  618. { };
  619. template<int _Num>
  620. struct is_placeholder<const _Placeholder<_Num> >
  621. : public integral_constant<int, _Num>
  622. { };
  623. // Like tuple_element_t but SFINAE-friendly.
  624. template<std::size_t __i, typename _Tuple>
  625. using _Safe_tuple_element_t
  626. = typename enable_if<(__i < tuple_size<_Tuple>::value),
  627. tuple_element<__i, _Tuple>>::type::type;
  628. /**
  629. * Maps an argument to bind() into an actual argument to the bound
  630. * function object [func.bind.bind]/10. Only the first parameter should
  631. * be specified: the rest are used to determine among the various
  632. * implementations. Note that, although this class is a function
  633. * object, it isn't entirely normal because it takes only two
  634. * parameters regardless of the number of parameters passed to the
  635. * bind expression. The first parameter is the bound argument and
  636. * the second parameter is a tuple containing references to the
  637. * rest of the arguments.
  638. */
  639. template<typename _Arg,
  640. bool _IsBindExp = is_bind_expression<_Arg>::value,
  641. bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
  642. class _Mu;
  643. /**
  644. * If the argument is reference_wrapper<_Tp>, returns the
  645. * underlying reference.
  646. * C++11 [func.bind.bind] p10 bullet 1.
  647. */
  648. template<typename _Tp>
  649. class _Mu<reference_wrapper<_Tp>, false, false>
  650. {
  651. public:
  652. /* Note: This won't actually work for const volatile
  653. * reference_wrappers, because reference_wrapper::get() is const
  654. * but not volatile-qualified. This might be a defect in the TR.
  655. */
  656. template<typename _CVRef, typename _Tuple>
  657. _Tp&
  658. operator()(_CVRef& __arg, _Tuple&) const volatile
  659. { return __arg.get(); }
  660. };
  661. /**
  662. * If the argument is a bind expression, we invoke the underlying
  663. * function object with the same cv-qualifiers as we are given and
  664. * pass along all of our arguments (unwrapped).
  665. * C++11 [func.bind.bind] p10 bullet 2.
  666. */
  667. template<typename _Arg>
  668. class _Mu<_Arg, true, false>
  669. {
  670. public:
  671. template<typename _CVArg, typename... _Args>
  672. auto
  673. operator()(_CVArg& __arg,
  674. tuple<_Args...>& __tuple) const volatile
  675. -> decltype(__arg(declval<_Args>()...))
  676. {
  677. // Construct an index tuple and forward to __call
  678. typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
  679. _Indexes;
  680. return this->__call(__arg, __tuple, _Indexes());
  681. }
  682. private:
  683. // Invokes the underlying function object __arg by unpacking all
  684. // of the arguments in the tuple.
  685. template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
  686. auto
  687. __call(_CVArg& __arg, tuple<_Args...>& __tuple,
  688. const _Index_tuple<_Indexes...>&) const volatile
  689. -> decltype(__arg(declval<_Args>()...))
  690. {
  691. return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...);
  692. }
  693. };
  694. /**
  695. * If the argument is a placeholder for the Nth argument, returns
  696. * a reference to the Nth argument to the bind function object.
  697. * C++11 [func.bind.bind] p10 bullet 3.
  698. */
  699. template<typename _Arg>
  700. class _Mu<_Arg, false, true>
  701. {
  702. public:
  703. template<typename _Tuple>
  704. _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
  705. operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
  706. {
  707. using __type
  708. = __tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>;
  709. return std::forward<__type>(
  710. ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
  711. }
  712. };
  713. /**
  714. * If the argument is just a value, returns a reference to that
  715. * value. The cv-qualifiers on the reference are determined by the caller.
  716. * C++11 [func.bind.bind] p10 bullet 4.
  717. */
  718. template<typename _Arg>
  719. class _Mu<_Arg, false, false>
  720. {
  721. public:
  722. template<typename _CVArg, typename _Tuple>
  723. _CVArg&&
  724. operator()(_CVArg&& __arg, _Tuple&) const volatile
  725. { return std::forward<_CVArg>(__arg); }
  726. };
  727. /**
  728. * Maps member pointers into instances of _Mem_fn but leaves all
  729. * other function objects untouched. Used by std::bind(). The
  730. * primary template handles the non-member-pointer case.
  731. */
  732. template<typename _Tp>
  733. struct _Maybe_wrap_member_pointer
  734. {
  735. typedef _Tp type;
  736. static constexpr const _Tp&
  737. __do_wrap(const _Tp& __x)
  738. { return __x; }
  739. static constexpr _Tp&&
  740. __do_wrap(_Tp&& __x)
  741. { return static_cast<_Tp&&>(__x); }
  742. };
  743. /**
  744. * Maps member pointers into instances of _Mem_fn but leaves all
  745. * other function objects untouched. Used by std::bind(). This
  746. * partial specialization handles the member pointer case.
  747. */
  748. template<typename _Tp, typename _Class>
  749. struct _Maybe_wrap_member_pointer<_Tp _Class::*>
  750. {
  751. typedef _Mem_fn<_Tp _Class::*> type;
  752. static constexpr type
  753. __do_wrap(_Tp _Class::* __pm)
  754. { return type(__pm); }
  755. };
  756. // Specialization needed to prevent "forming reference to void" errors when
  757. // bind<void>() is called, because argument deduction instantiates
  758. // _Maybe_wrap_member_pointer<void> outside the immediate context where
  759. // SFINAE applies.
  760. template<>
  761. struct _Maybe_wrap_member_pointer<void>
  762. {
  763. typedef void type;
  764. };
  765. // std::get<I> for volatile-qualified tuples
  766. template<std::size_t _Ind, typename... _Tp>
  767. inline auto
  768. __volget(volatile tuple<_Tp...>& __tuple)
  769. -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
  770. { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
  771. // std::get<I> for const-volatile-qualified tuples
  772. template<std::size_t _Ind, typename... _Tp>
  773. inline auto
  774. __volget(const volatile tuple<_Tp...>& __tuple)
  775. -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
  776. { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
  777. /// Type of the function object returned from bind().
  778. template<typename _Signature>
  779. struct _Bind;
  780. template<typename _Functor, typename... _Bound_args>
  781. class _Bind<_Functor(_Bound_args...)>
  782. : public _Weak_result_type<_Functor>
  783. {
  784. typedef _Bind __self_type;
  785. typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
  786. _Bound_indexes;
  787. _Functor _M_f;
  788. tuple<_Bound_args...> _M_bound_args;
  789. // Call unqualified
  790. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  791. _Result
  792. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
  793. {
  794. return _M_f(_Mu<_Bound_args>()
  795. (std::get<_Indexes>(_M_bound_args), __args)...);
  796. }
  797. // Call as const
  798. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  799. _Result
  800. __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
  801. {
  802. return _M_f(_Mu<_Bound_args>()
  803. (std::get<_Indexes>(_M_bound_args), __args)...);
  804. }
  805. // Call as volatile
  806. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  807. _Result
  808. __call_v(tuple<_Args...>&& __args,
  809. _Index_tuple<_Indexes...>) volatile
  810. {
  811. return _M_f(_Mu<_Bound_args>()
  812. (__volget<_Indexes>(_M_bound_args), __args)...);
  813. }
  814. // Call as const volatile
  815. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  816. _Result
  817. __call_c_v(tuple<_Args...>&& __args,
  818. _Index_tuple<_Indexes...>) const volatile
  819. {
  820. return _M_f(_Mu<_Bound_args>()
  821. (__volget<_Indexes>(_M_bound_args), __args)...);
  822. }
  823. public:
  824. template<typename... _Args>
  825. explicit _Bind(const _Functor& __f, _Args&&... __args)
  826. : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
  827. { }
  828. template<typename... _Args>
  829. explicit _Bind(_Functor&& __f, _Args&&... __args)
  830. : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
  831. { }
  832. _Bind(const _Bind&) = default;
  833. _Bind(_Bind&& __b)
  834. : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
  835. { }
  836. // Call unqualified
  837. template<typename... _Args, typename _Result
  838. = decltype( std::declval<_Functor&>()(
  839. _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
  840. std::declval<tuple<_Args...>&>() )... ) )>
  841. _Result
  842. operator()(_Args&&... __args)
  843. {
  844. return this->__call<_Result>(
  845. std::forward_as_tuple(std::forward<_Args>(__args)...),
  846. _Bound_indexes());
  847. }
  848. // Call as const
  849. template<typename... _Args, typename _Result
  850. = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
  851. typename add_const<_Functor>::type&>::type>()(
  852. _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
  853. std::declval<tuple<_Args...>&>() )... ) )>
  854. _Result
  855. operator()(_Args&&... __args) const
  856. {
  857. return this->__call_c<_Result>(
  858. std::forward_as_tuple(std::forward<_Args>(__args)...),
  859. _Bound_indexes());
  860. }
  861. // Call as volatile
  862. template<typename... _Args, typename _Result
  863. = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
  864. typename add_volatile<_Functor>::type&>::type>()(
  865. _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
  866. std::declval<tuple<_Args...>&>() )... ) )>
  867. _Result
  868. operator()(_Args&&... __args) volatile
  869. {
  870. return this->__call_v<_Result>(
  871. std::forward_as_tuple(std::forward<_Args>(__args)...),
  872. _Bound_indexes());
  873. }
  874. // Call as const volatile
  875. template<typename... _Args, typename _Result
  876. = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
  877. typename add_cv<_Functor>::type&>::type>()(
  878. _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
  879. std::declval<tuple<_Args...>&>() )... ) )>
  880. _Result
  881. operator()(_Args&&... __args) const volatile
  882. {
  883. return this->__call_c_v<_Result>(
  884. std::forward_as_tuple(std::forward<_Args>(__args)...),
  885. _Bound_indexes());
  886. }
  887. };
  888. /// Type of the function object returned from bind<R>().
  889. template<typename _Result, typename _Signature>
  890. struct _Bind_result;
  891. template<typename _Result, typename _Functor, typename... _Bound_args>
  892. class _Bind_result<_Result, _Functor(_Bound_args...)>
  893. {
  894. typedef _Bind_result __self_type;
  895. typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
  896. _Bound_indexes;
  897. _Functor _M_f;
  898. tuple<_Bound_args...> _M_bound_args;
  899. // sfinae types
  900. template<typename _Res>
  901. struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
  902. template<typename _Res>
  903. struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
  904. // Call unqualified
  905. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  906. _Result
  907. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  908. typename __disable_if_void<_Res>::type = 0)
  909. {
  910. return _M_f(_Mu<_Bound_args>()
  911. (std::get<_Indexes>(_M_bound_args), __args)...);
  912. }
  913. // Call unqualified, return void
  914. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  915. void
  916. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  917. typename __enable_if_void<_Res>::type = 0)
  918. {
  919. _M_f(_Mu<_Bound_args>()
  920. (std::get<_Indexes>(_M_bound_args), __args)...);
  921. }
  922. // Call as const
  923. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  924. _Result
  925. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  926. typename __disable_if_void<_Res>::type = 0) const
  927. {
  928. return _M_f(_Mu<_Bound_args>()
  929. (std::get<_Indexes>(_M_bound_args), __args)...);
  930. }
  931. // Call as const, return void
  932. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  933. void
  934. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  935. typename __enable_if_void<_Res>::type = 0) const
  936. {
  937. _M_f(_Mu<_Bound_args>()
  938. (std::get<_Indexes>(_M_bound_args), __args)...);
  939. }
  940. // Call as volatile
  941. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  942. _Result
  943. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  944. typename __disable_if_void<_Res>::type = 0) volatile
  945. {
  946. return _M_f(_Mu<_Bound_args>()
  947. (__volget<_Indexes>(_M_bound_args), __args)...);
  948. }
  949. // Call as volatile, return void
  950. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  951. void
  952. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  953. typename __enable_if_void<_Res>::type = 0) volatile
  954. {
  955. _M_f(_Mu<_Bound_args>()
  956. (__volget<_Indexes>(_M_bound_args), __args)...);
  957. }
  958. // Call as const volatile
  959. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  960. _Result
  961. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
  962. typename __disable_if_void<_Res>::type = 0) const volatile
  963. {
  964. return _M_f(_Mu<_Bound_args>()
  965. (__volget<_Indexes>(_M_bound_args), __args)...);
  966. }
  967. // Call as const volatile, return void
  968. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  969. void
  970. __call(tuple<_Args...>&& __args,
  971. _Index_tuple<_Indexes...>,
  972. typename __enable_if_void<_Res>::type = 0) const volatile
  973. {
  974. _M_f(_Mu<_Bound_args>()
  975. (__volget<_Indexes>(_M_bound_args), __args)...);
  976. }
  977. public:
  978. typedef _Result result_type;
  979. template<typename... _Args>
  980. explicit _Bind_result(const _Functor& __f, _Args&&... __args)
  981. : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
  982. { }
  983. template<typename... _Args>
  984. explicit _Bind_result(_Functor&& __f, _Args&&... __args)
  985. : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
  986. { }
  987. _Bind_result(const _Bind_result&) = default;
  988. _Bind_result(_Bind_result&& __b)
  989. : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
  990. { }
  991. // Call unqualified
  992. template<typename... _Args>
  993. result_type
  994. operator()(_Args&&... __args)
  995. {
  996. return this->__call<_Result>(
  997. std::forward_as_tuple(std::forward<_Args>(__args)...),
  998. _Bound_indexes());
  999. }
  1000. // Call as const
  1001. template<typename... _Args>
  1002. result_type
  1003. operator()(_Args&&... __args) const
  1004. {
  1005. return this->__call<_Result>(
  1006. std::forward_as_tuple(std::forward<_Args>(__args)...),
  1007. _Bound_indexes());
  1008. }
  1009. // Call as volatile
  1010. template<typename... _Args>
  1011. result_type
  1012. operator()(_Args&&... __args) volatile
  1013. {
  1014. return this->__call<_Result>(
  1015. std::forward_as_tuple(std::forward<_Args>(__args)...),
  1016. _Bound_indexes());
  1017. }
  1018. // Call as const volatile
  1019. template<typename... _Args>
  1020. result_type
  1021. operator()(_Args&&... __args) const volatile
  1022. {
  1023. return this->__call<_Result>(
  1024. std::forward_as_tuple(std::forward<_Args>(__args)...),
  1025. _Bound_indexes());
  1026. }
  1027. };
  1028. /**
  1029. * @brief Class template _Bind is always a bind expression.
  1030. * @ingroup binders
  1031. */
  1032. template<typename _Signature>
  1033. struct is_bind_expression<_Bind<_Signature> >
  1034. : public true_type { };
  1035. /**
  1036. * @brief Class template _Bind is always a bind expression.
  1037. * @ingroup binders
  1038. */
  1039. template<typename _Signature>
  1040. struct is_bind_expression<const _Bind<_Signature> >
  1041. : public true_type { };
  1042. /**
  1043. * @brief Class template _Bind is always a bind expression.
  1044. * @ingroup binders
  1045. */
  1046. template<typename _Signature>
  1047. struct is_bind_expression<volatile _Bind<_Signature> >
  1048. : public true_type { };
  1049. /**
  1050. * @brief Class template _Bind is always a bind expression.
  1051. * @ingroup binders
  1052. */
  1053. template<typename _Signature>
  1054. struct is_bind_expression<const volatile _Bind<_Signature>>
  1055. : public true_type { };
  1056. /**
  1057. * @brief Class template _Bind_result is always a bind expression.
  1058. * @ingroup binders
  1059. */
  1060. template<typename _Result, typename _Signature>
  1061. struct is_bind_expression<_Bind_result<_Result, _Signature>>
  1062. : public true_type { };
  1063. /**
  1064. * @brief Class template _Bind_result is always a bind expression.
  1065. * @ingroup binders
  1066. */
  1067. template<typename _Result, typename _Signature>
  1068. struct is_bind_expression<const _Bind_result<_Result, _Signature>>
  1069. : public true_type { };
  1070. /**
  1071. * @brief Class template _Bind_result is always a bind expression.
  1072. * @ingroup binders
  1073. */
  1074. template<typename _Result, typename _Signature>
  1075. struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
  1076. : public true_type { };
  1077. /**
  1078. * @brief Class template _Bind_result is always a bind expression.
  1079. * @ingroup binders
  1080. */
  1081. template<typename _Result, typename _Signature>
  1082. struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
  1083. : public true_type { };
  1084. template<typename _Func, typename... _BoundArgs>
  1085. struct _Bind_check_arity { };
  1086. template<typename _Ret, typename... _Args, typename... _BoundArgs>
  1087. struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
  1088. {
  1089. static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
  1090. "Wrong number of arguments for function");
  1091. };
  1092. template<typename _Ret, typename... _Args, typename... _BoundArgs>
  1093. struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
  1094. {
  1095. static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
  1096. "Wrong number of arguments for function");
  1097. };
  1098. template<typename _Tp, typename _Class, typename... _BoundArgs>
  1099. struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
  1100. {
  1101. using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
  1102. using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
  1103. static_assert(_Varargs::value
  1104. ? sizeof...(_BoundArgs) >= _Arity::value + 1
  1105. : sizeof...(_BoundArgs) == _Arity::value + 1,
  1106. "Wrong number of arguments for pointer-to-member");
  1107. };
  1108. // Trait type used to remove std::bind() from overload set via SFINAE
  1109. // when first argument has integer type, so that std::bind() will
  1110. // not be a better match than ::bind() from the BSD Sockets API.
  1111. template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
  1112. using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
  1113. template<bool _SocketLike, typename _Func, typename... _BoundArgs>
  1114. struct _Bind_helper
  1115. : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
  1116. {
  1117. typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
  1118. __maybe_type;
  1119. typedef typename __maybe_type::type __func_type;
  1120. typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
  1121. };
  1122. // Partial specialization for is_socketlike == true, does not define
  1123. // nested type so std::bind() will not participate in overload resolution
  1124. // when the first argument might be a socket file descriptor.
  1125. template<typename _Func, typename... _BoundArgs>
  1126. struct _Bind_helper<true, _Func, _BoundArgs...>
  1127. { };
  1128. /**
  1129. * @brief Function template for std::bind.
  1130. * @ingroup binders
  1131. */
  1132. template<typename _Func, typename... _BoundArgs>
  1133. inline typename
  1134. _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
  1135. bind(_Func&& __f, _BoundArgs&&... __args)
  1136. {
  1137. typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
  1138. typedef typename __helper_type::__maybe_type __maybe_type;
  1139. typedef typename __helper_type::type __result_type;
  1140. return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
  1141. std::forward<_BoundArgs>(__args)...);
  1142. }
  1143. template<typename _Result, typename _Func, typename... _BoundArgs>
  1144. struct _Bindres_helper
  1145. : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
  1146. {
  1147. typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
  1148. __maybe_type;
  1149. typedef typename __maybe_type::type __functor_type;
  1150. typedef _Bind_result<_Result,
  1151. __functor_type(typename decay<_BoundArgs>::type...)>
  1152. type;
  1153. };
  1154. /**
  1155. * @brief Function template for std::bind<R>.
  1156. * @ingroup binders
  1157. */
  1158. template<typename _Result, typename _Func, typename... _BoundArgs>
  1159. inline
  1160. typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
  1161. bind(_Func&& __f, _BoundArgs&&... __args)
  1162. {
  1163. typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
  1164. typedef typename __helper_type::__maybe_type __maybe_type;
  1165. typedef typename __helper_type::type __result_type;
  1166. return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
  1167. std::forward<_BoundArgs>(__args)...);
  1168. }
  1169. template<typename _Signature>
  1170. struct _Bind_simple;
  1171. template<typename _Callable, typename... _Args>
  1172. struct _Bind_simple<_Callable(_Args...)>
  1173. {
  1174. typedef typename result_of<_Callable(_Args...)>::type result_type;
  1175. template<typename _Tp, typename... _Up>
  1176. explicit
  1177. _Bind_simple(_Tp&& __f, _Up&&... __args)
  1178. : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
  1179. { }
  1180. _Bind_simple(const _Bind_simple&) = default;
  1181. _Bind_simple(_Bind_simple&&) = default;
  1182. result_type
  1183. operator()()
  1184. {
  1185. typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
  1186. return _M_invoke(_Indices());
  1187. }
  1188. private:
  1189. template<std::size_t... _Indices>
  1190. typename result_of<_Callable(_Args...)>::type
  1191. _M_invoke(_Index_tuple<_Indices...>)
  1192. {
  1193. // std::bind always forwards bound arguments as lvalues,
  1194. // but this type can call functions which only accept rvalues.
  1195. return std::forward<_Callable>(std::get<0>(_M_bound))(
  1196. std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
  1197. }
  1198. std::tuple<_Callable, _Args...> _M_bound;
  1199. };
  1200. template<typename _Func, typename... _BoundArgs>
  1201. struct _Bind_simple_helper
  1202. : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
  1203. {
  1204. typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
  1205. __maybe_type;
  1206. typedef typename __maybe_type::type __func_type;
  1207. typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
  1208. __type;
  1209. };
  1210. // Simplified version of std::bind for internal use, without support for
  1211. // unbound arguments, placeholders or nested bind expressions.
  1212. template<typename _Callable, typename... _Args>
  1213. typename _Bind_simple_helper<_Callable, _Args...>::__type
  1214. __bind_simple(_Callable&& __callable, _Args&&... __args)
  1215. {
  1216. typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
  1217. typedef typename __helper_type::__maybe_type __maybe_type;
  1218. typedef typename __helper_type::__type __result_type;
  1219. return __result_type(
  1220. __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
  1221. std::forward<_Args>(__args)...);
  1222. }
  1223. /**
  1224. * @brief Exception class thrown when class template function's
  1225. * operator() is called with an empty target.
  1226. * @ingroup exceptions
  1227. */
  1228. class bad_function_call : public std::exception
  1229. {
  1230. public:
  1231. virtual ~bad_function_call() noexcept;
  1232. const char* what() const noexcept;
  1233. };
  1234. /**
  1235. * Trait identifying "location-invariant" types, meaning that the
  1236. * address of the object (or any of its members) will not escape.
  1237. * Trivially copyable types are location-invariant and users can
  1238. * specialize this trait for other types.
  1239. */
  1240. template<typename _Tp>
  1241. struct __is_location_invariant
  1242. : is_trivially_copyable<_Tp>::type
  1243. { };
  1244. class _Undefined_class;
  1245. union _Nocopy_types
  1246. {
  1247. void* _M_object;
  1248. const void* _M_const_object;
  1249. void (*_M_function_pointer)();
  1250. void (_Undefined_class::*_M_member_pointer)();
  1251. };
  1252. union [[gnu::may_alias]] _Any_data
  1253. {
  1254. void* _M_access() { return &_M_pod_data[0]; }
  1255. const void* _M_access() const { return &_M_pod_data[0]; }
  1256. template<typename _Tp>
  1257. _Tp&
  1258. _M_access()
  1259. { return *static_cast<_Tp*>(_M_access()); }
  1260. template<typename _Tp>
  1261. const _Tp&
  1262. _M_access() const
  1263. { return *static_cast<const _Tp*>(_M_access()); }
  1264. _Nocopy_types _M_unused;
  1265. char _M_pod_data[sizeof(_Nocopy_types)];
  1266. };
  1267. enum _Manager_operation
  1268. {
  1269. __get_type_info,
  1270. __get_functor_ptr,
  1271. __clone_functor,
  1272. __destroy_functor
  1273. };
  1274. // Simple type wrapper that helps avoid annoying const problems
  1275. // when casting between void pointers and pointers-to-pointers.
  1276. template<typename _Tp>
  1277. struct _Simple_type_wrapper
  1278. {
  1279. _Simple_type_wrapper(_Tp __value) : __value(__value) { }
  1280. _Tp __value;
  1281. };
  1282. template<typename _Tp>
  1283. struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
  1284. : __is_location_invariant<_Tp>
  1285. { };
  1286. // Converts a reference to a function object into a callable
  1287. // function object.
  1288. template<typename _Functor>
  1289. inline _Functor&
  1290. __callable_functor(_Functor& __f)
  1291. { return __f; }
  1292. template<typename _Member, typename _Class>
  1293. inline _Mem_fn<_Member _Class::*>
  1294. __callable_functor(_Member _Class::* &__p)
  1295. { return std::mem_fn(__p); }
  1296. template<typename _Member, typename _Class>
  1297. inline _Mem_fn<_Member _Class::*>
  1298. __callable_functor(_Member _Class::* const &__p)
  1299. { return std::mem_fn(__p); }
  1300. template<typename _Member, typename _Class>
  1301. inline _Mem_fn<_Member _Class::*>
  1302. __callable_functor(_Member _Class::* volatile &__p)
  1303. { return std::mem_fn(__p); }
  1304. template<typename _Member, typename _Class>
  1305. inline _Mem_fn<_Member _Class::*>
  1306. __callable_functor(_Member _Class::* const volatile &__p)
  1307. { return std::mem_fn(__p); }
  1308. template<typename _Signature>
  1309. class function;
  1310. /// Base class of all polymorphic function object wrappers.
  1311. class _Function_base
  1312. {
  1313. public:
  1314. static const std::size_t _M_max_size = sizeof(_Nocopy_types);
  1315. static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
  1316. template<typename _Functor>
  1317. class _Base_manager
  1318. {
  1319. protected:
  1320. static const bool __stored_locally =
  1321. (__is_location_invariant<_Functor>::value
  1322. && sizeof(_Functor) <= _M_max_size
  1323. && __alignof__(_Functor) <= _M_max_align
  1324. && (_M_max_align % __alignof__(_Functor) == 0));
  1325. typedef integral_constant<bool, __stored_locally> _Local_storage;
  1326. // Retrieve a pointer to the function object
  1327. static _Functor*
  1328. _M_get_pointer(const _Any_data& __source)
  1329. {
  1330. const _Functor* __ptr =
  1331. __stored_locally? std::__addressof(__source._M_access<_Functor>())
  1332. /* have stored a pointer */ : __source._M_access<_Functor*>();
  1333. return const_cast<_Functor*>(__ptr);
  1334. }
  1335. // Clone a location-invariant function object that fits within
  1336. // an _Any_data structure.
  1337. static void
  1338. _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
  1339. {
  1340. new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
  1341. }
  1342. // Clone a function object that is not location-invariant or
  1343. // that cannot fit into an _Any_data structure.
  1344. static void
  1345. _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
  1346. {
  1347. __dest._M_access<_Functor*>() =
  1348. new _Functor(*__source._M_access<_Functor*>());
  1349. }
  1350. // Destroying a location-invariant object may still require
  1351. // destruction.
  1352. static void
  1353. _M_destroy(_Any_data& __victim, true_type)
  1354. {
  1355. __victim._M_access<_Functor>().~_Functor();
  1356. }
  1357. // Destroying an object located on the heap.
  1358. static void
  1359. _M_destroy(_Any_data& __victim, false_type)
  1360. {
  1361. delete __victim._M_access<_Functor*>();
  1362. }
  1363. public:
  1364. static bool
  1365. _M_manager(_Any_data& __dest, const _Any_data& __source,
  1366. _Manager_operation __op)
  1367. {
  1368. switch (__op)
  1369. {
  1370. #if __cpp_rtti
  1371. case __get_type_info:
  1372. __dest._M_access<const type_info*>() = &typeid(_Functor);
  1373. break;
  1374. #endif
  1375. case __get_functor_ptr:
  1376. __dest._M_access<_Functor*>() = _M_get_pointer(__source);
  1377. break;
  1378. case __clone_functor:
  1379. _M_clone(__dest, __source, _Local_storage());
  1380. break;
  1381. case __destroy_functor:
  1382. _M_destroy(__dest, _Local_storage());
  1383. break;
  1384. }
  1385. return false;
  1386. }
  1387. static void
  1388. _M_init_functor(_Any_data& __functor, _Functor&& __f)
  1389. { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
  1390. template<typename _Signature>
  1391. static bool
  1392. _M_not_empty_function(const function<_Signature>& __f)
  1393. { return static_cast<bool>(__f); }
  1394. template<typename _Tp>
  1395. static bool
  1396. _M_not_empty_function(_Tp* __fp)
  1397. { return __fp != nullptr; }
  1398. template<typename _Class, typename _Tp>
  1399. static bool
  1400. _M_not_empty_function(_Tp _Class::* __mp)
  1401. { return __mp != nullptr; }
  1402. template<typename _Tp>
  1403. static bool
  1404. _M_not_empty_function(const _Tp&)
  1405. { return true; }
  1406. private:
  1407. static void
  1408. _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
  1409. { new (__functor._M_access()) _Functor(std::move(__f)); }
  1410. static void
  1411. _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
  1412. { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
  1413. };
  1414. template<typename _Functor>
  1415. class _Ref_manager : public _Base_manager<_Functor*>
  1416. {
  1417. typedef _Function_base::_Base_manager<_Functor*> _Base;
  1418. public:
  1419. static bool
  1420. _M_manager(_Any_data& __dest, const _Any_data& __source,
  1421. _Manager_operation __op)
  1422. {
  1423. switch (__op)
  1424. {
  1425. #if __cpp_rtti
  1426. case __get_type_info:
  1427. __dest._M_access<const type_info*>() = &typeid(_Functor);
  1428. break;
  1429. #endif
  1430. case __get_functor_ptr:
  1431. __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
  1432. return is_const<_Functor>::value;
  1433. break;
  1434. default:
  1435. _Base::_M_manager(__dest, __source, __op);
  1436. }
  1437. return false;
  1438. }
  1439. static void
  1440. _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
  1441. {
  1442. _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
  1443. }
  1444. };
  1445. _Function_base() : _M_manager(nullptr) { }
  1446. ~_Function_base()
  1447. {
  1448. if (_M_manager)
  1449. _M_manager(_M_functor, _M_functor, __destroy_functor);
  1450. }
  1451. bool _M_empty() const { return !_M_manager; }
  1452. typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
  1453. _Manager_operation);
  1454. _Any_data _M_functor;
  1455. _Manager_type _M_manager;
  1456. };
  1457. template<typename _Signature, typename _Functor>
  1458. class _Function_handler;
  1459. template<typename _Res, typename _Functor, typename... _ArgTypes>
  1460. class _Function_handler<_Res(_ArgTypes...), _Functor>
  1461. : public _Function_base::_Base_manager<_Functor>
  1462. {
  1463. typedef _Function_base::_Base_manager<_Functor> _Base;
  1464. public:
  1465. static _Res
  1466. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1467. {
  1468. return (*_Base::_M_get_pointer(__functor))(
  1469. std::forward<_ArgTypes>(__args)...);
  1470. }
  1471. };
  1472. template<typename _Functor, typename... _ArgTypes>
  1473. class _Function_handler<void(_ArgTypes...), _Functor>
  1474. : public _Function_base::_Base_manager<_Functor>
  1475. {
  1476. typedef _Function_base::_Base_manager<_Functor> _Base;
  1477. public:
  1478. static void
  1479. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1480. {
  1481. (*_Base::_M_get_pointer(__functor))(
  1482. std::forward<_ArgTypes>(__args)...);
  1483. }
  1484. };
  1485. template<typename _Res, typename _Functor, typename... _ArgTypes>
  1486. class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
  1487. : public _Function_base::_Ref_manager<_Functor>
  1488. {
  1489. typedef _Function_base::_Ref_manager<_Functor> _Base;
  1490. public:
  1491. static _Res
  1492. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1493. {
  1494. return std::__callable_functor(**_Base::_M_get_pointer(__functor))(
  1495. std::forward<_ArgTypes>(__args)...);
  1496. }
  1497. };
  1498. template<typename _Functor, typename... _ArgTypes>
  1499. class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
  1500. : public _Function_base::_Ref_manager<_Functor>
  1501. {
  1502. typedef _Function_base::_Ref_manager<_Functor> _Base;
  1503. public:
  1504. static void
  1505. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1506. {
  1507. std::__callable_functor(**_Base::_M_get_pointer(__functor))(
  1508. std::forward<_ArgTypes>(__args)...);
  1509. }
  1510. };
  1511. template<typename _Class, typename _Member, typename _Res,
  1512. typename... _ArgTypes>
  1513. class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
  1514. : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
  1515. {
  1516. typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
  1517. _Base;
  1518. public:
  1519. static _Res
  1520. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1521. {
  1522. return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
  1523. std::forward<_ArgTypes>(__args)...);
  1524. }
  1525. };
  1526. template<typename _Class, typename _Member, typename... _ArgTypes>
  1527. class _Function_handler<void(_ArgTypes...), _Member _Class::*>
  1528. : public _Function_base::_Base_manager<
  1529. _Simple_type_wrapper< _Member _Class::* > >
  1530. {
  1531. typedef _Member _Class::* _Functor;
  1532. typedef _Simple_type_wrapper<_Functor> _Wrapper;
  1533. typedef _Function_base::_Base_manager<_Wrapper> _Base;
  1534. public:
  1535. static bool
  1536. _M_manager(_Any_data& __dest, const _Any_data& __source,
  1537. _Manager_operation __op)
  1538. {
  1539. switch (__op)
  1540. {
  1541. #if __cpp_rtti
  1542. case __get_type_info:
  1543. __dest._M_access<const type_info*>() = &typeid(_Functor);
  1544. break;
  1545. #endif
  1546. case __get_functor_ptr:
  1547. __dest._M_access<_Functor*>() =
  1548. &_Base::_M_get_pointer(__source)->__value;
  1549. break;
  1550. default:
  1551. _Base::_M_manager(__dest, __source, __op);
  1552. }
  1553. return false;
  1554. }
  1555. static void
  1556. _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
  1557. {
  1558. std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
  1559. std::forward<_ArgTypes>(__args)...);
  1560. }
  1561. };
  1562. template<typename _From, typename _To>
  1563. using __check_func_return_type
  1564. = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
  1565. /**
  1566. * @brief Primary class template for std::function.
  1567. * @ingroup functors
  1568. *
  1569. * Polymorphic function wrapper.
  1570. */
  1571. template<typename _Res, typename... _ArgTypes>
  1572. class function<_Res(_ArgTypes...)>
  1573. : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
  1574. private _Function_base
  1575. {
  1576. typedef _Res _Signature_type(_ArgTypes...);
  1577. template<typename _Func,
  1578. typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
  1579. struct _Callable : __check_func_return_type<_Res2, _Res> { };
  1580. // Used so the return type convertibility checks aren't done when
  1581. // performing overload resolution for copy construction/assignment.
  1582. template<typename _Tp>
  1583. struct _Callable<function, _Tp> : false_type { };
  1584. template<typename _Cond, typename _Tp>
  1585. using _Requires = typename enable_if<_Cond::value, _Tp>::type;
  1586. public:
  1587. typedef _Res result_type;
  1588. // [3.7.2.1] construct/copy/destroy
  1589. /**
  1590. * @brief Default construct creates an empty function call wrapper.
  1591. * @post @c !(bool)*this
  1592. */
  1593. function() noexcept
  1594. : _Function_base() { }
  1595. /**
  1596. * @brief Creates an empty function call wrapper.
  1597. * @post @c !(bool)*this
  1598. */
  1599. function(nullptr_t) noexcept
  1600. : _Function_base() { }
  1601. /**
  1602. * @brief %Function copy constructor.
  1603. * @param __x A %function object with identical call signature.
  1604. * @post @c bool(*this) == bool(__x)
  1605. *
  1606. * The newly-created %function contains a copy of the target of @a
  1607. * __x (if it has one).
  1608. */
  1609. function(const function& __x);
  1610. /**
  1611. * @brief %Function move constructor.
  1612. * @param __x A %function object rvalue with identical call signature.
  1613. *
  1614. * The newly-created %function contains the target of @a __x
  1615. * (if it has one).
  1616. */
  1617. function(function&& __x) : _Function_base()
  1618. {
  1619. __x.swap(*this);
  1620. }
  1621. // TODO: needs allocator_arg_t
  1622. /**
  1623. * @brief Builds a %function that targets a copy of the incoming
  1624. * function object.
  1625. * @param __f A %function object that is callable with parameters of
  1626. * type @c T1, @c T2, ..., @c TN and returns a value convertible
  1627. * to @c Res.
  1628. *
  1629. * The newly-created %function object will target a copy of
  1630. * @a __f. If @a __f is @c reference_wrapper<F>, then this function
  1631. * object will contain a reference to the function object @c
  1632. * __f.get(). If @a __f is a NULL function pointer or NULL
  1633. * pointer-to-member, the newly-created object will be empty.
  1634. *
  1635. * If @a __f is a non-NULL function pointer or an object of type @c
  1636. * reference_wrapper<F>, this function will not throw.
  1637. */
  1638. template<typename _Functor,
  1639. typename = _Requires<__not_<is_same<_Functor, function>>, void>,
  1640. typename = _Requires<_Callable<_Functor>, void>>
  1641. function(_Functor);
  1642. /**
  1643. * @brief %Function assignment operator.
  1644. * @param __x A %function with identical call signature.
  1645. * @post @c (bool)*this == (bool)x
  1646. * @returns @c *this
  1647. *
  1648. * The target of @a __x is copied to @c *this. If @a __x has no
  1649. * target, then @c *this will be empty.
  1650. *
  1651. * If @a __x targets a function pointer or a reference to a function
  1652. * object, then this operation will not throw an %exception.
  1653. */
  1654. function&
  1655. operator=(const function& __x)
  1656. {
  1657. function(__x).swap(*this);
  1658. return *this;
  1659. }
  1660. /**
  1661. * @brief %Function move-assignment operator.
  1662. * @param __x A %function rvalue with identical call signature.
  1663. * @returns @c *this
  1664. *
  1665. * The target of @a __x is moved to @c *this. If @a __x has no
  1666. * target, then @c *this will be empty.
  1667. *
  1668. * If @a __x targets a function pointer or a reference to a function
  1669. * object, then this operation will not throw an %exception.
  1670. */
  1671. function&
  1672. operator=(function&& __x)
  1673. {
  1674. function(std::move(__x)).swap(*this);
  1675. return *this;
  1676. }
  1677. /**
  1678. * @brief %Function assignment to zero.
  1679. * @post @c !(bool)*this
  1680. * @returns @c *this
  1681. *
  1682. * The target of @c *this is deallocated, leaving it empty.
  1683. */
  1684. function&
  1685. operator=(nullptr_t) noexcept
  1686. {
  1687. if (_M_manager)
  1688. {
  1689. _M_manager(_M_functor, _M_functor, __destroy_functor);
  1690. _M_manager = nullptr;
  1691. _M_invoker = nullptr;
  1692. }
  1693. return *this;
  1694. }
  1695. /**
  1696. * @brief %Function assignment to a new target.
  1697. * @param __f A %function object that is callable with parameters of
  1698. * type @c T1, @c T2, ..., @c TN and returns a value convertible
  1699. * to @c Res.
  1700. * @return @c *this
  1701. *
  1702. * This %function object wrapper will target a copy of @a
  1703. * __f. If @a __f is @c reference_wrapper<F>, then this function
  1704. * object will contain a reference to the function object @c
  1705. * __f.get(). If @a __f is a NULL function pointer or NULL
  1706. * pointer-to-member, @c this object will be empty.
  1707. *
  1708. * If @a __f is a non-NULL function pointer or an object of type @c
  1709. * reference_wrapper<F>, this function will not throw.
  1710. */
  1711. template<typename _Functor>
  1712. _Requires<_Callable<typename decay<_Functor>::type>, function&>
  1713. operator=(_Functor&& __f)
  1714. {
  1715. function(std::forward<_Functor>(__f)).swap(*this);
  1716. return *this;
  1717. }
  1718. /// @overload
  1719. template<typename _Functor>
  1720. function&
  1721. operator=(reference_wrapper<_Functor> __f) noexcept
  1722. {
  1723. function(__f).swap(*this);
  1724. return *this;
  1725. }
  1726. // [3.7.2.2] function modifiers
  1727. /**
  1728. * @brief Swap the targets of two %function objects.
  1729. * @param __x A %function with identical call signature.
  1730. *
  1731. * Swap the targets of @c this function object and @a __f. This
  1732. * function will not throw an %exception.
  1733. */
  1734. void swap(function& __x)
  1735. {
  1736. std::swap(_M_functor, __x._M_functor);
  1737. std::swap(_M_manager, __x._M_manager);
  1738. std::swap(_M_invoker, __x._M_invoker);
  1739. }
  1740. // TODO: needs allocator_arg_t
  1741. /*
  1742. template<typename _Functor, typename _Alloc>
  1743. void
  1744. assign(_Functor&& __f, const _Alloc& __a)
  1745. {
  1746. function(allocator_arg, __a,
  1747. std::forward<_Functor>(__f)).swap(*this);
  1748. }
  1749. */
  1750. // [3.7.2.3] function capacity
  1751. /**
  1752. * @brief Determine if the %function wrapper has a target.
  1753. *
  1754. * @return @c true when this %function object contains a target,
  1755. * or @c false when it is empty.
  1756. *
  1757. * This function will not throw an %exception.
  1758. */
  1759. explicit operator bool() const noexcept
  1760. { return !_M_empty(); }
  1761. // [3.7.2.4] function invocation
  1762. /**
  1763. * @brief Invokes the function targeted by @c *this.
  1764. * @returns the result of the target.
  1765. * @throws bad_function_call when @c !(bool)*this
  1766. *
  1767. * The function call operator invokes the target function object
  1768. * stored by @c this.
  1769. */
  1770. _Res operator()(_ArgTypes... __args) const;
  1771. #if __cpp_rtti
  1772. // [3.7.2.5] function target access
  1773. /**
  1774. * @brief Determine the type of the target of this function object
  1775. * wrapper.
  1776. *
  1777. * @returns the type identifier of the target function object, or
  1778. * @c typeid(void) if @c !(bool)*this.
  1779. *
  1780. * This function will not throw an %exception.
  1781. */
  1782. const type_info& target_type() const noexcept;
  1783. /**
  1784. * @brief Access the stored target function object.
  1785. *
  1786. * @return Returns a pointer to the stored target function object,
  1787. * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
  1788. * pointer.
  1789. *
  1790. * This function will not throw an %exception.
  1791. */
  1792. template<typename _Functor> _Functor* target() noexcept;
  1793. /// @overload
  1794. template<typename _Functor> const _Functor* target() const noexcept;
  1795. #endif
  1796. private:
  1797. using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
  1798. _Invoker_type _M_invoker;
  1799. };
  1800. // Out-of-line member definitions.
  1801. template<typename _Res, typename... _ArgTypes>
  1802. function<_Res(_ArgTypes...)>::
  1803. function(const function& __x)
  1804. : _Function_base()
  1805. {
  1806. if (static_cast<bool>(__x))
  1807. {
  1808. __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
  1809. _M_invoker = __x._M_invoker;
  1810. _M_manager = __x._M_manager;
  1811. }
  1812. }
  1813. template<typename _Res, typename... _ArgTypes>
  1814. template<typename _Functor, typename, typename>
  1815. function<_Res(_ArgTypes...)>::
  1816. function(_Functor __f)
  1817. : _Function_base()
  1818. {
  1819. typedef _Function_handler<_Signature_type, _Functor> _My_handler;
  1820. if (_My_handler::_M_not_empty_function(__f))
  1821. {
  1822. _My_handler::_M_init_functor(_M_functor, std::move(__f));
  1823. _M_invoker = &_My_handler::_M_invoke;
  1824. _M_manager = &_My_handler::_M_manager;
  1825. }
  1826. }
  1827. template<typename _Res, typename... _ArgTypes>
  1828. _Res
  1829. function<_Res(_ArgTypes...)>::
  1830. operator()(_ArgTypes... __args) const
  1831. {
  1832. if (_M_empty())
  1833. __throw_bad_function_call();
  1834. return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
  1835. }
  1836. #if __cpp_rtti
  1837. template<typename _Res, typename... _ArgTypes>
  1838. const type_info&
  1839. function<_Res(_ArgTypes...)>::
  1840. target_type() const noexcept
  1841. {
  1842. if (_M_manager)
  1843. {
  1844. _Any_data __typeinfo_result;
  1845. _M_manager(__typeinfo_result, _M_functor, __get_type_info);
  1846. return *__typeinfo_result._M_access<const type_info*>();
  1847. }
  1848. else
  1849. return typeid(void);
  1850. }
  1851. template<typename _Res, typename... _ArgTypes>
  1852. template<typename _Functor>
  1853. _Functor*
  1854. function<_Res(_ArgTypes...)>::
  1855. target() noexcept
  1856. {
  1857. if (typeid(_Functor) == target_type() && _M_manager)
  1858. {
  1859. _Any_data __ptr;
  1860. if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
  1861. && !is_const<_Functor>::value)
  1862. return 0;
  1863. else
  1864. return __ptr._M_access<_Functor*>();
  1865. }
  1866. else
  1867. return 0;
  1868. }
  1869. template<typename _Res, typename... _ArgTypes>
  1870. template<typename _Functor>
  1871. const _Functor*
  1872. function<_Res(_ArgTypes...)>::
  1873. target() const noexcept
  1874. {
  1875. if (typeid(_Functor) == target_type() && _M_manager)
  1876. {
  1877. _Any_data __ptr;
  1878. _M_manager(__ptr, _M_functor, __get_functor_ptr);
  1879. return __ptr._M_access<const _Functor*>();
  1880. }
  1881. else
  1882. return 0;
  1883. }
  1884. #endif
  1885. // [20.7.15.2.6] null pointer comparisons
  1886. /**
  1887. * @brief Compares a polymorphic function object wrapper against 0
  1888. * (the NULL pointer).
  1889. * @returns @c true if the wrapper has no target, @c false otherwise
  1890. *
  1891. * This function will not throw an %exception.
  1892. */
  1893. template<typename _Res, typename... _Args>
  1894. inline bool
  1895. operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
  1896. { return !static_cast<bool>(__f); }
  1897. /// @overload
  1898. template<typename _Res, typename... _Args>
  1899. inline bool
  1900. operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
  1901. { return !static_cast<bool>(__f); }
  1902. /**
  1903. * @brief Compares a polymorphic function object wrapper against 0
  1904. * (the NULL pointer).
  1905. * @returns @c false if the wrapper has no target, @c true otherwise
  1906. *
  1907. * This function will not throw an %exception.
  1908. */
  1909. template<typename _Res, typename... _Args>
  1910. inline bool
  1911. operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
  1912. { return static_cast<bool>(__f); }
  1913. /// @overload
  1914. template<typename _Res, typename... _Args>
  1915. inline bool
  1916. operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
  1917. { return static_cast<bool>(__f); }
  1918. // [20.7.15.2.7] specialized algorithms
  1919. /**
  1920. * @brief Swap the targets of two polymorphic function object wrappers.
  1921. *
  1922. * This function will not throw an %exception.
  1923. */
  1924. template<typename _Res, typename... _Args>
  1925. inline void
  1926. swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
  1927. { __x.swap(__y); }
  1928. _GLIBCXX_END_NAMESPACE_VERSION
  1929. } // namespace std
  1930. #endif // C++11
  1931. #endif // _GLIBCXX_FUNCTIONAL