1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001-2016 Free Software Foundation, Inc.
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)
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.
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.
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/>.
27 * Silicon Graphics Computer Systems, Inc.
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.
39 /** @file include/functional
40 * This is a Standard C++ Library header.
43 #ifndef _GLIBCXX_FUNCTIONAL
44 #define _GLIBCXX_FUNCTIONAL 1
46 #pragma GCC system_header
48 #include <bits/c++config.h>
49 #include <bits/stl_function.h>
51 #if __cplusplus >= 201103L
56 #include <type_traits>
57 #include <bits/functexcept.h>
58 #include <bits/functional_hash.h>
60 namespace std _GLIBCXX_VISIBILITY(default)
62 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 template<typename _MemberPointer>
66 template<typename _Tp, typename _Class>
67 _Mem_fn<_Tp _Class::*>
68 mem_fn(_Tp _Class::*) noexcept;
70 /// If we have found a result_type, extract it.
71 template<typename _Functor, typename = __void_t<>>
72 struct _Maybe_get_result_type
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; };
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.
84 template<typename _Functor>
85 struct _Weak_result_type_impl
86 : _Maybe_get_result_type<_Functor>
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; };
94 template<typename _Res, typename... _ArgTypes>
95 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
96 { typedef _Res result_type; };
98 template<typename _Res, typename... _ArgTypes>
99 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
100 { typedef _Res result_type; };
102 template<typename _Res, typename... _ArgTypes>
103 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
104 { typedef _Res result_type; };
106 template<typename _Res, typename... _ArgTypes>
107 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
108 { typedef _Res result_type; };
110 template<typename _Res, typename... _ArgTypes>
111 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
112 { typedef _Res result_type; };
114 template<typename _Res, typename... _ArgTypes>
115 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
116 { typedef _Res result_type; };
118 template<typename _Res, typename... _ArgTypes>
119 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
120 { typedef _Res result_type; };
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; };
127 template<typename _Res, typename... _ArgTypes>
128 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
129 { typedef _Res result_type; };
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; };
136 template<typename _Res, typename... _ArgTypes>
137 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
138 { typedef _Res result_type; };
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; };
145 template<typename _Res, typename _Class, typename... _ArgTypes>
146 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
147 { typedef _Res result_type; };
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; };
154 template<typename _Res, typename _Class, typename... _ArgTypes>
155 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
156 { typedef _Res result_type; };
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; };
163 template<typename _Res, typename _Class, typename... _ArgTypes>
164 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
165 { typedef _Res result_type; };
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...)
171 { typedef _Res result_type; };
173 template<typename _Res, typename _Class, typename... _ArgTypes>
174 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
176 { typedef _Res result_type; };
179 * Strip top-level cv-qualifiers from the function object and let
180 * _Weak_result_type_impl perform the real work.
182 template<typename _Functor>
183 struct _Weak_result_type
184 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
187 template<typename _Tp, typename _Up = typename decay<_Tp>::type>
192 // Equivalent to std::forward<_Tp>
193 static constexpr _Tp&&
194 _S_fwd(_Tp& __t) noexcept { return static_cast<_Tp&&>(__t); }
197 template<typename _Tp, typename _Up>
198 struct _Unwrap<_Tp, reference_wrapper<_Up>>
202 // Get an lvalue-reference from a reference_wrapper.
204 _S_fwd(const _Tp& __t) noexcept { __t.get(); }
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); }
214 template<typename _Res, typename _Fn, typename... _Args>
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)...); }
220 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
222 __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
225 (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...)))
226 { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
228 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
230 __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
233 ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...)))
235 return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
238 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
240 __invoke_impl(__invoke_memobj_ref, _MemFun&& __f, _Tp&& __t)
241 noexcept(noexcept(__invfwd<_Tp>(__t).*__f))
242 { return __invfwd<_Tp>(__t).*__f; }
244 template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
246 __invoke_impl(__invoke_memobj_deref, _MemFun&& __f, _Tp&& __t,
248 noexcept(noexcept((*std::forward<_Tp>(__t)).*__f))
249 { return (*std::forward<_Tp>(__t)).*__f; }
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)
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)...);
263 #if __cplusplus > 201402L
264 # define __cpp_lib_invoke 201411
266 /// Invoke a callable object.
267 template<typename _Callable, typename... _Args>
268 inline result_of_t<_Callable&&(_Args&&...)>
269 invoke(_Callable&& __fn, _Args&&... __args)
271 return std::__invoke(std::forward<_Callable>(__fn),
272 std::forward<_Args>(__args)...);
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.
281 template<bool _Unary, bool _Binary, typename _Tp>
282 struct _Reference_wrapper_base_impl;
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>
290 // Nested argument_type only.
291 template<typename _Tp>
292 struct _Reference_wrapper_base_impl<true, false, _Tp>
293 : _Weak_result_type<_Tp>
295 typedef typename _Tp::argument_type argument_type;
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>
303 typedef typename _Tp::first_argument_type first_argument_type;
304 typedef typename _Tp::second_argument_type second_argument_type;
307 // All the nested argument types.
308 template<typename _Tp>
309 struct _Reference_wrapper_base_impl<true, true, _Tp>
310 : _Weak_result_type<_Tp>
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;
317 _GLIBCXX_HAS_NESTED_TYPE(argument_type)
318 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
319 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
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.
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,
336 // - a function type (unary)
337 template<typename _Res, typename _T1>
338 struct _Reference_wrapper_base<_Res(_T1)>
339 : unary_function<_T1, _Res>
342 template<typename _Res, typename _T1>
343 struct _Reference_wrapper_base<_Res(_T1) const>
344 : unary_function<_T1, _Res>
347 template<typename _Res, typename _T1>
348 struct _Reference_wrapper_base<_Res(_T1) volatile>
349 : unary_function<_T1, _Res>
352 template<typename _Res, typename _T1>
353 struct _Reference_wrapper_base<_Res(_T1) const volatile>
354 : unary_function<_T1, _Res>
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>
363 template<typename _Res, typename _T1, typename _T2>
364 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
365 : binary_function<_T1, _T2, _Res>
368 template<typename _Res, typename _T1, typename _T2>
369 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
370 : binary_function<_T1, _T2, _Res>
373 template<typename _Res, typename _T1, typename _T2>
374 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
375 : binary_function<_T1, _T2, _Res>
378 // - a function pointer type (unary)
379 template<typename _Res, typename _T1>
380 struct _Reference_wrapper_base<_Res(*)(_T1)>
381 : unary_function<_T1, _Res>
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>
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>
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>
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>
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>
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>
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>
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>
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>
439 * @brief Primary class template for reference_wrapper.
443 template<typename _Tp>
444 class reference_wrapper
445 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
452 reference_wrapper(_Tp& __indata) noexcept
453 : _M_data(std::__addressof(__indata))
456 reference_wrapper(_Tp&&) = delete;
458 reference_wrapper(const reference_wrapper&) = default;
461 operator=(const reference_wrapper&) = default;
463 operator _Tp&() const noexcept
464 { return this->get(); }
470 template<typename... _Args>
471 typename result_of<_Tp&(_Args&&...)>::type
472 operator()(_Args&&... __args) const
474 return std::__invoke(get(), std::forward<_Args>(__args)...);
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); }
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); }
491 template<typename _Tp>
492 void ref(const _Tp&&) = delete;
494 template<typename _Tp>
495 void cref(const _Tp&&) = delete;
497 /// Partial specialization.
498 template<typename _Tp>
499 inline reference_wrapper<_Tp>
500 ref(reference_wrapper<_Tp> __t) noexcept
501 { return ref(__t.get()); }
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()); }
511 template<typename... _Types>
512 struct _Pack : integral_constant<size_t, sizeof...(_Types)>
515 template<typename _From, typename _To, bool = _From::value == _To::value>
516 struct _AllConvertible : false_type
519 template<typename... _From, typename... _To>
520 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
521 : __and_<is_convertible<_From, _To>...>
524 template<typename _Tp1, typename _Tp2>
525 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
526 typename std::decay<_Tp2>::type>>;
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.
533 template<typename _Res, typename... _ArgTypes>
534 struct _Maybe_unary_or_binary_function { };
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> { };
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> { };
546 template<typename _Signature>
547 struct _Mem_fn_traits;
549 template<typename _Res, typename _Class, typename... _ArgTypes>
550 struct _Mem_fn_traits_base
552 using __result_type = _Res;
554 = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
555 using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
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...> \
563 using __vararg = false_type; \
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...> \
569 using __vararg = true_type; \
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)
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)
582 #undef _GLIBCXX_MEM_FN_TRAITS
583 #undef _GLIBCXX_MEM_FN_TRAITS2
585 template<typename _MemFunPtr,
586 bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
588 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
590 using _Traits = _Mem_fn_traits<_MemFunPtr>;
592 using _Arity = typename _Traits::__arity;
593 using _Varargs = typename _Traits::__vararg;
595 template<typename _Func, typename... _BoundArgs>
596 friend struct _Bind_check_arity;
602 using result_type = typename _Traits::__result_type;
605 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
607 template<typename... _Args>
609 operator()(_Args&&... __args) const
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)...); }
616 // Partial specialization for member object pointers.
617 template<typename _MemObjPtr>
618 class _Mem_fn_base<_MemObjPtr, false>
620 using _Arity = integral_constant<size_t, 0>;
621 using _Varargs = false_type;
623 template<typename _Func, typename... _BoundArgs>
624 friend struct _Bind_check_arity;
630 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
632 template<typename _Tp>
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)); }
640 template<typename _Res, typename _Class>
641 struct _Mem_fn<_Res _Class::*>
642 : _Mem_fn_base<_Res _Class::*>
644 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
647 // _GLIBCXX_RESOLVE_LIB_DEFECTS
648 // 2048. Unnecessary mem_fn overloads
650 * @brief Returns a function object that forwards to the member
654 template<typename _Tp, typename _Class>
655 inline _Mem_fn<_Tp _Class::*>
656 mem_fn(_Tp _Class::* __pm) noexcept
658 return _Mem_fn<_Tp _Class::*>(__pm);
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().
666 * C++11 [func.bind.isbind].
669 template<typename _Tp>
670 struct is_bind_expression
671 : public false_type { };
674 * @brief Determines if the given type _Tp is a placeholder in a
675 * bind() expression and, if so, which placeholder it is.
677 * C++11 [func.bind.isplace].
680 template<typename _Tp>
681 struct is_placeholder
682 : public integral_constant<int, 0>
685 /** @brief The type of placeholder objects defined by libstdc++.
688 template<int _Num> struct _Placeholder { };
690 _GLIBCXX_END_NAMESPACE_VERSION
692 /** @namespace std::placeholders
693 * @brief ISO C++11 entities sub-namespace for functional.
696 namespace placeholders
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.
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
735 _GLIBCXX_BEGIN_NAMESPACE_VERSION
738 * Partial specialization of is_placeholder that provides the placeholder
739 * number for the placeholder objects defined by libstdc++.
743 struct is_placeholder<_Placeholder<_Num> >
744 : public integral_constant<int, _Num>
748 struct is_placeholder<const _Placeholder<_Num> >
749 : public integral_constant<int, _Num>
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;
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.
770 template<typename _Arg,
771 bool _IsBindExp = is_bind_expression<_Arg>::value,
772 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
776 * If the argument is reference_wrapper<_Tp>, returns the
777 * underlying reference.
778 * C++11 [func.bind.bind] p10 bullet 1.
780 template<typename _Tp>
781 class _Mu<reference_wrapper<_Tp>, false, false>
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.
788 template<typename _CVRef, typename _Tuple>
790 operator()(_CVRef& __arg, _Tuple&) const volatile
791 { return __arg.get(); }
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.
800 template<typename _Arg>
801 class _Mu<_Arg, true, false>
804 template<typename _CVArg, typename... _Args>
806 operator()(_CVArg& __arg,
807 tuple<_Args...>& __tuple) const volatile
808 -> decltype(__arg(declval<_Args>()...))
810 // Construct an index tuple and forward to __call
811 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
813 return this->__call(__arg, __tuple, _Indexes());
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>
821 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
822 const _Index_tuple<_Indexes...>&) const volatile
823 -> decltype(__arg(declval<_Args>()...))
825 return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...);
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.
834 template<typename _Arg>
835 class _Mu<_Arg, false, true>
838 template<typename _Tuple>
839 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
840 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
843 = __tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>;
844 return std::forward<__type>(
845 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
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.
854 template<typename _Arg>
855 class _Mu<_Arg, false, false>
858 template<typename _CVArg, typename _Tuple>
860 operator()(_CVArg&& __arg, _Tuple&) const volatile
861 { return std::forward<_CVArg>(__arg); }
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.
869 template<typename _Tp>
870 struct _Maybe_wrap_member_pointer
874 static constexpr const _Tp&
875 __do_wrap(const _Tp& __x)
878 static constexpr _Tp&&
880 { return static_cast<_Tp&&>(__x); }
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.
888 template<typename _Tp, typename _Class>
889 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
891 typedef _Mem_fn<_Tp _Class::*> type;
893 static constexpr type
894 __do_wrap(_Tp _Class::* __pm)
895 { return type(__pm); }
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
903 struct _Maybe_wrap_member_pointer<void>
908 // std::get<I> for volatile-qualified tuples
909 template<std::size_t _Ind, typename... _Tp>
911 __volget(volatile tuple<_Tp...>& __tuple)
912 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
913 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
915 // std::get<I> for const-volatile-qualified tuples
916 template<std::size_t _Ind, typename... _Tp>
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)); }
922 /// Type of the function object returned from bind().
923 template<typename _Signature>
926 template<typename _Functor, typename... _Bound_args>
927 class _Bind<_Functor(_Bound_args...)>
928 : public _Weak_result_type<_Functor>
930 typedef _Bind __self_type;
931 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
935 tuple<_Bound_args...> _M_bound_args;
938 template<typename _Result, typename... _Args, std::size_t... _Indexes>
940 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
942 return _M_f(_Mu<_Bound_args>()
943 (std::get<_Indexes>(_M_bound_args), __args)...);
947 template<typename _Result, typename... _Args, std::size_t... _Indexes>
949 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
951 return _M_f(_Mu<_Bound_args>()
952 (std::get<_Indexes>(_M_bound_args), __args)...);
956 template<typename _Result, typename... _Args, std::size_t... _Indexes>
958 __call_v(tuple<_Args...>&& __args,
959 _Index_tuple<_Indexes...>) volatile
961 return _M_f(_Mu<_Bound_args>()
962 (__volget<_Indexes>(_M_bound_args), __args)...);
965 // Call as const volatile
966 template<typename _Result, typename... _Args, std::size_t... _Indexes>
968 __call_c_v(tuple<_Args...>&& __args,
969 _Index_tuple<_Indexes...>) const volatile
971 return _M_f(_Mu<_Bound_args>()
972 (__volget<_Indexes>(_M_bound_args), __args)...);
976 template<typename... _Args>
977 explicit _Bind(const _Functor& __f, _Args&&... __args)
978 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
981 template<typename... _Args>
982 explicit _Bind(_Functor&& __f, _Args&&... __args)
983 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
986 _Bind(const _Bind&) = default;
989 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
993 template<typename... _Args, typename _Result
994 = decltype( std::declval<_Functor&>()(
995 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
996 std::declval<tuple<_Args...>&>() )... ) )>
998 operator()(_Args&&... __args)
1000 return this->__call<_Result>(
1001 std::forward_as_tuple(std::forward<_Args>(__args)...),
1006 template<typename... _Args, typename _Result
1007 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1008 typename add_const<_Functor>::type&>::type>()(
1009 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1010 std::declval<tuple<_Args...>&>() )... ) )>
1012 operator()(_Args&&... __args) const
1014 return this->__call_c<_Result>(
1015 std::forward_as_tuple(std::forward<_Args>(__args)...),
1020 template<typename... _Args, typename _Result
1021 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1022 typename add_volatile<_Functor>::type&>::type>()(
1023 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1024 std::declval<tuple<_Args...>&>() )... ) )>
1026 operator()(_Args&&... __args) volatile
1028 return this->__call_v<_Result>(
1029 std::forward_as_tuple(std::forward<_Args>(__args)...),
1033 // Call as const volatile
1034 template<typename... _Args, typename _Result
1035 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1036 typename add_cv<_Functor>::type&>::type>()(
1037 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1038 std::declval<tuple<_Args...>&>() )... ) )>
1040 operator()(_Args&&... __args) const volatile
1042 return this->__call_c_v<_Result>(
1043 std::forward_as_tuple(std::forward<_Args>(__args)...),
1048 /// Type of the function object returned from bind<R>().
1049 template<typename _Result, typename _Signature>
1050 struct _Bind_result;
1052 template<typename _Result, typename _Functor, typename... _Bound_args>
1053 class _Bind_result<_Result, _Functor(_Bound_args...)>
1055 typedef _Bind_result __self_type;
1056 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1060 tuple<_Bound_args...> _M_bound_args;
1063 template<typename _Res>
1064 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1065 template<typename _Res>
1066 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1069 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1071 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1072 typename __disable_if_void<_Res>::type = 0)
1074 return _M_f(_Mu<_Bound_args>()
1075 (std::get<_Indexes>(_M_bound_args), __args)...);
1078 // Call unqualified, return void
1079 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1081 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1082 typename __enable_if_void<_Res>::type = 0)
1084 _M_f(_Mu<_Bound_args>()
1085 (std::get<_Indexes>(_M_bound_args), __args)...);
1089 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1091 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1092 typename __disable_if_void<_Res>::type = 0) const
1094 return _M_f(_Mu<_Bound_args>()
1095 (std::get<_Indexes>(_M_bound_args), __args)...);
1098 // Call as const, return void
1099 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1101 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1102 typename __enable_if_void<_Res>::type = 0) const
1104 _M_f(_Mu<_Bound_args>()
1105 (std::get<_Indexes>(_M_bound_args), __args)...);
1109 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1111 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1112 typename __disable_if_void<_Res>::type = 0) volatile
1114 return _M_f(_Mu<_Bound_args>()
1115 (__volget<_Indexes>(_M_bound_args), __args)...);
1118 // Call as volatile, return void
1119 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1121 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1122 typename __enable_if_void<_Res>::type = 0) volatile
1124 _M_f(_Mu<_Bound_args>()
1125 (__volget<_Indexes>(_M_bound_args), __args)...);
1128 // Call as const volatile
1129 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1131 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1132 typename __disable_if_void<_Res>::type = 0) const volatile
1134 return _M_f(_Mu<_Bound_args>()
1135 (__volget<_Indexes>(_M_bound_args), __args)...);
1138 // Call as const volatile, return void
1139 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1141 __call(tuple<_Args...>&& __args,
1142 _Index_tuple<_Indexes...>,
1143 typename __enable_if_void<_Res>::type = 0) const volatile
1145 _M_f(_Mu<_Bound_args>()
1146 (__volget<_Indexes>(_M_bound_args), __args)...);
1150 typedef _Result result_type;
1152 template<typename... _Args>
1153 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1154 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1157 template<typename... _Args>
1158 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1159 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1162 _Bind_result(const _Bind_result&) = default;
1164 _Bind_result(_Bind_result&& __b)
1165 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1169 template<typename... _Args>
1171 operator()(_Args&&... __args)
1173 return this->__call<_Result>(
1174 std::forward_as_tuple(std::forward<_Args>(__args)...),
1179 template<typename... _Args>
1181 operator()(_Args&&... __args) const
1183 return this->__call<_Result>(
1184 std::forward_as_tuple(std::forward<_Args>(__args)...),
1189 template<typename... _Args>
1191 operator()(_Args&&... __args) volatile
1193 return this->__call<_Result>(
1194 std::forward_as_tuple(std::forward<_Args>(__args)...),
1198 // Call as const volatile
1199 template<typename... _Args>
1201 operator()(_Args&&... __args) const volatile
1203 return this->__call<_Result>(
1204 std::forward_as_tuple(std::forward<_Args>(__args)...),
1210 * @brief Class template _Bind is always a bind expression.
1213 template<typename _Signature>
1214 struct is_bind_expression<_Bind<_Signature> >
1215 : public true_type { };
1218 * @brief Class template _Bind is always a bind expression.
1221 template<typename _Signature>
1222 struct is_bind_expression<const _Bind<_Signature> >
1223 : public true_type { };
1226 * @brief Class template _Bind is always a bind expression.
1229 template<typename _Signature>
1230 struct is_bind_expression<volatile _Bind<_Signature> >
1231 : public true_type { };
1234 * @brief Class template _Bind is always a bind expression.
1237 template<typename _Signature>
1238 struct is_bind_expression<const volatile _Bind<_Signature>>
1239 : public true_type { };
1242 * @brief Class template _Bind_result is always a bind expression.
1245 template<typename _Result, typename _Signature>
1246 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1247 : public true_type { };
1250 * @brief Class template _Bind_result is always a bind expression.
1253 template<typename _Result, typename _Signature>
1254 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1255 : public true_type { };
1258 * @brief Class template _Bind_result is always a bind expression.
1261 template<typename _Result, typename _Signature>
1262 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1263 : public true_type { };
1266 * @brief Class template _Bind_result is always a bind expression.
1269 template<typename _Result, typename _Signature>
1270 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1271 : public true_type { };
1273 template<typename _Func, typename... _BoundArgs>
1274 struct _Bind_check_arity { };
1276 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1277 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
1279 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
1280 "Wrong number of arguments for function");
1283 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1284 struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
1286 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
1287 "Wrong number of arguments for function");
1290 template<typename _Tp, typename _Class, typename... _BoundArgs>
1291 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
1293 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
1294 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
1295 static_assert(_Varargs::value
1296 ? sizeof...(_BoundArgs) >= _Arity::value + 1
1297 : sizeof...(_BoundArgs) == _Arity::value + 1,
1298 "Wrong number of arguments for pointer-to-member");
1301 // Trait type used to remove std::bind() from overload set via SFINAE
1302 // when first argument has integer type, so that std::bind() will
1303 // not be a better match than ::bind() from the BSD Sockets API.
1304 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
1305 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1307 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1309 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1311 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1313 typedef typename __maybe_type::type __func_type;
1314 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1317 // Partial specialization for is_socketlike == true, does not define
1318 // nested type so std::bind() will not participate in overload resolution
1319 // when the first argument might be a socket file descriptor.
1320 template<typename _Func, typename... _BoundArgs>
1321 struct _Bind_helper<true, _Func, _BoundArgs...>
1325 * @brief Function template for std::bind.
1328 template<typename _Func, typename... _BoundArgs>
1330 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1331 bind(_Func&& __f, _BoundArgs&&... __args)
1333 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1334 typedef typename __helper_type::__maybe_type __maybe_type;
1335 typedef typename __helper_type::type __result_type;
1336 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1337 std::forward<_BoundArgs>(__args)...);
1340 template<typename _Result, typename _Func, typename... _BoundArgs>
1341 struct _Bindres_helper
1342 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1344 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1346 typedef typename __maybe_type::type __functor_type;
1347 typedef _Bind_result<_Result,
1348 __functor_type(typename decay<_BoundArgs>::type...)>
1353 * @brief Function template for std::bind<R>.
1356 template<typename _Result, typename _Func, typename... _BoundArgs>
1358 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1359 bind(_Func&& __f, _BoundArgs&&... __args)
1361 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1362 typedef typename __helper_type::__maybe_type __maybe_type;
1363 typedef typename __helper_type::type __result_type;
1364 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1365 std::forward<_BoundArgs>(__args)...);
1368 template<typename _Signature>
1369 struct _Bind_simple;
1371 template<typename _Callable, typename... _Args>
1372 struct _Bind_simple<_Callable(_Args...)>
1374 typedef typename result_of<_Callable(_Args...)>::type result_type;
1376 template<typename _Tp, typename... _Up>
1378 _Bind_simple(_Tp&& __f, _Up&&... __args)
1379 : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
1382 _Bind_simple(const _Bind_simple&) = default;
1383 _Bind_simple(_Bind_simple&&) = default;
1388 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1389 return _M_invoke(_Indices());
1393 template<std::size_t... _Indices>
1394 typename result_of<_Callable(_Args...)>::type
1395 _M_invoke(_Index_tuple<_Indices...>)
1397 // std::bind always forwards bound arguments as lvalues,
1398 // but this type can call functions which only accept rvalues.
1399 return std::forward<_Callable>(std::get<0>(_M_bound))(
1400 std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
1403 std::tuple<_Callable, _Args...> _M_bound;
1406 template<typename _Func, typename... _BoundArgs>
1407 struct _Bind_simple_helper
1408 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1410 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1412 typedef typename __maybe_type::type __func_type;
1413 typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1417 // Simplified version of std::bind for internal use, without support for
1418 // unbound arguments, placeholders or nested bind expressions.
1419 template<typename _Callable, typename... _Args>
1420 typename _Bind_simple_helper<_Callable, _Args...>::__type
1421 __bind_simple(_Callable&& __callable, _Args&&... __args)
1423 typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
1424 typedef typename __helper_type::__maybe_type __maybe_type;
1425 typedef typename __helper_type::__type __result_type;
1426 return __result_type(
1427 __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
1428 std::forward<_Args>(__args)...);
1432 * @brief Exception class thrown when class template function's
1433 * operator() is called with an empty target.
1434 * @ingroup exceptions
1436 class bad_function_call : public std::exception
1439 virtual ~bad_function_call() noexcept;
1441 const char* what() const noexcept;
1445 * Trait identifying "location-invariant" types, meaning that the
1446 * address of the object (or any of its members) will not escape.
1447 * Trivially copyable types are location-invariant and users can
1448 * specialize this trait for other types.
1450 template<typename _Tp>
1451 struct __is_location_invariant
1452 : is_trivially_copyable<_Tp>::type
1455 class _Undefined_class;
1460 const void* _M_const_object;
1461 void (*_M_function_pointer)();
1462 void (_Undefined_class::*_M_member_pointer)();
1467 void* _M_access() { return &_M_pod_data[0]; }
1468 const void* _M_access() const { return &_M_pod_data[0]; }
1470 template<typename _Tp>
1473 { return *static_cast<_Tp*>(_M_access()); }
1475 template<typename _Tp>
1478 { return *static_cast<const _Tp*>(_M_access()); }
1480 _Nocopy_types _M_unused;
1481 char _M_pod_data[sizeof(_Nocopy_types)];
1484 enum _Manager_operation
1492 // Simple type wrapper that helps avoid annoying const problems
1493 // when casting between void pointers and pointers-to-pointers.
1494 template<typename _Tp>
1495 struct _Simple_type_wrapper
1497 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1502 template<typename _Tp>
1503 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1504 : __is_location_invariant<_Tp>
1507 // Converts a reference to a function object into a callable
1509 template<typename _Functor>
1511 __callable_functor(_Functor& __f)
1514 template<typename _Member, typename _Class>
1515 inline _Mem_fn<_Member _Class::*>
1516 __callable_functor(_Member _Class::* &__p)
1517 { return std::mem_fn(__p); }
1519 template<typename _Member, typename _Class>
1520 inline _Mem_fn<_Member _Class::*>
1521 __callable_functor(_Member _Class::* const &__p)
1522 { return std::mem_fn(__p); }
1524 template<typename _Member, typename _Class>
1525 inline _Mem_fn<_Member _Class::*>
1526 __callable_functor(_Member _Class::* volatile &__p)
1527 { return std::mem_fn(__p); }
1529 template<typename _Member, typename _Class>
1530 inline _Mem_fn<_Member _Class::*>
1531 __callable_functor(_Member _Class::* const volatile &__p)
1532 { return std::mem_fn(__p); }
1534 template<typename _Signature>
1537 /// Base class of all polymorphic function object wrappers.
1538 class _Function_base
1541 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1542 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1544 template<typename _Functor>
1548 static const bool __stored_locally =
1549 (__is_location_invariant<_Functor>::value
1550 && sizeof(_Functor) <= _M_max_size
1551 && __alignof__(_Functor) <= _M_max_align
1552 && (_M_max_align % __alignof__(_Functor) == 0));
1554 typedef integral_constant<bool, __stored_locally> _Local_storage;
1556 // Retrieve a pointer to the function object
1558 _M_get_pointer(const _Any_data& __source)
1560 const _Functor* __ptr =
1561 __stored_locally? std::__addressof(__source._M_access<_Functor>())
1562 /* have stored a pointer */ : __source._M_access<_Functor*>();
1563 return const_cast<_Functor*>(__ptr);
1566 // Clone a location-invariant function object that fits within
1567 // an _Any_data structure.
1569 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1571 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1574 // Clone a function object that is not location-invariant or
1575 // that cannot fit into an _Any_data structure.
1577 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1579 __dest._M_access<_Functor*>() =
1580 new _Functor(*__source._M_access<_Functor*>());
1583 // Destroying a location-invariant object may still require
1586 _M_destroy(_Any_data& __victim, true_type)
1588 __victim._M_access<_Functor>().~_Functor();
1591 // Destroying an object located on the heap.
1593 _M_destroy(_Any_data& __victim, false_type)
1595 delete __victim._M_access<_Functor*>();
1600 _M_manager(_Any_data& __dest, const _Any_data& __source,
1601 _Manager_operation __op)
1606 case __get_type_info:
1607 __dest._M_access<const type_info*>() = &typeid(_Functor);
1610 case __get_functor_ptr:
1611 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1614 case __clone_functor:
1615 _M_clone(__dest, __source, _Local_storage());
1618 case __destroy_functor:
1619 _M_destroy(__dest, _Local_storage());
1626 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1627 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1629 template<typename _Signature>
1631 _M_not_empty_function(const function<_Signature>& __f)
1632 { return static_cast<bool>(__f); }
1634 template<typename _Tp>
1636 _M_not_empty_function(_Tp* const& __fp)
1639 template<typename _Class, typename _Tp>
1641 _M_not_empty_function(_Tp _Class::* const& __mp)
1644 template<typename _Tp>
1646 _M_not_empty_function(const _Tp&)
1651 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1652 { new (__functor._M_access()) _Functor(std::move(__f)); }
1655 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1656 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1659 template<typename _Functor>
1660 class _Ref_manager : public _Base_manager<_Functor*>
1662 typedef _Function_base::_Base_manager<_Functor*> _Base;
1666 _M_manager(_Any_data& __dest, const _Any_data& __source,
1667 _Manager_operation __op)
1672 case __get_type_info:
1673 __dest._M_access<const type_info*>() = &typeid(_Functor);
1676 case __get_functor_ptr:
1677 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1678 return is_const<_Functor>::value;
1682 _Base::_M_manager(__dest, __source, __op);
1688 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1690 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1694 _Function_base() : _M_manager(nullptr) { }
1699 _M_manager(_M_functor, _M_functor, __destroy_functor);
1703 bool _M_empty() const { return !_M_manager; }
1705 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1706 _Manager_operation);
1708 _Any_data _M_functor;
1709 _Manager_type _M_manager;
1712 template<typename _Signature, typename _Functor>
1713 class _Function_handler;
1715 template<typename _Res, typename _Functor, typename... _ArgTypes>
1716 class _Function_handler<_Res(_ArgTypes...), _Functor>
1717 : public _Function_base::_Base_manager<_Functor>
1719 typedef _Function_base::_Base_manager<_Functor> _Base;
1723 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1725 return (*_Base::_M_get_pointer(__functor))(
1726 std::forward<_ArgTypes>(__args)...);
1730 template<typename _Functor, typename... _ArgTypes>
1731 class _Function_handler<void(_ArgTypes...), _Functor>
1732 : public _Function_base::_Base_manager<_Functor>
1734 typedef _Function_base::_Base_manager<_Functor> _Base;
1738 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1740 (*_Base::_M_get_pointer(__functor))(
1741 std::forward<_ArgTypes>(__args)...);
1745 template<typename _Res, typename _Functor, typename... _ArgTypes>
1746 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1747 : public _Function_base::_Ref_manager<_Functor>
1749 typedef _Function_base::_Ref_manager<_Functor> _Base;
1753 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1755 return std::__callable_functor(**_Base::_M_get_pointer(__functor))(
1756 std::forward<_ArgTypes>(__args)...);
1760 template<typename _Functor, typename... _ArgTypes>
1761 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1762 : public _Function_base::_Ref_manager<_Functor>
1764 typedef _Function_base::_Ref_manager<_Functor> _Base;
1768 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1770 std::__callable_functor(**_Base::_M_get_pointer(__functor))(
1771 std::forward<_ArgTypes>(__args)...);
1775 template<typename _Class, typename _Member, typename _Res,
1776 typename... _ArgTypes>
1777 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1778 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1780 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1785 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1787 return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1788 std::forward<_ArgTypes>(__args)...);
1792 template<typename _Class, typename _Member, typename... _ArgTypes>
1793 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1794 : public _Function_base::_Base_manager<
1795 _Simple_type_wrapper< _Member _Class::* > >
1797 typedef _Member _Class::* _Functor;
1798 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1799 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1803 _M_manager(_Any_data& __dest, const _Any_data& __source,
1804 _Manager_operation __op)
1809 case __get_type_info:
1810 __dest._M_access<const type_info*>() = &typeid(_Functor);
1813 case __get_functor_ptr:
1814 __dest._M_access<_Functor*>() =
1815 &_Base::_M_get_pointer(__source)->__value;
1819 _Base::_M_manager(__dest, __source, __op);
1825 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1827 std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1828 std::forward<_ArgTypes>(__args)...);
1832 template<typename _From, typename _To>
1833 using __check_func_return_type
1834 = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
1837 * @brief Primary class template for std::function.
1840 * Polymorphic function wrapper.
1842 template<typename _Res, typename... _ArgTypes>
1843 class function<_Res(_ArgTypes...)>
1844 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1845 private _Function_base
1847 typedef _Res _Signature_type(_ArgTypes...);
1849 template<typename _Functor>
1851 = decltype(std::__callable_functor(std::declval<_Functor&>())
1852 (std::declval<_ArgTypes>()...) );
1854 // Used so the return type convertibility checks aren't done when
1855 // performing overload resolution for copy construction/assignment.
1856 template<typename _Tp>
1857 using _NotSelf = __not_<is_same<_Tp, function>>;
1859 template<typename _Functor>
1861 = __and_<_NotSelf<_Functor>,
1862 __check_func_return_type<_Invoke<_Functor>, _Res>>;
1864 template<typename _Cond, typename _Tp>
1865 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
1868 typedef _Res result_type;
1870 // [3.7.2.1] construct/copy/destroy
1873 * @brief Default construct creates an empty function call wrapper.
1874 * @post @c !(bool)*this
1877 : _Function_base() { }
1880 * @brief Creates an empty function call wrapper.
1881 * @post @c !(bool)*this
1883 function(nullptr_t) noexcept
1884 : _Function_base() { }
1887 * @brief %Function copy constructor.
1888 * @param __x A %function object with identical call signature.
1889 * @post @c bool(*this) == bool(__x)
1891 * The newly-created %function contains a copy of the target of @a
1892 * __x (if it has one).
1894 function(const function& __x);
1897 * @brief %Function move constructor.
1898 * @param __x A %function object rvalue with identical call signature.
1900 * The newly-created %function contains the target of @a __x
1903 function(function&& __x) : _Function_base()
1908 // TODO: needs allocator_arg_t
1911 * @brief Builds a %function that targets a copy of the incoming
1913 * @param __f A %function object that is callable with parameters of
1914 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1917 * The newly-created %function object will target a copy of
1918 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
1919 * object will contain a reference to the function object @c
1920 * __f.get(). If @a __f is a NULL function pointer or NULL
1921 * pointer-to-member, the newly-created object will be empty.
1923 * If @a __f is a non-NULL function pointer or an object of type @c
1924 * reference_wrapper<F>, this function will not throw.
1926 template<typename _Functor,
1927 typename = _Requires<_Callable<_Functor>, void>>
1931 * @brief %Function assignment operator.
1932 * @param __x A %function with identical call signature.
1933 * @post @c (bool)*this == (bool)x
1936 * The target of @a __x is copied to @c *this. If @a __x has no
1937 * target, then @c *this will be empty.
1939 * If @a __x targets a function pointer or a reference to a function
1940 * object, then this operation will not throw an %exception.
1943 operator=(const function& __x)
1945 function(__x).swap(*this);
1950 * @brief %Function move-assignment operator.
1951 * @param __x A %function rvalue with identical call signature.
1954 * The target of @a __x is moved to @c *this. If @a __x has no
1955 * target, then @c *this will be empty.
1957 * If @a __x targets a function pointer or a reference to a function
1958 * object, then this operation will not throw an %exception.
1961 operator=(function&& __x)
1963 function(std::move(__x)).swap(*this);
1968 * @brief %Function assignment to zero.
1969 * @post @c !(bool)*this
1972 * The target of @c *this is deallocated, leaving it empty.
1975 operator=(nullptr_t) noexcept
1979 _M_manager(_M_functor, _M_functor, __destroy_functor);
1980 _M_manager = nullptr;
1981 _M_invoker = nullptr;
1987 * @brief %Function assignment to a new target.
1988 * @param __f A %function object that is callable with parameters of
1989 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1993 * This %function object wrapper will target a copy of @a
1994 * __f. If @a __f is @c reference_wrapper<F>, then this function
1995 * object will contain a reference to the function object @c
1996 * __f.get(). If @a __f is a NULL function pointer or NULL
1997 * pointer-to-member, @c this object will be empty.
1999 * If @a __f is a non-NULL function pointer or an object of type @c
2000 * reference_wrapper<F>, this function will not throw.
2002 template<typename _Functor>
2003 _Requires<_Callable<typename decay<_Functor>::type>, function&>
2004 operator=(_Functor&& __f)
2006 function(std::forward<_Functor>(__f)).swap(*this);
2011 template<typename _Functor>
2013 operator=(reference_wrapper<_Functor> __f) noexcept
2015 function(__f).swap(*this);
2019 // [3.7.2.2] function modifiers
2022 * @brief Swap the targets of two %function objects.
2023 * @param __x A %function with identical call signature.
2025 * Swap the targets of @c this function object and @a __f. This
2026 * function will not throw an %exception.
2028 void swap(function& __x)
2030 std::swap(_M_functor, __x._M_functor);
2031 std::swap(_M_manager, __x._M_manager);
2032 std::swap(_M_invoker, __x._M_invoker);
2035 // TODO: needs allocator_arg_t
2037 template<typename _Functor, typename _Alloc>
2039 assign(_Functor&& __f, const _Alloc& __a)
2041 function(allocator_arg, __a,
2042 std::forward<_Functor>(__f)).swap(*this);
2046 // [3.7.2.3] function capacity
2049 * @brief Determine if the %function wrapper has a target.
2051 * @return @c true when this %function object contains a target,
2052 * or @c false when it is empty.
2054 * This function will not throw an %exception.
2056 explicit operator bool() const noexcept
2057 { return !_M_empty(); }
2059 // [3.7.2.4] function invocation
2062 * @brief Invokes the function targeted by @c *this.
2063 * @returns the result of the target.
2064 * @throws bad_function_call when @c !(bool)*this
2066 * The function call operator invokes the target function object
2067 * stored by @c this.
2069 _Res operator()(_ArgTypes... __args) const;
2072 // [3.7.2.5] function target access
2074 * @brief Determine the type of the target of this function object
2077 * @returns the type identifier of the target function object, or
2078 * @c typeid(void) if @c !(bool)*this.
2080 * This function will not throw an %exception.
2082 const type_info& target_type() const noexcept;
2085 * @brief Access the stored target function object.
2087 * @return Returns a pointer to the stored target function object,
2088 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2091 * This function will not throw an %exception.
2093 template<typename _Functor> _Functor* target() noexcept;
2096 template<typename _Functor> const _Functor* target() const noexcept;
2100 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
2101 _Invoker_type _M_invoker;
2104 // Out-of-line member definitions.
2105 template<typename _Res, typename... _ArgTypes>
2106 function<_Res(_ArgTypes...)>::
2107 function(const function& __x)
2110 if (static_cast<bool>(__x))
2112 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2113 _M_invoker = __x._M_invoker;
2114 _M_manager = __x._M_manager;
2118 template<typename _Res, typename... _ArgTypes>
2119 template<typename _Functor, typename>
2120 function<_Res(_ArgTypes...)>::
2121 function(_Functor __f)
2124 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2126 if (_My_handler::_M_not_empty_function(__f))
2128 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2129 _M_invoker = &_My_handler::_M_invoke;
2130 _M_manager = &_My_handler::_M_manager;
2134 template<typename _Res, typename... _ArgTypes>
2136 function<_Res(_ArgTypes...)>::
2137 operator()(_ArgTypes... __args) const
2140 __throw_bad_function_call();
2141 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2145 template<typename _Res, typename... _ArgTypes>
2147 function<_Res(_ArgTypes...)>::
2148 target_type() const noexcept
2152 _Any_data __typeinfo_result;
2153 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2154 return *__typeinfo_result._M_access<const type_info*>();
2157 return typeid(void);
2160 template<typename _Res, typename... _ArgTypes>
2161 template<typename _Functor>
2163 function<_Res(_ArgTypes...)>::
2166 if (typeid(_Functor) == target_type() && _M_manager)
2169 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2170 && !is_const<_Functor>::value)
2173 return __ptr._M_access<_Functor*>();
2179 template<typename _Res, typename... _ArgTypes>
2180 template<typename _Functor>
2182 function<_Res(_ArgTypes...)>::
2183 target() const noexcept
2185 if (typeid(_Functor) == target_type() && _M_manager)
2188 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2189 return __ptr._M_access<const _Functor*>();
2196 // [20.7.15.2.6] null pointer comparisons
2199 * @brief Compares a polymorphic function object wrapper against 0
2200 * (the NULL pointer).
2201 * @returns @c true if the wrapper has no target, @c false otherwise
2203 * This function will not throw an %exception.
2205 template<typename _Res, typename... _Args>
2207 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2208 { return !static_cast<bool>(__f); }
2211 template<typename _Res, typename... _Args>
2213 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2214 { return !static_cast<bool>(__f); }
2217 * @brief Compares a polymorphic function object wrapper against 0
2218 * (the NULL pointer).
2219 * @returns @c false if the wrapper has no target, @c true otherwise
2221 * This function will not throw an %exception.
2223 template<typename _Res, typename... _Args>
2225 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2226 { return static_cast<bool>(__f); }
2229 template<typename _Res, typename... _Args>
2231 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2232 { return static_cast<bool>(__f); }
2234 // [20.7.15.2.7] specialized algorithms
2237 * @brief Swap the targets of two polymorphic function object wrappers.
2239 * This function will not throw an %exception.
2241 template<typename _Res, typename... _Args>
2243 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2246 _GLIBCXX_END_NAMESPACE_VERSION
2251 #endif // _GLIBCXX_FUNCTIONAL