1*404b540aSrobert // Functor implementations -*- C++ -*- 2*404b540aSrobert 3*404b540aSrobert // Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc. 4*404b540aSrobert // 5*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free 6*404b540aSrobert // software; you can redistribute it and/or modify it under the 7*404b540aSrobert // terms of the GNU General Public License as published by the 8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option) 9*404b540aSrobert // any later version. 10*404b540aSrobert 11*404b540aSrobert // This library is distributed in the hope that it will be useful, 12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*404b540aSrobert // GNU General Public License for more details. 15*404b540aSrobert 16*404b540aSrobert // You should have received a copy of the GNU General Public License along 17*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free 18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19*404b540aSrobert // USA. 20*404b540aSrobert 21*404b540aSrobert // As a special exception, you may use this file as part of a free software 22*404b540aSrobert // library without restriction. Specifically, if other files instantiate 23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile 24*404b540aSrobert // this file and link it with other files to produce an executable, this 25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by 26*404b540aSrobert // the GNU General Public License. This exception does not however 27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by 28*404b540aSrobert // the GNU General Public License. 29*404b540aSrobert 30*404b540aSrobert /* 31*404b540aSrobert * 32*404b540aSrobert * Copyright (c) 1994 33*404b540aSrobert * Hewlett-Packard Company 34*404b540aSrobert * 35*404b540aSrobert * Permission to use, copy, modify, distribute and sell this software 36*404b540aSrobert * and its documentation for any purpose is hereby granted without fee, 37*404b540aSrobert * provided that the above copyright notice appear in all copies and 38*404b540aSrobert * that both that copyright notice and this permission notice appear 39*404b540aSrobert * in supporting documentation. Hewlett-Packard Company makes no 40*404b540aSrobert * representations about the suitability of this software for any 41*404b540aSrobert * purpose. It is provided "as is" without express or implied warranty. 42*404b540aSrobert * 43*404b540aSrobert * 44*404b540aSrobert * Copyright (c) 1996-1998 45*404b540aSrobert * Silicon Graphics Computer Systems, Inc. 46*404b540aSrobert * 47*404b540aSrobert * Permission to use, copy, modify, distribute and sell this software 48*404b540aSrobert * and its documentation for any purpose is hereby granted without fee, 49*404b540aSrobert * provided that the above copyright notice appear in all copies and 50*404b540aSrobert * that both that copyright notice and this permission notice appear 51*404b540aSrobert * in supporting documentation. Silicon Graphics makes no 52*404b540aSrobert * representations about the suitability of this software for any 53*404b540aSrobert * purpose. It is provided "as is" without express or implied warranty. 54*404b540aSrobert */ 55*404b540aSrobert 56*404b540aSrobert /** @file stl_function.h 57*404b540aSrobert * This is an internal header file, included by other library headers. 58*404b540aSrobert * You should not attempt to use it directly. 59*404b540aSrobert */ 60*404b540aSrobert 61*404b540aSrobert #ifndef _FUNCTION_H 62*404b540aSrobert #define _FUNCTION_H 1 63*404b540aSrobert 64*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std) 65*404b540aSrobert 66*404b540aSrobert // 20.3.1 base classes 67*404b540aSrobert /** @defgroup s20_3_1_base Functor Base Classes 68*404b540aSrobert * Function objects, or @e functors, are objects with an @c operator() 69*404b540aSrobert * defined and accessible. They can be passed as arguments to algorithm 70*404b540aSrobert * templates and used in place of a function pointer. Not only is the 71*404b540aSrobert * resulting expressiveness of the library increased, but the generated 72*404b540aSrobert * code can be more efficient than what you might write by hand. When we 73*404b540aSrobert * refer to "functors," then, generally we include function pointers in 74*404b540aSrobert * the description as well. 75*404b540aSrobert * 76*404b540aSrobert * Often, functors are only created as temporaries passed to algorithm 77*404b540aSrobert * calls, rather than being created as named variables. 78*404b540aSrobert * 79*404b540aSrobert * Two examples taken from the standard itself follow. To perform a 80*404b540aSrobert * by-element addition of two vectors @c a and @c b containing @c double, 81*404b540aSrobert * and put the result in @c a, use 82*404b540aSrobert * \code 83*404b540aSrobert * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>()); 84*404b540aSrobert * \endcode 85*404b540aSrobert * To negate every element in @c a, use 86*404b540aSrobert * \code 87*404b540aSrobert * transform(a.begin(), a.end(), a.begin(), negate<double>()); 88*404b540aSrobert * \endcode 89*404b540aSrobert * The addition and negation functions will be inlined directly. 90*404b540aSrobert * 91*404b540aSrobert * The standard functors are derived from structs named @c unary_function 92*404b540aSrobert * and @c binary_function. These two classes contain nothing but typedefs, 93*404b540aSrobert * to aid in generic (template) programming. If you write your own 94*404b540aSrobert * functors, you might consider doing the same. 95*404b540aSrobert * 96*404b540aSrobert * @{ 97*404b540aSrobert */ 98*404b540aSrobert /** 99*404b540aSrobert * This is one of the @link s20_3_1_base functor base classes@endlink. 100*404b540aSrobert */ 101*404b540aSrobert template <class _Arg, class _Result> 102*404b540aSrobert struct unary_function 103*404b540aSrobert { 104*404b540aSrobert typedef _Arg argument_type; ///< @c argument_type is the type of the 105*404b540aSrobert /// argument (no surprises here) 106*404b540aSrobert 107*404b540aSrobert typedef _Result result_type; ///< @c result_type is the return type 108*404b540aSrobert }; 109*404b540aSrobert 110*404b540aSrobert /** 111*404b540aSrobert * This is one of the @link s20_3_1_base functor base classes@endlink. 112*404b540aSrobert */ 113*404b540aSrobert template <class _Arg1, class _Arg2, class _Result> 114*404b540aSrobert struct binary_function 115*404b540aSrobert { 116*404b540aSrobert typedef _Arg1 first_argument_type; ///< the type of the first argument 117*404b540aSrobert /// (no surprises here) 118*404b540aSrobert 119*404b540aSrobert typedef _Arg2 second_argument_type; ///< the type of the second argument 120*404b540aSrobert typedef _Result result_type; ///< type of the return type 121*404b540aSrobert }; 122*404b540aSrobert /** @} */ 123*404b540aSrobert 124*404b540aSrobert // 20.3.2 arithmetic 125*404b540aSrobert /** @defgroup s20_3_2_arithmetic Arithmetic Classes 126*404b540aSrobert * Because basic math often needs to be done during an algorithm, the library 127*404b540aSrobert * provides functors for those operations. See the documentation for 128*404b540aSrobert * @link s20_3_1_base the base classes@endlink for examples of their use. 129*404b540aSrobert * 130*404b540aSrobert * @{ 131*404b540aSrobert */ 132*404b540aSrobert /// One of the @link s20_3_2_arithmetic math functors@endlink. 133*404b540aSrobert template <class _Tp> 134*404b540aSrobert struct plus : public binary_function<_Tp, _Tp, _Tp> 135*404b540aSrobert { 136*404b540aSrobert _Tp operatorplus137*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 138*404b540aSrobert { return __x + __y; } 139*404b540aSrobert }; 140*404b540aSrobert 141*404b540aSrobert /// One of the @link s20_3_2_arithmetic math functors@endlink. 142*404b540aSrobert template <class _Tp> 143*404b540aSrobert struct minus : public binary_function<_Tp, _Tp, _Tp> 144*404b540aSrobert { 145*404b540aSrobert _Tp operatorminus146*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 147*404b540aSrobert { return __x - __y; } 148*404b540aSrobert }; 149*404b540aSrobert 150*404b540aSrobert /// One of the @link s20_3_2_arithmetic math functors@endlink. 151*404b540aSrobert template <class _Tp> 152*404b540aSrobert struct multiplies : public binary_function<_Tp, _Tp, _Tp> 153*404b540aSrobert { 154*404b540aSrobert _Tp operatormultiplies155*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 156*404b540aSrobert { return __x * __y; } 157*404b540aSrobert }; 158*404b540aSrobert 159*404b540aSrobert /// One of the @link s20_3_2_arithmetic math functors@endlink. 160*404b540aSrobert template <class _Tp> 161*404b540aSrobert struct divides : public binary_function<_Tp, _Tp, _Tp> 162*404b540aSrobert { 163*404b540aSrobert _Tp operatordivides164*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 165*404b540aSrobert { return __x / __y; } 166*404b540aSrobert }; 167*404b540aSrobert 168*404b540aSrobert /// One of the @link s20_3_2_arithmetic math functors@endlink. 169*404b540aSrobert template <class _Tp> 170*404b540aSrobert struct modulus : public binary_function<_Tp, _Tp, _Tp> 171*404b540aSrobert { 172*404b540aSrobert _Tp operatormodulus173*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 174*404b540aSrobert { return __x % __y; } 175*404b540aSrobert }; 176*404b540aSrobert 177*404b540aSrobert /// One of the @link s20_3_2_arithmetic math functors@endlink. 178*404b540aSrobert template <class _Tp> 179*404b540aSrobert struct negate : public unary_function<_Tp, _Tp> 180*404b540aSrobert { 181*404b540aSrobert _Tp operatornegate182*404b540aSrobert operator()(const _Tp& __x) const 183*404b540aSrobert { return -__x; } 184*404b540aSrobert }; 185*404b540aSrobert /** @} */ 186*404b540aSrobert 187*404b540aSrobert // 20.3.3 comparisons 188*404b540aSrobert /** @defgroup s20_3_3_comparisons Comparison Classes 189*404b540aSrobert * The library provides six wrapper functors for all the basic comparisons 190*404b540aSrobert * in C++, like @c <. 191*404b540aSrobert * 192*404b540aSrobert * @{ 193*404b540aSrobert */ 194*404b540aSrobert /// One of the @link s20_3_3_comparisons comparison functors@endlink. 195*404b540aSrobert template <class _Tp> 196*404b540aSrobert struct equal_to : public binary_function<_Tp, _Tp, bool> 197*404b540aSrobert { 198*404b540aSrobert bool operatorequal_to199*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 200*404b540aSrobert { return __x == __y; } 201*404b540aSrobert }; 202*404b540aSrobert 203*404b540aSrobert /// One of the @link s20_3_3_comparisons comparison functors@endlink. 204*404b540aSrobert template <class _Tp> 205*404b540aSrobert struct not_equal_to : public binary_function<_Tp, _Tp, bool> 206*404b540aSrobert { 207*404b540aSrobert bool operatornot_equal_to208*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 209*404b540aSrobert { return __x != __y; } 210*404b540aSrobert }; 211*404b540aSrobert 212*404b540aSrobert /// One of the @link s20_3_3_comparisons comparison functors@endlink. 213*404b540aSrobert template <class _Tp> 214*404b540aSrobert struct greater : public binary_function<_Tp, _Tp, bool> 215*404b540aSrobert { 216*404b540aSrobert bool operatorgreater217*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 218*404b540aSrobert { return __x > __y; } 219*404b540aSrobert }; 220*404b540aSrobert 221*404b540aSrobert /// One of the @link s20_3_3_comparisons comparison functors@endlink. 222*404b540aSrobert template <class _Tp> 223*404b540aSrobert struct less : public binary_function<_Tp, _Tp, bool> 224*404b540aSrobert { 225*404b540aSrobert bool operatorless226*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 227*404b540aSrobert { return __x < __y; } 228*404b540aSrobert }; 229*404b540aSrobert 230*404b540aSrobert /// One of the @link s20_3_3_comparisons comparison functors@endlink. 231*404b540aSrobert template <class _Tp> 232*404b540aSrobert struct greater_equal : public binary_function<_Tp, _Tp, bool> 233*404b540aSrobert { 234*404b540aSrobert bool operatorgreater_equal235*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 236*404b540aSrobert { return __x >= __y; } 237*404b540aSrobert }; 238*404b540aSrobert 239*404b540aSrobert /// One of the @link s20_3_3_comparisons comparison functors@endlink. 240*404b540aSrobert template <class _Tp> 241*404b540aSrobert struct less_equal : public binary_function<_Tp, _Tp, bool> 242*404b540aSrobert { 243*404b540aSrobert bool operatorless_equal244*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 245*404b540aSrobert { return __x <= __y; } 246*404b540aSrobert }; 247*404b540aSrobert /** @} */ 248*404b540aSrobert 249*404b540aSrobert // 20.3.4 logical operations 250*404b540aSrobert /** @defgroup s20_3_4_logical Boolean Operations Classes 251*404b540aSrobert * Here are wrapper functors for Boolean operations: @c &&, @c ||, and @c !. 252*404b540aSrobert * 253*404b540aSrobert * @{ 254*404b540aSrobert */ 255*404b540aSrobert /// One of the @link s20_3_4_logical Boolean operations functors@endlink. 256*404b540aSrobert template <class _Tp> 257*404b540aSrobert struct logical_and : public binary_function<_Tp, _Tp, bool> 258*404b540aSrobert { 259*404b540aSrobert bool operatorlogical_and260*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 261*404b540aSrobert { return __x && __y; } 262*404b540aSrobert }; 263*404b540aSrobert 264*404b540aSrobert /// One of the @link s20_3_4_logical Boolean operations functors@endlink. 265*404b540aSrobert template <class _Tp> 266*404b540aSrobert struct logical_or : public binary_function<_Tp, _Tp, bool> 267*404b540aSrobert { 268*404b540aSrobert bool operatorlogical_or269*404b540aSrobert operator()(const _Tp& __x, const _Tp& __y) const 270*404b540aSrobert { return __x || __y; } 271*404b540aSrobert }; 272*404b540aSrobert 273*404b540aSrobert /// One of the @link s20_3_4_logical Boolean operations functors@endlink. 274*404b540aSrobert template <class _Tp> 275*404b540aSrobert struct logical_not : public unary_function<_Tp, bool> 276*404b540aSrobert { 277*404b540aSrobert bool operatorlogical_not278*404b540aSrobert operator()(const _Tp& __x) const 279*404b540aSrobert { return !__x; } 280*404b540aSrobert }; 281*404b540aSrobert /** @} */ 282*404b540aSrobert 283*404b540aSrobert // 20.3.5 negators 284*404b540aSrobert /** @defgroup s20_3_5_negators Negators 285*404b540aSrobert * The functions @c not1 and @c not2 each take a predicate functor 286*404b540aSrobert * and return an instance of @c unary_negate or 287*404b540aSrobert * @c binary_negate, respectively. These classes are functors whose 288*404b540aSrobert * @c operator() performs the stored predicate function and then returns 289*404b540aSrobert * the negation of the result. 290*404b540aSrobert * 291*404b540aSrobert * For example, given a vector of integers and a trivial predicate, 292*404b540aSrobert * \code 293*404b540aSrobert * struct IntGreaterThanThree 294*404b540aSrobert * : public std::unary_function<int, bool> 295*404b540aSrobert * { 296*404b540aSrobert * bool operator() (int x) { return x > 3; } 297*404b540aSrobert * }; 298*404b540aSrobert * 299*404b540aSrobert * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree())); 300*404b540aSrobert * \endcode 301*404b540aSrobert * The call to @c find_if will locate the first index (i) of @c v for which 302*404b540aSrobert * "!(v[i] > 3)" is true. 303*404b540aSrobert * 304*404b540aSrobert * The not1/unary_negate combination works on predicates taking a single 305*404b540aSrobert * argument. The not2/binary_negate combination works on predicates which 306*404b540aSrobert * take two arguments. 307*404b540aSrobert * 308*404b540aSrobert * @{ 309*404b540aSrobert */ 310*404b540aSrobert /// One of the @link s20_3_5_negators negation functors@endlink. 311*404b540aSrobert template <class _Predicate> 312*404b540aSrobert class unary_negate 313*404b540aSrobert : public unary_function<typename _Predicate::argument_type, bool> 314*404b540aSrobert { 315*404b540aSrobert protected: 316*404b540aSrobert _Predicate _M_pred; 317*404b540aSrobert public: 318*404b540aSrobert explicit unary_negate(const _Predicate & __x)319*404b540aSrobert unary_negate(const _Predicate& __x) : _M_pred(__x) {} 320*404b540aSrobert 321*404b540aSrobert bool operator()322*404b540aSrobert operator()(const typename _Predicate::argument_type& __x) const 323*404b540aSrobert { return !_M_pred(__x); } 324*404b540aSrobert }; 325*404b540aSrobert 326*404b540aSrobert /// One of the @link s20_3_5_negators negation functors@endlink. 327*404b540aSrobert template <class _Predicate> 328*404b540aSrobert inline unary_negate<_Predicate> not1(const _Predicate & __pred)329*404b540aSrobert not1(const _Predicate& __pred) 330*404b540aSrobert { return unary_negate<_Predicate>(__pred); } 331*404b540aSrobert 332*404b540aSrobert /// One of the @link s20_3_5_negators negation functors@endlink. 333*404b540aSrobert template <class _Predicate> 334*404b540aSrobert class binary_negate 335*404b540aSrobert : public binary_function<typename _Predicate::first_argument_type, 336*404b540aSrobert typename _Predicate::second_argument_type, 337*404b540aSrobert bool> 338*404b540aSrobert { 339*404b540aSrobert protected: 340*404b540aSrobert _Predicate _M_pred; 341*404b540aSrobert public: 342*404b540aSrobert explicit binary_negate(const _Predicate & __x)343*404b540aSrobert binary_negate(const _Predicate& __x) 344*404b540aSrobert : _M_pred(__x) { } 345*404b540aSrobert 346*404b540aSrobert bool operator()347*404b540aSrobert operator()(const typename _Predicate::first_argument_type& __x, 348*404b540aSrobert const typename _Predicate::second_argument_type& __y) const 349*404b540aSrobert { return !_M_pred(__x, __y); } 350*404b540aSrobert }; 351*404b540aSrobert 352*404b540aSrobert /// One of the @link s20_3_5_negators negation functors@endlink. 353*404b540aSrobert template <class _Predicate> 354*404b540aSrobert inline binary_negate<_Predicate> not2(const _Predicate & __pred)355*404b540aSrobert not2(const _Predicate& __pred) 356*404b540aSrobert { return binary_negate<_Predicate>(__pred); } 357*404b540aSrobert /** @} */ 358*404b540aSrobert 359*404b540aSrobert // 20.3.6 binders 360*404b540aSrobert /** @defgroup s20_3_6_binder Binder Classes 361*404b540aSrobert * Binders turn functions/functors with two arguments into functors with 362*404b540aSrobert * a single argument, storing an argument to be applied later. For 363*404b540aSrobert * example, a variable @c B of type @c binder1st is constructed from a 364*404b540aSrobert * functor @c f and an argument @c x. Later, B's @c operator() is called 365*404b540aSrobert * with a single argument @c y. The return value is the value of @c f(x,y). 366*404b540aSrobert * @c B can be "called" with various arguments (y1, y2, ...) and will in 367*404b540aSrobert * turn call @c f(x,y1), @c f(x,y2), ... 368*404b540aSrobert * 369*404b540aSrobert * The function @c bind1st is provided to save some typing. It takes the 370*404b540aSrobert * function and an argument as parameters, and returns an instance of 371*404b540aSrobert * @c binder1st. 372*404b540aSrobert * 373*404b540aSrobert * The type @c binder2nd and its creator function @c bind2nd do the same 374*404b540aSrobert * thing, but the stored argument is passed as the second parameter instead 375*404b540aSrobert * of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a 376*404b540aSrobert * functor whose @c operator() accepts a floating-point number, subtracts 377*404b540aSrobert * 1.3 from it, and returns the result. (If @c bind1st had been used, 378*404b540aSrobert * the functor would perform "1.3 - x" instead. 379*404b540aSrobert * 380*404b540aSrobert * Creator-wrapper functions like @c bind1st are intended to be used in 381*404b540aSrobert * calling algorithms. Their return values will be temporary objects. 382*404b540aSrobert * (The goal is to not require you to type names like 383*404b540aSrobert * @c std::binder1st<std::plus<int>> for declaring a variable to hold the 384*404b540aSrobert * return value from @c bind1st(std::plus<int>,5). 385*404b540aSrobert * 386*404b540aSrobert * These become more useful when combined with the composition functions. 387*404b540aSrobert * 388*404b540aSrobert * @{ 389*404b540aSrobert */ 390*404b540aSrobert /// One of the @link s20_3_6_binder binder functors@endlink. 391*404b540aSrobert template <class _Operation> 392*404b540aSrobert class binder1st 393*404b540aSrobert : public unary_function<typename _Operation::second_argument_type, 394*404b540aSrobert typename _Operation::result_type> 395*404b540aSrobert { 396*404b540aSrobert protected: 397*404b540aSrobert _Operation op; 398*404b540aSrobert typename _Operation::first_argument_type value; 399*404b540aSrobert public: binder1st(const _Operation & __x,const typename _Operation::first_argument_type & __y)400*404b540aSrobert binder1st(const _Operation& __x, 401*404b540aSrobert const typename _Operation::first_argument_type& __y) 402*404b540aSrobert : op(__x), value(__y) {} 403*404b540aSrobert 404*404b540aSrobert typename _Operation::result_type operator()405*404b540aSrobert operator()(const typename _Operation::second_argument_type& __x) const 406*404b540aSrobert { return op(value, __x); } 407*404b540aSrobert 408*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS 409*404b540aSrobert // 109. Missing binders for non-const sequence elements 410*404b540aSrobert typename _Operation::result_type operator()411*404b540aSrobert operator()(typename _Operation::second_argument_type& __x) const 412*404b540aSrobert { return op(value, __x); } 413*404b540aSrobert }; 414*404b540aSrobert 415*404b540aSrobert /// One of the @link s20_3_6_binder binder functors@endlink. 416*404b540aSrobert template <class _Operation, class _Tp> 417*404b540aSrobert inline binder1st<_Operation> bind1st(const _Operation & __fn,const _Tp & __x)418*404b540aSrobert bind1st(const _Operation& __fn, const _Tp& __x) 419*404b540aSrobert { 420*404b540aSrobert typedef typename _Operation::first_argument_type _Arg1_type; 421*404b540aSrobert return binder1st<_Operation>(__fn, _Arg1_type(__x)); 422*404b540aSrobert } 423*404b540aSrobert 424*404b540aSrobert /// One of the @link s20_3_6_binder binder functors@endlink. 425*404b540aSrobert template <class _Operation> 426*404b540aSrobert class binder2nd 427*404b540aSrobert : public unary_function<typename _Operation::first_argument_type, 428*404b540aSrobert typename _Operation::result_type> 429*404b540aSrobert { 430*404b540aSrobert protected: 431*404b540aSrobert _Operation op; 432*404b540aSrobert typename _Operation::second_argument_type value; 433*404b540aSrobert public: binder2nd(const _Operation & __x,const typename _Operation::second_argument_type & __y)434*404b540aSrobert binder2nd(const _Operation& __x, 435*404b540aSrobert const typename _Operation::second_argument_type& __y) 436*404b540aSrobert : op(__x), value(__y) {} 437*404b540aSrobert 438*404b540aSrobert typename _Operation::result_type operator()439*404b540aSrobert operator()(const typename _Operation::first_argument_type& __x) const 440*404b540aSrobert { return op(__x, value); } 441*404b540aSrobert 442*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS 443*404b540aSrobert // 109. Missing binders for non-const sequence elements 444*404b540aSrobert typename _Operation::result_type operator()445*404b540aSrobert operator()(typename _Operation::first_argument_type& __x) const 446*404b540aSrobert { return op(__x, value); } 447*404b540aSrobert }; 448*404b540aSrobert 449*404b540aSrobert /// One of the @link s20_3_6_binder binder functors@endlink. 450*404b540aSrobert template <class _Operation, class _Tp> 451*404b540aSrobert inline binder2nd<_Operation> bind2nd(const _Operation & __fn,const _Tp & __x)452*404b540aSrobert bind2nd(const _Operation& __fn, const _Tp& __x) 453*404b540aSrobert { 454*404b540aSrobert typedef typename _Operation::second_argument_type _Arg2_type; 455*404b540aSrobert return binder2nd<_Operation>(__fn, _Arg2_type(__x)); 456*404b540aSrobert } 457*404b540aSrobert /** @} */ 458*404b540aSrobert 459*404b540aSrobert // 20.3.7 adaptors pointers functions 460*404b540aSrobert /** @defgroup s20_3_7_adaptors Adaptors for pointers to functions 461*404b540aSrobert * The advantage of function objects over pointers to functions is that 462*404b540aSrobert * the objects in the standard library declare nested typedefs describing 463*404b540aSrobert * their argument and result types with uniform names (e.g., @c result_type 464*404b540aSrobert * from the base classes @c unary_function and @c binary_function). 465*404b540aSrobert * Sometimes those typedefs are required, not just optional. 466*404b540aSrobert * 467*404b540aSrobert * Adaptors are provided to turn pointers to unary (single-argument) and 468*404b540aSrobert * binary (double-argument) functions into function objects. The 469*404b540aSrobert * long-winded functor @c pointer_to_unary_function is constructed with a 470*404b540aSrobert * function pointer @c f, and its @c operator() called with argument @c x 471*404b540aSrobert * returns @c f(x). The functor @c pointer_to_binary_function does the same 472*404b540aSrobert * thing, but with a double-argument @c f and @c operator(). 473*404b540aSrobert * 474*404b540aSrobert * The function @c ptr_fun takes a pointer-to-function @c f and constructs 475*404b540aSrobert * an instance of the appropriate functor. 476*404b540aSrobert * 477*404b540aSrobert * @{ 478*404b540aSrobert */ 479*404b540aSrobert /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. 480*404b540aSrobert template <class _Arg, class _Result> 481*404b540aSrobert class pointer_to_unary_function : public unary_function<_Arg, _Result> 482*404b540aSrobert { 483*404b540aSrobert protected: 484*404b540aSrobert _Result (*_M_ptr)(_Arg); 485*404b540aSrobert public: pointer_to_unary_function()486*404b540aSrobert pointer_to_unary_function() {} 487*404b540aSrobert 488*404b540aSrobert explicit pointer_to_unary_function(_Result (* __x)(_Arg))489*404b540aSrobert pointer_to_unary_function(_Result (*__x)(_Arg)) 490*404b540aSrobert : _M_ptr(__x) {} 491*404b540aSrobert 492*404b540aSrobert _Result operator()493*404b540aSrobert operator()(_Arg __x) const 494*404b540aSrobert { return _M_ptr(__x); } 495*404b540aSrobert }; 496*404b540aSrobert 497*404b540aSrobert /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. 498*404b540aSrobert template <class _Arg, class _Result> 499*404b540aSrobert inline pointer_to_unary_function<_Arg, _Result> ptr_fun(_Result (* __x)(_Arg))500*404b540aSrobert ptr_fun(_Result (*__x)(_Arg)) 501*404b540aSrobert { return pointer_to_unary_function<_Arg, _Result>(__x); } 502*404b540aSrobert 503*404b540aSrobert /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. 504*404b540aSrobert template <class _Arg1, class _Arg2, class _Result> 505*404b540aSrobert class pointer_to_binary_function 506*404b540aSrobert : public binary_function<_Arg1, _Arg2, _Result> 507*404b540aSrobert { 508*404b540aSrobert protected: 509*404b540aSrobert _Result (*_M_ptr)(_Arg1, _Arg2); 510*404b540aSrobert public: pointer_to_binary_function()511*404b540aSrobert pointer_to_binary_function() {} 512*404b540aSrobert 513*404b540aSrobert explicit pointer_to_binary_function(_Result (* __x)(_Arg1,_Arg2))514*404b540aSrobert pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2)) 515*404b540aSrobert : _M_ptr(__x) {} 516*404b540aSrobert 517*404b540aSrobert _Result operator()518*404b540aSrobert operator()(_Arg1 __x, _Arg2 __y) const 519*404b540aSrobert { return _M_ptr(__x, __y); } 520*404b540aSrobert }; 521*404b540aSrobert 522*404b540aSrobert /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink. 523*404b540aSrobert template <class _Arg1, class _Arg2, class _Result> 524*404b540aSrobert inline pointer_to_binary_function<_Arg1, _Arg2, _Result> ptr_fun(_Result (* __x)(_Arg1,_Arg2))525*404b540aSrobert ptr_fun(_Result (*__x)(_Arg1, _Arg2)) 526*404b540aSrobert { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); } 527*404b540aSrobert /** @} */ 528*404b540aSrobert 529*404b540aSrobert template <class _Tp> 530*404b540aSrobert struct _Identity : public unary_function<_Tp,_Tp> 531*404b540aSrobert { 532*404b540aSrobert _Tp& operator_Identity533*404b540aSrobert operator()(_Tp& __x) const 534*404b540aSrobert { return __x; } 535*404b540aSrobert 536*404b540aSrobert const _Tp& operator_Identity537*404b540aSrobert operator()(const _Tp& __x) const 538*404b540aSrobert { return __x; } 539*404b540aSrobert }; 540*404b540aSrobert 541*404b540aSrobert template <class _Pair> 542*404b540aSrobert struct _Select1st : public unary_function<_Pair, 543*404b540aSrobert typename _Pair::first_type> 544*404b540aSrobert { 545*404b540aSrobert typename _Pair::first_type& operator_Select1st546*404b540aSrobert operator()(_Pair& __x) const 547*404b540aSrobert { return __x.first; } 548*404b540aSrobert 549*404b540aSrobert const typename _Pair::first_type& operator_Select1st550*404b540aSrobert operator()(const _Pair& __x) const 551*404b540aSrobert { return __x.first; } 552*404b540aSrobert }; 553*404b540aSrobert 554*404b540aSrobert template <class _Pair> 555*404b540aSrobert struct _Select2nd : public unary_function<_Pair, 556*404b540aSrobert typename _Pair::second_type> 557*404b540aSrobert { 558*404b540aSrobert typename _Pair::second_type& operator_Select2nd559*404b540aSrobert operator()(_Pair& __x) const 560*404b540aSrobert { return __x.second; } 561*404b540aSrobert 562*404b540aSrobert const typename _Pair::second_type& operator_Select2nd563*404b540aSrobert operator()(const _Pair& __x) const 564*404b540aSrobert { return __x.second; } 565*404b540aSrobert }; 566*404b540aSrobert 567*404b540aSrobert // 20.3.8 adaptors pointers members 568*404b540aSrobert /** @defgroup s20_3_8_memadaptors Adaptors for pointers to members 569*404b540aSrobert * There are a total of 8 = 2^3 function objects in this family. 570*404b540aSrobert * (1) Member functions taking no arguments vs member functions taking 571*404b540aSrobert * one argument. 572*404b540aSrobert * (2) Call through pointer vs call through reference. 573*404b540aSrobert * (3) Const vs non-const member function. 574*404b540aSrobert * 575*404b540aSrobert * All of this complexity is in the function objects themselves. You can 576*404b540aSrobert * ignore it by using the helper function mem_fun and mem_fun_ref, 577*404b540aSrobert * which create whichever type of adaptor is appropriate. 578*404b540aSrobert * 579*404b540aSrobert * @{ 580*404b540aSrobert */ 581*404b540aSrobert /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 582*404b540aSrobert template <class _Ret, class _Tp> 583*404b540aSrobert class mem_fun_t : public unary_function<_Tp*, _Ret> 584*404b540aSrobert { 585*404b540aSrobert public: 586*404b540aSrobert explicit mem_fun_t(_Ret (_Tp::* __pf)())587*404b540aSrobert mem_fun_t(_Ret (_Tp::*__pf)()) 588*404b540aSrobert : _M_f(__pf) {} 589*404b540aSrobert 590*404b540aSrobert _Ret operator()591*404b540aSrobert operator()(_Tp* __p) const 592*404b540aSrobert { return (__p->*_M_f)(); } 593*404b540aSrobert private: 594*404b540aSrobert _Ret (_Tp::*_M_f)(); 595*404b540aSrobert }; 596*404b540aSrobert 597*404b540aSrobert /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 598*404b540aSrobert template <class _Ret, class _Tp> 599*404b540aSrobert class const_mem_fun_t : public unary_function<const _Tp*, _Ret> 600*404b540aSrobert { 601*404b540aSrobert public: 602*404b540aSrobert explicit const_mem_fun_t(_Ret (_Tp::* __pf)()const)603*404b540aSrobert const_mem_fun_t(_Ret (_Tp::*__pf)() const) 604*404b540aSrobert : _M_f(__pf) {} 605*404b540aSrobert 606*404b540aSrobert _Ret operator()607*404b540aSrobert operator()(const _Tp* __p) const 608*404b540aSrobert { return (__p->*_M_f)(); } 609*404b540aSrobert private: 610*404b540aSrobert _Ret (_Tp::*_M_f)() const; 611*404b540aSrobert }; 612*404b540aSrobert 613*404b540aSrobert /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 614*404b540aSrobert template <class _Ret, class _Tp> 615*404b540aSrobert class mem_fun_ref_t : public unary_function<_Tp, _Ret> 616*404b540aSrobert { 617*404b540aSrobert public: 618*404b540aSrobert explicit mem_fun_ref_t(_Ret (_Tp::* __pf)())619*404b540aSrobert mem_fun_ref_t(_Ret (_Tp::*__pf)()) 620*404b540aSrobert : _M_f(__pf) {} 621*404b540aSrobert 622*404b540aSrobert _Ret operator()623*404b540aSrobert operator()(_Tp& __r) const 624*404b540aSrobert { return (__r.*_M_f)(); } 625*404b540aSrobert private: 626*404b540aSrobert _Ret (_Tp::*_M_f)(); 627*404b540aSrobert }; 628*404b540aSrobert 629*404b540aSrobert /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 630*404b540aSrobert template <class _Ret, class _Tp> 631*404b540aSrobert class const_mem_fun_ref_t : public unary_function<_Tp, _Ret> 632*404b540aSrobert { 633*404b540aSrobert public: 634*404b540aSrobert explicit const_mem_fun_ref_t(_Ret (_Tp::* __pf)()const)635*404b540aSrobert const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) 636*404b540aSrobert : _M_f(__pf) {} 637*404b540aSrobert 638*404b540aSrobert _Ret operator()639*404b540aSrobert operator()(const _Tp& __r) const 640*404b540aSrobert { return (__r.*_M_f)(); } 641*404b540aSrobert private: 642*404b540aSrobert _Ret (_Tp::*_M_f)() const; 643*404b540aSrobert }; 644*404b540aSrobert 645*404b540aSrobert /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 646*404b540aSrobert template <class _Ret, class _Tp, class _Arg> 647*404b540aSrobert class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret> 648*404b540aSrobert { 649*404b540aSrobert public: 650*404b540aSrobert explicit mem_fun1_t(_Ret (_Tp::* __pf)(_Arg))651*404b540aSrobert mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) 652*404b540aSrobert : _M_f(__pf) {} 653*404b540aSrobert 654*404b540aSrobert _Ret operator()655*404b540aSrobert operator()(_Tp* __p, _Arg __x) const 656*404b540aSrobert { return (__p->*_M_f)(__x); } 657*404b540aSrobert private: 658*404b540aSrobert _Ret (_Tp::*_M_f)(_Arg); 659*404b540aSrobert }; 660*404b540aSrobert 661*404b540aSrobert /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 662*404b540aSrobert template <class _Ret, class _Tp, class _Arg> 663*404b540aSrobert class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret> 664*404b540aSrobert { 665*404b540aSrobert public: 666*404b540aSrobert explicit const_mem_fun1_t(_Ret (_Tp::* __pf)(_Arg)const)667*404b540aSrobert const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) 668*404b540aSrobert : _M_f(__pf) {} 669*404b540aSrobert 670*404b540aSrobert _Ret operator()671*404b540aSrobert operator()(const _Tp* __p, _Arg __x) const 672*404b540aSrobert { return (__p->*_M_f)(__x); } 673*404b540aSrobert private: 674*404b540aSrobert _Ret (_Tp::*_M_f)(_Arg) const; 675*404b540aSrobert }; 676*404b540aSrobert 677*404b540aSrobert /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 678*404b540aSrobert template <class _Ret, class _Tp, class _Arg> 679*404b540aSrobert class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret> 680*404b540aSrobert { 681*404b540aSrobert public: 682*404b540aSrobert explicit mem_fun1_ref_t(_Ret (_Tp::* __pf)(_Arg))683*404b540aSrobert mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) 684*404b540aSrobert : _M_f(__pf) {} 685*404b540aSrobert 686*404b540aSrobert _Ret operator()687*404b540aSrobert operator()(_Tp& __r, _Arg __x) const 688*404b540aSrobert { return (__r.*_M_f)(__x); } 689*404b540aSrobert private: 690*404b540aSrobert _Ret (_Tp::*_M_f)(_Arg); 691*404b540aSrobert }; 692*404b540aSrobert 693*404b540aSrobert /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink. 694*404b540aSrobert template <class _Ret, class _Tp, class _Arg> 695*404b540aSrobert class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret> 696*404b540aSrobert { 697*404b540aSrobert public: 698*404b540aSrobert explicit const_mem_fun1_ref_t(_Ret (_Tp::* __pf)(_Arg)const)699*404b540aSrobert const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) 700*404b540aSrobert : _M_f(__pf) {} 701*404b540aSrobert 702*404b540aSrobert _Ret operator()703*404b540aSrobert operator()(const _Tp& __r, _Arg __x) const 704*404b540aSrobert { return (__r.*_M_f)(__x); } 705*404b540aSrobert private: 706*404b540aSrobert _Ret (_Tp::*_M_f)(_Arg) const; 707*404b540aSrobert }; 708*404b540aSrobert 709*404b540aSrobert // Mem_fun adaptor helper functions. There are only two: 710*404b540aSrobert // mem_fun and mem_fun_ref. 711*404b540aSrobert template <class _Ret, class _Tp> 712*404b540aSrobert inline mem_fun_t<_Ret, _Tp> mem_fun(_Ret (_Tp::* __f)())713*404b540aSrobert mem_fun(_Ret (_Tp::*__f)()) 714*404b540aSrobert { return mem_fun_t<_Ret, _Tp>(__f); } 715*404b540aSrobert 716*404b540aSrobert template <class _Ret, class _Tp> 717*404b540aSrobert inline const_mem_fun_t<_Ret, _Tp> mem_fun(_Ret (_Tp::* __f)()const)718*404b540aSrobert mem_fun(_Ret (_Tp::*__f)() const) 719*404b540aSrobert { return const_mem_fun_t<_Ret, _Tp>(__f); } 720*404b540aSrobert 721*404b540aSrobert template <class _Ret, class _Tp> 722*404b540aSrobert inline mem_fun_ref_t<_Ret, _Tp> mem_fun_ref(_Ret (_Tp::* __f)())723*404b540aSrobert mem_fun_ref(_Ret (_Tp::*__f)()) 724*404b540aSrobert { return mem_fun_ref_t<_Ret, _Tp>(__f); } 725*404b540aSrobert 726*404b540aSrobert template <class _Ret, class _Tp> 727*404b540aSrobert inline const_mem_fun_ref_t<_Ret, _Tp> mem_fun_ref(_Ret (_Tp::* __f)()const)728*404b540aSrobert mem_fun_ref(_Ret (_Tp::*__f)() const) 729*404b540aSrobert { return const_mem_fun_ref_t<_Ret, _Tp>(__f); } 730*404b540aSrobert 731*404b540aSrobert template <class _Ret, class _Tp, class _Arg> 732*404b540aSrobert inline mem_fun1_t<_Ret, _Tp, _Arg> mem_fun(_Ret (_Tp::* __f)(_Arg))733*404b540aSrobert mem_fun(_Ret (_Tp::*__f)(_Arg)) 734*404b540aSrobert { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 735*404b540aSrobert 736*404b540aSrobert template <class _Ret, class _Tp, class _Arg> 737*404b540aSrobert inline const_mem_fun1_t<_Ret, _Tp, _Arg> mem_fun(_Ret (_Tp::* __f)(_Arg)const)738*404b540aSrobert mem_fun(_Ret (_Tp::*__f)(_Arg) const) 739*404b540aSrobert { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); } 740*404b540aSrobert 741*404b540aSrobert template <class _Ret, class _Tp, class _Arg> 742*404b540aSrobert inline mem_fun1_ref_t<_Ret, _Tp, _Arg> mem_fun_ref(_Ret (_Tp::* __f)(_Arg))743*404b540aSrobert mem_fun_ref(_Ret (_Tp::*__f)(_Arg)) 744*404b540aSrobert { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 745*404b540aSrobert 746*404b540aSrobert template <class _Ret, class _Tp, class _Arg> 747*404b540aSrobert inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg> mem_fun_ref(_Ret (_Tp::* __f)(_Arg)const)748*404b540aSrobert mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const) 749*404b540aSrobert { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } 750*404b540aSrobert 751*404b540aSrobert /** @} */ 752*404b540aSrobert 753*404b540aSrobert _GLIBCXX_END_NAMESPACE 754*404b540aSrobert 755*404b540aSrobert #endif /* _FUNCTION_H */ 756