libstdc++
|
00001 // Functor implementations -*- C++ -*- 00002 00003 // Copyright (C) 2001-2014 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996-1998 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file bits/stl_function.h 00052 * This is an internal header file, included by other library headers. 00053 * Do not attempt to use it directly. @headername{functional} 00054 */ 00055 00056 #ifndef _STL_FUNCTION_H 00057 #define _STL_FUNCTION_H 1 00058 00059 #if __cplusplus > 201103L 00060 #include <bits/move.h> 00061 #endif 00062 00063 namespace std _GLIBCXX_VISIBILITY(default) 00064 { 00065 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00066 00067 // 20.3.1 base classes 00068 /** @defgroup functors Function Objects 00069 * @ingroup utilities 00070 * 00071 * Function objects, or @e functors, are objects with an @c operator() 00072 * defined and accessible. They can be passed as arguments to algorithm 00073 * templates and used in place of a function pointer. Not only is the 00074 * resulting expressiveness of the library increased, but the generated 00075 * code can be more efficient than what you might write by hand. When we 00076 * refer to @a functors, then, generally we include function pointers in 00077 * the description as well. 00078 * 00079 * Often, functors are only created as temporaries passed to algorithm 00080 * calls, rather than being created as named variables. 00081 * 00082 * Two examples taken from the standard itself follow. To perform a 00083 * by-element addition of two vectors @c a and @c b containing @c double, 00084 * and put the result in @c a, use 00085 * \code 00086 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>()); 00087 * \endcode 00088 * To negate every element in @c a, use 00089 * \code 00090 * transform(a.begin(), a.end(), a.begin(), negate<double>()); 00091 * \endcode 00092 * The addition and negation functions will be inlined directly. 00093 * 00094 * The standard functors are derived from structs named @c unary_function 00095 * and @c binary_function. These two classes contain nothing but typedefs, 00096 * to aid in generic (template) programming. If you write your own 00097 * functors, you might consider doing the same. 00098 * 00099 * @{ 00100 */ 00101 /** 00102 * This is one of the @link functors functor base classes@endlink. 00103 */ 00104 template<typename _Arg, typename _Result> 00105 struct unary_function 00106 { 00107 /// @c argument_type is the type of the argument 00108 typedef _Arg argument_type; 00109 00110 /// @c result_type is the return type 00111 typedef _Result result_type; 00112 }; 00113 00114 /** 00115 * This is one of the @link functors functor base classes@endlink. 00116 */ 00117 template<typename _Arg1, typename _Arg2, typename _Result> 00118 struct binary_function 00119 { 00120 /// @c first_argument_type is the type of the first argument 00121 typedef _Arg1 first_argument_type; 00122 00123 /// @c second_argument_type is the type of the second argument 00124 typedef _Arg2 second_argument_type; 00125 00126 /// @c result_type is the return type 00127 typedef _Result result_type; 00128 }; 00129 /** @} */ 00130 00131 // 20.3.2 arithmetic 00132 /** @defgroup arithmetic_functors Arithmetic Classes 00133 * @ingroup functors 00134 * 00135 * Because basic math often needs to be done during an algorithm, 00136 * the library provides functors for those operations. See the 00137 * documentation for @link functors the base classes@endlink 00138 * for examples of their use. 00139 * 00140 * @{ 00141 */ 00142 00143 #if __cplusplus > 201103L 00144 struct __is_transparent; // undefined 00145 00146 template<typename _Tp = void> 00147 struct plus; 00148 00149 template<typename _Tp = void> 00150 struct minus; 00151 00152 template<typename _Tp = void> 00153 struct multiplies; 00154 00155 template<typename _Tp = void> 00156 struct divides; 00157 00158 template<typename _Tp = void> 00159 struct modulus; 00160 00161 template<typename _Tp = void> 00162 struct negate; 00163 #endif 00164 00165 /// One of the @link arithmetic_functors math functors@endlink. 00166 template<typename _Tp> 00167 struct plus : public binary_function<_Tp, _Tp, _Tp> 00168 { 00169 _Tp 00170 operator()(const _Tp& __x, const _Tp& __y) const 00171 { return __x + __y; } 00172 }; 00173 00174 /// One of the @link arithmetic_functors math functors@endlink. 00175 template<typename _Tp> 00176 struct minus : public binary_function<_Tp, _Tp, _Tp> 00177 { 00178 _Tp 00179 operator()(const _Tp& __x, const _Tp& __y) const 00180 { return __x - __y; } 00181 }; 00182 00183 /// One of the @link arithmetic_functors math functors@endlink. 00184 template<typename _Tp> 00185 struct multiplies : public binary_function<_Tp, _Tp, _Tp> 00186 { 00187 _Tp 00188 operator()(const _Tp& __x, const _Tp& __y) const 00189 { return __x * __y; } 00190 }; 00191 00192 /// One of the @link arithmetic_functors math functors@endlink. 00193 template<typename _Tp> 00194 struct divides : public binary_function<_Tp, _Tp, _Tp> 00195 { 00196 _Tp 00197 operator()(const _Tp& __x, const _Tp& __y) const 00198 { return __x / __y; } 00199 }; 00200 00201 /// One of the @link arithmetic_functors math functors@endlink. 00202 template<typename _Tp> 00203 struct modulus : public binary_function<_Tp, _Tp, _Tp> 00204 { 00205 _Tp 00206 operator()(const _Tp& __x, const _Tp& __y) const 00207 { return __x % __y; } 00208 }; 00209 00210 /// One of the @link arithmetic_functors math functors@endlink. 00211 template<typename _Tp> 00212 struct negate : public unary_function<_Tp, _Tp> 00213 { 00214 _Tp 00215 operator()(const _Tp& __x) const 00216 { return -__x; } 00217 }; 00218 00219 #if __cplusplus > 201103L 00220 00221 #define __cpp_lib_transparent_operators 201210 00222 //#define __cpp_lib_generic_associative_lookup 201304 00223 00224 template<> 00225 struct plus<void> 00226 { 00227 template <typename _Tp, typename _Up> 00228 auto 00229 operator()(_Tp&& __t, _Up&& __u) const 00230 noexcept(noexcept(std::forward<_Tp>(__t) + std::forward<_Up>(__u))) 00231 -> decltype(std::forward<_Tp>(__t) + std::forward<_Up>(__u)) 00232 { return std::forward<_Tp>(__t) + std::forward<_Up>(__u); } 00233 00234 typedef __is_transparent is_transparent; 00235 }; 00236 00237 /// One of the @link arithmetic_functors math functors@endlink. 00238 template<> 00239 struct minus<void> 00240 { 00241 template <typename _Tp, typename _Up> 00242 auto 00243 operator()(_Tp&& __t, _Up&& __u) const 00244 noexcept(noexcept(std::forward<_Tp>(__t) - std::forward<_Up>(__u))) 00245 -> decltype(std::forward<_Tp>(__t) - std::forward<_Up>(__u)) 00246 { return std::forward<_Tp>(__t) - std::forward<_Up>(__u); } 00247 00248 typedef __is_transparent is_transparent; 00249 }; 00250 00251 /// One of the @link arithmetic_functors math functors@endlink. 00252 template<> 00253 struct multiplies<void> 00254 { 00255 template <typename _Tp, typename _Up> 00256 auto 00257 operator()(_Tp&& __t, _Up&& __u) const 00258 noexcept(noexcept(std::forward<_Tp>(__t) * std::forward<_Up>(__u))) 00259 -> decltype(std::forward<_Tp>(__t) * std::forward<_Up>(__u)) 00260 { return std::forward<_Tp>(__t) * std::forward<_Up>(__u); } 00261 00262 typedef __is_transparent is_transparent; 00263 }; 00264 00265 /// One of the @link arithmetic_functors math functors@endlink. 00266 template<> 00267 struct divides<void> 00268 { 00269 template <typename _Tp, typename _Up> 00270 auto 00271 operator()(_Tp&& __t, _Up&& __u) const 00272 noexcept(noexcept(std::forward<_Tp>(__t) / std::forward<_Up>(__u))) 00273 -> decltype(std::forward<_Tp>(__t) / std::forward<_Up>(__u)) 00274 { return std::forward<_Tp>(__t) / std::forward<_Up>(__u); } 00275 00276 typedef __is_transparent is_transparent; 00277 }; 00278 00279 /// One of the @link arithmetic_functors math functors@endlink. 00280 template<> 00281 struct modulus<void> 00282 { 00283 template <typename _Tp, typename _Up> 00284 auto 00285 operator()(_Tp&& __t, _Up&& __u) const 00286 noexcept(noexcept(std::forward<_Tp>(__t) % std::forward<_Up>(__u))) 00287 -> decltype(std::forward<_Tp>(__t) % std::forward<_Up>(__u)) 00288 { return std::forward<_Tp>(__t) % std::forward<_Up>(__u); } 00289 00290 typedef __is_transparent is_transparent; 00291 }; 00292 00293 /// One of the @link arithmetic_functors math functors@endlink. 00294 template<> 00295 struct negate<void> 00296 { 00297 template <typename _Tp> 00298 auto 00299 operator()(_Tp&& __t) const 00300 noexcept(noexcept(-std::forward<_Tp>(__t))) 00301 -> decltype(-std::forward<_Tp>(__t)) 00302 { return -std::forward<_Tp>(__t); } 00303 00304 typedef __is_transparent is_transparent; 00305 }; 00306 #endif 00307 /** @} */ 00308 00309 // 20.3.3 comparisons 00310 /** @defgroup comparison_functors Comparison Classes 00311 * @ingroup functors 00312 * 00313 * The library provides six wrapper functors for all the basic comparisons 00314 * in C++, like @c <. 00315 * 00316 * @{ 00317 */ 00318 #if __cplusplus > 201103L 00319 template<typename _Tp = void> 00320 struct equal_to; 00321 00322 template<typename _Tp = void> 00323 struct not_equal_to; 00324 00325 template<typename _Tp = void> 00326 struct greater; 00327 00328 template<typename _Tp = void> 00329 struct less; 00330 00331 template<typename _Tp = void> 00332 struct greater_equal; 00333 00334 template<typename _Tp = void> 00335 struct less_equal; 00336 #endif 00337 00338 /// One of the @link comparison_functors comparison functors@endlink. 00339 template<typename _Tp> 00340 struct equal_to : public binary_function<_Tp, _Tp, bool> 00341 { 00342 bool 00343 operator()(const _Tp& __x, const _Tp& __y) const 00344 { return __x == __y; } 00345 }; 00346 00347 /// One of the @link comparison_functors comparison functors@endlink. 00348 template<typename _Tp> 00349 struct not_equal_to : public binary_function<_Tp, _Tp, bool> 00350 { 00351 bool 00352 operator()(const _Tp& __x, const _Tp& __y) const 00353 { return __x != __y; } 00354 }; 00355 00356 /// One of the @link comparison_functors comparison functors@endlink. 00357 template<typename _Tp> 00358 struct greater : public binary_function<_Tp, _Tp, bool> 00359 { 00360 bool 00361 operator()(const _Tp& __x, const _Tp& __y) const 00362 { return __x > __y; } 00363 }; 00364 00365 /// One of the @link comparison_functors comparison functors@endlink. 00366 template<typename _Tp> 00367 struct less : public binary_function<_Tp, _Tp, bool> 00368 { 00369 bool 00370 operator()(const _Tp& __x, const _Tp& __y) const 00371 { return __x < __y; } 00372 }; 00373 00374 /// One of the @link comparison_functors comparison functors@endlink. 00375 template<typename _Tp> 00376 struct greater_equal : public binary_function<_Tp, _Tp, bool> 00377 { 00378 bool 00379 operator()(const _Tp& __x, const _Tp& __y) const 00380 { return __x >= __y; } 00381 }; 00382 00383 /// One of the @link comparison_functors comparison functors@endlink. 00384 template<typename _Tp> 00385 struct less_equal : public binary_function<_Tp, _Tp, bool> 00386 { 00387 bool 00388 operator()(const _Tp& __x, const _Tp& __y) const 00389 { return __x <= __y; } 00390 }; 00391 00392 #if __cplusplus > 201103L 00393 /// One of the @link comparison_functors comparison functors@endlink. 00394 template<> 00395 struct equal_to<void> 00396 { 00397 template <typename _Tp, typename _Up> 00398 auto 00399 operator()(_Tp&& __t, _Up&& __u) const 00400 noexcept(noexcept(std::forward<_Tp>(__t) == std::forward<_Up>(__u))) 00401 -> decltype(std::forward<_Tp>(__t) == std::forward<_Up>(__u)) 00402 { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); } 00403 00404 typedef __is_transparent is_transparent; 00405 }; 00406 00407 /// One of the @link comparison_functors comparison functors@endlink. 00408 template<> 00409 struct not_equal_to<void> 00410 { 00411 template <typename _Tp, typename _Up> 00412 auto 00413 operator()(_Tp&& __t, _Up&& __u) const 00414 noexcept(noexcept(std::forward<_Tp>(__t) != std::forward<_Up>(__u))) 00415 -> decltype(std::forward<_Tp>(__t) != std::forward<_Up>(__u)) 00416 { return std::forward<_Tp>(__t) != std::forward<_Up>(__u); } 00417 00418 typedef __is_transparent is_transparent; 00419 }; 00420 00421 /// One of the @link comparison_functors comparison functors@endlink. 00422 template<> 00423 struct greater<void> 00424 { 00425 template <typename _Tp, typename _Up> 00426 auto 00427 operator()(_Tp&& __t, _Up&& __u) const 00428 noexcept(noexcept(std::forward<_Tp>(__t) > std::forward<_Up>(__u))) 00429 -> decltype(std::forward<_Tp>(__t) > std::forward<_Up>(__u)) 00430 { return std::forward<_Tp>(__t) > std::forward<_Up>(__u); } 00431 00432 typedef __is_transparent is_transparent; 00433 }; 00434 00435 /// One of the @link comparison_functors comparison functors@endlink. 00436 template<> 00437 struct less<void> 00438 { 00439 template <typename _Tp, typename _Up> 00440 auto 00441 operator()(_Tp&& __t, _Up&& __u) const 00442 noexcept(noexcept(std::forward<_Tp>(__t) < std::forward<_Up>(__u))) 00443 -> decltype(std::forward<_Tp>(__t) < std::forward<_Up>(__u)) 00444 { return std::forward<_Tp>(__t) < std::forward<_Up>(__u); } 00445 00446 typedef __is_transparent is_transparent; 00447 }; 00448 00449 /// One of the @link comparison_functors comparison functors@endlink. 00450 template<> 00451 struct greater_equal<void> 00452 { 00453 template <typename _Tp, typename _Up> 00454 auto 00455 operator()(_Tp&& __t, _Up&& __u) const 00456 noexcept(noexcept(std::forward<_Tp>(__t) >= std::forward<_Up>(__u))) 00457 -> decltype(std::forward<_Tp>(__t) >= std::forward<_Up>(__u)) 00458 { return std::forward<_Tp>(__t) >= std::forward<_Up>(__u); } 00459 00460 typedef __is_transparent is_transparent; 00461 }; 00462 00463 /// One of the @link comparison_functors comparison functors@endlink. 00464 template<> 00465 struct less_equal<void> 00466 { 00467 template <typename _Tp, typename _Up> 00468 auto 00469 operator()(_Tp&& __t, _Up&& __u) const 00470 noexcept(noexcept(std::forward<_Tp>(__t) <= std::forward<_Up>(__u))) 00471 -> decltype(std::forward<_Tp>(__t) <= std::forward<_Up>(__u)) 00472 { return std::forward<_Tp>(__t) <= std::forward<_Up>(__u); } 00473 00474 typedef __is_transparent is_transparent; 00475 }; 00476 #endif 00477 /** @} */ 00478 00479 // 20.3.4 logical operations 00480 /** @defgroup logical_functors Boolean Operations Classes 00481 * @ingroup functors 00482 * 00483 * Here are wrapper functors for Boolean operations: @c &&, @c ||, 00484 * and @c !. 00485 * 00486 * @{ 00487 */ 00488 #if __cplusplus > 201103L 00489 template<typename _Tp = void> 00490 struct logical_and; 00491 00492 template<typename _Tp = void> 00493 struct logical_or; 00494 00495 template<typename _Tp = void> 00496 struct logical_not; 00497 #endif 00498 00499 /// One of the @link logical_functors Boolean operations functors@endlink. 00500 template<typename _Tp> 00501 struct logical_and : public binary_function<_Tp, _Tp, bool> 00502 { 00503 bool 00504 operator()(const _Tp& __x, const _Tp& __y) const 00505 { return __x && __y; } 00506 }; 00507 00508 /// One of the @link logical_functors Boolean operations functors@endlink. 00509 template<typename _Tp> 00510 struct logical_or : public binary_function<_Tp, _Tp, bool> 00511 { 00512 bool 00513 operator()(const _Tp& __x, const _Tp& __y) const 00514 { return __x || __y; } 00515 }; 00516 00517 /// One of the @link logical_functors Boolean operations functors@endlink. 00518 template<typename _Tp> 00519 struct logical_not : public unary_function<_Tp, bool> 00520 { 00521 bool 00522 operator()(const _Tp& __x) const 00523 { return !__x; } 00524 }; 00525 00526 #if __cplusplus > 201103L 00527 /// One of the @link logical_functors Boolean operations functors@endlink. 00528 template<> 00529 struct logical_and<void> 00530 { 00531 template <typename _Tp, typename _Up> 00532 auto 00533 operator()(_Tp&& __t, _Up&& __u) const 00534 noexcept(noexcept(std::forward<_Tp>(__t) && std::forward<_Up>(__u))) 00535 -> decltype(std::forward<_Tp>(__t) && std::forward<_Up>(__u)) 00536 { return std::forward<_Tp>(__t) && std::forward<_Up>(__u); } 00537 00538 typedef __is_transparent is_transparent; 00539 }; 00540 00541 /// One of the @link logical_functors Boolean operations functors@endlink. 00542 template<> 00543 struct logical_or<void> 00544 { 00545 template <typename _Tp, typename _Up> 00546 auto 00547 operator()(_Tp&& __t, _Up&& __u) const 00548 noexcept(noexcept(std::forward<_Tp>(__t) || std::forward<_Up>(__u))) 00549 -> decltype(std::forward<_Tp>(__t) || std::forward<_Up>(__u)) 00550 { return std::forward<_Tp>(__t) || std::forward<_Up>(__u); } 00551 00552 typedef __is_transparent is_transparent; 00553 }; 00554 00555 /// One of the @link logical_functors Boolean operations functors@endlink. 00556 template<> 00557 struct logical_not<void> 00558 { 00559 template <typename _Tp> 00560 auto 00561 operator()(_Tp&& __t) const 00562 noexcept(noexcept(!std::forward<_Tp>(__t))) 00563 -> decltype(!std::forward<_Tp>(__t)) 00564 { return !std::forward<_Tp>(__t); } 00565 00566 typedef __is_transparent is_transparent; 00567 }; 00568 #endif 00569 /** @} */ 00570 00571 #if __cplusplus > 201103L 00572 template<typename _Tp = void> 00573 struct bit_and; 00574 00575 template<typename _Tp = void> 00576 struct bit_or; 00577 00578 template<typename _Tp = void> 00579 struct bit_xor; 00580 00581 template<typename _Tp = void> 00582 struct bit_not; 00583 #endif 00584 00585 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00586 // DR 660. Missing Bitwise Operations. 00587 template<typename _Tp> 00588 struct bit_and : public binary_function<_Tp, _Tp, _Tp> 00589 { 00590 _Tp 00591 operator()(const _Tp& __x, const _Tp& __y) const 00592 { return __x & __y; } 00593 }; 00594 00595 template<typename _Tp> 00596 struct bit_or : public binary_function<_Tp, _Tp, _Tp> 00597 { 00598 _Tp 00599 operator()(const _Tp& __x, const _Tp& __y) const 00600 { return __x | __y; } 00601 }; 00602 00603 template<typename _Tp> 00604 struct bit_xor : public binary_function<_Tp, _Tp, _Tp> 00605 { 00606 _Tp 00607 operator()(const _Tp& __x, const _Tp& __y) const 00608 { return __x ^ __y; } 00609 }; 00610 00611 template<typename _Tp> 00612 struct bit_not : public unary_function<_Tp, _Tp> 00613 { 00614 _Tp 00615 operator()(const _Tp& __x) const 00616 { return ~__x; } 00617 }; 00618 00619 #if __cplusplus > 201103L 00620 template <> 00621 struct bit_and<void> 00622 { 00623 template <typename _Tp, typename _Up> 00624 auto 00625 operator()(_Tp&& __t, _Up&& __u) const 00626 noexcept(noexcept(std::forward<_Tp>(__t) & std::forward<_Up>(__u))) 00627 -> decltype(std::forward<_Tp>(__t) & std::forward<_Up>(__u)) 00628 { return std::forward<_Tp>(__t) & std::forward<_Up>(__u); } 00629 00630 typedef __is_transparent is_transparent; 00631 }; 00632 00633 template <> 00634 struct bit_or<void> 00635 { 00636 template <typename _Tp, typename _Up> 00637 auto 00638 operator()(_Tp&& __t, _Up&& __u) const 00639 noexcept(noexcept(std::forward<_Tp>(__t) | std::forward<_Up>(__u))) 00640 -> decltype(std::forward<_Tp>(__t) | std::forward<_Up>(__u)) 00641 { return std::forward<_Tp>(__t) | std::forward<_Up>(__u); } 00642 00643 typedef __is_transparent is_transparent; 00644 }; 00645 00646 template <> 00647 struct bit_xor<void> 00648 { 00649 template <typename _Tp, typename _Up> 00650 auto 00651 operator()(_Tp&& __t, _Up&& __u) const 00652 noexcept(noexcept(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u))) 00653 -> decltype(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u)) 00654 { return std::forward<_Tp>(__t) ^ std::forward<_Up>(__u); } 00655 00656 typedef __is_transparent is_transparent; 00657 }; 00658 00659 template <> 00660 struct bit_not<void> 00661 { 00662 template <typename _Tp> 00663 auto 00664 operator()(_Tp&& __t) const 00665 noexcept(noexcept(~std::forward<_Tp>(__t))) 00666 -> decltype(~std::forward<_Tp>(__t)) 00667 { return ~std::forward<_Tp>(__t); } 00668 00669 typedef __is_transparent is_transparent; 00670 }; 00671 #endif 00672 00673 // 20.3.5 negators 00674 /** @defgroup negators Negators 00675 * @ingroup functors 00676 * 00677 * The functions @c not1 and @c not2 each take a predicate functor 00678 * and return an instance of @c unary_negate or 00679 * @c binary_negate, respectively. These classes are functors whose 00680 * @c operator() performs the stored predicate function and then returns 00681 * the negation of the result. 00682 * 00683 * For example, given a vector of integers and a trivial predicate, 00684 * \code 00685 * struct IntGreaterThanThree 00686 * : public std::unary_function<int, bool> 00687 * { 00688 * bool operator() (int x) { return x > 3; } 00689 * }; 00690 * 00691 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree())); 00692 * \endcode 00693 * The call to @c find_if will locate the first index (i) of @c v for which 00694 * <code>!(v[i] > 3)</code> is true. 00695 * 00696 * The not1/unary_negate combination works on predicates taking a single 00697 * argument. The not2/binary_negate combination works on predicates which 00698 * take two arguments. 00699 * 00700 * @{ 00701 */ 00702 /// One of the @link negators negation functors@endlink. 00703 template<typename _Predicate> 00704 class unary_negate 00705 : public unary_function<typename _Predicate::argument_type, bool> 00706 { 00707 protected: 00708 _Predicate _M_pred; 00709 00710 public: 00711 explicit 00712 unary_negate(const _Predicate& __x) : _M_pred(__x) { } 00713 00714 bool 00715 operator()(const typename _Predicate::argument_type& __x) const 00716 { return !_M_pred(__x); } 00717 }; 00718 00719 /// One of the @link negators negation functors@endlink. 00720 template<typename _Predicate> 00721 inline unary_negate<_Predicate> 00722 not1(const _Predicate& __pred) 00723 { return unary_negate<_Predicate>(__pred); } 00724 00725 /// One of the @link negators negation functors@endlink. 00726 template<typename _Predicate> 00727 class binary_negate 00728 : public binary_function<typename _Predicate::first_argument_type, 00729 typename _Predicate::second_argument_type, bool> 00730 { 00731 protected: 00732 _Predicate _M_pred; 00733 00734 public: 00735 explicit 00736 binary_negate(const _Predicate& __x) : _M_pred(__x) { } 00737 00738 bool 00739 operator()(const typename _Predicate::first_argument_type& __x, 00740 const typename _Predicate::second_argument_type& __y) const 00741 { return !_M_pred(__x, __y); } 00742 }; 00743 00744 /// One of the @link negators negation functors@endlink. 00745 template<typename _Predicate> 00746 inline binary_negate<_Predicate> 00747 not2(const _Predicate& __pred) 00748 { return binary_negate<_Predicate>(__pred); } 00749 /** @} */ 00750 00751 // 20.3.7 adaptors pointers functions 00752 /** @defgroup pointer_adaptors Adaptors for pointers to functions 00753 * @ingroup functors 00754 * 00755 * The advantage of function objects over pointers to functions is that 00756 * the objects in the standard library declare nested typedefs describing 00757 * their argument and result types with uniform names (e.g., @c result_type 00758 * from the base classes @c unary_function and @c binary_function). 00759 * Sometimes those typedefs are required, not just optional. 00760 * 00761 * Adaptors are provided to turn pointers to unary (single-argument) and 00762 * binary (double-argument) functions into function objects. The 00763 * long-winded functor @c pointer_to_unary_function is constructed with a 00764 * function pointer @c f, and its @c operator() called with argument @c x 00765 * returns @c f(x). The functor @c pointer_to_binary_function does the same 00766 * thing, but with a double-argument @c f and @c operator(). 00767 * 00768 * The function @c ptr_fun takes a pointer-to-function @c f and constructs 00769 * an instance of the appropriate functor. 00770 * 00771 * @{ 00772 */ 00773 /// One of the @link pointer_adaptors adaptors for function pointers@endlink. 00774 template<typename _Arg, typename _Result> 00775 class pointer_to_unary_function : public unary_function<_Arg, _Result> 00776 { 00777 protected: 00778 _Result (*_M_ptr)(_Arg); 00779 00780 public: 00781 pointer_to_unary_function() { } 00782 00783 explicit 00784 pointer_to_unary_function(_Result (*__x)(_Arg)) 00785 : _M_ptr(__x) { } 00786 00787 _Result 00788 operator()(_Arg __x) const 00789 { return _M_ptr(__x); } 00790 }; 00791 00792 /// One of the @link pointer_adaptors adaptors for function pointers@endlink. 00793 template<typename _Arg, typename _Result> 00794 inline pointer_to_unary_function<_Arg, _Result> 00795 ptr_fun(_Result (*__x)(_Arg)) 00796 { return pointer_to_unary_function<_Arg, _Result>(__x); } 00797 00798 /// One of the @link pointer_adaptors adaptors for function pointers@endlink. 00799 template<typename _Arg1, typename _Arg2, typename _Result> 00800 class pointer_to_binary_function 00801 : public binary_function<_Arg1, _Arg2, _Result> 00802 { 00803 protected: 00804 _Result (*_M_ptr)(_Arg1, _Arg2); 00805 00806 public: 00807 pointer_to_binary_function() { } 00808 00809 explicit 00810 pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2)) 00811 : _M_ptr(__x) { } 00812 00813 _Result 00814 operator()(_Arg1 __x, _Arg2 __y) const 00815 { return _M_ptr(__x, __y); } 00816 }; 00817 00818 /// One of the @link pointer_adaptors adaptors for function pointers@endlink. 00819 template<typename _Arg1, typename _Arg2, typename _Result> 00820 inline pointer_to_binary_function<_Arg1, _Arg2, _Result> 00821 ptr_fun(_Result (*__x)(_Arg1, _Arg2)) 00822 { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); } 00823 /** @} */ 00824 00825 template<typename _Tp> 00826 struct _Identity 00827 : public unary_function<_Tp,_Tp> 00828 { 00829 _Tp& 00830 operator()(_Tp& __x) const 00831 { return __x; } 00832 00833 const _Tp& 00834 operator()(const _Tp& __x) const 00835 { return __x; } 00836 }; 00837 00838 template<typename _Pair> 00839 struct _Select1st 00840 : public unary_function<_Pair, typename _Pair::first_type> 00841 { 00842 typename _Pair::first_type& 00843 operator()(_Pair& __x) const 00844 { return __x.first; } 00845 00846 const typename _Pair::first_type& 00847 operator()(const _Pair& __x) const 00848 { return __x.first; } 00849 00850 #if __cplusplus >= 201103L 00851 template<typename _Pair2> 00852 typename _Pair2::first_type& 00853 operator()(_Pair2& __x) const 00854 { return __x.first; } 00855 00856 template<typename _Pair2> 00857 const typename _Pair2::first_type& 00858 operator()(const _Pair2& __x) const 00859 { return __x.first; } 00860 #endif 00861 }; 00862 00863 template<typename _Pair> 00864 struct _Select2nd 00865 : public unary_function<_Pair, typename _Pair::second_type> 00866 { 00867 typename _Pair::second_type& 00868 operator()(_Pair& __x) const 00869 { return __x.second; } 00870 00871 const typename _Pair::second_type& 00872 operator()(const _Pair& __x) const 00873 { return __x.second; } 00874 }; 00875 00876 // 20.3.8 adaptors pointers members 00877 /** @defgroup memory_adaptors Adaptors for pointers to members 00878 * @ingroup functors 00879 * 00880 * There are a total of 8 = 2^3 function objects in this family. 00881 * (1) Member functions taking no arguments vs member functions taking 00882 * one argument. 00883 * (2) Call through pointer vs call through reference. 00884 * (3) Const vs non-const member function. 00885 * 00886 * All of this complexity is in the function objects themselves. You can 00887 * ignore it by using the helper function mem_fun and mem_fun_ref, 00888 * which create whichever type of adaptor is appropriate. 00889 * 00890 * @{ 00891 */ 00892 /// One of the @link memory_adaptors adaptors for member 00893 /// pointers@endlink. 00894 template<typename _Ret, typename _Tp> 00895 class mem_fun_t : public unary_function<_Tp*, _Ret> 00896 { 00897 public: 00898 explicit 00899 mem_fun_t(_Ret (_Tp::*__pf)()) 00900 : _M_f(__pf) { } 00901 00902 _Ret 00903 operator()(_Tp* __p) const 00904 { return (__p->*_M_f)(); } 00905 00906 private: 00907 _Ret (_Tp::*_M_f)(); 00908 }; 00909 00910 /// One of the @link memory_adaptors adaptors for member 00911 /// pointers@endlink. 00912 template<typename _Ret, typename _Tp> 00913 class const_mem_fun_t : public unary_function<const _Tp*, _Ret> 00914 { 00915 public: 00916 explicit 00917 const_mem_fun_t(_Ret (_Tp::*__pf)() const) 00918 : _M_f(__pf) { } 00919 00920 _Ret 00921 operator()(const _Tp* __p) const 00922 { return (__p->*_M_f)(); } 00923 00924 private: 00925 _Ret (_Tp::*_M_f)() const; 00926 }; 00927 00928 /// One of the @link memory_adaptors adaptors for member 00929 /// pointers@endlink. 00930 template<typename _Ret, typename _Tp> 00931 class mem_fun_ref_t : public unary_function<_Tp, _Ret> 00932 { 00933 public: 00934 explicit 00935 mem_fun_ref_t(_Ret (_Tp::*__pf)()) 00936 : _M_f(__pf) { } 00937 00938 _Ret 00939 operator()(_Tp& __r) const 00940 { return (__r.*_M_f)(); } 00941 00942 private: 00943 _Ret (_Tp::*_M_f)(); 00944 }; 00945 00946 /// One of the @link memory_adaptors adaptors for member 00947 /// pointers@endlink. 00948 template<typename _Ret, typename _Tp> 00949 class const_mem_fun_ref_t : public unary_function<_Tp, _Ret> 00950 { 00951 public: 00952 explicit 00953 const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) 00954 : _M_f(__pf) { } 00955 00956 _Ret 00957 operator()(const _Tp& __r) const 00958 { return (__r.*_M_f)(); } 00959 00960 private: 00961 _Ret (_Tp::*_M_f)() const; 00962 }; 00963 00964 /// One of the @link memory_adaptors adaptors for member 00965 /// pointers@endlink. 00966 template<typename _Ret, typename _Tp, typename _Arg> 00967 class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret> 00968 { 00969 public: 00970 explicit 00971 mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) 00972 : _M_f(__pf) { } 00973 00974 _Ret 00975 operator()(_Tp* __p, _Arg __x) const 00976 { return (__p->*_M_f)(__x); } 00977 00978 private: 00979 _Ret (_Tp::*_M_f)(_Arg); 00980 }; 00981 00982 /// One of the @link memory_adaptors adaptors for member 00983 /// pointers@endlink. 00984 template<typename _Ret, typename _Tp, typename _Arg> 00985 class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret> 00986 { 00987 public: 00988 explicit 00989 const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) 00990 : _M_f(__pf) { } 00991 00992 _Ret 00993 operator()(const _Tp* __p, _Arg __x) const 00994 { return (__p->*_M_f)(__x); } 00995 00996 private: 00997 _Ret (_Tp::*_M_f)(_Arg) const; 00998 }; 00999 01000 /// One of the @link memory_adaptors adaptors for member 01001 /// pointers@endlink. 01002 template<typename _Ret, typename _Tp, typename _Arg> 01003 class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret> 01004 { 01005 public: 01006 explicit 01007 mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) 01008 : _M_f(__pf) { } 01009 01010 _Ret 01011 operator()(_Tp& __r, _Arg __x) const 01012 { return (__r.*_M_f)(__x); } 01013 01014 private: 01015 _Ret (_Tp::*_M_f)(_Arg); 01016 }; 01017 01018 /// One of the @link memory_adaptors adaptors for member 01019 /// pointers@endlink. 01020 template<typename _Ret, typename _Tp, typename _Arg> 01021 class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret> 01022 { 01023 public: 01024 explicit 01025 const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) 01026 : _M_f(__pf) { } 01027 01028 _Ret 01029 operator()(const _Tp& __r, _Arg __x) const 01030 { return (__r.*_M_f)(__x); } 01031 01032 private: 01033 _Ret (_Tp::*_M_f)(_Arg) const; 01034 }; 01035 01036 // Mem_fun adaptor helper functions. There are only two: 01037 // mem_fun and mem_fun_ref. 01038 template<typename _Ret, typename _Tp> 01039 inline mem_fun_t<_Ret, _Tp> 01040 mem_fun(_Ret (_Tp::*__f)()) 01041 { return mem_fun_t<_Ret, _Tp>(__f); } 01042 01043 template<typename _Ret, typename _Tp> 01044 inline const_mem_fun_t<_Ret, _Tp> 01045 mem_fun(_Ret (_Tp::*__f)() const) 01046 { return const_mem_fun_t<_Ret, _Tp>(__f); } 01047 01048 template<typename _Ret, typename _Tp> 01049 inline mem_fun_ref_t<_Ret, _Tp> 01050 mem_fun_ref(_Ret (_Tp::*__f)()) 01051 { return mem_fun_ref_t<_Ret, _Tp>(__f); } 01052 01053 template<typename _Ret, typename _Tp> 01054 inline const_mem_fun_ref_t<_Ret, _Tp> 01055 mem_fun_ref(_Ret (_Tp::*__f)() const) 01056 { return const_mem_fun_ref_t<_Ret, _Tp>(__f); } 01057 01058 template<typename _Ret, typename _Tp, typename _Arg> 01059 inline mem_fun1_t<_Ret, _Tp, _Arg> 01060 mem_fun(_Ret (_Tp::*__f)(_Arg)) 01061 { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 01062 01063 template<typename _Ret, typename _Tp, typename _Arg> 01064 inline const_mem_fun1_t<_Ret, _Tp, _Arg> 01065 mem_fun(_Ret (_Tp::*__f)(_Arg) const) 01066 { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 01067 01068 template<typename _Ret, typename _Tp, typename _Arg> 01069 inline mem_fun1_ref_t<_Ret, _Tp, _Arg> 01070 mem_fun_ref(_Ret (_Tp::*__f)(_Arg)) 01071 { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 01072 01073 template<typename _Ret, typename _Tp, typename _Arg> 01074 inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg> 01075 mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const) 01076 { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 01077 01078 /** @} */ 01079 01080 _GLIBCXX_END_NAMESPACE_VERSION 01081 } // namespace 01082 01083 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED 01084 # include <backward/binders.h> 01085 #endif 01086 01087 #endif /* _STL_FUNCTION_H */