RISC-V: Add --specs=nosys.specs support.
[official-gcc.git] / libstdc++-v3 / include / bits / stl_function.h
blobf5f98b253957265b04b87310d140f9b96b09f939
1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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/>.
27 * Copyright (c) 1994
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>
61 #endif
63 namespace std _GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 // 20.3.1 base classes
68 /** @defgroup functors Function Objects
69 * @ingroup utilities
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
85 * \code
86 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
87 * \endcode
88 * To negate every element in @c a, use
89 * \code
90 * transform(a.begin(), a.end(), a.begin(), negate<double>());
91 * \endcode
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.
99 * @{
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;
129 /** @} */
131 // 20.3.2 arithmetic
132 /** @defgroup arithmetic_functors Arithmetic Classes
133 * @ingroup functors
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.
140 * @{
143 #if __cplusplus > 201103L
144 struct __is_transparent; // undefined
146 template<typename _Tp = void>
147 struct plus;
149 template<typename _Tp = void>
150 struct minus;
152 template<typename _Tp = void>
153 struct multiplies;
155 template<typename _Tp = void>
156 struct divides;
158 template<typename _Tp = void>
159 struct modulus;
161 template<typename _Tp = void>
162 struct negate;
163 #endif
165 /// One of the @link arithmetic_functors math functors@endlink.
166 template<typename _Tp>
167 struct plus : public binary_function<_Tp, _Tp, _Tp>
169 _GLIBCXX14_CONSTEXPR
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>
179 _GLIBCXX14_CONSTEXPR
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>
189 _GLIBCXX14_CONSTEXPR
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>
199 _GLIBCXX14_CONSTEXPR
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>
209 _GLIBCXX14_CONSTEXPR
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>
219 _GLIBCXX14_CONSTEXPR
221 operator()(const _Tp& __x) const
222 { return -__x; }
225 #if __cplusplus > 201103L
227 #define __cpp_lib_transparent_operators 201510
229 template<>
230 struct plus<void>
232 template <typename _Tp, typename _Up>
233 _GLIBCXX14_CONSTEXPR
234 auto
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.
244 template<>
245 struct minus<void>
247 template <typename _Tp, typename _Up>
248 _GLIBCXX14_CONSTEXPR
249 auto
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.
259 template<>
260 struct multiplies<void>
262 template <typename _Tp, typename _Up>
263 _GLIBCXX14_CONSTEXPR
264 auto
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.
274 template<>
275 struct divides<void>
277 template <typename _Tp, typename _Up>
278 _GLIBCXX14_CONSTEXPR
279 auto
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.
289 template<>
290 struct modulus<void>
292 template <typename _Tp, typename _Up>
293 _GLIBCXX14_CONSTEXPR
294 auto
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.
304 template<>
305 struct negate<void>
307 template <typename _Tp>
308 _GLIBCXX14_CONSTEXPR
309 auto
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;
317 #endif
318 /** @} */
320 // 20.3.3 comparisons
321 /** @defgroup comparison_functors Comparison Classes
322 * @ingroup functors
324 * The library provides six wrapper functors for all the basic comparisons
325 * in C++, like @c <.
327 * @{
329 #if __cplusplus > 201103L
330 template<typename _Tp = void>
331 struct equal_to;
333 template<typename _Tp = void>
334 struct not_equal_to;
336 template<typename _Tp = void>
337 struct greater;
339 template<typename _Tp = void>
340 struct less;
342 template<typename _Tp = void>
343 struct greater_equal;
345 template<typename _Tp = void>
346 struct less_equal;
347 #endif
349 /// One of the @link comparison_functors comparison functors@endlink.
350 template<typename _Tp>
351 struct equal_to : public binary_function<_Tp, _Tp, bool>
353 _GLIBCXX14_CONSTEXPR
354 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>
363 _GLIBCXX14_CONSTEXPR
364 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>
373 _GLIBCXX14_CONSTEXPR
374 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>
383 _GLIBCXX14_CONSTEXPR
384 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>
393 _GLIBCXX14_CONSTEXPR
394 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>
403 _GLIBCXX14_CONSTEXPR
404 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.
411 template<>
412 struct equal_to<void>
414 template <typename _Tp, typename _Up>
415 _GLIBCXX14_CONSTEXPR
416 auto
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.
426 template<>
427 struct not_equal_to<void>
429 template <typename _Tp, typename _Up>
430 _GLIBCXX14_CONSTEXPR
431 auto
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.
441 template<>
442 struct greater<void>
444 template <typename _Tp, typename _Up>
445 _GLIBCXX14_CONSTEXPR
446 auto
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.
456 template<>
457 struct less<void>
459 template <typename _Tp, typename _Up>
460 _GLIBCXX14_CONSTEXPR
461 auto
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.
471 template<>
472 struct greater_equal<void>
474 template <typename _Tp, typename _Up>
475 _GLIBCXX14_CONSTEXPR
476 auto
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.
486 template<>
487 struct less_equal<void>
489 template <typename _Tp, typename _Up>
490 _GLIBCXX14_CONSTEXPR
491 auto
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;
499 #endif
500 /** @} */
502 // 20.3.4 logical operations
503 /** @defgroup logical_functors Boolean Operations Classes
504 * @ingroup functors
506 * Here are wrapper functors for Boolean operations: @c &&, @c ||,
507 * and @c !.
509 * @{
511 #if __cplusplus > 201103L
512 template<typename _Tp = void>
513 struct logical_and;
515 template<typename _Tp = void>
516 struct logical_or;
518 template<typename _Tp = void>
519 struct logical_not;
520 #endif
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>
526 _GLIBCXX14_CONSTEXPR
527 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>
536 _GLIBCXX14_CONSTEXPR
537 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>
546 _GLIBCXX14_CONSTEXPR
547 bool
548 operator()(const _Tp& __x) const
549 { return !__x; }
552 #if __cplusplus > 201103L
553 /// One of the @link logical_functors Boolean operations functors@endlink.
554 template<>
555 struct logical_and<void>
557 template <typename _Tp, typename _Up>
558 _GLIBCXX14_CONSTEXPR
559 auto
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.
569 template<>
570 struct logical_or<void>
572 template <typename _Tp, typename _Up>
573 _GLIBCXX14_CONSTEXPR
574 auto
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.
584 template<>
585 struct logical_not<void>
587 template <typename _Tp>
588 _GLIBCXX14_CONSTEXPR
589 auto
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;
597 #endif
598 /** @} */
600 #if __cplusplus > 201103L
601 template<typename _Tp = void>
602 struct bit_and;
604 template<typename _Tp = void>
605 struct bit_or;
607 template<typename _Tp = void>
608 struct bit_xor;
610 template<typename _Tp = void>
611 struct bit_not;
612 #endif
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>
619 _GLIBCXX14_CONSTEXPR
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>
628 _GLIBCXX14_CONSTEXPR
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>
637 _GLIBCXX14_CONSTEXPR
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>
646 _GLIBCXX14_CONSTEXPR
648 operator()(const _Tp& __x) const
649 { return ~__x; }
652 #if __cplusplus > 201103L
653 template <>
654 struct bit_and<void>
656 template <typename _Tp, typename _Up>
657 _GLIBCXX14_CONSTEXPR
658 auto
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;
667 template <>
668 struct bit_or<void>
670 template <typename _Tp, typename _Up>
671 _GLIBCXX14_CONSTEXPR
672 auto
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;
681 template <>
682 struct bit_xor<void>
684 template <typename _Tp, typename _Up>
685 _GLIBCXX14_CONSTEXPR
686 auto
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;
695 template <>
696 struct bit_not<void>
698 template <typename _Tp>
699 _GLIBCXX14_CONSTEXPR
700 auto
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;
708 #endif
710 // 20.3.5 negators
711 /** @defgroup negators Negators
712 * @ingroup functors
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,
721 * \code
722 * struct IntGreaterThanThree
723 * : public std::unary_function<int, bool>
725 * bool operator() (int x) { return x > 3; }
726 * };
728 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
729 * \endcode
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.
737 * @{
739 /// One of the @link negators negation functors@endlink.
740 template<typename _Predicate>
741 class unary_negate
742 : public unary_function<typename _Predicate::argument_type, bool>
744 protected:
745 _Predicate _M_pred;
747 public:
748 _GLIBCXX14_CONSTEXPR
749 explicit
750 unary_negate(const _Predicate& __x) : _M_pred(__x) { }
752 _GLIBCXX14_CONSTEXPR
753 bool
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>
760 _GLIBCXX14_CONSTEXPR
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>
767 class binary_negate
768 : public binary_function<typename _Predicate::first_argument_type,
769 typename _Predicate::second_argument_type, bool>
771 protected:
772 _Predicate _M_pred;
774 public:
775 _GLIBCXX14_CONSTEXPR
776 explicit
777 binary_negate(const _Predicate& __x) : _M_pred(__x) { }
779 _GLIBCXX14_CONSTEXPR
780 bool
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>
788 _GLIBCXX14_CONSTEXPR
789 inline binary_negate<_Predicate>
790 not2(const _Predicate& __pred)
791 { return binary_negate<_Predicate>(__pred); }
792 /** @} */
794 // 20.3.7 adaptors pointers functions
795 /** @defgroup pointer_adaptors Adaptors for pointers to functions
796 * @ingroup functors
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.
814 * @{
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>
820 protected:
821 _Result (*_M_ptr)(_Arg);
823 public:
824 pointer_to_unary_function() { }
826 explicit
827 pointer_to_unary_function(_Result (*__x)(_Arg))
828 : _M_ptr(__x) { }
830 _Result
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>
846 protected:
847 _Result (*_M_ptr)(_Arg1, _Arg2);
849 public:
850 pointer_to_binary_function() { }
852 explicit
853 pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
854 : _M_ptr(__x) { }
856 _Result
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); }
866 /** @} */
868 template<typename _Tp>
869 struct _Identity
870 : public unary_function<_Tp, _Tp>
872 _Tp&
873 operator()(_Tp& __x) const
874 { return __x; }
876 const _Tp&
877 operator()(const _Tp& __x) const
878 { return __x; }
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>
885 struct _Select1st
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; }
906 #endif
909 template<typename _Pair>
910 struct _Select2nd
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
924 * @ingroup functors
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
928 * one argument.
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.
936 * @{
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>
943 public:
944 explicit
945 mem_fun_t(_Ret (_Tp::*__pf)())
946 : _M_f(__pf) { }
948 _Ret
949 operator()(_Tp* __p) const
950 { return (__p->*_M_f)(); }
952 private:
953 _Ret (_Tp::*_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>
961 public:
962 explicit
963 const_mem_fun_t(_Ret (_Tp::*__pf)() const)
964 : _M_f(__pf) { }
966 _Ret
967 operator()(const _Tp* __p) const
968 { return (__p->*_M_f)(); }
970 private:
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>
979 public:
980 explicit
981 mem_fun_ref_t(_Ret (_Tp::*__pf)())
982 : _M_f(__pf) { }
984 _Ret
985 operator()(_Tp& __r) const
986 { return (__r.*_M_f)(); }
988 private:
989 _Ret (_Tp::*_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>
997 public:
998 explicit
999 const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
1000 : _M_f(__pf) { }
1002 _Ret
1003 operator()(const _Tp& __r) const
1004 { return (__r.*_M_f)(); }
1006 private:
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>
1015 public:
1016 explicit
1017 mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
1018 : _M_f(__pf) { }
1020 _Ret
1021 operator()(_Tp* __p, _Arg __x) const
1022 { return (__p->*_M_f)(__x); }
1024 private:
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>
1033 public:
1034 explicit
1035 const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
1036 : _M_f(__pf) { }
1038 _Ret
1039 operator()(const _Tp* __p, _Arg __x) const
1040 { return (__p->*_M_f)(__x); }
1042 private:
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>
1051 public:
1052 explicit
1053 mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
1054 : _M_f(__pf) { }
1056 _Ret
1057 operator()(_Tp& __r, _Arg __x) const
1058 { return (__r.*_M_f)(__x); }
1060 private:
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>
1069 public:
1070 explicit
1071 const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
1072 : _M_f(__pf) { }
1074 _Ret
1075 operator()(const _Tp& __r, _Arg __x) const
1076 { return (__r.*_M_f)(__x); }
1078 private:
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); }
1124 /** @} */
1126 _GLIBCXX_END_NAMESPACE_VERSION
1127 } // namespace
1129 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1130 # include <backward/binders.h>
1131 #endif
1133 #endif /* _STL_FUNCTION_H */