1// Functor implementations -*- C++ -*-
2
3// Copyright (C) 2001-2015 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.
10
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.
15
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.
19
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/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
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.
37 *
38 *
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
41 *
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.
49 */
50
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}
54 */
55
56#ifndef _STL_FUNCTION_H
57#define _STL_FUNCTION_H 1
58
59#if __cplusplus > 201103L
60#include <bits/move.h>
61#endif
62
63namespace std _GLIBCXX_VISIBILITY(default)
64{
65_GLIBCXX_BEGIN_NAMESPACE_VERSION
66
67 // 20.3.1 base classes
68 /** @defgroup functors Function Objects
69 * @ingroup utilities
70 *
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.
78 *
79 * Often, functors are only created as temporaries passed to algorithm
80 * calls, rather than being created as named variables.
81 *
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.
93 *
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.
98 *
99 * @{
100 */
101 /**
102 * This is one of the @link functors functor base classes@endlink.
103 */
104 template<typename _Arg, typename _Result>
105 struct unary_function
106 {
107 /// @c argument_type is the type of the argument
108 typedef _Arg argument_type;
109
110 /// @c result_type is the return type
111 typedef _Result result_type;
112 };
113
114 /**
115 * This is one of the @link functors functor base classes@endlink.
116 */
117 template<typename _Arg1, typename _Arg2, typename _Result>
118 struct binary_function
119 {
120 /// @c first_argument_type is the type of the first argument
121 typedef _Arg1 first_argument_type;
122
123 /// @c second_argument_type is the type of the second argument
124 typedef _Arg2 second_argument_type;
125
126 /// @c result_type is the return type
127 typedef _Result result_type;
128 };
129 /** @} */
130
131 // 20.3.2 arithmetic
132 /** @defgroup arithmetic_functors Arithmetic Classes
133 * @ingroup functors
134 *
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.
139 *
140 * @{
141 */
142
143#if __cplusplus > 201103L
144 struct __is_transparent; // undefined
145
146 template<typename _Tp = void>
147 struct plus;
148
149 template<typename _Tp = void>
150 struct minus;
151
152 template<typename _Tp = void>
153 struct multiplies;
154
155 template<typename _Tp = void>
156 struct divides;
157
158 template<typename _Tp = void>
159 struct modulus;
160
161 template<typename _Tp = void>
162 struct negate;
163#endif
164
165 /// One of the @link arithmetic_functors math functors@endlink.
166 template<typename _Tp>
167 struct plus : public binary_function<_Tp, _Tp, _Tp>
168 {
169 _GLIBCXX14_CONSTEXPR
170 _Tp
171 operator()(const _Tp& __x, const _Tp& __y) const
172 { return __x + __y; }
173 };
174
175 /// One of the @link arithmetic_functors math functors@endlink.
176 template<typename _Tp>
177 struct minus : public binary_function<_Tp, _Tp, _Tp>
178 {
179 _GLIBCXX14_CONSTEXPR
180 _Tp
181 operator()(const _Tp& __x, const _Tp& __y) const
182 { return __x - __y; }
183 };
184
185 /// One of the @link arithmetic_functors math functors@endlink.
186 template<typename _Tp>
187 struct multiplies : public binary_function<_Tp, _Tp, _Tp>
188 {
189 _GLIBCXX14_CONSTEXPR
190 _Tp
191 operator()(const _Tp& __x, const _Tp& __y) const
192 { return __x * __y; }
193 };
194
195 /// One of the @link arithmetic_functors math functors@endlink.
196 template<typename _Tp>
197 struct divides : public binary_function<_Tp, _Tp, _Tp>
198 {
199 _GLIBCXX14_CONSTEXPR
200 _Tp
201 operator()(const _Tp& __x, const _Tp& __y) const
202 { return __x / __y; }
203 };
204
205 /// One of the @link arithmetic_functors math functors@endlink.
206 template<typename _Tp>
207 struct modulus : public binary_function<_Tp, _Tp, _Tp>
208 {
209 _GLIBCXX14_CONSTEXPR
210 _Tp
211 operator()(const _Tp& __x, const _Tp& __y) const
212 { return __x % __y; }
213 };
214
215 /// One of the @link arithmetic_functors math functors@endlink.
216 template<typename _Tp>
217 struct negate : public unary_function<_Tp, _Tp>
218 {
219 _GLIBCXX14_CONSTEXPR
220 _Tp
221 operator()(const _Tp& __x) const
222 { return -__x; }
223 };
224
225#if __cplusplus > 201103L
226
227#define __cpp_lib_transparent_operators 201210
228//#define __cpp_lib_generic_associative_lookup 201304
229
230 template<>
231 struct plus<void>
232 {
233 template <typename _Tp, typename _Up>
234 _GLIBCXX14_CONSTEXPR
235 auto
236 operator()(_Tp&& __t, _Up&& __u) const
237 noexcept(noexcept(std::forward<_Tp>(__t) + std::forward<_Up>(__u)))
238 -> decltype(std::forward<_Tp>(__t) + std::forward<_Up>(__u))
239 { return std::forward<_Tp>(__t) + std::forward<_Up>(__u); }
240
241 typedef __is_transparent is_transparent;
242 };
243
244 /// One of the @link arithmetic_functors math functors@endlink.
245 template<>
246 struct minus<void>
247 {
248 template <typename _Tp, typename _Up>
249 _GLIBCXX14_CONSTEXPR
250 auto
251 operator()(_Tp&& __t, _Up&& __u) const
252 noexcept(noexcept(std::forward<_Tp>(__t) - std::forward<_Up>(__u)))
253 -> decltype(std::forward<_Tp>(__t) - std::forward<_Up>(__u))
254 { return std::forward<_Tp>(__t) - std::forward<_Up>(__u); }
255
256 typedef __is_transparent is_transparent;
257 };
258
259 /// One of the @link arithmetic_functors math functors@endlink.
260 template<>
261 struct multiplies<void>
262 {
263 template <typename _Tp, typename _Up>
264 _GLIBCXX14_CONSTEXPR
265 auto
266 operator()(_Tp&& __t, _Up&& __u) const
267 noexcept(noexcept(std::forward<_Tp>(__t) * std::forward<_Up>(__u)))
268 -> decltype(std::forward<_Tp>(__t) * std::forward<_Up>(__u))
269 { return std::forward<_Tp>(__t) * std::forward<_Up>(__u); }
270
271 typedef __is_transparent is_transparent;
272 };
273
274 /// One of the @link arithmetic_functors math functors@endlink.
275 template<>
276 struct divides<void>
277 {
278 template <typename _Tp, typename _Up>
279 _GLIBCXX14_CONSTEXPR
280 auto
281 operator()(_Tp&& __t, _Up&& __u) const
282 noexcept(noexcept(std::forward<_Tp>(__t) / std::forward<_Up>(__u)))
283 -> decltype(std::forward<_Tp>(__t) / std::forward<_Up>(__u))
284 { return std::forward<_Tp>(__t) / std::forward<_Up>(__u); }
285
286 typedef __is_transparent is_transparent;
287 };
288
289 /// One of the @link arithmetic_functors math functors@endlink.
290 template<>
291 struct modulus<void>
292 {
293 template <typename _Tp, typename _Up>
294 _GLIBCXX14_CONSTEXPR
295 auto
296 operator()(_Tp&& __t, _Up&& __u) const
297 noexcept(noexcept(std::forward<_Tp>(__t) % std::forward<_Up>(__u)))
298 -> decltype(std::forward<_Tp>(__t) % std::forward<_Up>(__u))
299 { return std::forward<_Tp>(__t) % std::forward<_Up>(__u); }
300
301 typedef __is_transparent is_transparent;
302 };
303
304 /// One of the @link arithmetic_functors math functors@endlink.
305 template<>
306 struct negate<void>
307 {
308 template <typename _Tp>
309 _GLIBCXX14_CONSTEXPR
310 auto
311 operator()(_Tp&& __t) const
312 noexcept(noexcept(-std::forward<_Tp>(__t)))
313 -> decltype(-std::forward<_Tp>(__t))
314 { return -std::forward<_Tp>(__t); }
315
316 typedef __is_transparent is_transparent;
317 };
318#endif
319 /** @} */
320
321 // 20.3.3 comparisons
322 /** @defgroup comparison_functors Comparison Classes
323 * @ingroup functors
324 *
325 * The library provides six wrapper functors for all the basic comparisons
326 * in C++, like @c <.
327 *
328 * @{
329 */
330#if __cplusplus > 201103L
331 template<typename _Tp = void>
332 struct equal_to;
333
334 template<typename _Tp = void>
335 struct not_equal_to;
336
337 template<typename _Tp = void>
338 struct greater;
339
340 template<typename _Tp = void>
341 struct less;
342
343 template<typename _Tp = void>
344 struct greater_equal;
345
346 template<typename _Tp = void>
347 struct less_equal;
348#endif
349
350 /// One of the @link comparison_functors comparison functors@endlink.
351 template<typename _Tp>
352 struct equal_to : public binary_function<_Tp, _Tp, bool>
353 {
354 _GLIBCXX14_CONSTEXPR
355 bool
356 operator()(const _Tp& __x, const _Tp& __y) const
357 { return __x == __y; }
358 };
359
360 /// One of the @link comparison_functors comparison functors@endlink.
361 template<typename _Tp>
362 struct not_equal_to : public binary_function<_Tp, _Tp, bool>
363 {
364 _GLIBCXX14_CONSTEXPR
365 bool
366 operator()(const _Tp& __x, const _Tp& __y) const
367 { return __x != __y; }
368 };
369
370 /// One of the @link comparison_functors comparison functors@endlink.
371 template<typename _Tp>
372 struct greater : public binary_function<_Tp, _Tp, bool>
373 {
374 _GLIBCXX14_CONSTEXPR
375 bool
376 operator()(const _Tp& __x, const _Tp& __y) const
377 { return __x > __y; }
378 };
379
380 /// One of the @link comparison_functors comparison functors@endlink.
381 template<typename _Tp>
382 struct less : public binary_function<_Tp, _Tp, bool>
383 {
384 _GLIBCXX14_CONSTEXPR
385 bool
386 operator()(const _Tp& __x, const _Tp& __y) const
387 { return __x < __y; }
388 };
389
390 /// One of the @link comparison_functors comparison functors@endlink.
391 template<typename _Tp>
392 struct greater_equal : public binary_function<_Tp, _Tp, bool>
393 {
394 _GLIBCXX14_CONSTEXPR
395 bool
396 operator()(const _Tp& __x, const _Tp& __y) const
397 { return __x >= __y; }
398 };
399
400 /// One of the @link comparison_functors comparison functors@endlink.
401 template<typename _Tp>
402 struct less_equal : public binary_function<_Tp, _Tp, bool>
403 {
404 _GLIBCXX14_CONSTEXPR
405 bool
406 operator()(const _Tp& __x, const _Tp& __y) const
407 { return __x <= __y; }
408 };
409
410#if __cplusplus > 201103L
411 /// One of the @link comparison_functors comparison functors@endlink.
412 template<>
413 struct equal_to<void>
414 {
415 template <typename _Tp, typename _Up>
416 _GLIBCXX14_CONSTEXPR
417 auto
418 operator()(_Tp&& __t, _Up&& __u) const
419 noexcept(noexcept(std::forward<_Tp>(__t) == std::forward<_Up>(__u)))
420 -> decltype(std::forward<_Tp>(__t) == std::forward<_Up>(__u))
421 { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); }
422
423 typedef __is_transparent is_transparent;
424 };
425
426 /// One of the @link comparison_functors comparison functors@endlink.
427 template<>
428 struct not_equal_to<void>
429 {
430 template <typename _Tp, typename _Up>
431 _GLIBCXX14_CONSTEXPR
432 auto
433 operator()(_Tp&& __t, _Up&& __u) const
434 noexcept(noexcept(std::forward<_Tp>(__t) != std::forward<_Up>(__u)))
435 -> decltype(std::forward<_Tp>(__t) != std::forward<_Up>(__u))
436 { return std::forward<_Tp>(__t) != std::forward<_Up>(__u); }
437
438 typedef __is_transparent is_transparent;
439 };
440
441 /// One of the @link comparison_functors comparison functors@endlink.
442 template<>
443 struct greater<void>
444 {
445 template <typename _Tp, typename _Up>
446 _GLIBCXX14_CONSTEXPR
447 auto
448 operator()(_Tp&& __t, _Up&& __u) const
449 noexcept(noexcept(std::forward<_Tp>(__t) > std::forward<_Up>(__u)))
450 -> decltype(std::forward<_Tp>(__t) > std::forward<_Up>(__u))
451 { return std::forward<_Tp>(__t) > std::forward<_Up>(__u); }
452
453 typedef __is_transparent is_transparent;
454 };
455
456 /// One of the @link comparison_functors comparison functors@endlink.
457 template<>
458 struct less<void>
459 {
460 template <typename _Tp, typename _Up>
461 _GLIBCXX14_CONSTEXPR
462 auto
463 operator()(_Tp&& __t, _Up&& __u) const
464 noexcept(noexcept(std::forward<_Tp>(__t) < std::forward<_Up>(__u)))
465 -> decltype(std::forward<_Tp>(__t) < std::forward<_Up>(__u))
466 { return std::forward<_Tp>(__t) < std::forward<_Up>(__u); }
467
468 typedef __is_transparent is_transparent;
469 };
470
471 /// One of the @link comparison_functors comparison functors@endlink.
472 template<>
473 struct greater_equal<void>
474 {
475 template <typename _Tp, typename _Up>
476 _GLIBCXX14_CONSTEXPR
477 auto
478 operator()(_Tp&& __t, _Up&& __u) const
479 noexcept(noexcept(std::forward<_Tp>(__t) >= std::forward<_Up>(__u)))
480 -> decltype(std::forward<_Tp>(__t) >= std::forward<_Up>(__u))
481 { return std::forward<_Tp>(__t) >= std::forward<_Up>(__u); }
482
483 typedef __is_transparent is_transparent;
484 };
485
486 /// One of the @link comparison_functors comparison functors@endlink.
487 template<>
488 struct less_equal<void>
489 {
490 template <typename _Tp, typename _Up>
491 _GLIBCXX14_CONSTEXPR
492 auto
493 operator()(_Tp&& __t, _Up&& __u) const
494 noexcept(noexcept(std::forward<_Tp>(__t) <= std::forward<_Up>(__u)))
495 -> decltype(std::forward<_Tp>(__t) <= std::forward<_Up>(__u))
496 { return std::forward<_Tp>(__t) <= std::forward<_Up>(__u); }
497
498 typedef __is_transparent is_transparent;
499 };
500#endif
501 /** @} */
502
503 // 20.3.4 logical operations
504 /** @defgroup logical_functors Boolean Operations Classes
505 * @ingroup functors
506 *
507 * Here are wrapper functors for Boolean operations: @c &&, @c ||,
508 * and @c !.
509 *
510 * @{
511 */
512#if __cplusplus > 201103L
513 template<typename _Tp = void>
514 struct logical_and;
515
516 template<typename _Tp = void>
517 struct logical_or;
518
519 template<typename _Tp = void>
520 struct logical_not;
521#endif
522
523 /// One of the @link logical_functors Boolean operations functors@endlink.
524 template<typename _Tp>
525 struct logical_and : public binary_function<_Tp, _Tp, bool>
526 {
527 _GLIBCXX14_CONSTEXPR
528 bool
529 operator()(const _Tp& __x, const _Tp& __y) const
530 { return __x && __y; }
531 };
532
533 /// One of the @link logical_functors Boolean operations functors@endlink.
534 template<typename _Tp>
535 struct logical_or : public binary_function<_Tp, _Tp, bool>
536 {
537 _GLIBCXX14_CONSTEXPR
538 bool
539 operator()(const _Tp& __x, const _Tp& __y) const
540 { return __x || __y; }
541 };
542
543 /// One of the @link logical_functors Boolean operations functors@endlink.
544 template<typename _Tp>
545 struct logical_not : public unary_function<_Tp, bool>
546 {
547 _GLIBCXX14_CONSTEXPR
548 bool
549 operator()(const _Tp& __x) const
550 { return !__x; }
551 };
552
553#if __cplusplus > 201103L
554 /// One of the @link logical_functors Boolean operations functors@endlink.
555 template<>
556 struct logical_and<void>
557 {
558 template <typename _Tp, typename _Up>
559 _GLIBCXX14_CONSTEXPR
560 auto
561 operator()(_Tp&& __t, _Up&& __u) const
562 noexcept(noexcept(std::forward<_Tp>(__t) && std::forward<_Up>(__u)))
563 -> decltype(std::forward<_Tp>(__t) && std::forward<_Up>(__u))
564 { return std::forward<_Tp>(__t) && std::forward<_Up>(__u); }
565
566 typedef __is_transparent is_transparent;
567 };
568
569 /// One of the @link logical_functors Boolean operations functors@endlink.
570 template<>
571 struct logical_or<void>
572 {
573 template <typename _Tp, typename _Up>
574 _GLIBCXX14_CONSTEXPR
575 auto
576 operator()(_Tp&& __t, _Up&& __u) const
577 noexcept(noexcept(std::forward<_Tp>(__t) || std::forward<_Up>(__u)))
578 -> decltype(std::forward<_Tp>(__t) || std::forward<_Up>(__u))
579 { return std::forward<_Tp>(__t) || std::forward<_Up>(__u); }
580
581 typedef __is_transparent is_transparent;
582 };
583
584 /// One of the @link logical_functors Boolean operations functors@endlink.
585 template<>
586 struct logical_not<void>
587 {
588 template <typename _Tp>
589 _GLIBCXX14_CONSTEXPR
590 auto
591 operator()(_Tp&& __t) const
592 noexcept(noexcept(!std::forward<_Tp>(__t)))
593 -> decltype(!std::forward<_Tp>(__t))
594 { return !std::forward<_Tp>(__t); }
595
596 typedef __is_transparent is_transparent;
597 };
598#endif
599 /** @} */
600
601#if __cplusplus > 201103L
602 template<typename _Tp = void>
603 struct bit_and;
604
605 template<typename _Tp = void>
606 struct bit_or;
607
608 template<typename _Tp = void>
609 struct bit_xor;
610
611 template<typename _Tp = void>
612 struct bit_not;
613#endif
614
615 // _GLIBCXX_RESOLVE_LIB_DEFECTS
616 // DR 660. Missing Bitwise Operations.
617 template<typename _Tp>
618 struct bit_and : public binary_function<_Tp, _Tp, _Tp>
619 {
620 _GLIBCXX14_CONSTEXPR
621 _Tp
622 operator()(const _Tp& __x, const _Tp& __y) const
623 { return __x & __y; }
624 };
625
626 template<typename _Tp>
627 struct bit_or : public binary_function<_Tp, _Tp, _Tp>
628 {
629 _GLIBCXX14_CONSTEXPR
630 _Tp
631 operator()(const _Tp& __x, const _Tp& __y) const
632 { return __x | __y; }
633 };
634
635 template<typename _Tp>
636 struct bit_xor : public binary_function<_Tp, _Tp, _Tp>
637 {
638 _GLIBCXX14_CONSTEXPR
639 _Tp
640 operator()(const _Tp& __x, const _Tp& __y) const
641 { return __x ^ __y; }
642 };
643
644 template<typename _Tp>
645 struct bit_not : public unary_function<_Tp, _Tp>
646 {
647 _GLIBCXX14_CONSTEXPR
648 _Tp
649 operator()(const _Tp& __x) const
650 { return ~__x; }
651 };
652
653#if __cplusplus > 201103L
654 template <>
655 struct bit_and<void>
656 {
657 template <typename _Tp, typename _Up>
658 _GLIBCXX14_CONSTEXPR
659 auto
660 operator()(_Tp&& __t, _Up&& __u) const
661 noexcept(noexcept(std::forward<_Tp>(__t) & std::forward<_Up>(__u)))
662 -> decltype(std::forward<_Tp>(__t) & std::forward<_Up>(__u))
663 { return std::forward<_Tp>(__t) & std::forward<_Up>(__u); }
664
665 typedef __is_transparent is_transparent;
666 };
667
668 template <>
669 struct bit_or<void>
670 {
671 template <typename _Tp, typename _Up>
672 _GLIBCXX14_CONSTEXPR
673 auto
674 operator()(_Tp&& __t, _Up&& __u) const
675 noexcept(noexcept(std::forward<_Tp>(__t) | std::forward<_Up>(__u)))
676 -> decltype(std::forward<_Tp>(__t) | std::forward<_Up>(__u))
677 { return std::forward<_Tp>(__t) | std::forward<_Up>(__u); }
678
679 typedef __is_transparent is_transparent;
680 };
681
682 template <>
683 struct bit_xor<void>
684 {
685 template <typename _Tp, typename _Up>
686 _GLIBCXX14_CONSTEXPR
687 auto
688 operator()(_Tp&& __t, _Up&& __u) const
689 noexcept(noexcept(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u)))
690 -> decltype(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u))
691 { return std::forward<_Tp>(__t) ^ std::forward<_Up>(__u); }
692
693 typedef __is_transparent is_transparent;
694 };
695
696 template <>
697 struct bit_not<void>
698 {
699 template <typename _Tp>
700 _GLIBCXX14_CONSTEXPR
701 auto
702 operator()(_Tp&& __t) const
703 noexcept(noexcept(~std::forward<_Tp>(__t)))
704 -> decltype(~std::forward<_Tp>(__t))
705 { return ~std::forward<_Tp>(__t); }
706
707 typedef __is_transparent is_transparent;
708 };
709#endif
710
711 // 20.3.5 negators
712 /** @defgroup negators Negators
713 * @ingroup functors
714 *
715 * The functions @c not1 and @c not2 each take a predicate functor
716 * and return an instance of @c unary_negate or
717 * @c binary_negate, respectively. These classes are functors whose
718 * @c operator() performs the stored predicate function and then returns
719 * the negation of the result.
720 *
721 * For example, given a vector of integers and a trivial predicate,
722 * \code
723 * struct IntGreaterThanThree
724 * : public std::unary_function<int, bool>
725 * {
726 * bool operator() (int x) { return x > 3; }
727 * };
728 *
729 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
730 * \endcode
731 * The call to @c find_if will locate the first index (i) of @c v for which
732 * <code>!(v[i] > 3)</code> is true.
733 *
734 * The not1/unary_negate combination works on predicates taking a single
735 * argument. The not2/binary_negate combination works on predicates which
736 * take two arguments.
737 *
738 * @{
739 */
740 /// One of the @link negators negation functors@endlink.
741 template<typename _Predicate>
742 class unary_negate
743 : public unary_function<typename _Predicate::argument_type, bool>
744 {
745 protected:
746 _Predicate _M_pred;
747
748 public:
749 _GLIBCXX14_CONSTEXPR
750 explicit
751 unary_negate(const _Predicate& __x) : _M_pred(__x) { }
752
753 _GLIBCXX14_CONSTEXPR
754 bool
755 operator()(const typename _Predicate::argument_type& __x) const
756 { return !_M_pred(__x); }
757 };
758
759 /// One of the @link negators negation functors@endlink.
760 template<typename _Predicate>
761 _GLIBCXX14_CONSTEXPR
762 inline unary_negate<_Predicate>
763 not1(const _Predicate& __pred)
764 { return unary_negate<_Predicate>(__pred); }
765
766 /// One of the @link negators negation functors@endlink.
767 template<typename _Predicate>
768 class binary_negate
769 : public binary_function<typename _Predicate::first_argument_type,
770 typename _Predicate::second_argument_type, bool>
771 {
772 protected:
773 _Predicate _M_pred;
774
775 public:
776 _GLIBCXX14_CONSTEXPR
777 explicit
778 binary_negate(const _Predicate& __x) : _M_pred(__x) { }
779
780 _GLIBCXX14_CONSTEXPR
781 bool
782 operator()(const typename _Predicate::first_argument_type& __x,
783 const typename _Predicate::second_argument_type& __y) const
784 { return !_M_pred(__x, __y); }
785 };
786
787 /// One of the @link negators negation functors@endlink.
788 template<typename _Predicate>
789 _GLIBCXX14_CONSTEXPR
790 inline binary_negate<_Predicate>
791 not2(const _Predicate& __pred)
792 { return binary_negate<_Predicate>(__pred); }
793 /** @} */
794
795 // 20.3.7 adaptors pointers functions
796 /** @defgroup pointer_adaptors Adaptors for pointers to functions
797 * @ingroup functors
798 *
799 * The advantage of function objects over pointers to functions is that
800 * the objects in the standard library declare nested typedefs describing
801 * their argument and result types with uniform names (e.g., @c result_type
802 * from the base classes @c unary_function and @c binary_function).
803 * Sometimes those typedefs are required, not just optional.
804 *
805 * Adaptors are provided to turn pointers to unary (single-argument) and
806 * binary (double-argument) functions into function objects. The
807 * long-winded functor @c pointer_to_unary_function is constructed with a
808 * function pointer @c f, and its @c operator() called with argument @c x
809 * returns @c f(x). The functor @c pointer_to_binary_function does the same
810 * thing, but with a double-argument @c f and @c operator().
811 *
812 * The function @c ptr_fun takes a pointer-to-function @c f and constructs
813 * an instance of the appropriate functor.
814 *
815 * @{
816 */
817 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
818 template<typename _Arg, typename _Result>
819 class pointer_to_unary_function : public unary_function<_Arg, _Result>
820 {
821 protected:
822 _Result (*_M_ptr)(_Arg);
823
824 public:
825 pointer_to_unary_function() { }
826
827 explicit
828 pointer_to_unary_function(_Result (*__x)(_Arg))
829 : _M_ptr(__x) { }
830
831 _Result
832 operator()(_Arg __x) const
833 { return _M_ptr(__x); }
834 };
835
836 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
837 template<typename _Arg, typename _Result>
838 inline pointer_to_unary_function<_Arg, _Result>
839 ptr_fun(_Result (*__x)(_Arg))
840 { return pointer_to_unary_function<_Arg, _Result>(__x); }
841
842 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
843 template<typename _Arg1, typename _Arg2, typename _Result>
844 class pointer_to_binary_function
845 : public binary_function<_Arg1, _Arg2, _Result>
846 {
847 protected:
848 _Result (*_M_ptr)(_Arg1, _Arg2);
849
850 public:
851 pointer_to_binary_function() { }
852
853 explicit
854 pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
855 : _M_ptr(__x) { }
856
857 _Result
858 operator()(_Arg1 __x, _Arg2 __y) const
859 { return _M_ptr(__x, __y); }
860 };
861
862 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
863 template<typename _Arg1, typename _Arg2, typename _Result>
864 inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
865 ptr_fun(_Result (*__x)(_Arg1, _Arg2))
866 { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
867 /** @} */
868
869 template<typename _Tp>
870 struct _Identity
871 : public unary_function<_Tp,_Tp>
872 {
873 _Tp&
874 operator()(_Tp& __x) const
875 { return __x; }
876
877 const _Tp&
878 operator()(const _Tp& __x) const
879 { return __x; }
880 };
881
882 template<typename _Pair>
883 struct _Select1st
884 : public unary_function<_Pair, typename _Pair::first_type>
885 {
886 typename _Pair::first_type&
887 operator()(_Pair& __x) const
888 { return __x.first; }
889
890 const typename _Pair::first_type&
891 operator()(const _Pair& __x) const
892 { return __x.first; }
893
894#if __cplusplus >= 201103L
895 template<typename _Pair2>
896 typename _Pair2::first_type&
897 operator()(_Pair2& __x) const
898 { return __x.first; }
899
900 template<typename _Pair2>
901 const typename _Pair2::first_type&
902 operator()(const _Pair2& __x) const
903 { return __x.first; }
904#endif
905 };
906
907 template<typename _Pair>
908 struct _Select2nd
909 : public unary_function<_Pair, typename _Pair::second_type>
910 {
911 typename _Pair::second_type&
912 operator()(_Pair& __x) const
913 { return __x.second; }
914
915 const typename _Pair::second_type&
916 operator()(const _Pair& __x) const
917 { return __x.second; }
918 };
919
920 // 20.3.8 adaptors pointers members
921 /** @defgroup memory_adaptors Adaptors for pointers to members
922 * @ingroup functors
923 *
924 * There are a total of 8 = 2^3 function objects in this family.
925 * (1) Member functions taking no arguments vs member functions taking
926 * one argument.
927 * (2) Call through pointer vs call through reference.
928 * (3) Const vs non-const member function.
929 *
930 * All of this complexity is in the function objects themselves. You can
931 * ignore it by using the helper function mem_fun and mem_fun_ref,
932 * which create whichever type of adaptor is appropriate.
933 *
934 * @{
935 */
936 /// One of the @link memory_adaptors adaptors for member
937 /// pointers@endlink.
938 template<typename _Ret, typename _Tp>
939 class mem_fun_t : public unary_function<_Tp*, _Ret>
940 {
941 public:
942 explicit
943 mem_fun_t(_Ret (_Tp::*__pf)())
944 : _M_f(__pf) { }
945
946 _Ret
947 operator()(_Tp* __p) const
948 { return (__p->*_M_f)(); }
949
950 private:
951 _Ret (_Tp::*_M_f)();
952 };
953
954 /// One of the @link memory_adaptors adaptors for member
955 /// pointers@endlink.
956 template<typename _Ret, typename _Tp>
957 class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
958 {
959 public:
960 explicit
961 const_mem_fun_t(_Ret (_Tp::*__pf)() const)
962 : _M_f(__pf) { }
963
964 _Ret
965 operator()(const _Tp* __p) const
966 { return (__p->*_M_f)(); }
967
968 private:
969 _Ret (_Tp::*_M_f)() const;
970 };
971
972 /// One of the @link memory_adaptors adaptors for member
973 /// pointers@endlink.
974 template<typename _Ret, typename _Tp>
975 class mem_fun_ref_t : public unary_function<_Tp, _Ret>
976 {
977 public:
978 explicit
979 mem_fun_ref_t(_Ret (_Tp::*__pf)())
980 : _M_f(__pf) { }
981
982 _Ret
983 operator()(_Tp& __r) const
984 { return (__r.*_M_f)(); }
985
986 private:
987 _Ret (_Tp::*_M_f)();
988 };
989
990 /// One of the @link memory_adaptors adaptors for member
991 /// pointers@endlink.
992 template<typename _Ret, typename _Tp>
993 class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
994 {
995 public:
996 explicit
997 const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
998 : _M_f(__pf) { }
999
1000 _Ret
1001 operator()(const _Tp& __r) const
1002 { return (__r.*_M_f)(); }
1003
1004 private:
1005 _Ret (_Tp::*_M_f)() const;
1006 };
1007
1008 /// One of the @link memory_adaptors adaptors for member
1009 /// pointers@endlink.
1010 template<typename _Ret, typename _Tp, typename _Arg>
1011 class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
1012 {
1013 public:
1014 explicit
1015 mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
1016 : _M_f(__pf) { }
1017
1018 _Ret
1019 operator()(_Tp* __p, _Arg __x) const
1020 { return (__p->*_M_f)(__x); }
1021
1022 private:
1023 _Ret (_Tp::*_M_f)(_Arg);
1024 };
1025
1026 /// One of the @link memory_adaptors adaptors for member
1027 /// pointers@endlink.
1028 template<typename _Ret, typename _Tp, typename _Arg>
1029 class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
1030 {
1031 public:
1032 explicit
1033 const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
1034 : _M_f(__pf) { }
1035
1036 _Ret
1037 operator()(const _Tp* __p, _Arg __x) const
1038 { return (__p->*_M_f)(__x); }
1039
1040 private:
1041 _Ret (_Tp::*_M_f)(_Arg) const;
1042 };
1043
1044 /// One of the @link memory_adaptors adaptors for member
1045 /// pointers@endlink.
1046 template<typename _Ret, typename _Tp, typename _Arg>
1047 class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
1048 {
1049 public:
1050 explicit
1051 mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
1052 : _M_f(__pf) { }
1053
1054 _Ret
1055 operator()(_Tp& __r, _Arg __x) const
1056 { return (__r.*_M_f)(__x); }
1057
1058 private:
1059 _Ret (_Tp::*_M_f)(_Arg);
1060 };
1061
1062 /// One of the @link memory_adaptors adaptors for member
1063 /// pointers@endlink.
1064 template<typename _Ret, typename _Tp, typename _Arg>
1065 class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
1066 {
1067 public:
1068 explicit
1069 const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
1070 : _M_f(__pf) { }
1071
1072 _Ret
1073 operator()(const _Tp& __r, _Arg __x) const
1074 { return (__r.*_M_f)(__x); }
1075
1076 private:
1077 _Ret (_Tp::*_M_f)(_Arg) const;
1078 };
1079
1080 // Mem_fun adaptor helper functions. There are only two:
1081 // mem_fun and mem_fun_ref.
1082 template<typename _Ret, typename _Tp>
1083 inline mem_fun_t<_Ret, _Tp>
1084 mem_fun(_Ret (_Tp::*__f)())
1085 { return mem_fun_t<_Ret, _Tp>(__f); }
1086
1087 template<typename _Ret, typename _Tp>
1088 inline const_mem_fun_t<_Ret, _Tp>
1089 mem_fun(_Ret (_Tp::*__f)() const)
1090 { return const_mem_fun_t<_Ret, _Tp>(__f); }
1091
1092 template<typename _Ret, typename _Tp>
1093 inline mem_fun_ref_t<_Ret, _Tp>
1094 mem_fun_ref(_Ret (_Tp::*__f)())
1095 { return mem_fun_ref_t<_Ret, _Tp>(__f); }
1096
1097 template<typename _Ret, typename _Tp>
1098 inline const_mem_fun_ref_t<_Ret, _Tp>
1099 mem_fun_ref(_Ret (_Tp::*__f)() const)
1100 { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
1101
1102 template<typename _Ret, typename _Tp, typename _Arg>
1103 inline mem_fun1_t<_Ret, _Tp, _Arg>
1104 mem_fun(_Ret (_Tp::*__f)(_Arg))
1105 { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
1106
1107 template<typename _Ret, typename _Tp, typename _Arg>
1108 inline const_mem_fun1_t<_Ret, _Tp, _Arg>
1109 mem_fun(_Ret (_Tp::*__f)(_Arg) const)
1110 { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
1111
1112 template<typename _Ret, typename _Tp, typename _Arg>
1113 inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
1114 mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
1115 { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
1116
1117 template<typename _Ret, typename _Tp, typename _Arg>
1118 inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
1119 mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
1120 { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
1121
1122 /** @} */
1123
1124_GLIBCXX_END_NAMESPACE_VERSION
1125} // namespace
1126
1127#if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1128# include <backward/binders.h>
1129#endif
1130
1131#endif /* _STL_FUNCTION_H */
1132