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