]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stl_function.h
Implement SD-6: SG10 Feature Test Recommendations
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_function.h
CommitLineData
42526146
PE
1// Functor implementations -*- C++ -*-
2
aa118a03 3// Copyright (C) 2001-2014 Free Software Foundation, Inc.
42526146
PE
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
42526146
PE
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
748086b7
JJ
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.
42526146 19
748086b7
JJ
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/>.
42526146 24
725dc051
BK
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
f910786b 51/** @file bits/stl_function.h
729e3d3f 52 * This is an internal header file, included by other library headers.
f910786b 53 * Do not attempt to use it directly. @headername{functional}
725dc051
BK
54 */
55
046d30f4
PC
56#ifndef _STL_FUNCTION_H
57#define _STL_FUNCTION_H 1
725dc051 58
d081231a
JW
59#if __cplusplus > 201103L
60#include <bits/move.h>
61#endif
62
12ffa228
BK
63namespace std _GLIBCXX_VISIBILITY(default)
64{
65_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 66
f6592a9e 67 // 20.3.1 base classes
aac2878e
BK
68 /** @defgroup functors Function Objects
69 * @ingroup utilities
70 *
f6592a9e
PC
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
2a60a9f6 76 * refer to @a functors, then, generally we include function pointers in
f6592a9e
PC
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 *
5380bc7e 94 * The standard functors are derived from structs named @c unary_function
f6592a9e
PC
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 /**
aac2878e 102 * This is one of the @link functors functor base classes@endlink.
f6592a9e 103 */
bd1a56a0 104 template<typename _Arg, typename _Result>
f6592a9e
PC
105 struct unary_function
106 {
f910786b
BK
107 /// @c argument_type is the type of the argument
108 typedef _Arg argument_type;
f6592a9e 109
f910786b
BK
110 /// @c result_type is the return type
111 typedef _Result result_type;
f6592a9e
PC
112 };
113
114 /**
aac2878e 115 * This is one of the @link functors functor base classes@endlink.
f6592a9e 116 */
bd1a56a0 117 template<typename _Arg1, typename _Arg2, typename _Result>
f6592a9e
PC
118 struct binary_function
119 {
f910786b
BK
120 /// @c first_argument_type is the type of the first argument
121 typedef _Arg1 first_argument_type;
f6592a9e 122
f910786b
BK
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;
ed6814f7 128 };
f6592a9e
PC
129 /** @} */
130
131 // 20.3.2 arithmetic
aac2878e
BK
132 /** @defgroup arithmetic_functors Arithmetic Classes
133 * @ingroup functors
134 *
65be6ddd
BK
135 * Because basic math often needs to be done during an algorithm,
136 * the library provides functors for those operations. See the
aac2878e 137 * documentation for @link functors the base classes@endlink
65be6ddd 138 * for examples of their use.
f6592a9e
PC
139 *
140 * @{
141 */
d081231a
JW
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
aac2878e 165 /// One of the @link arithmetic_functors math functors@endlink.
bd1a56a0 166 template<typename _Tp>
92ff3e43 167 struct plus : public binary_function<_Tp, _Tp, _Tp>
ed6814f7 168 {
f6592a9e
PC
169 _Tp
170 operator()(const _Tp& __x, const _Tp& __y) const
171 { return __x + __y; }
172 };
173
aac2878e 174 /// One of the @link arithmetic_functors math functors@endlink.
bd1a56a0 175 template<typename _Tp>
92ff3e43 176 struct minus : public binary_function<_Tp, _Tp, _Tp>
f6592a9e
PC
177 {
178 _Tp
179 operator()(const _Tp& __x, const _Tp& __y) const
180 { return __x - __y; }
181 };
182
aac2878e 183 /// One of the @link arithmetic_functors math functors@endlink.
bd1a56a0 184 template<typename _Tp>
92ff3e43 185 struct multiplies : public binary_function<_Tp, _Tp, _Tp>
f6592a9e
PC
186 {
187 _Tp
188 operator()(const _Tp& __x, const _Tp& __y) const
189 { return __x * __y; }
190 };
191
aac2878e 192 /// One of the @link arithmetic_functors math functors@endlink.
bd1a56a0 193 template<typename _Tp>
92ff3e43 194 struct divides : public binary_function<_Tp, _Tp, _Tp>
f6592a9e
PC
195 {
196 _Tp
197 operator()(const _Tp& __x, const _Tp& __y) const
198 { return __x / __y; }
199 };
200
aac2878e 201 /// One of the @link arithmetic_functors math functors@endlink.
bd1a56a0 202 template<typename _Tp>
92ff3e43 203 struct modulus : public binary_function<_Tp, _Tp, _Tp>
f6592a9e
PC
204 {
205 _Tp
206 operator()(const _Tp& __x, const _Tp& __y) const
207 { return __x % __y; }
208 };
209
aac2878e 210 /// One of the @link arithmetic_functors math functors@endlink.
bd1a56a0 211 template<typename _Tp>
92ff3e43 212 struct negate : public unary_function<_Tp, _Tp>
f6592a9e
PC
213 {
214 _Tp
215 operator()(const _Tp& __x) const
216 { return -__x; }
217 };
d081231a
JW
218
219#if __cplusplus > 201103L
a15f7cb8
ESR
220
221#define __cpp_lib_transparent_operators 201210
222#define __cpp_lib_generic_associative_lookup 201304
223
d081231a
JW
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
f6592a9e
PC
307 /** @} */
308
309 // 20.3.3 comparisons
aac2878e
BK
310 /** @defgroup comparison_functors Comparison Classes
311 * @ingroup functors
312 *
f6592a9e
PC
313 * The library provides six wrapper functors for all the basic comparisons
314 * in C++, like @c <.
315 *
316 * @{
317 */
d081231a
JW
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
aac2878e 338 /// One of the @link comparison_functors comparison functors@endlink.
bd1a56a0 339 template<typename _Tp>
ed6814f7 340 struct equal_to : public binary_function<_Tp, _Tp, bool>
f6592a9e
PC
341 {
342 bool
343 operator()(const _Tp& __x, const _Tp& __y) const
344 { return __x == __y; }
345 };
346
aac2878e 347 /// One of the @link comparison_functors comparison functors@endlink.
bd1a56a0 348 template<typename _Tp>
ed6814f7 349 struct not_equal_to : public binary_function<_Tp, _Tp, bool>
f6592a9e
PC
350 {
351 bool
352 operator()(const _Tp& __x, const _Tp& __y) const
353 { return __x != __y; }
354 };
355
aac2878e 356 /// One of the @link comparison_functors comparison functors@endlink.
bd1a56a0 357 template<typename _Tp>
92ff3e43 358 struct greater : public binary_function<_Tp, _Tp, bool>
f6592a9e
PC
359 {
360 bool
361 operator()(const _Tp& __x, const _Tp& __y) const
362 { return __x > __y; }
363 };
364
aac2878e 365 /// One of the @link comparison_functors comparison functors@endlink.
bd1a56a0 366 template<typename _Tp>
92ff3e43 367 struct less : public binary_function<_Tp, _Tp, bool>
f6592a9e
PC
368 {
369 bool
370 operator()(const _Tp& __x, const _Tp& __y) const
371 { return __x < __y; }
372 };
373
aac2878e 374 /// One of the @link comparison_functors comparison functors@endlink.
bd1a56a0 375 template<typename _Tp>
92ff3e43 376 struct greater_equal : public binary_function<_Tp, _Tp, bool>
f6592a9e
PC
377 {
378 bool
379 operator()(const _Tp& __x, const _Tp& __y) const
380 { return __x >= __y; }
381 };
382
aac2878e 383 /// One of the @link comparison_functors comparison functors@endlink.
bd1a56a0 384 template<typename _Tp>
92ff3e43 385 struct less_equal : public binary_function<_Tp, _Tp, bool>
f6592a9e
PC
386 {
387 bool
388 operator()(const _Tp& __x, const _Tp& __y) const
389 { return __x <= __y; }
390 };
d081231a
JW
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
f6592a9e 477 /** @} */
ed6814f7 478
f6592a9e 479 // 20.3.4 logical operations
aac2878e
BK
480 /** @defgroup logical_functors Boolean Operations Classes
481 * @ingroup functors
482 *
65be6ddd
BK
483 * Here are wrapper functors for Boolean operations: @c &&, @c ||,
484 * and @c !.
f6592a9e
PC
485 *
486 * @{
487 */
d081231a
JW
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
aac2878e 499 /// One of the @link logical_functors Boolean operations functors@endlink.
bd1a56a0 500 template<typename _Tp>
92ff3e43 501 struct logical_and : public binary_function<_Tp, _Tp, bool>
f6592a9e
PC
502 {
503 bool
504 operator()(const _Tp& __x, const _Tp& __y) const
505 { return __x && __y; }
506 };
507
aac2878e 508 /// One of the @link logical_functors Boolean operations functors@endlink.
bd1a56a0 509 template<typename _Tp>
92ff3e43 510 struct logical_or : public binary_function<_Tp, _Tp, bool>
f6592a9e
PC
511 {
512 bool
513 operator()(const _Tp& __x, const _Tp& __y) const
514 { return __x || __y; }
515 };
516
aac2878e 517 /// One of the @link logical_functors Boolean operations functors@endlink.
bd1a56a0 518 template<typename _Tp>
92ff3e43 519 struct logical_not : public unary_function<_Tp, bool>
f6592a9e
PC
520 {
521 bool
522 operator()(const _Tp& __x) const
523 { return !__x; }
524 };
d081231a
JW
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
bef49287
JW
561 operator()(_Tp&& __t) const
562 noexcept(noexcept(!std::forward<_Tp>(__t)))
563 -> decltype(!std::forward<_Tp>(__t))
d081231a
JW
564 { return !std::forward<_Tp>(__t); }
565
566 typedef __is_transparent is_transparent;
567 };
568#endif
f6592a9e
PC
569 /** @} */
570
d081231a
JW
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
2ee0c1fb
PC
585 // _GLIBCXX_RESOLVE_LIB_DEFECTS
586 // DR 660. Missing Bitwise Operations.
bd1a56a0 587 template<typename _Tp>
2ee0c1fb
PC
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
bd1a56a0 595 template<typename _Tp>
2ee0c1fb
PC
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
bd1a56a0 603 template<typename _Tp>
2ee0c1fb
PC
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
d081231a
JW
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
f6592a9e 673 // 20.3.5 negators
aac2878e
BK
674 /** @defgroup negators Negators
675 * @ingroup functors
676 *
f6592a9e
PC
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 * };
ed6814f7 690 *
f6592a9e
PC
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
2a60a9f6 694 * <code>!(v[i] > 3)</code> is true.
f6592a9e
PC
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 */
aac2878e 702 /// One of the @link negators negation functors@endlink.
bd1a56a0 703 template<typename _Predicate>
f6592a9e
PC
704 class unary_negate
705 : public unary_function<typename _Predicate::argument_type, bool>
706 {
707 protected:
708 _Predicate _M_pred;
bd1a56a0 709
f6592a9e 710 public:
7c920151 711 explicit
bd1a56a0 712 unary_negate(const _Predicate& __x) : _M_pred(__x) { }
ed6814f7 713
f6592a9e
PC
714 bool
715 operator()(const typename _Predicate::argument_type& __x) const
716 { return !_M_pred(__x); }
717 };
718
aac2878e 719 /// One of the @link negators negation functors@endlink.
bd1a56a0 720 template<typename _Predicate>
ed6814f7 721 inline unary_negate<_Predicate>
f6592a9e
PC
722 not1(const _Predicate& __pred)
723 { return unary_negate<_Predicate>(__pred); }
724
aac2878e 725 /// One of the @link negators negation functors@endlink.
bd1a56a0 726 template<typename _Predicate>
ed6814f7 727 class binary_negate
f6592a9e 728 : public binary_function<typename _Predicate::first_argument_type,
65be6ddd 729 typename _Predicate::second_argument_type, bool>
f6592a9e
PC
730 {
731 protected:
732 _Predicate _M_pred;
65be6ddd 733
f6592a9e 734 public:
7c920151 735 explicit
65be6ddd 736 binary_negate(const _Predicate& __x) : _M_pred(__x) { }
f6592a9e
PC
737
738 bool
ed6814f7 739 operator()(const typename _Predicate::first_argument_type& __x,
f6592a9e
PC
740 const typename _Predicate::second_argument_type& __y) const
741 { return !_M_pred(__x, __y); }
742 };
743
aac2878e 744 /// One of the @link negators negation functors@endlink.
bd1a56a0 745 template<typename _Predicate>
ed6814f7 746 inline binary_negate<_Predicate>
f6592a9e
PC
747 not2(const _Predicate& __pred)
748 { return binary_negate<_Predicate>(__pred); }
749 /** @} */
ed6814f7 750
f6592a9e 751 // 20.3.7 adaptors pointers functions
aac2878e
BK
752 /** @defgroup pointer_adaptors Adaptors for pointers to functions
753 * @ingroup functors
754 *
f6592a9e
PC
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 */
aac2878e 773 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
bd1a56a0 774 template<typename _Arg, typename _Result>
f6592a9e
PC
775 class pointer_to_unary_function : public unary_function<_Arg, _Result>
776 {
777 protected:
778 _Result (*_M_ptr)(_Arg);
bd1a56a0 779
f6592a9e 780 public:
bd1a56a0 781 pointer_to_unary_function() { }
ed6814f7 782
7c920151
PC
783 explicit
784 pointer_to_unary_function(_Result (*__x)(_Arg))
bd1a56a0 785 : _M_ptr(__x) { }
725dc051 786
f6592a9e
PC
787 _Result
788 operator()(_Arg __x) const
789 { return _M_ptr(__x); }
790 };
791
aac2878e 792 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
bd1a56a0 793 template<typename _Arg, typename _Result>
f6592a9e
PC
794 inline pointer_to_unary_function<_Arg, _Result>
795 ptr_fun(_Result (*__x)(_Arg))
796 { return pointer_to_unary_function<_Arg, _Result>(__x); }
797
aac2878e 798 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
bd1a56a0 799 template<typename _Arg1, typename _Arg2, typename _Result>
f6592a9e 800 class pointer_to_binary_function
afd4cbbb 801 : public binary_function<_Arg1, _Arg2, _Result>
f6592a9e
PC
802 {
803 protected:
804 _Result (*_M_ptr)(_Arg1, _Arg2);
bd1a56a0 805
f6592a9e 806 public:
bd1a56a0 807 pointer_to_binary_function() { }
f6592a9e 808
7c920151 809 explicit
ed6814f7 810 pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
bd1a56a0 811 : _M_ptr(__x) { }
725dc051 812
f6592a9e
PC
813 _Result
814 operator()(_Arg1 __x, _Arg2 __y) const
815 { return _M_ptr(__x, __y); }
816 };
817
aac2878e 818 /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
bd1a56a0 819 template<typename _Arg1, typename _Arg2, typename _Result>
ed6814f7 820 inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
f6592a9e
PC
821 ptr_fun(_Result (*__x)(_Arg1, _Arg2))
822 { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
823 /** @} */
ed6814f7 824
bd1a56a0 825 template<typename _Tp>
b9b09214 826 struct _Identity
b9b09214 827 : public unary_function<_Tp,_Tp>
f6592a9e
PC
828 {
829 _Tp&
830 operator()(_Tp& __x) const
831 { return __x; }
ed6814f7 832
f6592a9e
PC
833 const _Tp&
834 operator()(const _Tp& __x) const
835 { return __x; }
836 };
837
bd1a56a0 838 template<typename _Pair>
b9b09214 839 struct _Select1st
b9b09214 840 : public unary_function<_Pair, typename _Pair::first_type>
f6592a9e
PC
841 {
842 typename _Pair::first_type&
843 operator()(_Pair& __x) const
844 { return __x.first; }
ed6814f7 845
f6592a9e
PC
846 const typename _Pair::first_type&
847 operator()(const _Pair& __x) const
848 { return __x.first; }
777a1e28 849
734f5023 850#if __cplusplus >= 201103L
777a1e28
PC
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
f6592a9e
PC
861 };
862
bd1a56a0 863 template<typename _Pair>
b9b09214 864 struct _Select2nd
b9b09214 865 : public unary_function<_Pair, typename _Pair::second_type>
f6592a9e
PC
866 {
867 typename _Pair::second_type&
868 operator()(_Pair& __x) const
869 { return __x.second; }
ed6814f7 870
f6592a9e
PC
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
aac2878e
BK
877 /** @defgroup memory_adaptors Adaptors for pointers to members
878 * @ingroup functors
879 *
d5f261c1 880 * There are a total of 8 = 2^3 function objects in this family.
f6592a9e
PC
881 * (1) Member functions taking no arguments vs member functions taking
882 * one argument.
883 * (2) Call through pointer vs call through reference.
d5f261c1 884 * (3) Const vs non-const member function.
f6592a9e
PC
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 */
aac2878e 892 /// One of the @link memory_adaptors adaptors for member
65be6ddd 893 /// pointers@endlink.
bd1a56a0 894 template<typename _Ret, typename _Tp>
f6592a9e
PC
895 class mem_fun_t : public unary_function<_Tp*, _Ret>
896 {
897 public:
7c920151
PC
898 explicit
899 mem_fun_t(_Ret (_Tp::*__pf)())
bd1a56a0 900 : _M_f(__pf) { }
f6592a9e
PC
901
902 _Ret
903 operator()(_Tp* __p) const
904 { return (__p->*_M_f)(); }
bd1a56a0 905
f6592a9e
PC
906 private:
907 _Ret (_Tp::*_M_f)();
908 };
909
aac2878e 910 /// One of the @link memory_adaptors adaptors for member
65be6ddd 911 /// pointers@endlink.
bd1a56a0 912 template<typename _Ret, typename _Tp>
f6592a9e
PC
913 class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
914 {
915 public:
7c920151
PC
916 explicit
917 const_mem_fun_t(_Ret (_Tp::*__pf)() const)
bd1a56a0 918 : _M_f(__pf) { }
f6592a9e
PC
919
920 _Ret
921 operator()(const _Tp* __p) const
922 { return (__p->*_M_f)(); }
bd1a56a0 923
f6592a9e
PC
924 private:
925 _Ret (_Tp::*_M_f)() const;
926 };
927
aac2878e 928 /// One of the @link memory_adaptors adaptors for member
65be6ddd 929 /// pointers@endlink.
bd1a56a0 930 template<typename _Ret, typename _Tp>
f6592a9e
PC
931 class mem_fun_ref_t : public unary_function<_Tp, _Ret>
932 {
933 public:
7c920151
PC
934 explicit
935 mem_fun_ref_t(_Ret (_Tp::*__pf)())
bd1a56a0 936 : _M_f(__pf) { }
f6592a9e
PC
937
938 _Ret
939 operator()(_Tp& __r) const
940 { return (__r.*_M_f)(); }
bd1a56a0 941
f6592a9e
PC
942 private:
943 _Ret (_Tp::*_M_f)();
944 };
ed6814f7 945
aac2878e 946 /// One of the @link memory_adaptors adaptors for member
65be6ddd 947 /// pointers@endlink.
bd1a56a0 948 template<typename _Ret, typename _Tp>
f6592a9e
PC
949 class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
950 {
951 public:
7c920151
PC
952 explicit
953 const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
bd1a56a0 954 : _M_f(__pf) { }
f6592a9e
PC
955
956 _Ret
957 operator()(const _Tp& __r) const
958 { return (__r.*_M_f)(); }
bd1a56a0 959
f6592a9e
PC
960 private:
961 _Ret (_Tp::*_M_f)() const;
962 };
963
aac2878e 964 /// One of the @link memory_adaptors adaptors for member
65be6ddd 965 /// pointers@endlink.
bd1a56a0 966 template<typename _Ret, typename _Tp, typename _Arg>
f6592a9e
PC
967 class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
968 {
969 public:
7c920151 970 explicit
ed6814f7 971 mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
bd1a56a0 972 : _M_f(__pf) { }
f6592a9e
PC
973
974 _Ret
975 operator()(_Tp* __p, _Arg __x) const
976 { return (__p->*_M_f)(__x); }
bd1a56a0 977
f6592a9e
PC
978 private:
979 _Ret (_Tp::*_M_f)(_Arg);
980 };
981
aac2878e 982 /// One of the @link memory_adaptors adaptors for member
65be6ddd 983 /// pointers@endlink.
bd1a56a0 984 template<typename _Ret, typename _Tp, typename _Arg>
f6592a9e
PC
985 class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
986 {
987 public:
7c920151
PC
988 explicit
989 const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
bd1a56a0 990 : _M_f(__pf) { }
f6592a9e
PC
991
992 _Ret
993 operator()(const _Tp* __p, _Arg __x) const
994 { return (__p->*_M_f)(__x); }
bd1a56a0 995
f6592a9e
PC
996 private:
997 _Ret (_Tp::*_M_f)(_Arg) const;
998 };
999
aac2878e 1000 /// One of the @link memory_adaptors adaptors for member
65be6ddd 1001 /// pointers@endlink.
bd1a56a0 1002 template<typename _Ret, typename _Tp, typename _Arg>
f6592a9e
PC
1003 class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
1004 {
1005 public:
7c920151
PC
1006 explicit
1007 mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
bd1a56a0 1008 : _M_f(__pf) { }
f6592a9e
PC
1009
1010 _Ret
1011 operator()(_Tp& __r, _Arg __x) const
1012 { return (__r.*_M_f)(__x); }
bd1a56a0 1013
f6592a9e
PC
1014 private:
1015 _Ret (_Tp::*_M_f)(_Arg);
1016 };
1017
aac2878e 1018 /// One of the @link memory_adaptors adaptors for member
65be6ddd 1019 /// pointers@endlink.
bd1a56a0 1020 template<typename _Ret, typename _Tp, typename _Arg>
f6592a9e
PC
1021 class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
1022 {
1023 public:
7c920151
PC
1024 explicit
1025 const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
bd1a56a0 1026 : _M_f(__pf) { }
f6592a9e
PC
1027
1028 _Ret
1029 operator()(const _Tp& __r, _Arg __x) const
1030 { return (__r.*_M_f)(__x); }
bd1a56a0 1031
f6592a9e
PC
1032 private:
1033 _Ret (_Tp::*_M_f)(_Arg) const;
1034 };
ed6814f7 1035
f6592a9e
PC
1036 // Mem_fun adaptor helper functions. There are only two:
1037 // mem_fun and mem_fun_ref.
bd1a56a0 1038 template<typename _Ret, typename _Tp>
92ff3e43 1039 inline mem_fun_t<_Ret, _Tp>
f6592a9e 1040 mem_fun(_Ret (_Tp::*__f)())
92ff3e43 1041 { return mem_fun_t<_Ret, _Tp>(__f); }
f6592a9e 1042
bd1a56a0 1043 template<typename _Ret, typename _Tp>
92ff3e43 1044 inline const_mem_fun_t<_Ret, _Tp>
f6592a9e 1045 mem_fun(_Ret (_Tp::*__f)() const)
92ff3e43 1046 { return const_mem_fun_t<_Ret, _Tp>(__f); }
ed6814f7 1047
bd1a56a0 1048 template<typename _Ret, typename _Tp>
92ff3e43 1049 inline mem_fun_ref_t<_Ret, _Tp>
ed6814f7 1050 mem_fun_ref(_Ret (_Tp::*__f)())
92ff3e43 1051 { return mem_fun_ref_t<_Ret, _Tp>(__f); }
f6592a9e 1052
bd1a56a0 1053 template<typename _Ret, typename _Tp>
92ff3e43 1054 inline const_mem_fun_ref_t<_Ret, _Tp>
f6592a9e 1055 mem_fun_ref(_Ret (_Tp::*__f)() const)
92ff3e43 1056 { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
f6592a9e 1057
bd1a56a0 1058 template<typename _Ret, typename _Tp, typename _Arg>
92ff3e43 1059 inline mem_fun1_t<_Ret, _Tp, _Arg>
f6592a9e 1060 mem_fun(_Ret (_Tp::*__f)(_Arg))
92ff3e43 1061 { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
f6592a9e 1062
bd1a56a0 1063 template<typename _Ret, typename _Tp, typename _Arg>
92ff3e43 1064 inline const_mem_fun1_t<_Ret, _Tp, _Arg>
f6592a9e 1065 mem_fun(_Ret (_Tp::*__f)(_Arg) const)
92ff3e43 1066 { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
f6592a9e 1067
bd1a56a0 1068 template<typename _Ret, typename _Tp, typename _Arg>
92ff3e43 1069 inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
f6592a9e 1070 mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
92ff3e43 1071 { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
f6592a9e 1072
bd1a56a0 1073 template<typename _Ret, typename _Tp, typename _Arg>
92ff3e43 1074 inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
f6592a9e 1075 mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
92ff3e43 1076 { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
f6592a9e
PC
1077
1078 /** @} */
ed6814f7 1079
12ffa228
BK
1080_GLIBCXX_END_NAMESPACE_VERSION
1081} // namespace
725dc051 1082
734f5023 1083#if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
40abbf1f
BK
1084# include <backward/binders.h>
1085#endif
1086
046d30f4 1087#endif /* _STL_FUNCTION_H */