]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Functor implementations -*- C++ -*- |
2 | ||
8d9254fc | 3 | // Copyright (C) 2001-2020 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 |
63 | namespace 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 | { |
8dff34fe | 169 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
170 | _Tp |
171 | operator()(const _Tp& __x, const _Tp& __y) const | |
172 | { return __x + __y; } | |
173 | }; | |
174 | ||
aac2878e | 175 | /// One of the @link arithmetic_functors math functors@endlink. |
bd1a56a0 | 176 | template<typename _Tp> |
92ff3e43 | 177 | struct minus : public binary_function<_Tp, _Tp, _Tp> |
f6592a9e | 178 | { |
8dff34fe | 179 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
180 | _Tp |
181 | operator()(const _Tp& __x, const _Tp& __y) const | |
182 | { return __x - __y; } | |
183 | }; | |
184 | ||
aac2878e | 185 | /// One of the @link arithmetic_functors math functors@endlink. |
bd1a56a0 | 186 | template<typename _Tp> |
92ff3e43 | 187 | struct multiplies : public binary_function<_Tp, _Tp, _Tp> |
f6592a9e | 188 | { |
8dff34fe | 189 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
190 | _Tp |
191 | operator()(const _Tp& __x, const _Tp& __y) const | |
192 | { return __x * __y; } | |
193 | }; | |
194 | ||
aac2878e | 195 | /// One of the @link arithmetic_functors math functors@endlink. |
bd1a56a0 | 196 | template<typename _Tp> |
92ff3e43 | 197 | struct divides : public binary_function<_Tp, _Tp, _Tp> |
f6592a9e | 198 | { |
8dff34fe | 199 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
200 | _Tp |
201 | operator()(const _Tp& __x, const _Tp& __y) const | |
202 | { return __x / __y; } | |
203 | }; | |
204 | ||
aac2878e | 205 | /// One of the @link arithmetic_functors math functors@endlink. |
bd1a56a0 | 206 | template<typename _Tp> |
92ff3e43 | 207 | struct modulus : public binary_function<_Tp, _Tp, _Tp> |
f6592a9e | 208 | { |
8dff34fe | 209 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
210 | _Tp |
211 | operator()(const _Tp& __x, const _Tp& __y) const | |
212 | { return __x % __y; } | |
213 | }; | |
214 | ||
aac2878e | 215 | /// One of the @link arithmetic_functors math functors@endlink. |
bd1a56a0 | 216 | template<typename _Tp> |
92ff3e43 | 217 | struct negate : public unary_function<_Tp, _Tp> |
f6592a9e | 218 | { |
8dff34fe | 219 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
220 | _Tp |
221 | operator()(const _Tp& __x) const | |
222 | { return -__x; } | |
223 | }; | |
d081231a JW |
224 | |
225 | #if __cplusplus > 201103L | |
a15f7cb8 | 226 | |
b7dabce5 | 227 | #define __cpp_lib_transparent_operators 201510 |
a15f7cb8 | 228 | |
d081231a JW |
229 | template<> |
230 | struct plus<void> | |
231 | { | |
232 | template <typename _Tp, typename _Up> | |
8dff34fe | 233 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
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> | |
8dff34fe | 248 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
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> | |
8dff34fe | 263 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
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> | |
8dff34fe | 278 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
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> | |
8dff34fe | 293 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
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> | |
8dff34fe | 308 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
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 | |
f6592a9e PC |
318 | /** @} */ |
319 | ||
320 | // 20.3.3 comparisons | |
aac2878e BK |
321 | /** @defgroup comparison_functors Comparison Classes |
322 | * @ingroup functors | |
323 | * | |
f6592a9e PC |
324 | * The library provides six wrapper functors for all the basic comparisons |
325 | * in C++, like @c <. | |
326 | * | |
327 | * @{ | |
328 | */ | |
d081231a JW |
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 | ||
aac2878e | 349 | /// One of the @link comparison_functors comparison functors@endlink. |
bd1a56a0 | 350 | template<typename _Tp> |
ed6814f7 | 351 | struct equal_to : public binary_function<_Tp, _Tp, bool> |
f6592a9e | 352 | { |
8dff34fe | 353 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
354 | bool |
355 | operator()(const _Tp& __x, const _Tp& __y) const | |
356 | { return __x == __y; } | |
357 | }; | |
358 | ||
aac2878e | 359 | /// One of the @link comparison_functors comparison functors@endlink. |
bd1a56a0 | 360 | template<typename _Tp> |
ed6814f7 | 361 | struct not_equal_to : public binary_function<_Tp, _Tp, bool> |
f6592a9e | 362 | { |
8dff34fe | 363 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
364 | bool |
365 | operator()(const _Tp& __x, const _Tp& __y) const | |
366 | { return __x != __y; } | |
367 | }; | |
368 | ||
aac2878e | 369 | /// One of the @link comparison_functors comparison functors@endlink. |
bd1a56a0 | 370 | template<typename _Tp> |
92ff3e43 | 371 | struct greater : public binary_function<_Tp, _Tp, bool> |
f6592a9e | 372 | { |
8dff34fe | 373 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
374 | bool |
375 | operator()(const _Tp& __x, const _Tp& __y) const | |
376 | { return __x > __y; } | |
377 | }; | |
378 | ||
aac2878e | 379 | /// One of the @link comparison_functors comparison functors@endlink. |
bd1a56a0 | 380 | template<typename _Tp> |
92ff3e43 | 381 | struct less : public binary_function<_Tp, _Tp, bool> |
f6592a9e | 382 | { |
8dff34fe | 383 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
384 | bool |
385 | operator()(const _Tp& __x, const _Tp& __y) const | |
386 | { return __x < __y; } | |
387 | }; | |
388 | ||
aac2878e | 389 | /// One of the @link comparison_functors comparison functors@endlink. |
bd1a56a0 | 390 | template<typename _Tp> |
92ff3e43 | 391 | struct greater_equal : public binary_function<_Tp, _Tp, bool> |
f6592a9e | 392 | { |
8dff34fe | 393 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
394 | bool |
395 | operator()(const _Tp& __x, const _Tp& __y) const | |
396 | { return __x >= __y; } | |
397 | }; | |
398 | ||
aac2878e | 399 | /// One of the @link comparison_functors comparison functors@endlink. |
bd1a56a0 | 400 | template<typename _Tp> |
92ff3e43 | 401 | struct less_equal : public binary_function<_Tp, _Tp, bool> |
f6592a9e | 402 | { |
8dff34fe | 403 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
404 | bool |
405 | operator()(const _Tp& __x, const _Tp& __y) const | |
406 | { return __x <= __y; } | |
407 | }; | |
d081231a | 408 | |
0b3ec8f4 JW |
409 | // Partial specialization of std::greater for pointers. |
410 | template<typename _Tp> | |
411 | struct greater<_Tp*> : public binary_function<_Tp*, _Tp*, bool> | |
412 | { | |
413 | _GLIBCXX14_CONSTEXPR bool | |
414 | operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW | |
415 | { | |
6cdf1946 JJ |
416 | #if __cplusplus >= 201402L |
417 | #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED | |
418 | if (__builtin_is_constant_evaluated()) | |
419 | #else | |
420 | if (__builtin_constant_p(__x > __y)) | |
421 | #endif | |
0b3ec8f4 | 422 | return __x > __y; |
6cdf1946 | 423 | #endif |
0b3ec8f4 JW |
424 | return (__UINTPTR_TYPE__)__x > (__UINTPTR_TYPE__)__y; |
425 | } | |
426 | }; | |
427 | ||
428 | // Partial specialization of std::less for pointers. | |
429 | template<typename _Tp> | |
430 | struct less<_Tp*> : public binary_function<_Tp*, _Tp*, bool> | |
431 | { | |
432 | _GLIBCXX14_CONSTEXPR bool | |
433 | operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW | |
434 | { | |
6cdf1946 JJ |
435 | #if __cplusplus >= 201402L |
436 | #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED | |
437 | if (__builtin_is_constant_evaluated()) | |
438 | #else | |
439 | if (__builtin_constant_p(__x < __y)) | |
440 | #endif | |
0b3ec8f4 | 441 | return __x < __y; |
6cdf1946 | 442 | #endif |
0b3ec8f4 JW |
443 | return (__UINTPTR_TYPE__)__x < (__UINTPTR_TYPE__)__y; |
444 | } | |
445 | }; | |
446 | ||
447 | // Partial specialization of std::greater_equal for pointers. | |
448 | template<typename _Tp> | |
449 | struct greater_equal<_Tp*> : public binary_function<_Tp*, _Tp*, bool> | |
450 | { | |
451 | _GLIBCXX14_CONSTEXPR bool | |
452 | operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW | |
453 | { | |
6cdf1946 JJ |
454 | #if __cplusplus >= 201402L |
455 | #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED | |
456 | if (__builtin_is_constant_evaluated()) | |
457 | #else | |
458 | if (__builtin_constant_p(__x >= __y)) | |
459 | #endif | |
0b3ec8f4 | 460 | return __x >= __y; |
6cdf1946 | 461 | #endif |
0b3ec8f4 JW |
462 | return (__UINTPTR_TYPE__)__x >= (__UINTPTR_TYPE__)__y; |
463 | } | |
464 | }; | |
465 | ||
466 | // Partial specialization of std::less_equal for pointers. | |
467 | template<typename _Tp> | |
468 | struct less_equal<_Tp*> : public binary_function<_Tp*, _Tp*, bool> | |
469 | { | |
470 | _GLIBCXX14_CONSTEXPR bool | |
471 | operator()(_Tp* __x, _Tp* __y) const _GLIBCXX_NOTHROW | |
472 | { | |
6cdf1946 JJ |
473 | #if __cplusplus >= 201402L |
474 | #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED | |
475 | if (__builtin_is_constant_evaluated()) | |
476 | #else | |
477 | if (__builtin_constant_p(__x <= __y)) | |
478 | #endif | |
0b3ec8f4 | 479 | return __x <= __y; |
6cdf1946 | 480 | #endif |
0b3ec8f4 JW |
481 | return (__UINTPTR_TYPE__)__x <= (__UINTPTR_TYPE__)__y; |
482 | } | |
483 | }; | |
484 | ||
485 | #if __cplusplus >= 201402L | |
d081231a JW |
486 | /// One of the @link comparison_functors comparison functors@endlink. |
487 | template<> | |
488 | struct equal_to<void> | |
489 | { | |
490 | template <typename _Tp, typename _Up> | |
0b3ec8f4 | 491 | constexpr auto |
d081231a JW |
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 | ||
500 | /// One of the @link comparison_functors comparison functors@endlink. | |
501 | template<> | |
502 | struct not_equal_to<void> | |
503 | { | |
504 | template <typename _Tp, typename _Up> | |
0b3ec8f4 | 505 | constexpr auto |
d081231a JW |
506 | operator()(_Tp&& __t, _Up&& __u) const |
507 | noexcept(noexcept(std::forward<_Tp>(__t) != std::forward<_Up>(__u))) | |
508 | -> decltype(std::forward<_Tp>(__t) != std::forward<_Up>(__u)) | |
509 | { return std::forward<_Tp>(__t) != std::forward<_Up>(__u); } | |
510 | ||
511 | typedef __is_transparent is_transparent; | |
512 | }; | |
513 | ||
514 | /// One of the @link comparison_functors comparison functors@endlink. | |
515 | template<> | |
516 | struct greater<void> | |
517 | { | |
518 | template <typename _Tp, typename _Up> | |
0b3ec8f4 | 519 | constexpr auto |
d081231a JW |
520 | operator()(_Tp&& __t, _Up&& __u) const |
521 | noexcept(noexcept(std::forward<_Tp>(__t) > std::forward<_Up>(__u))) | |
522 | -> decltype(std::forward<_Tp>(__t) > std::forward<_Up>(__u)) | |
0b3ec8f4 JW |
523 | { |
524 | return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u), | |
525 | __ptr_cmp<_Tp, _Up>{}); | |
526 | } | |
527 | ||
528 | template<typename _Tp, typename _Up> | |
529 | constexpr bool | |
530 | operator()(_Tp* __t, _Up* __u) const noexcept | |
531 | { return greater<common_type_t<_Tp*, _Up*>>{}(__t, __u); } | |
d081231a JW |
532 | |
533 | typedef __is_transparent is_transparent; | |
0b3ec8f4 JW |
534 | |
535 | private: | |
536 | template <typename _Tp, typename _Up> | |
537 | static constexpr decltype(auto) | |
538 | _S_cmp(_Tp&& __t, _Up&& __u, false_type) | |
539 | { return std::forward<_Tp>(__t) > std::forward<_Up>(__u); } | |
540 | ||
541 | template <typename _Tp, typename _Up> | |
542 | static constexpr bool | |
543 | _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept | |
544 | { | |
545 | return greater<const volatile void*>{}( | |
546 | static_cast<const volatile void*>(std::forward<_Tp>(__t)), | |
547 | static_cast<const volatile void*>(std::forward<_Up>(__u))); | |
548 | } | |
549 | ||
7c69a7d9 | 550 | // True if there is no viable operator> member function. |
0b3ec8f4 JW |
551 | template<typename _Tp, typename _Up, typename = void> |
552 | struct __not_overloaded2 : true_type { }; | |
553 | ||
554 | // False if we can call T.operator>(U) | |
555 | template<typename _Tp, typename _Up> | |
556 | struct __not_overloaded2<_Tp, _Up, __void_t< | |
557 | decltype(std::declval<_Tp>().operator>(std::declval<_Up>()))>> | |
558 | : false_type { }; | |
559 | ||
7c69a7d9 JW |
560 | // True if there is no overloaded operator> for these operands. |
561 | template<typename _Tp, typename _Up, typename = void> | |
562 | struct __not_overloaded : __not_overloaded2<_Tp, _Up> { }; | |
563 | ||
564 | // False if we can call operator>(T,U) | |
0b3ec8f4 | 565 | template<typename _Tp, typename _Up> |
7c69a7d9 JW |
566 | struct __not_overloaded<_Tp, _Up, __void_t< |
567 | decltype(operator>(std::declval<_Tp>(), std::declval<_Up>()))>> | |
568 | : false_type { }; | |
0b3ec8f4 JW |
569 | |
570 | template<typename _Tp, typename _Up> | |
571 | using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>, | |
572 | is_convertible<_Tp, const volatile void*>, | |
573 | is_convertible<_Up, const volatile void*>>; | |
d081231a JW |
574 | }; |
575 | ||
576 | /// One of the @link comparison_functors comparison functors@endlink. | |
577 | template<> | |
578 | struct less<void> | |
579 | { | |
580 | template <typename _Tp, typename _Up> | |
0b3ec8f4 | 581 | constexpr auto |
d081231a JW |
582 | operator()(_Tp&& __t, _Up&& __u) const |
583 | noexcept(noexcept(std::forward<_Tp>(__t) < std::forward<_Up>(__u))) | |
584 | -> decltype(std::forward<_Tp>(__t) < std::forward<_Up>(__u)) | |
0b3ec8f4 JW |
585 | { |
586 | return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u), | |
587 | __ptr_cmp<_Tp, _Up>{}); | |
588 | } | |
589 | ||
590 | template<typename _Tp, typename _Up> | |
591 | constexpr bool | |
592 | operator()(_Tp* __t, _Up* __u) const noexcept | |
593 | { return less<common_type_t<_Tp*, _Up*>>{}(__t, __u); } | |
d081231a JW |
594 | |
595 | typedef __is_transparent is_transparent; | |
0b3ec8f4 JW |
596 | |
597 | private: | |
598 | template <typename _Tp, typename _Up> | |
599 | static constexpr decltype(auto) | |
600 | _S_cmp(_Tp&& __t, _Up&& __u, false_type) | |
601 | { return std::forward<_Tp>(__t) < std::forward<_Up>(__u); } | |
602 | ||
603 | template <typename _Tp, typename _Up> | |
604 | static constexpr bool | |
605 | _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept | |
606 | { | |
607 | return less<const volatile void*>{}( | |
608 | static_cast<const volatile void*>(std::forward<_Tp>(__t)), | |
609 | static_cast<const volatile void*>(std::forward<_Up>(__u))); | |
610 | } | |
611 | ||
7c69a7d9 | 612 | // True if there is no viable operator< member function. |
0b3ec8f4 JW |
613 | template<typename _Tp, typename _Up, typename = void> |
614 | struct __not_overloaded2 : true_type { }; | |
615 | ||
616 | // False if we can call T.operator<(U) | |
617 | template<typename _Tp, typename _Up> | |
618 | struct __not_overloaded2<_Tp, _Up, __void_t< | |
619 | decltype(std::declval<_Tp>().operator<(std::declval<_Up>()))>> | |
620 | : false_type { }; | |
621 | ||
7c69a7d9 JW |
622 | // True if there is no overloaded operator< for these operands. |
623 | template<typename _Tp, typename _Up, typename = void> | |
624 | struct __not_overloaded : __not_overloaded2<_Tp, _Up> { }; | |
625 | ||
626 | // False if we can call operator<(T,U) | |
0b3ec8f4 | 627 | template<typename _Tp, typename _Up> |
7c69a7d9 JW |
628 | struct __not_overloaded<_Tp, _Up, __void_t< |
629 | decltype(operator<(std::declval<_Tp>(), std::declval<_Up>()))>> | |
630 | : false_type { }; | |
0b3ec8f4 JW |
631 | |
632 | template<typename _Tp, typename _Up> | |
633 | using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>, | |
634 | is_convertible<_Tp, const volatile void*>, | |
635 | is_convertible<_Up, const volatile void*>>; | |
d081231a JW |
636 | }; |
637 | ||
638 | /// One of the @link comparison_functors comparison functors@endlink. | |
639 | template<> | |
640 | struct greater_equal<void> | |
641 | { | |
642 | template <typename _Tp, typename _Up> | |
0b3ec8f4 | 643 | constexpr auto |
d081231a JW |
644 | operator()(_Tp&& __t, _Up&& __u) const |
645 | noexcept(noexcept(std::forward<_Tp>(__t) >= std::forward<_Up>(__u))) | |
646 | -> decltype(std::forward<_Tp>(__t) >= std::forward<_Up>(__u)) | |
0b3ec8f4 JW |
647 | { |
648 | return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u), | |
649 | __ptr_cmp<_Tp, _Up>{}); | |
650 | } | |
651 | ||
652 | template<typename _Tp, typename _Up> | |
653 | constexpr bool | |
654 | operator()(_Tp* __t, _Up* __u) const noexcept | |
655 | { return greater_equal<common_type_t<_Tp*, _Up*>>{}(__t, __u); } | |
d081231a JW |
656 | |
657 | typedef __is_transparent is_transparent; | |
0b3ec8f4 JW |
658 | |
659 | private: | |
660 | template <typename _Tp, typename _Up> | |
661 | static constexpr decltype(auto) | |
662 | _S_cmp(_Tp&& __t, _Up&& __u, false_type) | |
663 | { return std::forward<_Tp>(__t) >= std::forward<_Up>(__u); } | |
664 | ||
665 | template <typename _Tp, typename _Up> | |
666 | static constexpr bool | |
667 | _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept | |
668 | { | |
669 | return greater_equal<const volatile void*>{}( | |
670 | static_cast<const volatile void*>(std::forward<_Tp>(__t)), | |
671 | static_cast<const volatile void*>(std::forward<_Up>(__u))); | |
672 | } | |
673 | ||
7c69a7d9 | 674 | // True if there is no viable operator>= member function. |
0b3ec8f4 JW |
675 | template<typename _Tp, typename _Up, typename = void> |
676 | struct __not_overloaded2 : true_type { }; | |
677 | ||
678 | // False if we can call T.operator>=(U) | |
679 | template<typename _Tp, typename _Up> | |
680 | struct __not_overloaded2<_Tp, _Up, __void_t< | |
681 | decltype(std::declval<_Tp>().operator>=(std::declval<_Up>()))>> | |
682 | : false_type { }; | |
683 | ||
7c69a7d9 JW |
684 | // True if there is no overloaded operator>= for these operands. |
685 | template<typename _Tp, typename _Up, typename = void> | |
686 | struct __not_overloaded : __not_overloaded2<_Tp, _Up> { }; | |
687 | ||
688 | // False if we can call operator>=(T,U) | |
0b3ec8f4 | 689 | template<typename _Tp, typename _Up> |
7c69a7d9 JW |
690 | struct __not_overloaded<_Tp, _Up, __void_t< |
691 | decltype(operator>=(std::declval<_Tp>(), std::declval<_Up>()))>> | |
692 | : false_type { }; | |
0b3ec8f4 JW |
693 | |
694 | template<typename _Tp, typename _Up> | |
695 | using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>, | |
696 | is_convertible<_Tp, const volatile void*>, | |
697 | is_convertible<_Up, const volatile void*>>; | |
d081231a JW |
698 | }; |
699 | ||
700 | /// One of the @link comparison_functors comparison functors@endlink. | |
701 | template<> | |
702 | struct less_equal<void> | |
703 | { | |
704 | template <typename _Tp, typename _Up> | |
0b3ec8f4 | 705 | constexpr auto |
d081231a JW |
706 | operator()(_Tp&& __t, _Up&& __u) const |
707 | noexcept(noexcept(std::forward<_Tp>(__t) <= std::forward<_Up>(__u))) | |
708 | -> decltype(std::forward<_Tp>(__t) <= std::forward<_Up>(__u)) | |
0b3ec8f4 JW |
709 | { |
710 | return _S_cmp(std::forward<_Tp>(__t), std::forward<_Up>(__u), | |
711 | __ptr_cmp<_Tp, _Up>{}); | |
712 | } | |
713 | ||
714 | template<typename _Tp, typename _Up> | |
715 | constexpr bool | |
716 | operator()(_Tp* __t, _Up* __u) const noexcept | |
717 | { return less_equal<common_type_t<_Tp*, _Up*>>{}(__t, __u); } | |
d081231a JW |
718 | |
719 | typedef __is_transparent is_transparent; | |
0b3ec8f4 JW |
720 | |
721 | private: | |
722 | template <typename _Tp, typename _Up> | |
723 | static constexpr decltype(auto) | |
724 | _S_cmp(_Tp&& __t, _Up&& __u, false_type) | |
725 | { return std::forward<_Tp>(__t) <= std::forward<_Up>(__u); } | |
726 | ||
727 | template <typename _Tp, typename _Up> | |
728 | static constexpr bool | |
729 | _S_cmp(_Tp&& __t, _Up&& __u, true_type) noexcept | |
730 | { | |
731 | return less_equal<const volatile void*>{}( | |
732 | static_cast<const volatile void*>(std::forward<_Tp>(__t)), | |
733 | static_cast<const volatile void*>(std::forward<_Up>(__u))); | |
734 | } | |
735 | ||
7c69a7d9 | 736 | // True if there is no viable operator<= member function. |
0b3ec8f4 JW |
737 | template<typename _Tp, typename _Up, typename = void> |
738 | struct __not_overloaded2 : true_type { }; | |
739 | ||
740 | // False if we can call T.operator<=(U) | |
741 | template<typename _Tp, typename _Up> | |
742 | struct __not_overloaded2<_Tp, _Up, __void_t< | |
743 | decltype(std::declval<_Tp>().operator<=(std::declval<_Up>()))>> | |
744 | : false_type { }; | |
745 | ||
7c69a7d9 JW |
746 | // True if there is no overloaded operator<= for these operands. |
747 | template<typename _Tp, typename _Up, typename = void> | |
748 | struct __not_overloaded : __not_overloaded2<_Tp, _Up> { }; | |
749 | ||
750 | // False if we can call operator<=(T,U) | |
0b3ec8f4 | 751 | template<typename _Tp, typename _Up> |
7c69a7d9 JW |
752 | struct __not_overloaded<_Tp, _Up, __void_t< |
753 | decltype(operator<=(std::declval<_Tp>(), std::declval<_Up>()))>> | |
754 | : false_type { }; | |
0b3ec8f4 JW |
755 | |
756 | template<typename _Tp, typename _Up> | |
757 | using __ptr_cmp = __and_<__not_overloaded<_Tp, _Up>, | |
758 | is_convertible<_Tp, const volatile void*>, | |
759 | is_convertible<_Up, const volatile void*>>; | |
d081231a | 760 | }; |
0b3ec8f4 | 761 | #endif // C++14 |
f6592a9e | 762 | /** @} */ |
ed6814f7 | 763 | |
f6592a9e | 764 | // 20.3.4 logical operations |
aac2878e BK |
765 | /** @defgroup logical_functors Boolean Operations Classes |
766 | * @ingroup functors | |
767 | * | |
65be6ddd BK |
768 | * Here are wrapper functors for Boolean operations: @c &&, @c ||, |
769 | * and @c !. | |
f6592a9e PC |
770 | * |
771 | * @{ | |
772 | */ | |
d081231a JW |
773 | #if __cplusplus > 201103L |
774 | template<typename _Tp = void> | |
775 | struct logical_and; | |
776 | ||
777 | template<typename _Tp = void> | |
778 | struct logical_or; | |
779 | ||
780 | template<typename _Tp = void> | |
781 | struct logical_not; | |
782 | #endif | |
783 | ||
aac2878e | 784 | /// One of the @link logical_functors Boolean operations functors@endlink. |
bd1a56a0 | 785 | template<typename _Tp> |
92ff3e43 | 786 | struct logical_and : public binary_function<_Tp, _Tp, bool> |
f6592a9e | 787 | { |
8dff34fe | 788 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
789 | bool |
790 | operator()(const _Tp& __x, const _Tp& __y) const | |
791 | { return __x && __y; } | |
792 | }; | |
793 | ||
aac2878e | 794 | /// One of the @link logical_functors Boolean operations functors@endlink. |
bd1a56a0 | 795 | template<typename _Tp> |
92ff3e43 | 796 | struct logical_or : public binary_function<_Tp, _Tp, bool> |
f6592a9e | 797 | { |
8dff34fe | 798 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
799 | bool |
800 | operator()(const _Tp& __x, const _Tp& __y) const | |
801 | { return __x || __y; } | |
802 | }; | |
803 | ||
aac2878e | 804 | /// One of the @link logical_functors Boolean operations functors@endlink. |
bd1a56a0 | 805 | template<typename _Tp> |
92ff3e43 | 806 | struct logical_not : public unary_function<_Tp, bool> |
f6592a9e | 807 | { |
8dff34fe | 808 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
809 | bool |
810 | operator()(const _Tp& __x) const | |
811 | { return !__x; } | |
812 | }; | |
d081231a JW |
813 | |
814 | #if __cplusplus > 201103L | |
815 | /// One of the @link logical_functors Boolean operations functors@endlink. | |
816 | template<> | |
817 | struct logical_and<void> | |
818 | { | |
819 | template <typename _Tp, typename _Up> | |
8dff34fe | 820 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
821 | auto |
822 | operator()(_Tp&& __t, _Up&& __u) const | |
823 | noexcept(noexcept(std::forward<_Tp>(__t) && std::forward<_Up>(__u))) | |
824 | -> decltype(std::forward<_Tp>(__t) && std::forward<_Up>(__u)) | |
825 | { return std::forward<_Tp>(__t) && std::forward<_Up>(__u); } | |
826 | ||
827 | typedef __is_transparent is_transparent; | |
828 | }; | |
829 | ||
830 | /// One of the @link logical_functors Boolean operations functors@endlink. | |
831 | template<> | |
832 | struct logical_or<void> | |
833 | { | |
834 | template <typename _Tp, typename _Up> | |
8dff34fe | 835 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
836 | auto |
837 | operator()(_Tp&& __t, _Up&& __u) const | |
838 | noexcept(noexcept(std::forward<_Tp>(__t) || std::forward<_Up>(__u))) | |
839 | -> decltype(std::forward<_Tp>(__t) || std::forward<_Up>(__u)) | |
840 | { return std::forward<_Tp>(__t) || std::forward<_Up>(__u); } | |
841 | ||
842 | typedef __is_transparent is_transparent; | |
843 | }; | |
844 | ||
845 | /// One of the @link logical_functors Boolean operations functors@endlink. | |
846 | template<> | |
847 | struct logical_not<void> | |
848 | { | |
849 | template <typename _Tp> | |
8dff34fe | 850 | _GLIBCXX14_CONSTEXPR |
d081231a | 851 | auto |
bef49287 JW |
852 | operator()(_Tp&& __t) const |
853 | noexcept(noexcept(!std::forward<_Tp>(__t))) | |
854 | -> decltype(!std::forward<_Tp>(__t)) | |
d081231a JW |
855 | { return !std::forward<_Tp>(__t); } |
856 | ||
857 | typedef __is_transparent is_transparent; | |
858 | }; | |
859 | #endif | |
f6592a9e PC |
860 | /** @} */ |
861 | ||
d081231a JW |
862 | #if __cplusplus > 201103L |
863 | template<typename _Tp = void> | |
864 | struct bit_and; | |
865 | ||
866 | template<typename _Tp = void> | |
867 | struct bit_or; | |
868 | ||
869 | template<typename _Tp = void> | |
870 | struct bit_xor; | |
871 | ||
872 | template<typename _Tp = void> | |
873 | struct bit_not; | |
874 | #endif | |
875 | ||
2ee0c1fb PC |
876 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
877 | // DR 660. Missing Bitwise Operations. | |
bd1a56a0 | 878 | template<typename _Tp> |
2ee0c1fb PC |
879 | struct bit_and : public binary_function<_Tp, _Tp, _Tp> |
880 | { | |
8dff34fe | 881 | _GLIBCXX14_CONSTEXPR |
2ee0c1fb PC |
882 | _Tp |
883 | operator()(const _Tp& __x, const _Tp& __y) const | |
884 | { return __x & __y; } | |
885 | }; | |
886 | ||
bd1a56a0 | 887 | template<typename _Tp> |
2ee0c1fb PC |
888 | struct bit_or : public binary_function<_Tp, _Tp, _Tp> |
889 | { | |
8dff34fe | 890 | _GLIBCXX14_CONSTEXPR |
2ee0c1fb PC |
891 | _Tp |
892 | operator()(const _Tp& __x, const _Tp& __y) const | |
893 | { return __x | __y; } | |
894 | }; | |
895 | ||
bd1a56a0 | 896 | template<typename _Tp> |
2ee0c1fb PC |
897 | struct bit_xor : public binary_function<_Tp, _Tp, _Tp> |
898 | { | |
8dff34fe | 899 | _GLIBCXX14_CONSTEXPR |
2ee0c1fb PC |
900 | _Tp |
901 | operator()(const _Tp& __x, const _Tp& __y) const | |
902 | { return __x ^ __y; } | |
903 | }; | |
904 | ||
d081231a JW |
905 | template<typename _Tp> |
906 | struct bit_not : public unary_function<_Tp, _Tp> | |
907 | { | |
8dff34fe | 908 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
909 | _Tp |
910 | operator()(const _Tp& __x) const | |
911 | { return ~__x; } | |
912 | }; | |
913 | ||
914 | #if __cplusplus > 201103L | |
915 | template <> | |
916 | struct bit_and<void> | |
917 | { | |
918 | template <typename _Tp, typename _Up> | |
8dff34fe | 919 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
920 | auto |
921 | operator()(_Tp&& __t, _Up&& __u) const | |
922 | noexcept(noexcept(std::forward<_Tp>(__t) & std::forward<_Up>(__u))) | |
923 | -> decltype(std::forward<_Tp>(__t) & std::forward<_Up>(__u)) | |
924 | { return std::forward<_Tp>(__t) & std::forward<_Up>(__u); } | |
925 | ||
926 | typedef __is_transparent is_transparent; | |
927 | }; | |
928 | ||
929 | template <> | |
930 | struct bit_or<void> | |
931 | { | |
932 | template <typename _Tp, typename _Up> | |
8dff34fe | 933 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
934 | auto |
935 | operator()(_Tp&& __t, _Up&& __u) const | |
936 | noexcept(noexcept(std::forward<_Tp>(__t) | std::forward<_Up>(__u))) | |
937 | -> decltype(std::forward<_Tp>(__t) | std::forward<_Up>(__u)) | |
938 | { return std::forward<_Tp>(__t) | std::forward<_Up>(__u); } | |
939 | ||
940 | typedef __is_transparent is_transparent; | |
941 | }; | |
942 | ||
943 | template <> | |
944 | struct bit_xor<void> | |
945 | { | |
946 | template <typename _Tp, typename _Up> | |
8dff34fe | 947 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
948 | auto |
949 | operator()(_Tp&& __t, _Up&& __u) const | |
950 | noexcept(noexcept(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u))) | |
951 | -> decltype(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u)) | |
952 | { return std::forward<_Tp>(__t) ^ std::forward<_Up>(__u); } | |
953 | ||
954 | typedef __is_transparent is_transparent; | |
955 | }; | |
956 | ||
957 | template <> | |
958 | struct bit_not<void> | |
959 | { | |
960 | template <typename _Tp> | |
8dff34fe | 961 | _GLIBCXX14_CONSTEXPR |
d081231a JW |
962 | auto |
963 | operator()(_Tp&& __t) const | |
964 | noexcept(noexcept(~std::forward<_Tp>(__t))) | |
965 | -> decltype(~std::forward<_Tp>(__t)) | |
966 | { return ~std::forward<_Tp>(__t); } | |
967 | ||
968 | typedef __is_transparent is_transparent; | |
969 | }; | |
970 | #endif | |
971 | ||
f6592a9e | 972 | // 20.3.5 negators |
aac2878e BK |
973 | /** @defgroup negators Negators |
974 | * @ingroup functors | |
975 | * | |
f6592a9e PC |
976 | * The functions @c not1 and @c not2 each take a predicate functor |
977 | * and return an instance of @c unary_negate or | |
978 | * @c binary_negate, respectively. These classes are functors whose | |
979 | * @c operator() performs the stored predicate function and then returns | |
980 | * the negation of the result. | |
981 | * | |
982 | * For example, given a vector of integers and a trivial predicate, | |
983 | * \code | |
984 | * struct IntGreaterThanThree | |
985 | * : public std::unary_function<int, bool> | |
986 | * { | |
987 | * bool operator() (int x) { return x > 3; } | |
988 | * }; | |
ed6814f7 | 989 | * |
f6592a9e PC |
990 | * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree())); |
991 | * \endcode | |
992 | * The call to @c find_if will locate the first index (i) of @c v for which | |
2a60a9f6 | 993 | * <code>!(v[i] > 3)</code> is true. |
f6592a9e PC |
994 | * |
995 | * The not1/unary_negate combination works on predicates taking a single | |
996 | * argument. The not2/binary_negate combination works on predicates which | |
997 | * take two arguments. | |
998 | * | |
999 | * @{ | |
1000 | */ | |
aac2878e | 1001 | /// One of the @link negators negation functors@endlink. |
bd1a56a0 | 1002 | template<typename _Predicate> |
f6592a9e PC |
1003 | class unary_negate |
1004 | : public unary_function<typename _Predicate::argument_type, bool> | |
1005 | { | |
1006 | protected: | |
1007 | _Predicate _M_pred; | |
bd1a56a0 | 1008 | |
f6592a9e | 1009 | public: |
8dff34fe | 1010 | _GLIBCXX14_CONSTEXPR |
7c920151 | 1011 | explicit |
bd1a56a0 | 1012 | unary_negate(const _Predicate& __x) : _M_pred(__x) { } |
ed6814f7 | 1013 | |
8dff34fe | 1014 | _GLIBCXX14_CONSTEXPR |
f6592a9e PC |
1015 | bool |
1016 | operator()(const typename _Predicate::argument_type& __x) const | |
1017 | { return !_M_pred(__x); } | |
1018 | }; | |
1019 | ||
aac2878e | 1020 | /// One of the @link negators negation functors@endlink. |
bd1a56a0 | 1021 | template<typename _Predicate> |
8dff34fe | 1022 | _GLIBCXX14_CONSTEXPR |
ed6814f7 | 1023 | inline unary_negate<_Predicate> |
f6592a9e PC |
1024 | not1(const _Predicate& __pred) |
1025 | { return unary_negate<_Predicate>(__pred); } | |
1026 | ||
aac2878e | 1027 | /// One of the @link negators negation functors@endlink. |
bd1a56a0 | 1028 | template<typename _Predicate> |
ed6814f7 | 1029 | class binary_negate |
f6592a9e | 1030 | : public binary_function<typename _Predicate::first_argument_type, |
65be6ddd | 1031 | typename _Predicate::second_argument_type, bool> |
f6592a9e PC |
1032 | { |
1033 | protected: | |
1034 | _Predicate _M_pred; | |
65be6ddd | 1035 | |
f6592a9e | 1036 | public: |
8dff34fe | 1037 | _GLIBCXX14_CONSTEXPR |
7c920151 | 1038 | explicit |
65be6ddd | 1039 | binary_negate(const _Predicate& __x) : _M_pred(__x) { } |
f6592a9e | 1040 | |
8dff34fe | 1041 | _GLIBCXX14_CONSTEXPR |
f6592a9e | 1042 | bool |
ed6814f7 | 1043 | operator()(const typename _Predicate::first_argument_type& __x, |
f6592a9e PC |
1044 | const typename _Predicate::second_argument_type& __y) const |
1045 | { return !_M_pred(__x, __y); } | |
1046 | }; | |
1047 | ||
aac2878e | 1048 | /// One of the @link negators negation functors@endlink. |
bd1a56a0 | 1049 | template<typename _Predicate> |
8dff34fe | 1050 | _GLIBCXX14_CONSTEXPR |
ed6814f7 | 1051 | inline binary_negate<_Predicate> |
f6592a9e PC |
1052 | not2(const _Predicate& __pred) |
1053 | { return binary_negate<_Predicate>(__pred); } | |
1054 | /** @} */ | |
ed6814f7 | 1055 | |
f6592a9e | 1056 | // 20.3.7 adaptors pointers functions |
aac2878e BK |
1057 | /** @defgroup pointer_adaptors Adaptors for pointers to functions |
1058 | * @ingroup functors | |
1059 | * | |
f6592a9e PC |
1060 | * The advantage of function objects over pointers to functions is that |
1061 | * the objects in the standard library declare nested typedefs describing | |
1062 | * their argument and result types with uniform names (e.g., @c result_type | |
1063 | * from the base classes @c unary_function and @c binary_function). | |
1064 | * Sometimes those typedefs are required, not just optional. | |
1065 | * | |
1066 | * Adaptors are provided to turn pointers to unary (single-argument) and | |
1067 | * binary (double-argument) functions into function objects. The | |
1068 | * long-winded functor @c pointer_to_unary_function is constructed with a | |
1069 | * function pointer @c f, and its @c operator() called with argument @c x | |
1070 | * returns @c f(x). The functor @c pointer_to_binary_function does the same | |
1071 | * thing, but with a double-argument @c f and @c operator(). | |
1072 | * | |
1073 | * The function @c ptr_fun takes a pointer-to-function @c f and constructs | |
1074 | * an instance of the appropriate functor. | |
1075 | * | |
1076 | * @{ | |
1077 | */ | |
aac2878e | 1078 | /// One of the @link pointer_adaptors adaptors for function pointers@endlink. |
bd1a56a0 | 1079 | template<typename _Arg, typename _Result> |
f6592a9e PC |
1080 | class pointer_to_unary_function : public unary_function<_Arg, _Result> |
1081 | { | |
1082 | protected: | |
1083 | _Result (*_M_ptr)(_Arg); | |
bd1a56a0 | 1084 | |
f6592a9e | 1085 | public: |
bd1a56a0 | 1086 | pointer_to_unary_function() { } |
ed6814f7 | 1087 | |
7c920151 PC |
1088 | explicit |
1089 | pointer_to_unary_function(_Result (*__x)(_Arg)) | |
bd1a56a0 | 1090 | : _M_ptr(__x) { } |
725dc051 | 1091 | |
f6592a9e PC |
1092 | _Result |
1093 | operator()(_Arg __x) const | |
1094 | { return _M_ptr(__x); } | |
1095 | }; | |
1096 | ||
aac2878e | 1097 | /// One of the @link pointer_adaptors adaptors for function pointers@endlink. |
bd1a56a0 | 1098 | template<typename _Arg, typename _Result> |
f6592a9e PC |
1099 | inline pointer_to_unary_function<_Arg, _Result> |
1100 | ptr_fun(_Result (*__x)(_Arg)) | |
1101 | { return pointer_to_unary_function<_Arg, _Result>(__x); } | |
1102 | ||
aac2878e | 1103 | /// One of the @link pointer_adaptors adaptors for function pointers@endlink. |
bd1a56a0 | 1104 | template<typename _Arg1, typename _Arg2, typename _Result> |
f6592a9e | 1105 | class pointer_to_binary_function |
afd4cbbb | 1106 | : public binary_function<_Arg1, _Arg2, _Result> |
f6592a9e PC |
1107 | { |
1108 | protected: | |
1109 | _Result (*_M_ptr)(_Arg1, _Arg2); | |
bd1a56a0 | 1110 | |
f6592a9e | 1111 | public: |
bd1a56a0 | 1112 | pointer_to_binary_function() { } |
f6592a9e | 1113 | |
7c920151 | 1114 | explicit |
ed6814f7 | 1115 | pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2)) |
bd1a56a0 | 1116 | : _M_ptr(__x) { } |
725dc051 | 1117 | |
f6592a9e PC |
1118 | _Result |
1119 | operator()(_Arg1 __x, _Arg2 __y) const | |
1120 | { return _M_ptr(__x, __y); } | |
1121 | }; | |
1122 | ||
aac2878e | 1123 | /// One of the @link pointer_adaptors adaptors for function pointers@endlink. |
bd1a56a0 | 1124 | template<typename _Arg1, typename _Arg2, typename _Result> |
ed6814f7 | 1125 | inline pointer_to_binary_function<_Arg1, _Arg2, _Result> |
f6592a9e PC |
1126 | ptr_fun(_Result (*__x)(_Arg1, _Arg2)) |
1127 | { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); } | |
1128 | /** @} */ | |
ed6814f7 | 1129 | |
bd1a56a0 | 1130 | template<typename _Tp> |
b9b09214 | 1131 | struct _Identity |
866e4d38 | 1132 | : public unary_function<_Tp, _Tp> |
f6592a9e PC |
1133 | { |
1134 | _Tp& | |
1135 | operator()(_Tp& __x) const | |
1136 | { return __x; } | |
ed6814f7 | 1137 | |
f6592a9e PC |
1138 | const _Tp& |
1139 | operator()(const _Tp& __x) const | |
1140 | { return __x; } | |
1141 | }; | |
1142 | ||
866e4d38 JW |
1143 | // Partial specialization, avoids confusing errors in e.g. std::set<const T>. |
1144 | template<typename _Tp> struct _Identity<const _Tp> : _Identity<_Tp> { }; | |
1145 | ||
bd1a56a0 | 1146 | template<typename _Pair> |
b9b09214 | 1147 | struct _Select1st |
b9b09214 | 1148 | : public unary_function<_Pair, typename _Pair::first_type> |
f6592a9e PC |
1149 | { |
1150 | typename _Pair::first_type& | |
1151 | operator()(_Pair& __x) const | |
1152 | { return __x.first; } | |
ed6814f7 | 1153 | |
f6592a9e PC |
1154 | const typename _Pair::first_type& |
1155 | operator()(const _Pair& __x) const | |
1156 | { return __x.first; } | |
777a1e28 | 1157 | |
734f5023 | 1158 | #if __cplusplus >= 201103L |
777a1e28 PC |
1159 | template<typename _Pair2> |
1160 | typename _Pair2::first_type& | |
1161 | operator()(_Pair2& __x) const | |
1162 | { return __x.first; } | |
1163 | ||
1164 | template<typename _Pair2> | |
1165 | const typename _Pair2::first_type& | |
1166 | operator()(const _Pair2& __x) const | |
1167 | { return __x.first; } | |
1168 | #endif | |
f6592a9e PC |
1169 | }; |
1170 | ||
bd1a56a0 | 1171 | template<typename _Pair> |
b9b09214 | 1172 | struct _Select2nd |
b9b09214 | 1173 | : public unary_function<_Pair, typename _Pair::second_type> |
f6592a9e PC |
1174 | { |
1175 | typename _Pair::second_type& | |
1176 | operator()(_Pair& __x) const | |
1177 | { return __x.second; } | |
ed6814f7 | 1178 | |
f6592a9e PC |
1179 | const typename _Pair::second_type& |
1180 | operator()(const _Pair& __x) const | |
1181 | { return __x.second; } | |
1182 | }; | |
1183 | ||
1184 | // 20.3.8 adaptors pointers members | |
aac2878e BK |
1185 | /** @defgroup memory_adaptors Adaptors for pointers to members |
1186 | * @ingroup functors | |
1187 | * | |
d5f261c1 | 1188 | * There are a total of 8 = 2^3 function objects in this family. |
f6592a9e PC |
1189 | * (1) Member functions taking no arguments vs member functions taking |
1190 | * one argument. | |
1191 | * (2) Call through pointer vs call through reference. | |
d5f261c1 | 1192 | * (3) Const vs non-const member function. |
f6592a9e PC |
1193 | * |
1194 | * All of this complexity is in the function objects themselves. You can | |
1195 | * ignore it by using the helper function mem_fun and mem_fun_ref, | |
1196 | * which create whichever type of adaptor is appropriate. | |
1197 | * | |
1198 | * @{ | |
1199 | */ | |
aac2878e | 1200 | /// One of the @link memory_adaptors adaptors for member |
65be6ddd | 1201 | /// pointers@endlink. |
bd1a56a0 | 1202 | template<typename _Ret, typename _Tp> |
f6592a9e PC |
1203 | class mem_fun_t : public unary_function<_Tp*, _Ret> |
1204 | { | |
1205 | public: | |
7c920151 PC |
1206 | explicit |
1207 | mem_fun_t(_Ret (_Tp::*__pf)()) | |
bd1a56a0 | 1208 | : _M_f(__pf) { } |
f6592a9e PC |
1209 | |
1210 | _Ret | |
1211 | operator()(_Tp* __p) const | |
1212 | { return (__p->*_M_f)(); } | |
bd1a56a0 | 1213 | |
f6592a9e PC |
1214 | private: |
1215 | _Ret (_Tp::*_M_f)(); | |
1216 | }; | |
1217 | ||
aac2878e | 1218 | /// One of the @link memory_adaptors adaptors for member |
65be6ddd | 1219 | /// pointers@endlink. |
bd1a56a0 | 1220 | template<typename _Ret, typename _Tp> |
f6592a9e PC |
1221 | class const_mem_fun_t : public unary_function<const _Tp*, _Ret> |
1222 | { | |
1223 | public: | |
7c920151 PC |
1224 | explicit |
1225 | const_mem_fun_t(_Ret (_Tp::*__pf)() const) | |
bd1a56a0 | 1226 | : _M_f(__pf) { } |
f6592a9e PC |
1227 | |
1228 | _Ret | |
1229 | operator()(const _Tp* __p) const | |
1230 | { return (__p->*_M_f)(); } | |
bd1a56a0 | 1231 | |
f6592a9e PC |
1232 | private: |
1233 | _Ret (_Tp::*_M_f)() const; | |
1234 | }; | |
1235 | ||
aac2878e | 1236 | /// One of the @link memory_adaptors adaptors for member |
65be6ddd | 1237 | /// pointers@endlink. |
bd1a56a0 | 1238 | template<typename _Ret, typename _Tp> |
f6592a9e PC |
1239 | class mem_fun_ref_t : public unary_function<_Tp, _Ret> |
1240 | { | |
1241 | public: | |
7c920151 PC |
1242 | explicit |
1243 | mem_fun_ref_t(_Ret (_Tp::*__pf)()) | |
bd1a56a0 | 1244 | : _M_f(__pf) { } |
f6592a9e PC |
1245 | |
1246 | _Ret | |
1247 | operator()(_Tp& __r) const | |
1248 | { return (__r.*_M_f)(); } | |
bd1a56a0 | 1249 | |
f6592a9e PC |
1250 | private: |
1251 | _Ret (_Tp::*_M_f)(); | |
1252 | }; | |
ed6814f7 | 1253 | |
aac2878e | 1254 | /// One of the @link memory_adaptors adaptors for member |
65be6ddd | 1255 | /// pointers@endlink. |
bd1a56a0 | 1256 | template<typename _Ret, typename _Tp> |
f6592a9e PC |
1257 | class const_mem_fun_ref_t : public unary_function<_Tp, _Ret> |
1258 | { | |
1259 | public: | |
7c920151 PC |
1260 | explicit |
1261 | const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) | |
bd1a56a0 | 1262 | : _M_f(__pf) { } |
f6592a9e PC |
1263 | |
1264 | _Ret | |
1265 | operator()(const _Tp& __r) const | |
1266 | { return (__r.*_M_f)(); } | |
bd1a56a0 | 1267 | |
f6592a9e PC |
1268 | private: |
1269 | _Ret (_Tp::*_M_f)() const; | |
1270 | }; | |
1271 | ||
aac2878e | 1272 | /// One of the @link memory_adaptors adaptors for member |
65be6ddd | 1273 | /// pointers@endlink. |
bd1a56a0 | 1274 | template<typename _Ret, typename _Tp, typename _Arg> |
f6592a9e PC |
1275 | class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret> |
1276 | { | |
1277 | public: | |
7c920151 | 1278 | explicit |
ed6814f7 | 1279 | mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) |
bd1a56a0 | 1280 | : _M_f(__pf) { } |
f6592a9e PC |
1281 | |
1282 | _Ret | |
1283 | operator()(_Tp* __p, _Arg __x) const | |
1284 | { return (__p->*_M_f)(__x); } | |
bd1a56a0 | 1285 | |
f6592a9e PC |
1286 | private: |
1287 | _Ret (_Tp::*_M_f)(_Arg); | |
1288 | }; | |
1289 | ||
aac2878e | 1290 | /// One of the @link memory_adaptors adaptors for member |
65be6ddd | 1291 | /// pointers@endlink. |
bd1a56a0 | 1292 | template<typename _Ret, typename _Tp, typename _Arg> |
f6592a9e PC |
1293 | class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret> |
1294 | { | |
1295 | public: | |
7c920151 PC |
1296 | explicit |
1297 | const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) | |
bd1a56a0 | 1298 | : _M_f(__pf) { } |
f6592a9e PC |
1299 | |
1300 | _Ret | |
1301 | operator()(const _Tp* __p, _Arg __x) const | |
1302 | { return (__p->*_M_f)(__x); } | |
bd1a56a0 | 1303 | |
f6592a9e PC |
1304 | private: |
1305 | _Ret (_Tp::*_M_f)(_Arg) const; | |
1306 | }; | |
1307 | ||
aac2878e | 1308 | /// One of the @link memory_adaptors adaptors for member |
65be6ddd | 1309 | /// pointers@endlink. |
bd1a56a0 | 1310 | template<typename _Ret, typename _Tp, typename _Arg> |
f6592a9e PC |
1311 | class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret> |
1312 | { | |
1313 | public: | |
7c920151 PC |
1314 | explicit |
1315 | mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) | |
bd1a56a0 | 1316 | : _M_f(__pf) { } |
f6592a9e PC |
1317 | |
1318 | _Ret | |
1319 | operator()(_Tp& __r, _Arg __x) const | |
1320 | { return (__r.*_M_f)(__x); } | |
bd1a56a0 | 1321 | |
f6592a9e PC |
1322 | private: |
1323 | _Ret (_Tp::*_M_f)(_Arg); | |
1324 | }; | |
1325 | ||
aac2878e | 1326 | /// One of the @link memory_adaptors adaptors for member |
65be6ddd | 1327 | /// pointers@endlink. |
bd1a56a0 | 1328 | template<typename _Ret, typename _Tp, typename _Arg> |
f6592a9e PC |
1329 | class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret> |
1330 | { | |
1331 | public: | |
7c920151 PC |
1332 | explicit |
1333 | const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) | |
bd1a56a0 | 1334 | : _M_f(__pf) { } |
f6592a9e PC |
1335 | |
1336 | _Ret | |
1337 | operator()(const _Tp& __r, _Arg __x) const | |
1338 | { return (__r.*_M_f)(__x); } | |
bd1a56a0 | 1339 | |
f6592a9e PC |
1340 | private: |
1341 | _Ret (_Tp::*_M_f)(_Arg) const; | |
1342 | }; | |
ed6814f7 | 1343 | |
f6592a9e PC |
1344 | // Mem_fun adaptor helper functions. There are only two: |
1345 | // mem_fun and mem_fun_ref. | |
bd1a56a0 | 1346 | template<typename _Ret, typename _Tp> |
92ff3e43 | 1347 | inline mem_fun_t<_Ret, _Tp> |
f6592a9e | 1348 | mem_fun(_Ret (_Tp::*__f)()) |
92ff3e43 | 1349 | { return mem_fun_t<_Ret, _Tp>(__f); } |
f6592a9e | 1350 | |
bd1a56a0 | 1351 | template<typename _Ret, typename _Tp> |
92ff3e43 | 1352 | inline const_mem_fun_t<_Ret, _Tp> |
f6592a9e | 1353 | mem_fun(_Ret (_Tp::*__f)() const) |
92ff3e43 | 1354 | { return const_mem_fun_t<_Ret, _Tp>(__f); } |
ed6814f7 | 1355 | |
bd1a56a0 | 1356 | template<typename _Ret, typename _Tp> |
92ff3e43 | 1357 | inline mem_fun_ref_t<_Ret, _Tp> |
ed6814f7 | 1358 | mem_fun_ref(_Ret (_Tp::*__f)()) |
92ff3e43 | 1359 | { return mem_fun_ref_t<_Ret, _Tp>(__f); } |
f6592a9e | 1360 | |
bd1a56a0 | 1361 | template<typename _Ret, typename _Tp> |
92ff3e43 | 1362 | inline const_mem_fun_ref_t<_Ret, _Tp> |
f6592a9e | 1363 | mem_fun_ref(_Ret (_Tp::*__f)() const) |
92ff3e43 | 1364 | { return const_mem_fun_ref_t<_Ret, _Tp>(__f); } |
f6592a9e | 1365 | |
bd1a56a0 | 1366 | template<typename _Ret, typename _Tp, typename _Arg> |
92ff3e43 | 1367 | inline mem_fun1_t<_Ret, _Tp, _Arg> |
f6592a9e | 1368 | mem_fun(_Ret (_Tp::*__f)(_Arg)) |
92ff3e43 | 1369 | { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); } |
f6592a9e | 1370 | |
bd1a56a0 | 1371 | template<typename _Ret, typename _Tp, typename _Arg> |
92ff3e43 | 1372 | inline const_mem_fun1_t<_Ret, _Tp, _Arg> |
f6592a9e | 1373 | mem_fun(_Ret (_Tp::*__f)(_Arg) const) |
92ff3e43 | 1374 | { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); } |
f6592a9e | 1375 | |
bd1a56a0 | 1376 | template<typename _Ret, typename _Tp, typename _Arg> |
92ff3e43 | 1377 | inline mem_fun1_ref_t<_Ret, _Tp, _Arg> |
f6592a9e | 1378 | mem_fun_ref(_Ret (_Tp::*__f)(_Arg)) |
92ff3e43 | 1379 | { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } |
f6592a9e | 1380 | |
bd1a56a0 | 1381 | template<typename _Ret, typename _Tp, typename _Arg> |
92ff3e43 | 1382 | inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg> |
f6592a9e | 1383 | mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const) |
92ff3e43 | 1384 | { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); } |
f6592a9e PC |
1385 | |
1386 | /** @} */ | |
ed6814f7 | 1387 | |
12ffa228 BK |
1388 | _GLIBCXX_END_NAMESPACE_VERSION |
1389 | } // namespace | |
725dc051 | 1390 | |
734f5023 | 1391 | #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED |
40abbf1f BK |
1392 | # include <backward/binders.h> |
1393 | #endif | |
1394 | ||
046d30f4 | 1395 | #endif /* _STL_FUNCTION_H */ |