re PR libstdc++/45893 ([C++0x] [DR 817] Finish updating std::bind to rvalue refs)
[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 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 namespace
849 {
850 _Placeholder<1> _1;
851 _Placeholder<2> _2;
852 _Placeholder<3> _3;
853 _Placeholder<4> _4;
854 _Placeholder<5> _5;
855 _Placeholder<6> _6;
856 _Placeholder<7> _7;
857 _Placeholder<8> _8;
858 _Placeholder<9> _9;
859 _Placeholder<10> _10;
860 _Placeholder<11> _11;
861 _Placeholder<12> _12;
862 _Placeholder<13> _13;
863 _Placeholder<14> _14;
864 _Placeholder<15> _15;
865 _Placeholder<16> _16;
866 _Placeholder<17> _17;
867 _Placeholder<18> _18;
868 _Placeholder<19> _19;
869 _Placeholder<20> _20;
870 _Placeholder<21> _21;
871 _Placeholder<22> _22;
872 _Placeholder<23> _23;
873 _Placeholder<24> _24;
874 _Placeholder<25> _25;
875 _Placeholder<26> _26;
876 _Placeholder<27> _27;
877 _Placeholder<28> _28;
878 _Placeholder<29> _29;
879 }
880 }
881
882 /**
883 * Partial specialization of is_placeholder that provides the placeholder
884 * number for the placeholder objects defined by libstdc++.
885 * @ingroup binders
886 */
887 template<int _Num>
888 struct is_placeholder<_Placeholder<_Num> >
889 : public integral_constant<int, _Num>
890 { };
891
892 /**
893 * Used by _Safe_tuple_element to indicate that there is no tuple
894 * element at this position.
895 */
896 struct _No_tuple_element;
897
898 /**
899 * Implementation helper for _Safe_tuple_element. This primary
900 * template handles the case where it is safe to use @c
901 * tuple_element.
902 */
903 template<int __i, typename _Tuple, bool _IsSafe>
904 struct _Safe_tuple_element_impl
905 : tuple_element<__i, _Tuple> { };
906
907 /**
908 * Implementation helper for _Safe_tuple_element. This partial
909 * specialization handles the case where it is not safe to use @c
910 * tuple_element. We just return @c _No_tuple_element.
911 */
912 template<int __i, typename _Tuple>
913 struct _Safe_tuple_element_impl<__i, _Tuple, false>
914 {
915 typedef _No_tuple_element type;
916 };
917
918 /**
919 * Like tuple_element, but returns @c _No_tuple_element when
920 * tuple_element would return an error.
921 */
922 template<int __i, typename _Tuple>
923 struct _Safe_tuple_element
924 : _Safe_tuple_element_impl<__i, _Tuple,
925 (__i >= 0 && __i < tuple_size<_Tuple>::value)>
926 { };
927
928 /**
929 * Maps an argument to bind() into an actual argument to the bound
930 * function object [TR1 3.6.3/5]. Only the first parameter should
931 * be specified: the rest are used to determine among the various
932 * implementations. Note that, although this class is a function
933 * object, it isn't entirely normal because it takes only two
934 * parameters regardless of the number of parameters passed to the
935 * bind expression. The first parameter is the bound argument and
936 * the second parameter is a tuple containing references to the
937 * rest of the arguments.
938 */
939 template<typename _Arg,
940 bool _IsBindExp = is_bind_expression<_Arg>::value,
941 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
942 class _Mu;
943
944 /**
945 * If the argument is reference_wrapper<_Tp>, returns the
946 * underlying reference. [TR1 3.6.3/5 bullet 1]
947 */
948 template<typename _Tp>
949 class _Mu<reference_wrapper<_Tp>, false, false>
950 {
951 public:
952 typedef _Tp& result_type;
953
954 /* Note: This won't actually work for const volatile
955 * reference_wrappers, because reference_wrapper::get() is const
956 * but not volatile-qualified. This might be a defect in the TR.
957 */
958 template<typename _CVRef, typename _Tuple>
959 result_type
960 operator()(_CVRef& __arg, _Tuple&) const volatile
961 { return __arg.get(); }
962 };
963
964 /**
965 * If the argument is a bind expression, we invoke the underlying
966 * function object with the same cv-qualifiers as we are given and
967 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
968 */
969 template<typename _Arg>
970 class _Mu<_Arg, true, false>
971 {
972 public:
973 template<typename _CVArg, typename... _Args>
974 auto
975 operator()(_CVArg& __arg,
976 tuple<_Args...>& __tuple) const volatile
977 -> decltype(__arg(declval<_Args>()...))
978 {
979 // Construct an index tuple and forward to __call
980 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
981 _Indexes;
982 return this->__call(__arg, __tuple, _Indexes());
983 }
984
985 private:
986 // Invokes the underlying function object __arg by unpacking all
987 // of the arguments in the tuple.
988 template<typename _CVArg, typename... _Args, int... _Indexes>
989 auto
990 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
991 const _Index_tuple<_Indexes...>&) const volatile
992 -> decltype(__arg(declval<_Args>()...))
993 {
994 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
995 }
996 };
997
998 /**
999 * If the argument is a placeholder for the Nth argument, returns
1000 * a reference to the Nth argument to the bind function object.
1001 * [TR1 3.6.3/5 bullet 3]
1002 */
1003 template<typename _Arg>
1004 class _Mu<_Arg, false, true>
1005 {
1006 public:
1007 template<typename _Signature> class result;
1008
1009 template<typename _CVMu, typename _CVArg, typename _Tuple>
1010 class result<_CVMu(_CVArg, _Tuple)>
1011 {
1012 // Add a reference, if it hasn't already been done for us.
1013 // This allows us to be a little bit sloppy in constructing
1014 // the tuple that we pass to result_of<...>.
1015 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1016 - 1), _Tuple>::type
1017 __base_type;
1018
1019 public:
1020 typedef typename add_rvalue_reference<__base_type>::type type;
1021 };
1022
1023 template<typename _Tuple>
1024 typename result<_Mu(_Arg, _Tuple)>::type
1025 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1026 {
1027 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
1028 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
1029 }
1030 };
1031
1032 /**
1033 * If the argument is just a value, returns a reference to that
1034 * value. The cv-qualifiers on the reference are the same as the
1035 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1036 */
1037 template<typename _Arg>
1038 class _Mu<_Arg, false, false>
1039 {
1040 public:
1041 template<typename _Signature> struct result;
1042
1043 template<typename _CVMu, typename _CVArg, typename _Tuple>
1044 struct result<_CVMu(_CVArg, _Tuple)>
1045 {
1046 typedef typename add_lvalue_reference<_CVArg>::type type;
1047 };
1048
1049 // Pick up the cv-qualifiers of the argument
1050 template<typename _CVArg, typename _Tuple>
1051 _CVArg&&
1052 operator()(_CVArg&& __arg, _Tuple&) const volatile
1053 { return std::forward<_CVArg>(__arg); }
1054 };
1055
1056 /**
1057 * Maps member pointers into instances of _Mem_fn but leaves all
1058 * other function objects untouched. Used by tr1::bind(). The
1059 * primary template handles the non--member-pointer case.
1060 */
1061 template<typename _Tp>
1062 struct _Maybe_wrap_member_pointer
1063 {
1064 typedef _Tp type;
1065
1066 static const _Tp&
1067 __do_wrap(const _Tp& __x)
1068 { return __x; }
1069
1070 static _Tp&&
1071 __do_wrap(_Tp&& __x)
1072 { return static_cast<_Tp&&>(__x); }
1073 };
1074
1075 /**
1076 * Maps member pointers into instances of _Mem_fn but leaves all
1077 * other function objects untouched. Used by tr1::bind(). This
1078 * partial specialization handles the member pointer case.
1079 */
1080 template<typename _Tp, typename _Class>
1081 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1082 {
1083 typedef _Mem_fn<_Tp _Class::*> type;
1084
1085 static type
1086 __do_wrap(_Tp _Class::* __pm)
1087 { return type(__pm); }
1088 };
1089
1090 // Specialization needed to prevent "forming reference to void" errors when
1091 // bind<void>() is called, because argument deduction instantiates
1092 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1093 // SFINAE applies.
1094 template<>
1095 struct _Maybe_wrap_member_pointer<void>
1096 {
1097 typedef void type;
1098 };
1099
1100 // std::get<I> for volatile-qualified tuples
1101 template<size_t _Ind, typename... _Tp>
1102 inline auto
1103 __volget(volatile tuple<_Tp...>& __tuple)
1104 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
1105 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1106
1107 // std::get<I> for const-volatile-qualified tuples
1108 template<size_t _Ind, typename... _Tp>
1109 inline auto
1110 __volget(const volatile tuple<_Tp...>& __tuple)
1111 -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
1112 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1113
1114 /// Type of the function object returned from bind().
1115 template<typename _Signature>
1116 struct _Bind;
1117
1118 template<typename _Functor, typename... _Bound_args>
1119 class _Bind<_Functor(_Bound_args...)>
1120 : public _Weak_result_type<_Functor>
1121 {
1122 typedef _Bind __self_type;
1123 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1124 _Bound_indexes;
1125
1126 _Functor _M_f;
1127 tuple<_Bound_args...> _M_bound_args;
1128
1129 // Call unqualified
1130 template<typename _Result, typename... _Args, int... _Indexes>
1131 _Result
1132 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1133 {
1134 return _M_f(_Mu<_Bound_args>()
1135 (get<_Indexes>(_M_bound_args), __args)...);
1136 }
1137
1138 // Call as const
1139 template<typename _Result, typename... _Args, int... _Indexes>
1140 _Result
1141 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1142 {
1143 return _M_f(_Mu<_Bound_args>()
1144 (get<_Indexes>(_M_bound_args), __args)...);
1145 }
1146
1147 // Call as volatile
1148 template<typename _Result, typename... _Args, int... _Indexes>
1149 _Result
1150 __call_v(tuple<_Args...>&& __args,
1151 _Index_tuple<_Indexes...>) volatile
1152 {
1153 return _M_f(_Mu<_Bound_args>()
1154 (__volget<_Indexes>(_M_bound_args), __args)...);
1155 }
1156
1157 // Call as const volatile
1158 template<typename _Result, typename... _Args, int... _Indexes>
1159 _Result
1160 __call_c_v(tuple<_Args...>&& __args,
1161 _Index_tuple<_Indexes...>) const volatile
1162 {
1163 return _M_f(_Mu<_Bound_args>()
1164 (__volget<_Indexes>(_M_bound_args), __args)...);
1165 }
1166
1167 public:
1168 template<typename... _Args>
1169 explicit _Bind(const _Functor& __f, _Args&&... __args)
1170 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1171 { }
1172
1173 template<typename... _Args>
1174 explicit _Bind(_Functor&& __f, _Args&&... __args)
1175 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1176 { }
1177
1178 _Bind(const _Bind&) = default;
1179
1180 _Bind(_Bind&& __b)
1181 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1182 { }
1183
1184 // Call unqualified
1185 template<typename... _Args, typename _Result
1186 = decltype( std::declval<_Functor>()(
1187 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1188 std::declval<tuple<_Args...>&>() )... ) )>
1189 _Result
1190 operator()(_Args&&... __args)
1191 {
1192 return this->__call<_Result>(
1193 std::forward_as_tuple(std::forward<_Args>(__args)...),
1194 _Bound_indexes());
1195 }
1196
1197 // Call as const
1198 template<typename... _Args, typename _Result
1199 = decltype( std::declval<const _Functor>()(
1200 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1201 std::declval<tuple<_Args...>&>() )... ) )>
1202 _Result
1203 operator()(_Args&&... __args) const
1204 {
1205 return this->__call_c<_Result>(
1206 std::forward_as_tuple(std::forward<_Args>(__args)...),
1207 _Bound_indexes());
1208 }
1209
1210 // Call as volatile
1211 template<typename... _Args, typename _Result
1212 = decltype( std::declval<volatile _Functor>()(
1213 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1214 std::declval<tuple<_Args...>&>() )... ) )>
1215 _Result
1216 operator()(_Args&&... __args) volatile
1217 {
1218 return this->__call_v<_Result>(
1219 std::forward_as_tuple(std::forward<_Args>(__args)...),
1220 _Bound_indexes());
1221 }
1222
1223 // Call as const volatile
1224 template<typename... _Args, typename _Result
1225 = decltype( std::declval<const volatile _Functor>()(
1226 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1227 std::declval<tuple<_Args...>&>() )... ) )>
1228 _Result
1229 operator()(_Args&&... __args) const volatile
1230 {
1231 return this->__call_c_v<_Result>(
1232 std::forward_as_tuple(std::forward<_Args>(__args)...),
1233 _Bound_indexes());
1234 }
1235 };
1236
1237 /// Type of the function object returned from bind<R>().
1238 template<typename _Result, typename _Signature>
1239 struct _Bind_result;
1240
1241 template<typename _Result, typename _Functor, typename... _Bound_args>
1242 class _Bind_result<_Result, _Functor(_Bound_args...)>
1243 {
1244 typedef _Bind_result __self_type;
1245 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1246 _Bound_indexes;
1247
1248 _Functor _M_f;
1249 tuple<_Bound_args...> _M_bound_args;
1250
1251 // sfinae types
1252 template<typename _Res>
1253 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1254 template<typename _Res>
1255 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1256
1257 // Call unqualified
1258 template<typename _Res, typename... _Args, int... _Indexes>
1259 _Result
1260 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1261 typename __disable_if_void<_Res>::type = 0)
1262 {
1263 return _M_f(_Mu<_Bound_args>()
1264 (get<_Indexes>(_M_bound_args), __args)...);
1265 }
1266
1267 // Call unqualified, return void
1268 template<typename _Res, typename... _Args, int... _Indexes>
1269 void
1270 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1271 typename __enable_if_void<_Res>::type = 0)
1272 {
1273 _M_f(_Mu<_Bound_args>()
1274 (get<_Indexes>(_M_bound_args), __args)...);
1275 }
1276
1277 // Call as const
1278 template<typename _Res, typename... _Args, int... _Indexes>
1279 _Result
1280 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1281 typename __disable_if_void<_Res>::type = 0) const
1282 {
1283 return _M_f(_Mu<_Bound_args>()
1284 (get<_Indexes>(_M_bound_args), __args)...);
1285 }
1286
1287 // Call as const, return void
1288 template<typename _Res, typename... _Args, int... _Indexes>
1289 void
1290 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1291 typename __enable_if_void<_Res>::type = 0) const
1292 {
1293 _M_f(_Mu<_Bound_args>()
1294 (get<_Indexes>(_M_bound_args), __args)...);
1295 }
1296
1297 // Call as volatile
1298 template<typename _Res, typename... _Args, int... _Indexes>
1299 _Result
1300 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1301 typename __disable_if_void<_Res>::type = 0) volatile
1302 {
1303 return _M_f(_Mu<_Bound_args>()
1304 (__volget<_Indexes>(_M_bound_args), __args)...);
1305 }
1306
1307 // Call as volatile, return void
1308 template<typename _Res, typename... _Args, int... _Indexes>
1309 void
1310 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1311 typename __enable_if_void<_Res>::type = 0) volatile
1312 {
1313 _M_f(_Mu<_Bound_args>()
1314 (__volget<_Indexes>(_M_bound_args), __args)...);
1315 }
1316
1317 // Call as const volatile
1318 template<typename _Res, typename... _Args, int... _Indexes>
1319 _Result
1320 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1321 typename __disable_if_void<_Res>::type = 0) const volatile
1322 {
1323 return _M_f(_Mu<_Bound_args>()
1324 (__volget<_Indexes>(_M_bound_args), __args)...);
1325 }
1326
1327 // Call as const volatile, return void
1328 template<typename _Res, typename... _Args, int... _Indexes>
1329 void
1330 __call(tuple<_Args...>&& __args,
1331 _Index_tuple<_Indexes...>,
1332 typename __enable_if_void<_Res>::type = 0) const volatile
1333 {
1334 _M_f(_Mu<_Bound_args>()
1335 (__volget<_Indexes>(_M_bound_args), __args)...);
1336 }
1337
1338 public:
1339 typedef _Result result_type;
1340
1341 template<typename... _Args>
1342 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1343 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1344 { }
1345
1346 template<typename... _Args>
1347 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1348 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1349 { }
1350
1351 _Bind_result(const _Bind_result&) = default;
1352
1353 _Bind_result(_Bind_result&& __b)
1354 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1355 { }
1356
1357 // Call unqualified
1358 template<typename... _Args>
1359 result_type
1360 operator()(_Args&&... __args)
1361 {
1362 return this->__call<_Result>(
1363 std::forward_as_tuple(std::forward<_Args>(__args)...),
1364 _Bound_indexes());
1365 }
1366
1367 // Call as const
1368 template<typename... _Args>
1369 result_type
1370 operator()(_Args&&... __args) const
1371 {
1372 return this->__call<_Result>(
1373 std::forward_as_tuple(std::forward<_Args>(__args)...),
1374 _Bound_indexes());
1375 }
1376
1377 // Call as volatile
1378 template<typename... _Args>
1379 result_type
1380 operator()(_Args&&... __args) volatile
1381 {
1382 return this->__call<_Result>(
1383 std::forward_as_tuple(std::forward<_Args>(__args)...),
1384 _Bound_indexes());
1385 }
1386
1387 // Call as const volatile
1388 template<typename... _Args>
1389 result_type
1390 operator()(_Args&&... __args) const volatile
1391 {
1392 return this->__call<_Result>(
1393 std::forward_as_tuple(std::forward<_Args>(__args)...),
1394 _Bound_indexes());
1395 }
1396 };
1397
1398 /**
1399 * @brief Class template _Bind is always a bind expression.
1400 * @ingroup binders
1401 */
1402 template<typename _Signature>
1403 struct is_bind_expression<_Bind<_Signature> >
1404 : public true_type { };
1405
1406 /**
1407 * @brief Class template _Bind is always a bind expression.
1408 * @ingroup binders
1409 */
1410 template<typename _Result, typename _Signature>
1411 struct is_bind_expression<_Bind_result<_Result, _Signature> >
1412 : public true_type { };
1413
1414 template<typename _Functor, typename... _ArgTypes>
1415 struct _Bind_helper
1416 {
1417 typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
1418 __maybe_type;
1419 typedef typename __maybe_type::type __functor_type;
1420 typedef _Bind<__functor_type(typename decay<_ArgTypes>::type...)> type;
1421 };
1422
1423 /**
1424 * @brief Function template for std::bind.
1425 * @ingroup binders
1426 */
1427 template<typename _Functor, typename... _ArgTypes>
1428 inline
1429 typename _Bind_helper<_Functor, _ArgTypes...>::type
1430 bind(_Functor&& __f, _ArgTypes&&... __args)
1431 {
1432 typedef _Bind_helper<_Functor, _ArgTypes...> __helper_type;
1433 typedef typename __helper_type::__maybe_type __maybe_type;
1434 typedef typename __helper_type::type __result_type;
1435 return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
1436 std::forward<_ArgTypes>(__args)...);
1437 }
1438
1439 template<typename _Result, typename _Functor, typename... _ArgTypes>
1440 struct _Bindres_helper
1441 {
1442 typedef _Maybe_wrap_member_pointer<typename decay<_Functor>::type>
1443 __maybe_type;
1444 typedef typename __maybe_type::type __functor_type;
1445 typedef _Bind_result<_Result,
1446 __functor_type(typename decay<_ArgTypes>::type...)>
1447 type;
1448 };
1449
1450 /**
1451 * @brief Function template for std::bind<R>.
1452 * @ingroup binders
1453 */
1454 template<typename _Result, typename _Functor, typename... _ArgTypes>
1455 inline
1456 typename _Bindres_helper<_Result, _Functor, _ArgTypes...>::type
1457 bind(_Functor&& __f, _ArgTypes&&... __args)
1458 {
1459 typedef _Bindres_helper<_Result, _Functor, _ArgTypes...> __helper_type;
1460 typedef typename __helper_type::__maybe_type __maybe_type;
1461 typedef typename __helper_type::type __result_type;
1462 return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
1463 std::forward<_ArgTypes>(__args)...);
1464 }
1465
1466 /**
1467 * @brief Exception class thrown when class template function's
1468 * operator() is called with an empty target.
1469 * @ingroup exceptions
1470 */
1471 class bad_function_call : public std::exception { };
1472
1473 /**
1474 * Trait identifying "location-invariant" types, meaning that the
1475 * address of the object (or any of its members) will not escape.
1476 * Also implies a trivial copy constructor and assignment operator.
1477 */
1478 template<typename _Tp>
1479 struct __is_location_invariant
1480 : integral_constant<bool, (is_pointer<_Tp>::value
1481 || is_member_pointer<_Tp>::value)>
1482 { };
1483
1484 class _Undefined_class;
1485
1486 union _Nocopy_types
1487 {
1488 void* _M_object;
1489 const void* _M_const_object;
1490 void (*_M_function_pointer)();
1491 void (_Undefined_class::*_M_member_pointer)();
1492 };
1493
1494 union _Any_data
1495 {
1496 void* _M_access() { return &_M_pod_data[0]; }
1497 const void* _M_access() const { return &_M_pod_data[0]; }
1498
1499 template<typename _Tp>
1500 _Tp&
1501 _M_access()
1502 { return *static_cast<_Tp*>(_M_access()); }
1503
1504 template<typename _Tp>
1505 const _Tp&
1506 _M_access() const
1507 { return *static_cast<const _Tp*>(_M_access()); }
1508
1509 _Nocopy_types _M_unused;
1510 char _M_pod_data[sizeof(_Nocopy_types)];
1511 };
1512
1513 enum _Manager_operation
1514 {
1515 __get_type_info,
1516 __get_functor_ptr,
1517 __clone_functor,
1518 __destroy_functor
1519 };
1520
1521 // Simple type wrapper that helps avoid annoying const problems
1522 // when casting between void pointers and pointers-to-pointers.
1523 template<typename _Tp>
1524 struct _Simple_type_wrapper
1525 {
1526 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1527
1528 _Tp __value;
1529 };
1530
1531 template<typename _Tp>
1532 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1533 : __is_location_invariant<_Tp>
1534 { };
1535
1536 // Converts a reference to a function object into a callable
1537 // function object.
1538 template<typename _Functor>
1539 inline _Functor&
1540 __callable_functor(_Functor& __f)
1541 { return __f; }
1542
1543 template<typename _Member, typename _Class>
1544 inline _Mem_fn<_Member _Class::*>
1545 __callable_functor(_Member _Class::* &__p)
1546 { return mem_fn(__p); }
1547
1548 template<typename _Member, typename _Class>
1549 inline _Mem_fn<_Member _Class::*>
1550 __callable_functor(_Member _Class::* const &__p)
1551 { return mem_fn(__p); }
1552
1553 template<typename _Signature>
1554 class function;
1555
1556 /// Base class of all polymorphic function object wrappers.
1557 class _Function_base
1558 {
1559 public:
1560 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1561 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1562
1563 template<typename _Functor>
1564 class _Base_manager
1565 {
1566 protected:
1567 static const bool __stored_locally =
1568 (__is_location_invariant<_Functor>::value
1569 && sizeof(_Functor) <= _M_max_size
1570 && __alignof__(_Functor) <= _M_max_align
1571 && (_M_max_align % __alignof__(_Functor) == 0));
1572
1573 typedef integral_constant<bool, __stored_locally> _Local_storage;
1574
1575 // Retrieve a pointer to the function object
1576 static _Functor*
1577 _M_get_pointer(const _Any_data& __source)
1578 {
1579 const _Functor* __ptr =
1580 __stored_locally? &__source._M_access<_Functor>()
1581 /* have stored a pointer */ : __source._M_access<_Functor*>();
1582 return const_cast<_Functor*>(__ptr);
1583 }
1584
1585 // Clone a location-invariant function object that fits within
1586 // an _Any_data structure.
1587 static void
1588 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1589 {
1590 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1591 }
1592
1593 // Clone a function object that is not location-invariant or
1594 // that cannot fit into an _Any_data structure.
1595 static void
1596 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1597 {
1598 __dest._M_access<_Functor*>() =
1599 new _Functor(*__source._M_access<_Functor*>());
1600 }
1601
1602 // Destroying a location-invariant object may still require
1603 // destruction.
1604 static void
1605 _M_destroy(_Any_data& __victim, true_type)
1606 {
1607 __victim._M_access<_Functor>().~_Functor();
1608 }
1609
1610 // Destroying an object located on the heap.
1611 static void
1612 _M_destroy(_Any_data& __victim, false_type)
1613 {
1614 delete __victim._M_access<_Functor*>();
1615 }
1616
1617 public:
1618 static bool
1619 _M_manager(_Any_data& __dest, const _Any_data& __source,
1620 _Manager_operation __op)
1621 {
1622 switch (__op)
1623 {
1624 #ifdef __GXX_RTTI
1625 case __get_type_info:
1626 __dest._M_access<const type_info*>() = &typeid(_Functor);
1627 break;
1628 #endif
1629 case __get_functor_ptr:
1630 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1631 break;
1632
1633 case __clone_functor:
1634 _M_clone(__dest, __source, _Local_storage());
1635 break;
1636
1637 case __destroy_functor:
1638 _M_destroy(__dest, _Local_storage());
1639 break;
1640 }
1641 return false;
1642 }
1643
1644 static void
1645 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1646 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1647
1648 template<typename _Signature>
1649 static bool
1650 _M_not_empty_function(const function<_Signature>& __f)
1651 { return static_cast<bool>(__f); }
1652
1653 template<typename _Tp>
1654 static bool
1655 _M_not_empty_function(const _Tp*& __fp)
1656 { return __fp; }
1657
1658 template<typename _Class, typename _Tp>
1659 static bool
1660 _M_not_empty_function(_Tp _Class::* const& __mp)
1661 { return __mp; }
1662
1663 template<typename _Tp>
1664 static bool
1665 _M_not_empty_function(const _Tp&)
1666 { return true; }
1667
1668 private:
1669 static void
1670 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1671 { new (__functor._M_access()) _Functor(std::move(__f)); }
1672
1673 static void
1674 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1675 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
1676 };
1677
1678 template<typename _Functor>
1679 class _Ref_manager : public _Base_manager<_Functor*>
1680 {
1681 typedef _Function_base::_Base_manager<_Functor*> _Base;
1682
1683 public:
1684 static bool
1685 _M_manager(_Any_data& __dest, const _Any_data& __source,
1686 _Manager_operation __op)
1687 {
1688 switch (__op)
1689 {
1690 #ifdef __GXX_RTTI
1691 case __get_type_info:
1692 __dest._M_access<const type_info*>() = &typeid(_Functor);
1693 break;
1694 #endif
1695 case __get_functor_ptr:
1696 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1697 return is_const<_Functor>::value;
1698 break;
1699
1700 default:
1701 _Base::_M_manager(__dest, __source, __op);
1702 }
1703 return false;
1704 }
1705
1706 static void
1707 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1708 {
1709 // TBD: Use address_of function instead.
1710 _Base::_M_init_functor(__functor, &__f.get());
1711 }
1712 };
1713
1714 _Function_base() : _M_manager(0) { }
1715
1716 ~_Function_base()
1717 {
1718 if (_M_manager)
1719 _M_manager(_M_functor, _M_functor, __destroy_functor);
1720 }
1721
1722
1723 bool _M_empty() const { return !_M_manager; }
1724
1725 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1726 _Manager_operation);
1727
1728 _Any_data _M_functor;
1729 _Manager_type _M_manager;
1730 };
1731
1732 template<typename _Signature, typename _Functor>
1733 class _Function_handler;
1734
1735 template<typename _Res, typename _Functor, typename... _ArgTypes>
1736 class _Function_handler<_Res(_ArgTypes...), _Functor>
1737 : public _Function_base::_Base_manager<_Functor>
1738 {
1739 typedef _Function_base::_Base_manager<_Functor> _Base;
1740
1741 public:
1742 static _Res
1743 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1744 {
1745 return (*_Base::_M_get_pointer(__functor))(
1746 std::forward<_ArgTypes>(__args)...);
1747 }
1748 };
1749
1750 template<typename _Functor, typename... _ArgTypes>
1751 class _Function_handler<void(_ArgTypes...), _Functor>
1752 : public _Function_base::_Base_manager<_Functor>
1753 {
1754 typedef _Function_base::_Base_manager<_Functor> _Base;
1755
1756 public:
1757 static void
1758 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1759 {
1760 (*_Base::_M_get_pointer(__functor))(
1761 std::forward<_ArgTypes>(__args)...);
1762 }
1763 };
1764
1765 template<typename _Res, typename _Functor, typename... _ArgTypes>
1766 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1767 : public _Function_base::_Ref_manager<_Functor>
1768 {
1769 typedef _Function_base::_Ref_manager<_Functor> _Base;
1770
1771 public:
1772 static _Res
1773 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1774 {
1775 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1776 std::forward<_ArgTypes>(__args)...);
1777 }
1778 };
1779
1780 template<typename _Functor, typename... _ArgTypes>
1781 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1782 : public _Function_base::_Ref_manager<_Functor>
1783 {
1784 typedef _Function_base::_Ref_manager<_Functor> _Base;
1785
1786 public:
1787 static void
1788 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1789 {
1790 __callable_functor(**_Base::_M_get_pointer(__functor))(
1791 std::forward<_ArgTypes>(__args)...);
1792 }
1793 };
1794
1795 template<typename _Class, typename _Member, typename _Res,
1796 typename... _ArgTypes>
1797 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1798 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1799 {
1800 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1801 _Base;
1802
1803 public:
1804 static _Res
1805 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1806 {
1807 return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1808 std::forward<_ArgTypes>(__args)...);
1809 }
1810 };
1811
1812 template<typename _Class, typename _Member, typename... _ArgTypes>
1813 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1814 : public _Function_base::_Base_manager<
1815 _Simple_type_wrapper< _Member _Class::* > >
1816 {
1817 typedef _Member _Class::* _Functor;
1818 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1819 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1820
1821 public:
1822 static bool
1823 _M_manager(_Any_data& __dest, const _Any_data& __source,
1824 _Manager_operation __op)
1825 {
1826 switch (__op)
1827 {
1828 #ifdef __GXX_RTTI
1829 case __get_type_info:
1830 __dest._M_access<const type_info*>() = &typeid(_Functor);
1831 break;
1832 #endif
1833 case __get_functor_ptr:
1834 __dest._M_access<_Functor*>() =
1835 &_Base::_M_get_pointer(__source)->__value;
1836 break;
1837
1838 default:
1839 _Base::_M_manager(__dest, __source, __op);
1840 }
1841 return false;
1842 }
1843
1844 static void
1845 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1846 {
1847 mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1848 std::forward<_ArgTypes>(__args)...);
1849 }
1850 };
1851
1852 /**
1853 * @brief Primary class template for std::function.
1854 * @ingroup functors
1855 *
1856 * Polymorphic function wrapper.
1857 */
1858 template<typename _Res, typename... _ArgTypes>
1859 class function<_Res(_ArgTypes...)>
1860 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
1861 private _Function_base
1862 {
1863 typedef _Res _Signature_type(_ArgTypes...);
1864
1865 struct _Useless { };
1866
1867 public:
1868 typedef _Res result_type;
1869
1870 // [3.7.2.1] construct/copy/destroy
1871
1872 /**
1873 * @brief Default construct creates an empty function call wrapper.
1874 * @post @c !(bool)*this
1875 */
1876 function() : _Function_base() { }
1877
1878 /**
1879 * @brief Creates an empty function call wrapper.
1880 * @post @c !(bool)*this
1881 */
1882 function(nullptr_t) : _Function_base() { }
1883
1884 /**
1885 * @brief %Function copy constructor.
1886 * @param x A %function object with identical call signature.
1887 * @post @c (bool)*this == (bool)x
1888 *
1889 * The newly-created %function contains a copy of the target of @a
1890 * x (if it has one).
1891 */
1892 function(const function& __x);
1893
1894 /**
1895 * @brief %Function move constructor.
1896 * @param x A %function object rvalue with identical call signature.
1897 *
1898 * The newly-created %function contains the target of @a x
1899 * (if it has one).
1900 */
1901 function(function&& __x) : _Function_base()
1902 {
1903 __x.swap(*this);
1904 }
1905
1906 // TODO: needs allocator_arg_t
1907
1908 /**
1909 * @brief Builds a %function that targets a copy of the incoming
1910 * function object.
1911 * @param f A %function object that is callable with parameters of
1912 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1913 * to @c Res.
1914 *
1915 * The newly-created %function object will target a copy of @a
1916 * f. If @a f is @c reference_wrapper<F>, then this function
1917 * object will contain a reference to the function object @c
1918 * f.get(). If @a f is a NULL function pointer or NULL
1919 * pointer-to-member, the newly-created object will be empty.
1920 *
1921 * If @a f is a non-NULL function pointer or an object of type @c
1922 * reference_wrapper<F>, this function will not throw.
1923 */
1924 template<typename _Functor>
1925 function(_Functor __f,
1926 typename enable_if<
1927 !is_integral<_Functor>::value, _Useless>::type
1928 = _Useless());
1929
1930 /**
1931 * @brief %Function assignment operator.
1932 * @param x A %function with identical call signature.
1933 * @post @c (bool)*this == (bool)x
1934 * @returns @c *this
1935 *
1936 * The target of @a x is copied to @c *this. If @a x has no
1937 * target, then @c *this will be empty.
1938 *
1939 * If @a x targets a function pointer or a reference to a function
1940 * object, then this operation will not throw an %exception.
1941 */
1942 function&
1943 operator=(const function& __x)
1944 {
1945 function(__x).swap(*this);
1946 return *this;
1947 }
1948
1949 /**
1950 * @brief %Function move-assignment operator.
1951 * @param x A %function rvalue with identical call signature.
1952 * @returns @c *this
1953 *
1954 * The target of @a x is moved to @c *this. If @a x has no
1955 * target, then @c *this will be empty.
1956 *
1957 * If @a x targets a function pointer or a reference to a function
1958 * object, then this operation will not throw an %exception.
1959 */
1960 function&
1961 operator=(function&& __x)
1962 {
1963 function(std::move(__x)).swap(*this);
1964 return *this;
1965 }
1966
1967 /**
1968 * @brief %Function assignment to zero.
1969 * @post @c !(bool)*this
1970 * @returns @c *this
1971 *
1972 * The target of @c *this is deallocated, leaving it empty.
1973 */
1974 function&
1975 operator=(nullptr_t)
1976 {
1977 if (_M_manager)
1978 {
1979 _M_manager(_M_functor, _M_functor, __destroy_functor);
1980 _M_manager = 0;
1981 _M_invoker = 0;
1982 }
1983 return *this;
1984 }
1985
1986 /**
1987 * @brief %Function assignment to a new target.
1988 * @param f A %function object that is callable with parameters of
1989 * type @c T1, @c T2, ..., @c TN and returns a value convertible
1990 * to @c Res.
1991 * @return @c *this
1992 *
1993 * This %function object wrapper will target a copy of @a
1994 * f. If @a f is @c reference_wrapper<F>, then this function
1995 * object will contain a reference to the function object @c
1996 * f.get(). If @a f is a NULL function pointer or NULL
1997 * pointer-to-member, @c this object will be empty.
1998 *
1999 * If @a f is a non-NULL function pointer or an object of type @c
2000 * reference_wrapper<F>, this function will not throw.
2001 */
2002 template<typename _Functor>
2003 typename enable_if<!is_integral<_Functor>::value, function&>::type
2004 operator=(_Functor&& __f)
2005 {
2006 function(std::forward<_Functor>(__f)).swap(*this);
2007 return *this;
2008 }
2009
2010 /// @overload
2011 template<typename _Functor>
2012 typename enable_if<!is_integral<_Functor>::value, function&>::type
2013 operator=(reference_wrapper<_Functor> __f)
2014 {
2015 function(__f).swap(*this);
2016 return *this;
2017 }
2018
2019 // [3.7.2.2] function modifiers
2020
2021 /**
2022 * @brief Swap the targets of two %function objects.
2023 * @param f A %function with identical call signature.
2024 *
2025 * Swap the targets of @c this function object and @a f. This
2026 * function will not throw an %exception.
2027 */
2028 void swap(function& __x)
2029 {
2030 std::swap(_M_functor, __x._M_functor);
2031 std::swap(_M_manager, __x._M_manager);
2032 std::swap(_M_invoker, __x._M_invoker);
2033 }
2034
2035 // TODO: needs allocator_arg_t
2036 /*
2037 template<typename _Functor, typename _Alloc>
2038 void
2039 assign(_Functor&& __f, const _Alloc& __a)
2040 {
2041 function(allocator_arg, __a,
2042 std::forward<_Functor>(__f)).swap(*this);
2043 }
2044 */
2045
2046 // [3.7.2.3] function capacity
2047
2048 /**
2049 * @brief Determine if the %function wrapper has a target.
2050 *
2051 * @return @c true when this %function object contains a target,
2052 * or @c false when it is empty.
2053 *
2054 * This function will not throw an %exception.
2055 */
2056 explicit operator bool() const
2057 { return !_M_empty(); }
2058
2059 // [3.7.2.4] function invocation
2060
2061 /**
2062 * @brief Invokes the function targeted by @c *this.
2063 * @returns the result of the target.
2064 * @throws bad_function_call when @c !(bool)*this
2065 *
2066 * The function call operator invokes the target function object
2067 * stored by @c this.
2068 */
2069 _Res operator()(_ArgTypes... __args) const;
2070
2071 #ifdef __GXX_RTTI
2072 // [3.7.2.5] function target access
2073 /**
2074 * @brief Determine the type of the target of this function object
2075 * wrapper.
2076 *
2077 * @returns the type identifier of the target function object, or
2078 * @c typeid(void) if @c !(bool)*this.
2079 *
2080 * This function will not throw an %exception.
2081 */
2082 const type_info& target_type() const;
2083
2084 /**
2085 * @brief Access the stored target function object.
2086 *
2087 * @return Returns a pointer to the stored target function object,
2088 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2089 * pointer.
2090 *
2091 * This function will not throw an %exception.
2092 */
2093 template<typename _Functor> _Functor* target();
2094
2095 /// @overload
2096 template<typename _Functor> const _Functor* target() const;
2097 #endif
2098
2099 // deleted overloads
2100 template<typename _Res2, typename... _ArgTypes2>
2101 void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
2102 template<typename _Res2, typename... _ArgTypes2>
2103 void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
2104
2105 private:
2106 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2107 _Invoker_type _M_invoker;
2108 };
2109
2110 // Out-of-line member definitions.
2111 template<typename _Res, typename... _ArgTypes>
2112 function<_Res(_ArgTypes...)>::
2113 function(const function& __x)
2114 : _Function_base()
2115 {
2116 if (static_cast<bool>(__x))
2117 {
2118 _M_invoker = __x._M_invoker;
2119 _M_manager = __x._M_manager;
2120 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2121 }
2122 }
2123
2124 template<typename _Res, typename... _ArgTypes>
2125 template<typename _Functor>
2126 function<_Res(_ArgTypes...)>::
2127 function(_Functor __f,
2128 typename enable_if<
2129 !is_integral<_Functor>::value, _Useless>::type)
2130 : _Function_base()
2131 {
2132 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2133
2134 if (_My_handler::_M_not_empty_function(__f))
2135 {
2136 _M_invoker = &_My_handler::_M_invoke;
2137 _M_manager = &_My_handler::_M_manager;
2138 _My_handler::_M_init_functor(_M_functor, std::move(__f));
2139 }
2140 }
2141
2142 template<typename _Res, typename... _ArgTypes>
2143 _Res
2144 function<_Res(_ArgTypes...)>::
2145 operator()(_ArgTypes... __args) const
2146 {
2147 if (_M_empty())
2148 __throw_bad_function_call();
2149 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2150 }
2151
2152 #ifdef __GXX_RTTI
2153 template<typename _Res, typename... _ArgTypes>
2154 const type_info&
2155 function<_Res(_ArgTypes...)>::
2156 target_type() const
2157 {
2158 if (_M_manager)
2159 {
2160 _Any_data __typeinfo_result;
2161 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2162 return *__typeinfo_result._M_access<const type_info*>();
2163 }
2164 else
2165 return typeid(void);
2166 }
2167
2168 template<typename _Res, typename... _ArgTypes>
2169 template<typename _Functor>
2170 _Functor*
2171 function<_Res(_ArgTypes...)>::
2172 target()
2173 {
2174 if (typeid(_Functor) == target_type() && _M_manager)
2175 {
2176 _Any_data __ptr;
2177 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2178 && !is_const<_Functor>::value)
2179 return 0;
2180 else
2181 return __ptr._M_access<_Functor*>();
2182 }
2183 else
2184 return 0;
2185 }
2186
2187 template<typename _Res, typename... _ArgTypes>
2188 template<typename _Functor>
2189 const _Functor*
2190 function<_Res(_ArgTypes...)>::
2191 target() const
2192 {
2193 if (typeid(_Functor) == target_type() && _M_manager)
2194 {
2195 _Any_data __ptr;
2196 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2197 return __ptr._M_access<const _Functor*>();
2198 }
2199 else
2200 return 0;
2201 }
2202 #endif
2203
2204 // [20.7.15.2.6] null pointer comparisons
2205
2206 /**
2207 * @brief Compares a polymorphic function object wrapper against 0
2208 * (the NULL pointer).
2209 * @returns @c true if the wrapper has no target, @c false otherwise
2210 *
2211 * This function will not throw an %exception.
2212 */
2213 template<typename _Res, typename... _Args>
2214 inline bool
2215 operator==(const function<_Res(_Args...)>& __f, nullptr_t)
2216 { return !static_cast<bool>(__f); }
2217
2218 /// @overload
2219 template<typename _Res, typename... _Args>
2220 inline bool
2221 operator==(nullptr_t, const function<_Res(_Args...)>& __f)
2222 { return !static_cast<bool>(__f); }
2223
2224 /**
2225 * @brief Compares a polymorphic function object wrapper against 0
2226 * (the NULL pointer).
2227 * @returns @c false if the wrapper has no target, @c true otherwise
2228 *
2229 * This function will not throw an %exception.
2230 */
2231 template<typename _Res, typename... _Args>
2232 inline bool
2233 operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
2234 { return static_cast<bool>(__f); }
2235
2236 /// @overload
2237 template<typename _Res, typename... _Args>
2238 inline bool
2239 operator!=(nullptr_t, const function<_Res(_Args...)>& __f)
2240 { return static_cast<bool>(__f); }
2241
2242 // [20.7.15.2.7] specialized algorithms
2243
2244 /**
2245 * @brief Swap the targets of two polymorphic function object wrappers.
2246 *
2247 * This function will not throw an %exception.
2248 */
2249 template<typename _Res, typename... _Args>
2250 inline void
2251 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2252 { __x.swap(__y); }
2253 }
2254
2255 #endif // __GXX_EXPERIMENTAL_CXX0X__
2256
2257 #endif // _GLIBCXX_FUNCTIONAL