1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
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.
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}
56 #ifndef _BACKWARD_BINDERS_H
57 #define _BACKWARD_BINDERS_H 1
59 // Suppress deprecated warning for this file.
60 #pragma GCC diagnostic push
61 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
63 namespace std
_GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68 /** @defgroup binders Binder Classes
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), ...
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
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.
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).
97 * These become more useful when combined with the composition functions.
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).
106 /// One of the @link binders binder functors@endlink.
107 template<typename _Operation
>
109 : public unary_function
<typename
_Operation::second_argument_type
,
110 typename
_Operation::result_type
>
114 typename
_Operation::first_argument_type value
;
117 binder1st(const _Operation
& __x
,
118 const typename
_Operation::first_argument_type
& __y
)
119 : op(__x
), value(__y
) { }
121 typename
_Operation::result_type
122 operator()(const typename
_Operation::second_argument_type
& __x
) const
123 { return op(value
, __x
); }
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
;
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
)
137 typedef typename
_Operation::first_argument_type _Arg1_type
;
138 return binder1st
<_Operation
>(__fn
, _Arg1_type(__x
));
141 /// One of the @link binders binder functors@endlink.
142 template<typename _Operation
>
144 : public unary_function
<typename
_Operation::first_argument_type
,
145 typename
_Operation::result_type
>
149 typename
_Operation::second_argument_type value
;
152 binder2nd(const _Operation
& __x
,
153 const typename
_Operation::second_argument_type
& __y
)
154 : op(__x
), value(__y
) { }
156 typename
_Operation::result_type
157 operator()(const typename
_Operation::first_argument_type
& __x
) const
158 { return op(__x
, value
); }
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
;
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
)
172 typedef typename
_Operation::second_argument_type _Arg2_type
;
173 return binder2nd
<_Operation
>(__fn
, _Arg2_type(__x
));
177 _GLIBCXX_END_NAMESPACE_VERSION
180 #pragma GCC diagnostic pop
182 #endif /* _BACKWARD_BINDERS_H */