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