Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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