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