1 // Functor implementations -*- C++ -*- 2 3 // Copyright (C) 2001-2018 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /* 26 * 27 * Copyright (c) 1994 28 * Hewlett-Packard Company 29 * 30 * Permission to use, copy, modify, distribute and sell this software 31 * and its documentation for any purpose is hereby granted without fee, 32 * provided that the above copyright notice appear in all copies and 33 * that both that copyright notice and this permission notice appear 34 * in supporting documentation. Hewlett-Packard Company makes no 35 * representations about the suitability of this software for any 36 * purpose. It is provided "as is" without express or implied warranty. 37 * 38 * 39 * Copyright (c) 1996-1998 40 * Silicon Graphics Computer Systems, Inc. 41 * 42 * Permission to use, copy, modify, distribute and sell this software 43 * and its documentation for any purpose is hereby granted without fee, 44 * provided that the above copyright notice appear in all copies and 45 * that both that copyright notice and this permission notice appear 46 * in supporting documentation. Silicon Graphics makes no 47 * representations about the suitability of this software for any 48 * purpose. It is provided "as is" without express or implied warranty. 49 */ 50 51 /** @file backward/binders.h 52 * This is an internal header file, included by other library headers. 53 * Do not attempt to use it directly. @headername{functional} 54 */ 55 56 #ifndef _BACKWARD_BINDERS_H 57 #define _BACKWARD_BINDERS_H 1 58 59 // Suppress deprecated warning for this file. 60 #pragma GCC diagnostic push 61 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 62 63 namespace std _GLIBCXX_VISIBILITY(default) 64 { 65 _GLIBCXX_BEGIN_NAMESPACE_VERSION 66 67 // 20.3.6 binders 68 /** @defgroup binders Binder Classes 69 * @ingroup functors 70 * 71 * Binders turn functions/functors with two arguments into functors 72 * with a single argument, storing an argument to be applied later. 73 * For example, a variable @c B of type @c binder1st is constructed 74 * from a functor @c f and an argument @c x. Later, B's @c 75 * operator() is called with a single argument @c y. The return 76 * value is the value of @c f(x,y). @c B can be @a called with 77 * various arguments (y1, y2, ...) and will in turn call @c 78 * f(x,y1), @c f(x,y2), ... 79 * 80 * The function @c bind1st is provided to save some typing. It takes the 81 * function and an argument as parameters, and returns an instance of 82 * @c binder1st. 83 * 84 * The type @c binder2nd and its creator function @c bind2nd do the same 85 * thing, but the stored argument is passed as the second parameter instead 86 * of the first, e.g., @c bind2nd(std::minus<float>(),1.3) will create a 87 * functor whose @c operator() accepts a floating-point number, subtracts 88 * 1.3 from it, and returns the result. (If @c bind1st had been used, 89 * the functor would perform <em>1.3 - x</em> instead. 90 * 91 * Creator-wrapper functions like @c bind1st are intended to be used in 92 * calling algorithms. Their return values will be temporary objects. 93 * (The goal is to not require you to type names like 94 * @c std::binder1st<std::plus<int>> for declaring a variable to hold the 95 * return value from @c bind1st(std::plus<int>(),5). 96 * 97 * These become more useful when combined with the composition functions. 98 * 99 * These functions are deprecated in C++11 and can be replaced by 100 * @c std::bind (or @c std::tr1::bind) which is more powerful and flexible, 101 * supporting functions with any number of arguments. Uses of @c bind1st 102 * can be replaced by @c std::bind(f, x, std::placeholders::_1) and 103 * @c bind2nd by @c std::bind(f, std::placeholders::_1, x). 104 * @{ 105 */ 106 /// One of the @link binders binder functors@endlink. 107 template<typename _Operation> 108 class binder1st 109 : public unary_function<typename _Operation::second_argument_type, 110 typename _Operation::result_type> 111 { 112 protected: 113 _Operation op; 114 typename _Operation::first_argument_type value; 115 116 public: 117 binder1st(const _Operation& __x, 118 const typename _Operation::first_argument_type& __y) 119 : op(__x), value(__y) { } 120 121 typename _Operation::result_type 122 operator()(const typename _Operation::second_argument_type& __x) const 123 { return op(value, __x); } 124 125 // _GLIBCXX_RESOLVE_LIB_DEFECTS 126 // 109. Missing binders for non-const sequence elements 127 typename _Operation::result_type 128 operator()(typename _Operation::second_argument_type& __x) const 129 { return op(value, __x); } 130 } _GLIBCXX_DEPRECATED; 131 132 /// One of the @link binders binder functors@endlink. 133 template<typename _Operation, typename _Tp> 134 inline binder1st<_Operation> 135 bind1st(const _Operation& __fn, const _Tp& __x) 136 { 137 typedef typename _Operation::first_argument_type _Arg1_type; 138 return binder1st<_Operation>(__fn, _Arg1_type(__x)); 139 } 140 141 /// One of the @link binders binder functors@endlink. 142 template<typename _Operation> 143 class binder2nd 144 : public unary_function<typename _Operation::first_argument_type, 145 typename _Operation::result_type> 146 { 147 protected: 148 _Operation op; 149 typename _Operation::second_argument_type value; 150 151 public: 152 binder2nd(const _Operation& __x, 153 const typename _Operation::second_argument_type& __y) 154 : op(__x), value(__y) { } 155 156 typename _Operation::result_type 157 operator()(const typename _Operation::first_argument_type& __x) const 158 { return op(__x, value); } 159 160 // _GLIBCXX_RESOLVE_LIB_DEFECTS 161 // 109. Missing binders for non-const sequence elements 162 typename _Operation::result_type 163 operator()(typename _Operation::first_argument_type& __x) const 164 { return op(__x, value); } 165 } _GLIBCXX_DEPRECATED; 166 167 /// One of the @link binders binder functors@endlink. 168 template<typename _Operation, typename _Tp> 169 inline binder2nd<_Operation> 170 bind2nd(const _Operation& __fn, const _Tp& __x) 171 { 172 typedef typename _Operation::second_argument_type _Arg2_type; 173 return binder2nd<_Operation>(__fn, _Arg2_type(__x)); 174 } 175 /** @} */ 176 177 _GLIBCXX_END_NAMESPACE_VERSION 178 } // namespace 179 180 #pragma GCC diagnostic pop 181 182 #endif /* _BACKWARD_BINDERS_H */ 183