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