stl_function.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. // Functor implementations -*- C++ -*-
  2. // Copyright (C) 2001-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996-1998
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_function.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{functional}
  48. */
  49. #ifndef _STL_FUNCTION_H
  50. #define _STL_FUNCTION_H 1
  51. #if __cplusplus > 201103L
  52. #include <bits/move.h>
  53. #endif
  54. namespace std _GLIBCXX_VISIBILITY(default)
  55. {
  56. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  57. // 20.3.1 base classes
  58. /** @defgroup functors Function Objects
  59. * @ingroup utilities
  60. *
  61. * Function objects, or @e functors, are objects with an @c operator()
  62. * defined and accessible. They can be passed as arguments to algorithm
  63. * templates and used in place of a function pointer. Not only is the
  64. * resulting expressiveness of the library increased, but the generated
  65. * code can be more efficient than what you might write by hand. When we
  66. * refer to @a functors, then, generally we include function pointers in
  67. * the description as well.
  68. *
  69. * Often, functors are only created as temporaries passed to algorithm
  70. * calls, rather than being created as named variables.
  71. *
  72. * Two examples taken from the standard itself follow. To perform a
  73. * by-element addition of two vectors @c a and @c b containing @c double,
  74. * and put the result in @c a, use
  75. * \code
  76. * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
  77. * \endcode
  78. * To negate every element in @c a, use
  79. * \code
  80. * transform(a.begin(), a.end(), a.begin(), negate<double>());
  81. * \endcode
  82. * The addition and negation functions will be inlined directly.
  83. *
  84. * The standard functors are derived from structs named @c unary_function
  85. * and @c binary_function. These two classes contain nothing but typedefs,
  86. * to aid in generic (template) programming. If you write your own
  87. * functors, you might consider doing the same.
  88. *
  89. * @{
  90. */
  91. /**
  92. * This is one of the @link functors functor base classes@endlink.
  93. */
  94. template<typename _Arg, typename _Result>
  95. struct unary_function
  96. {
  97. /// @c argument_type is the type of the argument
  98. typedef _Arg argument_type;
  99. /// @c result_type is the return type
  100. typedef _Result result_type;
  101. };
  102. /**
  103. * This is one of the @link functors functor base classes@endlink.
  104. */
  105. template<typename _Arg1, typename _Arg2, typename _Result>
  106. struct binary_function
  107. {
  108. /// @c first_argument_type is the type of the first argument
  109. typedef _Arg1 first_argument_type;
  110. /// @c second_argument_type is the type of the second argument
  111. typedef _Arg2 second_argument_type;
  112. /// @c result_type is the return type
  113. typedef _Result result_type;
  114. };
  115. /** @} */
  116. // 20.3.2 arithmetic
  117. /** @defgroup arithmetic_functors Arithmetic Classes
  118. * @ingroup functors
  119. *
  120. * Because basic math often needs to be done during an algorithm,
  121. * the library provides functors for those operations. See the
  122. * documentation for @link functors the base classes@endlink
  123. * for examples of their use.
  124. *
  125. * @{
  126. */
  127. #if __cplusplus > 201103L
  128. struct __is_transparent; // undefined
  129. template<typename _Tp = void>
  130. struct plus;
  131. template<typename _Tp = void>
  132. struct minus;
  133. template<typename _Tp = void>
  134. struct multiplies;
  135. template<typename _Tp = void>
  136. struct divides;
  137. template<typename _Tp = void>
  138. struct modulus;
  139. template<typename _Tp = void>
  140. struct negate;
  141. #endif
  142. /// One of the @link arithmetic_functors math functors@endlink.
  143. template<typename _Tp>
  144. struct plus : public binary_function<_Tp, _Tp, _Tp>
  145. {
  146. _GLIBCXX14_CONSTEXPR
  147. _Tp
  148. operator()(const _Tp& __x, const _Tp& __y) const
  149. { return __x + __y; }
  150. };
  151. /// One of the @link arithmetic_functors math functors@endlink.
  152. template<typename _Tp>
  153. struct minus : public binary_function<_Tp, _Tp, _Tp>
  154. {
  155. _GLIBCXX14_CONSTEXPR
  156. _Tp
  157. operator()(const _Tp& __x, const _Tp& __y) const
  158. { return __x - __y; }
  159. };
  160. /// One of the @link arithmetic_functors math functors@endlink.
  161. template<typename _Tp>
  162. struct multiplies : public binary_function<_Tp, _Tp, _Tp>
  163. {
  164. _GLIBCXX14_CONSTEXPR
  165. _Tp
  166. operator()(const _Tp& __x, const _Tp& __y) const
  167. { return __x * __y; }
  168. };
  169. /// One of the @link arithmetic_functors math functors@endlink.
  170. template<typename _Tp>
  171. struct divides : public binary_function<_Tp, _Tp, _Tp>
  172. {
  173. _GLIBCXX14_CONSTEXPR
  174. _Tp
  175. operator()(const _Tp& __x, const _Tp& __y) const
  176. { return __x / __y; }
  177. };
  178. /// One of the @link arithmetic_functors math functors@endlink.
  179. template<typename _Tp>
  180. struct modulus : public binary_function<_Tp, _Tp, _Tp>
  181. {
  182. _GLIBCXX14_CONSTEXPR
  183. _Tp
  184. operator()(const _Tp& __x, const _Tp& __y) const
  185. { return __x % __y; }
  186. };
  187. /// One of the @link arithmetic_functors math functors@endlink.
  188. template<typename _Tp>
  189. struct negate : public unary_function<_Tp, _Tp>
  190. {
  191. _GLIBCXX14_CONSTEXPR
  192. _Tp
  193. operator()(const _Tp& __x) const
  194. { return -__x; }
  195. };
  196. #if __cplusplus > 201103L
  197. #define __cpp_lib_transparent_operators 201510
  198. template<>
  199. struct plus<void>
  200. {
  201. template <typename _Tp, typename _Up>
  202. _GLIBCXX14_CONSTEXPR
  203. auto
  204. operator()(_Tp&& __t, _Up&& __u) const
  205. noexcept(noexcept(std::forward<_Tp>(__t) + std::forward<_Up>(__u)))
  206. -> decltype(std::forward<_Tp>(__t) + std::forward<_Up>(__u))
  207. { return std::forward<_Tp>(__t) + std::forward<_Up>(__u); }
  208. typedef __is_transparent is_transparent;
  209. };
  210. /// One of the @link arithmetic_functors math functors@endlink.
  211. template<>
  212. struct minus<void>
  213. {
  214. template <typename _Tp, typename _Up>
  215. _GLIBCXX14_CONSTEXPR
  216. auto
  217. operator()(_Tp&& __t, _Up&& __u) const
  218. noexcept(noexcept(std::forward<_Tp>(__t) - std::forward<_Up>(__u)))
  219. -> decltype(std::forward<_Tp>(__t) - std::forward<_Up>(__u))
  220. { return std::forward<_Tp>(__t) - std::forward<_Up>(__u); }
  221. typedef __is_transparent is_transparent;
  222. };
  223. /// One of the @link arithmetic_functors math functors@endlink.
  224. template<>
  225. struct multiplies<void>
  226. {
  227. template <typename _Tp, typename _Up>
  228. _GLIBCXX14_CONSTEXPR
  229. auto
  230. operator()(_Tp&& __t, _Up&& __u) const
  231. noexcept(noexcept(std::forward<_Tp>(__t) * std::forward<_Up>(__u)))
  232. -> decltype(std::forward<_Tp>(__t) * std::forward<_Up>(__u))
  233. { return std::forward<_Tp>(__t) * std::forward<_Up>(__u); }
  234. typedef __is_transparent is_transparent;
  235. };
  236. /// One of the @link arithmetic_functors math functors@endlink.
  237. template<>
  238. struct divides<void>
  239. {
  240. template <typename _Tp, typename _Up>
  241. _GLIBCXX14_CONSTEXPR
  242. auto
  243. operator()(_Tp&& __t, _Up&& __u) const
  244. noexcept(noexcept(std::forward<_Tp>(__t) / std::forward<_Up>(__u)))
  245. -> decltype(std::forward<_Tp>(__t) / std::forward<_Up>(__u))
  246. { return std::forward<_Tp>(__t) / std::forward<_Up>(__u); }
  247. typedef __is_transparent is_transparent;
  248. };
  249. /// One of the @link arithmetic_functors math functors@endlink.
  250. template<>
  251. struct modulus<void>
  252. {
  253. template <typename _Tp, typename _Up>
  254. _GLIBCXX14_CONSTEXPR
  255. auto
  256. operator()(_Tp&& __t, _Up&& __u) const
  257. noexcept(noexcept(std::forward<_Tp>(__t) % std::forward<_Up>(__u)))
  258. -> decltype(std::forward<_Tp>(__t) % std::forward<_Up>(__u))
  259. { return std::forward<_Tp>(__t) % std::forward<_Up>(__u); }
  260. typedef __is_transparent is_transparent;
  261. };
  262. /// One of the @link arithmetic_functors math functors@endlink.
  263. template<>
  264. struct negate<void>
  265. {
  266. template <typename _Tp>
  267. _GLIBCXX14_CONSTEXPR
  268. auto
  269. operator()(_Tp&& __t) const
  270. noexcept(noexcept(-std::forward<_Tp>(__t)))
  271. -> decltype(-std::forward<_Tp>(__t))
  272. { return -std::forward<_Tp>(__t); }
  273. typedef __is_transparent is_transparent;
  274. };
  275. #endif
  276. /** @} */
  277. // 20.3.3 comparisons
  278. /** @defgroup comparison_functors Comparison Classes
  279. * @ingroup functors
  280. *
  281. * The library provides six wrapper functors for all the basic comparisons
  282. * in C++, like @c <.
  283. *
  284. * @{
  285. */
  286. #if __cplusplus > 201103L
  287. template<typename _Tp = void>
  288. struct equal_to;
  289. template<typename _Tp = void>
  290. struct not_equal_to;
  291. template<typename _Tp = void>
  292. struct greater;
  293. template<typename _Tp = void>
  294. struct less;
  295. template<typename _Tp = void>
  296. struct greater_equal;
  297. template<typename _Tp = void>
  298. struct less_equal;
  299. #endif
  300. /// One of the @link comparison_functors comparison functors@endlink.
  301. template<typename _Tp>
  302. struct equal_to : public binary_function<_Tp, _Tp, bool>
  303. {
  304. _GLIBCXX14_CONSTEXPR
  305. bool
  306. operator()(const _Tp& __x, const _Tp& __y) const
  307. { return __x == __y; }
  308. };
  309. /// One of the @link comparison_functors comparison functors@endlink.
  310. template<typename _Tp>
  311. struct not_equal_to : public binary_function<_Tp, _Tp, bool>
  312. {
  313. _GLIBCXX14_CONSTEXPR
  314. bool
  315. operator()(const _Tp& __x, const _Tp& __y) const
  316. { return __x != __y; }
  317. };
  318. /// One of the @link comparison_functors comparison functors@endlink.
  319. template<typename _Tp>
  320. struct greater : public binary_function<_Tp, _Tp, bool>
  321. {
  322. _GLIBCXX14_CONSTEXPR
  323. bool
  324. operator()(const _Tp& __x, const _Tp& __y) const
  325. { return __x > __y; }
  326. };
  327. /// One of the @link comparison_functors comparison functors@endlink.
  328. template<typename _Tp>
  329. struct less : public binary_function<_Tp, _Tp, bool>
  330. {
  331. _GLIBCXX14_CONSTEXPR
  332. bool
  333. operator()(const _Tp& __x, const _Tp& __y) const
  334. { return __x < __y; }
  335. };
  336. /// One of the @link comparison_functors comparison functors@endlink.
  337. template<typename _Tp>
  338. struct greater_equal : public binary_function<_Tp, _Tp, bool>
  339. {
  340. _GLIBCXX14_CONSTEXPR
  341. bool
  342. operator()(const _Tp& __x, const _Tp& __y) const
  343. { return __x >= __y; }
  344. };
  345. /// One of the @link comparison_functors comparison functors@endlink.
  346. template<typename _Tp>
  347. struct less_equal : public binary_function<_Tp, _Tp, bool>
  348. {
  349. _GLIBCXX14_CONSTEXPR
  350. bool
  351. operator()(const _Tp& __x, const _Tp& __y) const
  352. { return __x <= __y; }
  353. };
  354. // Partial specialization of std::greater for pointers.
  355. template<typename _Tp>
  356. struct greater<_Tp*> : public binary_function<_Tp*, _Tp*, bool>
  357. {
  358. _GLIBCXX14_CONSTEXPR bool
  359. operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW
  360. {
  361. if (__builtin_constant_p (__x > __y))
  362. return __x > __y;
  363. return (__UINTPTR_TYPE__)__x > (__UINTPTR_TYPE__)__y;
  364. }
  365. };
  366. // Partial specialization of std::less for pointers.
  367. template<typename _Tp>
  368. struct less<_Tp*> : public binary_function<_Tp*, _Tp*, bool>
  369. {
  370. _GLIBCXX14_CONSTEXPR bool
  371. operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW
  372. {
  373. if (__builtin_constant_p (__x < __y))
  374. return __x < __y;
  375. return (__UINTPTR_TYPE__)__x < (__UINTPTR_TYPE__)__y;
  376. }
  377. };
  378. // Partial specialization of std::greater_equal for pointers.
  379. template<typename _Tp>
  380. struct greater_equal<_Tp*> : public binary_function<_Tp*, _Tp*, bool>
  381. {
  382. _GLIBCXX14_CONSTEXPR bool
  383. operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW
  384. {
  385. if (__builtin_constant_p (__x >= __y))
  386. return __x >= __y;
  387. return (__UINTPTR_TYPE__)__x >= (__UINTPTR_TYPE__)__y;
  388. }
  389. };
  390. // Partial specialization of std::less_equal for pointers.
  391. template<typename _Tp>
  392. struct less_equal<_Tp*> : public binary_function<_Tp*, _Tp*, bool>
  393. {
  394. _GLIBCXX14_CONSTEXPR bool
  395. operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW
  396. {
  397. if (__builtin_constant_p (__x <= __y))
  398. return __x <= __y;
  399. return (__UINTPTR_TYPE__)__x <= (__UINTPTR_TYPE__)__y;
  400. }
  401. };
  402. #if __cplusplus >= 201402L
  403. /// One of the @link comparison_functors comparison functors@endlink.
  404. template<>
  405. struct equal_to<void>
  406. {
  407. template <typename _Tp, typename _Up>
  408. constexpr auto
  409. operator()(_Tp&& __t, _Up&& __u) const
  410. noexcept(noexcept(std::forward<_Tp>(__t) == std::forward<_Up>(__u)))
  411. -> decltype(std::forward<_Tp>(__t) == std::forward<_Up>(__u))
  412. { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); }
  413. typedef __is_transparent is_transparent;
  414. };
  415. /// One of the @link comparison_functors comparison functors@endlink.
  416. template<>
  417. struct not_equal_to<void>
  418. {
  419. template <typename _Tp, typename _Up>
  420. constexpr auto
  421. operator()(_Tp&& __t, _Up&& __u) const
  422. noexcept(noexcept(std::forward<_Tp>(__t) != std::forward<_Up>(__u)))
  423. -> decltype(std::forward<_Tp>(__t) != std::forward<_Up>(__u))
  424. { return std::forward<_Tp>(__t) != std::forward<_Up>(__u); }
  425. typedef __is_transparent is_transparent;
  426. };
  427. /// One of the @link comparison_functors comparison functors@endlink.
  428. template<>
  429. struct greater<void>
  430. {
  431. template <typename _Tp, typename _Up>
  432. constexpr auto
  433. operator()(_Tp&& __t, _Up&& __u) const
  434. noexcept(noexcept(std::forward<_Tp>(__t) > std::forward<_Up>(__u)))
  435. -> decltype(std::forward<_Tp>(__t) > std::forward<_Up>(__u))
  436. {
  437. return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u),
  438. __ptr_cmp<_Tp, _Up>{});
  439. }
  440. template<typename _Tp, typename _Up>
  441. constexpr bool
  442. operator()(_Tp* __t, _Up* __u) const noexcept
  443. { return greater<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
  444. typedef __is_transparent is_transparent;
  445. private:
  446. template <typename _Tp, typename _Up>
  447. static constexpr decltype(auto)
  448. _S_cmp(_Tp&& __t, _Up&& __u, false_type)
  449. { return std::forward<_Tp>(__t) > std::forward<_Up>(__u); }
  450. template <typename _Tp, typename _Up>
  451. static constexpr bool
  452. _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept
  453. {
  454. return greater<const volatile void*>{}(
  455. static_cast<const volatile void*>(std::forward<_Tp>(__t)),
  456. static_cast<const volatile void*>(std::forward<_Up>(__u)));
  457. }
  458. // True if there is no viable operator> member function.
  459. template<typename _Tp, typename _Up, typename = void>
  460. struct __not_overloaded2 : true_type { };
  461. // False if we can call T.operator>(U)
  462. template<typename _Tp, typename _Up>
  463. struct __not_overloaded2<_Tp, _Up, __void_t<
  464. decltype(std::declval<_Tp>().operator>(std::declval<_Up>()))>>
  465. : false_type { };
  466. // True if there is no overloaded operator> for these operands.
  467. template<typename _Tp, typename _Up, typename = void>
  468. struct __not_overloaded : __not_overloaded2<_Tp, _Up> { };
  469. // False if we can call operator>(T,U)
  470. template<typename _Tp, typename _Up>
  471. struct __not_overloaded<_Tp, _Up, __void_t<
  472. decltype(operator>(std::declval<_Tp>(), std::declval<_Up>()))>>
  473. : false_type { };
  474. template<typename _Tp, typename _Up>
  475. using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>,
  476. is_convertible<_Tp, const volatile void*>,
  477. is_convertible<_Up, const volatile void*>>;
  478. };
  479. /// One of the @link comparison_functors comparison functors@endlink.
  480. template<>
  481. struct less<void>
  482. {
  483. template <typename _Tp, typename _Up>
  484. constexpr auto
  485. operator()(_Tp&& __t, _Up&& __u) const
  486. noexcept(noexcept(std::forward<_Tp>(__t) < std::forward<_Up>(__u)))
  487. -> decltype(std::forward<_Tp>(__t) < std::forward<_Up>(__u))
  488. {
  489. return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u),
  490. __ptr_cmp<_Tp, _Up>{});
  491. }
  492. template<typename _Tp, typename _Up>
  493. constexpr bool
  494. operator()(_Tp* __t, _Up* __u) const noexcept
  495. { return less<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
  496. typedef __is_transparent is_transparent;
  497. private:
  498. template <typename _Tp, typename _Up>
  499. static constexpr decltype(auto)
  500. _S_cmp(_Tp&& __t, _Up&& __u, false_type)
  501. { return std::forward<_Tp>(__t) < std::forward<_Up>(__u); }
  502. template <typename _Tp, typename _Up>
  503. static constexpr bool
  504. _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept
  505. {
  506. return less<const volatile void*>{}(
  507. static_cast<const volatile void*>(std::forward<_Tp>(__t)),
  508. static_cast<const volatile void*>(std::forward<_Up>(__u)));
  509. }
  510. // True if there is no viable operator< member function.
  511. template<typename _Tp, typename _Up, typename = void>
  512. struct __not_overloaded2 : true_type { };
  513. // False if we can call T.operator<(U)
  514. template<typename _Tp, typename _Up>
  515. struct __not_overloaded2<_Tp, _Up, __void_t<
  516. decltype(std::declval<_Tp>().operator<(std::declval<_Up>()))>>
  517. : false_type { };
  518. // True if there is no overloaded operator< for these operands.
  519. template<typename _Tp, typename _Up, typename = void>
  520. struct __not_overloaded : __not_overloaded2<_Tp, _Up> { };
  521. // False if we can call operator<(T,U)
  522. template<typename _Tp, typename _Up>
  523. struct __not_overloaded<_Tp, _Up, __void_t<
  524. decltype(operator<(std::declval<_Tp>(), std::declval<_Up>()))>>
  525. : false_type { };
  526. template<typename _Tp, typename _Up>
  527. using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>,
  528. is_convertible<_Tp, const volatile void*>,
  529. is_convertible<_Up, const volatile void*>>;
  530. };
  531. /// One of the @link comparison_functors comparison functors@endlink.
  532. template<>
  533. struct greater_equal<void>
  534. {
  535. template <typename _Tp, typename _Up>
  536. constexpr auto
  537. operator()(_Tp&& __t, _Up&& __u) const
  538. noexcept(noexcept(std::forward<_Tp>(__t) >= std::forward<_Up>(__u)))
  539. -> decltype(std::forward<_Tp>(__t) >= std::forward<_Up>(__u))
  540. {
  541. return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u),
  542. __ptr_cmp<_Tp, _Up>{});
  543. }
  544. template<typename _Tp, typename _Up>
  545. constexpr bool
  546. operator()(_Tp* __t, _Up* __u) const noexcept
  547. { return greater_equal<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
  548. typedef __is_transparent is_transparent;
  549. private:
  550. template <typename _Tp, typename _Up>
  551. static constexpr decltype(auto)
  552. _S_cmp(_Tp&& __t, _Up&& __u, false_type)
  553. { return std::forward<_Tp>(__t) >= std::forward<_Up>(__u); }
  554. template <typename _Tp, typename _Up>
  555. static constexpr bool
  556. _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept
  557. {
  558. return greater_equal<const volatile void*>{}(
  559. static_cast<const volatile void*>(std::forward<_Tp>(__t)),
  560. static_cast<const volatile void*>(std::forward<_Up>(__u)));
  561. }
  562. // True if there is no viable operator>= member function.
  563. template<typename _Tp, typename _Up, typename = void>
  564. struct __not_overloaded2 : true_type { };
  565. // False if we can call T.operator>=(U)
  566. template<typename _Tp, typename _Up>
  567. struct __not_overloaded2<_Tp, _Up, __void_t<
  568. decltype(std::declval<_Tp>().operator>=(std::declval<_Up>()))>>
  569. : false_type { };
  570. // True if there is no overloaded operator>= for these operands.
  571. template<typename _Tp, typename _Up, typename = void>
  572. struct __not_overloaded : __not_overloaded2<_Tp, _Up> { };
  573. // False if we can call operator>=(T,U)
  574. template<typename _Tp, typename _Up>
  575. struct __not_overloaded<_Tp, _Up, __void_t<
  576. decltype(operator>=(std::declval<_Tp>(), std::declval<_Up>()))>>
  577. : false_type { };
  578. template<typename _Tp, typename _Up>
  579. using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>,
  580. is_convertible<_Tp, const volatile void*>,
  581. is_convertible<_Up, const volatile void*>>;
  582. };
  583. /// One of the @link comparison_functors comparison functors@endlink.
  584. template<>
  585. struct less_equal<void>
  586. {
  587. template <typename _Tp, typename _Up>
  588. constexpr auto
  589. operator()(_Tp&& __t, _Up&& __u) const
  590. noexcept(noexcept(std::forward<_Tp>(__t) <= std::forward<_Up>(__u)))
  591. -> decltype(std::forward<_Tp>(__t) <= std::forward<_Up>(__u))
  592. {
  593. return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u),
  594. __ptr_cmp<_Tp, _Up>{});
  595. }
  596. template<typename _Tp, typename _Up>
  597. constexpr bool
  598. operator()(_Tp* __t, _Up* __u) const noexcept
  599. { return less_equal<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
  600. typedef __is_transparent is_transparent;
  601. private:
  602. template <typename _Tp, typename _Up>
  603. static constexpr decltype(auto)
  604. _S_cmp(_Tp&& __t, _Up&& __u, false_type)
  605. { return std::forward<_Tp>(__t) <= std::forward<_Up>(__u); }
  606. template <typename _Tp, typename _Up>
  607. static constexpr bool
  608. _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept
  609. {
  610. return less_equal<const volatile void*>{}(
  611. static_cast<const volatile void*>(std::forward<_Tp>(__t)),
  612. static_cast<const volatile void*>(std::forward<_Up>(__u)));
  613. }
  614. // True if there is no viable operator<= member function.
  615. template<typename _Tp, typename _Up, typename = void>
  616. struct __not_overloaded2 : true_type { };
  617. // False if we can call T.operator<=(U)
  618. template<typename _Tp, typename _Up>
  619. struct __not_overloaded2<_Tp, _Up, __void_t<
  620. decltype(std::declval<_Tp>().operator<=(std::declval<_Up>()))>>
  621. : false_type { };
  622. // True if there is no overloaded operator<= for these operands.
  623. template<typename _Tp, typename _Up, typename = void>
  624. struct __not_overloaded : __not_overloaded2<_Tp, _Up> { };
  625. // False if we can call operator<=(T,U)
  626. template<typename _Tp, typename _Up>
  627. struct __not_overloaded<_Tp, _Up, __void_t<
  628. decltype(operator<=(std::declval<_Tp>(), std::declval<_Up>()))>>
  629. : false_type { };
  630. template<typename _Tp, typename _Up>
  631. using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>,
  632. is_convertible<_Tp, const volatile void*>,
  633. is_convertible<_Up, const volatile void*>>;
  634. };
  635. #endif // C++14
  636. /** @} */
  637. // 20.3.4 logical operations
  638. /** @defgroup logical_functors Boolean Operations Classes
  639. * @ingroup functors
  640. *
  641. * Here are wrapper functors for Boolean operations: @c &&, @c ||,
  642. * and @c !.
  643. *
  644. * @{
  645. */
  646. #if __cplusplus > 201103L
  647. template<typename _Tp = void>
  648. struct logical_and;
  649. template<typename _Tp = void>
  650. struct logical_or;
  651. template<typename _Tp = void>
  652. struct logical_not;
  653. #endif
  654. /// One of the @link logical_functors Boolean operations functors@endlink.
  655. template<typename _Tp>
  656. struct logical_and : public binary_function<_Tp, _Tp, bool>
  657. {
  658. _GLIBCXX14_CONSTEXPR
  659. bool
  660. operator()(const _Tp& __x, const _Tp& __y) const
  661. { return __x && __y; }
  662. };
  663. /// One of the @link logical_functors Boolean operations functors@endlink.
  664. template<typename _Tp>
  665. struct logical_or : public binary_function<_Tp, _Tp, bool>
  666. {
  667. _GLIBCXX14_CONSTEXPR
  668. bool
  669. operator()(const _Tp& __x, const _Tp& __y) const
  670. { return __x || __y; }
  671. };
  672. /// One of the @link logical_functors Boolean operations functors@endlink.
  673. template<typename _Tp>
  674. struct logical_not : public unary_function<_Tp, bool>
  675. {
  676. _GLIBCXX14_CONSTEXPR
  677. bool
  678. operator()(const _Tp& __x) const
  679. { return !__x; }
  680. };
  681. #if __cplusplus > 201103L
  682. /// One of the @link logical_functors Boolean operations functors@endlink.
  683. template<>
  684. struct logical_and<void>
  685. {
  686. template <typename _Tp, typename _Up>
  687. _GLIBCXX14_CONSTEXPR
  688. auto
  689. operator()(_Tp&& __t, _Up&& __u) const
  690. noexcept(noexcept(std::forward<_Tp>(__t) && std::forward<_Up>(__u)))
  691. -> decltype(std::forward<_Tp>(__t) && std::forward<_Up>(__u))
  692. { return std::forward<_Tp>(__t) && std::forward<_Up>(__u); }
  693. typedef __is_transparent is_transparent;
  694. };
  695. /// One of the @link logical_functors Boolean operations functors@endlink.
  696. template<>
  697. struct logical_or<void>
  698. {
  699. template <typename _Tp, typename _Up>
  700. _GLIBCXX14_CONSTEXPR
  701. auto
  702. operator()(_Tp&& __t, _Up&& __u) const
  703. noexcept(noexcept(std::forward<_Tp>(__t) || std::forward<_Up>(__u)))
  704. -> decltype(std::forward<_Tp>(__t) || std::forward<_Up>(__u))
  705. { return std::forward<_Tp>(__t) || std::forward<_Up>(__u); }
  706. typedef __is_transparent is_transparent;
  707. };
  708. /// One of the @link logical_functors Boolean operations functors@endlink.
  709. template<>
  710. struct logical_not<void>
  711. {
  712. template <typename _Tp>
  713. _GLIBCXX14_CONSTEXPR
  714. auto
  715. operator()(_Tp&& __t) const
  716. noexcept(noexcept(!std::forward<_Tp>(__t)))
  717. -> decltype(!std::forward<_Tp>(__t))
  718. { return !std::forward<_Tp>(__t); }
  719. typedef __is_transparent is_transparent;
  720. };
  721. #endif
  722. /** @} */
  723. #if __cplusplus > 201103L
  724. template<typename _Tp = void>
  725. struct bit_and;
  726. template<typename _Tp = void>
  727. struct bit_or;
  728. template<typename _Tp = void>
  729. struct bit_xor;
  730. template<typename _Tp = void>
  731. struct bit_not;
  732. #endif
  733. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  734. // DR 660. Missing Bitwise Operations.
  735. template<typename _Tp>
  736. struct bit_and : public binary_function<_Tp, _Tp, _Tp>
  737. {
  738. _GLIBCXX14_CONSTEXPR
  739. _Tp
  740. operator()(const _Tp& __x, const _Tp& __y) const
  741. { return __x & __y; }
  742. };
  743. template<typename _Tp>
  744. struct bit_or : public binary_function<_Tp, _Tp, _Tp>
  745. {
  746. _GLIBCXX14_CONSTEXPR
  747. _Tp
  748. operator()(const _Tp& __x, const _Tp& __y) const
  749. { return __x | __y; }
  750. };
  751. template<typename _Tp>
  752. struct bit_xor : public binary_function<_Tp, _Tp, _Tp>
  753. {
  754. _GLIBCXX14_CONSTEXPR
  755. _Tp
  756. operator()(const _Tp& __x, const _Tp& __y) const
  757. { return __x ^ __y; }
  758. };
  759. template<typename _Tp>
  760. struct bit_not : public unary_function<_Tp, _Tp>
  761. {
  762. _GLIBCXX14_CONSTEXPR
  763. _Tp
  764. operator()(const _Tp& __x) const
  765. { return ~__x; }
  766. };
  767. #if __cplusplus > 201103L
  768. template <>
  769. struct bit_and<void>
  770. {
  771. template <typename _Tp, typename _Up>
  772. _GLIBCXX14_CONSTEXPR
  773. auto
  774. operator()(_Tp&& __t, _Up&& __u) const
  775. noexcept(noexcept(std::forward<_Tp>(__t) & std::forward<_Up>(__u)))
  776. -> decltype(std::forward<_Tp>(__t) & std::forward<_Up>(__u))
  777. { return std::forward<_Tp>(__t) & std::forward<_Up>(__u); }
  778. typedef __is_transparent is_transparent;
  779. };
  780. template <>
  781. struct bit_or<void>
  782. {
  783. template <typename _Tp, typename _Up>
  784. _GLIBCXX14_CONSTEXPR
  785. auto
  786. operator()(_Tp&& __t, _Up&& __u) const
  787. noexcept(noexcept(std::forward<_Tp>(__t) | std::forward<_Up>(__u)))
  788. -> decltype(std::forward<_Tp>(__t) | std::forward<_Up>(__u))
  789. { return std::forward<_Tp>(__t) | std::forward<_Up>(__u); }
  790. typedef __is_transparent is_transparent;
  791. };
  792. template <>
  793. struct bit_xor<void>
  794. {
  795. template <typename _Tp, typename _Up>
  796. _GLIBCXX14_CONSTEXPR
  797. auto
  798. operator()(_Tp&& __t, _Up&& __u) const
  799. noexcept(noexcept(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u)))
  800. -> decltype(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u))
  801. { return std::forward<_Tp>(__t) ^ std::forward<_Up>(__u); }
  802. typedef __is_transparent is_transparent;
  803. };
  804. template <>
  805. struct bit_not<void>
  806. {
  807. template <typename _Tp>
  808. _GLIBCXX14_CONSTEXPR
  809. auto
  810. operator()(_Tp&& __t) const
  811. noexcept(noexcept(~std::forward<_Tp>(__t)))
  812. -> decltype(~std::forward<_Tp>(__t))
  813. { return ~std::forward<_Tp>(__t); }
  814. typedef __is_transparent is_transparent;
  815. };
  816. #endif
  817. // 20.3.5 negators
  818. /** @defgroup negators Negators
  819. * @ingroup functors
  820. *
  821. * The functions @c not1 and @c not2 each take a predicate functor
  822. * and return an instance of @c unary_negate or
  823. * @c binary_negate, respectively. These classes are functors whose
  824. * @c operator() performs the stored predicate function and then returns
  825. * the negation of the result.
  826. *
  827. * For example, given a vector of integers and a trivial predicate,
  828. * \code
  829. * struct IntGreaterThanThree
  830. * : public std::unary_function<int, bool>
  831. * {
  832. * bool operator() (int x) { return x > 3; }
  833. * };
  834. *
  835. * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
  836. * \endcode
  837. * The call to @c find_if will locate the first index (i) of @c v for which
  838. * <code>!(v[i] > 3)</code> is true.
  839. *
  840. * The not1/unary_negate combination works on predicates taking a single
  841. * argument. The not2/binary_negate combination works on predicates which
  842. * take two arguments.
  843. *
  844. * @{
  845. */
  846. /// One of the @link negators negation functors@endlink.
  847. template<typename _Predicate>
  848. class unary_negate
  849. : public unary_function<typename _Predicate::argument_type, bool>
  850. {
  851. protected:
  852. _Predicate _M_pred;
  853. public:
  854. _GLIBCXX14_CONSTEXPR
  855. explicit
  856. unary_negate(const _Predicate& __x) : _M_pred(__x) { }
  857. _GLIBCXX14_CONSTEXPR
  858. bool
  859. operator()(const typename _Predicate::argument_type& __x) const
  860. { return !_M_pred(__x); }
  861. };
  862. /// One of the @link negators negation functors@endlink.
  863. template<typename _Predicate>
  864. _GLIBCXX14_CONSTEXPR
  865. inline unary_negate<_Predicate>
  866. not1(const _Predicate& __pred)
  867. { return unary_negate<_Predicate>(__pred); }
  868. /// One of the @link negators negation functors@endlink.
  869. template<typename _Predicate>
  870. class binary_negate
  871. : public binary_function<typename _Predicate::first_argument_type,
  872. typename _Predicate::second_argument_type, bool>
  873. {
  874. protected:
  875. _Predicate _M_pred;
  876. public:
  877. _GLIBCXX14_CONSTEXPR
  878. explicit
  879. binary_negate(const _Predicate& __x) : _M_pred(__x) { }
  880. _GLIBCXX14_CONSTEXPR
  881. bool
  882. operator()(const typename _Predicate::first_argument_type& __x,
  883. const typename _Predicate::second_argument_type& __y) const
  884. { return !_M_pred(__x, __y); }
  885. };
  886. /// One of the @link negators negation functors@endlink.
  887. template<typename _Predicate>
  888. _GLIBCXX14_CONSTEXPR
  889. inline binary_negate<_Predicate>
  890. not2(const _Predicate& __pred)
  891. { return binary_negate<_Predicate>(__pred); }
  892. /** @} */
  893. // 20.3.7 adaptors pointers functions
  894. /** @defgroup pointer_adaptors Adaptors for pointers to functions
  895. * @ingroup functors
  896. *
  897. * The advantage of function objects over pointers to functions is that
  898. * the objects in the standard library declare nested typedefs describing
  899. * their argument and result types with uniform names (e.g., @c result_type
  900. * from the base classes @c unary_function and @c binary_function).
  901. * Sometimes those typedefs are required, not just optional.
  902. *
  903. * Adaptors are provided to turn pointers to unary (single-argument) and
  904. * binary (double-argument) functions into function objects. The
  905. * long-winded functor @c pointer_to_unary_function is constructed with a
  906. * function pointer @c f, and its @c operator() called with argument @c x
  907. * returns @c f(x). The functor @c pointer_to_binary_function does the same
  908. * thing, but with a double-argument @c f and @c operator().
  909. *
  910. * The function @c ptr_fun takes a pointer-to-function @c f and constructs
  911. * an instance of the appropriate functor.
  912. *
  913. * @{
  914. */
  915. /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
  916. template<typename _Arg, typename _Result>
  917. class pointer_to_unary_function : public unary_function<_Arg, _Result>
  918. {
  919. protected:
  920. _Result (*_M_ptr)(_Arg);
  921. public:
  922. pointer_to_unary_function() { }
  923. explicit
  924. pointer_to_unary_function(_Result (*__x)(_Arg))
  925. : _M_ptr(__x) { }
  926. _Result
  927. operator()(_Arg __x) const
  928. { return _M_ptr(__x); }
  929. };
  930. /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
  931. template<typename _Arg, typename _Result>
  932. inline pointer_to_unary_function<_Arg, _Result>
  933. ptr_fun(_Result (*__x)(_Arg))
  934. { return pointer_to_unary_function<_Arg, _Result>(__x); }
  935. /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
  936. template<typename _Arg1, typename _Arg2, typename _Result>
  937. class pointer_to_binary_function
  938. : public binary_function<_Arg1, _Arg2, _Result>
  939. {
  940. protected:
  941. _Result (*_M_ptr)(_Arg1, _Arg2);
  942. public:
  943. pointer_to_binary_function() { }
  944. explicit
  945. pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
  946. : _M_ptr(__x) { }
  947. _Result
  948. operator()(_Arg1 __x, _Arg2 __y) const
  949. { return _M_ptr(__x, __y); }
  950. };
  951. /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
  952. template<typename _Arg1, typename _Arg2, typename _Result>
  953. inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
  954. ptr_fun(_Result (*__x)(_Arg1, _Arg2))
  955. { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
  956. /** @} */
  957. template<typename _Tp>
  958. struct _Identity
  959. : public unary_function<_Tp, _Tp>
  960. {
  961. _Tp&
  962. operator()(_Tp& __x) const
  963. { return __x; }
  964. const _Tp&
  965. operator()(const _Tp& __x) const
  966. { return __x; }
  967. };
  968. // Partial specialization, avoids confusing errors in e.g. std::set<const T>.
  969. template<typename _Tp> struct _Identity<const _Tp> : _Identity<_Tp> { };
  970. template<typename _Pair>
  971. struct _Select1st
  972. : public unary_function<_Pair, typename _Pair::first_type>
  973. {
  974. typename _Pair::first_type&
  975. operator()(_Pair& __x) const
  976. { return __x.first; }
  977. const typename _Pair::first_type&
  978. operator()(const _Pair& __x) const
  979. { return __x.first; }
  980. #if __cplusplus >= 201103L
  981. template<typename _Pair2>
  982. typename _Pair2::first_type&
  983. operator()(_Pair2& __x) const
  984. { return __x.first; }
  985. template<typename _Pair2>
  986. const typename _Pair2::first_type&
  987. operator()(const _Pair2& __x) const
  988. { return __x.first; }
  989. #endif
  990. };
  991. template<typename _Pair>
  992. struct _Select2nd
  993. : public unary_function<_Pair, typename _Pair::second_type>
  994. {
  995. typename _Pair::second_type&
  996. operator()(_Pair& __x) const
  997. { return __x.second; }
  998. const typename _Pair::second_type&
  999. operator()(const _Pair& __x) const
  1000. { return __x.second; }
  1001. };
  1002. // 20.3.8 adaptors pointers members
  1003. /** @defgroup memory_adaptors Adaptors for pointers to members
  1004. * @ingroup functors
  1005. *
  1006. * There are a total of 8 = 2^3 function objects in this family.
  1007. * (1) Member functions taking no arguments vs member functions taking
  1008. * one argument.
  1009. * (2) Call through pointer vs call through reference.
  1010. * (3) Const vs non-const member function.
  1011. *
  1012. * All of this complexity is in the function objects themselves. You can
  1013. * ignore it by using the helper function mem_fun and mem_fun_ref,
  1014. * which create whichever type of adaptor is appropriate.
  1015. *
  1016. * @{
  1017. */
  1018. /// One of the @link memory_adaptors adaptors for member
  1019. /// pointers@endlink.
  1020. template<typename _Ret, typename _Tp>
  1021. class mem_fun_t : public unary_function<_Tp*, _Ret>
  1022. {
  1023. public:
  1024. explicit
  1025. mem_fun_t(_Ret (_Tp::*__pf)())
  1026. : _M_f(__pf) { }
  1027. _Ret
  1028. operator()(_Tp* __p) const
  1029. { return (__p->*_M_f)(); }
  1030. private:
  1031. _Ret (_Tp::*_M_f)();
  1032. };
  1033. /// One of the @link memory_adaptors adaptors for member
  1034. /// pointers@endlink.
  1035. template<typename _Ret, typename _Tp>
  1036. class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
  1037. {
  1038. public:
  1039. explicit
  1040. const_mem_fun_t(_Ret (_Tp::*__pf)() const)
  1041. : _M_f(__pf) { }
  1042. _Ret
  1043. operator()(const _Tp* __p) const
  1044. { return (__p->*_M_f)(); }
  1045. private:
  1046. _Ret (_Tp::*_M_f)() const;
  1047. };
  1048. /// One of the @link memory_adaptors adaptors for member
  1049. /// pointers@endlink.
  1050. template<typename _Ret, typename _Tp>
  1051. class mem_fun_ref_t : public unary_function<_Tp, _Ret>
  1052. {
  1053. public:
  1054. explicit
  1055. mem_fun_ref_t(_Ret (_Tp::*__pf)())
  1056. : _M_f(__pf) { }
  1057. _Ret
  1058. operator()(_Tp& __r) const
  1059. { return (__r.*_M_f)(); }
  1060. private:
  1061. _Ret (_Tp::*_M_f)();
  1062. };
  1063. /// One of the @link memory_adaptors adaptors for member
  1064. /// pointers@endlink.
  1065. template<typename _Ret, typename _Tp>
  1066. class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
  1067. {
  1068. public:
  1069. explicit
  1070. const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
  1071. : _M_f(__pf) { }
  1072. _Ret
  1073. operator()(const _Tp& __r) const
  1074. { return (__r.*_M_f)(); }
  1075. private:
  1076. _Ret (_Tp::*_M_f)() const;
  1077. };
  1078. /// One of the @link memory_adaptors adaptors for member
  1079. /// pointers@endlink.
  1080. template<typename _Ret, typename _Tp, typename _Arg>
  1081. class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
  1082. {
  1083. public:
  1084. explicit
  1085. mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
  1086. : _M_f(__pf) { }
  1087. _Ret
  1088. operator()(_Tp* __p, _Arg __x) const
  1089. { return (__p->*_M_f)(__x); }
  1090. private:
  1091. _Ret (_Tp::*_M_f)(_Arg);
  1092. };
  1093. /// One of the @link memory_adaptors adaptors for member
  1094. /// pointers@endlink.
  1095. template<typename _Ret, typename _Tp, typename _Arg>
  1096. class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
  1097. {
  1098. public:
  1099. explicit
  1100. const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
  1101. : _M_f(__pf) { }
  1102. _Ret
  1103. operator()(const _Tp* __p, _Arg __x) const
  1104. { return (__p->*_M_f)(__x); }
  1105. private:
  1106. _Ret (_Tp::*_M_f)(_Arg) const;
  1107. };
  1108. /// One of the @link memory_adaptors adaptors for member
  1109. /// pointers@endlink.
  1110. template<typename _Ret, typename _Tp, typename _Arg>
  1111. class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
  1112. {
  1113. public:
  1114. explicit
  1115. mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
  1116. : _M_f(__pf) { }
  1117. _Ret
  1118. operator()(_Tp& __r, _Arg __x) const
  1119. { return (__r.*_M_f)(__x); }
  1120. private:
  1121. _Ret (_Tp::*_M_f)(_Arg);
  1122. };
  1123. /// One of the @link memory_adaptors adaptors for member
  1124. /// pointers@endlink.
  1125. template<typename _Ret, typename _Tp, typename _Arg>
  1126. class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
  1127. {
  1128. public:
  1129. explicit
  1130. const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
  1131. : _M_f(__pf) { }
  1132. _Ret
  1133. operator()(const _Tp& __r, _Arg __x) const
  1134. { return (__r.*_M_f)(__x); }
  1135. private:
  1136. _Ret (_Tp::*_M_f)(_Arg) const;
  1137. };
  1138. // Mem_fun adaptor helper functions. There are only two:
  1139. // mem_fun and mem_fun_ref.
  1140. template<typename _Ret, typename _Tp>
  1141. inline mem_fun_t<_Ret, _Tp>
  1142. mem_fun(_Ret (_Tp::*__f)())
  1143. { return mem_fun_t<_Ret, _Tp>(__f); }
  1144. template<typename _Ret, typename _Tp>
  1145. inline const_mem_fun_t<_Ret, _Tp>
  1146. mem_fun(_Ret (_Tp::*__f)() const)
  1147. { return const_mem_fun_t<_Ret, _Tp>(__f); }
  1148. template<typename _Ret, typename _Tp>
  1149. inline mem_fun_ref_t<_Ret, _Tp>
  1150. mem_fun_ref(_Ret (_Tp::*__f)())
  1151. { return mem_fun_ref_t<_Ret, _Tp>(__f); }
  1152. template<typename _Ret, typename _Tp>
  1153. inline const_mem_fun_ref_t<_Ret, _Tp>
  1154. mem_fun_ref(_Ret (_Tp::*__f)() const)
  1155. { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
  1156. template<typename _Ret, typename _Tp, typename _Arg>
  1157. inline mem_fun1_t<_Ret, _Tp, _Arg>
  1158. mem_fun(_Ret (_Tp::*__f)(_Arg))
  1159. { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
  1160. template<typename _Ret, typename _Tp, typename _Arg>
  1161. inline const_mem_fun1_t<_Ret, _Tp, _Arg>
  1162. mem_fun(_Ret (_Tp::*__f)(_Arg) const)
  1163. { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
  1164. template<typename _Ret, typename _Tp, typename _Arg>
  1165. inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
  1166. mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
  1167. { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
  1168. template<typename _Ret, typename _Tp, typename _Arg>
  1169. inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
  1170. mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
  1171. { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
  1172. /** @} */
  1173. _GLIBCXX_END_NAMESPACE_VERSION
  1174. } // namespace
  1175. #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
  1176. # include <backward/binders.h>
  1177. #endif
  1178. #endif /* _STL_FUNCTION_H */