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