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