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