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