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