You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1396 lines
41KB

  1. // Functor implementations -*- C++ -*-
  2. // Copyright (C) 2001-2020 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 __cplusplus >= 201402L
  362. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  363. if (__builtin_is_constant_evaluated())
  364. #else
  365. if (__builtin_constant_p(__x > __y))
  366. #endif
  367. return __x > __y;
  368. #endif
  369. return (__UINTPTR_TYPE__)__x > (__UINTPTR_TYPE__)__y;
  370. }
  371. };
  372. // Partial specialization of std::less for pointers.
  373. template<typename _Tp>
  374. struct less<_Tp*> : public binary_function<_Tp*, _Tp*, bool>
  375. {
  376. _GLIBCXX14_CONSTEXPR bool
  377. operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW
  378. {
  379. #if __cplusplus >= 201402L
  380. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  381. if (__builtin_is_constant_evaluated())
  382. #else
  383. if (__builtin_constant_p(__x < __y))
  384. #endif
  385. return __x < __y;
  386. #endif
  387. return (__UINTPTR_TYPE__)__x < (__UINTPTR_TYPE__)__y;
  388. }
  389. };
  390. // Partial specialization of std::greater_equal for pointers.
  391. template<typename _Tp>
  392. struct greater_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 __cplusplus >= 201402L
  398. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  399. if (__builtin_is_constant_evaluated())
  400. #else
  401. if (__builtin_constant_p(__x >= __y))
  402. #endif
  403. return __x >= __y;
  404. #endif
  405. return (__UINTPTR_TYPE__)__x >= (__UINTPTR_TYPE__)__y;
  406. }
  407. };
  408. // Partial specialization of std::less_equal for pointers.
  409. template<typename _Tp>
  410. struct less_equal<_Tp*> : public binary_function<_Tp*, _Tp*, bool>
  411. {
  412. _GLIBCXX14_CONSTEXPR bool
  413. operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW
  414. {
  415. #if __cplusplus >= 201402L
  416. #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  417. if (__builtin_is_constant_evaluated())
  418. #else
  419. if (__builtin_constant_p(__x <= __y))
  420. #endif
  421. return __x <= __y;
  422. #endif
  423. return (__UINTPTR_TYPE__)__x <= (__UINTPTR_TYPE__)__y;
  424. }
  425. };
  426. #if __cplusplus >= 201402L
  427. /// One of the @link comparison_functors comparison functors@endlink.
  428. template<>
  429. struct equal_to<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. { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); }
  437. typedef __is_transparent is_transparent;
  438. };
  439. /// One of the @link comparison_functors comparison functors@endlink.
  440. template<>
  441. struct not_equal_to<void>
  442. {
  443. template <typename _Tp, typename _Up>
  444. constexpr auto
  445. operator()(_Tp&& __t, _Up&& __u) const
  446. noexcept(noexcept(std::forward<_Tp>(__t) != std::forward<_Up>(__u)))
  447. -> decltype(std::forward<_Tp>(__t) != std::forward<_Up>(__u))
  448. { return std::forward<_Tp>(__t) != std::forward<_Up>(__u); }
  449. typedef __is_transparent is_transparent;
  450. };
  451. /// One of the @link comparison_functors comparison functors@endlink.
  452. template<>
  453. struct greater<void>
  454. {
  455. template <typename _Tp, typename _Up>
  456. constexpr auto
  457. operator()(_Tp&& __t, _Up&& __u) const
  458. noexcept(noexcept(std::forward<_Tp>(__t) > std::forward<_Up>(__u)))
  459. -> decltype(std::forward<_Tp>(__t) > std::forward<_Up>(__u))
  460. {
  461. return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u),
  462. __ptr_cmp<_Tp, _Up>{});
  463. }
  464. template<typename _Tp, typename _Up>
  465. constexpr bool
  466. operator()(_Tp* __t, _Up* __u) const noexcept
  467. { return greater<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
  468. typedef __is_transparent is_transparent;
  469. private:
  470. template <typename _Tp, typename _Up>
  471. static constexpr decltype(auto)
  472. _S_cmp(_Tp&& __t, _Up&& __u, false_type)
  473. { return std::forward<_Tp>(__t) > std::forward<_Up>(__u); }
  474. template <typename _Tp, typename _Up>
  475. static constexpr bool
  476. _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept
  477. {
  478. return greater<const volatile void*>{}(
  479. static_cast<const volatile void*>(std::forward<_Tp>(__t)),
  480. static_cast<const volatile void*>(std::forward<_Up>(__u)));
  481. }
  482. // True if there is no viable operator> member function.
  483. template<typename _Tp, typename _Up, typename = void>
  484. struct __not_overloaded2 : true_type { };
  485. // False if we can call T.operator>(U)
  486. template<typename _Tp, typename _Up>
  487. struct __not_overloaded2<_Tp, _Up, __void_t<
  488. decltype(std::declval<_Tp>().operator>(std::declval<_Up>()))>>
  489. : false_type { };
  490. // True if there is no overloaded operator> for these operands.
  491. template<typename _Tp, typename _Up, typename = void>
  492. struct __not_overloaded : __not_overloaded2<_Tp, _Up> { };
  493. // False if we can call operator>(T,U)
  494. template<typename _Tp, typename _Up>
  495. struct __not_overloaded<_Tp, _Up, __void_t<
  496. decltype(operator>(std::declval<_Tp>(), std::declval<_Up>()))>>
  497. : false_type { };
  498. template<typename _Tp, typename _Up>
  499. using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>,
  500. is_convertible<_Tp, const volatile void*>,
  501. is_convertible<_Up, const volatile void*>>;
  502. };
  503. /// One of the @link comparison_functors comparison functors@endlink.
  504. template<>
  505. struct less<void>
  506. {
  507. template <typename _Tp, typename _Up>
  508. constexpr auto
  509. operator()(_Tp&& __t, _Up&& __u) const
  510. noexcept(noexcept(std::forward<_Tp>(__t) < std::forward<_Up>(__u)))
  511. -> decltype(std::forward<_Tp>(__t) < std::forward<_Up>(__u))
  512. {
  513. return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u),
  514. __ptr_cmp<_Tp, _Up>{});
  515. }
  516. template<typename _Tp, typename _Up>
  517. constexpr bool
  518. operator()(_Tp* __t, _Up* __u) const noexcept
  519. { return less<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
  520. typedef __is_transparent is_transparent;
  521. private:
  522. template <typename _Tp, typename _Up>
  523. static constexpr decltype(auto)
  524. _S_cmp(_Tp&& __t, _Up&& __u, false_type)
  525. { return std::forward<_Tp>(__t) < std::forward<_Up>(__u); }
  526. template <typename _Tp, typename _Up>
  527. static constexpr bool
  528. _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept
  529. {
  530. return less<const volatile void*>{}(
  531. static_cast<const volatile void*>(std::forward<_Tp>(__t)),
  532. static_cast<const volatile void*>(std::forward<_Up>(__u)));
  533. }
  534. // True if there is no viable operator< member function.
  535. template<typename _Tp, typename _Up, typename = void>
  536. struct __not_overloaded2 : true_type { };
  537. // False if we can call T.operator<(U)
  538. template<typename _Tp, typename _Up>
  539. struct __not_overloaded2<_Tp, _Up, __void_t<
  540. decltype(std::declval<_Tp>().operator<(std::declval<_Up>()))>>
  541. : false_type { };
  542. // True if there is no overloaded operator< for these operands.
  543. template<typename _Tp, typename _Up, typename = void>
  544. struct __not_overloaded : __not_overloaded2<_Tp, _Up> { };
  545. // False if we can call operator<(T,U)
  546. template<typename _Tp, typename _Up>
  547. struct __not_overloaded<_Tp, _Up, __void_t<
  548. decltype(operator<(std::declval<_Tp>(), std::declval<_Up>()))>>
  549. : false_type { };
  550. template<typename _Tp, typename _Up>
  551. using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>,
  552. is_convertible<_Tp, const volatile void*>,
  553. is_convertible<_Up, const volatile void*>>;
  554. };
  555. /// One of the @link comparison_functors comparison functors@endlink.
  556. template<>
  557. struct greater_equal<void>
  558. {
  559. template <typename _Tp, typename _Up>
  560. constexpr auto
  561. operator()(_Tp&& __t, _Up&& __u) const
  562. noexcept(noexcept(std::forward<_Tp>(__t) >= std::forward<_Up>(__u)))
  563. -> decltype(std::forward<_Tp>(__t) >= std::forward<_Up>(__u))
  564. {
  565. return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u),
  566. __ptr_cmp<_Tp, _Up>{});
  567. }
  568. template<typename _Tp, typename _Up>
  569. constexpr bool
  570. operator()(_Tp* __t, _Up* __u) const noexcept
  571. { return greater_equal<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
  572. typedef __is_transparent is_transparent;
  573. private:
  574. template <typename _Tp, typename _Up>
  575. static constexpr decltype(auto)
  576. _S_cmp(_Tp&& __t, _Up&& __u, false_type)
  577. { return std::forward<_Tp>(__t) >= std::forward<_Up>(__u); }
  578. template <typename _Tp, typename _Up>
  579. static constexpr bool
  580. _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept
  581. {
  582. return greater_equal<const volatile void*>{}(
  583. static_cast<const volatile void*>(std::forward<_Tp>(__t)),
  584. static_cast<const volatile void*>(std::forward<_Up>(__u)));
  585. }
  586. // True if there is no viable operator>= member function.
  587. template<typename _Tp, typename _Up, typename = void>
  588. struct __not_overloaded2 : true_type { };
  589. // False if we can call T.operator>=(U)
  590. template<typename _Tp, typename _Up>
  591. struct __not_overloaded2<_Tp, _Up, __void_t<
  592. decltype(std::declval<_Tp>().operator>=(std::declval<_Up>()))>>
  593. : false_type { };
  594. // True if there is no overloaded operator>= for these operands.
  595. template<typename _Tp, typename _Up, typename = void>
  596. struct __not_overloaded : __not_overloaded2<_Tp, _Up> { };
  597. // False if we can call operator>=(T,U)
  598. template<typename _Tp, typename _Up>
  599. struct __not_overloaded<_Tp, _Up, __void_t<
  600. decltype(operator>=(std::declval<_Tp>(), std::declval<_Up>()))>>
  601. : false_type { };
  602. template<typename _Tp, typename _Up>
  603. using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>,
  604. is_convertible<_Tp, const volatile void*>,
  605. is_convertible<_Up, const volatile void*>>;
  606. };
  607. /// One of the @link comparison_functors comparison functors@endlink.
  608. template<>
  609. struct less_equal<void>
  610. {
  611. template <typename _Tp, typename _Up>
  612. constexpr auto
  613. operator()(_Tp&& __t, _Up&& __u) const
  614. noexcept(noexcept(std::forward<_Tp>(__t) <= std::forward<_Up>(__u)))
  615. -> decltype(std::forward<_Tp>(__t) <= std::forward<_Up>(__u))
  616. {
  617. return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u),
  618. __ptr_cmp<_Tp, _Up>{});
  619. }
  620. template<typename _Tp, typename _Up>
  621. constexpr bool
  622. operator()(_Tp* __t, _Up* __u) const noexcept
  623. { return less_equal<common_type_t<_Tp*, _Up*>>{}(__t, __u); }
  624. typedef __is_transparent is_transparent;
  625. private:
  626. template <typename _Tp, typename _Up>
  627. static constexpr decltype(auto)
  628. _S_cmp(_Tp&& __t, _Up&& __u, false_type)
  629. { return std::forward<_Tp>(__t) <= std::forward<_Up>(__u); }
  630. template <typename _Tp, typename _Up>
  631. static constexpr bool
  632. _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept
  633. {
  634. return less_equal<const volatile void*>{}(
  635. static_cast<const volatile void*>(std::forward<_Tp>(__t)),
  636. static_cast<const volatile void*>(std::forward<_Up>(__u)));
  637. }
  638. // True if there is no viable operator<= member function.
  639. template<typename _Tp, typename _Up, typename = void>
  640. struct __not_overloaded2 : true_type { };
  641. // False if we can call T.operator<=(U)
  642. template<typename _Tp, typename _Up>
  643. struct __not_overloaded2<_Tp, _Up, __void_t<
  644. decltype(std::declval<_Tp>().operator<=(std::declval<_Up>()))>>
  645. : false_type { };
  646. // True if there is no overloaded operator<= for these operands.
  647. template<typename _Tp, typename _Up, typename = void>
  648. struct __not_overloaded : __not_overloaded2<_Tp, _Up> { };
  649. // False if we can call operator<=(T,U)
  650. template<typename _Tp, typename _Up>
  651. struct __not_overloaded<_Tp, _Up, __void_t<
  652. decltype(operator<=(std::declval<_Tp>(), std::declval<_Up>()))>>
  653. : false_type { };
  654. template<typename _Tp, typename _Up>
  655. using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>,
  656. is_convertible<_Tp, const volatile void*>,
  657. is_convertible<_Up, const volatile void*>>;
  658. };
  659. #endif // C++14
  660. /** @} */
  661. // 20.3.4 logical operations
  662. /** @defgroup logical_functors Boolean Operations Classes
  663. * @ingroup functors
  664. *
  665. * Here are wrapper functors for Boolean operations: @c &&, @c ||,
  666. * and @c !.
  667. *
  668. * @{
  669. */
  670. #if __cplusplus > 201103L
  671. template<typename _Tp = void>
  672. struct logical_and;
  673. template<typename _Tp = void>
  674. struct logical_or;
  675. template<typename _Tp = void>
  676. struct logical_not;
  677. #endif
  678. /// One of the @link logical_functors Boolean operations functors@endlink.
  679. template<typename _Tp>
  680. struct logical_and : public binary_function<_Tp, _Tp, bool>
  681. {
  682. _GLIBCXX14_CONSTEXPR
  683. bool
  684. operator()(const _Tp& __x, const _Tp& __y) const
  685. { return __x && __y; }
  686. };
  687. /// One of the @link logical_functors Boolean operations functors@endlink.
  688. template<typename _Tp>
  689. struct logical_or : public binary_function<_Tp, _Tp, bool>
  690. {
  691. _GLIBCXX14_CONSTEXPR
  692. bool
  693. operator()(const _Tp& __x, const _Tp& __y) const
  694. { return __x || __y; }
  695. };
  696. /// One of the @link logical_functors Boolean operations functors@endlink.
  697. template<typename _Tp>
  698. struct logical_not : public unary_function<_Tp, bool>
  699. {
  700. _GLIBCXX14_CONSTEXPR
  701. bool
  702. operator()(const _Tp& __x) const
  703. { return !__x; }
  704. };
  705. #if __cplusplus > 201103L
  706. /// One of the @link logical_functors Boolean operations functors@endlink.
  707. template<>
  708. struct logical_and<void>
  709. {
  710. template <typename _Tp, typename _Up>
  711. _GLIBCXX14_CONSTEXPR
  712. auto
  713. operator()(_Tp&& __t, _Up&& __u) const
  714. noexcept(noexcept(std::forward<_Tp>(__t) && std::forward<_Up>(__u)))
  715. -> decltype(std::forward<_Tp>(__t) && std::forward<_Up>(__u))
  716. { return std::forward<_Tp>(__t) && std::forward<_Up>(__u); }
  717. typedef __is_transparent is_transparent;
  718. };
  719. /// One of the @link logical_functors Boolean operations functors@endlink.
  720. template<>
  721. struct logical_or<void>
  722. {
  723. template <typename _Tp, typename _Up>
  724. _GLIBCXX14_CONSTEXPR
  725. auto
  726. operator()(_Tp&& __t, _Up&& __u) const
  727. noexcept(noexcept(std::forward<_Tp>(__t) || std::forward<_Up>(__u)))
  728. -> decltype(std::forward<_Tp>(__t) || std::forward<_Up>(__u))
  729. { return std::forward<_Tp>(__t) || std::forward<_Up>(__u); }
  730. typedef __is_transparent is_transparent;
  731. };
  732. /// One of the @link logical_functors Boolean operations functors@endlink.
  733. template<>
  734. struct logical_not<void>
  735. {
  736. template <typename _Tp>
  737. _GLIBCXX14_CONSTEXPR
  738. auto
  739. operator()(_Tp&& __t) const
  740. noexcept(noexcept(!std::forward<_Tp>(__t)))
  741. -> decltype(!std::forward<_Tp>(__t))
  742. { return !std::forward<_Tp>(__t); }
  743. typedef __is_transparent is_transparent;
  744. };
  745. #endif
  746. /** @} */
  747. #if __cplusplus > 201103L
  748. template<typename _Tp = void>
  749. struct bit_and;
  750. template<typename _Tp = void>
  751. struct bit_or;
  752. template<typename _Tp = void>
  753. struct bit_xor;
  754. template<typename _Tp = void>
  755. struct bit_not;
  756. #endif
  757. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  758. // DR 660. Missing Bitwise Operations.
  759. template<typename _Tp>
  760. struct bit_and : public binary_function<_Tp, _Tp, _Tp>
  761. {
  762. _GLIBCXX14_CONSTEXPR
  763. _Tp
  764. operator()(const _Tp& __x, const _Tp& __y) const
  765. { return __x & __y; }
  766. };
  767. template<typename _Tp>
  768. struct bit_or : public binary_function<_Tp, _Tp, _Tp>
  769. {
  770. _GLIBCXX14_CONSTEXPR
  771. _Tp
  772. operator()(const _Tp& __x, const _Tp& __y) const
  773. { return __x | __y; }
  774. };
  775. template<typename _Tp>
  776. struct bit_xor : public binary_function<_Tp, _Tp, _Tp>
  777. {
  778. _GLIBCXX14_CONSTEXPR
  779. _Tp
  780. operator()(const _Tp& __x, const _Tp& __y) const
  781. { return __x ^ __y; }
  782. };
  783. template<typename _Tp>
  784. struct bit_not : public unary_function<_Tp, _Tp>
  785. {
  786. _GLIBCXX14_CONSTEXPR
  787. _Tp
  788. operator()(const _Tp& __x) const
  789. { return ~__x; }
  790. };
  791. #if __cplusplus > 201103L
  792. template <>
  793. struct bit_and<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_or<void>
  806. {
  807. template <typename _Tp, typename _Up>
  808. _GLIBCXX14_CONSTEXPR
  809. auto
  810. operator()(_Tp&& __t, _Up&& __u) const
  811. noexcept(noexcept(std::forward<_Tp>(__t) | std::forward<_Up>(__u)))
  812. -> decltype(std::forward<_Tp>(__t) | std::forward<_Up>(__u))
  813. { return std::forward<_Tp>(__t) | std::forward<_Up>(__u); }
  814. typedef __is_transparent is_transparent;
  815. };
  816. template <>
  817. struct bit_xor<void>
  818. {
  819. template <typename _Tp, typename _Up>
  820. _GLIBCXX14_CONSTEXPR
  821. auto
  822. operator()(_Tp&& __t, _Up&& __u) const
  823. noexcept(noexcept(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u)))
  824. -> decltype(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u))
  825. { return std::forward<_Tp>(__t) ^ std::forward<_Up>(__u); }
  826. typedef __is_transparent is_transparent;
  827. };
  828. template <>
  829. struct bit_not<void>
  830. {
  831. template <typename _Tp>
  832. _GLIBCXX14_CONSTEXPR
  833. auto
  834. operator()(_Tp&& __t) const
  835. noexcept(noexcept(~std::forward<_Tp>(__t)))
  836. -> decltype(~std::forward<_Tp>(__t))
  837. { return ~std::forward<_Tp>(__t); }
  838. typedef __is_transparent is_transparent;
  839. };
  840. #endif
  841. // 20.3.5 negators
  842. /** @defgroup negators Negators
  843. * @ingroup functors
  844. *
  845. * The functions @c not1 and @c not2 each take a predicate functor
  846. * and return an instance of @c unary_negate or
  847. * @c binary_negate, respectively. These classes are functors whose
  848. * @c operator() performs the stored predicate function and then returns
  849. * the negation of the result.
  850. *
  851. * For example, given a vector of integers and a trivial predicate,
  852. * \code
  853. * struct IntGreaterThanThree
  854. * : public std::unary_function<int, bool>
  855. * {
  856. * bool operator() (int x) { return x > 3; }
  857. * };
  858. *
  859. * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
  860. * \endcode
  861. * The call to @c find_if will locate the first index (i) of @c v for which
  862. * <code>!(v[i] > 3)</code> is true.
  863. *
  864. * The not1/unary_negate combination works on predicates taking a single
  865. * argument. The not2/binary_negate combination works on predicates which
  866. * take two arguments.
  867. *
  868. * @{
  869. */
  870. /// One of the @link negators negation functors@endlink.
  871. template<typename _Predicate>
  872. class unary_negate
  873. : public unary_function<typename _Predicate::argument_type, bool>
  874. {
  875. protected:
  876. _Predicate _M_pred;
  877. public:
  878. _GLIBCXX14_CONSTEXPR
  879. explicit
  880. unary_negate(const _Predicate& __x) : _M_pred(__x) { }
  881. _GLIBCXX14_CONSTEXPR
  882. bool
  883. operator()(const typename _Predicate::argument_type& __x) const
  884. { return !_M_pred(__x); }
  885. };
  886. /// One of the @link negators negation functors@endlink.
  887. template<typename _Predicate>
  888. _GLIBCXX14_CONSTEXPR
  889. inline unary_negate<_Predicate>
  890. not1(const _Predicate& __pred)
  891. { return unary_negate<_Predicate>(__pred); }
  892. /// One of the @link negators negation functors@endlink.
  893. template<typename _Predicate>
  894. class binary_negate
  895. : public binary_function<typename _Predicate::first_argument_type,
  896. typename _Predicate::second_argument_type, bool>
  897. {
  898. protected:
  899. _Predicate _M_pred;
  900. public:
  901. _GLIBCXX14_CONSTEXPR
  902. explicit
  903. binary_negate(const _Predicate& __x) : _M_pred(__x) { }
  904. _GLIBCXX14_CONSTEXPR
  905. bool
  906. operator()(const typename _Predicate::first_argument_type& __x,
  907. const typename _Predicate::second_argument_type& __y) const
  908. { return !_M_pred(__x, __y); }
  909. };
  910. /// One of the @link negators negation functors@endlink.
  911. template<typename _Predicate>
  912. _GLIBCXX14_CONSTEXPR
  913. inline binary_negate<_Predicate>
  914. not2(const _Predicate& __pred)
  915. { return binary_negate<_Predicate>(__pred); }
  916. /** @} */
  917. // 20.3.7 adaptors pointers functions
  918. /** @defgroup pointer_adaptors Adaptors for pointers to functions
  919. * @ingroup functors
  920. *
  921. * The advantage of function objects over pointers to functions is that
  922. * the objects in the standard library declare nested typedefs describing
  923. * their argument and result types with uniform names (e.g., @c result_type
  924. * from the base classes @c unary_function and @c binary_function).
  925. * Sometimes those typedefs are required, not just optional.
  926. *
  927. * Adaptors are provided to turn pointers to unary (single-argument) and
  928. * binary (double-argument) functions into function objects. The
  929. * long-winded functor @c pointer_to_unary_function is constructed with a
  930. * function pointer @c f, and its @c operator() called with argument @c x
  931. * returns @c f(x). The functor @c pointer_to_binary_function does the same
  932. * thing, but with a double-argument @c f and @c operator().
  933. *
  934. * The function @c ptr_fun takes a pointer-to-function @c f and constructs
  935. * an instance of the appropriate functor.
  936. *
  937. * @{
  938. */
  939. /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
  940. template<typename _Arg, typename _Result>
  941. class pointer_to_unary_function : public unary_function<_Arg, _Result>
  942. {
  943. protected:
  944. _Result (*_M_ptr)(_Arg);
  945. public:
  946. pointer_to_unary_function() { }
  947. explicit
  948. pointer_to_unary_function(_Result (*__x)(_Arg))
  949. : _M_ptr(__x) { }
  950. _Result
  951. operator()(_Arg __x) const
  952. { return _M_ptr(__x); }
  953. };
  954. /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
  955. template<typename _Arg, typename _Result>
  956. inline pointer_to_unary_function<_Arg, _Result>
  957. ptr_fun(_Result (*__x)(_Arg))
  958. { return pointer_to_unary_function<_Arg, _Result>(__x); }
  959. /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
  960. template<typename _Arg1, typename _Arg2, typename _Result>
  961. class pointer_to_binary_function
  962. : public binary_function<_Arg1, _Arg2, _Result>
  963. {
  964. protected:
  965. _Result (*_M_ptr)(_Arg1, _Arg2);
  966. public:
  967. pointer_to_binary_function() { }
  968. explicit
  969. pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
  970. : _M_ptr(__x) { }
  971. _Result
  972. operator()(_Arg1 __x, _Arg2 __y) const
  973. { return _M_ptr(__x, __y); }
  974. };
  975. /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
  976. template<typename _Arg1, typename _Arg2, typename _Result>
  977. inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
  978. ptr_fun(_Result (*__x)(_Arg1, _Arg2))
  979. { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
  980. /** @} */
  981. template<typename _Tp>
  982. struct _Identity
  983. : public unary_function<_Tp, _Tp>
  984. {
  985. _Tp&
  986. operator()(_Tp& __x) const
  987. { return __x; }
  988. const _Tp&
  989. operator()(const _Tp& __x) const
  990. { return __x; }
  991. };
  992. // Partial specialization, avoids confusing errors in e.g. std::set<const T>.
  993. template<typename _Tp> struct _Identity<const _Tp> : _Identity<_Tp> { };
  994. template<typename _Pair>
  995. struct _Select1st
  996. : public unary_function<_Pair, typename _Pair::first_type>
  997. {
  998. typename _Pair::first_type&
  999. operator()(_Pair& __x) const
  1000. { return __x.first; }
  1001. const typename _Pair::first_type&
  1002. operator()(const _Pair& __x) const
  1003. { return __x.first; }
  1004. #if __cplusplus >= 201103L
  1005. template<typename _Pair2>
  1006. typename _Pair2::first_type&
  1007. operator()(_Pair2& __x) const
  1008. { return __x.first; }
  1009. template<typename _Pair2>
  1010. const typename _Pair2::first_type&
  1011. operator()(const _Pair2& __x) const
  1012. { return __x.first; }
  1013. #endif
  1014. };
  1015. template<typename _Pair>
  1016. struct _Select2nd
  1017. : public unary_function<_Pair, typename _Pair::second_type>
  1018. {
  1019. typename _Pair::second_type&
  1020. operator()(_Pair& __x) const
  1021. { return __x.second; }
  1022. const typename _Pair::second_type&
  1023. operator()(const _Pair& __x) const
  1024. { return __x.second; }
  1025. };
  1026. // 20.3.8 adaptors pointers members
  1027. /** @defgroup memory_adaptors Adaptors for pointers to members
  1028. * @ingroup functors
  1029. *
  1030. * There are a total of 8 = 2^3 function objects in this family.
  1031. * (1) Member functions taking no arguments vs member functions taking
  1032. * one argument.
  1033. * (2) Call through pointer vs call through reference.
  1034. * (3) Const vs non-const member function.
  1035. *
  1036. * All of this complexity is in the function objects themselves. You can
  1037. * ignore it by using the helper function mem_fun and mem_fun_ref,
  1038. * which create whichever type of adaptor is appropriate.
  1039. *
  1040. * @{
  1041. */
  1042. /// One of the @link memory_adaptors adaptors for member
  1043. /// pointers@endlink.
  1044. template<typename _Ret, typename _Tp>
  1045. class mem_fun_t : public unary_function<_Tp*, _Ret>
  1046. {
  1047. public:
  1048. explicit
  1049. mem_fun_t(_Ret (_Tp::*__pf)())
  1050. : _M_f(__pf) { }
  1051. _Ret
  1052. operator()(_Tp* __p) const
  1053. { return (__p->*_M_f)(); }
  1054. private:
  1055. _Ret (_Tp::*_M_f)();
  1056. };
  1057. /// One of the @link memory_adaptors adaptors for member
  1058. /// pointers@endlink.
  1059. template<typename _Ret, typename _Tp>
  1060. class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
  1061. {
  1062. public:
  1063. explicit
  1064. const_mem_fun_t(_Ret (_Tp::*__pf)() const)
  1065. : _M_f(__pf) { }
  1066. _Ret
  1067. operator()(const _Tp* __p) const
  1068. { return (__p->*_M_f)(); }
  1069. private:
  1070. _Ret (_Tp::*_M_f)() const;
  1071. };
  1072. /// One of the @link memory_adaptors adaptors for member
  1073. /// pointers@endlink.
  1074. template<typename _Ret, typename _Tp>
  1075. class mem_fun_ref_t : public unary_function<_Tp, _Ret>
  1076. {
  1077. public:
  1078. explicit
  1079. mem_fun_ref_t(_Ret (_Tp::*__pf)())
  1080. : _M_f(__pf) { }
  1081. _Ret
  1082. operator()(_Tp& __r) const
  1083. { return (__r.*_M_f)(); }
  1084. private:
  1085. _Ret (_Tp::*_M_f)();
  1086. };
  1087. /// One of the @link memory_adaptors adaptors for member
  1088. /// pointers@endlink.
  1089. template<typename _Ret, typename _Tp>
  1090. class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
  1091. {
  1092. public:
  1093. explicit
  1094. const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
  1095. : _M_f(__pf) { }
  1096. _Ret
  1097. operator()(const _Tp& __r) const
  1098. { return (__r.*_M_f)(); }
  1099. private:
  1100. _Ret (_Tp::*_M_f)() const;
  1101. };
  1102. /// One of the @link memory_adaptors adaptors for member
  1103. /// pointers@endlink.
  1104. template<typename _Ret, typename _Tp, typename _Arg>
  1105. class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
  1106. {
  1107. public:
  1108. explicit
  1109. mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
  1110. : _M_f(__pf) { }
  1111. _Ret
  1112. operator()(_Tp* __p, _Arg __x) const
  1113. { return (__p->*_M_f)(__x); }
  1114. private:
  1115. _Ret (_Tp::*_M_f)(_Arg);
  1116. };
  1117. /// One of the @link memory_adaptors adaptors for member
  1118. /// pointers@endlink.
  1119. template<typename _Ret, typename _Tp, typename _Arg>
  1120. class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
  1121. {
  1122. public:
  1123. explicit
  1124. const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
  1125. : _M_f(__pf) { }
  1126. _Ret
  1127. operator()(const _Tp* __p, _Arg __x) const
  1128. { return (__p->*_M_f)(__x); }
  1129. private:
  1130. _Ret (_Tp::*_M_f)(_Arg) const;
  1131. };
  1132. /// One of the @link memory_adaptors adaptors for member
  1133. /// pointers@endlink.
  1134. template<typename _Ret, typename _Tp, typename _Arg>
  1135. class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
  1136. {
  1137. public:
  1138. explicit
  1139. mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
  1140. : _M_f(__pf) { }
  1141. _Ret
  1142. operator()(_Tp& __r, _Arg __x) const
  1143. { return (__r.*_M_f)(__x); }
  1144. private:
  1145. _Ret (_Tp::*_M_f)(_Arg);
  1146. };
  1147. /// One of the @link memory_adaptors adaptors for member
  1148. /// pointers@endlink.
  1149. template<typename _Ret, typename _Tp, typename _Arg>
  1150. class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
  1151. {
  1152. public:
  1153. explicit
  1154. const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
  1155. : _M_f(__pf) { }
  1156. _Ret
  1157. operator()(const _Tp& __r, _Arg __x) const
  1158. { return (__r.*_M_f)(__x); }
  1159. private:
  1160. _Ret (_Tp::*_M_f)(_Arg) const;
  1161. };
  1162. // Mem_fun adaptor helper functions. There are only two:
  1163. // mem_fun and mem_fun_ref.
  1164. template<typename _Ret, typename _Tp>
  1165. inline mem_fun_t<_Ret, _Tp>
  1166. mem_fun(_Ret (_Tp::*__f)())
  1167. { return mem_fun_t<_Ret, _Tp>(__f); }
  1168. template<typename _Ret, typename _Tp>
  1169. inline const_mem_fun_t<_Ret, _Tp>
  1170. mem_fun(_Ret (_Tp::*__f)() const)
  1171. { return const_mem_fun_t<_Ret, _Tp>(__f); }
  1172. template<typename _Ret, typename _Tp>
  1173. inline mem_fun_ref_t<_Ret, _Tp>
  1174. mem_fun_ref(_Ret (_Tp::*__f)())
  1175. { return mem_fun_ref_t<_Ret, _Tp>(__f); }
  1176. template<typename _Ret, typename _Tp>
  1177. inline const_mem_fun_ref_t<_Ret, _Tp>
  1178. mem_fun_ref(_Ret (_Tp::*__f)() const)
  1179. { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
  1180. template<typename _Ret, typename _Tp, typename _Arg>
  1181. inline mem_fun1_t<_Ret, _Tp, _Arg>
  1182. mem_fun(_Ret (_Tp::*__f)(_Arg))
  1183. { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
  1184. template<typename _Ret, typename _Tp, typename _Arg>
  1185. inline const_mem_fun1_t<_Ret, _Tp, _Arg>
  1186. mem_fun(_Ret (_Tp::*__f)(_Arg) const)
  1187. { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
  1188. template<typename _Ret, typename _Tp, typename _Arg>
  1189. inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
  1190. mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
  1191. { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
  1192. template<typename _Ret, typename _Tp, typename _Arg>
  1193. inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
  1194. mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
  1195. { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
  1196. /** @} */
  1197. _GLIBCXX_END_NAMESPACE_VERSION
  1198. } // namespace
  1199. #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
  1200. # include <backward/binders.h>
  1201. #endif
  1202. #endif /* _STL_FUNCTION_H */