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