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 template<> 00221 struct plus<void> 00222 { 00223 template <typename _Tp, typename _Up> 00224 auto 00225 operator()(_Tp&& __t, _Up&& __u) const 00226 noexcept(noexcept(std::forward<_Tp>(__t) + std::forward<_Up>(__u))) 00227 -> decltype(std::forward<_Tp>(__t) + std::forward<_Up>(__u)) 00228 { return std::forward<_Tp>(__t) + std::forward<_Up>(__u); } 00229 00230 typedef __is_transparent is_transparent; 00231 }; 00232 00233 /// One of the @link arithmetic_functors math functors@endlink. 00234 template<> 00235 struct minus<void> 00236 { 00237 template <typename _Tp, typename _Up> 00238 auto 00239 operator()(_Tp&& __t, _Up&& __u) const 00240 noexcept(noexcept(std::forward<_Tp>(__t) - std::forward<_Up>(__u))) 00241 -> decltype(std::forward<_Tp>(__t) - std::forward<_Up>(__u)) 00242 { return std::forward<_Tp>(__t) - std::forward<_Up>(__u); } 00243 00244 typedef __is_transparent is_transparent; 00245 }; 00246 00247 /// One of the @link arithmetic_functors math functors@endlink. 00248 template<> 00249 struct multiplies<void> 00250 { 00251 template <typename _Tp, typename _Up> 00252 auto 00253 operator()(_Tp&& __t, _Up&& __u) const 00254 noexcept(noexcept(std::forward<_Tp>(__t) * std::forward<_Up>(__u))) 00255 -> decltype(std::forward<_Tp>(__t) * std::forward<_Up>(__u)) 00256 { return std::forward<_Tp>(__t) * std::forward<_Up>(__u); } 00257 00258 typedef __is_transparent is_transparent; 00259 }; 00260 00261 /// One of the @link arithmetic_functors math functors@endlink. 00262 template<> 00263 struct divides<void> 00264 { 00265 template <typename _Tp, typename _Up> 00266 auto 00267 operator()(_Tp&& __t, _Up&& __u) const 00268 noexcept(noexcept(std::forward<_Tp>(__t) / std::forward<_Up>(__u))) 00269 -> decltype(std::forward<_Tp>(__t) / std::forward<_Up>(__u)) 00270 { return std::forward<_Tp>(__t) / std::forward<_Up>(__u); } 00271 00272 typedef __is_transparent is_transparent; 00273 }; 00274 00275 /// One of the @link arithmetic_functors math functors@endlink. 00276 template<> 00277 struct modulus<void> 00278 { 00279 template <typename _Tp, typename _Up> 00280 auto 00281 operator()(_Tp&& __t, _Up&& __u) const 00282 noexcept(noexcept(std::forward<_Tp>(__t) % std::forward<_Up>(__u))) 00283 -> decltype(std::forward<_Tp>(__t) % std::forward<_Up>(__u)) 00284 { return std::forward<_Tp>(__t) % std::forward<_Up>(__u); } 00285 00286 typedef __is_transparent is_transparent; 00287 }; 00288 00289 /// One of the @link arithmetic_functors math functors@endlink. 00290 template<> 00291 struct negate<void> 00292 { 00293 template <typename _Tp> 00294 auto 00295 operator()(_Tp&& __t) const 00296 noexcept(noexcept(-std::forward<_Tp>(__t))) 00297 -> decltype(-std::forward<_Tp>(__t)) 00298 { return -std::forward<_Tp>(__t); } 00299 00300 typedef __is_transparent is_transparent; 00301 }; 00302 #endif 00303 /** @} */ 00304 00305 // 20.3.3 comparisons 00306 /** @defgroup comparison_functors Comparison Classes 00307 * @ingroup functors 00308 * 00309 * The library provides six wrapper functors for all the basic comparisons 00310 * in C++, like @c <. 00311 * 00312 * @{ 00313 */ 00314 #if __cplusplus > 201103L 00315 template<typename _Tp = void> 00316 struct equal_to; 00317 00318 template<typename _Tp = void> 00319 struct not_equal_to; 00320 00321 template<typename _Tp = void> 00322 struct greater; 00323 00324 template<typename _Tp = void> 00325 struct less; 00326 00327 template<typename _Tp = void> 00328 struct greater_equal; 00329 00330 template<typename _Tp = void> 00331 struct less_equal; 00332 #endif 00333 00334 /// One of the @link comparison_functors comparison functors@endlink. 00335 template<typename _Tp> 00336 struct equal_to : public binary_function<_Tp, _Tp, bool> 00337 { 00338 bool 00339 operator()(const _Tp& __x, const _Tp& __y) const 00340 { return __x == __y; } 00341 }; 00342 00343 /// One of the @link comparison_functors comparison functors@endlink. 00344 template<typename _Tp> 00345 struct not_equal_to : public binary_function<_Tp, _Tp, bool> 00346 { 00347 bool 00348 operator()(const _Tp& __x, const _Tp& __y) const 00349 { return __x != __y; } 00350 }; 00351 00352 /// One of the @link comparison_functors comparison functors@endlink. 00353 template<typename _Tp> 00354 struct greater : public binary_function<_Tp, _Tp, bool> 00355 { 00356 bool 00357 operator()(const _Tp& __x, const _Tp& __y) const 00358 { return __x > __y; } 00359 }; 00360 00361 /// One of the @link comparison_functors comparison functors@endlink. 00362 template<typename _Tp> 00363 struct less : public binary_function<_Tp, _Tp, bool> 00364 { 00365 bool 00366 operator()(const _Tp& __x, const _Tp& __y) const 00367 { return __x < __y; } 00368 }; 00369 00370 /// One of the @link comparison_functors comparison functors@endlink. 00371 template<typename _Tp> 00372 struct greater_equal : public binary_function<_Tp, _Tp, bool> 00373 { 00374 bool 00375 operator()(const _Tp& __x, const _Tp& __y) const 00376 { return __x >= __y; } 00377 }; 00378 00379 /// One of the @link comparison_functors comparison functors@endlink. 00380 template<typename _Tp> 00381 struct less_equal : public binary_function<_Tp, _Tp, bool> 00382 { 00383 bool 00384 operator()(const _Tp& __x, const _Tp& __y) const 00385 { return __x <= __y; } 00386 }; 00387 00388 #if __cplusplus > 201103L 00389 /// One of the @link comparison_functors comparison functors@endlink. 00390 template<> 00391 struct equal_to<void> 00392 { 00393 template <typename _Tp, typename _Up> 00394 auto 00395 operator()(_Tp&& __t, _Up&& __u) const 00396 noexcept(noexcept(std::forward<_Tp>(__t) == std::forward<_Up>(__u))) 00397 -> decltype(std::forward<_Tp>(__t) == std::forward<_Up>(__u)) 00398 { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); } 00399 00400 typedef __is_transparent is_transparent; 00401 }; 00402 00403 /// One of the @link comparison_functors comparison functors@endlink. 00404 template<> 00405 struct not_equal_to<void> 00406 { 00407 template <typename _Tp, typename _Up> 00408 auto 00409 operator()(_Tp&& __t, _Up&& __u) const 00410 noexcept(noexcept(std::forward<_Tp>(__t) != std::forward<_Up>(__u))) 00411 -> decltype(std::forward<_Tp>(__t) != std::forward<_Up>(__u)) 00412 { return std::forward<_Tp>(__t) != std::forward<_Up>(__u); } 00413 00414 typedef __is_transparent is_transparent; 00415 }; 00416 00417 /// One of the @link comparison_functors comparison functors@endlink. 00418 template<> 00419 struct greater<void> 00420 { 00421 template <typename _Tp, typename _Up> 00422 auto 00423 operator()(_Tp&& __t, _Up&& __u) const 00424 noexcept(noexcept(std::forward<_Tp>(__t) > std::forward<_Up>(__u))) 00425 -> decltype(std::forward<_Tp>(__t) > std::forward<_Up>(__u)) 00426 { return std::forward<_Tp>(__t) > std::forward<_Up>(__u); } 00427 00428 typedef __is_transparent is_transparent; 00429 }; 00430 00431 /// One of the @link comparison_functors comparison functors@endlink. 00432 template<> 00433 struct less<void> 00434 { 00435 template <typename _Tp, typename _Up> 00436 auto 00437 operator()(_Tp&& __t, _Up&& __u) const 00438 noexcept(noexcept(std::forward<_Tp>(__t) < std::forward<_Up>(__u))) 00439 -> decltype(std::forward<_Tp>(__t) < std::forward<_Up>(__u)) 00440 { return std::forward<_Tp>(__t) < std::forward<_Up>(__u); } 00441 00442 typedef __is_transparent is_transparent; 00443 }; 00444 00445 /// One of the @link comparison_functors comparison functors@endlink. 00446 template<> 00447 struct greater_equal<void> 00448 { 00449 template <typename _Tp, typename _Up> 00450 auto 00451 operator()(_Tp&& __t, _Up&& __u) const 00452 noexcept(noexcept(std::forward<_Tp>(__t) >= std::forward<_Up>(__u))) 00453 -> decltype(std::forward<_Tp>(__t) >= std::forward<_Up>(__u)) 00454 { return std::forward<_Tp>(__t) >= std::forward<_Up>(__u); } 00455 00456 typedef __is_transparent is_transparent; 00457 }; 00458 00459 /// One of the @link comparison_functors comparison functors@endlink. 00460 template<> 00461 struct less_equal<void> 00462 { 00463 template <typename _Tp, typename _Up> 00464 auto 00465 operator()(_Tp&& __t, _Up&& __u) const 00466 noexcept(noexcept(std::forward<_Tp>(__t) <= std::forward<_Up>(__u))) 00467 -> decltype(std::forward<_Tp>(__t) <= std::forward<_Up>(__u)) 00468 { return std::forward<_Tp>(__t) <= std::forward<_Up>(__u); } 00469 00470 typedef __is_transparent is_transparent; 00471 }; 00472 #endif 00473 /** @} */ 00474 00475 // 20.3.4 logical operations 00476 /** @defgroup logical_functors Boolean Operations Classes 00477 * @ingroup functors 00478 * 00479 * Here are wrapper functors for Boolean operations: @c &&, @c ||, 00480 * and @c !. 00481 * 00482 * @{ 00483 */ 00484 #if __cplusplus > 201103L 00485 template<typename _Tp = void> 00486 struct logical_and; 00487 00488 template<typename _Tp = void> 00489 struct logical_or; 00490 00491 template<typename _Tp = void> 00492 struct logical_not; 00493 #endif 00494 00495 /// One of the @link logical_functors Boolean operations functors@endlink. 00496 template<typename _Tp> 00497 struct logical_and : public binary_function<_Tp, _Tp, bool> 00498 { 00499 bool 00500 operator()(const _Tp& __x, const _Tp& __y) const 00501 { return __x && __y; } 00502 }; 00503 00504 /// One of the @link logical_functors Boolean operations functors@endlink. 00505 template<typename _Tp> 00506 struct logical_or : public binary_function<_Tp, _Tp, bool> 00507 { 00508 bool 00509 operator()(const _Tp& __x, const _Tp& __y) const 00510 { return __x || __y; } 00511 }; 00512 00513 /// One of the @link logical_functors Boolean operations functors@endlink. 00514 template<typename _Tp> 00515 struct logical_not : public unary_function<_Tp, bool> 00516 { 00517 bool 00518 operator()(const _Tp& __x) const 00519 { return !__x; } 00520 }; 00521 00522 #if __cplusplus > 201103L 00523 /// One of the @link logical_functors Boolean operations functors@endlink. 00524 template<> 00525 struct logical_and<void> 00526 { 00527 template <typename _Tp, typename _Up> 00528 auto 00529 operator()(_Tp&& __t, _Up&& __u) const 00530 noexcept(noexcept(std::forward<_Tp>(__t) && std::forward<_Up>(__u))) 00531 -> decltype(std::forward<_Tp>(__t) && std::forward<_Up>(__u)) 00532 { return std::forward<_Tp>(__t) && std::forward<_Up>(__u); } 00533 00534 typedef __is_transparent is_transparent; 00535 }; 00536 00537 /// One of the @link logical_functors Boolean operations functors@endlink. 00538 template<> 00539 struct logical_or<void> 00540 { 00541 template <typename _Tp, typename _Up> 00542 auto 00543 operator()(_Tp&& __t, _Up&& __u) const 00544 noexcept(noexcept(std::forward<_Tp>(__t) || std::forward<_Up>(__u))) 00545 -> decltype(std::forward<_Tp>(__t) || std::forward<_Up>(__u)) 00546 { return std::forward<_Tp>(__t) || std::forward<_Up>(__u); } 00547 00548 typedef __is_transparent is_transparent; 00549 }; 00550 00551 /// One of the @link logical_functors Boolean operations functors@endlink. 00552 template<> 00553 struct logical_not<void> 00554 { 00555 template <typename _Tp> 00556 auto 00557 operator()(_Tp&& __t) const 00558 noexcept(noexcept(!std::forward<_Tp>(__t))) 00559 -> decltype(!std::forward<_Tp>(__t)) 00560 { return !std::forward<_Tp>(__t); } 00561 00562 typedef __is_transparent is_transparent; 00563 }; 00564 #endif 00565 /** @} */ 00566 00567 #if __cplusplus > 201103L 00568 template<typename _Tp = void> 00569 struct bit_and; 00570 00571 template<typename _Tp = void> 00572 struct bit_or; 00573 00574 template<typename _Tp = void> 00575 struct bit_xor; 00576 00577 template<typename _Tp = void> 00578 struct bit_not; 00579 #endif 00580 00581 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00582 // DR 660. Missing Bitwise Operations. 00583 template<typename _Tp> 00584 struct bit_and : public binary_function<_Tp, _Tp, _Tp> 00585 { 00586 _Tp 00587 operator()(const _Tp& __x, const _Tp& __y) const 00588 { return __x & __y; } 00589 }; 00590 00591 template<typename _Tp> 00592 struct bit_or : public binary_function<_Tp, _Tp, _Tp> 00593 { 00594 _Tp 00595 operator()(const _Tp& __x, const _Tp& __y) const 00596 { return __x | __y; } 00597 }; 00598 00599 template<typename _Tp> 00600 struct bit_xor : public binary_function<_Tp, _Tp, _Tp> 00601 { 00602 _Tp 00603 operator()(const _Tp& __x, const _Tp& __y) const 00604 { return __x ^ __y; } 00605 }; 00606 00607 template<typename _Tp> 00608 struct bit_not : public unary_function<_Tp, _Tp> 00609 { 00610 _Tp 00611 operator()(const _Tp& __x) const 00612 { return ~__x; } 00613 }; 00614 00615 #if __cplusplus > 201103L 00616 template <> 00617 struct bit_and<void> 00618 { 00619 template <typename _Tp, typename _Up> 00620 auto 00621 operator()(_Tp&& __t, _Up&& __u) const 00622 noexcept(noexcept(std::forward<_Tp>(__t) & std::forward<_Up>(__u))) 00623 -> decltype(std::forward<_Tp>(__t) & std::forward<_Up>(__u)) 00624 { return std::forward<_Tp>(__t) & std::forward<_Up>(__u); } 00625 00626 typedef __is_transparent is_transparent; 00627 }; 00628 00629 template <> 00630 struct bit_or<void> 00631 { 00632 template <typename _Tp, typename _Up> 00633 auto 00634 operator()(_Tp&& __t, _Up&& __u) const 00635 noexcept(noexcept(std::forward<_Tp>(__t) | std::forward<_Up>(__u))) 00636 -> decltype(std::forward<_Tp>(__t) | std::forward<_Up>(__u)) 00637 { return std::forward<_Tp>(__t) | std::forward<_Up>(__u); } 00638 00639 typedef __is_transparent is_transparent; 00640 }; 00641 00642 template <> 00643 struct bit_xor<void> 00644 { 00645 template <typename _Tp, typename _Up> 00646 auto 00647 operator()(_Tp&& __t, _Up&& __u) const 00648 noexcept(noexcept(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u))) 00649 -> decltype(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u)) 00650 { return std::forward<_Tp>(__t) ^ std::forward<_Up>(__u); } 00651 00652 typedef __is_transparent is_transparent; 00653 }; 00654 00655 template <> 00656 struct bit_not<void> 00657 { 00658 template <typename _Tp> 00659 auto 00660 operator()(_Tp&& __t) const 00661 noexcept(noexcept(~std::forward<_Tp>(__t))) 00662 -> decltype(~std::forward<_Tp>(__t)) 00663 { return ~std::forward<_Tp>(__t); } 00664 00665 typedef __is_transparent is_transparent; 00666 }; 00667 #endif 00668 00669 // 20.3.5 negators 00670 /** @defgroup negators Negators 00671 * @ingroup functors 00672 * 00673 * The functions @c not1 and @c not2 each take a predicate functor 00674 * and return an instance of @c unary_negate or 00675 * @c binary_negate, respectively. These classes are functors whose 00676 * @c operator() performs the stored predicate function and then returns 00677 * the negation of the result. 00678 * 00679 * For example, given a vector of integers and a trivial predicate, 00680 * \code 00681 * struct IntGreaterThanThree 00682 * : public std::unary_function<int, bool> 00683 * { 00684 * bool operator() (int x) { return x > 3; } 00685 * }; 00686 * 00687 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree())); 00688 * \endcode 00689 * The call to @c find_if will locate the first index (i) of @c v for which 00690 * <code>!(v[i] > 3)</code> is true. 00691 * 00692 * The not1/unary_negate combination works on predicates taking a single 00693 * argument. The not2/binary_negate combination works on predicates which 00694 * take two arguments. 00695 * 00696 * @{ 00697 */ 00698 /// One of the @link negators negation functors@endlink. 00699 template<typename _Predicate> 00700 class unary_negate 00701 : public unary_function<typename _Predicate::argument_type, bool> 00702 { 00703 protected: 00704 _Predicate _M_pred; 00705 00706 public: 00707 explicit 00708 unary_negate(const _Predicate& __x) : _M_pred(__x) { } 00709 00710 bool 00711 operator()(const typename _Predicate::argument_type& __x) const 00712 { return !_M_pred(__x); } 00713 }; 00714 00715 /// One of the @link negators negation functors@endlink. 00716 template<typename _Predicate> 00717 inline unary_negate<_Predicate> 00718 not1(const _Predicate& __pred) 00719 { return unary_negate<_Predicate>(__pred); } 00720 00721 /// One of the @link negators negation functors@endlink. 00722 template<typename _Predicate> 00723 class binary_negate 00724 : public binary_function<typename _Predicate::first_argument_type, 00725 typename _Predicate::second_argument_type, bool> 00726 { 00727 protected: 00728 _Predicate _M_pred; 00729 00730 public: 00731 explicit 00732 binary_negate(const _Predicate& __x) : _M_pred(__x) { } 00733 00734 bool 00735 operator()(const typename _Predicate::first_argument_type& __x, 00736 const typename _Predicate::second_argument_type& __y) const 00737 { return !_M_pred(__x, __y); } 00738 }; 00739 00740 /// One of the @link negators negation functors@endlink. 00741 template<typename _Predicate> 00742 inline binary_negate<_Predicate> 00743 not2(const _Predicate& __pred) 00744 { return binary_negate<_Predicate>(__pred); } 00745 /** @} */ 00746 00747 // 20.3.7 adaptors pointers functions 00748 /** @defgroup pointer_adaptors Adaptors for pointers to functions 00749 * @ingroup functors 00750 * 00751 * The advantage of function objects over pointers to functions is that 00752 * the objects in the standard library declare nested typedefs describing 00753 * their argument and result types with uniform names (e.g., @c result_type 00754 * from the base classes @c unary_function and @c binary_function). 00755 * Sometimes those typedefs are required, not just optional. 00756 * 00757 * Adaptors are provided to turn pointers to unary (single-argument) and 00758 * binary (double-argument) functions into function objects. The 00759 * long-winded functor @c pointer_to_unary_function is constructed with a 00760 * function pointer @c f, and its @c operator() called with argument @c x 00761 * returns @c f(x). The functor @c pointer_to_binary_function does the same 00762 * thing, but with a double-argument @c f and @c operator(). 00763 * 00764 * The function @c ptr_fun takes a pointer-to-function @c f and constructs 00765 * an instance of the appropriate functor. 00766 * 00767 * @{ 00768 */ 00769 /// One of the @link pointer_adaptors adaptors for function pointers@endlink. 00770 template<typename _Arg, typename _Result> 00771 class pointer_to_unary_function : public unary_function<_Arg, _Result> 00772 { 00773 protected: 00774 _Result (*_M_ptr)(_Arg); 00775 00776 public: 00777 pointer_to_unary_function() { } 00778 00779 explicit 00780 pointer_to_unary_function(_Result (*__x)(_Arg)) 00781 : _M_ptr(__x) { } 00782 00783 _Result 00784 operator()(_Arg __x) const 00785 { return _M_ptr(__x); } 00786 }; 00787 00788 /// One of the @link pointer_adaptors adaptors for function pointers@endlink. 00789 template<typename _Arg, typename _Result> 00790 inline pointer_to_unary_function<_Arg, _Result> 00791 ptr_fun(_Result (*__x)(_Arg)) 00792 { return pointer_to_unary_function<_Arg, _Result>(__x); } 00793 00794 /// One of the @link pointer_adaptors adaptors for function pointers@endlink. 00795 template<typename _Arg1, typename _Arg2, typename _Result> 00796 class pointer_to_binary_function 00797 : public binary_function<_Arg1, _Arg2, _Result> 00798 { 00799 protected: 00800 _Result (*_M_ptr)(_Arg1, _Arg2); 00801 00802 public: 00803 pointer_to_binary_function() { } 00804 00805 explicit 00806 pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2)) 00807 : _M_ptr(__x) { } 00808 00809 _Result 00810 operator()(_Arg1 __x, _Arg2 __y) const 00811 { return _M_ptr(__x, __y); } 00812 }; 00813 00814 /// One of the @link pointer_adaptors adaptors for function pointers@endlink. 00815 template<typename _Arg1, typename _Arg2, typename _Result> 00816 inline pointer_to_binary_function<_Arg1, _Arg2, _Result> 00817 ptr_fun(_Result (*__x)(_Arg1, _Arg2)) 00818 { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); } 00819 /** @} */ 00820 00821 template<typename _Tp> 00822 struct _Identity 00823 : public unary_function<_Tp,_Tp> 00824 { 00825 _Tp& 00826 operator()(_Tp& __x) const 00827 { return __x; } 00828 00829 const _Tp& 00830 operator()(const _Tp& __x) const 00831 { return __x; } 00832 }; 00833 00834 template<typename _Pair> 00835 struct _Select1st 00836 : public unary_function<_Pair, typename _Pair::first_type> 00837 { 00838 typename _Pair::first_type& 00839 operator()(_Pair& __x) const 00840 { return __x.first; } 00841 00842 const typename _Pair::first_type& 00843 operator()(const _Pair& __x) const 00844 { return __x.first; } 00845 00846 #if __cplusplus >= 201103L 00847 template<typename _Pair2> 00848 typename _Pair2::first_type& 00849 operator()(_Pair2& __x) const 00850 { return __x.first; } 00851 00852 template<typename _Pair2> 00853 const typename _Pair2::first_type& 00854 operator()(const _Pair2& __x) const 00855 { return __x.first; } 00856 #endif 00857 }; 00858 00859 template<typename _Pair> 00860 struct _Select2nd 00861 : public unary_function<_Pair, typename _Pair::second_type> 00862 { 00863 typename _Pair::second_type& 00864 operator()(_Pair& __x) const 00865 { return __x.second; } 00866 00867 const typename _Pair::second_type& 00868 operator()(const _Pair& __x) const 00869 { return __x.second; } 00870 }; 00871 00872 // 20.3.8 adaptors pointers members 00873 /** @defgroup memory_adaptors Adaptors for pointers to members 00874 * @ingroup functors 00875 * 00876 * There are a total of 8 = 2^3 function objects in this family. 00877 * (1) Member functions taking no arguments vs member functions taking 00878 * one argument. 00879 * (2) Call through pointer vs call through reference. 00880 * (3) Const vs non-const member function. 00881 * 00882 * All of this complexity is in the function objects themselves. You can 00883 * ignore it by using the helper function mem_fun and mem_fun_ref, 00884 * which create whichever type of adaptor is appropriate. 00885 * 00886 * @{ 00887 */ 00888 /// One of the @link memory_adaptors adaptors for member 00889 /// pointers@endlink. 00890 template<typename _Ret, typename _Tp> 00891 class mem_fun_t : public unary_function<_Tp*, _Ret> 00892 { 00893 public: 00894 explicit 00895 mem_fun_t(_Ret (_Tp::*__pf)()) 00896 : _M_f(__pf) { } 00897 00898 _Ret 00899 operator()(_Tp* __p) const 00900 { return (__p->*_M_f)(); } 00901 00902 private: 00903 _Ret (_Tp::*_M_f)(); 00904 }; 00905 00906 /// One of the @link memory_adaptors adaptors for member 00907 /// pointers@endlink. 00908 template<typename _Ret, typename _Tp> 00909 class const_mem_fun_t : public unary_function<const _Tp*, _Ret> 00910 { 00911 public: 00912 explicit 00913 const_mem_fun_t(_Ret (_Tp::*__pf)() const) 00914 : _M_f(__pf) { } 00915 00916 _Ret 00917 operator()(const _Tp* __p) const 00918 { return (__p->*_M_f)(); } 00919 00920 private: 00921 _Ret (_Tp::*_M_f)() const; 00922 }; 00923 00924 /// One of the @link memory_adaptors adaptors for member 00925 /// pointers@endlink. 00926 template<typename _Ret, typename _Tp> 00927 class mem_fun_ref_t : public unary_function<_Tp, _Ret> 00928 { 00929 public: 00930 explicit 00931 mem_fun_ref_t(_Ret (_Tp::*__pf)()) 00932 : _M_f(__pf) { } 00933 00934 _Ret 00935 operator()(_Tp& __r) const 00936 { return (__r.*_M_f)(); } 00937 00938 private: 00939 _Ret (_Tp::*_M_f)(); 00940 }; 00941 00942 /// One of the @link memory_adaptors adaptors for member 00943 /// pointers@endlink. 00944 template<typename _Ret, typename _Tp> 00945 class const_mem_fun_ref_t : public unary_function<_Tp, _Ret> 00946 { 00947 public: 00948 explicit 00949 const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) 00950 : _M_f(__pf) { } 00951 00952 _Ret 00953 operator()(const _Tp& __r) const 00954 { return (__r.*_M_f)(); } 00955 00956 private: 00957 _Ret (_Tp::*_M_f)() const; 00958 }; 00959 00960 /// One of the @link memory_adaptors adaptors for member 00961 /// pointers@endlink. 00962 template<typename _Ret, typename _Tp, typename _Arg> 00963 class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret> 00964 { 00965 public: 00966 explicit 00967 mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) 00968 : _M_f(__pf) { } 00969 00970 _Ret 00971 operator()(_Tp* __p, _Arg __x) const 00972 { return (__p->*_M_f)(__x); } 00973 00974 private: 00975 _Ret (_Tp::*_M_f)(_Arg); 00976 }; 00977 00978 /// One of the @link memory_adaptors adaptors for member 00979 /// pointers@endlink. 00980 template<typename _Ret, typename _Tp, typename _Arg> 00981 class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret> 00982 { 00983 public: 00984 explicit 00985 const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) 00986 : _M_f(__pf) { } 00987 00988 _Ret 00989 operator()(const _Tp* __p, _Arg __x) const 00990 { return (__p->*_M_f)(__x); } 00991 00992 private: 00993 _Ret (_Tp::*_M_f)(_Arg) const; 00994 }; 00995 00996 /// One of the @link memory_adaptors adaptors for member 00997 /// pointers@endlink. 00998 template<typename _Ret, typename _Tp, typename _Arg> 00999 class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret> 01000 { 01001 public: 01002 explicit 01003 mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) 01004 : _M_f(__pf) { } 01005 01006 _Ret 01007 operator()(_Tp& __r, _Arg __x) const 01008 { return (__r.*_M_f)(__x); } 01009 01010 private: 01011 _Ret (_Tp::*_M_f)(_Arg); 01012 }; 01013 01014 /// One of the @link memory_adaptors adaptors for member 01015 /// pointers@endlink. 01016 template<typename _Ret, typename _Tp, typename _Arg> 01017 class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret> 01018 { 01019 public: 01020 explicit 01021 const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) 01022 : _M_f(__pf) { } 01023 01024 _Ret 01025 operator()(const _Tp& __r, _Arg __x) const 01026 { return (__r.*_M_f)(__x); } 01027 01028 private: 01029 _Ret (_Tp::*_M_f)(_Arg) const; 01030 }; 01031 01032 // Mem_fun adaptor helper functions. There are only two: 01033 // mem_fun and mem_fun_ref. 01034 template<typename _Ret, typename _Tp> 01035 inline mem_fun_t<_Ret, _Tp> 01036 mem_fun(_Ret (_Tp::*__f)()) 01037 { return mem_fun_t<_Ret, _Tp>(__f); } 01038 01039 template<typename _Ret, typename _Tp> 01040 inline const_mem_fun_t<_Ret, _Tp> 01041 mem_fun(_Ret (_Tp::*__f)() const) 01042 { return const_mem_fun_t<_Ret, _Tp>(__f); } 01043 01044 template<typename _Ret, typename _Tp> 01045 inline mem_fun_ref_t<_Ret, _Tp> 01046 mem_fun_ref(_Ret (_Tp::*__f)()) 01047 { return mem_fun_ref_t<_Ret, _Tp>(__f); } 01048 01049 template<typename _Ret, typename _Tp> 01050 inline const_mem_fun_ref_t<_Ret, _Tp> 01051 mem_fun_ref(_Ret (_Tp::*__f)() const) 01052 { return const_mem_fun_ref_t<_Ret, _Tp>(__f); } 01053 01054 template<typename _Ret, typename _Tp, typename _Arg> 01055 inline mem_fun1_t<_Ret, _Tp, _Arg> 01056 mem_fun(_Ret (_Tp::*__f)(_Arg)) 01057 { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 01058 01059 template<typename _Ret, typename _Tp, typename _Arg> 01060 inline const_mem_fun1_t<_Ret, _Tp, _Arg> 01061 mem_fun(_Ret (_Tp::*__f)(_Arg) const) 01062 { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 01063 01064 template<typename _Ret, typename _Tp, typename _Arg> 01065 inline mem_fun1_ref_t<_Ret, _Tp, _Arg> 01066 mem_fun_ref(_Ret (_Tp::*__f)(_Arg)) 01067 { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 01068 01069 template<typename _Ret, typename _Tp, typename _Arg> 01070 inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg> 01071 mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const) 01072 { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 01073 01074 /** @} */ 01075 01076 _GLIBCXX_END_NAMESPACE_VERSION 01077 } // namespace 01078 01079 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED 01080 # include <backward/binders.h> 01081 #endif 01082 01083 #endif /* _STL_FUNCTION_H */