]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/functional
dwarf2out.c (ranges_table): Change into vec<dw_ranges, va_gc> *.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / functional
1 // <functional> -*- C++ -*-
2
3 // Copyright (C) 2001-2016 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /*
26 * Copyright (c) 1997
27 * Silicon Graphics Computer Systems, Inc.
28 *
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
36 *
37 */
38
39 /** @file include/functional
40 * This is a Standard C++ Library header.
41 */
42
43 #ifndef _GLIBCXX_FUNCTIONAL
44 #define _GLIBCXX_FUNCTIONAL 1
45
46 #pragma GCC system_header
47
48 #include <bits/c++config.h>
49 #include <bits/stl_function.h>
50
51 #if __cplusplus >= 201103L
52
53 #include <typeinfo>
54 #include <new>
55 #include <tuple>
56 #include <type_traits>
57 #include <bits/functexcept.h>
58 #include <bits/functional_hash.h>
59 #include <bits/invoke.h>
60
61 #if __cplusplus > 201402L
62 #include <unordered_map>
63 #include <vector>
64 #include <array>
65 #include <utility>
66 #include <bits/stl_algo.h>
67 #endif
68
69 namespace std _GLIBCXX_VISIBILITY(default)
70 {
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72
73 template<typename _MemberPointer>
74 class _Mem_fn;
75 template<typename _Tp, typename _Class>
76 _Mem_fn<_Tp _Class::*>
77 mem_fn(_Tp _Class::*) noexcept;
78
79 /// If we have found a result_type, extract it.
80 template<typename _Functor, typename = __void_t<>>
81 struct _Maybe_get_result_type
82 { };
83
84 template<typename _Functor>
85 struct _Maybe_get_result_type<_Functor,
86 __void_t<typename _Functor::result_type>>
87 { typedef typename _Functor::result_type result_type; };
88
89 /**
90 * Base class for any function object that has a weak result type, as
91 * defined in 20.8.2 [func.require] of C++11.
92 */
93 template<typename _Functor>
94 struct _Weak_result_type_impl
95 : _Maybe_get_result_type<_Functor>
96 { };
97
98 /// Retrieve the result type for a function type.
99 template<typename _Res, typename... _ArgTypes>
100 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
101 { typedef _Res result_type; };
102
103 template<typename _Res, typename... _ArgTypes>
104 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
105 { typedef _Res result_type; };
106
107 template<typename _Res, typename... _ArgTypes>
108 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
109 { typedef _Res result_type; };
110
111 template<typename _Res, typename... _ArgTypes>
112 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
113 { typedef _Res result_type; };
114
115 template<typename _Res, typename... _ArgTypes>
116 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
117 { typedef _Res result_type; };
118
119 template<typename _Res, typename... _ArgTypes>
120 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
121 { typedef _Res result_type; };
122
123 template<typename _Res, typename... _ArgTypes>
124 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
125 { typedef _Res result_type; };
126
127 template<typename _Res, typename... _ArgTypes>
128 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
129 { typedef _Res result_type; };
130
131 /// Retrieve the result type for a function reference.
132 template<typename _Res, typename... _ArgTypes>
133 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
134 { typedef _Res result_type; };
135
136 template<typename _Res, typename... _ArgTypes>
137 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
138 { typedef _Res result_type; };
139
140 /// Retrieve the result type for a function pointer.
141 template<typename _Res, typename... _ArgTypes>
142 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
143 { typedef _Res result_type; };
144
145 template<typename _Res, typename... _ArgTypes>
146 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
147 { typedef _Res result_type; };
148
149 /// Retrieve result type for a member function pointer.
150 template<typename _Res, typename _Class, typename... _ArgTypes>
151 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
152 { typedef _Res result_type; };
153
154 template<typename _Res, typename _Class, typename... _ArgTypes>
155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
156 { typedef _Res result_type; };
157
158 /// Retrieve result type for a const member function pointer.
159 template<typename _Res, typename _Class, typename... _ArgTypes>
160 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
161 { typedef _Res result_type; };
162
163 template<typename _Res, typename _Class, typename... _ArgTypes>
164 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
165 { typedef _Res result_type; };
166
167 /// Retrieve result type for a volatile member function pointer.
168 template<typename _Res, typename _Class, typename... _ArgTypes>
169 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
170 { typedef _Res result_type; };
171
172 template<typename _Res, typename _Class, typename... _ArgTypes>
173 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
174 { typedef _Res result_type; };
175
176 /// Retrieve result type for a const volatile member function pointer.
177 template<typename _Res, typename _Class, typename... _ArgTypes>
178 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
179 const volatile>
180 { typedef _Res result_type; };
181
182 template<typename _Res, typename _Class, typename... _ArgTypes>
183 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
184 const volatile>
185 { typedef _Res result_type; };
186
187 /**
188 * Strip top-level cv-qualifiers from the function object and let
189 * _Weak_result_type_impl perform the real work.
190 */
191 template<typename _Functor>
192 struct _Weak_result_type
193 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
194 { };
195
196 #if __cplusplus > 201402L
197 # define __cpp_lib_invoke 201411
198
199 /// Invoke a callable object.
200 template<typename _Callable, typename... _Args>
201 inline result_of_t<_Callable&&(_Args&&...)>
202 invoke(_Callable&& __fn, _Args&&... __args)
203 noexcept(is_nothrow_callable_v<_Callable&&(_Args&&...)>)
204 {
205 return std::__invoke(std::forward<_Callable>(__fn),
206 std::forward<_Args>(__args)...);
207 }
208 #endif
209
210 // Detect nested argument_type.
211 template<typename _Tp, typename = __void_t<>>
212 struct _Refwrap_base_arg1
213 { };
214
215 // Nested argument_type.
216 template<typename _Tp>
217 struct _Refwrap_base_arg1<_Tp,
218 __void_t<typename _Tp::argument_type>>
219 {
220 typedef typename _Tp::argument_type argument_type;
221 };
222
223 // Detect nested first_argument_type and second_argument_type.
224 template<typename _Tp, typename = __void_t<>>
225 struct _Refwrap_base_arg2
226 { };
227
228 // Nested first_argument_type and second_argument_type.
229 template<typename _Tp>
230 struct _Refwrap_base_arg2<_Tp,
231 __void_t<typename _Tp::first_argument_type,
232 typename _Tp::second_argument_type>>
233 {
234 typedef typename _Tp::first_argument_type first_argument_type;
235 typedef typename _Tp::second_argument_type second_argument_type;
236 };
237
238 /**
239 * Derives from unary_function or binary_function when it
240 * can. Specializations handle all of the easy cases. The primary
241 * template determines what to do with a class type, which may
242 * derive from both unary_function and binary_function.
243 */
244 template<typename _Tp>
245 struct _Reference_wrapper_base
246 : _Weak_result_type<_Tp>, _Refwrap_base_arg1<_Tp>, _Refwrap_base_arg2<_Tp>
247 { };
248
249 // - a function type (unary)
250 template<typename _Res, typename _T1>
251 struct _Reference_wrapper_base<_Res(_T1)>
252 : unary_function<_T1, _Res>
253 { };
254
255 template<typename _Res, typename _T1>
256 struct _Reference_wrapper_base<_Res(_T1) const>
257 : unary_function<_T1, _Res>
258 { };
259
260 template<typename _Res, typename _T1>
261 struct _Reference_wrapper_base<_Res(_T1) volatile>
262 : unary_function<_T1, _Res>
263 { };
264
265 template<typename _Res, typename _T1>
266 struct _Reference_wrapper_base<_Res(_T1) const volatile>
267 : unary_function<_T1, _Res>
268 { };
269
270 // - a function type (binary)
271 template<typename _Res, typename _T1, typename _T2>
272 struct _Reference_wrapper_base<_Res(_T1, _T2)>
273 : binary_function<_T1, _T2, _Res>
274 { };
275
276 template<typename _Res, typename _T1, typename _T2>
277 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
278 : binary_function<_T1, _T2, _Res>
279 { };
280
281 template<typename _Res, typename _T1, typename _T2>
282 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
283 : binary_function<_T1, _T2, _Res>
284 { };
285
286 template<typename _Res, typename _T1, typename _T2>
287 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
288 : binary_function<_T1, _T2, _Res>
289 { };
290
291 // - a function pointer type (unary)
292 template<typename _Res, typename _T1>
293 struct _Reference_wrapper_base<_Res(*)(_T1)>
294 : unary_function<_T1, _Res>
295 { };
296
297 // - a function pointer type (binary)
298 template<typename _Res, typename _T1, typename _T2>
299 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
300 : binary_function<_T1, _T2, _Res>
301 { };
302
303 // - a pointer to member function type (unary, no qualifiers)
304 template<typename _Res, typename _T1>
305 struct _Reference_wrapper_base<_Res (_T1::*)()>
306 : unary_function<_T1*, _Res>
307 { };
308
309 // - a pointer to member function type (binary, no qualifiers)
310 template<typename _Res, typename _T1, typename _T2>
311 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
312 : binary_function<_T1*, _T2, _Res>
313 { };
314
315 // - a pointer to member function type (unary, const)
316 template<typename _Res, typename _T1>
317 struct _Reference_wrapper_base<_Res (_T1::*)() const>
318 : unary_function<const _T1*, _Res>
319 { };
320
321 // - a pointer to member function type (binary, const)
322 template<typename _Res, typename _T1, typename _T2>
323 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
324 : binary_function<const _T1*, _T2, _Res>
325 { };
326
327 // - a pointer to member function type (unary, volatile)
328 template<typename _Res, typename _T1>
329 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
330 : unary_function<volatile _T1*, _Res>
331 { };
332
333 // - a pointer to member function type (binary, volatile)
334 template<typename _Res, typename _T1, typename _T2>
335 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
336 : binary_function<volatile _T1*, _T2, _Res>
337 { };
338
339 // - a pointer to member function type (unary, const volatile)
340 template<typename _Res, typename _T1>
341 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
342 : unary_function<const volatile _T1*, _Res>
343 { };
344
345 // - a pointer to member function type (binary, const volatile)
346 template<typename _Res, typename _T1, typename _T2>
347 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
348 : binary_function<const volatile _T1*, _T2, _Res>
349 { };
350
351 /**
352 * @brief Primary class template for reference_wrapper.
353 * @ingroup functors
354 * @{
355 */
356 template<typename _Tp>
357 class reference_wrapper
358 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
359 {
360 _Tp* _M_data;
361
362 public:
363 typedef _Tp type;
364
365 reference_wrapper(_Tp& __indata) noexcept
366 : _M_data(std::__addressof(__indata))
367 { }
368
369 reference_wrapper(_Tp&&) = delete;
370
371 reference_wrapper(const reference_wrapper&) = default;
372
373 reference_wrapper&
374 operator=(const reference_wrapper&) = default;
375
376 operator _Tp&() const noexcept
377 { return this->get(); }
378
379 _Tp&
380 get() const noexcept
381 { return *_M_data; }
382
383 template<typename... _Args>
384 typename result_of<_Tp&(_Args&&...)>::type
385 operator()(_Args&&... __args) const
386 {
387 return std::__invoke(get(), std::forward<_Args>(__args)...);
388 }
389 };
390
391
392 /// Denotes a reference should be taken to a variable.
393 template<typename _Tp>
394 inline reference_wrapper<_Tp>
395 ref(_Tp& __t) noexcept
396 { return reference_wrapper<_Tp>(__t); }
397
398 /// Denotes a const reference should be taken to a variable.
399 template<typename _Tp>
400 inline reference_wrapper<const _Tp>
401 cref(const _Tp& __t) noexcept
402 { return reference_wrapper<const _Tp>(__t); }
403
404 template<typename _Tp>
405 void ref(const _Tp&&) = delete;
406
407 template<typename _Tp>
408 void cref(const _Tp&&) = delete;
409
410 /// Partial specialization.
411 template<typename _Tp>
412 inline reference_wrapper<_Tp>
413 ref(reference_wrapper<_Tp> __t) noexcept
414 { return ref(__t.get()); }
415
416 /// Partial specialization.
417 template<typename _Tp>
418 inline reference_wrapper<const _Tp>
419 cref(reference_wrapper<_Tp> __t) noexcept
420 { return cref(__t.get()); }
421
422 // @} group functors
423
424 template<typename... _Types>
425 struct _Pack : integral_constant<size_t, sizeof...(_Types)>
426 { };
427
428 template<typename _From, typename _To, bool = _From::value == _To::value>
429 struct _AllConvertible : false_type
430 { };
431
432 template<typename... _From, typename... _To>
433 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
434 : __and_<is_convertible<_From, _To>...>
435 { };
436
437 template<typename _Tp1, typename _Tp2>
438 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
439 typename std::decay<_Tp2>::type>>;
440
441 /**
442 * Derives from @c unary_function or @c binary_function, or perhaps
443 * nothing, depending on the number of arguments provided. The
444 * primary template is the basis case, which derives nothing.
445 */
446 template<typename _Res, typename... _ArgTypes>
447 struct _Maybe_unary_or_binary_function { };
448
449 /// Derives from @c unary_function, as appropriate.
450 template<typename _Res, typename _T1>
451 struct _Maybe_unary_or_binary_function<_Res, _T1>
452 : std::unary_function<_T1, _Res> { };
453
454 /// Derives from @c binary_function, as appropriate.
455 template<typename _Res, typename _T1, typename _T2>
456 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
457 : std::binary_function<_T1, _T2, _Res> { };
458
459 template<typename _Signature>
460 struct _Mem_fn_traits;
461
462 template<typename _Res, typename _Class, typename... _ArgTypes>
463 struct _Mem_fn_traits_base
464 {
465 using __result_type = _Res;
466 using __maybe_type
467 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
468 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
469 };
470
471 #define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \
472 template<typename _Res, typename _Class, typename... _ArgTypes> \
473 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \
474 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
475 { \
476 using __vararg = false_type; \
477 }; \
478 template<typename _Res, typename _Class, typename... _ArgTypes> \
479 struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \
480 : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \
481 { \
482 using __vararg = true_type; \
483 };
484
485 #define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \
486 _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \
487 _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \
488 _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \
489 _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
490
491 _GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
492 _GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
493 _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
494
495 #undef _GLIBCXX_MEM_FN_TRAITS
496 #undef _GLIBCXX_MEM_FN_TRAITS2
497
498 template<typename _MemFunPtr,
499 bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
500 class _Mem_fn_base
501 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
502 {
503 using _Traits = _Mem_fn_traits<_MemFunPtr>;
504
505 using _Arity = typename _Traits::__arity;
506 using _Varargs = typename _Traits::__vararg;
507
508 template<typename _Func, typename... _BoundArgs>
509 friend struct _Bind_check_arity;
510
511 _MemFunPtr _M_pmf;
512
513 public:
514
515 using result_type = typename _Traits::__result_type;
516
517 explicit constexpr
518 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
519
520 template<typename... _Args>
521 auto
522 operator()(_Args&&... __args) const
523 noexcept(noexcept(
524 std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
525 -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
526 { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
527 };
528
529 // Partial specialization for member object pointers.
530 template<typename _MemObjPtr>
531 class _Mem_fn_base<_MemObjPtr, false>
532 {
533 using _Arity = integral_constant<size_t, 0>;
534 using _Varargs = false_type;
535
536 template<typename _Func, typename... _BoundArgs>
537 friend struct _Bind_check_arity;
538
539 _MemObjPtr _M_pm;
540
541 public:
542 explicit constexpr
543 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
544
545 template<typename _Tp>
546 auto
547 operator()(_Tp&& __obj) const
548 noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
549 -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
550 { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
551 };
552
553 template<typename _Res, typename _Class>
554 struct _Mem_fn<_Res _Class::*>
555 : _Mem_fn_base<_Res _Class::*>
556 {
557 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
558 };
559
560 // _GLIBCXX_RESOLVE_LIB_DEFECTS
561 // 2048. Unnecessary mem_fn overloads
562 /**
563 * @brief Returns a function object that forwards to the member
564 * pointer @a pm.
565 * @ingroup functors
566 */
567 template<typename _Tp, typename _Class>
568 inline _Mem_fn<_Tp _Class::*>
569 mem_fn(_Tp _Class::* __pm) noexcept
570 {
571 return _Mem_fn<_Tp _Class::*>(__pm);
572 }
573
574 /**
575 * @brief Determines if the given type _Tp is a function object that
576 * should be treated as a subexpression when evaluating calls to
577 * function objects returned by bind().
578 *
579 * C++11 [func.bind.isbind].
580 * @ingroup binders
581 */
582 template<typename _Tp>
583 struct is_bind_expression
584 : public false_type { };
585
586 /**
587 * @brief Determines if the given type _Tp is a placeholder in a
588 * bind() expression and, if so, which placeholder it is.
589 *
590 * C++11 [func.bind.isplace].
591 * @ingroup binders
592 */
593 template<typename _Tp>
594 struct is_placeholder
595 : public integral_constant<int, 0>
596 { };
597
598 #if __cplusplus > 201402L
599 template <typename _Tp> constexpr bool is_bind_expression_v
600 = is_bind_expression<_Tp>::value;
601 template <typename _Tp> constexpr int is_placeholder_v
602 = is_placeholder<_Tp>::value;
603 #endif // C++17
604
605 /** @brief The type of placeholder objects defined by libstdc++.
606 * @ingroup binders
607 */
608 template<int _Num> struct _Placeholder { };
609
610 _GLIBCXX_END_NAMESPACE_VERSION
611
612 /** @namespace std::placeholders
613 * @brief ISO C++11 entities sub-namespace for functional.
614 * @ingroup binders
615 */
616 namespace placeholders
617 {
618 _GLIBCXX_BEGIN_NAMESPACE_VERSION
619 /* Define a large number of placeholders. There is no way to
620 * simplify this with variadic templates, because we're introducing
621 * unique names for each.
622 */
623 extern const _Placeholder<1> _1;
624 extern const _Placeholder<2> _2;
625 extern const _Placeholder<3> _3;
626 extern const _Placeholder<4> _4;
627 extern const _Placeholder<5> _5;
628 extern const _Placeholder<6> _6;
629 extern const _Placeholder<7> _7;
630 extern const _Placeholder<8> _8;
631 extern const _Placeholder<9> _9;
632 extern const _Placeholder<10> _10;
633 extern const _Placeholder<11> _11;
634 extern const _Placeholder<12> _12;
635 extern const _Placeholder<13> _13;
636 extern const _Placeholder<14> _14;
637 extern const _Placeholder<15> _15;
638 extern const _Placeholder<16> _16;
639 extern const _Placeholder<17> _17;
640 extern const _Placeholder<18> _18;
641 extern const _Placeholder<19> _19;
642 extern const _Placeholder<20> _20;
643 extern const _Placeholder<21> _21;
644 extern const _Placeholder<22> _22;
645 extern const _Placeholder<23> _23;
646 extern const _Placeholder<24> _24;
647 extern const _Placeholder<25> _25;
648 extern const _Placeholder<26> _26;
649 extern const _Placeholder<27> _27;
650 extern const _Placeholder<28> _28;
651 extern const _Placeholder<29> _29;
652 _GLIBCXX_END_NAMESPACE_VERSION
653 }
654
655 _GLIBCXX_BEGIN_NAMESPACE_VERSION
656
657 /**
658 * Partial specialization of is_placeholder that provides the placeholder
659 * number for the placeholder objects defined by libstdc++.
660 * @ingroup binders
661 */
662 template<int _Num>
663 struct is_placeholder<_Placeholder<_Num> >
664 : public integral_constant<int, _Num>
665 { };
666
667 template<int _Num>
668 struct is_placeholder<const _Placeholder<_Num> >
669 : public integral_constant<int, _Num>
670 { };
671
672
673 // Like tuple_element_t but SFINAE-friendly.
674 template<std::size_t __i, typename _Tuple>
675 using _Safe_tuple_element_t
676 = typename enable_if<(__i < tuple_size<_Tuple>::value),
677 tuple_element<__i, _Tuple>>::type::type;
678
679 /**
680 * Maps an argument to bind() into an actual argument to the bound
681 * function object [func.bind.bind]/10. Only the first parameter should
682 * be specified: the rest are used to determine among the various
683 * implementations. Note that, although this class is a function
684 * object, it isn't entirely normal because it takes only two
685 * parameters regardless of the number of parameters passed to the
686 * bind expression. The first parameter is the bound argument and
687 * the second parameter is a tuple containing references to the
688 * rest of the arguments.
689 */
690 template<typename _Arg,
691 bool _IsBindExp = is_bind_expression<_Arg>::value,
692 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
693 class _Mu;
694
695 /**
696 * If the argument is reference_wrapper<_Tp>, returns the
697 * underlying reference.
698 * C++11 [func.bind.bind] p10 bullet 1.
699 */
700 template<typename _Tp>
701 class _Mu<reference_wrapper<_Tp>, false, false>
702 {
703 public:
704 /* Note: This won't actually work for const volatile
705 * reference_wrappers, because reference_wrapper::get() is const
706 * but not volatile-qualified. This might be a defect in the TR.
707 */
708 template<typename _CVRef, typename _Tuple>
709 _Tp&
710 operator()(_CVRef& __arg, _Tuple&) const volatile
711 { return __arg.get(); }
712 };
713
714 /**
715 * If the argument is a bind expression, we invoke the underlying
716 * function object with the same cv-qualifiers as we are given and
717 * pass along all of our arguments (unwrapped).
718 * C++11 [func.bind.bind] p10 bullet 2.
719 */
720 template<typename _Arg>
721 class _Mu<_Arg, true, false>
722 {
723 public:
724 template<typename _CVArg, typename... _Args>
725 auto
726 operator()(_CVArg& __arg,
727 tuple<_Args...>& __tuple) const volatile
728 -> decltype(__arg(declval<_Args>()...))
729 {
730 // Construct an index tuple and forward to __call
731 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
732 _Indexes;
733 return this->__call(__arg, __tuple, _Indexes());
734 }
735
736 private:
737 // Invokes the underlying function object __arg by unpacking all
738 // of the arguments in the tuple.
739 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
740 auto
741 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
742 const _Index_tuple<_Indexes...>&) const volatile
743 -> decltype(__arg(declval<_Args>()...))
744 {
745 return __arg(std::get<_Indexes>(std::move(__tuple))...);
746 }
747 };
748
749 /**
750 * If the argument is a placeholder for the Nth argument, returns
751 * a reference to the Nth argument to the bind function object.
752 * C++11 [func.bind.bind] p10 bullet 3.
753 */
754 template<typename _Arg>
755 class _Mu<_Arg, false, true>
756 {
757 public:
758 template<typename _Tuple>
759 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
760 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
761 {
762 return
763 ::std::get<(is_placeholder<_Arg>::value - 1)>(std::move(__tuple));
764 }
765 };
766
767 /**
768 * If the argument is just a value, returns a reference to that
769 * value. The cv-qualifiers on the reference are determined by the caller.
770 * C++11 [func.bind.bind] p10 bullet 4.
771 */
772 template<typename _Arg>
773 class _Mu<_Arg, false, false>
774 {
775 public:
776 template<typename _CVArg, typename _Tuple>
777 _CVArg&&
778 operator()(_CVArg&& __arg, _Tuple&) const volatile
779 { return std::forward<_CVArg>(__arg); }
780 };
781
782 // std::get<I> for volatile-qualified tuples
783 template<std::size_t _Ind, typename... _Tp>
784 inline auto
785 __volget(volatile tuple<_Tp...>& __tuple)
786 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
787 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
788
789 // std::get<I> for const-volatile-qualified tuples
790 template<std::size_t _Ind, typename... _Tp>
791 inline auto
792 __volget(const volatile tuple<_Tp...>& __tuple)
793 -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
794 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
795
796 /// Type of the function object returned from bind().
797 template<typename _Signature>
798 struct _Bind;
799
800 template<typename _Functor, typename... _Bound_args>
801 class _Bind<_Functor(_Bound_args...)>
802 : public _Weak_result_type<_Functor>
803 {
804 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
805 _Bound_indexes;
806
807 _Functor _M_f;
808 tuple<_Bound_args...> _M_bound_args;
809
810 // Call unqualified
811 template<typename _Result, typename... _Args, std::size_t... _Indexes>
812 _Result
813 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
814 {
815 return std::__invoke(_M_f,
816 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
817 );
818 }
819
820 // Call as const
821 template<typename _Result, typename... _Args, std::size_t... _Indexes>
822 _Result
823 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
824 {
825 return std::__invoke(_M_f,
826 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
827 );
828 }
829
830 // Call as volatile
831 template<typename _Result, typename... _Args, std::size_t... _Indexes>
832 _Result
833 __call_v(tuple<_Args...>&& __args,
834 _Index_tuple<_Indexes...>) volatile
835 {
836 return std::__invoke(_M_f,
837 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
838 );
839 }
840
841 // Call as const volatile
842 template<typename _Result, typename... _Args, std::size_t... _Indexes>
843 _Result
844 __call_c_v(tuple<_Args...>&& __args,
845 _Index_tuple<_Indexes...>) const volatile
846 {
847 return std::__invoke(_M_f,
848 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
849 );
850 }
851
852 template<typename _BoundArg, typename _CallArgs>
853 using _Mu_type = decltype(
854 _Mu<typename remove_cv<_BoundArg>::type>()(
855 std::declval<_BoundArg&>(), std::declval<_CallArgs&>()) );
856
857 template<typename _Fn, typename _CallArgs, typename... _BArgs>
858 using _Res_type_impl
859 = typename result_of< _Fn&(_Mu_type<_BArgs, _CallArgs>...) >::type;
860
861 template<typename _CallArgs>
862 using _Res_type = _Res_type_impl<_Functor, _CallArgs, _Bound_args...>;
863
864 template<typename _CallArgs>
865 using __dependent = typename
866 enable_if<bool(tuple_size<_CallArgs>::value+1), _Functor>::type;
867
868 template<typename _CallArgs, template<class> class __cv_quals>
869 using _Res_type_cv = _Res_type_impl<
870 typename __cv_quals<__dependent<_CallArgs>>::type,
871 _CallArgs,
872 typename __cv_quals<_Bound_args>::type...>;
873
874 public:
875 template<typename... _Args>
876 explicit _Bind(const _Functor& __f, _Args&&... __args)
877 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
878 { }
879
880 template<typename... _Args>
881 explicit _Bind(_Functor&& __f, _Args&&... __args)
882 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
883 { }
884
885 _Bind(const _Bind&) = default;
886
887 _Bind(_Bind&& __b)
888 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
889 { }
890
891 // Call unqualified
892 template<typename... _Args,
893 typename _Result = _Res_type<tuple<_Args...>>>
894 _Result
895 operator()(_Args&&... __args)
896 {
897 return this->__call<_Result>(
898 std::forward_as_tuple(std::forward<_Args>(__args)...),
899 _Bound_indexes());
900 }
901
902 // Call as const
903 template<typename... _Args,
904 typename _Result = _Res_type_cv<tuple<_Args...>, add_const>>
905 _Result
906 operator()(_Args&&... __args) const
907 {
908 return this->__call_c<_Result>(
909 std::forward_as_tuple(std::forward<_Args>(__args)...),
910 _Bound_indexes());
911 }
912
913 #if __cplusplus > 201402L
914 # define _GLIBCXX_DEPR_BIND \
915 [[deprecated("std::bind does not support volatile in C++17")]]
916 #else
917 # define _GLIBCXX_DEPR_BIND
918 #endif
919 // Call as volatile
920 template<typename... _Args,
921 typename _Result = _Res_type_cv<tuple<_Args...>, add_volatile>>
922 _GLIBCXX_DEPR_BIND
923 _Result
924 operator()(_Args&&... __args) volatile
925 {
926 return this->__call_v<_Result>(
927 std::forward_as_tuple(std::forward<_Args>(__args)...),
928 _Bound_indexes());
929 }
930
931 // Call as const volatile
932 template<typename... _Args,
933 typename _Result = _Res_type_cv<tuple<_Args...>, add_cv>>
934 _GLIBCXX_DEPR_BIND
935 _Result
936 operator()(_Args&&... __args) const volatile
937 {
938 return this->__call_c_v<_Result>(
939 std::forward_as_tuple(std::forward<_Args>(__args)...),
940 _Bound_indexes());
941 }
942 };
943
944 /// Type of the function object returned from bind<R>().
945 template<typename _Result, typename _Signature>
946 struct _Bind_result;
947
948 template<typename _Result, typename _Functor, typename... _Bound_args>
949 class _Bind_result<_Result, _Functor(_Bound_args...)>
950 {
951 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
952 _Bound_indexes;
953
954 _Functor _M_f;
955 tuple<_Bound_args...> _M_bound_args;
956
957 // sfinae types
958 template<typename _Res>
959 using __enable_if_void
960 = typename enable_if<is_void<_Res>{}>::type;
961
962 template<typename _Res>
963 using __disable_if_void
964 = typename enable_if<!is_void<_Res>{}, _Result>::type;
965
966 // Call unqualified
967 template<typename _Res, typename... _Args, std::size_t... _Indexes>
968 __disable_if_void<_Res>
969 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
970 {
971 return std::__invoke(_M_f, _Mu<_Bound_args>()
972 (std::get<_Indexes>(_M_bound_args), __args)...);
973 }
974
975 // Call unqualified, return void
976 template<typename _Res, typename... _Args, std::size_t... _Indexes>
977 __enable_if_void<_Res>
978 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
979 {
980 std::__invoke(_M_f, _Mu<_Bound_args>()
981 (std::get<_Indexes>(_M_bound_args), __args)...);
982 }
983
984 // Call as const
985 template<typename _Res, typename... _Args, std::size_t... _Indexes>
986 __disable_if_void<_Res>
987 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
988 {
989 return std::__invoke(_M_f, _Mu<_Bound_args>()
990 (std::get<_Indexes>(_M_bound_args), __args)...);
991 }
992
993 // Call as const, return void
994 template<typename _Res, typename... _Args, std::size_t... _Indexes>
995 __enable_if_void<_Res>
996 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
997 {
998 std::__invoke(_M_f, _Mu<_Bound_args>()
999 (std::get<_Indexes>(_M_bound_args), __args)...);
1000 }
1001
1002 // Call as volatile
1003 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1004 __disable_if_void<_Res>
1005 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
1006 {
1007 return std::__invoke(_M_f, _Mu<_Bound_args>()
1008 (__volget<_Indexes>(_M_bound_args), __args)...);
1009 }
1010
1011 // Call as volatile, return void
1012 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1013 __enable_if_void<_Res>
1014 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
1015 {
1016 std::__invoke(_M_f, _Mu<_Bound_args>()
1017 (__volget<_Indexes>(_M_bound_args), __args)...);
1018 }
1019
1020 // Call as const volatile
1021 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1022 __disable_if_void<_Res>
1023 __call(tuple<_Args...>&& __args,
1024 _Index_tuple<_Indexes...>) const volatile
1025 {
1026 return std::__invoke(_M_f, _Mu<_Bound_args>()
1027 (__volget<_Indexes>(_M_bound_args), __args)...);
1028 }
1029
1030 // Call as const volatile, return void
1031 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1032 __enable_if_void<_Res>
1033 __call(tuple<_Args...>&& __args,
1034 _Index_tuple<_Indexes...>) const volatile
1035 {
1036 std::__invoke(_M_f, _Mu<_Bound_args>()
1037 (__volget<_Indexes>(_M_bound_args), __args)...);
1038 }
1039
1040 public:
1041 typedef _Result result_type;
1042
1043 template<typename... _Args>
1044 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1045 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1046 { }
1047
1048 template<typename... _Args>
1049 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1050 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1051 { }
1052
1053 _Bind_result(const _Bind_result&) = default;
1054
1055 _Bind_result(_Bind_result&& __b)
1056 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1057 { }
1058
1059 // Call unqualified
1060 template<typename... _Args>
1061 result_type
1062 operator()(_Args&&... __args)
1063 {
1064 return this->__call<_Result>(
1065 std::forward_as_tuple(std::forward<_Args>(__args)...),
1066 _Bound_indexes());
1067 }
1068
1069 // Call as const
1070 template<typename... _Args>
1071 result_type
1072 operator()(_Args&&... __args) const
1073 {
1074 return this->__call<_Result>(
1075 std::forward_as_tuple(std::forward<_Args>(__args)...),
1076 _Bound_indexes());
1077 }
1078
1079 // Call as volatile
1080 template<typename... _Args>
1081 _GLIBCXX_DEPR_BIND
1082 result_type
1083 operator()(_Args&&... __args) volatile
1084 {
1085 return this->__call<_Result>(
1086 std::forward_as_tuple(std::forward<_Args>(__args)...),
1087 _Bound_indexes());
1088 }
1089
1090 // Call as const volatile
1091 template<typename... _Args>
1092 _GLIBCXX_DEPR_BIND
1093 result_type
1094 operator()(_Args&&... __args) const volatile
1095 {
1096 return this->__call<_Result>(
1097 std::forward_as_tuple(std::forward<_Args>(__args)...),
1098 _Bound_indexes());
1099 }
1100 };
1101 #undef _GLIBCXX_DEPR_BIND
1102
1103 /**
1104 * @brief Class template _Bind is always a bind expression.
1105 * @ingroup binders
1106 */
1107 template<typename _Signature>
1108 struct is_bind_expression<_Bind<_Signature> >
1109 : public true_type { };
1110
1111 /**
1112 * @brief Class template _Bind is always a bind expression.
1113 * @ingroup binders
1114 */
1115 template<typename _Signature>
1116 struct is_bind_expression<const _Bind<_Signature> >
1117 : public true_type { };
1118
1119 /**
1120 * @brief Class template _Bind is always a bind expression.
1121 * @ingroup binders
1122 */
1123 template<typename _Signature>
1124 struct is_bind_expression<volatile _Bind<_Signature> >
1125 : public true_type { };
1126
1127 /**
1128 * @brief Class template _Bind is always a bind expression.
1129 * @ingroup binders
1130 */
1131 template<typename _Signature>
1132 struct is_bind_expression<const volatile _Bind<_Signature>>
1133 : public true_type { };
1134
1135 /**
1136 * @brief Class template _Bind_result is always a bind expression.
1137 * @ingroup binders
1138 */
1139 template<typename _Result, typename _Signature>
1140 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1141 : public true_type { };
1142
1143 /**
1144 * @brief Class template _Bind_result is always a bind expression.
1145 * @ingroup binders
1146 */
1147 template<typename _Result, typename _Signature>
1148 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1149 : public true_type { };
1150
1151 /**
1152 * @brief Class template _Bind_result is always a bind expression.
1153 * @ingroup binders
1154 */
1155 template<typename _Result, typename _Signature>
1156 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1157 : public true_type { };
1158
1159 /**
1160 * @brief Class template _Bind_result is always a bind expression.
1161 * @ingroup binders
1162 */
1163 template<typename _Result, typename _Signature>
1164 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1165 : public true_type { };
1166
1167 template<typename _Func, typename... _BoundArgs>
1168 struct _Bind_check_arity { };
1169
1170 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1171 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
1172 {
1173 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
1174 "Wrong number of arguments for function");
1175 };
1176
1177 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1178 struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
1179 {
1180 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
1181 "Wrong number of arguments for function");
1182 };
1183
1184 template<typename _Tp, typename _Class, typename... _BoundArgs>
1185 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
1186 {
1187 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
1188 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
1189 static_assert(_Varargs::value
1190 ? sizeof...(_BoundArgs) >= _Arity::value + 1
1191 : sizeof...(_BoundArgs) == _Arity::value + 1,
1192 "Wrong number of arguments for pointer-to-member");
1193 };
1194
1195 // Trait type used to remove std::bind() from overload set via SFINAE
1196 // when first argument has integer type, so that std::bind() will
1197 // not be a better match than ::bind() from the BSD Sockets API.
1198 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1199 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1200
1201 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1202 struct _Bind_helper
1203 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1204 {
1205 typedef typename decay<_Func>::type __func_type;
1206 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1207 };
1208
1209 // Partial specialization for is_socketlike == true, does not define
1210 // nested type so std::bind() will not participate in overload resolution
1211 // when the first argument might be a socket file descriptor.
1212 template<typename _Func, typename... _BoundArgs>
1213 struct _Bind_helper<true, _Func, _BoundArgs...>
1214 { };
1215
1216 /**
1217 * @brief Function template for std::bind.
1218 * @ingroup binders
1219 */
1220 template<typename _Func, typename... _BoundArgs>
1221 inline typename
1222 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1223 bind(_Func&& __f, _BoundArgs&&... __args)
1224 {
1225 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1226 return typename __helper_type::type(std::forward<_Func>(__f),
1227 std::forward<_BoundArgs>(__args)...);
1228 }
1229
1230 template<typename _Result, typename _Func, typename... _BoundArgs>
1231 struct _Bindres_helper
1232 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1233 {
1234 typedef typename decay<_Func>::type __functor_type;
1235 typedef _Bind_result<_Result,
1236 __functor_type(typename decay<_BoundArgs>::type...)>
1237 type;
1238 };
1239
1240 /**
1241 * @brief Function template for std::bind<R>.
1242 * @ingroup binders
1243 */
1244 template<typename _Result, typename _Func, typename... _BoundArgs>
1245 inline
1246 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1247 bind(_Func&& __f, _BoundArgs&&... __args)
1248 {
1249 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1250 return typename __helper_type::type(std::forward<_Func>(__f),
1251 std::forward<_BoundArgs>(__args)...);
1252 }
1253
1254 /**
1255 * @brief Exception class thrown when class template function's
1256 * operator() is called with an empty target.
1257 * @ingroup exceptions
1258 */
1259 class bad_function_call : public std::exception
1260 {
1261 public:
1262 virtual ~bad_function_call() noexcept;
1263
1264 const char* what() const noexcept;
1265 };
1266
1267 /**
1268 * Trait identifying "location-invariant" types, meaning that the
1269 * address of the object (or any of its members) will not escape.
1270 * Trivially copyable types are location-invariant and users can
1271 * specialize this trait for other types.
1272 */
1273 template<typename _Tp>
1274 struct __is_location_invariant
1275 : is_trivially_copyable<_Tp>::type
1276 { };
1277
1278 class _Undefined_class;
1279
1280 union _Nocopy_types
1281 {
1282 void* _M_object;
1283 const void* _M_const_object;
1284 void (*_M_function_pointer)();
1285 void (_Undefined_class::*_M_member_pointer)();
1286 };
1287
1288 union [[gnu::may_alias]] _Any_data
1289 {
1290 void* _M_access() { return &_M_pod_data[0]; }
1291 const void* _M_access() const { return &_M_pod_data[0]; }
1292
1293 template<typename _Tp>
1294 _Tp&
1295 _M_access()
1296 { return *static_cast<_Tp*>(_M_access()); }
1297
1298 template<typename _Tp>
1299 const _Tp&
1300 _M_access() const
1301 { return *static_cast<const _Tp*>(_M_access()); }
1302
1303 _Nocopy_types _M_unused;
1304 char _M_pod_data[sizeof(_Nocopy_types)];
1305 };
1306
1307 enum _Manager_operation
1308 {
1309 __get_type_info,
1310 __get_functor_ptr,
1311 __clone_functor,
1312 __destroy_functor
1313 };
1314
1315 // Simple type wrapper that helps avoid annoying const problems
1316 // when casting between void pointers and pointers-to-pointers.
1317 template<typename _Tp>
1318 struct _Simple_type_wrapper
1319 {
1320 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1321
1322 _Tp __value;
1323 };
1324
1325 template<typename _Tp>
1326 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1327 : __is_location_invariant<_Tp>
1328 { };
1329
1330 template<typename _Signature>
1331 class function;
1332
1333 /// Base class of all polymorphic function object wrappers.
1334 class _Function_base
1335 {
1336 public:
1337 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1338 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1339
1340 template<typename _Functor>
1341 class _Base_manager
1342 {
1343 protected:
1344 static const bool __stored_locally =
1345 (__is_location_invariant<_Functor>::value
1346 && sizeof(_Functor) <= _M_max_size
1347 && __alignof__(_Functor) <= _M_max_align
1348 && (_M_max_align % __alignof__(_Functor) == 0));
1349
1350 typedef integral_constant<bool, __stored_locally> _Local_storage;
1351
1352 // Retrieve a pointer to the function object
1353 static _Functor*
1354 _M_get_pointer(const _Any_data& __source)
1355 {
1356 const _Functor* __ptr =
1357 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1358 /* have stored a pointer */ : __source._M_access<_Functor*>();
1359 return const_cast<_Functor*>(__ptr);
1360 }
1361
1362 // Clone a location-invariant function object that fits within
1363 // an _Any_data structure.
1364 static void
1365 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1366 {
1367 ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1368 }
1369
1370 // Clone a function object that is not location-invariant or
1371 // that cannot fit into an _Any_data structure.
1372 static void
1373 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1374 {
1375 __dest._M_access<_Functor*>() =
1376 new _Functor(*__source._M_access<_Functor*>());
1377 }
1378
1379 // Destroying a location-invariant object may still require
1380 // destruction.
1381 static void
1382 _M_destroy(_Any_data& __victim, true_type)
1383 {
1384 __victim._M_access<_Functor>().~_Functor();
1385 }
1386
1387 // Destroying an object located on the heap.
1388 static void
1389 _M_destroy(_Any_data& __victim, false_type)
1390 {
1391 delete __victim._M_access<_Functor*>();
1392 }
1393
1394 public:
1395 static bool
1396 _M_manager(_Any_data& __dest, const _Any_data& __source,
1397 _Manager_operation __op)
1398 {
1399 switch (__op)
1400 {
1401 #if __cpp_rtti
1402 case __get_type_info:
1403 __dest._M_access<const type_info*>() = &typeid(_Functor);
1404 break;
1405 #endif
1406 case __get_functor_ptr:
1407 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1408 break;
1409
1410 case __clone_functor:
1411 _M_clone(__dest, __source, _Local_storage());
1412 break;
1413
1414 case __destroy_functor:
1415 _M_destroy(__dest, _Local_storage());
1416 break;
1417 }
1418 return false;
1419 }
1420
1421 static void
1422 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1423 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1424
1425 template<typename _Signature>
1426 static bool
1427 _M_not_empty_function(const function<_Signature>& __f)
1428 { return static_cast<bool>(__f); }
1429
1430 template<typename _Tp>
1431 static bool
1432 _M_not_empty_function(_Tp* __fp)
1433 { return __fp != nullptr; }
1434
1435 template<typename _Class, typename _Tp>
1436 static bool
1437 _M_not_empty_function(_Tp _Class::* __mp)
1438 { return __mp != nullptr; }
1439
1440 template<typename _Tp>
1441 static bool
1442 _M_not_empty_function(const _Tp&)
1443 { return true; }
1444
1445 private:
1446 static void
1447 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1448 { ::new (__functor._M_access()) _Functor(std::move(__f)); }
1449
1450 static void
1451 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1452 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1453 };
1454
1455 template<typename _Functor>
1456 class _Ref_manager : public _Base_manager<_Functor*>
1457 {
1458 typedef _Function_base::_Base_manager<_Functor*> _Base;
1459
1460 public:
1461 static bool
1462 _M_manager(_Any_data& __dest, const _Any_data& __source,
1463 _Manager_operation __op)
1464 {
1465 switch (__op)
1466 {
1467 #if __cpp_rtti
1468 case __get_type_info:
1469 __dest._M_access<const type_info*>() = &typeid(_Functor);
1470 break;
1471 #endif
1472 case __get_functor_ptr:
1473 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1474 return is_const<_Functor>::value;
1475 break;
1476
1477 default:
1478 _Base::_M_manager(__dest, __source, __op);
1479 }
1480 return false;
1481 }
1482
1483 static void
1484 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1485 {
1486 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1487 }
1488 };
1489
1490 _Function_base() : _M_manager(nullptr) { }
1491
1492 ~_Function_base()
1493 {
1494 if (_M_manager)
1495 _M_manager(_M_functor, _M_functor, __destroy_functor);
1496 }
1497
1498
1499 bool _M_empty() const { return !_M_manager; }
1500
1501 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1502 _Manager_operation);
1503
1504 _Any_data _M_functor;
1505 _Manager_type _M_manager;
1506 };
1507
1508 template<typename _Signature, typename _Functor>
1509 class _Function_handler;
1510
1511 template<typename _Res, typename _Functor, typename... _ArgTypes>
1512 class _Function_handler<_Res(_ArgTypes...), _Functor>
1513 : public _Function_base::_Base_manager<_Functor>
1514 {
1515 typedef _Function_base::_Base_manager<_Functor> _Base;
1516
1517 public:
1518 static _Res
1519 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1520 {
1521 return (*_Base::_M_get_pointer(__functor))(
1522 std::forward<_ArgTypes>(__args)...);
1523 }
1524 };
1525
1526 template<typename _Functor, typename... _ArgTypes>
1527 class _Function_handler<void(_ArgTypes...), _Functor>
1528 : public _Function_base::_Base_manager<_Functor>
1529 {
1530 typedef _Function_base::_Base_manager<_Functor> _Base;
1531
1532 public:
1533 static void
1534 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1535 {
1536 (*_Base::_M_get_pointer(__functor))(
1537 std::forward<_ArgTypes>(__args)...);
1538 }
1539 };
1540
1541 template<typename _Res, typename _Functor, typename... _ArgTypes>
1542 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1543 : public _Function_base::_Ref_manager<_Functor>
1544 {
1545 typedef _Function_base::_Ref_manager<_Functor> _Base;
1546
1547 public:
1548 static _Res
1549 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1550 {
1551 return std::__invoke(**_Base::_M_get_pointer(__functor),
1552 std::forward<_ArgTypes>(__args)...);
1553 }
1554 };
1555
1556 template<typename _Functor, typename... _ArgTypes>
1557 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1558 : public _Function_base::_Ref_manager<_Functor>
1559 {
1560 typedef _Function_base::_Ref_manager<_Functor> _Base;
1561
1562 public:
1563 static void
1564 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1565 {
1566 std::__invoke(**_Base::_M_get_pointer(__functor),
1567 std::forward<_ArgTypes>(__args)...);
1568 }
1569 };
1570
1571 template<typename _Class, typename _Member, typename _Res,
1572 typename... _ArgTypes>
1573 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1574 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1575 {
1576 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1577 _Base;
1578
1579 public:
1580 static _Res
1581 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1582 {
1583 return std::__invoke(_Base::_M_get_pointer(__functor)->__value,
1584 std::forward<_ArgTypes>(__args)...);
1585 }
1586 };
1587
1588 template<typename _Class, typename _Member, typename... _ArgTypes>
1589 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1590 : public _Function_base::_Base_manager<
1591 _Simple_type_wrapper< _Member _Class::* > >
1592 {
1593 typedef _Member _Class::* _Functor;
1594 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1595 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1596
1597 public:
1598 static bool
1599 _M_manager(_Any_data& __dest, const _Any_data& __source,
1600 _Manager_operation __op)
1601 {
1602 switch (__op)
1603 {
1604 #if __cpp_rtti
1605 case __get_type_info:
1606 __dest._M_access<const type_info*>() = &typeid(_Functor);
1607 break;
1608 #endif
1609 case __get_functor_ptr:
1610 __dest._M_access<_Functor*>() =
1611 &_Base::_M_get_pointer(__source)->__value;
1612 break;
1613
1614 default:
1615 _Base::_M_manager(__dest, __source, __op);
1616 }
1617 return false;
1618 }
1619
1620 static void
1621 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1622 {
1623 std::__invoke(_Base::_M_get_pointer(__functor)->__value,
1624 std::forward<_ArgTypes>(__args)...);
1625 }
1626 };
1627
1628 template<typename _From, typename _To>
1629 using __check_func_return_type
1630 = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
1631
1632 /**
1633 * @brief Primary class template for std::function.
1634 * @ingroup functors
1635 *
1636 * Polymorphic function wrapper.
1637 */
1638 template<typename _Res, typename... _ArgTypes>
1639 class function<_Res(_ArgTypes...)>
1640 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1641 private _Function_base
1642 {
1643 typedef _Res _Signature_type(_ArgTypes...);
1644
1645 template<typename _Func,
1646 typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type>
1647 struct _Callable : __check_func_return_type<_Res2, _Res> { };
1648
1649 // Used so the return type convertibility checks aren't done when
1650 // performing overload resolution for copy construction/assignment.
1651 template<typename _Tp>
1652 struct _Callable<function, _Tp> : false_type { };
1653
1654 template<typename _Cond, typename _Tp>
1655 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
1656
1657 public:
1658 typedef _Res result_type;
1659
1660 // [3.7.2.1] construct/copy/destroy
1661
1662 /**
1663 * @brief Default construct creates an empty function call wrapper.
1664 * @post @c !(bool)*this
1665 */
1666 function() noexcept
1667 : _Function_base() { }
1668
1669 /**
1670 * @brief Creates an empty function call wrapper.
1671 * @post @c !(bool)*this
1672 */
1673 function(nullptr_t) noexcept
1674 : _Function_base() { }
1675
1676 /**
1677 * @brief %Function copy constructor.
1678 * @param __x A %function object with identical call signature.
1679 * @post @c bool(*this) == bool(__x)
1680 *
1681 * The newly-created %function contains a copy of the target of @a
1682 * __x (if it has one).
1683 */
1684 function(const function& __x);
1685
1686 /**
1687 * @brief %Function move constructor.
1688 * @param __x A %function object rvalue with identical call signature.
1689 *
1690 * The newly-created %function contains the target of @a __x
1691 * (if it has one).
1692 */
1693 function(function&& __x) : _Function_base()
1694 {
1695 __x.swap(*this);
1696 }
1697
1698 /**
1699 * @brief Builds a %function that targets a copy of the incoming
1700 * function object.
1701 * @param __f A %function object that is callable with parameters of
1702 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1703 * to @c Res.
1704 *
1705 * The newly-created %function object will target a copy of
1706 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
1707 * object will contain a reference to the function object @c
1708 * __f.get(). If @a __f is a NULL function pointer or NULL
1709 * pointer-to-member, the newly-created object will be empty.
1710 *
1711 * If @a __f is a non-NULL function pointer or an object of type @c
1712 * reference_wrapper<F>, this function will not throw.
1713 */
1714 template<typename _Functor,
1715 typename = _Requires<__not_<is_same<_Functor, function>>, void>,
1716 typename = _Requires<_Callable<_Functor>, void>>
1717 function(_Functor);
1718
1719 /**
1720 * @brief %Function assignment operator.
1721 * @param __x A %function with identical call signature.
1722 * @post @c (bool)*this == (bool)x
1723 * @returns @c *this
1724 *
1725 * The target of @a __x is copied to @c *this. If @a __x has no
1726 * target, then @c *this will be empty.
1727 *
1728 * If @a __x targets a function pointer or a reference to a function
1729 * object, then this operation will not throw an %exception.
1730 */
1731 function&
1732 operator=(const function& __x)
1733 {
1734 function(__x).swap(*this);
1735 return *this;
1736 }
1737
1738 /**
1739 * @brief %Function move-assignment operator.
1740 * @param __x A %function rvalue with identical call signature.
1741 * @returns @c *this
1742 *
1743 * The target of @a __x is moved to @c *this. If @a __x has no
1744 * target, then @c *this will be empty.
1745 *
1746 * If @a __x targets a function pointer or a reference to a function
1747 * object, then this operation will not throw an %exception.
1748 */
1749 function&
1750 operator=(function&& __x)
1751 {
1752 function(std::move(__x)).swap(*this);
1753 return *this;
1754 }
1755
1756 /**
1757 * @brief %Function assignment to zero.
1758 * @post @c !(bool)*this
1759 * @returns @c *this
1760 *
1761 * The target of @c *this is deallocated, leaving it empty.
1762 */
1763 function&
1764 operator=(nullptr_t) noexcept
1765 {
1766 if (_M_manager)
1767 {
1768 _M_manager(_M_functor, _M_functor, __destroy_functor);
1769 _M_manager = nullptr;
1770 _M_invoker = nullptr;
1771 }
1772 return *this;
1773 }
1774
1775 /**
1776 * @brief %Function assignment to a new target.
1777 * @param __f A %function object that is callable with parameters of
1778 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1779 * to @c Res.
1780 * @return @c *this
1781 *
1782 * This %function object wrapper will target a copy of @a
1783 * __f. If @a __f is @c reference_wrapper<F>, then this function
1784 * object will contain a reference to the function object @c
1785 * __f.get(). If @a __f is a NULL function pointer or NULL
1786 * pointer-to-member, @c this object will be empty.
1787 *
1788 * If @a __f is a non-NULL function pointer or an object of type @c
1789 * reference_wrapper<F>, this function will not throw.
1790 */
1791 template<typename _Functor>
1792 _Requires<_Callable<typename decay<_Functor>::type>, function&>
1793 operator=(_Functor&& __f)
1794 {
1795 function(std::forward<_Functor>(__f)).swap(*this);
1796 return *this;
1797 }
1798
1799 /// @overload
1800 template<typename _Functor>
1801 function&
1802 operator=(reference_wrapper<_Functor> __f) noexcept
1803 {
1804 function(__f).swap(*this);
1805 return *this;
1806 }
1807
1808 // [3.7.2.2] function modifiers
1809
1810 /**
1811 * @brief Swap the targets of two %function objects.
1812 * @param __x A %function with identical call signature.
1813 *
1814 * Swap the targets of @c this function object and @a __f. This
1815 * function will not throw an %exception.
1816 */
1817 void swap(function& __x) noexcept
1818 {
1819 std::swap(_M_functor, __x._M_functor);
1820 std::swap(_M_manager, __x._M_manager);
1821 std::swap(_M_invoker, __x._M_invoker);
1822 }
1823
1824 // [3.7.2.3] function capacity
1825
1826 /**
1827 * @brief Determine if the %function wrapper has a target.
1828 *
1829 * @return @c true when this %function object contains a target,
1830 * or @c false when it is empty.
1831 *
1832 * This function will not throw an %exception.
1833 */
1834 explicit operator bool() const noexcept
1835 { return !_M_empty(); }
1836
1837 // [3.7.2.4] function invocation
1838
1839 /**
1840 * @brief Invokes the function targeted by @c *this.
1841 * @returns the result of the target.
1842 * @throws bad_function_call when @c !(bool)*this
1843 *
1844 * The function call operator invokes the target function object
1845 * stored by @c this.
1846 */
1847 _Res operator()(_ArgTypes... __args) const;
1848
1849 #if __cpp_rtti
1850 // [3.7.2.5] function target access
1851 /**
1852 * @brief Determine the type of the target of this function object
1853 * wrapper.
1854 *
1855 * @returns the type identifier of the target function object, or
1856 * @c typeid(void) if @c !(bool)*this.
1857 *
1858 * This function will not throw an %exception.
1859 */
1860 const type_info& target_type() const noexcept;
1861
1862 /**
1863 * @brief Access the stored target function object.
1864 *
1865 * @return Returns a pointer to the stored target function object,
1866 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
1867 * pointer.
1868 *
1869 * This function will not throw an %exception.
1870 */
1871 template<typename _Functor> _Functor* target() noexcept;
1872
1873 /// @overload
1874 template<typename _Functor> const _Functor* target() const noexcept;
1875 #endif
1876
1877 private:
1878 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
1879 _Invoker_type _M_invoker;
1880 };
1881
1882 // Out-of-line member definitions.
1883 template<typename _Res, typename... _ArgTypes>
1884 function<_Res(_ArgTypes...)>::
1885 function(const function& __x)
1886 : _Function_base()
1887 {
1888 if (static_cast<bool>(__x))
1889 {
1890 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
1891 _M_invoker = __x._M_invoker;
1892 _M_manager = __x._M_manager;
1893 }
1894 }
1895
1896 template<typename _Res, typename... _ArgTypes>
1897 template<typename _Functor, typename, typename>
1898 function<_Res(_ArgTypes...)>::
1899 function(_Functor __f)
1900 : _Function_base()
1901 {
1902 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
1903
1904 if (_My_handler::_M_not_empty_function(__f))
1905 {
1906 _My_handler::_M_init_functor(_M_functor, std::move(__f));
1907 _M_invoker = &_My_handler::_M_invoke;
1908 _M_manager = &_My_handler::_M_manager;
1909 }
1910 }
1911
1912 template<typename _Res, typename... _ArgTypes>
1913 _Res
1914 function<_Res(_ArgTypes...)>::
1915 operator()(_ArgTypes... __args) const
1916 {
1917 if (_M_empty())
1918 __throw_bad_function_call();
1919 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
1920 }
1921
1922 #if __cpp_rtti
1923 template<typename _Res, typename... _ArgTypes>
1924 const type_info&
1925 function<_Res(_ArgTypes...)>::
1926 target_type() const noexcept
1927 {
1928 if (_M_manager)
1929 {
1930 _Any_data __typeinfo_result;
1931 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
1932 return *__typeinfo_result._M_access<const type_info*>();
1933 }
1934 else
1935 return typeid(void);
1936 }
1937
1938 template<typename _Res, typename... _ArgTypes>
1939 template<typename _Functor>
1940 _Functor*
1941 function<_Res(_ArgTypes...)>::
1942 target() noexcept
1943 {
1944 if (typeid(_Functor) == target_type() && _M_manager)
1945 {
1946 _Any_data __ptr;
1947 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
1948 && !is_const<_Functor>::value)
1949 return 0;
1950 else
1951 return __ptr._M_access<_Functor*>();
1952 }
1953 else
1954 return 0;
1955 }
1956
1957 template<typename _Res, typename... _ArgTypes>
1958 template<typename _Functor>
1959 const _Functor*
1960 function<_Res(_ArgTypes...)>::
1961 target() const noexcept
1962 {
1963 if (typeid(_Functor) == target_type() && _M_manager)
1964 {
1965 _Any_data __ptr;
1966 _M_manager(__ptr, _M_functor, __get_functor_ptr);
1967 return __ptr._M_access<const _Functor*>();
1968 }
1969 else
1970 return 0;
1971 }
1972 #endif
1973
1974 // [20.7.15.2.6] null pointer comparisons
1975
1976 /**
1977 * @brief Compares a polymorphic function object wrapper against 0
1978 * (the NULL pointer).
1979 * @returns @c true if the wrapper has no target, @c false otherwise
1980 *
1981 * This function will not throw an %exception.
1982 */
1983 template<typename _Res, typename... _Args>
1984 inline bool
1985 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
1986 { return !static_cast<bool>(__f); }
1987
1988 /// @overload
1989 template<typename _Res, typename... _Args>
1990 inline bool
1991 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
1992 { return !static_cast<bool>(__f); }
1993
1994 /**
1995 * @brief Compares a polymorphic function object wrapper against 0
1996 * (the NULL pointer).
1997 * @returns @c false if the wrapper has no target, @c true otherwise
1998 *
1999 * This function will not throw an %exception.
2000 */
2001 template<typename _Res, typename... _Args>
2002 inline bool
2003 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2004 { return static_cast<bool>(__f); }
2005
2006 /// @overload
2007 template<typename _Res, typename... _Args>
2008 inline bool
2009 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2010 { return static_cast<bool>(__f); }
2011
2012 // [20.7.15.2.7] specialized algorithms
2013
2014 /**
2015 * @brief Swap the targets of two polymorphic function object wrappers.
2016 *
2017 * This function will not throw an %exception.
2018 */
2019 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2020 // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps
2021 template<typename _Res, typename... _Args>
2022 inline void
2023 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept
2024 { __x.swap(__y); }
2025
2026 #if __cplusplus >= 201402L
2027 /// Generalized negator.
2028 template<typename _Fn>
2029 class _Not_fn
2030 {
2031 template<typename _Tp>
2032 using __is_nothrow_negatable
2033 = __bool_constant<noexcept(!std::declval<_Tp>())>;
2034
2035 template<typename _Fn2, typename... _Args>
2036 using __noexcept_cond = __and_<
2037 __is_nothrow_callable<_Fn2(_Args&&...)>,
2038 __is_nothrow_negatable<result_of_t<_Fn2(_Args&&...)>>
2039 >;
2040
2041 public:
2042 template<typename _Fn2>
2043 _Not_fn(_Fn2&& __fn, int)
2044 : _M_fn(std::forward<_Fn2>(__fn)) { }
2045
2046 _Not_fn(const _Not_fn& __fn) = default;
2047 _Not_fn(_Not_fn&& __fn) = default;
2048 ~_Not_fn() = default;
2049
2050 template<typename... _Args>
2051 auto
2052 operator()(_Args&&... __args) &
2053 noexcept(__noexcept_cond<_Fn&, _Args&&...>::value)
2054 -> decltype(!std::declval<result_of_t<_Fn&(_Args&&...)>>())
2055 { return !std::__invoke(_M_fn, std::forward<_Args>(__args)...); }
2056
2057 template<typename... _Args>
2058 auto
2059 operator()(_Args&&... __args) const &
2060 noexcept(__noexcept_cond<const _Fn&, _Args&&...>::value)
2061 -> decltype(!std::declval<result_of_t<const _Fn&(_Args&&...)>>())
2062 { return !std::__invoke(_M_fn, std::forward<_Args>(__args)...); }
2063
2064 template<typename... _Args>
2065 auto
2066 operator()(_Args&&... __args) &&
2067 noexcept(__noexcept_cond<_Fn&&, _Args&&...>::value)
2068 -> decltype(!std::declval<result_of_t<_Fn&&(_Args&&...)>>())
2069 {
2070 return !std::__invoke(std::move(_M_fn),
2071 std::forward<_Args>(__args)...);
2072 }
2073
2074 template<typename... _Args>
2075 auto
2076 operator()(_Args&&... __args) const &&
2077 noexcept(__noexcept_cond<const _Fn&&, _Args&&...>::value)
2078 -> decltype(!std::declval<result_of_t<const _Fn&&(_Args&&...)>>())
2079 {
2080 return !std::__invoke(std::move(_M_fn),
2081 std::forward<_Args>(__args)...);
2082 }
2083
2084 private:
2085 _Fn _M_fn;
2086 };
2087 #endif // C++14
2088
2089 #if __cplusplus > 201402L
2090
2091 #define __cpp_lib_not_fn 201603
2092
2093 /// [func.not_fn] Function template not_fn
2094 template<typename _Fn>
2095 inline auto
2096 not_fn(_Fn&& __fn)
2097 noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
2098 {
2099 return _Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
2100 }
2101
2102 // Searchers
2103 #define __cpp_lib_boyer_moore_searcher 201603
2104
2105 template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
2106 class default_searcher
2107 {
2108 public:
2109 default_searcher(_ForwardIterator1 __pat_first,
2110 _ForwardIterator1 __pat_last,
2111 _BinaryPredicate __pred = _BinaryPredicate())
2112 : _M_m(__pat_first, __pat_last, std::move(__pred))
2113 { }
2114
2115 template<typename _ForwardIterator2>
2116 pair<_ForwardIterator2, _ForwardIterator2>
2117 operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
2118 {
2119 _ForwardIterator2 __first_ret =
2120 std::search(__first, __last,
2121 std::get<0>(_M_m), std::get<1>(_M_m),
2122 std::get<2>(_M_m));
2123 _ForwardIterator2 __second_ret = __first_ret == __last ?
2124 __last : std::next(__first_ret, std::distance(std::get<0>(_M_m),
2125 std::get<1>(_M_m)));
2126 return std::make_pair(__first_ret, __second_ret);
2127 }
2128
2129 private:
2130 std::tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
2131 };
2132
2133 template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
2134 struct __boyer_moore_map_base
2135 {
2136 template<typename _RAIter>
2137 __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
2138 _Hash&& __hf, _Pred&& __pred)
2139 : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
2140 {
2141 if (__patlen > 0)
2142 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
2143 _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
2144 }
2145
2146 using __diff_type = _Tp;
2147
2148 __diff_type
2149 _M_lookup(_Key __key, __diff_type __not_found) const
2150 {
2151 auto __iter = _M_bad_char.find(__key);
2152 if (__iter == _M_bad_char.end())
2153 return __not_found;
2154 return __iter->second;
2155 }
2156
2157 _Pred
2158 _M_pred() const { return _M_bad_char.key_eq(); }
2159
2160 _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
2161 };
2162
2163 template<typename _Tp, size_t _Len, typename _Pred>
2164 struct __boyer_moore_array_base
2165 {
2166 template<typename _RAIter, typename _Unused>
2167 __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
2168 _Unused&&, _Pred&& __pred)
2169 : _M_bad_char{ _GLIBCXX_STD_C::array<_Tp, _Len>{}, std::move(__pred) }
2170 {
2171 std::get<0>(_M_bad_char).fill(__patlen);
2172 if (__patlen > 0)
2173 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
2174 {
2175 auto __ch = __pat[__i];
2176 using _UCh = std::make_unsigned_t<decltype(__ch)>;
2177 auto __uch = static_cast<_UCh>(__ch);
2178 std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
2179 }
2180 }
2181
2182 using __diff_type = _Tp;
2183
2184 template<typename _Key>
2185 __diff_type
2186 _M_lookup(_Key __key, __diff_type __not_found) const
2187 {
2188 auto __ukey = static_cast<std::make_unsigned_t<_Key>>(__key);
2189 if (__ukey >= _Len)
2190 return __not_found;
2191 return std::get<0>(_M_bad_char)[__ukey];
2192 }
2193
2194 const _Pred&
2195 _M_pred() const { return std::get<1>(_M_bad_char); }
2196
2197 std::tuple<_GLIBCXX_STD_C::array<_Tp, _Len>, _Pred> _M_bad_char;
2198 };
2199
2200 template<typename _Pred>
2201 struct __is_std_equal_to : std::false_type { };
2202
2203 template<>
2204 struct __is_std_equal_to<std::equal_to<void>> : std::true_type { };
2205
2206 // Use __boyer_moore_array_base when pattern consists of narrow characters
2207 // and uses std::equal_to as the predicate.
2208 template<typename _RAIter, typename _Hash, typename _Pred,
2209 typename _Val = typename iterator_traits<_RAIter>::value_type,
2210 typename _Diff = typename iterator_traits<_RAIter>::difference_type>
2211 using __boyer_moore_base_t
2212 = std::conditional_t<sizeof(_Val) == 1 && is_integral<_Val>::value
2213 && __is_std_equal_to<_Pred>::value,
2214 __boyer_moore_array_base<_Diff, 256, _Pred>,
2215 __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
2216
2217 template<typename _RAIter, typename _Hash
2218 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
2219 typename _BinaryPredicate = std::equal_to<>>
2220 class boyer_moore_searcher
2221 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
2222 {
2223 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
2224 using typename _Base::__diff_type;
2225
2226 public:
2227 boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
2228 _Hash __hf = _Hash(),
2229 _BinaryPredicate __pred = _BinaryPredicate());
2230
2231 template<typename _RandomAccessIterator2>
2232 pair<_RandomAccessIterator2, _RandomAccessIterator2>
2233 operator()(_RandomAccessIterator2 __first,
2234 _RandomAccessIterator2 __last) const;
2235
2236 private:
2237 bool
2238 _M_is_prefix(_RAIter __word, __diff_type __len,
2239 __diff_type __pos)
2240 {
2241 const auto& __pred = this->_M_pred();
2242 __diff_type __suffixlen = __len - __pos;
2243 for (__diff_type __i = 0; __i < __suffixlen; ++__i)
2244 if (!__pred(__word[__i], __word[__pos + __i]))
2245 return false;
2246 return true;
2247 }
2248
2249 __diff_type
2250 _M_suffix_length(_RAIter __word, __diff_type __len,
2251 __diff_type __pos)
2252 {
2253 const auto& __pred = this->_M_pred();
2254 __diff_type __i = 0;
2255 while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
2256 && __i < __pos)
2257 {
2258 ++__i;
2259 }
2260 return __i;
2261 }
2262
2263 template<typename _Tp>
2264 __diff_type
2265 _M_bad_char_shift(_Tp __c) const
2266 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
2267
2268 _RAIter _M_pat;
2269 _RAIter _M_pat_end;
2270 _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
2271 };
2272
2273 template<typename _RAIter, typename _Hash
2274 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
2275 typename _BinaryPredicate = std::equal_to<>>
2276 class boyer_moore_horspool_searcher
2277 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
2278 {
2279 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
2280 using typename _Base::__diff_type;
2281
2282 public:
2283 boyer_moore_horspool_searcher(_RAIter __pat,
2284 _RAIter __pat_end,
2285 _Hash __hf = _Hash(),
2286 _BinaryPredicate __pred
2287 = _BinaryPredicate())
2288 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
2289 _M_pat(__pat), _M_pat_end(__pat_end)
2290 { }
2291
2292 template<typename _RandomAccessIterator2>
2293 pair<_RandomAccessIterator2, _RandomAccessIterator2>
2294 operator()(_RandomAccessIterator2 __first,
2295 _RandomAccessIterator2 __last) const
2296 {
2297 const auto& __pred = this->_M_pred();
2298 auto __patlen = _M_pat_end - _M_pat;
2299 if (__patlen == 0)
2300 return std::make_pair(__first, __first);
2301 auto __len = __last - __first;
2302 while (__len >= __patlen)
2303 {
2304 for (auto __scan = __patlen - 1;
2305 __pred(__first[__scan], _M_pat[__scan]); --__scan)
2306 if (__scan == 0)
2307 return std::make_pair(__first,
2308 std::next(__first, __patlen));
2309 auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
2310 __len -= __shift;
2311 __first += __shift;
2312 }
2313 return std::make_pair(__last, __last);
2314 }
2315
2316 private:
2317 template<typename _Tp>
2318 __diff_type
2319 _M_bad_char_shift(_Tp __c) const
2320 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
2321
2322 _RAIter _M_pat;
2323 _RAIter _M_pat_end;
2324 };
2325
2326 /// Generator function for default_searcher
2327 template<typename _ForwardIterator,
2328 typename _BinaryPredicate = std::equal_to<>>
2329 inline default_searcher<_ForwardIterator, _BinaryPredicate>
2330 make_default_searcher(_ForwardIterator __pat_first,
2331 _ForwardIterator __pat_last,
2332 _BinaryPredicate __pred = _BinaryPredicate())
2333 { return { __pat_first, __pat_last, __pred }; }
2334
2335 /// Generator function for boyer_moore_searcher
2336 template<typename _RAIter, typename _Hash
2337 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
2338 typename _BinaryPredicate = equal_to<>>
2339 inline boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>
2340 make_boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
2341 _Hash __hf = _Hash(),
2342 _BinaryPredicate __pred = _BinaryPredicate())
2343 { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
2344
2345 /// Generator function for boyer_moore_horspool_searcher
2346 template<typename _RAIter, typename _Hash
2347 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
2348 typename _BinaryPredicate = equal_to<>>
2349 inline boyer_moore_horspool_searcher<_RAIter, _Hash, _BinaryPredicate>
2350 make_boyer_moore_horspool_searcher(_RAIter __pat_first, _RAIter __pat_last,
2351 _Hash __hf = _Hash(),
2352 _BinaryPredicate __pred
2353 = _BinaryPredicate())
2354 { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
2355
2356 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
2357 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
2358 boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
2359 _Hash __hf, _BinaryPredicate __pred)
2360 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
2361 _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
2362 {
2363 auto __patlen = __pat_end - __pat;
2364 if (__patlen == 0)
2365 return;
2366 __diff_type __last_prefix = __patlen - 1;
2367 for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
2368 {
2369 if (_M_is_prefix(__pat, __patlen, __p + 1))
2370 __last_prefix = __p + 1;
2371 _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
2372 }
2373 for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
2374 {
2375 auto __slen = _M_suffix_length(__pat, __patlen, __p);
2376 auto __pos = __patlen - 1 - __slen;
2377 if (!__pred(__pat[__p - __slen], __pat[__pos]))
2378 _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
2379 }
2380 }
2381
2382 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
2383 template<typename _RandomAccessIterator2>
2384 pair<_RandomAccessIterator2, _RandomAccessIterator2>
2385 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
2386 operator()(_RandomAccessIterator2 __first,
2387 _RandomAccessIterator2 __last) const
2388 {
2389 auto __patlen = _M_pat_end - _M_pat;
2390 if (__patlen == 0)
2391 return std::make_pair(__first, __first);
2392 const auto& __pred = this->_M_pred();
2393 __diff_type __i = __patlen - 1;
2394 auto __stringlen = __last - __first;
2395 while (__i < __stringlen)
2396 {
2397 __diff_type __j = __patlen - 1;
2398 while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
2399 {
2400 --__i;
2401 --__j;
2402 }
2403 if (__j < 0)
2404 return std::make_pair(__first + __i + 1, std::next(__first,
2405 __patlen));
2406 __i += std::max(_M_bad_char_shift(__first[__i]),
2407 _M_good_suffix[__j]);
2408 }
2409 return std::make_pair(__last, __last);
2410 }
2411
2412 #endif // C++17
2413
2414 _GLIBCXX_END_NAMESPACE_VERSION
2415 } // namespace std
2416
2417 #endif // C++11
2418
2419 #endif // _GLIBCXX_FUNCTIONAL