Line data Source code
1 : // Functor implementations -*- C++ -*-
2 :
3 : // Copyright (C) 2001-2016 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 15 : 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 :
229 : template<>
230 : struct plus<void>
231 : {
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); }
239 :
240 : typedef __is_transparent is_transparent;
241 : };
242 :
243 : /// One of the @link arithmetic_functors math functors@endlink.
244 : template<>
245 : struct minus<void>
246 : {
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); }
254 :
255 : typedef __is_transparent is_transparent;
256 : };
257 :
258 : /// One of the @link arithmetic_functors math functors@endlink.
259 : template<>
260 : struct multiplies<void>
261 : {
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); }
269 :
270 : typedef __is_transparent is_transparent;
271 : };
272 :
273 : /// One of the @link arithmetic_functors math functors@endlink.
274 : template<>
275 : struct divides<void>
276 : {
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); }
284 :
285 : typedef __is_transparent is_transparent;
286 : };
287 :
288 : /// One of the @link arithmetic_functors math functors@endlink.
289 : template<>
290 : struct modulus<void>
291 : {
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); }
299 :
300 : typedef __is_transparent is_transparent;
301 : };
302 :
303 : /// One of the @link arithmetic_functors math functors@endlink.
304 : template<>
305 : struct negate<void>
306 : {
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); }
314 :
315 : typedef __is_transparent is_transparent;
316 : };
317 : #endif
318 : /** @} */
319 :
320 : // 20.3.3 comparisons
321 : /** @defgroup comparison_functors Comparison Classes
322 : * @ingroup functors
323 : *
324 : * The library provides six wrapper functors for all the basic comparisons
325 : * in C++, like @c <.
326 : *
327 : * @{
328 : */
329 : #if __cplusplus > 201103L
330 : template<typename _Tp = void>
331 : struct equal_to;
332 :
333 : template<typename _Tp = void>
334 : struct not_equal_to;
335 :
336 : template<typename _Tp = void>
337 : struct greater;
338 :
339 : template<typename _Tp = void>
340 : struct less;
341 :
342 : template<typename _Tp = void>
343 : struct greater_equal;
344 :
345 : template<typename _Tp = void>
346 : struct less_equal;
347 : #endif
348 :
349 : /// One of the @link comparison_functors comparison functors@endlink.
350 : template<typename _Tp>
351 : struct equal_to : public binary_function<_Tp, _Tp, bool>
352 : {
353 : _GLIBCXX14_CONSTEXPR
354 : bool
355 0 : operator()(const _Tp& __x, const _Tp& __y) const
356 0 : { return __x == __y; }
357 : };
358 :
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>
362 : {
363 : _GLIBCXX14_CONSTEXPR
364 : bool
365 : operator()(const _Tp& __x, const _Tp& __y) const
366 : { return __x != __y; }
367 : };
368 :
369 : /// One of the @link comparison_functors comparison functors@endlink.
370 : template<typename _Tp>
371 : struct greater : public binary_function<_Tp, _Tp, bool>
372 : {
373 : _GLIBCXX14_CONSTEXPR
374 : bool
375 : operator()(const _Tp& __x, const _Tp& __y) const
376 : { return __x > __y; }
377 : };
378 :
379 : /// One of the @link comparison_functors comparison functors@endlink.
380 : template<typename _Tp>
381 : struct less : public binary_function<_Tp, _Tp, bool>
382 : {
383 : _GLIBCXX14_CONSTEXPR
384 : bool
385 0 : operator()(const _Tp& __x, const _Tp& __y) const
386 0 : { return __x < __y; }
387 : };
388 :
389 : /// One of the @link comparison_functors comparison functors@endlink.
390 : template<typename _Tp>
391 : struct greater_equal : public binary_function<_Tp, _Tp, bool>
392 : {
393 : _GLIBCXX14_CONSTEXPR
394 : bool
395 : operator()(const _Tp& __x, const _Tp& __y) const
396 : { return __x >= __y; }
397 : };
398 :
399 : /// One of the @link comparison_functors comparison functors@endlink.
400 : template<typename _Tp>
401 : struct less_equal : public binary_function<_Tp, _Tp, bool>
402 : {
403 : _GLIBCXX14_CONSTEXPR
404 : bool
405 : operator()(const _Tp& __x, const _Tp& __y) const
406 : { return __x <= __y; }
407 : };
408 :
409 : #if __cplusplus > 201103L
410 : /// One of the @link comparison_functors comparison functors@endlink.
411 : template<>
412 : struct equal_to<void>
413 : {
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); }
421 :
422 : typedef __is_transparent is_transparent;
423 : };
424 :
425 : /// One of the @link comparison_functors comparison functors@endlink.
426 : template<>
427 : struct not_equal_to<void>
428 : {
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); }
436 :
437 : typedef __is_transparent is_transparent;
438 : };
439 :
440 : /// One of the @link comparison_functors comparison functors@endlink.
441 : template<>
442 : struct greater<void>
443 : {
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); }
451 :
452 : typedef __is_transparent is_transparent;
453 : };
454 :
455 : /// One of the @link comparison_functors comparison functors@endlink.
456 : template<>
457 : struct less<void>
458 : {
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); }
466 :
467 : typedef __is_transparent is_transparent;
468 : };
469 :
470 : /// One of the @link comparison_functors comparison functors@endlink.
471 : template<>
472 : struct greater_equal<void>
473 : {
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); }
481 :
482 : typedef __is_transparent is_transparent;
483 : };
484 :
485 : /// One of the @link comparison_functors comparison functors@endlink.
486 : template<>
487 : struct less_equal<void>
488 : {
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); }
496 :
497 : typedef __is_transparent is_transparent;
498 : };
499 : #endif
500 : /** @} */
501 :
502 : // 20.3.4 logical operations
503 : /** @defgroup logical_functors Boolean Operations Classes
504 : * @ingroup functors
505 : *
506 : * Here are wrapper functors for Boolean operations: @c &&, @c ||,
507 : * and @c !.
508 : *
509 : * @{
510 : */
511 : #if __cplusplus > 201103L
512 : template<typename _Tp = void>
513 : struct logical_and;
514 :
515 : template<typename _Tp = void>
516 : struct logical_or;
517 :
518 : template<typename _Tp = void>
519 : struct logical_not;
520 : #endif
521 :
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>
525 : {
526 : _GLIBCXX14_CONSTEXPR
527 : bool
528 : operator()(const _Tp& __x, const _Tp& __y) const
529 : { return __x && __y; }
530 : };
531 :
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>
535 : {
536 : _GLIBCXX14_CONSTEXPR
537 : bool
538 : operator()(const _Tp& __x, const _Tp& __y) const
539 : { return __x || __y; }
540 : };
541 :
542 : /// One of the @link logical_functors Boolean operations functors@endlink.
543 : template<typename _Tp>
544 : struct logical_not : public unary_function<_Tp, bool>
545 : {
546 : _GLIBCXX14_CONSTEXPR
547 : bool
548 : operator()(const _Tp& __x) const
549 : { return !__x; }
550 : };
551 :
552 : #if __cplusplus > 201103L
553 : /// One of the @link logical_functors Boolean operations functors@endlink.
554 : template<>
555 : struct logical_and<void>
556 : {
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); }
564 :
565 : typedef __is_transparent is_transparent;
566 : };
567 :
568 : /// One of the @link logical_functors Boolean operations functors@endlink.
569 : template<>
570 : struct logical_or<void>
571 : {
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); }
579 :
580 : typedef __is_transparent is_transparent;
581 : };
582 :
583 : /// One of the @link logical_functors Boolean operations functors@endlink.
584 : template<>
585 : struct logical_not<void>
586 : {
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); }
594 :
595 : typedef __is_transparent is_transparent;
596 : };
597 : #endif
598 : /** @} */
599 :
600 : #if __cplusplus > 201103L
601 : template<typename _Tp = void>
602 : struct bit_and;
603 :
604 : template<typename _Tp = void>
605 : struct bit_or;
606 :
607 : template<typename _Tp = void>
608 : struct bit_xor;
609 :
610 : template<typename _Tp = void>
611 : struct bit_not;
612 : #endif
613 :
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>
618 : {
619 : _GLIBCXX14_CONSTEXPR
620 : _Tp
621 : operator()(const _Tp& __x, const _Tp& __y) const
622 : { return __x & __y; }
623 : };
624 :
625 : template<typename _Tp>
626 : struct bit_or : public binary_function<_Tp, _Tp, _Tp>
627 : {
628 : _GLIBCXX14_CONSTEXPR
629 : _Tp
630 : operator()(const _Tp& __x, const _Tp& __y) const
631 : { return __x | __y; }
632 : };
633 :
634 : template<typename _Tp>
635 : struct bit_xor : public binary_function<_Tp, _Tp, _Tp>
636 : {
637 : _GLIBCXX14_CONSTEXPR
638 : _Tp
639 : operator()(const _Tp& __x, const _Tp& __y) const
640 : { return __x ^ __y; }
641 : };
642 :
643 : template<typename _Tp>
644 : struct bit_not : public unary_function<_Tp, _Tp>
645 : {
646 : _GLIBCXX14_CONSTEXPR
647 : _Tp
648 : operator()(const _Tp& __x) const
649 : { return ~__x; }
650 : };
651 :
652 : #if __cplusplus > 201103L
653 : template <>
654 : struct bit_and<void>
655 : {
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); }
663 :
664 : typedef __is_transparent is_transparent;
665 : };
666 :
667 : template <>
668 : struct bit_or<void>
669 : {
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); }
677 :
678 : typedef __is_transparent is_transparent;
679 : };
680 :
681 : template <>
682 : struct bit_xor<void>
683 : {
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); }
691 :
692 : typedef __is_transparent is_transparent;
693 : };
694 :
695 : template <>
696 : struct bit_not<void>
697 : {
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); }
705 :
706 : typedef __is_transparent is_transparent;
707 : };
708 : #endif
709 :
710 : // 20.3.5 negators
711 : /** @defgroup negators Negators
712 : * @ingroup functors
713 : *
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.
719 : *
720 : * For example, given a vector of integers and a trivial predicate,
721 : * \code
722 : * struct IntGreaterThanThree
723 : * : public std::unary_function<int, bool>
724 : * {
725 : * bool operator() (int x) { return x > 3; }
726 : * };
727 : *
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.
732 : *
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.
736 : *
737 : * @{
738 : */
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>
743 : {
744 : protected:
745 : _Predicate _M_pred;
746 :
747 : public:
748 : _GLIBCXX14_CONSTEXPR
749 : explicit
750 : unary_negate(const _Predicate& __x) : _M_pred(__x) { }
751 :
752 : _GLIBCXX14_CONSTEXPR
753 : bool
754 : operator()(const typename _Predicate::argument_type& __x) const
755 : { return !_M_pred(__x); }
756 : };
757 :
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); }
764 :
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>
770 : {
771 : protected:
772 : _Predicate _M_pred;
773 :
774 : public:
775 : _GLIBCXX14_CONSTEXPR
776 : explicit
777 : binary_negate(const _Predicate& __x) : _M_pred(__x) { }
778 :
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); }
784 : };
785 :
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 : /** @} */
793 :
794 : // 20.3.7 adaptors pointers functions
795 : /** @defgroup pointer_adaptors Adaptors for pointers to functions
796 : * @ingroup functors
797 : *
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.
803 : *
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().
810 : *
811 : * The function @c ptr_fun takes a pointer-to-function @c f and constructs
812 : * an instance of the appropriate functor.
813 : *
814 : * @{
815 : */
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>
819 : {
820 : protected:
821 : _Result (*_M_ptr)(_Arg);
822 :
823 : public:
824 : pointer_to_unary_function() { }
825 :
826 : explicit
827 : pointer_to_unary_function(_Result (*__x)(_Arg))
828 : : _M_ptr(__x) { }
829 :
830 : _Result
831 : operator()(_Arg __x) const
832 : { return _M_ptr(__x); }
833 : };
834 :
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); }
840 :
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>
845 : {
846 : protected:
847 : _Result (*_M_ptr)(_Arg1, _Arg2);
848 :
849 : public:
850 : pointer_to_binary_function() { }
851 :
852 : explicit
853 : pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
854 : : _M_ptr(__x) { }
855 :
856 : _Result
857 : operator()(_Arg1 __x, _Arg2 __y) const
858 : { return _M_ptr(__x, __y); }
859 : };
860 :
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 : /** @} */
867 :
868 : template<typename _Tp>
869 : struct _Identity
870 : : public unary_function<_Tp,_Tp>
871 : {
872 : _Tp&
873 : operator()(_Tp& __x) const
874 : { return __x; }
875 :
876 : const _Tp&
877 : operator()(const _Tp& __x) const
878 : { return __x; }
879 : };
880 :
881 : template<typename _Pair>
882 : struct _Select1st
883 : : public unary_function<_Pair, typename _Pair::first_type>
884 : {
885 : typename _Pair::first_type&
886 : operator()(_Pair& __x) const
887 : { return __x.first; }
888 :
889 : const typename _Pair::first_type&
890 0 : operator()(const _Pair& __x) const
891 0 : { return __x.first; }
892 :
893 : #if __cplusplus >= 201103L
894 : template<typename _Pair2>
895 : typename _Pair2::first_type&
896 : operator()(_Pair2& __x) const
897 : { return __x.first; }
898 :
899 : template<typename _Pair2>
900 : const typename _Pair2::first_type&
901 : operator()(const _Pair2& __x) const
902 : { return __x.first; }
903 : #endif
904 : };
905 :
906 : template<typename _Pair>
907 : struct _Select2nd
908 : : public unary_function<_Pair, typename _Pair::second_type>
909 : {
910 : typename _Pair::second_type&
911 : operator()(_Pair& __x) const
912 : { return __x.second; }
913 :
914 : const typename _Pair::second_type&
915 : operator()(const _Pair& __x) const
916 : { return __x.second; }
917 : };
918 :
919 : // 20.3.8 adaptors pointers members
920 : /** @defgroup memory_adaptors Adaptors for pointers to members
921 : * @ingroup functors
922 : *
923 : * There are a total of 8 = 2^3 function objects in this family.
924 : * (1) Member functions taking no arguments vs member functions taking
925 : * one argument.
926 : * (2) Call through pointer vs call through reference.
927 : * (3) Const vs non-const member function.
928 : *
929 : * All of this complexity is in the function objects themselves. You can
930 : * ignore it by using the helper function mem_fun and mem_fun_ref,
931 : * which create whichever type of adaptor is appropriate.
932 : *
933 : * @{
934 : */
935 : /// One of the @link memory_adaptors adaptors for member
936 : /// pointers@endlink.
937 : template<typename _Ret, typename _Tp>
938 : class mem_fun_t : public unary_function<_Tp*, _Ret>
939 : {
940 : public:
941 : explicit
942 : mem_fun_t(_Ret (_Tp::*__pf)())
943 : : _M_f(__pf) { }
944 :
945 : _Ret
946 : operator()(_Tp* __p) const
947 : { return (__p->*_M_f)(); }
948 :
949 : private:
950 : _Ret (_Tp::*_M_f)();
951 : };
952 :
953 : /// One of the @link memory_adaptors adaptors for member
954 : /// pointers@endlink.
955 : template<typename _Ret, typename _Tp>
956 : class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
957 : {
958 : public:
959 : explicit
960 : const_mem_fun_t(_Ret (_Tp::*__pf)() const)
961 : : _M_f(__pf) { }
962 :
963 : _Ret
964 : operator()(const _Tp* __p) const
965 : { return (__p->*_M_f)(); }
966 :
967 : private:
968 : _Ret (_Tp::*_M_f)() const;
969 : };
970 :
971 : /// One of the @link memory_adaptors adaptors for member
972 : /// pointers@endlink.
973 : template<typename _Ret, typename _Tp>
974 : class mem_fun_ref_t : public unary_function<_Tp, _Ret>
975 : {
976 : public:
977 : explicit
978 : mem_fun_ref_t(_Ret (_Tp::*__pf)())
979 : : _M_f(__pf) { }
980 :
981 : _Ret
982 : operator()(_Tp& __r) const
983 : { return (__r.*_M_f)(); }
984 :
985 : private:
986 : _Ret (_Tp::*_M_f)();
987 : };
988 :
989 : /// One of the @link memory_adaptors adaptors for member
990 : /// pointers@endlink.
991 : template<typename _Ret, typename _Tp>
992 : class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
993 : {
994 : public:
995 : explicit
996 : const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
997 : : _M_f(__pf) { }
998 :
999 : _Ret
1000 : operator()(const _Tp& __r) const
1001 : { return (__r.*_M_f)(); }
1002 :
1003 : private:
1004 : _Ret (_Tp::*_M_f)() const;
1005 : };
1006 :
1007 : /// One of the @link memory_adaptors adaptors for member
1008 : /// pointers@endlink.
1009 : template<typename _Ret, typename _Tp, typename _Arg>
1010 : class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
1011 : {
1012 : public:
1013 : explicit
1014 : mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
1015 : : _M_f(__pf) { }
1016 :
1017 : _Ret
1018 : operator()(_Tp* __p, _Arg __x) const
1019 : { return (__p->*_M_f)(__x); }
1020 :
1021 : private:
1022 : _Ret (_Tp::*_M_f)(_Arg);
1023 : };
1024 :
1025 : /// One of the @link memory_adaptors adaptors for member
1026 : /// pointers@endlink.
1027 : template<typename _Ret, typename _Tp, typename _Arg>
1028 : class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
1029 : {
1030 : public:
1031 : explicit
1032 : const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
1033 : : _M_f(__pf) { }
1034 :
1035 : _Ret
1036 : operator()(const _Tp* __p, _Arg __x) const
1037 : { return (__p->*_M_f)(__x); }
1038 :
1039 : private:
1040 : _Ret (_Tp::*_M_f)(_Arg) const;
1041 : };
1042 :
1043 : /// One of the @link memory_adaptors adaptors for member
1044 : /// pointers@endlink.
1045 : template<typename _Ret, typename _Tp, typename _Arg>
1046 : class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
1047 : {
1048 : public:
1049 : explicit
1050 : mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
1051 : : _M_f(__pf) { }
1052 :
1053 : _Ret
1054 : operator()(_Tp& __r, _Arg __x) const
1055 : { return (__r.*_M_f)(__x); }
1056 :
1057 : private:
1058 : _Ret (_Tp::*_M_f)(_Arg);
1059 : };
1060 :
1061 : /// One of the @link memory_adaptors adaptors for member
1062 : /// pointers@endlink.
1063 : template<typename _Ret, typename _Tp, typename _Arg>
1064 : class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
1065 : {
1066 : public:
1067 : explicit
1068 : const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
1069 : : _M_f(__pf) { }
1070 :
1071 : _Ret
1072 : operator()(const _Tp& __r, _Arg __x) const
1073 : { return (__r.*_M_f)(__x); }
1074 :
1075 : private:
1076 : _Ret (_Tp::*_M_f)(_Arg) const;
1077 : };
1078 :
1079 : // Mem_fun adaptor helper functions. There are only two:
1080 : // mem_fun and mem_fun_ref.
1081 : template<typename _Ret, typename _Tp>
1082 : inline mem_fun_t<_Ret, _Tp>
1083 : mem_fun(_Ret (_Tp::*__f)())
1084 : { return mem_fun_t<_Ret, _Tp>(__f); }
1085 :
1086 : template<typename _Ret, typename _Tp>
1087 : inline const_mem_fun_t<_Ret, _Tp>
1088 : mem_fun(_Ret (_Tp::*__f)() const)
1089 : { return const_mem_fun_t<_Ret, _Tp>(__f); }
1090 :
1091 : template<typename _Ret, typename _Tp>
1092 : inline mem_fun_ref_t<_Ret, _Tp>
1093 : mem_fun_ref(_Ret (_Tp::*__f)())
1094 : { return mem_fun_ref_t<_Ret, _Tp>(__f); }
1095 :
1096 : template<typename _Ret, typename _Tp>
1097 : inline const_mem_fun_ref_t<_Ret, _Tp>
1098 : mem_fun_ref(_Ret (_Tp::*__f)() const)
1099 : { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
1100 :
1101 : template<typename _Ret, typename _Tp, typename _Arg>
1102 : inline mem_fun1_t<_Ret, _Tp, _Arg>
1103 : mem_fun(_Ret (_Tp::*__f)(_Arg))
1104 : { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
1105 :
1106 : template<typename _Ret, typename _Tp, typename _Arg>
1107 : inline const_mem_fun1_t<_Ret, _Tp, _Arg>
1108 : mem_fun(_Ret (_Tp::*__f)(_Arg) const)
1109 : { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
1110 :
1111 : template<typename _Ret, typename _Tp, typename _Arg>
1112 : inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
1113 : mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
1114 : { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
1115 :
1116 : template<typename _Ret, typename _Tp, typename _Arg>
1117 : inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
1118 : mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
1119 : { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
1120 :
1121 : /** @} */
1122 :
1123 : _GLIBCXX_END_NAMESPACE_VERSION
1124 : } // namespace
1125 :
1126 : #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
1127 : # include <backward/binders.h>
1128 : #endif
1129 :
1130 : #endif /* _STL_FUNCTION_H */
|