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