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