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