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 bits/stl_function.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 _STL_FUNCTION_H
57 #define _STL_FUNCTION_H 1
59 #if __cplusplus > 201103L
60 #include <bits/move.h>
63 namespace std
_GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 // 20.3.1 base classes
68 /** @defgroup functors Function Objects
71 * Function objects, or @e functors, are objects with an @c operator()
72 * defined and accessible. They can be passed as arguments to algorithm
73 * templates and used in place of a function pointer. Not only is the
74 * resulting expressiveness of the library increased, but the generated
75 * code can be more efficient than what you might write by hand. When we
76 * refer to @a functors, then, generally we include function pointers in
77 * the description as well.
79 * Often, functors are only created as temporaries passed to algorithm
80 * calls, rather than being created as named variables.
82 * Two examples taken from the standard itself follow. To perform a
83 * by-element addition of two vectors @c a and @c b containing @c double,
84 * and put the result in @c a, use
86 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
88 * To negate every element in @c a, use
90 * transform(a.begin(), a.end(), a.begin(), negate<double>());
92 * The addition and negation functions will be inlined directly.
94 * The standard functors are derived from structs named @c unary_function
95 * and @c binary_function. These two classes contain nothing but typedefs,
96 * to aid in generic (template) programming. If you write your own
97 * functors, you might consider doing the same.
102 * This is one of the @link functors functor base classes@endlink.
104 template<typename _Arg
, typename _Result
>
105 struct unary_function
107 /// @c argument_type is the type of the argument
108 typedef _Arg argument_type
;
110 /// @c result_type is the return type
111 typedef _Result result_type
;
115 * This is one of the @link functors functor base classes@endlink.
117 template<typename _Arg1
, typename _Arg2
, typename _Result
>
118 struct binary_function
120 /// @c first_argument_type is the type of the first argument
121 typedef _Arg1 first_argument_type
;
123 /// @c second_argument_type is the type of the second argument
124 typedef _Arg2 second_argument_type
;
126 /// @c result_type is the return type
127 typedef _Result result_type
;
132 /** @defgroup arithmetic_functors Arithmetic Classes
135 * Because basic math often needs to be done during an algorithm,
136 * the library provides functors for those operations. See the
137 * documentation for @link functors the base classes@endlink
138 * for examples of their use.
143 #if __cplusplus > 201103L
144 struct __is_transparent
; // undefined
146 template<typename _Tp
= void>
149 template<typename _Tp
= void>
152 template<typename _Tp
= void>
155 template<typename _Tp
= void>
158 template<typename _Tp
= void>
161 template<typename _Tp
= void>
165 /// One of the @link arithmetic_functors math functors@endlink.
166 template<typename _Tp
>
167 struct plus
: public binary_function
<_Tp
, _Tp
, _Tp
>
171 operator()(const _Tp
& __x
, const _Tp
& __y
) const
172 { return __x
+ __y
; }
175 /// One of the @link arithmetic_functors math functors@endlink.
176 template<typename _Tp
>
177 struct minus
: public binary_function
<_Tp
, _Tp
, _Tp
>
181 operator()(const _Tp
& __x
, const _Tp
& __y
) const
182 { return __x
- __y
; }
185 /// One of the @link arithmetic_functors math functors@endlink.
186 template<typename _Tp
>
187 struct multiplies
: public binary_function
<_Tp
, _Tp
, _Tp
>
191 operator()(const _Tp
& __x
, const _Tp
& __y
) const
192 { return __x
* __y
; }
195 /// One of the @link arithmetic_functors math functors@endlink.
196 template<typename _Tp
>
197 struct divides
: public binary_function
<_Tp
, _Tp
, _Tp
>
201 operator()(const _Tp
& __x
, const _Tp
& __y
) const
202 { return __x
/ __y
; }
205 /// One of the @link arithmetic_functors math functors@endlink.
206 template<typename _Tp
>
207 struct modulus
: public binary_function
<_Tp
, _Tp
, _Tp
>
211 operator()(const _Tp
& __x
, const _Tp
& __y
) const
212 { return __x
% __y
; }
215 /// One of the @link arithmetic_functors math functors@endlink.
216 template<typename _Tp
>
217 struct negate
: public unary_function
<_Tp
, _Tp
>
221 operator()(const _Tp
& __x
) const
225 #if __cplusplus > 201103L
227 #define __cpp_lib_transparent_operators 201510
232 template <typename _Tp
, typename _Up
>
235 operator()(_Tp
&& __t
, _Up
&& __u
) const
236 noexcept(noexcept(std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
)))
237 -> decltype(std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
))
238 { return std::forward
<_Tp
>(__t
) + std::forward
<_Up
>(__u
); }
240 typedef __is_transparent is_transparent
;
243 /// One of the @link arithmetic_functors math functors@endlink.
247 template <typename _Tp
, typename _Up
>
250 operator()(_Tp
&& __t
, _Up
&& __u
) const
251 noexcept(noexcept(std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
)))
252 -> decltype(std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
))
253 { return std::forward
<_Tp
>(__t
) - std::forward
<_Up
>(__u
); }
255 typedef __is_transparent is_transparent
;
258 /// One of the @link arithmetic_functors math functors@endlink.
260 struct multiplies
<void>
262 template <typename _Tp
, typename _Up
>
265 operator()(_Tp
&& __t
, _Up
&& __u
) const
266 noexcept(noexcept(std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
)))
267 -> decltype(std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
))
268 { return std::forward
<_Tp
>(__t
) * std::forward
<_Up
>(__u
); }
270 typedef __is_transparent is_transparent
;
273 /// One of the @link arithmetic_functors math functors@endlink.
277 template <typename _Tp
, typename _Up
>
280 operator()(_Tp
&& __t
, _Up
&& __u
) const
281 noexcept(noexcept(std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
)))
282 -> decltype(std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
))
283 { return std::forward
<_Tp
>(__t
) / std::forward
<_Up
>(__u
); }
285 typedef __is_transparent is_transparent
;
288 /// One of the @link arithmetic_functors math functors@endlink.
292 template <typename _Tp
, typename _Up
>
295 operator()(_Tp
&& __t
, _Up
&& __u
) const
296 noexcept(noexcept(std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
)))
297 -> decltype(std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
))
298 { return std::forward
<_Tp
>(__t
) % std::forward
<_Up
>(__u
); }
300 typedef __is_transparent is_transparent
;
303 /// One of the @link arithmetic_functors math functors@endlink.
307 template <typename _Tp
>
310 operator()(_Tp
&& __t
) const
311 noexcept(noexcept(-std::forward
<_Tp
>(__t
)))
312 -> decltype(-std::forward
<_Tp
>(__t
))
313 { return -std::forward
<_Tp
>(__t
); }
315 typedef __is_transparent is_transparent
;
320 // 20.3.3 comparisons
321 /** @defgroup comparison_functors Comparison Classes
324 * The library provides six wrapper functors for all the basic comparisons
329 #if __cplusplus > 201103L
330 template<typename _Tp
= void>
333 template<typename _Tp
= void>
336 template<typename _Tp
= void>
339 template<typename _Tp
= void>
342 template<typename _Tp
= void>
343 struct greater_equal
;
345 template<typename _Tp
= void>
349 /// One of the @link comparison_functors comparison functors@endlink.
350 template<typename _Tp
>
351 struct equal_to
: public binary_function
<_Tp
, _Tp
, bool>
355 operator()(const _Tp
& __x
, const _Tp
& __y
) const
356 { return __x
== __y
; }
359 /// One of the @link comparison_functors comparison functors@endlink.
360 template<typename _Tp
>
361 struct not_equal_to
: public binary_function
<_Tp
, _Tp
, bool>
365 operator()(const _Tp
& __x
, const _Tp
& __y
) const
366 { return __x
!= __y
; }
369 /// One of the @link comparison_functors comparison functors@endlink.
370 template<typename _Tp
>
371 struct greater
: public binary_function
<_Tp
, _Tp
, bool>
375 operator()(const _Tp
& __x
, const _Tp
& __y
) const
376 { return __x
> __y
; }
379 /// One of the @link comparison_functors comparison functors@endlink.
380 template<typename _Tp
>
381 struct less
: public binary_function
<_Tp
, _Tp
, bool>
385 operator()(const _Tp
& __x
, const _Tp
& __y
) const
386 { return __x
< __y
; }
389 /// One of the @link comparison_functors comparison functors@endlink.
390 template<typename _Tp
>
391 struct greater_equal
: public binary_function
<_Tp
, _Tp
, bool>
395 operator()(const _Tp
& __x
, const _Tp
& __y
) const
396 { return __x
>= __y
; }
399 /// One of the @link comparison_functors comparison functors@endlink.
400 template<typename _Tp
>
401 struct less_equal
: public binary_function
<_Tp
, _Tp
, bool>
405 operator()(const _Tp
& __x
, const _Tp
& __y
) const
406 { return __x
<= __y
; }
409 #if __cplusplus > 201103L
410 /// One of the @link comparison_functors comparison functors@endlink.
412 struct equal_to
<void>
414 template <typename _Tp
, typename _Up
>
417 operator()(_Tp
&& __t
, _Up
&& __u
) const
418 noexcept(noexcept(std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
)))
419 -> decltype(std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
))
420 { return std::forward
<_Tp
>(__t
) == std::forward
<_Up
>(__u
); }
422 typedef __is_transparent is_transparent
;
425 /// One of the @link comparison_functors comparison functors@endlink.
427 struct not_equal_to
<void>
429 template <typename _Tp
, typename _Up
>
432 operator()(_Tp
&& __t
, _Up
&& __u
) const
433 noexcept(noexcept(std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
)))
434 -> decltype(std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
))
435 { return std::forward
<_Tp
>(__t
) != std::forward
<_Up
>(__u
); }
437 typedef __is_transparent is_transparent
;
440 /// One of the @link comparison_functors comparison functors@endlink.
444 template <typename _Tp
, typename _Up
>
447 operator()(_Tp
&& __t
, _Up
&& __u
) const
448 noexcept(noexcept(std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
)))
449 -> decltype(std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
))
450 { return std::forward
<_Tp
>(__t
) > std::forward
<_Up
>(__u
); }
452 typedef __is_transparent is_transparent
;
455 /// One of the @link comparison_functors comparison functors@endlink.
459 template <typename _Tp
, typename _Up
>
462 operator()(_Tp
&& __t
, _Up
&& __u
) const
463 noexcept(noexcept(std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
)))
464 -> decltype(std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
))
465 { return std::forward
<_Tp
>(__t
) < std::forward
<_Up
>(__u
); }
467 typedef __is_transparent is_transparent
;
470 /// One of the @link comparison_functors comparison functors@endlink.
472 struct greater_equal
<void>
474 template <typename _Tp
, typename _Up
>
477 operator()(_Tp
&& __t
, _Up
&& __u
) const
478 noexcept(noexcept(std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
)))
479 -> decltype(std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
))
480 { return std::forward
<_Tp
>(__t
) >= std::forward
<_Up
>(__u
); }
482 typedef __is_transparent is_transparent
;
485 /// One of the @link comparison_functors comparison functors@endlink.
487 struct less_equal
<void>
489 template <typename _Tp
, typename _Up
>
492 operator()(_Tp
&& __t
, _Up
&& __u
) const
493 noexcept(noexcept(std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
)))
494 -> decltype(std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
))
495 { return std::forward
<_Tp
>(__t
) <= std::forward
<_Up
>(__u
); }
497 typedef __is_transparent is_transparent
;
502 // 20.3.4 logical operations
503 /** @defgroup logical_functors Boolean Operations Classes
506 * Here are wrapper functors for Boolean operations: @c &&, @c ||,
511 #if __cplusplus > 201103L
512 template<typename _Tp
= void>
515 template<typename _Tp
= void>
518 template<typename _Tp
= void>
522 /// One of the @link logical_functors Boolean operations functors@endlink.
523 template<typename _Tp
>
524 struct logical_and
: public binary_function
<_Tp
, _Tp
, bool>
528 operator()(const _Tp
& __x
, const _Tp
& __y
) const
529 { return __x
&& __y
; }
532 /// One of the @link logical_functors Boolean operations functors@endlink.
533 template<typename _Tp
>
534 struct logical_or
: public binary_function
<_Tp
, _Tp
, bool>
538 operator()(const _Tp
& __x
, const _Tp
& __y
) const
539 { return __x
|| __y
; }
542 /// One of the @link logical_functors Boolean operations functors@endlink.
543 template<typename _Tp
>
544 struct logical_not
: public unary_function
<_Tp
, bool>
548 operator()(const _Tp
& __x
) const
552 #if __cplusplus > 201103L
553 /// One of the @link logical_functors Boolean operations functors@endlink.
555 struct logical_and
<void>
557 template <typename _Tp
, typename _Up
>
560 operator()(_Tp
&& __t
, _Up
&& __u
) const
561 noexcept(noexcept(std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
)))
562 -> decltype(std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
))
563 { return std::forward
<_Tp
>(__t
) && std::forward
<_Up
>(__u
); }
565 typedef __is_transparent is_transparent
;
568 /// One of the @link logical_functors Boolean operations functors@endlink.
570 struct logical_or
<void>
572 template <typename _Tp
, typename _Up
>
575 operator()(_Tp
&& __t
, _Up
&& __u
) const
576 noexcept(noexcept(std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
)))
577 -> decltype(std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
))
578 { return std::forward
<_Tp
>(__t
) || std::forward
<_Up
>(__u
); }
580 typedef __is_transparent is_transparent
;
583 /// One of the @link logical_functors Boolean operations functors@endlink.
585 struct logical_not
<void>
587 template <typename _Tp
>
590 operator()(_Tp
&& __t
) const
591 noexcept(noexcept(!std::forward
<_Tp
>(__t
)))
592 -> decltype(!std::forward
<_Tp
>(__t
))
593 { return !std::forward
<_Tp
>(__t
); }
595 typedef __is_transparent is_transparent
;
600 #if __cplusplus > 201103L
601 template<typename _Tp
= void>
604 template<typename _Tp
= void>
607 template<typename _Tp
= void>
610 template<typename _Tp
= void>
614 // _GLIBCXX_RESOLVE_LIB_DEFECTS
615 // DR 660. Missing Bitwise Operations.
616 template<typename _Tp
>
617 struct bit_and
: public binary_function
<_Tp
, _Tp
, _Tp
>
621 operator()(const _Tp
& __x
, const _Tp
& __y
) const
622 { return __x
& __y
; }
625 template<typename _Tp
>
626 struct bit_or
: public binary_function
<_Tp
, _Tp
, _Tp
>
630 operator()(const _Tp
& __x
, const _Tp
& __y
) const
631 { return __x
| __y
; }
634 template<typename _Tp
>
635 struct bit_xor
: public binary_function
<_Tp
, _Tp
, _Tp
>
639 operator()(const _Tp
& __x
, const _Tp
& __y
) const
640 { return __x
^ __y
; }
643 template<typename _Tp
>
644 struct bit_not
: public unary_function
<_Tp
, _Tp
>
648 operator()(const _Tp
& __x
) const
652 #if __cplusplus > 201103L
656 template <typename _Tp
, typename _Up
>
659 operator()(_Tp
&& __t
, _Up
&& __u
) const
660 noexcept(noexcept(std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
)))
661 -> decltype(std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
))
662 { return std::forward
<_Tp
>(__t
) & std::forward
<_Up
>(__u
); }
664 typedef __is_transparent is_transparent
;
670 template <typename _Tp
, typename _Up
>
673 operator()(_Tp
&& __t
, _Up
&& __u
) const
674 noexcept(noexcept(std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
)))
675 -> decltype(std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
))
676 { return std::forward
<_Tp
>(__t
) | std::forward
<_Up
>(__u
); }
678 typedef __is_transparent is_transparent
;
684 template <typename _Tp
, typename _Up
>
687 operator()(_Tp
&& __t
, _Up
&& __u
) const
688 noexcept(noexcept(std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
)))
689 -> decltype(std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
))
690 { return std::forward
<_Tp
>(__t
) ^ std::forward
<_Up
>(__u
); }
692 typedef __is_transparent is_transparent
;
698 template <typename _Tp
>
701 operator()(_Tp
&& __t
) const
702 noexcept(noexcept(~std::forward
<_Tp
>(__t
)))
703 -> decltype(~std::forward
<_Tp
>(__t
))
704 { return ~std::forward
<_Tp
>(__t
); }
706 typedef __is_transparent is_transparent
;
711 /** @defgroup negators Negators
714 * The functions @c not1 and @c not2 each take a predicate functor
715 * and return an instance of @c unary_negate or
716 * @c binary_negate, respectively. These classes are functors whose
717 * @c operator() performs the stored predicate function and then returns
718 * the negation of the result.
720 * For example, given a vector of integers and a trivial predicate,
722 * struct IntGreaterThanThree
723 * : public std::unary_function<int, bool>
725 * bool operator() (int x) { return x > 3; }
728 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
730 * The call to @c find_if will locate the first index (i) of @c v for which
731 * <code>!(v[i] > 3)</code> is true.
733 * The not1/unary_negate combination works on predicates taking a single
734 * argument. The not2/binary_negate combination works on predicates which
735 * take two arguments.
739 /// One of the @link negators negation functors@endlink.
740 template<typename _Predicate
>
742 : public unary_function
<typename
_Predicate::argument_type
, bool>
750 unary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
754 operator()(const typename
_Predicate::argument_type
& __x
) const
755 { return !_M_pred(__x
); }
758 /// One of the @link negators negation functors@endlink.
759 template<typename _Predicate
>
761 inline unary_negate
<_Predicate
>
762 not1(const _Predicate
& __pred
)
763 { return unary_negate
<_Predicate
>(__pred
); }
765 /// One of the @link negators negation functors@endlink.
766 template<typename _Predicate
>
768 : public binary_function
<typename
_Predicate::first_argument_type
,
769 typename
_Predicate::second_argument_type
, bool>
777 binary_negate(const _Predicate
& __x
) : _M_pred(__x
) { }
781 operator()(const typename
_Predicate::first_argument_type
& __x
,
782 const typename
_Predicate::second_argument_type
& __y
) const
783 { return !_M_pred(__x
, __y
); }
786 /// One of the @link negators negation functors@endlink.
787 template<typename _Predicate
>
789 inline binary_negate
<_Predicate
>
790 not2(const _Predicate
& __pred
)
791 { return binary_negate
<_Predicate
>(__pred
); }
794 // 20.3.7 adaptors pointers functions
795 /** @defgroup pointer_adaptors Adaptors for pointers to functions
798 * The advantage of function objects over pointers to functions is that
799 * the objects in the standard library declare nested typedefs describing
800 * their argument and result types with uniform names (e.g., @c result_type
801 * from the base classes @c unary_function and @c binary_function).
802 * Sometimes those typedefs are required, not just optional.
804 * Adaptors are provided to turn pointers to unary (single-argument) and
805 * binary (double-argument) functions into function objects. The
806 * long-winded functor @c pointer_to_unary_function is constructed with a
807 * function pointer @c f, and its @c operator() called with argument @c x
808 * returns @c f(x). The functor @c pointer_to_binary_function does the same
809 * thing, but with a double-argument @c f and @c operator().
811 * The function @c ptr_fun takes a pointer-to-function @c f and constructs
812 * an instance of the appropriate functor.
816 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
817 template<typename _Arg
, typename _Result
>
818 class pointer_to_unary_function
: public unary_function
<_Arg
, _Result
>
821 _Result (*_M_ptr
)(_Arg
);
824 pointer_to_unary_function() { }
827 pointer_to_unary_function(_Result (*__x
)(_Arg
))
831 operator()(_Arg __x
) const
832 { return _M_ptr(__x
); }
835 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
836 template<typename _Arg
, typename _Result
>
837 inline pointer_to_unary_function
<_Arg
, _Result
>
838 ptr_fun(_Result (*__x
)(_Arg
))
839 { return pointer_to_unary_function
<_Arg
, _Result
>(__x
); }
841 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
842 template<typename _Arg1
, typename _Arg2
, typename _Result
>
843 class pointer_to_binary_function
844 : public binary_function
<_Arg1
, _Arg2
, _Result
>
847 _Result (*_M_ptr
)(_Arg1
, _Arg2
);
850 pointer_to_binary_function() { }
853 pointer_to_binary_function(_Result (*__x
)(_Arg1
, _Arg2
))
857 operator()(_Arg1 __x
, _Arg2 __y
) const
858 { return _M_ptr(__x
, __y
); }
861 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
862 template<typename _Arg1
, typename _Arg2
, typename _Result
>
863 inline pointer_to_binary_function
<_Arg1
, _Arg2
, _Result
>
864 ptr_fun(_Result (*__x
)(_Arg1
, _Arg2
))
865 { return pointer_to_binary_function
<_Arg1
, _Arg2
, _Result
>(__x
); }
868 template<typename _Tp
>
870 : public unary_function
<_Tp
, _Tp
>
873 operator()(_Tp
& __x
) const
877 operator()(const _Tp
& __x
) const
881 // Partial specialization, avoids confusing errors in e.g. std::set<const T>.
882 template<typename _Tp
> struct _Identity
<const _Tp
> : _Identity
<_Tp
> { };
884 template<typename _Pair
>
886 : public unary_function
<_Pair
, typename
_Pair::first_type
>
888 typename
_Pair::first_type
&
889 operator()(_Pair
& __x
) const
890 { return __x
.first
; }
892 const typename
_Pair::first_type
&
893 operator()(const _Pair
& __x
) const
894 { return __x
.first
; }
896 #if __cplusplus >= 201103L
897 template<typename _Pair2
>
898 typename
_Pair2::first_type
&
899 operator()(_Pair2
& __x
) const
900 { return __x
.first
; }
902 template<typename _Pair2
>
903 const typename
_Pair2::first_type
&
904 operator()(const _Pair2
& __x
) const
905 { return __x
.first
; }
909 template<typename _Pair
>
911 : public unary_function
<_Pair
, typename
_Pair::second_type
>
913 typename
_Pair::second_type
&
914 operator()(_Pair
& __x
) const
915 { return __x
.second
; }
917 const typename
_Pair::second_type
&
918 operator()(const _Pair
& __x
) const
919 { return __x
.second
; }
922 // 20.3.8 adaptors pointers members
923 /** @defgroup memory_adaptors Adaptors for pointers to members
926 * There are a total of 8 = 2^3 function objects in this family.
927 * (1) Member functions taking no arguments vs member functions taking
929 * (2) Call through pointer vs call through reference.
930 * (3) Const vs non-const member function.
932 * All of this complexity is in the function objects themselves. You can
933 * ignore it by using the helper function mem_fun and mem_fun_ref,
934 * which create whichever type of adaptor is appropriate.
938 /// One of the @link memory_adaptors adaptors for member
939 /// pointers@endlink.
940 template<typename _Ret
, typename _Tp
>
941 class mem_fun_t
: public unary_function
<_Tp
*, _Ret
>
945 mem_fun_t(_Ret (_Tp::*__pf
)())
949 operator()(_Tp
* __p
) const
950 { return (__p
->*_M_f
)(); }
956 /// One of the @link memory_adaptors adaptors for member
957 /// pointers@endlink.
958 template<typename _Ret
, typename _Tp
>
959 class const_mem_fun_t
: public unary_function
<const _Tp
*, _Ret
>
963 const_mem_fun_t(_Ret (_Tp::*__pf
)() const)
967 operator()(const _Tp
* __p
) const
968 { return (__p
->*_M_f
)(); }
971 _Ret (_Tp::*_M_f
)() const;
974 /// One of the @link memory_adaptors adaptors for member
975 /// pointers@endlink.
976 template<typename _Ret
, typename _Tp
>
977 class mem_fun_ref_t
: public unary_function
<_Tp
, _Ret
>
981 mem_fun_ref_t(_Ret (_Tp::*__pf
)())
985 operator()(_Tp
& __r
) const
986 { return (__r
.*_M_f
)(); }
992 /// One of the @link memory_adaptors adaptors for member
993 /// pointers@endlink.
994 template<typename _Ret
, typename _Tp
>
995 class const_mem_fun_ref_t
: public unary_function
<_Tp
, _Ret
>
999 const_mem_fun_ref_t(_Ret (_Tp::*__pf
)() const)
1003 operator()(const _Tp
& __r
) const
1004 { return (__r
.*_M_f
)(); }
1007 _Ret (_Tp::*_M_f
)() const;
1010 /// One of the @link memory_adaptors adaptors for member
1011 /// pointers@endlink.
1012 template<typename _Ret
, typename _Tp
, typename _Arg
>
1013 class mem_fun1_t
: public binary_function
<_Tp
*, _Arg
, _Ret
>
1017 mem_fun1_t(_Ret (_Tp::*__pf
)(_Arg
))
1021 operator()(_Tp
* __p
, _Arg __x
) const
1022 { return (__p
->*_M_f
)(__x
); }
1025 _Ret (_Tp::*_M_f
)(_Arg
);
1028 /// One of the @link memory_adaptors adaptors for member
1029 /// pointers@endlink.
1030 template<typename _Ret
, typename _Tp
, typename _Arg
>
1031 class const_mem_fun1_t
: public binary_function
<const _Tp
*, _Arg
, _Ret
>
1035 const_mem_fun1_t(_Ret (_Tp::*__pf
)(_Arg
) const)
1039 operator()(const _Tp
* __p
, _Arg __x
) const
1040 { return (__p
->*_M_f
)(__x
); }
1043 _Ret (_Tp::*_M_f
)(_Arg
) const;
1046 /// One of the @link memory_adaptors adaptors for member
1047 /// pointers@endlink.
1048 template<typename _Ret
, typename _Tp
, typename _Arg
>
1049 class mem_fun1_ref_t
: public binary_function
<_Tp
, _Arg
, _Ret
>
1053 mem_fun1_ref_t(_Ret (_Tp::*__pf
)(_Arg
))
1057 operator()(_Tp
& __r
, _Arg __x
) const
1058 { return (__r
.*_M_f
)(__x
); }
1061 _Ret (_Tp::*_M_f
)(_Arg
);
1064 /// One of the @link memory_adaptors adaptors for member
1065 /// pointers@endlink.
1066 template<typename _Ret
, typename _Tp
, typename _Arg
>
1067 class const_mem_fun1_ref_t
: public binary_function
<_Tp
, _Arg
, _Ret
>
1071 const_mem_fun1_ref_t(_Ret (_Tp::*__pf
)(_Arg
) const)
1075 operator()(const _Tp
& __r
, _Arg __x
) const
1076 { return (__r
.*_M_f
)(__x
); }
1079 _Ret (_Tp::*_M_f
)(_Arg
) const;
1082 // Mem_fun adaptor helper functions. There are only two:
1083 // mem_fun and mem_fun_ref.
1084 template<typename _Ret
, typename _Tp
>
1085 inline mem_fun_t
<_Ret
, _Tp
>
1086 mem_fun(_Ret (_Tp::*__f
)())
1087 { return mem_fun_t
<_Ret
, _Tp
>(__f
); }
1089 template<typename _Ret
, typename _Tp
>
1090 inline const_mem_fun_t
<_Ret
, _Tp
>
1091 mem_fun(_Ret (_Tp::*__f
)() const)
1092 { return const_mem_fun_t
<_Ret
, _Tp
>(__f
); }
1094 template<typename _Ret
, typename _Tp
>
1095 inline mem_fun_ref_t
<_Ret
, _Tp
>
1096 mem_fun_ref(_Ret (_Tp::*__f
)())
1097 { return mem_fun_ref_t
<_Ret
, _Tp
>(__f
); }
1099 template<typename _Ret
, typename _Tp
>
1100 inline const_mem_fun_ref_t
<_Ret
, _Tp
>
1101 mem_fun_ref(_Ret (_Tp::*__f
)() const)
1102 { return const_mem_fun_ref_t
<_Ret
, _Tp
>(__f
); }
1104 template<typename _Ret
, typename _Tp
, typename _Arg
>
1105 inline mem_fun1_t
<_Ret
, _Tp
, _Arg
>
1106 mem_fun(_Ret (_Tp::*__f
)(_Arg
))
1107 { return mem_fun1_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1109 template<typename _Ret
, typename _Tp
, typename _Arg
>
1110 inline const_mem_fun1_t
<_Ret
, _Tp
, _Arg
>
1111 mem_fun(_Ret (_Tp::*__f
)(_Arg
) const)
1112 { return const_mem_fun1_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1114 template<typename _Ret
, typename _Tp
, typename _Arg
>
1115 inline mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>
1116 mem_fun_ref(_Ret (_Tp::*__f
)(_Arg
))
1117 { return mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1119 template<typename _Ret
, typename _Tp
, typename _Arg
>
1120 inline const_mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>
1121 mem_fun_ref(_Ret (_Tp::*__f
)(_Arg
) const)
1122 { return const_mem_fun1_ref_t
<_Ret
, _Tp
, _Arg
>(__f
); }
1126 _GLIBCXX_END_NAMESPACE_VERSION
1129 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1130 # include <backward/binders.h>
1133 #endif /* _STL_FUNCTION_H */