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