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
60namespace 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 _Bind __self_type;
931 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
932 _Bound_indexes;
933
934 _Functor _M_f;
935 tuple<_Bound_args...> _M_bound_args;
936
937 // Call unqualified
938 template<typename _Result, typename... _Args, std::size_t... _Indexes>
939 _Result
940 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
941 {
942 return _M_f(_Mu<_Bound_args>()
943 (std::get<_Indexes>(_M_bound_args), __args)...);
944 }
945
946 // Call as const
947 template<typename _Result, typename... _Args, std::size_t... _Indexes>
948 _Result
949 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
950 {
951 return _M_f(_Mu<_Bound_args>()
952 (std::get<_Indexes>(_M_bound_args), __args)...);
953 }
954
955 // Call as volatile
956 template<typename _Result, typename... _Args, std::size_t... _Indexes>
957 _Result
958 __call_v(tuple<_Args...>&& __args,
959 _Index_tuple<_Indexes...>) volatile
960 {
961 return _M_f(_Mu<_Bound_args>()
962 (__volget<_Indexes>(_M_bound_args), __args)...);
963 }
964
965 // Call as const volatile
966 template<typename _Result, typename... _Args, std::size_t... _Indexes>
967 _Result
968 __call_c_v(tuple<_Args...>&& __args,
969 _Index_tuple<_Indexes...>) const volatile
970 {
971 return _M_f(_Mu<_Bound_args>()
972 (__volget<_Indexes>(_M_bound_args), __args)...);
973 }
974
975 public:
976 template<typename... _Args>
977 explicit _Bind(const _Functor& __f, _Args&&... __args)
978 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
979 { }
980
981 template<typename... _Args>
982 explicit _Bind(_Functor&& __f, _Args&&... __args)
983 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
984 { }
985
986 _Bind(const _Bind&) = default;
987
988 _Bind(_Bind&& __b)
989 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
990 { }
991
992 // Call unqualified
993 template<typename... _Args, typename _Result
994 = decltype( std::declval<_Functor&>()(
995 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
996 std::declval<tuple<_Args...>&>() )... ) )>
997 _Result
998 operator()(_Args&&... __args)
999 {
1000 return this->__call<_Result>(
1001 std::forward_as_tuple(std::forward<_Args>(__args)...),
1002 _Bound_indexes());
1003 }
1004
1005 // Call as const
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...>&>() )... ) )>
1011 _Result
1012 operator()(_Args&&... __args) const
1013 {
1014 return this->__call_c<_Result>(
1015 std::forward_as_tuple(std::forward<_Args>(__args)...),
1016 _Bound_indexes());
1017 }
1018
1019 // Call as volatile
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...>&>() )... ) )>
1025 _Result
1026 operator()(_Args&&... __args) volatile
1027 {
1028 return this->__call_v<_Result>(
1029 std::forward_as_tuple(std::forward<_Args>(__args)...),
1030 _Bound_indexes());
1031 }
1032
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...>&>() )... ) )>
1039 _Result
1040 operator()(_Args&&... __args) const volatile
1041 {
1042 return this->__call_c_v<_Result>(
1043 std::forward_as_tuple(std::forward<_Args>(__args)...),
1044 _Bound_indexes());
1045 }
1046 };
1047
1048 /// Type of the function object returned from bind<R>().
1049 template<typename _Result, typename _Signature>
1050 struct _Bind_result;
1051
1052 template<typename _Result, typename _Functor, typename... _Bound_args>
1053 class _Bind_result<_Result, _Functor(_Bound_args...)>
1054 {
1055 typedef _Bind_result __self_type;
1056 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1057 _Bound_indexes;
1058
1059 _Functor _M_f;
1060 tuple<_Bound_args...> _M_bound_args;
1061
1062 // sfinae types
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> { };
1067
1068 // Call unqualified
1069 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1070 _Result
1071 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1072 typename __disable_if_void<_Res>::type = 0)
1073 {
1074 return _M_f(_Mu<_Bound_args>()
1075 (std::get<_Indexes>(_M_bound_args), __args)...);
1076 }
1077
1078 // Call unqualified, return void
1079 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1080 void
1081 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1082 typename __enable_if_void<_Res>::type = 0)
1083 {
1084 _M_f(_Mu<_Bound_args>()
1085 (std::get<_Indexes>(_M_bound_args), __args)...);
1086 }
1087
1088 // Call as const
1089 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1090 _Result
1091 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1092 typename __disable_if_void<_Res>::type = 0) const
1093 {
1094 return _M_f(_Mu<_Bound_args>()
1095 (std::get<_Indexes>(_M_bound_args), __args)...);
1096 }
1097
1098 // Call as const, return void
1099 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1100 void
1101 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1102 typename __enable_if_void<_Res>::type = 0) const
1103 {
1104 _M_f(_Mu<_Bound_args>()
1105 (std::get<_Indexes>(_M_bound_args), __args)...);
1106 }
1107
1108 // Call as volatile
1109 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1110 _Result
1111 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1112 typename __disable_if_void<_Res>::type = 0) volatile
1113 {
1114 return _M_f(_Mu<_Bound_args>()
1115 (__volget<_Indexes>(_M_bound_args), __args)...);
1116 }
1117
1118 // Call as volatile, return void
1119 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1120 void
1121 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1122 typename __enable_if_void<_Res>::type = 0) volatile
1123 {
1124 _M_f(_Mu<_Bound_args>()
1125 (__volget<_Indexes>(_M_bound_args), __args)...);
1126 }
1127
1128 // Call as const volatile
1129 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1130 _Result
1131 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1132 typename __disable_if_void<_Res>::type = 0) const volatile
1133 {
1134 return _M_f(_Mu<_Bound_args>()
1135 (__volget<_Indexes>(_M_bound_args), __args)...);
1136 }
1137
1138 // Call as const volatile, return void
1139 template<typename _Res, typename... _Args, std::size_t... _Indexes>
1140 void
1141 __call(tuple<_Args...>&& __args,
1142 _Index_tuple<_Indexes...>,
1143 typename __enable_if_void<_Res>::type = 0) const volatile
1144 {
1145 _M_f(_Mu<_Bound_args>()
1146 (__volget<_Indexes>(_M_bound_args), __args)...);
1147 }
1148
1149 public:
1150 typedef _Result result_type;
1151
1152 template<typename... _Args>
1153 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1154 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1155 { }
1156
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)...)
1160 { }
1161
1162 _Bind_result(const _Bind_result&) = default;
1163
1164 _Bind_result(_Bind_result&& __b)
1165 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1166 { }
1167
1168 // Call unqualified
1169 template<typename... _Args>
1170 result_type
1171 operator()(_Args&&... __args)
1172 {
1173 return this->__call<_Result>(
1174 std::forward_as_tuple(std::forward<_Args>(__args)...),
1175 _Bound_indexes());
1176 }
1177
1178 // Call as const
1179 template<typename... _Args>
1180 result_type
1181 operator()(_Args&&... __args) const
1182 {
1183 return this->__call<_Result>(
1184 std::forward_as_tuple(std::forward<_Args>(__args)...),
1185 _Bound_indexes());
1186 }
1187
1188 // Call as volatile
1189 template<typename... _Args>
1190 result_type
1191 operator()(_Args&&... __args) volatile
1192 {
1193 return this->__call<_Result>(
1194 std::forward_as_tuple(std::forward<_Args>(__args)...),
1195 _Bound_indexes());
1196 }
1197
1198 // Call as const volatile
1199 template<typename... _Args>
1200 result_type
1201 operator()(_Args&&... __args) const volatile
1202 {
1203 return this->__call<_Result>(
1204 std::forward_as_tuple(std::forward<_Args>(__args)...),
1205 _Bound_indexes());
1206 }
1207 };
1208
1209 /**
1210 * @brief Class template _Bind is always a bind expression.
1211 * @ingroup binders
1212 */
1213 template<typename _Signature>
1214 struct is_bind_expression<_Bind<_Signature> >
1215 : public true_type { };
1216
1217 /**
1218 * @brief Class template _Bind is always a bind expression.
1219 * @ingroup binders
1220 */
1221 template<typename _Signature>
1222 struct is_bind_expression<const _Bind<_Signature> >
1223 : public true_type { };
1224
1225 /**
1226 * @brief Class template _Bind is always a bind expression.
1227 * @ingroup binders
1228 */
1229 template<typename _Signature>
1230 struct is_bind_expression<volatile _Bind<_Signature> >
1231 : public true_type { };
1232
1233 /**
1234 * @brief Class template _Bind is always a bind expression.
1235 * @ingroup binders
1236 */
1237 template<typename _Signature>
1238 struct is_bind_expression<const volatile _Bind<_Signature>>
1239 : public true_type { };
1240
1241 /**
1242 * @brief Class template _Bind_result is always a bind expression.
1243 * @ingroup binders
1244 */
1245 template<typename _Result, typename _Signature>
1246 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1247 : public true_type { };
1248
1249 /**
1250 * @brief Class template _Bind_result is always a bind expression.
1251 * @ingroup binders
1252 */
1253 template<typename _Result, typename _Signature>
1254 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1255 : public true_type { };
1256
1257 /**
1258 * @brief Class template _Bind_result is always a bind expression.
1259 * @ingroup binders
1260 */
1261 template<typename _Result, typename _Signature>
1262 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1263 : public true_type { };
1264
1265 /**
1266 * @brief Class template _Bind_result is always a bind expression.
1267 * @ingroup binders
1268 */
1269 template<typename _Result, typename _Signature>
1270 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1271 : public true_type { };
1272
1273 template<typename _Func, typename... _BoundArgs>
1274 struct _Bind_check_arity { };
1275
1276 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1277 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
1278 {
1279 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
1280 "Wrong number of arguments for function");
1281 };
1282
1283 template<typename _Ret, typename... _Args, typename... _BoundArgs>
1284 struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
1285 {
1286 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
1287 "Wrong number of arguments for function");
1288 };
1289
1290 template<typename _Tp, typename _Class, typename... _BoundArgs>
1291 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
1292 {
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");
1299 };
1300
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>>;
1306
1307 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1308 struct _Bind_helper
1309 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1310 {
1311 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1312 __maybe_type;
1313 typedef typename __maybe_type::type __func_type;
1314 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1315 };
1316
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...>
1322 { };
1323
1324 /**
1325 * @brief Function template for std::bind.
1326 * @ingroup binders
1327 */
1328 template<typename _Func, typename... _BoundArgs>
1329 inline typename
1330 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1331 bind(_Func&& __f, _BoundArgs&&... __args)
1332 {
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)...);
1338 }
1339
1340 template<typename _Result, typename _Func, typename... _BoundArgs>
1341 struct _Bindres_helper
1342 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1343 {
1344 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1345 __maybe_type;
1346 typedef typename __maybe_type::type __functor_type;
1347 typedef _Bind_result<_Result,
1348 __functor_type(typename decay<_BoundArgs>::type...)>
1349 type;
1350 };
1351
1352 /**
1353 * @brief Function template for std::bind<R>.
1354 * @ingroup binders
1355 */
1356 template<typename _Result, typename _Func, typename... _BoundArgs>
1357 inline
1358 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1359 bind(_Func&& __f, _BoundArgs&&... __args)
1360 {
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)...);
1366 }
1367
1368 template<typename _Signature>
1369 struct _Bind_simple;
1370
1371 template<typename _Callable, typename... _Args>
1372 struct _Bind_simple<_Callable(_Args...)>
1373 {
1374 typedef typename result_of<_Callable(_Args...)>::type result_type;
1375
1376 template<typename _Tp, typename... _Up>
1377 explicit
1378 _Bind_simple(_Tp&& __f, _Up&&... __args)
1379 : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
1380 { }
1381
1382 _Bind_simple(const _Bind_simple&) = default;
1383 _Bind_simple(_Bind_simple&&) = default;
1384
1385 result_type
1386 operator()()
1387 {
1388 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1389 return _M_invoke(_Indices());
1390 }
1391
1392 private:
1393 template<std::size_t... _Indices>
1394 typename result_of<_Callable(_Args...)>::type
1395 _M_invoke(_Index_tuple<_Indices...>)
1396 {
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))...);
1401 }
1402
1403 std::tuple<_Callable, _Args...> _M_bound;
1404 };
1405
1406 template<typename _Func, typename... _BoundArgs>
1407 struct _Bind_simple_helper
1408 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1409 {
1410 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1411 __maybe_type;
1412 typedef typename __maybe_type::type __func_type;
1413 typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1414 __type;
1415 };
1416
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)
1422 {
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)...);
1429 }
1430
1431 /**
1432 * @brief Exception class thrown when class template function's
1433 * operator() is called with an empty target.
1434 * @ingroup exceptions
1435 */
1436 class bad_function_call : public std::exception
1437 {
1438 public:
1439 virtual ~bad_function_call() noexcept;
1440
1441 const char* what() const noexcept;
1442 };
1443
1444 /**
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.
1449 */
1450 template<typename _Tp>
1451 struct __is_location_invariant
1452 : is_trivially_copyable<_Tp>::type
1453 { };
1454
1455 class _Undefined_class;
1456
1457 union _Nocopy_types
1458 {
1459 void* _M_object;
1460 const void* _M_const_object;
1461 void (*_M_function_pointer)();
1462 void (_Undefined_class::*_M_member_pointer)();
1463 };
1464
1465 union _Any_data
1466 {
1467 void* _M_access() { return &_M_pod_data[0]; }
1468 const void* _M_access() const { return &_M_pod_data[0]; }
1469
1470 template<typename _Tp>
1471 _Tp&
1472 _M_access()
1473 { return *static_cast<_Tp*>(_M_access()); }
1474
1475 template<typename _Tp>
1476 const _Tp&
1477 _M_access() const
1478 { return *static_cast<const _Tp*>(_M_access()); }
1479
1480 _Nocopy_types _M_unused;
1481 char _M_pod_data[sizeof(_Nocopy_types)];
1482 };
1483
1484 enum _Manager_operation
1485 {
1486 __get_type_info,
1487 __get_functor_ptr,
1488 __clone_functor,
1489 __destroy_functor
1490 };
1491
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
1496 {
1497 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1498
1499 _Tp __value;
1500 };
1501
1502 template<typename _Tp>
1503 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1504 : __is_location_invariant<_Tp>
1505 { };
1506
1507 // Converts a reference to a function object into a callable
1508 // function object.
1509 template<typename _Functor>
1510 inline _Functor&
1511 __callable_functor(_Functor& __f)
1512 { return __f; }
1513
1514 template<typename _Member, typename _Class>
1515 inline _Mem_fn<_Member _Class::*>
1516 __callable_functor(_Member _Class::* &__p)
1517 { return std::mem_fn(__p); }
1518
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); }
1523
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); }
1528
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); }
1533
1534 template<typename _Signature>
1535 class function;
1536
1537 /// Base class of all polymorphic function object wrappers.
1538 class _Function_base
1539 {
1540 public:
1541 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1542 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1543
1544 template<typename _Functor>
1545 class _Base_manager
1546 {
1547 protected:
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));
1553
1554 typedef integral_constant<bool, __stored_locally> _Local_storage;
1555
1556 // Retrieve a pointer to the function object
1557 static _Functor*
1558 _M_get_pointer(const _Any_data& __source)
1559 {
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);
1564 }
1565
1566 // Clone a location-invariant function object that fits within
1567 // an _Any_data structure.
1568 static void
1569 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1570 {
1571 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1572 }
1573
1574 // Clone a function object that is not location-invariant or
1575 // that cannot fit into an _Any_data structure.
1576 static void
1577 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1578 {
1579 __dest._M_access<_Functor*>() =
1580 new _Functor(*__source._M_access<_Functor*>());
1581 }
1582
1583 // Destroying a location-invariant object may still require
1584 // destruction.
1585 static void
1586 _M_destroy(_Any_data& __victim, true_type)
1587 {
1588 __victim._M_access<_Functor>().~_Functor();
1589 }
1590
1591 // Destroying an object located on the heap.
1592 static void
1593 _M_destroy(_Any_data& __victim, false_type)
1594 {
1595 delete __victim._M_access<_Functor*>();
1596 }
1597
1598 public:
1599 static bool
1600 _M_manager(_Any_data& __dest, const _Any_data& __source,
1601 _Manager_operation __op)
1602 {
1603 switch (__op)
1604 {
1605#if __cpp_rtti
1606 case __get_type_info:
1607 __dest._M_access<const type_info*>() = &typeid(_Functor);
1608 break;
1609#endif
1610 case __get_functor_ptr:
1611 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1612 break;
1613
1614 case __clone_functor:
1615 _M_clone(__dest, __source, _Local_storage());
1616 break;
1617
1618 case __destroy_functor:
1619 _M_destroy(__dest, _Local_storage());
1620 break;
1621 }
1622 return false;
1623 }
1624
1625 static void
1626 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1627 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1628
1629 template<typename _Signature>
1630 static bool
1631 _M_not_empty_function(const function<_Signature>& __f)
1632 { return static_cast<bool>(__f); }
1633
1634 template<typename _Tp>
1635 static bool
1636 _M_not_empty_function(_Tp* __fp)
1637 { return __fp != nullptr; }
1638
1639 template<typename _Class, typename _Tp>
1640 static bool
1641 _M_not_empty_function(_Tp _Class::* __mp)
1642 { return __mp != nullptr; }
1643
1644 template<typename _Tp>
1645 static bool
1646 _M_not_empty_function(const _Tp&)
1647 { return true; }
1648
1649 private:
1650 static void
1651 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1652 { new (__functor._M_access()) _Functor(std::move(__f)); }
1653
1654 static void
1655 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1656 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1657 };
1658
1659 template<typename _Functor>
1660 class _Ref_manager : public _Base_manager<_Functor*>
1661 {
1662 typedef _Function_base::_Base_manager<_Functor*> _Base;
1663
1664 public:
1665 static bool
1666 _M_manager(_Any_data& __dest, const _Any_data& __source,
1667 _Manager_operation __op)
1668 {
1669 switch (__op)
1670 {
1671#if __cpp_rtti
1672 case __get_type_info:
1673 __dest._M_access<const type_info*>() = &typeid(_Functor);
1674 break;
1675#endif
1676 case __get_functor_ptr:
1677 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1678 return is_const<_Functor>::value;
1679 break;
1680
1681 default:
1682 _Base::_M_manager(__dest, __source, __op);
1683 }
1684 return false;
1685 }
1686
1687 static void
1688 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1689 {
1690 _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
1691 }
1692 };
1693
1694 _Function_base() : _M_manager(nullptr) { }
1695
1696 ~_Function_base()
1697 {
1698 if (_M_manager)
1699 _M_manager(_M_functor, _M_functor, __destroy_functor);
1700 }
1701
1702
1703 bool _M_empty() const { return !_M_manager; }
1704
1705 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1706 _Manager_operation);
1707
1708 _Any_data _M_functor;
1709 _Manager_type _M_manager;
1710 };
1711
1712 template<typename _Signature, typename _Functor>
1713 class _Function_handler;
1714
1715 template<typename _Res, typename _Functor, typename... _ArgTypes>
1716 class _Function_handler<_Res(_ArgTypes...), _Functor>
1717 : public _Function_base::_Base_manager<_Functor>
1718 {
1719 typedef _Function_base::_Base_manager<_Functor> _Base;
1720
1721 public:
1722 static _Res
1723 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1724 {
1725 return (*_Base::_M_get_pointer(__functor))(
1726 std::forward<_ArgTypes>(__args)...);
1727 }
1728 };
1729
1730 template<typename _Functor, typename... _ArgTypes>
1731 class _Function_handler<void(_ArgTypes...), _Functor>
1732 : public _Function_base::_Base_manager<_Functor>
1733 {
1734 typedef _Function_base::_Base_manager<_Functor> _Base;
1735
1736 public:
1737 static void
1738 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1739 {
1740 (*_Base::_M_get_pointer(__functor))(
1741 std::forward<_ArgTypes>(__args)...);
1742 }
1743 };
1744
1745 template<typename _Res, typename _Functor, typename... _ArgTypes>
1746 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1747 : public _Function_base::_Ref_manager<_Functor>
1748 {
1749 typedef _Function_base::_Ref_manager<_Functor> _Base;
1750
1751 public:
1752 static _Res
1753 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1754 {
1755 return std::__callable_functor(**_Base::_M_get_pointer(__functor))(
1756 std::forward<_ArgTypes>(__args)...);
1757 }
1758 };
1759
1760 template<typename _Functor, typename... _ArgTypes>
1761 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1762 : public _Function_base::_Ref_manager<_Functor>
1763 {
1764 typedef _Function_base::_Ref_manager<_Functor> _Base;
1765
1766 public:
1767 static void
1768 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1769 {
1770 std::__callable_functor(**_Base::_M_get_pointer(__functor))(
1771 std::forward<_ArgTypes>(__args)...);
1772 }
1773 };
1774
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::*>
1779 {
1780 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1781 _Base;
1782
1783 public:
1784 static _Res
1785 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1786 {
1787 return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1788 std::forward<_ArgTypes>(__args)...);
1789 }
1790 };
1791
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::* > >
1796 {
1797 typedef _Member _Class::* _Functor;
1798 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1799 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1800
1801 public:
1802 static bool
1803 _M_manager(_Any_data& __dest, const _Any_data& __source,
1804 _Manager_operation __op)
1805 {
1806 switch (__op)
1807 {
1808#if __cpp_rtti
1809 case __get_type_info:
1810 __dest._M_access<const type_info*>() = &typeid(_Functor);
1811 break;
1812#endif
1813 case __get_functor_ptr:
1814 __dest._M_access<_Functor*>() =
1815 &_Base::_M_get_pointer(__source)->__value;
1816 break;
1817
1818 default:
1819 _Base::_M_manager(__dest, __source, __op);
1820 }
1821 return false;
1822 }
1823
1824 static void
1825 _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
1826 {
1827 std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1828 std::forward<_ArgTypes>(__args)...);
1829 }
1830 };
1831
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>>;
1835
1836 /**
1837 * @brief Primary class template for std::function.
1838 * @ingroup functors
1839 *
1840 * Polymorphic function wrapper.
1841 */
1842 template<typename _Res, typename... _ArgTypes>
1843 class function<_Res(_ArgTypes...)>
1844 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1845 private _Function_base
1846 {
1847 typedef _Res _Signature_type(_ArgTypes...);
1848
1849 template<typename _Func,
1850 typename _Res2 = typename result_of<_Func(_ArgTypes...)>::type>
1851 struct _Callable : __check_func_return_type<_Res2, _Res> { };
1852
1853 // Used so the return type convertibility checks aren't done when
1854 // performing overload resolution for copy construction/assignment.
1855 template<typename _Tp>
1856 struct _Callable<function, _Tp> : false_type { };
1857
1858 template<typename _Cond, typename _Tp>
1859 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
1860
1861 public:
1862 typedef _Res result_type;
1863
1864 // [3.7.2.1] construct/copy/destroy
1865
1866 /**
1867 * @brief Default construct creates an empty function call wrapper.
1868 * @post @c !(bool)*this
1869 */
1870 function() noexcept
1871 : _Function_base() { }
1872
1873 /**
1874 * @brief Creates an empty function call wrapper.
1875 * @post @c !(bool)*this
1876 */
1877 function(nullptr_t) noexcept
1878 : _Function_base() { }
1879
1880 /**
1881 * @brief %Function copy constructor.
1882 * @param __x A %function object with identical call signature.
1883 * @post @c bool(*this) == bool(__x)
1884 *
1885 * The newly-created %function contains a copy of the target of @a
1886 * __x (if it has one).
1887 */
1888 function(const function& __x);
1889
1890 /**
1891 * @brief %Function move constructor.
1892 * @param __x A %function object rvalue with identical call signature.
1893 *
1894 * The newly-created %function contains the target of @a __x
1895 * (if it has one).
1896 */
1897 function(function&& __x) : _Function_base()
1898 {
1899 __x.swap(*this);
1900 }
1901
1902 // TODO: needs allocator_arg_t
1903
1904 /**
1905 * @brief Builds a %function that targets a copy of the incoming
1906 * function object.
1907 * @param __f A %function object that is callable with parameters of
1908 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1909 * to @c Res.
1910 *
1911 * The newly-created %function object will target a copy of
1912 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
1913 * object will contain a reference to the function object @c
1914 * __f.get(). If @a __f is a NULL function pointer or NULL
1915 * pointer-to-member, the newly-created object will be empty.
1916 *
1917 * If @a __f is a non-NULL function pointer or an object of type @c
1918 * reference_wrapper<F>, this function will not throw.
1919 */
1920 template<typename _Functor,
1921 typename = _Requires<__not_<is_same<_Functor, function>>, void>,
1922 typename = _Requires<_Callable<_Functor>, void>>
1923 function(_Functor);
1924
1925 /**
1926 * @brief %Function assignment operator.
1927 * @param __x A %function with identical call signature.
1928 * @post @c (bool)*this == (bool)x
1929 * @returns @c *this
1930 *
1931 * The target of @a __x is copied to @c *this. If @a __x has no
1932 * target, then @c *this will be empty.
1933 *
1934 * If @a __x targets a function pointer or a reference to a function
1935 * object, then this operation will not throw an %exception.
1936 */
1937 function&
1938 operator=(const function& __x)
1939 {
1940 function(__x).swap(*this);
1941 return *this;
1942 }
1943
1944 /**
1945 * @brief %Function move-assignment operator.
1946 * @param __x A %function rvalue with identical call signature.
1947 * @returns @c *this
1948 *
1949 * The target of @a __x is moved to @c *this. If @a __x has no
1950 * target, then @c *this will be empty.
1951 *
1952 * If @a __x targets a function pointer or a reference to a function
1953 * object, then this operation will not throw an %exception.
1954 */
1955 function&
1956 operator=(function&& __x)
1957 {
1958 function(std::move(__x)).swap(*this);
1959 return *this;
1960 }
1961
1962 /**
1963 * @brief %Function assignment to zero.
1964 * @post @c !(bool)*this
1965 * @returns @c *this
1966 *
1967 * The target of @c *this is deallocated, leaving it empty.
1968 */
1969 function&
1970 operator=(nullptr_t) noexcept
1971 {
1972 if (_M_manager)
1973 {
1974 _M_manager(_M_functor, _M_functor, __destroy_functor);
1975 _M_manager = nullptr;
1976 _M_invoker = nullptr;
1977 }
1978 return *this;
1979 }
1980
1981 /**
1982 * @brief %Function assignment to a new target.
1983 * @param __f A %function object that is callable with parameters of
1984 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1985 * to @c Res.
1986 * @return @c *this
1987 *
1988 * This %function object wrapper will target a copy of @a
1989 * __f. If @a __f is @c reference_wrapper<F>, then this function
1990 * object will contain a reference to the function object @c
1991 * __f.get(). If @a __f is a NULL function pointer or NULL
1992 * pointer-to-member, @c this object will be empty.
1993 *
1994 * If @a __f is a non-NULL function pointer or an object of type @c
1995 * reference_wrapper<F>, this function will not throw.
1996 */
1997 template<typename _Functor>
1998 _Requires<_Callable<typename decay<_Functor>::type>, function&>
1999 operator=(_Functor&& __f)
2000 {
2001 function(std::forward<_Functor>(__f)).swap(*this);
2002 return *this;
2003 }
2004
2005 /// @overload
2006 template<typename _Functor>
2007 function&
2008 operator=(reference_wrapper<_Functor> __f) noexcept
2009 {
2010 function(__f).swap(*this);
2011 return *this;
2012 }
2013
2014 // [3.7.2.2] function modifiers
2015
2016 /**
2017 * @brief Swap the targets of two %function objects.
2018 * @param __x A %function with identical call signature.
2019 *
2020 * Swap the targets of @c this function object and @a __f. This
2021 * function will not throw an %exception.
2022 */
2023 void swap(function& __x)
2024 {
2025 std::swap(_M_functor, __x._M_functor);
2026 std::swap(_M_manager, __x._M_manager);
2027 std::swap(_M_invoker, __x._M_invoker);
2028 }
2029
2030 // TODO: needs allocator_arg_t
2031 /*
2032 template<typename _Functor, typename _Alloc>
2033 void
2034 assign(_Functor&& __f, const _Alloc& __a)
2035 {
2036 function(allocator_arg, __a,
2037 std::forward<_Functor>(__f)).swap(*this);
2038 }
2039 */
2040
2041 // [3.7.2.3] function capacity
2042
2043 /**
2044 * @brief Determine if the %function wrapper has a target.
2045 *
2046 * @return @c true when this %function object contains a target,
2047 * or @c false when it is empty.
2048 *
2049 * This function will not throw an %exception.
2050 */
2051 explicit operator bool() const noexcept
2052 { return !_M_empty(); }
2053
2054 // [3.7.2.4] function invocation
2055
2056 /**
2057 * @brief Invokes the function targeted by @c *this.
2058 * @returns the result of the target.
2059 * @throws bad_function_call when @c !(bool)*this
2060 *
2061 * The function call operator invokes the target function object
2062 * stored by @c this.
2063 */
2064 _Res operator()(_ArgTypes... __args) const;
2065
2066#if __cpp_rtti
2067 // [3.7.2.5] function target access
2068 /**
2069 * @brief Determine the type of the target of this function object
2070 * wrapper.
2071 *
2072 * @returns the type identifier of the target function object, or
2073 * @c typeid(void) if @c !(bool)*this.
2074 *
2075 * This function will not throw an %exception.
2076 */
2077 const type_info& target_type() const noexcept;
2078
2079 /**
2080 * @brief Access the stored target function object.
2081 *
2082 * @return Returns a pointer to the stored target function object,
2083 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2084 * pointer.
2085 *
2086 * This function will not throw an %exception.
2087 */
2088 template<typename _Functor> _Functor* target() noexcept;
2089
2090 /// @overload
2091 template<typename _Functor> const _Functor* target() const noexcept;
2092#endif
2093
2094 private:
2095 using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
2096 _Invoker_type _M_invoker;
2097 };
2098
2099 // Out-of-line member definitions.
2100 template<typename _Res, typename... _ArgTypes>
2101 function<_Res(_ArgTypes...)>::
2102 function(const function& __x)
2103 : _Function_base()
2104 {
2105 if (static_cast<bool>(__x))
2106 {
2107 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2108 _M_invoker = __x._M_invoker;
2109 _M_manager = __x._M_manager;
2110 }
2111 }
2112
2113 template<typename _Res, typename... _ArgTypes>
2114 template<typename _Functor, typename, typename>
2115 function<_Res(_ArgTypes...)>::
2116 function(_Functor __f)
2117 : _Function_base()
2118 {
2119 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2120
2121 if (_My_handler::_M_not_empty_function(__f))
2122 {
2123 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2124 _M_invoker = &_My_handler::_M_invoke;
2125 _M_manager = &_My_handler::_M_manager;
2126 }
2127 }
2128
2129 template<typename _Res, typename... _ArgTypes>
2130 _Res
2131 function<_Res(_ArgTypes...)>::
2132 operator()(_ArgTypes... __args) const
2133 {
2134 if (_M_empty())
2135 __throw_bad_function_call();
2136 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2137 }
2138
2139#if __cpp_rtti
2140 template<typename _Res, typename... _ArgTypes>
2141 const type_info&
2142 function<_Res(_ArgTypes...)>::
2143 target_type() const noexcept
2144 {
2145 if (_M_manager)
2146 {
2147 _Any_data __typeinfo_result;
2148 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2149 return *__typeinfo_result._M_access<const type_info*>();
2150 }
2151 else
2152 return typeid(void);
2153 }
2154
2155 template<typename _Res, typename... _ArgTypes>
2156 template<typename _Functor>
2157 _Functor*
2158 function<_Res(_ArgTypes...)>::
2159 target() noexcept
2160 {
2161 if (typeid(_Functor) == target_type() && _M_manager)
2162 {
2163 _Any_data __ptr;
2164 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2165 && !is_const<_Functor>::value)
2166 return 0;
2167 else
2168 return __ptr._M_access<_Functor*>();
2169 }
2170 else
2171 return 0;
2172 }
2173
2174 template<typename _Res, typename... _ArgTypes>
2175 template<typename _Functor>
2176 const _Functor*
2177 function<_Res(_ArgTypes...)>::
2178 target() const noexcept
2179 {
2180 if (typeid(_Functor) == target_type() && _M_manager)
2181 {
2182 _Any_data __ptr;
2183 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2184 return __ptr._M_access<const _Functor*>();
2185 }
2186 else
2187 return 0;
2188 }
2189#endif
2190
2191 // [20.7.15.2.6] null pointer comparisons
2192
2193 /**
2194 * @brief Compares a polymorphic function object wrapper against 0
2195 * (the NULL pointer).
2196 * @returns @c true if the wrapper has no target, @c false otherwise
2197 *
2198 * This function will not throw an %exception.
2199 */
2200 template<typename _Res, typename... _Args>
2201 inline bool
2202 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2203 { return !static_cast<bool>(__f); }
2204
2205 /// @overload
2206 template<typename _Res, typename... _Args>
2207 inline bool
2208 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2209 { return !static_cast<bool>(__f); }
2210
2211 /**
2212 * @brief Compares a polymorphic function object wrapper against 0
2213 * (the NULL pointer).
2214 * @returns @c false if the wrapper has no target, @c true otherwise
2215 *
2216 * This function will not throw an %exception.
2217 */
2218 template<typename _Res, typename... _Args>
2219 inline bool
2220 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
2221 { return static_cast<bool>(__f); }
2222
2223 /// @overload
2224 template<typename _Res, typename... _Args>
2225 inline bool
2226 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
2227 { return static_cast<bool>(__f); }
2228
2229 // [20.7.15.2.7] specialized algorithms
2230
2231 /**
2232 * @brief Swap the targets of two polymorphic function object wrappers.
2233 *
2234 * This function will not throw an %exception.
2235 */
2236 template<typename _Res, typename... _Args>
2237 inline void
2238 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2239 { __x.swap(__y); }
2240
2241_GLIBCXX_END_NAMESPACE_VERSION
2242} // namespace std
2243
2244#endif // C++11
2245
2246#endif // _GLIBCXX_FUNCTIONAL
2247