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