functional 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096
  1. // -*- C++ -*-
  2. //===------------------------ functional ----------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_FUNCTIONAL
  10. #define _LIBCPP_FUNCTIONAL
  11. /*
  12. functional synopsis
  13. namespace std
  14. {
  15. template <class Arg, class Result>
  16. struct unary_function
  17. {
  18. typedef Arg argument_type;
  19. typedef Result result_type;
  20. };
  21. template <class Arg1, class Arg2, class Result>
  22. struct binary_function
  23. {
  24. typedef Arg1 first_argument_type;
  25. typedef Arg2 second_argument_type;
  26. typedef Result result_type;
  27. };
  28. template <class T>
  29. class reference_wrapper
  30. : public unary_function<T1, R> // if wrapping a unary functor
  31. : public binary_function<T1, T2, R> // if wraping a binary functor
  32. {
  33. public:
  34. // types
  35. typedef T type;
  36. typedef see below result_type; // Not always defined
  37. // construct/copy/destroy
  38. reference_wrapper(T&) noexcept;
  39. reference_wrapper(T&&) = delete; // do not bind to temps
  40. reference_wrapper(const reference_wrapper<T>& x) noexcept;
  41. // assignment
  42. reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
  43. // access
  44. operator T& () const noexcept;
  45. T& get() const noexcept;
  46. // invoke
  47. template <class... ArgTypes>
  48. typename result_of<T&(ArgTypes&&...)>::type
  49. operator() (ArgTypes&&...) const;
  50. };
  51. template <class T> reference_wrapper<T> ref(T& t) noexcept;
  52. template <class T> void ref(const T&& t) = delete;
  53. template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
  54. template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
  55. template <class T> void cref(const T&& t) = delete;
  56. template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
  57. template <class T> struct unwrap_reference; // since C++20
  58. template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { }; // since C++20
  59. template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
  60. template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
  61. template <class T> // <class T=void> in C++14
  62. struct plus : binary_function<T, T, T>
  63. {
  64. T operator()(const T& x, const T& y) const;
  65. };
  66. template <class T> // <class T=void> in C++14
  67. struct minus : binary_function<T, T, T>
  68. {
  69. T operator()(const T& x, const T& y) const;
  70. };
  71. template <class T> // <class T=void> in C++14
  72. struct multiplies : binary_function<T, T, T>
  73. {
  74. T operator()(const T& x, const T& y) const;
  75. };
  76. template <class T> // <class T=void> in C++14
  77. struct divides : binary_function<T, T, T>
  78. {
  79. T operator()(const T& x, const T& y) const;
  80. };
  81. template <class T> // <class T=void> in C++14
  82. struct modulus : binary_function<T, T, T>
  83. {
  84. T operator()(const T& x, const T& y) const;
  85. };
  86. template <class T> // <class T=void> in C++14
  87. struct negate : unary_function<T, T>
  88. {
  89. T operator()(const T& x) const;
  90. };
  91. template <class T> // <class T=void> in C++14
  92. struct equal_to : binary_function<T, T, bool>
  93. {
  94. bool operator()(const T& x, const T& y) const;
  95. };
  96. template <class T> // <class T=void> in C++14
  97. struct not_equal_to : binary_function<T, T, bool>
  98. {
  99. bool operator()(const T& x, const T& y) const;
  100. };
  101. template <class T> // <class T=void> in C++14
  102. struct greater : binary_function<T, T, bool>
  103. {
  104. bool operator()(const T& x, const T& y) const;
  105. };
  106. template <class T> // <class T=void> in C++14
  107. struct less : binary_function<T, T, bool>
  108. {
  109. bool operator()(const T& x, const T& y) const;
  110. };
  111. template <class T> // <class T=void> in C++14
  112. struct greater_equal : binary_function<T, T, bool>
  113. {
  114. bool operator()(const T& x, const T& y) const;
  115. };
  116. template <class T> // <class T=void> in C++14
  117. struct less_equal : binary_function<T, T, bool>
  118. {
  119. bool operator()(const T& x, const T& y) const;
  120. };
  121. template <class T> // <class T=void> in C++14
  122. struct logical_and : binary_function<T, T, bool>
  123. {
  124. bool operator()(const T& x, const T& y) const;
  125. };
  126. template <class T> // <class T=void> in C++14
  127. struct logical_or : binary_function<T, T, bool>
  128. {
  129. bool operator()(const T& x, const T& y) const;
  130. };
  131. template <class T> // <class T=void> in C++14
  132. struct logical_not : unary_function<T, bool>
  133. {
  134. bool operator()(const T& x) const;
  135. };
  136. template <class T> // <class T=void> in C++14
  137. struct bit_and : unary_function<T, bool>
  138. {
  139. bool operator()(const T& x, const T& y) const;
  140. };
  141. template <class T> // <class T=void> in C++14
  142. struct bit_or : unary_function<T, bool>
  143. {
  144. bool operator()(const T& x, const T& y) const;
  145. };
  146. template <class T> // <class T=void> in C++14
  147. struct bit_xor : unary_function<T, bool>
  148. {
  149. bool operator()(const T& x, const T& y) const;
  150. };
  151. template <class T=void> // C++14
  152. struct bit_xor : unary_function<T, bool>
  153. {
  154. bool operator()(const T& x) const;
  155. };
  156. template <class Predicate>
  157. class unary_negate // deprecated in C++17
  158. : public unary_function<typename Predicate::argument_type, bool>
  159. {
  160. public:
  161. explicit unary_negate(const Predicate& pred);
  162. bool operator()(const typename Predicate::argument_type& x) const;
  163. };
  164. template <class Predicate> // deprecated in C++17
  165. unary_negate<Predicate> not1(const Predicate& pred);
  166. template <class Predicate>
  167. class binary_negate // deprecated in C++17
  168. : public binary_function<typename Predicate::first_argument_type,
  169. typename Predicate::second_argument_type,
  170. bool>
  171. {
  172. public:
  173. explicit binary_negate(const Predicate& pred);
  174. bool operator()(const typename Predicate::first_argument_type& x,
  175. const typename Predicate::second_argument_type& y) const;
  176. };
  177. template <class Predicate> // deprecated in C++17
  178. binary_negate<Predicate> not2(const Predicate& pred);
  179. template <class F> unspecified not_fn(F&& f); // C++17
  180. template<class T> struct is_bind_expression;
  181. template<class T> struct is_placeholder;
  182. // See C++14 20.9.9, Function object binders
  183. template <class T> inline constexpr bool is_bind_expression_v
  184. = is_bind_expression<T>::value; // C++17
  185. template <class T> inline constexpr int is_placeholder_v
  186. = is_placeholder<T>::value; // C++17
  187. template<class Fn, class... BoundArgs>
  188. unspecified bind(Fn&&, BoundArgs&&...);
  189. template<class R, class Fn, class... BoundArgs>
  190. unspecified bind(Fn&&, BoundArgs&&...);
  191. template<class F, class... Args>
  192. invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
  193. noexcept(is_nothrow_invocable_v<F, Args...>);
  194. namespace placeholders {
  195. // M is the implementation-defined number of placeholders
  196. extern unspecified _1;
  197. extern unspecified _2;
  198. .
  199. .
  200. .
  201. extern unspecified _Mp;
  202. }
  203. template <class Operation>
  204. class binder1st // deprecated in C++11, removed in C++17
  205. : public unary_function<typename Operation::second_argument_type,
  206. typename Operation::result_type>
  207. {
  208. protected:
  209. Operation op;
  210. typename Operation::first_argument_type value;
  211. public:
  212. binder1st(const Operation& x, const typename Operation::first_argument_type y);
  213. typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
  214. typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
  215. };
  216. template <class Operation, class T>
  217. binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
  218. template <class Operation>
  219. class binder2nd // deprecated in C++11, removed in C++17
  220. : public unary_function<typename Operation::first_argument_type,
  221. typename Operation::result_type>
  222. {
  223. protected:
  224. Operation op;
  225. typename Operation::second_argument_type value;
  226. public:
  227. binder2nd(const Operation& x, const typename Operation::second_argument_type y);
  228. typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
  229. typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
  230. };
  231. template <class Operation, class T>
  232. binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
  233. template <class Arg, class Result> // deprecated in C++11, removed in C++17
  234. class pointer_to_unary_function : public unary_function<Arg, Result>
  235. {
  236. public:
  237. explicit pointer_to_unary_function(Result (*f)(Arg));
  238. Result operator()(Arg x) const;
  239. };
  240. template <class Arg, class Result>
  241. pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); // deprecated in C++11, removed in C++17
  242. template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
  243. class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
  244. {
  245. public:
  246. explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
  247. Result operator()(Arg1 x, Arg2 y) const;
  248. };
  249. template <class Arg1, class Arg2, class Result>
  250. pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); // deprecated in C++11, removed in C++17
  251. template<class S, class T> // deprecated in C++11, removed in C++17
  252. class mem_fun_t : public unary_function<T*, S>
  253. {
  254. public:
  255. explicit mem_fun_t(S (T::*p)());
  256. S operator()(T* p) const;
  257. };
  258. template<class S, class T, class A>
  259. class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
  260. {
  261. public:
  262. explicit mem_fun1_t(S (T::*p)(A));
  263. S operator()(T* p, A x) const;
  264. };
  265. template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
  266. template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A)); // deprecated in C++11, removed in C++17
  267. template<class S, class T>
  268. class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
  269. {
  270. public:
  271. explicit mem_fun_ref_t(S (T::*p)());
  272. S operator()(T& p) const;
  273. };
  274. template<class S, class T, class A>
  275. class mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
  276. {
  277. public:
  278. explicit mem_fun1_ref_t(S (T::*p)(A));
  279. S operator()(T& p, A x) const;
  280. };
  281. template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
  282. template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)); // deprecated in C++11, removed in C++17
  283. template <class S, class T>
  284. class const_mem_fun_t : public unary_function<const T*, S> // deprecated in C++11, removed in C++17
  285. {
  286. public:
  287. explicit const_mem_fun_t(S (T::*p)() const);
  288. S operator()(const T* p) const;
  289. };
  290. template <class S, class T, class A>
  291. class const_mem_fun1_t : public binary_function<const T*, A, S> // deprecated in C++11, removed in C++17
  292. {
  293. public:
  294. explicit const_mem_fun1_t(S (T::*p)(A) const);
  295. S operator()(const T* p, A x) const;
  296. };
  297. template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
  298. template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const); // deprecated in C++11, removed in C++17
  299. template <class S, class T>
  300. class const_mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
  301. {
  302. public:
  303. explicit const_mem_fun_ref_t(S (T::*p)() const);
  304. S operator()(const T& p) const;
  305. };
  306. template <class S, class T, class A>
  307. class const_mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
  308. {
  309. public:
  310. explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
  311. S operator()(const T& p, A x) const;
  312. };
  313. template <class S, class T> const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const); // deprecated in C++11, removed in C++17
  314. template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const); // deprecated in C++11, removed in C++17
  315. template<class R, class T> unspecified mem_fn(R T::*);
  316. class bad_function_call
  317. : public exception
  318. {
  319. };
  320. template<class> class function; // undefined
  321. template<class R, class... ArgTypes>
  322. class function<R(ArgTypes...)>
  323. : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
  324. // ArgTypes contains T1
  325. : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
  326. // ArgTypes contains T1 and T2
  327. {
  328. public:
  329. typedef R result_type;
  330. // construct/copy/destroy:
  331. function() noexcept;
  332. function(nullptr_t) noexcept;
  333. function(const function&);
  334. function(function&&) noexcept;
  335. template<class F>
  336. function(F);
  337. template<Allocator Alloc>
  338. function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
  339. template<Allocator Alloc>
  340. function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
  341. template<Allocator Alloc>
  342. function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
  343. template<Allocator Alloc>
  344. function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
  345. template<class F, Allocator Alloc>
  346. function(allocator_arg_t, const Alloc&, F); // removed in C++17
  347. function& operator=(const function&);
  348. function& operator=(function&&) noexcept;
  349. function& operator=(nullptr_t) noexcept;
  350. template<class F>
  351. function& operator=(F&&);
  352. template<class F>
  353. function& operator=(reference_wrapper<F>) noexcept;
  354. ~function();
  355. // function modifiers:
  356. void swap(function&) noexcept;
  357. template<class F, class Alloc>
  358. void assign(F&&, const Alloc&); // Removed in C++17
  359. // function capacity:
  360. explicit operator bool() const noexcept;
  361. // function invocation:
  362. R operator()(ArgTypes...) const;
  363. // function target access:
  364. const std::type_info& target_type() const noexcept;
  365. template <typename T> T* target() noexcept;
  366. template <typename T> const T* target() const noexcept;
  367. };
  368. // Deduction guides
  369. template<class R, class ...Args>
  370. function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
  371. template<class F>
  372. function(F) -> function<see-below>; // since C++17
  373. // Null pointer comparisons:
  374. template <class R, class ... ArgTypes>
  375. bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
  376. template <class R, class ... ArgTypes>
  377. bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
  378. template <class R, class ... ArgTypes>
  379. bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
  380. template <class R, class ... ArgTypes>
  381. bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
  382. // specialized algorithms:
  383. template <class R, class ... ArgTypes>
  384. void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
  385. template <class T> struct hash;
  386. template <> struct hash<bool>;
  387. template <> struct hash<char>;
  388. template <> struct hash<signed char>;
  389. template <> struct hash<unsigned char>;
  390. template <> struct hash<char16_t>;
  391. template <> struct hash<char32_t>;
  392. template <> struct hash<wchar_t>;
  393. template <> struct hash<short>;
  394. template <> struct hash<unsigned short>;
  395. template <> struct hash<int>;
  396. template <> struct hash<unsigned int>;
  397. template <> struct hash<long>;
  398. template <> struct hash<long long>;
  399. template <> struct hash<unsigned long>;
  400. template <> struct hash<unsigned long long>;
  401. template <> struct hash<float>;
  402. template <> struct hash<double>;
  403. template <> struct hash<long double>;
  404. template<class T> struct hash<T*>;
  405. template <> struct hash<nullptr_t>; // C++17
  406. } // std
  407. POLICY: For non-variadic implementations, the number of arguments is limited
  408. to 3. It is hoped that the need for non-variadic implementations
  409. will be minimal.
  410. */
  411. #include <__config>
  412. #include <type_traits>
  413. #include <typeinfo>
  414. #include <exception>
  415. #include <memory>
  416. #include <tuple>
  417. #include <utility>
  418. #include <version>
  419. #include <__functional_base>
  420. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  421. #pragma GCC system_header
  422. #endif
  423. _LIBCPP_BEGIN_NAMESPACE_STD
  424. #if _LIBCPP_STD_VER > 11
  425. template <class _Tp = void>
  426. #else
  427. template <class _Tp>
  428. #endif
  429. struct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
  430. {
  431. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  432. _Tp operator()(const _Tp& __x, const _Tp& __y) const
  433. {return __x + __y;}
  434. };
  435. #if _LIBCPP_STD_VER > 11
  436. template <>
  437. struct _LIBCPP_TEMPLATE_VIS plus<void>
  438. {
  439. template <class _T1, class _T2>
  440. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  441. auto operator()(_T1&& __t, _T2&& __u) const
  442. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
  443. -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
  444. { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
  445. typedef void is_transparent;
  446. };
  447. #endif
  448. #if _LIBCPP_STD_VER > 11
  449. template <class _Tp = void>
  450. #else
  451. template <class _Tp>
  452. #endif
  453. struct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
  454. {
  455. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  456. _Tp operator()(const _Tp& __x, const _Tp& __y) const
  457. {return __x - __y;}
  458. };
  459. #if _LIBCPP_STD_VER > 11
  460. template <>
  461. struct _LIBCPP_TEMPLATE_VIS minus<void>
  462. {
  463. template <class _T1, class _T2>
  464. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  465. auto operator()(_T1&& __t, _T2&& __u) const
  466. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
  467. -> decltype (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
  468. { return _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
  469. typedef void is_transparent;
  470. };
  471. #endif
  472. #if _LIBCPP_STD_VER > 11
  473. template <class _Tp = void>
  474. #else
  475. template <class _Tp>
  476. #endif
  477. struct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
  478. {
  479. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  480. _Tp operator()(const _Tp& __x, const _Tp& __y) const
  481. {return __x * __y;}
  482. };
  483. #if _LIBCPP_STD_VER > 11
  484. template <>
  485. struct _LIBCPP_TEMPLATE_VIS multiplies<void>
  486. {
  487. template <class _T1, class _T2>
  488. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  489. auto operator()(_T1&& __t, _T2&& __u) const
  490. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
  491. -> decltype (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
  492. { return _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
  493. typedef void is_transparent;
  494. };
  495. #endif
  496. #if _LIBCPP_STD_VER > 11
  497. template <class _Tp = void>
  498. #else
  499. template <class _Tp>
  500. #endif
  501. struct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
  502. {
  503. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  504. _Tp operator()(const _Tp& __x, const _Tp& __y) const
  505. {return __x / __y;}
  506. };
  507. #if _LIBCPP_STD_VER > 11
  508. template <>
  509. struct _LIBCPP_TEMPLATE_VIS divides<void>
  510. {
  511. template <class _T1, class _T2>
  512. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  513. auto operator()(_T1&& __t, _T2&& __u) const
  514. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
  515. -> decltype (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
  516. { return _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
  517. typedef void is_transparent;
  518. };
  519. #endif
  520. #if _LIBCPP_STD_VER > 11
  521. template <class _Tp = void>
  522. #else
  523. template <class _Tp>
  524. #endif
  525. struct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
  526. {
  527. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  528. _Tp operator()(const _Tp& __x, const _Tp& __y) const
  529. {return __x % __y;}
  530. };
  531. #if _LIBCPP_STD_VER > 11
  532. template <>
  533. struct _LIBCPP_TEMPLATE_VIS modulus<void>
  534. {
  535. template <class _T1, class _T2>
  536. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  537. auto operator()(_T1&& __t, _T2&& __u) const
  538. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
  539. -> decltype (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
  540. { return _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
  541. typedef void is_transparent;
  542. };
  543. #endif
  544. #if _LIBCPP_STD_VER > 11
  545. template <class _Tp = void>
  546. #else
  547. template <class _Tp>
  548. #endif
  549. struct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
  550. {
  551. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  552. _Tp operator()(const _Tp& __x) const
  553. {return -__x;}
  554. };
  555. #if _LIBCPP_STD_VER > 11
  556. template <>
  557. struct _LIBCPP_TEMPLATE_VIS negate<void>
  558. {
  559. template <class _Tp>
  560. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  561. auto operator()(_Tp&& __x) const
  562. _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
  563. -> decltype (- _VSTD::forward<_Tp>(__x))
  564. { return - _VSTD::forward<_Tp>(__x); }
  565. typedef void is_transparent;
  566. };
  567. #endif
  568. #if _LIBCPP_STD_VER > 11
  569. template <class _Tp = void>
  570. #else
  571. template <class _Tp>
  572. #endif
  573. struct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
  574. {
  575. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  576. bool operator()(const _Tp& __x, const _Tp& __y) const
  577. {return __x == __y;}
  578. };
  579. #if _LIBCPP_STD_VER > 11
  580. template <>
  581. struct _LIBCPP_TEMPLATE_VIS equal_to<void>
  582. {
  583. template <class _T1, class _T2>
  584. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  585. auto operator()(_T1&& __t, _T2&& __u) const
  586. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
  587. -> decltype (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
  588. { return _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
  589. typedef void is_transparent;
  590. };
  591. #endif
  592. #if _LIBCPP_STD_VER > 11
  593. template <class _Tp = void>
  594. #else
  595. template <class _Tp>
  596. #endif
  597. struct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
  598. {
  599. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  600. bool operator()(const _Tp& __x, const _Tp& __y) const
  601. {return __x != __y;}
  602. };
  603. #if _LIBCPP_STD_VER > 11
  604. template <>
  605. struct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
  606. {
  607. template <class _T1, class _T2>
  608. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  609. auto operator()(_T1&& __t, _T2&& __u) const
  610. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
  611. -> decltype (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
  612. { return _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
  613. typedef void is_transparent;
  614. };
  615. #endif
  616. #if _LIBCPP_STD_VER > 11
  617. template <class _Tp = void>
  618. #else
  619. template <class _Tp>
  620. #endif
  621. struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
  622. {
  623. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  624. bool operator()(const _Tp& __x, const _Tp& __y) const
  625. {return __x > __y;}
  626. };
  627. #if _LIBCPP_STD_VER > 11
  628. template <>
  629. struct _LIBCPP_TEMPLATE_VIS greater<void>
  630. {
  631. template <class _T1, class _T2>
  632. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  633. auto operator()(_T1&& __t, _T2&& __u) const
  634. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
  635. -> decltype (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
  636. { return _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
  637. typedef void is_transparent;
  638. };
  639. #endif
  640. // less in <__functional_base>
  641. #if _LIBCPP_STD_VER > 11
  642. template <class _Tp = void>
  643. #else
  644. template <class _Tp>
  645. #endif
  646. struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
  647. {
  648. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  649. bool operator()(const _Tp& __x, const _Tp& __y) const
  650. {return __x >= __y;}
  651. };
  652. #if _LIBCPP_STD_VER > 11
  653. template <>
  654. struct _LIBCPP_TEMPLATE_VIS greater_equal<void>
  655. {
  656. template <class _T1, class _T2>
  657. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  658. auto operator()(_T1&& __t, _T2&& __u) const
  659. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
  660. -> decltype (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
  661. { return _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
  662. typedef void is_transparent;
  663. };
  664. #endif
  665. #if _LIBCPP_STD_VER > 11
  666. template <class _Tp = void>
  667. #else
  668. template <class _Tp>
  669. #endif
  670. struct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
  671. {
  672. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  673. bool operator()(const _Tp& __x, const _Tp& __y) const
  674. {return __x <= __y;}
  675. };
  676. #if _LIBCPP_STD_VER > 11
  677. template <>
  678. struct _LIBCPP_TEMPLATE_VIS less_equal<void>
  679. {
  680. template <class _T1, class _T2>
  681. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  682. auto operator()(_T1&& __t, _T2&& __u) const
  683. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
  684. -> decltype (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
  685. { return _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
  686. typedef void is_transparent;
  687. };
  688. #endif
  689. #if _LIBCPP_STD_VER > 11
  690. template <class _Tp = void>
  691. #else
  692. template <class _Tp>
  693. #endif
  694. struct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
  695. {
  696. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  697. bool operator()(const _Tp& __x, const _Tp& __y) const
  698. {return __x && __y;}
  699. };
  700. #if _LIBCPP_STD_VER > 11
  701. template <>
  702. struct _LIBCPP_TEMPLATE_VIS logical_and<void>
  703. {
  704. template <class _T1, class _T2>
  705. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  706. auto operator()(_T1&& __t, _T2&& __u) const
  707. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
  708. -> decltype (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
  709. { return _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
  710. typedef void is_transparent;
  711. };
  712. #endif
  713. #if _LIBCPP_STD_VER > 11
  714. template <class _Tp = void>
  715. #else
  716. template <class _Tp>
  717. #endif
  718. struct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
  719. {
  720. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  721. bool operator()(const _Tp& __x, const _Tp& __y) const
  722. {return __x || __y;}
  723. };
  724. #if _LIBCPP_STD_VER > 11
  725. template <>
  726. struct _LIBCPP_TEMPLATE_VIS logical_or<void>
  727. {
  728. template <class _T1, class _T2>
  729. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  730. auto operator()(_T1&& __t, _T2&& __u) const
  731. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
  732. -> decltype (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
  733. { return _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
  734. typedef void is_transparent;
  735. };
  736. #endif
  737. #if _LIBCPP_STD_VER > 11
  738. template <class _Tp = void>
  739. #else
  740. template <class _Tp>
  741. #endif
  742. struct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
  743. {
  744. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  745. bool operator()(const _Tp& __x) const
  746. {return !__x;}
  747. };
  748. #if _LIBCPP_STD_VER > 11
  749. template <>
  750. struct _LIBCPP_TEMPLATE_VIS logical_not<void>
  751. {
  752. template <class _Tp>
  753. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  754. auto operator()(_Tp&& __x) const
  755. _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
  756. -> decltype (!_VSTD::forward<_Tp>(__x))
  757. { return !_VSTD::forward<_Tp>(__x); }
  758. typedef void is_transparent;
  759. };
  760. #endif
  761. #if _LIBCPP_STD_VER > 11
  762. template <class _Tp = void>
  763. #else
  764. template <class _Tp>
  765. #endif
  766. struct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
  767. {
  768. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  769. _Tp operator()(const _Tp& __x, const _Tp& __y) const
  770. {return __x & __y;}
  771. };
  772. #if _LIBCPP_STD_VER > 11
  773. template <>
  774. struct _LIBCPP_TEMPLATE_VIS bit_and<void>
  775. {
  776. template <class _T1, class _T2>
  777. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  778. auto operator()(_T1&& __t, _T2&& __u) const
  779. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
  780. -> decltype (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
  781. { return _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
  782. typedef void is_transparent;
  783. };
  784. #endif
  785. #if _LIBCPP_STD_VER > 11
  786. template <class _Tp = void>
  787. #else
  788. template <class _Tp>
  789. #endif
  790. struct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
  791. {
  792. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  793. _Tp operator()(const _Tp& __x, const _Tp& __y) const
  794. {return __x | __y;}
  795. };
  796. #if _LIBCPP_STD_VER > 11
  797. template <>
  798. struct _LIBCPP_TEMPLATE_VIS bit_or<void>
  799. {
  800. template <class _T1, class _T2>
  801. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  802. auto operator()(_T1&& __t, _T2&& __u) const
  803. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
  804. -> decltype (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
  805. { return _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
  806. typedef void is_transparent;
  807. };
  808. #endif
  809. #if _LIBCPP_STD_VER > 11
  810. template <class _Tp = void>
  811. #else
  812. template <class _Tp>
  813. #endif
  814. struct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
  815. {
  816. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  817. _Tp operator()(const _Tp& __x, const _Tp& __y) const
  818. {return __x ^ __y;}
  819. };
  820. #if _LIBCPP_STD_VER > 11
  821. template <>
  822. struct _LIBCPP_TEMPLATE_VIS bit_xor<void>
  823. {
  824. template <class _T1, class _T2>
  825. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  826. auto operator()(_T1&& __t, _T2&& __u) const
  827. _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
  828. -> decltype (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
  829. { return _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
  830. typedef void is_transparent;
  831. };
  832. #endif
  833. #if _LIBCPP_STD_VER > 11
  834. template <class _Tp = void>
  835. struct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
  836. {
  837. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  838. _Tp operator()(const _Tp& __x) const
  839. {return ~__x;}
  840. };
  841. template <>
  842. struct _LIBCPP_TEMPLATE_VIS bit_not<void>
  843. {
  844. template <class _Tp>
  845. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  846. auto operator()(_Tp&& __x) const
  847. _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
  848. -> decltype (~_VSTD::forward<_Tp>(__x))
  849. { return ~_VSTD::forward<_Tp>(__x); }
  850. typedef void is_transparent;
  851. };
  852. #endif
  853. template <class _Predicate>
  854. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
  855. : public unary_function<typename _Predicate::argument_type, bool>
  856. {
  857. _Predicate __pred_;
  858. public:
  859. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  860. explicit unary_negate(const _Predicate& __pred)
  861. : __pred_(__pred) {}
  862. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  863. bool operator()(const typename _Predicate::argument_type& __x) const
  864. {return !__pred_(__x);}
  865. };
  866. template <class _Predicate>
  867. _LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  868. unary_negate<_Predicate>
  869. not1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
  870. template <class _Predicate>
  871. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
  872. : public binary_function<typename _Predicate::first_argument_type,
  873. typename _Predicate::second_argument_type,
  874. bool>
  875. {
  876. _Predicate __pred_;
  877. public:
  878. _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
  879. binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
  880. _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  881. bool operator()(const typename _Predicate::first_argument_type& __x,
  882. const typename _Predicate::second_argument_type& __y) const
  883. {return !__pred_(__x, __y);}
  884. };
  885. template <class _Predicate>
  886. _LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  887. binary_negate<_Predicate>
  888. not2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
  889. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
  890. template <class __Operation>
  891. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
  892. : public unary_function<typename __Operation::second_argument_type,
  893. typename __Operation::result_type>
  894. {
  895. protected:
  896. __Operation op;
  897. typename __Operation::first_argument_type value;
  898. public:
  899. _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
  900. const typename __Operation::first_argument_type __y)
  901. : op(__x), value(__y) {}
  902. _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
  903. (typename __Operation::second_argument_type& __x) const
  904. {return op(value, __x);}
  905. _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
  906. (const typename __Operation::second_argument_type& __x) const
  907. {return op(value, __x);}
  908. };
  909. template <class __Operation, class _Tp>
  910. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  911. binder1st<__Operation>
  912. bind1st(const __Operation& __op, const _Tp& __x)
  913. {return binder1st<__Operation>(__op, __x);}
  914. template <class __Operation>
  915. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
  916. : public unary_function<typename __Operation::first_argument_type,
  917. typename __Operation::result_type>
  918. {
  919. protected:
  920. __Operation op;
  921. typename __Operation::second_argument_type value;
  922. public:
  923. _LIBCPP_INLINE_VISIBILITY
  924. binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
  925. : op(__x), value(__y) {}
  926. _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
  927. ( typename __Operation::first_argument_type& __x) const
  928. {return op(__x, value);}
  929. _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
  930. (const typename __Operation::first_argument_type& __x) const
  931. {return op(__x, value);}
  932. };
  933. template <class __Operation, class _Tp>
  934. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  935. binder2nd<__Operation>
  936. bind2nd(const __Operation& __op, const _Tp& __x)
  937. {return binder2nd<__Operation>(__op, __x);}
  938. template <class _Arg, class _Result>
  939. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
  940. : public unary_function<_Arg, _Result>
  941. {
  942. _Result (*__f_)(_Arg);
  943. public:
  944. _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
  945. : __f_(__f) {}
  946. _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
  947. {return __f_(__x);}
  948. };
  949. template <class _Arg, class _Result>
  950. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  951. pointer_to_unary_function<_Arg,_Result>
  952. ptr_fun(_Result (*__f)(_Arg))
  953. {return pointer_to_unary_function<_Arg,_Result>(__f);}
  954. template <class _Arg1, class _Arg2, class _Result>
  955. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
  956. : public binary_function<_Arg1, _Arg2, _Result>
  957. {
  958. _Result (*__f_)(_Arg1, _Arg2);
  959. public:
  960. _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
  961. : __f_(__f) {}
  962. _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
  963. {return __f_(__x, __y);}
  964. };
  965. template <class _Arg1, class _Arg2, class _Result>
  966. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  967. pointer_to_binary_function<_Arg1,_Arg2,_Result>
  968. ptr_fun(_Result (*__f)(_Arg1,_Arg2))
  969. {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
  970. template<class _Sp, class _Tp>
  971. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
  972. : public unary_function<_Tp*, _Sp>
  973. {
  974. _Sp (_Tp::*__p_)();
  975. public:
  976. _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
  977. : __p_(__p) {}
  978. _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
  979. {return (__p->*__p_)();}
  980. };
  981. template<class _Sp, class _Tp, class _Ap>
  982. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
  983. : public binary_function<_Tp*, _Ap, _Sp>
  984. {
  985. _Sp (_Tp::*__p_)(_Ap);
  986. public:
  987. _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
  988. : __p_(__p) {}
  989. _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
  990. {return (__p->*__p_)(__x);}
  991. };
  992. template<class _Sp, class _Tp>
  993. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  994. mem_fun_t<_Sp,_Tp>
  995. mem_fun(_Sp (_Tp::*__f)())
  996. {return mem_fun_t<_Sp,_Tp>(__f);}
  997. template<class _Sp, class _Tp, class _Ap>
  998. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  999. mem_fun1_t<_Sp,_Tp,_Ap>
  1000. mem_fun(_Sp (_Tp::*__f)(_Ap))
  1001. {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
  1002. template<class _Sp, class _Tp>
  1003. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
  1004. : public unary_function<_Tp, _Sp>
  1005. {
  1006. _Sp (_Tp::*__p_)();
  1007. public:
  1008. _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
  1009. : __p_(__p) {}
  1010. _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
  1011. {return (__p.*__p_)();}
  1012. };
  1013. template<class _Sp, class _Tp, class _Ap>
  1014. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
  1015. : public binary_function<_Tp, _Ap, _Sp>
  1016. {
  1017. _Sp (_Tp::*__p_)(_Ap);
  1018. public:
  1019. _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
  1020. : __p_(__p) {}
  1021. _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
  1022. {return (__p.*__p_)(__x);}
  1023. };
  1024. template<class _Sp, class _Tp>
  1025. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  1026. mem_fun_ref_t<_Sp,_Tp>
  1027. mem_fun_ref(_Sp (_Tp::*__f)())
  1028. {return mem_fun_ref_t<_Sp,_Tp>(__f);}
  1029. template<class _Sp, class _Tp, class _Ap>
  1030. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  1031. mem_fun1_ref_t<_Sp,_Tp,_Ap>
  1032. mem_fun_ref(_Sp (_Tp::*__f)(_Ap))
  1033. {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
  1034. template <class _Sp, class _Tp>
  1035. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
  1036. : public unary_function<const _Tp*, _Sp>
  1037. {
  1038. _Sp (_Tp::*__p_)() const;
  1039. public:
  1040. _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
  1041. : __p_(__p) {}
  1042. _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
  1043. {return (__p->*__p_)();}
  1044. };
  1045. template <class _Sp, class _Tp, class _Ap>
  1046. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
  1047. : public binary_function<const _Tp*, _Ap, _Sp>
  1048. {
  1049. _Sp (_Tp::*__p_)(_Ap) const;
  1050. public:
  1051. _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
  1052. : __p_(__p) {}
  1053. _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
  1054. {return (__p->*__p_)(__x);}
  1055. };
  1056. template <class _Sp, class _Tp>
  1057. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  1058. const_mem_fun_t<_Sp,_Tp>
  1059. mem_fun(_Sp (_Tp::*__f)() const)
  1060. {return const_mem_fun_t<_Sp,_Tp>(__f);}
  1061. template <class _Sp, class _Tp, class _Ap>
  1062. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  1063. const_mem_fun1_t<_Sp,_Tp,_Ap>
  1064. mem_fun(_Sp (_Tp::*__f)(_Ap) const)
  1065. {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
  1066. template <class _Sp, class _Tp>
  1067. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
  1068. : public unary_function<_Tp, _Sp>
  1069. {
  1070. _Sp (_Tp::*__p_)() const;
  1071. public:
  1072. _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
  1073. : __p_(__p) {}
  1074. _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
  1075. {return (__p.*__p_)();}
  1076. };
  1077. template <class _Sp, class _Tp, class _Ap>
  1078. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
  1079. : public binary_function<_Tp, _Ap, _Sp>
  1080. {
  1081. _Sp (_Tp::*__p_)(_Ap) const;
  1082. public:
  1083. _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
  1084. : __p_(__p) {}
  1085. _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
  1086. {return (__p.*__p_)(__x);}
  1087. };
  1088. template <class _Sp, class _Tp>
  1089. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  1090. const_mem_fun_ref_t<_Sp,_Tp>
  1091. mem_fun_ref(_Sp (_Tp::*__f)() const)
  1092. {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
  1093. template <class _Sp, class _Tp, class _Ap>
  1094. _LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
  1095. const_mem_fun1_ref_t<_Sp,_Tp,_Ap>
  1096. mem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
  1097. {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
  1098. #endif
  1099. ////////////////////////////////////////////////////////////////////////////////
  1100. // MEMFUN
  1101. //==============================================================================
  1102. template <class _Tp>
  1103. class __mem_fn
  1104. : public __weak_result_type<_Tp>
  1105. {
  1106. public:
  1107. // types
  1108. typedef _Tp type;
  1109. private:
  1110. type __f_;
  1111. public:
  1112. _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
  1113. #ifndef _LIBCPP_CXX03_LANG
  1114. // invoke
  1115. template <class... _ArgTypes>
  1116. _LIBCPP_INLINE_VISIBILITY
  1117. typename __invoke_return<type, _ArgTypes...>::type
  1118. operator() (_ArgTypes&&... __args) const {
  1119. return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
  1120. }
  1121. #else
  1122. template <class _A0>
  1123. _LIBCPP_INLINE_VISIBILITY
  1124. typename __invoke_return0<type, _A0>::type
  1125. operator() (_A0& __a0) const {
  1126. return __invoke(__f_, __a0);
  1127. }
  1128. template <class _A0>
  1129. _LIBCPP_INLINE_VISIBILITY
  1130. typename __invoke_return0<type, _A0 const>::type
  1131. operator() (_A0 const& __a0) const {
  1132. return __invoke(__f_, __a0);
  1133. }
  1134. template <class _A0, class _A1>
  1135. _LIBCPP_INLINE_VISIBILITY
  1136. typename __invoke_return1<type, _A0, _A1>::type
  1137. operator() (_A0& __a0, _A1& __a1) const {
  1138. return __invoke(__f_, __a0, __a1);
  1139. }
  1140. template <class _A0, class _A1>
  1141. _LIBCPP_INLINE_VISIBILITY
  1142. typename __invoke_return1<type, _A0 const, _A1>::type
  1143. operator() (_A0 const& __a0, _A1& __a1) const {
  1144. return __invoke(__f_, __a0, __a1);
  1145. }
  1146. template <class _A0, class _A1>
  1147. _LIBCPP_INLINE_VISIBILITY
  1148. typename __invoke_return1<type, _A0, _A1 const>::type
  1149. operator() (_A0& __a0, _A1 const& __a1) const {
  1150. return __invoke(__f_, __a0, __a1);
  1151. }
  1152. template <class _A0, class _A1>
  1153. _LIBCPP_INLINE_VISIBILITY
  1154. typename __invoke_return1<type, _A0 const, _A1 const>::type
  1155. operator() (_A0 const& __a0, _A1 const& __a1) const {
  1156. return __invoke(__f_, __a0, __a1);
  1157. }
  1158. template <class _A0, class _A1, class _A2>
  1159. _LIBCPP_INLINE_VISIBILITY
  1160. typename __invoke_return2<type, _A0, _A1, _A2>::type
  1161. operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
  1162. return __invoke(__f_, __a0, __a1, __a2);
  1163. }
  1164. template <class _A0, class _A1, class _A2>
  1165. _LIBCPP_INLINE_VISIBILITY
  1166. typename __invoke_return2<type, _A0 const, _A1, _A2>::type
  1167. operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
  1168. return __invoke(__f_, __a0, __a1, __a2);
  1169. }
  1170. template <class _A0, class _A1, class _A2>
  1171. _LIBCPP_INLINE_VISIBILITY
  1172. typename __invoke_return2<type, _A0, _A1 const, _A2>::type
  1173. operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
  1174. return __invoke(__f_, __a0, __a1, __a2);
  1175. }
  1176. template <class _A0, class _A1, class _A2>
  1177. _LIBCPP_INLINE_VISIBILITY
  1178. typename __invoke_return2<type, _A0, _A1, _A2 const>::type
  1179. operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
  1180. return __invoke(__f_, __a0, __a1, __a2);
  1181. }
  1182. template <class _A0, class _A1, class _A2>
  1183. _LIBCPP_INLINE_VISIBILITY
  1184. typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
  1185. operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
  1186. return __invoke(__f_, __a0, __a1, __a2);
  1187. }
  1188. template <class _A0, class _A1, class _A2>
  1189. _LIBCPP_INLINE_VISIBILITY
  1190. typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
  1191. operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
  1192. return __invoke(__f_, __a0, __a1, __a2);
  1193. }
  1194. template <class _A0, class _A1, class _A2>
  1195. _LIBCPP_INLINE_VISIBILITY
  1196. typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
  1197. operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
  1198. return __invoke(__f_, __a0, __a1, __a2);
  1199. }
  1200. template <class _A0, class _A1, class _A2>
  1201. _LIBCPP_INLINE_VISIBILITY
  1202. typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
  1203. operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
  1204. return __invoke(__f_, __a0, __a1, __a2);
  1205. }
  1206. #endif
  1207. };
  1208. template<class _Rp, class _Tp>
  1209. inline _LIBCPP_INLINE_VISIBILITY
  1210. __mem_fn<_Rp _Tp::*>
  1211. mem_fn(_Rp _Tp::* __pm) _NOEXCEPT
  1212. {
  1213. return __mem_fn<_Rp _Tp::*>(__pm);
  1214. }
  1215. ////////////////////////////////////////////////////////////////////////////////
  1216. // FUNCTION
  1217. //==============================================================================
  1218. // bad_function_call
  1219. class _LIBCPP_EXCEPTION_ABI bad_function_call
  1220. : public exception
  1221. {
  1222. #ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
  1223. public:
  1224. virtual ~bad_function_call() _NOEXCEPT;
  1225. virtual const char* what() const _NOEXCEPT;
  1226. #endif
  1227. };
  1228. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  1229. void __throw_bad_function_call()
  1230. {
  1231. #ifndef _LIBCPP_NO_EXCEPTIONS
  1232. throw bad_function_call();
  1233. #else
  1234. _VSTD::abort();
  1235. #endif
  1236. }
  1237. template<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
  1238. namespace __function
  1239. {
  1240. template<class _Rp>
  1241. struct __maybe_derive_from_unary_function
  1242. {
  1243. };
  1244. template<class _Rp, class _A1>
  1245. struct __maybe_derive_from_unary_function<_Rp(_A1)>
  1246. : public unary_function<_A1, _Rp>
  1247. {
  1248. };
  1249. template<class _Rp>
  1250. struct __maybe_derive_from_binary_function
  1251. {
  1252. };
  1253. template<class _Rp, class _A1, class _A2>
  1254. struct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
  1255. : public binary_function<_A1, _A2, _Rp>
  1256. {
  1257. };
  1258. template <class _Fp>
  1259. _LIBCPP_INLINE_VISIBILITY
  1260. bool __not_null(_Fp const&) { return true; }
  1261. template <class _Fp>
  1262. _LIBCPP_INLINE_VISIBILITY
  1263. bool __not_null(_Fp* __ptr) { return __ptr; }
  1264. template <class _Ret, class _Class>
  1265. _LIBCPP_INLINE_VISIBILITY
  1266. bool __not_null(_Ret _Class::*__ptr) { return __ptr; }
  1267. template <class _Fp>
  1268. _LIBCPP_INLINE_VISIBILITY
  1269. bool __not_null(function<_Fp> const& __f) { return !!__f; }
  1270. } // namespace __function
  1271. #ifndef _LIBCPP_CXX03_LANG
  1272. namespace __function {
  1273. // __alloc_func holds a functor and an allocator.
  1274. template <class _Fp, class _Ap, class _FB> class __alloc_func;
  1275. template <class _Fp, class _FB>
  1276. class __default_alloc_func;
  1277. template <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
  1278. class __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
  1279. {
  1280. __compressed_pair<_Fp, _Ap> __f_;
  1281. public:
  1282. typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
  1283. typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
  1284. _LIBCPP_INLINE_VISIBILITY
  1285. const _Target& __target() const { return __f_.first(); }
  1286. // WIN32 APIs may define __allocator, so use __get_allocator instead.
  1287. _LIBCPP_INLINE_VISIBILITY
  1288. const _Alloc& __get_allocator() const { return __f_.second(); }
  1289. _LIBCPP_INLINE_VISIBILITY
  1290. explicit __alloc_func(_Target&& __f)
  1291. : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
  1292. _VSTD::forward_as_tuple())
  1293. {
  1294. }
  1295. _LIBCPP_INLINE_VISIBILITY
  1296. explicit __alloc_func(const _Target& __f, const _Alloc& __a)
  1297. : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
  1298. _VSTD::forward_as_tuple(__a))
  1299. {
  1300. }
  1301. _LIBCPP_INLINE_VISIBILITY
  1302. explicit __alloc_func(const _Target& __f, _Alloc&& __a)
  1303. : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
  1304. _VSTD::forward_as_tuple(_VSTD::move(__a)))
  1305. {
  1306. }
  1307. _LIBCPP_INLINE_VISIBILITY
  1308. explicit __alloc_func(_Target&& __f, _Alloc&& __a)
  1309. : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
  1310. _VSTD::forward_as_tuple(_VSTD::move(__a)))
  1311. {
  1312. }
  1313. _LIBCPP_INLINE_VISIBILITY
  1314. _Rp operator()(_ArgTypes&&... __arg)
  1315. {
  1316. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  1317. return _Invoker::__call(__f_.first(),
  1318. _VSTD::forward<_ArgTypes>(__arg)...);
  1319. }
  1320. _LIBCPP_INLINE_VISIBILITY
  1321. __alloc_func* __clone() const
  1322. {
  1323. typedef allocator_traits<_Alloc> __alloc_traits;
  1324. typedef
  1325. typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
  1326. _AA;
  1327. _AA __a(__f_.second());
  1328. typedef __allocator_destructor<_AA> _Dp;
  1329. unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1330. ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
  1331. return __hold.release();
  1332. }
  1333. _LIBCPP_INLINE_VISIBILITY
  1334. void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
  1335. static void __destroy_and_delete(__alloc_func* __f) {
  1336. typedef allocator_traits<_Alloc> __alloc_traits;
  1337. typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
  1338. _FunAlloc;
  1339. _FunAlloc __a(__f->__get_allocator());
  1340. __f->destroy();
  1341. __a.deallocate(__f, 1);
  1342. }
  1343. };
  1344. template <class _Fp, class _Rp, class... _ArgTypes>
  1345. class __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
  1346. _Fp __f_;
  1347. public:
  1348. typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
  1349. _LIBCPP_INLINE_VISIBILITY
  1350. const _Target& __target() const { return __f_; }
  1351. _LIBCPP_INLINE_VISIBILITY
  1352. explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
  1353. _LIBCPP_INLINE_VISIBILITY
  1354. explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
  1355. _LIBCPP_INLINE_VISIBILITY
  1356. _Rp operator()(_ArgTypes&&... __arg) {
  1357. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  1358. return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
  1359. }
  1360. _LIBCPP_INLINE_VISIBILITY
  1361. __default_alloc_func* __clone() const {
  1362. __builtin_new_allocator::__holder_t __hold =
  1363. __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
  1364. __default_alloc_func* __res =
  1365. ::new (__hold.get()) __default_alloc_func(__f_);
  1366. (void)__hold.release();
  1367. return __res;
  1368. }
  1369. _LIBCPP_INLINE_VISIBILITY
  1370. void destroy() _NOEXCEPT { __f_.~_Target(); }
  1371. static void __destroy_and_delete(__default_alloc_func* __f) {
  1372. __f->destroy();
  1373. __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
  1374. }
  1375. };
  1376. // __base provides an abstract interface for copyable functors.
  1377. template<class _Fp> class __base;
  1378. template<class _Rp, class ..._ArgTypes>
  1379. class __base<_Rp(_ArgTypes...)>
  1380. {
  1381. __base(const __base&);
  1382. __base& operator=(const __base&);
  1383. public:
  1384. _LIBCPP_INLINE_VISIBILITY __base() {}
  1385. _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
  1386. virtual __base* __clone() const = 0;
  1387. virtual void __clone(__base*) const = 0;
  1388. virtual void destroy() _NOEXCEPT = 0;
  1389. virtual void destroy_deallocate() _NOEXCEPT = 0;
  1390. virtual _Rp operator()(_ArgTypes&& ...) = 0;
  1391. #ifndef _LIBCPP_NO_RTTI
  1392. virtual const void* target(const type_info&) const _NOEXCEPT = 0;
  1393. virtual const std::type_info& target_type() const _NOEXCEPT = 0;
  1394. #endif // _LIBCPP_NO_RTTI
  1395. };
  1396. // __func implements __base for a given functor type.
  1397. template<class _FD, class _Alloc, class _FB> class __func;
  1398. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1399. class __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
  1400. : public __base<_Rp(_ArgTypes...)>
  1401. {
  1402. __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
  1403. public:
  1404. _LIBCPP_INLINE_VISIBILITY
  1405. explicit __func(_Fp&& __f)
  1406. : __f_(_VSTD::move(__f)) {}
  1407. _LIBCPP_INLINE_VISIBILITY
  1408. explicit __func(const _Fp& __f, const _Alloc& __a)
  1409. : __f_(__f, __a) {}
  1410. _LIBCPP_INLINE_VISIBILITY
  1411. explicit __func(const _Fp& __f, _Alloc&& __a)
  1412. : __f_(__f, _VSTD::move(__a)) {}
  1413. _LIBCPP_INLINE_VISIBILITY
  1414. explicit __func(_Fp&& __f, _Alloc&& __a)
  1415. : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
  1416. virtual __base<_Rp(_ArgTypes...)>* __clone() const;
  1417. virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
  1418. virtual void destroy() _NOEXCEPT;
  1419. virtual void destroy_deallocate() _NOEXCEPT;
  1420. virtual _Rp operator()(_ArgTypes&&... __arg);
  1421. #ifndef _LIBCPP_NO_RTTI
  1422. virtual const void* target(const type_info&) const _NOEXCEPT;
  1423. virtual const std::type_info& target_type() const _NOEXCEPT;
  1424. #endif // _LIBCPP_NO_RTTI
  1425. };
  1426. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1427. __base<_Rp(_ArgTypes...)>*
  1428. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
  1429. {
  1430. typedef allocator_traits<_Alloc> __alloc_traits;
  1431. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1432. _Ap __a(__f_.__get_allocator());
  1433. typedef __allocator_destructor<_Ap> _Dp;
  1434. unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  1435. ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
  1436. return __hold.release();
  1437. }
  1438. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1439. void
  1440. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
  1441. {
  1442. ::new (__p) __func(__f_.__target(), __f_.__get_allocator());
  1443. }
  1444. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1445. void
  1446. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
  1447. {
  1448. __f_.destroy();
  1449. }
  1450. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1451. void
  1452. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
  1453. {
  1454. typedef allocator_traits<_Alloc> __alloc_traits;
  1455. typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
  1456. _Ap __a(__f_.__get_allocator());
  1457. __f_.destroy();
  1458. __a.deallocate(this, 1);
  1459. }
  1460. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1461. _Rp
  1462. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
  1463. {
  1464. return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  1465. }
  1466. #ifndef _LIBCPP_NO_RTTI
  1467. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1468. const void*
  1469. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
  1470. {
  1471. if (__ti == typeid(_Fp))
  1472. return &__f_.__target();
  1473. return (const void*)0;
  1474. }
  1475. template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
  1476. const std::type_info&
  1477. __func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
  1478. {
  1479. return typeid(_Fp);
  1480. }
  1481. #endif // _LIBCPP_NO_RTTI
  1482. // __value_func creates a value-type from a __func.
  1483. template <class _Fp> class __value_func;
  1484. template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
  1485. {
  1486. typename aligned_storage<3 * sizeof(void*)>::type __buf_;
  1487. typedef __base<_Rp(_ArgTypes...)> __func;
  1488. __func* __f_;
  1489. _LIBCPP_NO_CFI static __func* __as_base(void* p)
  1490. {
  1491. return reinterpret_cast<__func*>(p);
  1492. }
  1493. public:
  1494. _LIBCPP_INLINE_VISIBILITY
  1495. __value_func() _NOEXCEPT : __f_(0) {}
  1496. template <class _Fp, class _Alloc>
  1497. _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
  1498. : __f_(0)
  1499. {
  1500. typedef allocator_traits<_Alloc> __alloc_traits;
  1501. typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
  1502. typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
  1503. _FunAlloc;
  1504. if (__function::__not_null(__f))
  1505. {
  1506. _FunAlloc __af(__a);
  1507. if (sizeof(_Fun) <= sizeof(__buf_) &&
  1508. is_nothrow_copy_constructible<_Fp>::value &&
  1509. is_nothrow_copy_constructible<_FunAlloc>::value)
  1510. {
  1511. __f_ =
  1512. ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
  1513. }
  1514. else
  1515. {
  1516. typedef __allocator_destructor<_FunAlloc> _Dp;
  1517. unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
  1518. ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
  1519. __f_ = __hold.release();
  1520. }
  1521. }
  1522. }
  1523. template <class _Fp,
  1524. class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
  1525. _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
  1526. : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
  1527. _LIBCPP_INLINE_VISIBILITY
  1528. __value_func(const __value_func& __f)
  1529. {
  1530. if (__f.__f_ == 0)
  1531. __f_ = 0;
  1532. else if ((void*)__f.__f_ == &__f.__buf_)
  1533. {
  1534. __f_ = __as_base(&__buf_);
  1535. __f.__f_->__clone(__f_);
  1536. }
  1537. else
  1538. __f_ = __f.__f_->__clone();
  1539. }
  1540. _LIBCPP_INLINE_VISIBILITY
  1541. __value_func(__value_func&& __f) _NOEXCEPT
  1542. {
  1543. if (__f.__f_ == 0)
  1544. __f_ = 0;
  1545. else if ((void*)__f.__f_ == &__f.__buf_)
  1546. {
  1547. __f_ = __as_base(&__buf_);
  1548. __f.__f_->__clone(__f_);
  1549. }
  1550. else
  1551. {
  1552. __f_ = __f.__f_;
  1553. __f.__f_ = 0;
  1554. }
  1555. }
  1556. _LIBCPP_INLINE_VISIBILITY
  1557. ~__value_func()
  1558. {
  1559. if ((void*)__f_ == &__buf_)
  1560. __f_->destroy();
  1561. else if (__f_)
  1562. __f_->destroy_deallocate();
  1563. }
  1564. _LIBCPP_INLINE_VISIBILITY
  1565. __value_func& operator=(__value_func&& __f)
  1566. {
  1567. *this = nullptr;
  1568. if (__f.__f_ == 0)
  1569. __f_ = 0;
  1570. else if ((void*)__f.__f_ == &__f.__buf_)
  1571. {
  1572. __f_ = __as_base(&__buf_);
  1573. __f.__f_->__clone(__f_);
  1574. }
  1575. else
  1576. {
  1577. __f_ = __f.__f_;
  1578. __f.__f_ = 0;
  1579. }
  1580. return *this;
  1581. }
  1582. _LIBCPP_INLINE_VISIBILITY
  1583. __value_func& operator=(nullptr_t)
  1584. {
  1585. __func* __f = __f_;
  1586. __f_ = 0;
  1587. if ((void*)__f == &__buf_)
  1588. __f->destroy();
  1589. else if (__f)
  1590. __f->destroy_deallocate();
  1591. return *this;
  1592. }
  1593. _LIBCPP_INLINE_VISIBILITY
  1594. _Rp operator()(_ArgTypes&&... __args) const
  1595. {
  1596. if (__f_ == 0)
  1597. __throw_bad_function_call();
  1598. return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
  1599. }
  1600. _LIBCPP_INLINE_VISIBILITY
  1601. void swap(__value_func& __f) _NOEXCEPT
  1602. {
  1603. if (&__f == this)
  1604. return;
  1605. if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
  1606. {
  1607. typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
  1608. __func* __t = __as_base(&__tempbuf);
  1609. __f_->__clone(__t);
  1610. __f_->destroy();
  1611. __f_ = 0;
  1612. __f.__f_->__clone(__as_base(&__buf_));
  1613. __f.__f_->destroy();
  1614. __f.__f_ = 0;
  1615. __f_ = __as_base(&__buf_);
  1616. __t->__clone(__as_base(&__f.__buf_));
  1617. __t->destroy();
  1618. __f.__f_ = __as_base(&__f.__buf_);
  1619. }
  1620. else if ((void*)__f_ == &__buf_)
  1621. {
  1622. __f_->__clone(__as_base(&__f.__buf_));
  1623. __f_->destroy();
  1624. __f_ = __f.__f_;
  1625. __f.__f_ = __as_base(&__f.__buf_);
  1626. }
  1627. else if ((void*)__f.__f_ == &__f.__buf_)
  1628. {
  1629. __f.__f_->__clone(__as_base(&__buf_));
  1630. __f.__f_->destroy();
  1631. __f.__f_ = __f_;
  1632. __f_ = __as_base(&__buf_);
  1633. }
  1634. else
  1635. _VSTD::swap(__f_, __f.__f_);
  1636. }
  1637. _LIBCPP_INLINE_VISIBILITY
  1638. _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != 0; }
  1639. #ifndef _LIBCPP_NO_RTTI
  1640. _LIBCPP_INLINE_VISIBILITY
  1641. const std::type_info& target_type() const _NOEXCEPT
  1642. {
  1643. if (__f_ == 0)
  1644. return typeid(void);
  1645. return __f_->target_type();
  1646. }
  1647. template <typename _Tp>
  1648. _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
  1649. {
  1650. if (__f_ == 0)
  1651. return 0;
  1652. return (const _Tp*)__f_->target(typeid(_Tp));
  1653. }
  1654. #endif // _LIBCPP_NO_RTTI
  1655. };
  1656. // Storage for a functor object, to be used with __policy to manage copy and
  1657. // destruction.
  1658. union __policy_storage
  1659. {
  1660. mutable char __small[sizeof(void*) * 2];
  1661. void* __large;
  1662. };
  1663. // True if _Fun can safely be held in __policy_storage.__small.
  1664. template <typename _Fun>
  1665. struct __use_small_storage
  1666. : public _VSTD::integral_constant<
  1667. bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
  1668. _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
  1669. _VSTD::is_trivially_copy_constructible<_Fun>::value &&
  1670. _VSTD::is_trivially_destructible<_Fun>::value> {};
  1671. // Policy contains information about how to copy, destroy, and move the
  1672. // underlying functor. You can think of it as a vtable of sorts.
  1673. struct __policy
  1674. {
  1675. // Used to copy or destroy __large values. null for trivial objects.
  1676. void* (*const __clone)(const void*);
  1677. void (*const __destroy)(void*);
  1678. // True if this is the null policy (no value).
  1679. const bool __is_null;
  1680. // The target type. May be null if RTTI is disabled.
  1681. const std::type_info* const __type_info;
  1682. // Returns a pointer to a static policy object suitable for the functor
  1683. // type.
  1684. template <typename _Fun>
  1685. _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
  1686. {
  1687. return __choose_policy<_Fun>(__use_small_storage<_Fun>());
  1688. }
  1689. _LIBCPP_INLINE_VISIBILITY
  1690. static const __policy* __create_empty()
  1691. {
  1692. static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
  1693. true,
  1694. #ifndef _LIBCPP_NO_RTTI
  1695. &typeid(void)
  1696. #else
  1697. nullptr
  1698. #endif
  1699. };
  1700. return &__policy_;
  1701. }
  1702. private:
  1703. template <typename _Fun> static void* __large_clone(const void* __s)
  1704. {
  1705. const _Fun* __f = static_cast<const _Fun*>(__s);
  1706. return __f->__clone();
  1707. }
  1708. template <typename _Fun>
  1709. static void __large_destroy(void* __s) {
  1710. _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
  1711. }
  1712. template <typename _Fun>
  1713. _LIBCPP_INLINE_VISIBILITY static const __policy*
  1714. __choose_policy(/* is_small = */ false_type) {
  1715. static const _LIBCPP_CONSTEXPR __policy __policy_ = {
  1716. &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
  1717. #ifndef _LIBCPP_NO_RTTI
  1718. &typeid(typename _Fun::_Target)
  1719. #else
  1720. nullptr
  1721. #endif
  1722. };
  1723. return &__policy_;
  1724. }
  1725. template <typename _Fun>
  1726. _LIBCPP_INLINE_VISIBILITY static const __policy*
  1727. __choose_policy(/* is_small = */ true_type)
  1728. {
  1729. static const _LIBCPP_CONSTEXPR __policy __policy_ = {
  1730. nullptr, nullptr, false,
  1731. #ifndef _LIBCPP_NO_RTTI
  1732. &typeid(typename _Fun::_Target)
  1733. #else
  1734. nullptr
  1735. #endif
  1736. };
  1737. return &__policy_;
  1738. }
  1739. };
  1740. // Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
  1741. // faster for types that can be passed in registers.
  1742. template <typename _Tp>
  1743. using __fast_forward =
  1744. typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
  1745. // __policy_invoker calls an instance of __alloc_func held in __policy_storage.
  1746. template <class _Fp> struct __policy_invoker;
  1747. template <class _Rp, class... _ArgTypes>
  1748. struct __policy_invoker<_Rp(_ArgTypes...)>
  1749. {
  1750. typedef _Rp (*__Call)(const __policy_storage*,
  1751. __fast_forward<_ArgTypes>...);
  1752. __Call __call_;
  1753. // Creates an invoker that throws bad_function_call.
  1754. _LIBCPP_INLINE_VISIBILITY
  1755. __policy_invoker() : __call_(&__call_empty) {}
  1756. // Creates an invoker that calls the given instance of __func.
  1757. template <typename _Fun>
  1758. _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
  1759. {
  1760. return __policy_invoker(&__call_impl<_Fun>);
  1761. }
  1762. private:
  1763. _LIBCPP_INLINE_VISIBILITY
  1764. explicit __policy_invoker(__Call __c) : __call_(__c) {}
  1765. static _Rp __call_empty(const __policy_storage*,
  1766. __fast_forward<_ArgTypes>...)
  1767. {
  1768. __throw_bad_function_call();
  1769. }
  1770. template <typename _Fun>
  1771. static _Rp __call_impl(const __policy_storage* __buf,
  1772. __fast_forward<_ArgTypes>... __args)
  1773. {
  1774. _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
  1775. ? &__buf->__small
  1776. : __buf->__large);
  1777. return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
  1778. }
  1779. };
  1780. // __policy_func uses a __policy and __policy_invoker to create a type-erased,
  1781. // copyable functor.
  1782. template <class _Fp> class __policy_func;
  1783. template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
  1784. {
  1785. // Inline storage for small objects.
  1786. __policy_storage __buf_;
  1787. // Calls the value stored in __buf_. This could technically be part of
  1788. // policy, but storing it here eliminates a level of indirection inside
  1789. // operator().
  1790. typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
  1791. __invoker __invoker_;
  1792. // The policy that describes how to move / copy / destroy __buf_. Never
  1793. // null, even if the function is empty.
  1794. const __policy* __policy_;
  1795. public:
  1796. _LIBCPP_INLINE_VISIBILITY
  1797. __policy_func() : __policy_(__policy::__create_empty()) {}
  1798. template <class _Fp, class _Alloc>
  1799. _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
  1800. : __policy_(__policy::__create_empty())
  1801. {
  1802. typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
  1803. typedef allocator_traits<_Alloc> __alloc_traits;
  1804. typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
  1805. _FunAlloc;
  1806. if (__function::__not_null(__f))
  1807. {
  1808. __invoker_ = __invoker::template __create<_Fun>();
  1809. __policy_ = __policy::__create<_Fun>();
  1810. _FunAlloc __af(__a);
  1811. if (__use_small_storage<_Fun>())
  1812. {
  1813. ::new ((void*)&__buf_.__small)
  1814. _Fun(_VSTD::move(__f), _Alloc(__af));
  1815. }
  1816. else
  1817. {
  1818. typedef __allocator_destructor<_FunAlloc> _Dp;
  1819. unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
  1820. ::new ((void*)__hold.get())
  1821. _Fun(_VSTD::move(__f), _Alloc(__af));
  1822. __buf_.__large = __hold.release();
  1823. }
  1824. }
  1825. }
  1826. template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
  1827. _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
  1828. : __policy_(__policy::__create_empty()) {
  1829. typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
  1830. if (__function::__not_null(__f)) {
  1831. __invoker_ = __invoker::template __create<_Fun>();
  1832. __policy_ = __policy::__create<_Fun>();
  1833. if (__use_small_storage<_Fun>()) {
  1834. ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
  1835. } else {
  1836. __builtin_new_allocator::__holder_t __hold =
  1837. __builtin_new_allocator::__allocate_type<_Fun>(1);
  1838. __buf_.__large = ::new (__hold.get()) _Fun(_VSTD::move(__f));
  1839. (void)__hold.release();
  1840. }
  1841. }
  1842. }
  1843. _LIBCPP_INLINE_VISIBILITY
  1844. __policy_func(const __policy_func& __f)
  1845. : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
  1846. __policy_(__f.__policy_)
  1847. {
  1848. if (__policy_->__clone)
  1849. __buf_.__large = __policy_->__clone(__f.__buf_.__large);
  1850. }
  1851. _LIBCPP_INLINE_VISIBILITY
  1852. __policy_func(__policy_func&& __f)
  1853. : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
  1854. __policy_(__f.__policy_)
  1855. {
  1856. if (__policy_->__destroy)
  1857. {
  1858. __f.__policy_ = __policy::__create_empty();
  1859. __f.__invoker_ = __invoker();
  1860. }
  1861. }
  1862. _LIBCPP_INLINE_VISIBILITY
  1863. ~__policy_func()
  1864. {
  1865. if (__policy_->__destroy)
  1866. __policy_->__destroy(__buf_.__large);
  1867. }
  1868. _LIBCPP_INLINE_VISIBILITY
  1869. __policy_func& operator=(__policy_func&& __f)
  1870. {
  1871. *this = nullptr;
  1872. __buf_ = __f.__buf_;
  1873. __invoker_ = __f.__invoker_;
  1874. __policy_ = __f.__policy_;
  1875. __f.__policy_ = __policy::__create_empty();
  1876. __f.__invoker_ = __invoker();
  1877. return *this;
  1878. }
  1879. _LIBCPP_INLINE_VISIBILITY
  1880. __policy_func& operator=(nullptr_t)
  1881. {
  1882. const __policy* __p = __policy_;
  1883. __policy_ = __policy::__create_empty();
  1884. __invoker_ = __invoker();
  1885. if (__p->__destroy)
  1886. __p->__destroy(__buf_.__large);
  1887. return *this;
  1888. }
  1889. _LIBCPP_INLINE_VISIBILITY
  1890. _Rp operator()(_ArgTypes&&... __args) const
  1891. {
  1892. return __invoker_.__call_(_VSTD::addressof(__buf_),
  1893. _VSTD::forward<_ArgTypes>(__args)...);
  1894. }
  1895. _LIBCPP_INLINE_VISIBILITY
  1896. void swap(__policy_func& __f)
  1897. {
  1898. _VSTD::swap(__invoker_, __f.__invoker_);
  1899. _VSTD::swap(__policy_, __f.__policy_);
  1900. _VSTD::swap(__buf_, __f.__buf_);
  1901. }
  1902. _LIBCPP_INLINE_VISIBILITY
  1903. explicit operator bool() const _NOEXCEPT
  1904. {
  1905. return !__policy_->__is_null;
  1906. }
  1907. #ifndef _LIBCPP_NO_RTTI
  1908. _LIBCPP_INLINE_VISIBILITY
  1909. const std::type_info& target_type() const _NOEXCEPT
  1910. {
  1911. return *__policy_->__type_info;
  1912. }
  1913. template <typename _Tp>
  1914. _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
  1915. {
  1916. if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
  1917. return nullptr;
  1918. if (__policy_->__clone) // Out of line storage.
  1919. return reinterpret_cast<const _Tp*>(__buf_.__large);
  1920. else
  1921. return reinterpret_cast<const _Tp*>(&__buf_.__small);
  1922. }
  1923. #endif // _LIBCPP_NO_RTTI
  1924. };
  1925. } // __function
  1926. template<class _Rp, class ..._ArgTypes>
  1927. class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
  1928. : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
  1929. public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
  1930. {
  1931. #ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
  1932. typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
  1933. #else
  1934. typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
  1935. #endif
  1936. __func __f_;
  1937. template <class _Fp, bool = _And<
  1938. _IsNotSame<__uncvref_t<_Fp>, function>,
  1939. __invokable<_Fp&, _ArgTypes...>
  1940. >::value>
  1941. struct __callable;
  1942. template <class _Fp>
  1943. struct __callable<_Fp, true>
  1944. {
  1945. static const bool value = is_same<void, _Rp>::value ||
  1946. is_convertible<typename __invoke_of<_Fp&, _ArgTypes...>::type,
  1947. _Rp>::value;
  1948. };
  1949. template <class _Fp>
  1950. struct __callable<_Fp, false>
  1951. {
  1952. static const bool value = false;
  1953. };
  1954. template <class _Fp>
  1955. using _EnableIfCallable = typename enable_if<__callable<_Fp>::value>::type;
  1956. public:
  1957. typedef _Rp result_type;
  1958. // construct/copy/destroy:
  1959. _LIBCPP_INLINE_VISIBILITY
  1960. function() _NOEXCEPT { }
  1961. _LIBCPP_INLINE_VISIBILITY
  1962. function(nullptr_t) _NOEXCEPT {}
  1963. function(const function&);
  1964. function(function&&) _NOEXCEPT;
  1965. template<class _Fp, class = _EnableIfCallable<_Fp>>
  1966. function(_Fp);
  1967. #if _LIBCPP_STD_VER <= 14
  1968. template<class _Alloc>
  1969. _LIBCPP_INLINE_VISIBILITY
  1970. function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
  1971. template<class _Alloc>
  1972. _LIBCPP_INLINE_VISIBILITY
  1973. function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
  1974. template<class _Alloc>
  1975. function(allocator_arg_t, const _Alloc&, const function&);
  1976. template<class _Alloc>
  1977. function(allocator_arg_t, const _Alloc&, function&&);
  1978. template<class _Fp, class _Alloc, class = _EnableIfCallable<_Fp>>
  1979. function(allocator_arg_t, const _Alloc& __a, _Fp __f);
  1980. #endif
  1981. function& operator=(const function&);
  1982. function& operator=(function&&) _NOEXCEPT;
  1983. function& operator=(nullptr_t) _NOEXCEPT;
  1984. template<class _Fp, class = _EnableIfCallable<_Fp>>
  1985. function& operator=(_Fp&&);
  1986. ~function();
  1987. // function modifiers:
  1988. void swap(function&) _NOEXCEPT;
  1989. #if _LIBCPP_STD_VER <= 14
  1990. template<class _Fp, class _Alloc>
  1991. _LIBCPP_INLINE_VISIBILITY
  1992. void assign(_Fp&& __f, const _Alloc& __a)
  1993. {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
  1994. #endif
  1995. // function capacity:
  1996. _LIBCPP_INLINE_VISIBILITY
  1997. _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
  1998. return static_cast<bool>(__f_);
  1999. }
  2000. // deleted overloads close possible hole in the type system
  2001. template<class _R2, class... _ArgTypes2>
  2002. bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
  2003. template<class _R2, class... _ArgTypes2>
  2004. bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
  2005. public:
  2006. // function invocation:
  2007. _Rp operator()(_ArgTypes...) const;
  2008. #ifndef _LIBCPP_NO_RTTI
  2009. // function target access:
  2010. const std::type_info& target_type() const _NOEXCEPT;
  2011. template <typename _Tp> _Tp* target() _NOEXCEPT;
  2012. template <typename _Tp> const _Tp* target() const _NOEXCEPT;
  2013. #endif // _LIBCPP_NO_RTTI
  2014. };
  2015. #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
  2016. template<class _Rp, class ..._Ap>
  2017. function(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
  2018. template<class _Fp>
  2019. struct __strip_signature;
  2020. template<class _Rp, class _Gp, class ..._Ap>
  2021. struct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
  2022. template<class _Rp, class _Gp, class ..._Ap>
  2023. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
  2024. template<class _Rp, class _Gp, class ..._Ap>
  2025. struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
  2026. template<class _Rp, class _Gp, class ..._Ap>
  2027. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
  2028. template<class _Rp, class _Gp, class ..._Ap>
  2029. struct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
  2030. template<class _Rp, class _Gp, class ..._Ap>
  2031. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
  2032. template<class _Rp, class _Gp, class ..._Ap>
  2033. struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
  2034. template<class _Rp, class _Gp, class ..._Ap>
  2035. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
  2036. template<class _Rp, class _Gp, class ..._Ap>
  2037. struct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
  2038. template<class _Rp, class _Gp, class ..._Ap>
  2039. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
  2040. template<class _Rp, class _Gp, class ..._Ap>
  2041. struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
  2042. template<class _Rp, class _Gp, class ..._Ap>
  2043. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
  2044. template<class _Rp, class _Gp, class ..._Ap>
  2045. struct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
  2046. template<class _Rp, class _Gp, class ..._Ap>
  2047. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
  2048. template<class _Rp, class _Gp, class ..._Ap>
  2049. struct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
  2050. template<class _Rp, class _Gp, class ..._Ap>
  2051. struct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
  2052. template<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
  2053. function(_Fp) -> function<_Stripped>;
  2054. #endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
  2055. template<class _Rp, class ..._ArgTypes>
  2056. function<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
  2057. #if _LIBCPP_STD_VER <= 14
  2058. template<class _Rp, class ..._ArgTypes>
  2059. template <class _Alloc>
  2060. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
  2061. const function& __f) : __f_(__f.__f_) {}
  2062. #endif
  2063. template <class _Rp, class... _ArgTypes>
  2064. function<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
  2065. : __f_(_VSTD::move(__f.__f_)) {}
  2066. #if _LIBCPP_STD_VER <= 14
  2067. template<class _Rp, class ..._ArgTypes>
  2068. template <class _Alloc>
  2069. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
  2070. function&& __f)
  2071. : __f_(_VSTD::move(__f.__f_)) {}
  2072. #endif
  2073. template <class _Rp, class... _ArgTypes>
  2074. template <class _Fp, class>
  2075. function<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
  2076. #if _LIBCPP_STD_VER <= 14
  2077. template <class _Rp, class... _ArgTypes>
  2078. template <class _Fp, class _Alloc, class>
  2079. function<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
  2080. _Fp __f)
  2081. : __f_(_VSTD::move(__f), __a) {}
  2082. #endif
  2083. template<class _Rp, class ..._ArgTypes>
  2084. function<_Rp(_ArgTypes...)>&
  2085. function<_Rp(_ArgTypes...)>::operator=(const function& __f)
  2086. {
  2087. function(__f).swap(*this);
  2088. return *this;
  2089. }
  2090. template<class _Rp, class ..._ArgTypes>
  2091. function<_Rp(_ArgTypes...)>&
  2092. function<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
  2093. {
  2094. __f_ = std::move(__f.__f_);
  2095. return *this;
  2096. }
  2097. template<class _Rp, class ..._ArgTypes>
  2098. function<_Rp(_ArgTypes...)>&
  2099. function<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
  2100. {
  2101. __f_ = nullptr;
  2102. return *this;
  2103. }
  2104. template<class _Rp, class ..._ArgTypes>
  2105. template <class _Fp, class>
  2106. function<_Rp(_ArgTypes...)>&
  2107. function<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
  2108. {
  2109. function(_VSTD::forward<_Fp>(__f)).swap(*this);
  2110. return *this;
  2111. }
  2112. template<class _Rp, class ..._ArgTypes>
  2113. function<_Rp(_ArgTypes...)>::~function() {}
  2114. template<class _Rp, class ..._ArgTypes>
  2115. void
  2116. function<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
  2117. {
  2118. __f_.swap(__f.__f_);
  2119. }
  2120. template<class _Rp, class ..._ArgTypes>
  2121. _Rp
  2122. function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
  2123. {
  2124. return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  2125. }
  2126. #ifndef _LIBCPP_NO_RTTI
  2127. template<class _Rp, class ..._ArgTypes>
  2128. const std::type_info&
  2129. function<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
  2130. {
  2131. return __f_.target_type();
  2132. }
  2133. template<class _Rp, class ..._ArgTypes>
  2134. template <typename _Tp>
  2135. _Tp*
  2136. function<_Rp(_ArgTypes...)>::target() _NOEXCEPT
  2137. {
  2138. return (_Tp*)(__f_.template target<_Tp>());
  2139. }
  2140. template<class _Rp, class ..._ArgTypes>
  2141. template <typename _Tp>
  2142. const _Tp*
  2143. function<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
  2144. {
  2145. return __f_.template target<_Tp>();
  2146. }
  2147. #endif // _LIBCPP_NO_RTTI
  2148. template <class _Rp, class... _ArgTypes>
  2149. inline _LIBCPP_INLINE_VISIBILITY
  2150. bool
  2151. operator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
  2152. template <class _Rp, class... _ArgTypes>
  2153. inline _LIBCPP_INLINE_VISIBILITY
  2154. bool
  2155. operator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
  2156. template <class _Rp, class... _ArgTypes>
  2157. inline _LIBCPP_INLINE_VISIBILITY
  2158. bool
  2159. operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
  2160. template <class _Rp, class... _ArgTypes>
  2161. inline _LIBCPP_INLINE_VISIBILITY
  2162. bool
  2163. operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
  2164. template <class _Rp, class... _ArgTypes>
  2165. inline _LIBCPP_INLINE_VISIBILITY
  2166. void
  2167. swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
  2168. {return __x.swap(__y);}
  2169. #else // _LIBCPP_CXX03_LANG
  2170. #include <__functional_03>
  2171. #endif
  2172. ////////////////////////////////////////////////////////////////////////////////
  2173. // BIND
  2174. //==============================================================================
  2175. template<class _Tp> struct __is_bind_expression : public false_type {};
  2176. template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
  2177. : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
  2178. #if _LIBCPP_STD_VER > 14
  2179. template <class _Tp>
  2180. _LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
  2181. #endif
  2182. template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
  2183. template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
  2184. : public __is_placeholder<typename remove_cv<_Tp>::type> {};
  2185. #if _LIBCPP_STD_VER > 14
  2186. template <class _Tp>
  2187. _LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
  2188. #endif
  2189. namespace placeholders
  2190. {
  2191. template <int _Np> struct __ph {};
  2192. #if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
  2193. _LIBCPP_FUNC_VIS extern const __ph<1> _1;
  2194. _LIBCPP_FUNC_VIS extern const __ph<2> _2;
  2195. _LIBCPP_FUNC_VIS extern const __ph<3> _3;
  2196. _LIBCPP_FUNC_VIS extern const __ph<4> _4;
  2197. _LIBCPP_FUNC_VIS extern const __ph<5> _5;
  2198. _LIBCPP_FUNC_VIS extern const __ph<6> _6;
  2199. _LIBCPP_FUNC_VIS extern const __ph<7> _7;
  2200. _LIBCPP_FUNC_VIS extern const __ph<8> _8;
  2201. _LIBCPP_FUNC_VIS extern const __ph<9> _9;
  2202. _LIBCPP_FUNC_VIS extern const __ph<10> _10;
  2203. #else
  2204. /* _LIBCPP_INLINE_VAR */ constexpr __ph<1> _1{};
  2205. /* _LIBCPP_INLINE_VAR */ constexpr __ph<2> _2{};
  2206. /* _LIBCPP_INLINE_VAR */ constexpr __ph<3> _3{};
  2207. /* _LIBCPP_INLINE_VAR */ constexpr __ph<4> _4{};
  2208. /* _LIBCPP_INLINE_VAR */ constexpr __ph<5> _5{};
  2209. /* _LIBCPP_INLINE_VAR */ constexpr __ph<6> _6{};
  2210. /* _LIBCPP_INLINE_VAR */ constexpr __ph<7> _7{};
  2211. /* _LIBCPP_INLINE_VAR */ constexpr __ph<8> _8{};
  2212. /* _LIBCPP_INLINE_VAR */ constexpr __ph<9> _9{};
  2213. /* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
  2214. #endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
  2215. } // placeholders
  2216. template<int _Np>
  2217. struct __is_placeholder<placeholders::__ph<_Np> >
  2218. : public integral_constant<int, _Np> {};
  2219. #ifndef _LIBCPP_CXX03_LANG
  2220. template <class _Tp, class _Uj>
  2221. inline _LIBCPP_INLINE_VISIBILITY
  2222. _Tp&
  2223. __mu(reference_wrapper<_Tp> __t, _Uj&)
  2224. {
  2225. return __t.get();
  2226. }
  2227. template <class _Ti, class ..._Uj, size_t ..._Indx>
  2228. inline _LIBCPP_INLINE_VISIBILITY
  2229. typename __invoke_of<_Ti&, _Uj...>::type
  2230. __mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
  2231. {
  2232. return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
  2233. }
  2234. template <class _Ti, class ..._Uj>
  2235. inline _LIBCPP_INLINE_VISIBILITY
  2236. typename _EnableIf
  2237. <
  2238. is_bind_expression<_Ti>::value,
  2239. __invoke_of<_Ti&, _Uj...>
  2240. >::type
  2241. __mu(_Ti& __ti, tuple<_Uj...>& __uj)
  2242. {
  2243. typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
  2244. return __mu_expand(__ti, __uj, __indices());
  2245. }
  2246. template <bool IsPh, class _Ti, class _Uj>
  2247. struct __mu_return2 {};
  2248. template <class _Ti, class _Uj>
  2249. struct __mu_return2<true, _Ti, _Uj>
  2250. {
  2251. typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
  2252. };
  2253. template <class _Ti, class _Uj>
  2254. inline _LIBCPP_INLINE_VISIBILITY
  2255. typename enable_if
  2256. <
  2257. 0 < is_placeholder<_Ti>::value,
  2258. typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
  2259. >::type
  2260. __mu(_Ti&, _Uj& __uj)
  2261. {
  2262. const size_t _Indx = is_placeholder<_Ti>::value - 1;
  2263. return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
  2264. }
  2265. template <class _Ti, class _Uj>
  2266. inline _LIBCPP_INLINE_VISIBILITY
  2267. typename enable_if
  2268. <
  2269. !is_bind_expression<_Ti>::value &&
  2270. is_placeholder<_Ti>::value == 0 &&
  2271. !__is_reference_wrapper<_Ti>::value,
  2272. _Ti&
  2273. >::type
  2274. __mu(_Ti& __ti, _Uj&)
  2275. {
  2276. return __ti;
  2277. }
  2278. template <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
  2279. class _TupleUj>
  2280. struct __mu_return_impl;
  2281. template <bool _Invokable, class _Ti, class ..._Uj>
  2282. struct __mu_return_invokable // false
  2283. {
  2284. typedef __nat type;
  2285. };
  2286. template <class _Ti, class ..._Uj>
  2287. struct __mu_return_invokable<true, _Ti, _Uj...>
  2288. {
  2289. typedef typename __invoke_of<_Ti&, _Uj...>::type type;
  2290. };
  2291. template <class _Ti, class ..._Uj>
  2292. struct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
  2293. : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
  2294. {
  2295. };
  2296. template <class _Ti, class _TupleUj>
  2297. struct __mu_return_impl<_Ti, false, false, true, _TupleUj>
  2298. {
  2299. typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
  2300. _TupleUj>::type&& type;
  2301. };
  2302. template <class _Ti, class _TupleUj>
  2303. struct __mu_return_impl<_Ti, true, false, false, _TupleUj>
  2304. {
  2305. typedef typename _Ti::type& type;
  2306. };
  2307. template <class _Ti, class _TupleUj>
  2308. struct __mu_return_impl<_Ti, false, false, false, _TupleUj>
  2309. {
  2310. typedef _Ti& type;
  2311. };
  2312. template <class _Ti, class _TupleUj>
  2313. struct __mu_return
  2314. : public __mu_return_impl<_Ti,
  2315. __is_reference_wrapper<_Ti>::value,
  2316. is_bind_expression<_Ti>::value,
  2317. 0 < is_placeholder<_Ti>::value &&
  2318. is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
  2319. _TupleUj>
  2320. {
  2321. };
  2322. template <class _Fp, class _BoundArgs, class _TupleUj>
  2323. struct __is_valid_bind_return
  2324. {
  2325. static const bool value = false;
  2326. };
  2327. template <class _Fp, class ..._BoundArgs, class _TupleUj>
  2328. struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
  2329. {
  2330. static const bool value = __invokable<_Fp,
  2331. typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
  2332. };
  2333. template <class _Fp, class ..._BoundArgs, class _TupleUj>
  2334. struct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
  2335. {
  2336. static const bool value = __invokable<_Fp,
  2337. typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
  2338. };
  2339. template <class _Fp, class _BoundArgs, class _TupleUj,
  2340. bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
  2341. struct __bind_return;
  2342. template <class _Fp, class ..._BoundArgs, class _TupleUj>
  2343. struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
  2344. {
  2345. typedef typename __invoke_of
  2346. <
  2347. _Fp&,
  2348. typename __mu_return
  2349. <
  2350. _BoundArgs,
  2351. _TupleUj
  2352. >::type...
  2353. >::type type;
  2354. };
  2355. template <class _Fp, class ..._BoundArgs, class _TupleUj>
  2356. struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
  2357. {
  2358. typedef typename __invoke_of
  2359. <
  2360. _Fp&,
  2361. typename __mu_return
  2362. <
  2363. const _BoundArgs,
  2364. _TupleUj
  2365. >::type...
  2366. >::type type;
  2367. };
  2368. template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
  2369. inline _LIBCPP_INLINE_VISIBILITY
  2370. typename __bind_return<_Fp, _BoundArgs, _Args>::type
  2371. __apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
  2372. _Args&& __args)
  2373. {
  2374. return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
  2375. }
  2376. template<class _Fp, class ..._BoundArgs>
  2377. class __bind
  2378. : public __weak_result_type<typename decay<_Fp>::type>
  2379. {
  2380. protected:
  2381. typedef typename decay<_Fp>::type _Fd;
  2382. typedef tuple<typename decay<_BoundArgs>::type...> _Td;
  2383. private:
  2384. _Fd __f_;
  2385. _Td __bound_args_;
  2386. typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
  2387. public:
  2388. template <class _Gp, class ..._BA,
  2389. class = typename enable_if
  2390. <
  2391. is_constructible<_Fd, _Gp>::value &&
  2392. !is_same<typename remove_reference<_Gp>::type,
  2393. __bind>::value
  2394. >::type>
  2395. _LIBCPP_INLINE_VISIBILITY
  2396. explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
  2397. : __f_(_VSTD::forward<_Gp>(__f)),
  2398. __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
  2399. template <class ..._Args>
  2400. _LIBCPP_INLINE_VISIBILITY
  2401. typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
  2402. operator()(_Args&& ...__args)
  2403. {
  2404. return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
  2405. tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
  2406. }
  2407. template <class ..._Args>
  2408. _LIBCPP_INLINE_VISIBILITY
  2409. typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
  2410. operator()(_Args&& ...__args) const
  2411. {
  2412. return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
  2413. tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
  2414. }
  2415. };
  2416. template<class _Fp, class ..._BoundArgs>
  2417. struct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
  2418. template<class _Rp, class _Fp, class ..._BoundArgs>
  2419. class __bind_r
  2420. : public __bind<_Fp, _BoundArgs...>
  2421. {
  2422. typedef __bind<_Fp, _BoundArgs...> base;
  2423. typedef typename base::_Fd _Fd;
  2424. typedef typename base::_Td _Td;
  2425. public:
  2426. typedef _Rp result_type;
  2427. template <class _Gp, class ..._BA,
  2428. class = typename enable_if
  2429. <
  2430. is_constructible<_Fd, _Gp>::value &&
  2431. !is_same<typename remove_reference<_Gp>::type,
  2432. __bind_r>::value
  2433. >::type>
  2434. _LIBCPP_INLINE_VISIBILITY
  2435. explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
  2436. : base(_VSTD::forward<_Gp>(__f),
  2437. _VSTD::forward<_BA>(__bound_args)...) {}
  2438. template <class ..._Args>
  2439. _LIBCPP_INLINE_VISIBILITY
  2440. typename enable_if
  2441. <
  2442. is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
  2443. result_type>::value || is_void<_Rp>::value,
  2444. result_type
  2445. >::type
  2446. operator()(_Args&& ...__args)
  2447. {
  2448. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  2449. return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
  2450. }
  2451. template <class ..._Args>
  2452. _LIBCPP_INLINE_VISIBILITY
  2453. typename enable_if
  2454. <
  2455. is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
  2456. result_type>::value || is_void<_Rp>::value,
  2457. result_type
  2458. >::type
  2459. operator()(_Args&& ...__args) const
  2460. {
  2461. typedef __invoke_void_return_wrapper<_Rp> _Invoker;
  2462. return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
  2463. }
  2464. };
  2465. template<class _Rp, class _Fp, class ..._BoundArgs>
  2466. struct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
  2467. template<class _Fp, class ..._BoundArgs>
  2468. inline _LIBCPP_INLINE_VISIBILITY
  2469. __bind<_Fp, _BoundArgs...>
  2470. bind(_Fp&& __f, _BoundArgs&&... __bound_args)
  2471. {
  2472. typedef __bind<_Fp, _BoundArgs...> type;
  2473. return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
  2474. }
  2475. template<class _Rp, class _Fp, class ..._BoundArgs>
  2476. inline _LIBCPP_INLINE_VISIBILITY
  2477. __bind_r<_Rp, _Fp, _BoundArgs...>
  2478. bind(_Fp&& __f, _BoundArgs&&... __bound_args)
  2479. {
  2480. typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
  2481. return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
  2482. }
  2483. #endif // _LIBCPP_CXX03_LANG
  2484. #if _LIBCPP_STD_VER > 14
  2485. template <class _Fn, class ..._Args>
  2486. invoke_result_t<_Fn, _Args...>
  2487. invoke(_Fn&& __f, _Args&&... __args)
  2488. noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
  2489. {
  2490. return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
  2491. }
  2492. template <class _DecayFunc>
  2493. class _LIBCPP_TEMPLATE_VIS __not_fn_imp {
  2494. _DecayFunc __fd;
  2495. public:
  2496. __not_fn_imp() = delete;
  2497. template <class ..._Args>
  2498. _LIBCPP_INLINE_VISIBILITY
  2499. auto operator()(_Args&& ...__args) &
  2500. noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
  2501. -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
  2502. { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
  2503. template <class ..._Args>
  2504. _LIBCPP_INLINE_VISIBILITY
  2505. auto operator()(_Args&& ...__args) &&
  2506. noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
  2507. -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
  2508. { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
  2509. template <class ..._Args>
  2510. _LIBCPP_INLINE_VISIBILITY
  2511. auto operator()(_Args&& ...__args) const&
  2512. noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
  2513. -> decltype( !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
  2514. { return !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
  2515. template <class ..._Args>
  2516. _LIBCPP_INLINE_VISIBILITY
  2517. auto operator()(_Args&& ...__args) const&&
  2518. noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
  2519. -> decltype( !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
  2520. { return !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
  2521. private:
  2522. template <class _RawFunc,
  2523. class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
  2524. _LIBCPP_INLINE_VISIBILITY
  2525. explicit __not_fn_imp(_RawFunc&& __rf)
  2526. : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
  2527. template <class _RawFunc>
  2528. friend inline _LIBCPP_INLINE_VISIBILITY
  2529. __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
  2530. };
  2531. template <class _RawFunc>
  2532. inline _LIBCPP_INLINE_VISIBILITY
  2533. __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
  2534. return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
  2535. }
  2536. #endif
  2537. // struct hash<T*> in <memory>
  2538. template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
  2539. pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
  2540. __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  2541. _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
  2542. forward_iterator_tag, forward_iterator_tag)
  2543. {
  2544. if (__first2 == __last2)
  2545. return make_pair(__first1, __first1); // Everything matches an empty sequence
  2546. while (true)
  2547. {
  2548. // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
  2549. while (true)
  2550. {
  2551. if (__first1 == __last1) // return __last1 if no element matches *__first2
  2552. return make_pair(__last1, __last1);
  2553. if (__pred(*__first1, *__first2))
  2554. break;
  2555. ++__first1;
  2556. }
  2557. // *__first1 matches *__first2, now match elements after here
  2558. _ForwardIterator1 __m1 = __first1;
  2559. _ForwardIterator2 __m2 = __first2;
  2560. while (true)
  2561. {
  2562. if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
  2563. return make_pair(__first1, __m1);
  2564. if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
  2565. return make_pair(__last1, __last1);
  2566. if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
  2567. {
  2568. ++__first1;
  2569. break;
  2570. } // else there is a match, check next elements
  2571. }
  2572. }
  2573. }
  2574. template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
  2575. _LIBCPP_CONSTEXPR_AFTER_CXX11
  2576. pair<_RandomAccessIterator1, _RandomAccessIterator1>
  2577. __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
  2578. _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
  2579. random_access_iterator_tag, random_access_iterator_tag)
  2580. {
  2581. typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
  2582. typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
  2583. // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
  2584. const _D2 __len2 = __last2 - __first2;
  2585. if (__len2 == 0)
  2586. return make_pair(__first1, __first1);
  2587. const _D1 __len1 = __last1 - __first1;
  2588. if (__len1 < __len2)
  2589. return make_pair(__last1, __last1);
  2590. const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
  2591. while (true)
  2592. {
  2593. while (true)
  2594. {
  2595. if (__first1 == __s)
  2596. return make_pair(__last1, __last1);
  2597. if (__pred(*__first1, *__first2))
  2598. break;
  2599. ++__first1;
  2600. }
  2601. _RandomAccessIterator1 __m1 = __first1;
  2602. _RandomAccessIterator2 __m2 = __first2;
  2603. while (true)
  2604. {
  2605. if (++__m2 == __last2)
  2606. return make_pair(__first1, __first1 + __len2);
  2607. ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
  2608. if (!__pred(*__m1, *__m2))
  2609. {
  2610. ++__first1;
  2611. break;
  2612. }
  2613. }
  2614. }
  2615. }
  2616. #if _LIBCPP_STD_VER > 14
  2617. // default searcher
  2618. template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
  2619. class _LIBCPP_TYPE_VIS default_searcher {
  2620. public:
  2621. _LIBCPP_INLINE_VISIBILITY
  2622. default_searcher(_ForwardIterator __f, _ForwardIterator __l,
  2623. _BinaryPredicate __p = _BinaryPredicate())
  2624. : __first_(__f), __last_(__l), __pred_(__p) {}
  2625. template <typename _ForwardIterator2>
  2626. _LIBCPP_INLINE_VISIBILITY
  2627. pair<_ForwardIterator2, _ForwardIterator2>
  2628. operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
  2629. {
  2630. return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
  2631. typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
  2632. typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
  2633. }
  2634. private:
  2635. _ForwardIterator __first_;
  2636. _ForwardIterator __last_;
  2637. _BinaryPredicate __pred_;
  2638. };
  2639. #endif // _LIBCPP_STD_VER > 14
  2640. #if _LIBCPP_STD_VER > 17
  2641. template <class _Tp>
  2642. using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
  2643. template <class _Tp>
  2644. using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
  2645. #endif // > C++17
  2646. template <class _Container, class _Predicate>
  2647. inline void __libcpp_erase_if_container( _Container& __c, _Predicate __pred)
  2648. {
  2649. for (typename _Container::iterator __iter = __c.begin(), __last = __c.end(); __iter != __last;)
  2650. {
  2651. if (__pred(*__iter))
  2652. __iter = __c.erase(__iter);
  2653. else
  2654. ++__iter;
  2655. }
  2656. }
  2657. _LIBCPP_END_NAMESPACE_STD
  2658. #endif // _LIBCPP_FUNCTIONAL