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