1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010
4 // Free Software Foundation, Inc.
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)
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.
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.
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/>.
29 * Hewlett-Packard Company
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.
40 * Copyright (c) 1996-1998
41 * Silicon Graphics Computer Systems, Inc.
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.
52 /** @file bits/stl_function.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{functional}
57 #ifndef _STL_FUNCTION_H
58 #define _STL_FUNCTION_H 1
60 _GLIBCXX_BEGIN_NAMESPACE(std
)
62 // 20.3.1 base classes
63 /** @defgroup functors Function Objects
66 * Function objects, or @e functors, are objects with an @c operator()
67 * defined and accessible. They can be passed as arguments to algorithm
68 * templates and used in place of a function pointer. Not only is the
69 * resulting expressiveness of the library increased, but the generated
70 * code can be more efficient than what you might write by hand. When we
71 * refer to @a functors, then, generally we include function pointers in
72 * the description as well.
74 * Often, functors are only created as temporaries passed to algorithm
75 * calls, rather than being created as named variables.
77 * Two examples taken from the standard itself follow. To perform a
78 * by-element addition of two vectors @c a and @c b containing @c double,
79 * and put the result in @c a, use
81 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
83 * To negate every element in @c a, use
85 * transform(a.begin(), a.end(), a.begin(), negate<double>());
87 * The addition and negation functions will be inlined directly.
89 * The standard functors are derived from structs named @c unary_function
90 * and @c binary_function. These two classes contain nothing but typedefs,
91 * to aid in generic (template) programming. If you write your own
92 * functors, you might consider doing the same.
97 * This is one of the @link functors functor base classes@endlink.
99 template<typename _Arg
, typename _Result
>
100 struct unary_function
102 /// @c argument_type is the type of the argument
103 typedef _Arg argument_type
;
105 /// @c result_type is the return type
106 typedef _Result result_type
;
110 * This is one of the @link functors functor base classes@endlink.
112 template<typename _Arg1
, typename _Arg2
, typename _Result
>
113 struct binary_function
115 /// @c first_argument_type is the type of the first argument
116 typedef _Arg1 first_argument_type
;
118 /// @c second_argument_type is the type of the second argument
119 typedef _Arg2 second_argument_type
;
121 /// @c result_type is the return type
122 typedef _Result result_type
;
127 /** @defgroup arithmetic_functors Arithmetic Classes
130 * Because basic math often needs to be done during an algorithm,
131 * the library provides functors for those operations. See the
132 * documentation for @link functors the base classes@endlink
133 * for examples of their use.
137 /// One of the @link arithmetic_functors math functors@endlink.
138 template<typename _Tp
>
139 struct plus
: public binary_function
<_Tp
, _Tp
, _Tp
>
142 operator()(const _Tp
& __x
, const _Tp
& __y
) const
143 { return __x
+ __y
; }
146 /// One of the @link arithmetic_functors math functors@endlink.
147 template<typename _Tp
>
148 struct minus
: public binary_function
<_Tp
, _Tp
, _Tp
>
151 operator()(const _Tp
& __x
, const _Tp
& __y
) const
152 { return __x
- __y
; }
155 /// One of the @link arithmetic_functors math functors@endlink.
156 template<typename _Tp
>
157 struct multiplies
: public binary_function
<_Tp
, _Tp
, _Tp
>
160 operator()(const _Tp
& __x
, const _Tp
& __y
) const
161 { return __x
* __y
; }
164 /// One of the @link arithmetic_functors math functors@endlink.
165 template<typename _Tp
>
166 struct divides
: public binary_function
<_Tp
, _Tp
, _Tp
>
169 operator()(const _Tp
& __x
, const _Tp
& __y
) const
170 { return __x
/ __y
; }
173 /// One of the @link arithmetic_functors math functors@endlink.
174 template<typename _Tp
>
175 struct modulus
: public binary_function
<_Tp
, _Tp
, _Tp
>
178 operator()(const _Tp
& __x
, const _Tp
& __y
) const
179 { return __x
% __y
; }
182 /// One of the @link arithmetic_functors math functors@endlink.
183 template<typename _Tp
>
184 struct negate
: public unary_function
<_Tp
, _Tp
>
187 operator()(const _Tp
& __x
) const
192 // 20.3.3 comparisons
193 /** @defgroup comparison_functors Comparison Classes
196 * The library provides six wrapper functors for all the basic comparisons
201 /// One of the @link comparison_functors comparison functors@endlink.
202 template<typename _Tp
>
203 struct equal_to
: public binary_function
<_Tp
, _Tp
, bool>
206 operator()(const _Tp
& __x
, const _Tp
& __y
) const
207 { return __x
== __y
; }
210 /// One of the @link comparison_functors comparison functors@endlink.
211 template<typename _Tp
>
212 struct not_equal_to
: public binary_function
<_Tp
, _Tp
, bool>
215 operator()(const _Tp
& __x
, const _Tp
& __y
) const
216 { return __x
!= __y
; }
219 /// One of the @link comparison_functors comparison functors@endlink.
220 template<typename _Tp
>
221 struct greater
: public binary_function
<_Tp
, _Tp
, bool>
224 operator()(const _Tp
& __x
, const _Tp
& __y
) const
225 { return __x
> __y
; }
228 /// One of the @link comparison_functors comparison functors@endlink.
229 template<typename _Tp
>
230 struct less
: public binary_function
<_Tp
, _Tp
, bool>
233 operator()(const _Tp
& __x
, const _Tp
& __y
) const
234 { return __x
< __y
; }
237 /// One of the @link comparison_functors comparison functors@endlink.
238 template<typename _Tp
>
239 struct greater_equal
: public binary_function
<_Tp
, _Tp
, bool>
242 operator()(const _Tp
& __x
, const _Tp
& __y
) const
243 { return __x
>= __y
; }
246 /// One of the @link comparison_functors comparison functors@endlink.
247 template<typename _Tp
>
248 struct less_equal
: public binary_function
<_Tp
, _Tp
, bool>
251 operator()(const _Tp
& __x
, const _Tp
& __y
) const
252 { return __x
<= __y
; }
256 // 20.3.4 logical operations
257 /** @defgroup logical_functors Boolean Operations Classes
260 * Here are wrapper functors for Boolean operations: @c &&, @c ||,
265 /// One of the @link logical_functors Boolean operations functors@endlink.
266 template<typename _Tp
>
267 struct logical_and
: public binary_function
<_Tp
, _Tp
, bool>
270 operator()(const _Tp
& __x
, const _Tp
& __y
) const
271 { return __x
&& __y
; }
274 /// One of the @link logical_functors Boolean operations functors@endlink.
275 template<typename _Tp
>
276 struct logical_or
: public binary_function
<_Tp
, _Tp
, bool>
279 operator()(const _Tp
& __x
, const _Tp
& __y
) const
280 { return __x
|| __y
; }
283 /// One of the @link logical_functors Boolean operations functors@endlink.
284 template<typename _Tp
>
285 struct logical_not
: public unary_function
<_Tp
, bool>
288 operator()(const _Tp
& __x
) const
293 // _GLIBCXX_RESOLVE_LIB_DEFECTS
294 // DR 660. Missing Bitwise Operations.
295 template<typename _Tp
>
296 struct bit_and
: public binary_function
<_Tp
, _Tp
, _Tp
>
299 operator()(const _Tp
& __x
, const _Tp
& __y
) const
300 { return __x
& __y
; }
303 template<typename _Tp
>
304 struct bit_or
: public binary_function
<_Tp
, _Tp
, _Tp
>
307 operator()(const _Tp
& __x
, const _Tp
& __y
) const
308 { return __x
| __y
; }
311 template<typename _Tp
>
312 struct bit_xor
: public binary_function
<_Tp
, _Tp
, _Tp
>
315 operator()(const _Tp
& __x
, const _Tp
& __y
) const
316 { return __x
^ __y
; }
320 /** @defgroup negators Negators
323 * The functions @c not1 and @c not2 each take a predicate functor
324 * and return an instance of @c unary_negate or
325 * @c binary_negate, respectively. These classes are functors whose
326 * @c operator() performs the stored predicate function and then returns
327 * the negation of the result.
329 * For example, given a vector of integers and a trivial predicate,
331 * struct IntGreaterThanThree
332 * : public std::unary_function<int, bool>
334 * bool operator() (int x) { return x > 3; }
337 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
339 * The call to @c find_if will locate the first index (i) of @c v for which
340 * <code>!(v[i] > 3)</code> is true.
342 * The not1/unary_negate combination works on predicates taking a single
343 * argument. The not2/binary_negate combination works on predicates which
344 * take two arguments.
348 /// One of the @link negators negation functors@endlink.
349 template<typename _Predicate
>
351 : public unary_function
<typename
_Predicate::argument_type
, bool>
358 unary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
361 operator()(const typename
_Predicate::argument_type
& __x
) const
362 { return !_M_pred(__x
); }
365 /// One of the @link negators negation functors@endlink.
366 template<typename _Predicate
>
367 inline unary_negate
<_Predicate
>
368 not1(const _Predicate
& __pred
)
369 { return unary_negate
<_Predicate
>(__pred
); }
371 /// One of the @link negators negation functors@endlink.
372 template<typename _Predicate
>
374 : public binary_function
<typename
_Predicate::first_argument_type
,
375 typename
_Predicate::second_argument_type
, bool>
382 binary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
385 operator()(const typename
_Predicate::first_argument_type
& __x
,
386 const typename
_Predicate::second_argument_type
& __y
) const
387 { return !_M_pred(__x
, __y
); }
390 /// One of the @link negators negation functors@endlink.
391 template<typename _Predicate
>
392 inline binary_negate
<_Predicate
>
393 not2(const _Predicate
& __pred
)
394 { return binary_negate
<_Predicate
>(__pred
); }
397 // 20.3.7 adaptors pointers functions
398 /** @defgroup pointer_adaptors Adaptors for pointers to functions
401 * The advantage of function objects over pointers to functions is that
402 * the objects in the standard library declare nested typedefs describing
403 * their argument and result types with uniform names (e.g., @c result_type
404 * from the base classes @c unary_function and @c binary_function).
405 * Sometimes those typedefs are required, not just optional.
407 * Adaptors are provided to turn pointers to unary (single-argument) and
408 * binary (double-argument) functions into function objects. The
409 * long-winded functor @c pointer_to_unary_function is constructed with a
410 * function pointer @c f, and its @c operator() called with argument @c x
411 * returns @c f(x). The functor @c pointer_to_binary_function does the same
412 * thing, but with a double-argument @c f and @c operator().
414 * The function @c ptr_fun takes a pointer-to-function @c f and constructs
415 * an instance of the appropriate functor.
419 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
420 template<typename _Arg
, typename _Result
>
421 class pointer_to_unary_function
: public unary_function
<_Arg
, _Result
>
424 _Result (*_M_ptr
)(_Arg
);
427 pointer_to_unary_function() { }
430 pointer_to_unary_function(_Result (*__x
)(_Arg
))
434 operator()(_Arg __x
) const
435 { return _M_ptr(__x
); }
438 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
439 template<typename _Arg
, typename _Result
>
440 inline pointer_to_unary_function
<_Arg
, _Result
>
441 ptr_fun(_Result (*__x
)(_Arg
))
442 { return pointer_to_unary_function
<_Arg
, _Result
>(__x
); }
444 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
445 template<typename _Arg1
, typename _Arg2
, typename _Result
>
446 class pointer_to_binary_function
447 : public binary_function
<_Arg1
, _Arg2
, _Result
>
450 _Result (*_M_ptr
)(_Arg1
, _Arg2
);
453 pointer_to_binary_function() { }
456 pointer_to_binary_function(_Result (*__x
)(_Arg1
, _Arg2
))
460 operator()(_Arg1 __x
, _Arg2 __y
) const
461 { return _M_ptr(__x
, __y
); }
464 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
465 template<typename _Arg1
, typename _Arg2
, typename _Result
>
466 inline pointer_to_binary_function
<_Arg1
, _Arg2
, _Result
>
467 ptr_fun(_Result (*__x
)(_Arg1
, _Arg2
))
468 { return pointer_to_binary_function
<_Arg1
, _Arg2
, _Result
>(__x
); }
471 template<typename _Tp
>
472 struct _Identity
: public unary_function
<_Tp
,_Tp
>
475 operator()(_Tp
& __x
) const
479 operator()(const _Tp
& __x
) const
483 template<typename _Pair
>
484 struct _Select1st
: public unary_function
<_Pair
,
485 typename
_Pair::first_type
>
487 typename
_Pair::first_type
&
488 operator()(_Pair
& __x
) const
489 { return __x
.first
; }
491 const typename
_Pair::first_type
&
492 operator()(const _Pair
& __x
) const
493 { return __x
.first
; }
495 #ifdef __GXX_EXPERIMENTAL_CXX0X__
496 template<typename _Pair2
>
497 typename
_Pair2::first_type
&
498 operator()(_Pair2
& __x
) const
499 { return __x
.first
; }
501 template<typename _Pair2
>
502 const typename
_Pair2::first_type
&
503 operator()(const _Pair2
& __x
) const
504 { return __x
.first
; }
508 template<typename _Pair
>
509 struct _Select2nd
: public unary_function
<_Pair
,
510 typename
_Pair::second_type
>
512 typename
_Pair::second_type
&
513 operator()(_Pair
& __x
) const
514 { return __x
.second
; }
516 const typename
_Pair::second_type
&
517 operator()(const _Pair
& __x
) const
518 { return __x
.second
; }
521 // 20.3.8 adaptors pointers members
522 /** @defgroup memory_adaptors Adaptors for pointers to members
525 * There are a total of 8 = 2^3 function objects in this family.
526 * (1) Member functions taking no arguments vs member functions taking
528 * (2) Call through pointer vs call through reference.
529 * (3) Const vs non-const member function.
531 * All of this complexity is in the function objects themselves. You can
532 * ignore it by using the helper function mem_fun and mem_fun_ref,
533 * which create whichever type of adaptor is appropriate.
537 /// One of the @link memory_adaptors adaptors for member
538 /// pointers@endlink.
539 template<typename _Ret
, typename _Tp
>
540 class mem_fun_t
: public unary_function
<_Tp
*, _Ret
>
544 mem_fun_t(_Ret (_Tp::*__pf
)())
548 operator()(_Tp
* __p
) const
549 { return (__p
->*_M_f
)(); }
555 /// One of the @link memory_adaptors adaptors for member
556 /// pointers@endlink.
557 template<typename _Ret
, typename _Tp
>
558 class const_mem_fun_t
: public unary_function
<const _Tp
*, _Ret
>
562 const_mem_fun_t(_Ret (_Tp::*__pf
)() const)
566 operator()(const _Tp
* __p
) const
567 { return (__p
->*_M_f
)(); }
570 _Ret (_Tp::*_M_f
)() const;
573 /// One of the @link memory_adaptors adaptors for member
574 /// pointers@endlink.
575 template<typename _Ret
, typename _Tp
>
576 class mem_fun_ref_t
: public unary_function
<_Tp
, _Ret
>
580 mem_fun_ref_t(_Ret (_Tp::*__pf
)())
584 operator()(_Tp
& __r
) const
585 { return (__r
.*_M_f
)(); }
591 /// One of the @link memory_adaptors adaptors for member
592 /// pointers@endlink.
593 template<typename _Ret
, typename _Tp
>
594 class const_mem_fun_ref_t
: public unary_function
<_Tp
, _Ret
>
598 const_mem_fun_ref_t(_Ret (_Tp::*__pf
)() const)
602 operator()(const _Tp
& __r
) const
603 { return (__r
.*_M_f
)(); }
606 _Ret (_Tp::*_M_f
)() const;
609 /// One of the @link memory_adaptors adaptors for member
610 /// pointers@endlink.
611 template<typename _Ret
, typename _Tp
, typename _Arg
>
612 class mem_fun1_t
: public binary_function
<_Tp
*, _Arg
, _Ret
>
616 mem_fun1_t(_Ret (_Tp::*__pf
)(_Arg
))
620 operator()(_Tp
* __p
, _Arg __x
) const
621 { return (__p
->*_M_f
)(__x
); }
624 _Ret (_Tp::*_M_f
)(_Arg
);
627 /// One of the @link memory_adaptors adaptors for member
628 /// pointers@endlink.
629 template<typename _Ret
, typename _Tp
, typename _Arg
>
630 class const_mem_fun1_t
: public binary_function
<const _Tp
*, _Arg
, _Ret
>
634 const_mem_fun1_t(_Ret (_Tp::*__pf
)(_Arg
) const)
638 operator()(const _Tp
* __p
, _Arg __x
) const
639 { return (__p
->*_M_f
)(__x
); }
642 _Ret (_Tp::*_M_f
)(_Arg
) const;
645 /// One of the @link memory_adaptors adaptors for member
646 /// pointers@endlink.
647 template<typename _Ret
, typename _Tp
, typename _Arg
>
648 class mem_fun1_ref_t
: public binary_function
<_Tp
, _Arg
, _Ret
>
652 mem_fun1_ref_t(_Ret (_Tp::*__pf
)(_Arg
))
656 operator()(_Tp
& __r
, _Arg __x
) const
657 { return (__r
.*_M_f
)(__x
); }
660 _Ret (_Tp::*_M_f
)(_Arg
);
663 /// One of the @link memory_adaptors adaptors for member
664 /// pointers@endlink.
665 template<typename _Ret
, typename _Tp
, typename _Arg
>
666 class const_mem_fun1_ref_t
: public binary_function
<_Tp
, _Arg
, _Ret
>
670 const_mem_fun1_ref_t(_Ret (_Tp::*__pf
)(_Arg
) const)
674 operator()(const _Tp
& __r
, _Arg __x
) const
675 { return (__r
.*_M_f
)(__x
); }
678 _Ret (_Tp::*_M_f
)(_Arg
) const;
681 // Mem_fun adaptor helper functions. There are only two:
682 // mem_fun and mem_fun_ref.
683 template<typename _Ret
, typename _Tp
>
684 inline mem_fun_t
<_Ret
, _Tp
>
685 mem_fun(_Ret (_Tp::*__f
)())
686 { return mem_fun_t
<_Ret
, _Tp
>(__f
); }
688 template<typename _Ret
, typename _Tp
>
689 inline const_mem_fun_t
<_Ret
, _Tp
>
690 mem_fun(_Ret (_Tp::*__f
)() const)
691 { return const_mem_fun_t
<_Ret
, _Tp
>(__f
); }
693 template<typename _Ret
, typename _Tp
>
694 inline mem_fun_ref_t
<_Ret
, _Tp
>
695 mem_fun_ref(_Ret (_Tp::*__f
)())
696 { return mem_fun_ref_t
<_Ret
, _Tp
>(__f
); }
698 template<typename _Ret
, typename _Tp
>
699 inline const_mem_fun_ref_t
<_Ret
, _Tp
>
700 mem_fun_ref(_Ret (_Tp::*__f
)() const)
701 { return const_mem_fun_ref_t
<_Ret
, _Tp
>(__f
); }
703 template<typename _Ret
, typename _Tp
, typename _Arg
>
704 inline mem_fun1_t
<_Ret
, _Tp
, _Arg
>
705 mem_fun(_Ret (_Tp::*__f
)(_Arg
))
706 { return mem_fun1_t
<_Ret
, _Tp
, _Arg
>(__f
); }
708 template<typename _Ret
, typename _Tp
, typename _Arg
>
709 inline const_mem_fun1_t
<_Ret
, _Tp
, _Arg
>
710 mem_fun(_Ret (_Tp::*__f
)(_Arg
) const)
711 { return const_mem_fun1_t
<_Ret
, _Tp
, _Arg
>(__f
); }
713 template<typename _Ret
, typename _Tp
, typename _Arg
>
714 inline mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>
715 mem_fun_ref(_Ret (_Tp::*__f
)(_Arg
))
716 { return mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>(__f
); }
718 template<typename _Ret
, typename _Tp
, typename _Arg
>
719 inline const_mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>
720 mem_fun_ref(_Ret (_Tp::*__f
)(_Arg
) const)
721 { return const_mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>(__f
); }
725 _GLIBCXX_END_NAMESPACE
727 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_DEPRECATED
728 # include <backward/binders.h>
731 #endif /* _STL_FUNCTION_H */