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