re PR libstdc++/49668 ([C++0x] std::thread does not forward its args as rvalues)
[gcc.git] / libstdc++-v3 / include / std / functional
index eb10b348ad1ced62c7516bfe7014f9e15f7f3084..df3f9ceb7b43248a94be5430368f6289e945b4e9 100644 (file)
@@ -1,7 +1,7 @@
 // <functional> -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
-// Free Software Foundation, Inc.
+// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
+// 2011 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
 #include <new>
 #include <tuple>
 #include <type_traits>
+#include <bits/functexcept.h>
 #include <bits/functional_hash.h>
-#include <ext/type_traits.h>
 
-namespace std
+namespace std _GLIBCXX_VISIBILITY(default)
 {
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
   template<typename _MemberPointer>
     class _Mem_fn;
+  template<typename _Tp, typename _Class>
+    _Mem_fn<_Tp _Class::*>
+    mem_fn(_Tp _Class::*);
 
-  /**
-   *  Actual implementation of _Has_result_type, which uses SFINAE to
-   *  determine if the type _Tp has a publicly-accessible member type
-   *  result_type.
-  */
-  template<typename _Tp>
-    class _Has_result_type_helper : __sfinae_types
-    {
-      template<typename _Up>
-        struct _Wrap_type
-       { };
-
-      template<typename _Up>
-        static __one __test(_Wrap_type<typename _Up::result_type>*);
-
-      template<typename _Up>
-        static __two __test(...);
-
-    public:
-      static const bool value = sizeof(__test<_Tp>(0)) == 1;
-    };
-
-  template<typename _Tp>
-    struct _Has_result_type
-    : integral_constant<bool,
-             _Has_result_type_helper<typename remove_cv<_Tp>::type>::value>
-    { };
+_GLIBCXX_HAS_NESTED_TYPE(result_type)
 
-  /**
-   *  
-  */
   /// If we have found a result_type, extract it.
   template<bool _Has_result_type, typename _Functor>
     struct _Maybe_get_result_type
@@ -101,9 +77,7 @@ namespace std
 
   template<typename _Functor>
     struct _Maybe_get_result_type<true, _Functor>
-    {
-      typedef typename _Functor::result_type result_type;
-    };
+    { typedef typename _Functor::result_type result_type; };
 
   /**
    *  Base class for any function object that has a weak result type, as
@@ -111,58 +85,97 @@ namespace std
   */
   template<typename _Functor>
     struct _Weak_result_type_impl
-    : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor>
-    {
-    };
+    : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
+    { };
 
   /// Retrieve the result type for a function type.
-  template<typename _Res, typename... _ArgTypes> 
+  template<typename _Res, typename... _ArgTypes>
     struct _Weak_result_type_impl<_Res(_ArgTypes...)>
-    {
-      typedef _Res result_type;
-    };
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(_ArgTypes......)>
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
+    { typedef _Res result_type; };
 
   /// Retrieve the result type for a function reference.
-  template<typename _Res, typename... _ArgTypes> 
+  template<typename _Res, typename... _ArgTypes>
     struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
-    {
-      typedef _Res result_type;
-    };
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
+    { typedef _Res result_type; };
 
   /// Retrieve the result type for a function pointer.
-  template<typename _Res, typename... _ArgTypes> 
+  template<typename _Res, typename... _ArgTypes>
     struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
-    {
-      typedef _Res result_type;
-    };
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
+    { typedef _Res result_type; };
 
-  /// Retrieve result type for a member function pointer. 
-  template<typename _Res, typename _Class, typename... _ArgTypes> 
+  /// Retrieve result type for a member function pointer.
+  template<typename _Res, typename _Class, typename... _ArgTypes>
     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
-    {
-      typedef _Res result_type;
-    };
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename _Class, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
+    { typedef _Res result_type; };
 
-  /// Retrieve result type for a const member function pointer. 
-  template<typename _Res, typename _Class, typename... _ArgTypes> 
+  /// Retrieve result type for a const member function pointer.
+  template<typename _Res, typename _Class, typename... _ArgTypes>
     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
-    {
-      typedef _Res result_type;
-    };
+    { typedef _Res result_type; };
 
-  /// Retrieve result type for a volatile member function pointer. 
-  template<typename _Res, typename _Class, typename... _ArgTypes> 
+  template<typename _Res, typename _Class, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
+    { typedef _Res result_type; };
+
+  /// Retrieve result type for a volatile member function pointer.
+  template<typename _Res, typename _Class, typename... _ArgTypes>
     struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
-    {
-      typedef _Res result_type;
-    };
+    { typedef _Res result_type; };
 
-  /// Retrieve result type for a const volatile member function pointer. 
-  template<typename _Res, typename _Class, typename... _ArgTypes> 
-    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile>
-    {
-      typedef _Res result_type;
-    };
+  template<typename _Res, typename _Class, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
+    { typedef _Res result_type; };
+
+  /// Retrieve result type for a const volatile member function pointer.
+  template<typename _Res, typename _Class, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
+                                 const volatile>
+    { typedef _Res result_type; };
+
+  template<typename _Res, typename _Class, typename... _ArgTypes>
+    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
+                                 const volatile>
+    { typedef _Res result_type; };
 
   /**
    *  Strip top-level cv-qualifiers from the function object and let
@@ -171,68 +184,7 @@ namespace std
   template<typename _Functor>
     struct _Weak_result_type
     : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
-    {
-    };
-
-  template<typename _Signature>
-    class result_of;
-
-  /**
-   *  Actual implementation of result_of. When _Has_result_type is
-   *  true, gets its result from _Weak_result_type. Otherwise, uses
-   *  the function object's member template result to extract the
-   *  result type.
-  */
-  template<bool _Has_result_type, typename _Signature>
-    struct _Result_of_impl;
-
-  // Handle member data pointers using _Mem_fn's logic
-  template<typename _Res, typename _Class, typename _T1>
-    struct _Result_of_impl<false, _Res _Class::*(_T1)>
-    {
-      typedef typename _Mem_fn<_Res _Class::*>
-                ::template _Result_type<_T1>::type type;
-    };
-
-  /**
-   * Determine whether we can determine a result type from @c Functor 
-   * alone.
-   */ 
-  template<typename _Functor, typename... _ArgTypes>
-    class result_of<_Functor(_ArgTypes...)>
-    : public _Result_of_impl<
-               _Has_result_type<_Weak_result_type<_Functor> >::value,
-               _Functor(_ArgTypes...)>
-    {
-    };
-
-  /// We already know the result type for @c Functor; use it.
-  template<typename _Functor, typename... _ArgTypes>
-    struct _Result_of_impl<true, _Functor(_ArgTypes...)>
-    {
-      typedef typename _Weak_result_type<_Functor>::result_type type;
-    };
-
-  /**
-   * We need to compute the result type for this invocation the hard 
-   * way.
-   */
-  template<typename _Functor, typename... _ArgTypes>
-    struct _Result_of_impl<false, _Functor(_ArgTypes...)>
-    {
-      typedef typename _Functor
-                ::template result<_Functor(_ArgTypes...)>::type type;
-    };
-
-  /**
-   * It is unsafe to access ::result when there are zero arguments, so we 
-   * return @c void instead.
-   */
-  template<typename _Functor>
-    struct _Result_of_impl<false, _Functor()>
-    {
-      typedef void type;
-    };
+    { };
 
   /// Determines if the type _Tp derives from unary_function.
   template<typename _Tp>
@@ -240,7 +192,7 @@ namespace std
     {
     private:
       template<typename _T1, typename _Res>
-        static __one __test(const volatile unary_function<_T1, _Res>*);
+       static __one __test(const volatile unary_function<_T1, _Res>*);
 
       // It's tempting to change "..." to const volatile void*, but
       // that fails when _Tp is a function type.
@@ -256,7 +208,7 @@ namespace std
     {
     private:
       template<typename _T1, typename _T2, typename _Res>
-        static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
+       static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
 
       // It's tempting to change "..." to const volatile void*, but
       // that fails when _Tp is a function type.
@@ -266,60 +218,47 @@ namespace std
       static const bool value = sizeof(__test((_Tp*)0)) == 1;
     };
 
-  /// Turns a function type into a function pointer type
-  template<typename _Tp, bool _IsFunctionType = is_function<_Tp>::value>
-    struct _Function_to_function_pointer
-    {
-      typedef _Tp type;
-    };
-
-  template<typename _Tp>
-    struct _Function_to_function_pointer<_Tp, true>
-    {
-      typedef _Tp* type;
-    };
-
   /**
    * Invoke a function object, which may be either a member pointer or a
    * function object. The first parameter will tell which.
    */
   template<typename _Functor, typename... _Args>
     inline
-    typename __gnu_cxx::__enable_if<
-             (!is_member_pointer<_Functor>::value
-              && !is_function<_Functor>::value
-              && !is_function<typename remove_pointer<_Functor>::type>::value),
-             typename result_of<_Functor(_Args...)>::type
-           >::__type
-    __invoke(_Functor& __f, _Args&... __args)
+    typename enable_if<
+            (!is_member_pointer<_Functor>::value
+             && !is_function<_Functor>::value
+             && !is_function<typename remove_pointer<_Functor>::type>::value),
+            typename result_of<_Functor(_Args&&...)>::type
+          >::type
+    __invoke(_Functor& __f, _Args&&... __args)
     {
-      return __f(__args...);
+      return __f(std::forward<_Args>(__args)...);
     }
 
   template<typename _Functor, typename... _Args>
     inline
-    typename __gnu_cxx::__enable_if<
+    typename enable_if<
              (is_member_pointer<_Functor>::value
               && !is_function<_Functor>::value
               && !is_function<typename remove_pointer<_Functor>::type>::value),
-             typename result_of<_Functor(_Args...)>::type
-           >::__type
-    __invoke(_Functor& __f, _Args&... __args)
+             typename result_of<_Functor(_Args&&...)>::type
+           >::type
+    __invoke(_Functor& __f, _Args&&... __args)
     {
-      return mem_fn(__f)(__args...);
+      return mem_fn(__f)(std::forward<_Args>(__args)...);
     }
 
   // To pick up function references (that will become function pointers)
   template<typename _Functor, typename... _Args>
     inline
-    typename __gnu_cxx::__enable_if<
-             (is_pointer<_Functor>::value
-              && is_function<typename remove_pointer<_Functor>::type>::value),
-             typename result_of<_Functor(_Args...)>::type
-           >::__type
-    __invoke(_Functor __f, _Args&... __args)
+    typename enable_if<
+            (is_pointer<_Functor>::value
+             && is_function<typename remove_pointer<_Functor>::type>::value),
+            typename result_of<_Functor(_Args&&...)>::type
+          >::type
+    __invoke(_Functor __f, _Args&&... __args)
     {
-      return __f(__args...);
+      return __f(std::forward<_Args>(__args)...);
     }
 
   /**
@@ -330,40 +269,43 @@ namespace std
   template<bool _Unary, bool _Binary, typename _Tp>
     struct _Reference_wrapper_base_impl;
 
-  // Not a unary_function or binary_function, so try a weak result type.
+  // None of the nested argument types.
   template<typename _Tp>
     struct _Reference_wrapper_base_impl<false, false, _Tp>
     : _Weak_result_type<_Tp>
     { };
 
-  // unary_function but not binary_function
+  // Nested argument_type only.
   template<typename _Tp>
     struct _Reference_wrapper_base_impl<true, false, _Tp>
-    : unary_function<typename _Tp::argument_type,
-                    typename _Tp::result_type>
-    { };
+    : _Weak_result_type<_Tp>
+    {
+      typedef typename _Tp::argument_type argument_type;
+    };
 
-  // binary_function but not unary_function
+  // Nested first_argument_type and second_argument_type only.
   template<typename _Tp>
     struct _Reference_wrapper_base_impl<false, true, _Tp>
-    : binary_function<typename _Tp::first_argument_type,
-                     typename _Tp::second_argument_type,
-                     typename _Tp::result_type>
-    { };
+    : _Weak_result_type<_Tp>
+    {
+      typedef typename _Tp::first_argument_type first_argument_type;
+      typedef typename _Tp::second_argument_type second_argument_type;
+    };
 
-  // Both unary_function and binary_function. Import result_type to
-  // avoid conflicts.
+  // All the nested argument types.
    template<typename _Tp>
     struct _Reference_wrapper_base_impl<true, true, _Tp>
-    : unary_function<typename _Tp::argument_type,
-                    typename _Tp::result_type>,
-      binary_function<typename _Tp::first_argument_type,
-                     typename _Tp::second_argument_type,
-                     typename _Tp::result_type>
+    : _Weak_result_type<_Tp>
     {
-      typedef typename _Tp::result_type result_type;
+      typedef typename _Tp::argument_type argument_type;
+      typedef typename _Tp::first_argument_type first_argument_type;
+      typedef typename _Tp::second_argument_type second_argument_type;
     };
 
+  _GLIBCXX_HAS_NESTED_TYPE(argument_type)
+  _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
+  _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
+
   /**
    *  Derives from unary_function or binary_function when it
    *  can. Specializations handle all of the easy cases. The primary
@@ -373,8 +315,9 @@ namespace std
   template<typename _Tp>
     struct _Reference_wrapper_base
     : _Reference_wrapper_base_impl<
-      _Derives_from_unary_function<_Tp>::value,
-      _Derives_from_binary_function<_Tp>::value,
+      __has_argument_type<_Tp>::value,
+      __has_first_argument_type<_Tp>::value
+      && __has_second_argument_type<_Tp>::value,
       _Tp>
     { };
 
@@ -384,12 +327,42 @@ namespace std
     : unary_function<_T1, _Res>
     { };
 
+  template<typename _Res, typename _T1>
+    struct _Reference_wrapper_base<_Res(_T1) const>
+    : unary_function<_T1, _Res>
+    { };
+
+  template<typename _Res, typename _T1>
+    struct _Reference_wrapper_base<_Res(_T1) volatile>
+    : unary_function<_T1, _Res>
+    { };
+
+  template<typename _Res, typename _T1>
+    struct _Reference_wrapper_base<_Res(_T1) const volatile>
+    : unary_function<_T1, _Res>
+    { };
+
   // - a function type (binary)
   template<typename _Res, typename _T1, typename _T2>
     struct _Reference_wrapper_base<_Res(_T1, _T2)>
     : binary_function<_T1, _T2, _Res>
     { };
 
+  template<typename _Res, typename _T1, typename _T2>
+    struct _Reference_wrapper_base<_Res(_T1, _T2) const>
+    : binary_function<_T1, _T2, _Res>
+    { };
+
+  template<typename _Res, typename _T1, typename _T2>
+    struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
+    : binary_function<_T1, _T2, _Res>
+    { };
+
+  template<typename _Res, typename _T1, typename _T2>
+    struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
+    : binary_function<_T1, _T2, _Res>
+    { };
+
   // - a function pointer type (unary)
   template<typename _Res, typename _T1>
     struct _Reference_wrapper_base<_Res(*)(_T1)>
@@ -450,100 +423,100 @@ namespace std
     : binary_function<const volatile _T1*, _T2, _Res>
     { };
 
-  /// reference_wrapper
+  /**
+   *  @brief Primary class template for reference_wrapper.
+   *  @ingroup functors
+   *  @{
+   */
   template<typename _Tp>
     class reference_wrapper
     : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
     {
-      // If _Tp is a function type, we can't form result_of<_Tp(...)>,
-      // so turn it into a function pointer type.
-      typedef typename _Function_to_function_pointer<_Tp>::type
-        _M_func_type;
-
       _Tp* _M_data;
+
     public:
       typedef _Tp type;
 
-      explicit
-      reference_wrapper(_Tp& __indata): _M_data(&__indata)
+      reference_wrapper(_Tp& __indata) noexcept
+      : _M_data(std::__addressof(__indata))
       { }
 
-      reference_wrapper(const reference_wrapper<_Tp>& __inref):
-      _M_data(__inref._M_data)
+      reference_wrapper(_Tp&&) = delete;
+
+      reference_wrapper(const reference_wrapper<_Tp>& __inref) noexcept
+      : _M_data(__inref._M_data)
       { }
 
       reference_wrapper&
-      operator=(const reference_wrapper<_Tp>& __inref)
+      operator=(const reference_wrapper<_Tp>& __inref) noexcept
       {
-        _M_data = __inref._M_data;
-        return *this;
+       _M_data = __inref._M_data;
+       return *this;
       }
 
-      operator _Tp&() const
+      operator _Tp&() const noexcept
       { return this->get(); }
 
       _Tp&
-      get() const
+      get() const noexcept
       { return *_M_data; }
 
       template<typename... _Args>
-        typename result_of<_M_func_type(_Args...)>::type
-        operator()(_Args&... __args) const
-        {
-         return __invoke(get(), __args...);
+       typename result_of<_Tp&(_Args&&...)>::type
+       operator()(_Args&&... __args) const
+       {
+         return __invoke(get(), std::forward<_Args>(__args)...);
        }
     };
 
 
-  // Denotes a reference should be taken to a variable.
+  /// Denotes a reference should be taken to a variable.
   template<typename _Tp>
     inline reference_wrapper<_Tp>
-    ref(_Tp& __t)
+    ref(_Tp& __t) noexcept
     { return reference_wrapper<_Tp>(__t); }
 
-  // Denotes a const reference should be taken to a variable.
+  /// Denotes a const reference should be taken to a variable.
   template<typename _Tp>
     inline reference_wrapper<const _Tp>
-    cref(const _Tp& __t)
+    cref(const _Tp& __t) noexcept
     { return reference_wrapper<const _Tp>(__t); }
 
+  template<typename _Tp>
+    void ref(const _Tp&&) = delete;
+
+  template<typename _Tp>
+    void cref(const _Tp&&) = delete;
+
+  /// Partial specialization.
   template<typename _Tp>
     inline reference_wrapper<_Tp>
-    ref(reference_wrapper<_Tp> __t)
+    ref(reference_wrapper<_Tp> __t) noexcept
     { return ref(__t.get()); }
 
+  /// Partial specialization.
   template<typename _Tp>
     inline reference_wrapper<const _Tp>
-    cref(reference_wrapper<_Tp> __t)
+    cref(reference_wrapper<_Tp> __t) noexcept
     { return cref(__t.get()); }
 
-  template<typename _Tp, bool>
-    struct _Mem_fn_const_or_non
-    {
-      typedef const _Tp& type;
-    };
-
-  template<typename _Tp>
-    struct _Mem_fn_const_or_non<_Tp, false>
-    {
-      typedef _Tp& type;
-    };
+  // @} group functors
 
   /**
    * Derives from @c unary_function or @c binary_function, or perhaps
    * nothing, depending on the number of arguments provided. The
    * primary template is the basis case, which derives nothing.
    */
-  template<typename _Res, typename... _ArgTypes> 
+  template<typename _Res, typename... _ArgTypes>
     struct _Maybe_unary_or_binary_function { };
 
-  /// Derives from @c unary_function, as appropriate. 
-  template<typename _Res, typename _T1> 
+  /// Derives from @c unary_function, as appropriate.
+  template<typename _Res, typename _T1>
     struct _Maybe_unary_or_binary_function<_Res, _T1>
     : std::unary_function<_T1, _Res> { };
 
-  /// Derives from @c binary_function, as appropriate. 
-  template<typename _Res, typename _T1, typename _T2> 
+  /// Derives from @c binary_function, as appropriate.
+  template<typename _Res, typename _T1, typename _T2>
     struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
     : std::binary_function<_T1, _T2, _Res> { };
 
@@ -555,15 +528,15 @@ namespace std
       typedef _Res (_Class::*_Functor)(_ArgTypes...);
 
       template<typename _Tp>
-        _Res
-        _M_call(_Tp& __object, const volatile _Class *, 
-                _ArgTypes... __args) const
-        { return (__object.*__pmf)(__args...); }
+       _Res
+       _M_call(_Tp& __object, const volatile _Class *,
+               _ArgTypes... __args) const
+       { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       template<typename _Tp>
-        _Res
-        _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
-        { return ((*__ptr).*__pmf)(__args...); }
+       _Res
+       _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
+       { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
     public:
       typedef _Res result_type;
@@ -573,18 +546,21 @@ namespace std
       // Handle objects
       _Res
       operator()(_Class& __object, _ArgTypes... __args) const
-      { return (__object.*__pmf)(__args...); }
+      { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       // Handle pointers
       _Res
       operator()(_Class* __object, _ArgTypes... __args) const
-      { return (__object->*__pmf)(__args...); }
+      { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       // Handle smart pointers, references and pointers to derived
       template<typename _Tp>
-        _Res
+       _Res
        operator()(_Tp& __object, _ArgTypes... __args) const
-        { return _M_call(__object, &__object, __args...); }
+       {
+         return _M_call(__object, &__object,
+             std::forward<_ArgTypes>(__args)...);
+       }
 
     private:
       _Functor __pmf;
@@ -593,21 +569,21 @@ namespace std
   /// Implementation of @c mem_fn for const member function pointers.
   template<typename _Res, typename _Class, typename... _ArgTypes>
     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
-    : public _Maybe_unary_or_binary_function<_Res, const _Class*, 
+    : public _Maybe_unary_or_binary_function<_Res, const _Class*,
                                             _ArgTypes...>
     {
       typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
 
       template<typename _Tp>
-        _Res
-        _M_call(_Tp& __object, const volatile _Class *, 
-                _ArgTypes... __args) const
-        { return (__object.*__pmf)(__args...); }
+       _Res
+       _M_call(_Tp& __object, const volatile _Class *,
+               _ArgTypes... __args) const
+       { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       template<typename _Tp>
-        _Res
-        _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
-        { return ((*__ptr).*__pmf)(__args...); }
+       _Res
+       _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
+       { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
     public:
       typedef _Res result_type;
@@ -617,17 +593,20 @@ namespace std
       // Handle objects
       _Res
       operator()(const _Class& __object, _ArgTypes... __args) const
-      { return (__object.*__pmf)(__args...); }
+      { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       // Handle pointers
       _Res
       operator()(const _Class* __object, _ArgTypes... __args) const
-      { return (__object->*__pmf)(__args...); }
+      { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       // Handle smart pointers, references and pointers to derived
       template<typename _Tp>
-        _Res operator()(_Tp& __object, _ArgTypes... __args) const
-        { return _M_call(__object, &__object, __args...); }
+       _Res operator()(_Tp& __object, _ArgTypes... __args) const
+       {
+         return _M_call(__object, &__object,
+             std::forward<_ArgTypes>(__args)...);
+       }
 
     private:
       _Functor __pmf;
@@ -636,21 +615,21 @@ namespace std
   /// Implementation of @c mem_fn for volatile member function pointers.
   template<typename _Res, typename _Class, typename... _ArgTypes>
     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
-    : public _Maybe_unary_or_binary_function<_Res, volatile _Class*, 
+    : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
                                             _ArgTypes...>
     {
       typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
 
       template<typename _Tp>
-        _Res
-        _M_call(_Tp& __object, const volatile _Class *, 
-                _ArgTypes... __args) const
-        { return (__object.*__pmf)(__args...); }
+       _Res
+       _M_call(_Tp& __object, const volatile _Class *,
+               _ArgTypes... __args) const
+       { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       template<typename _Tp>
-        _Res
-        _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
-        { return ((*__ptr).*__pmf)(__args...); }
+       _Res
+       _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
+       { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
     public:
       typedef _Res result_type;
@@ -660,18 +639,21 @@ namespace std
       // Handle objects
       _Res
       operator()(volatile _Class& __object, _ArgTypes... __args) const
-      { return (__object.*__pmf)(__args...); }
+      { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       // Handle pointers
       _Res
       operator()(volatile _Class* __object, _ArgTypes... __args) const
-      { return (__object->*__pmf)(__args...); }
+      { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       // Handle smart pointers, references and pointers to derived
       template<typename _Tp>
-        _Res
+       _Res
        operator()(_Tp& __object, _ArgTypes... __args) const
-        { return _M_call(__object, &__object, __args...); }
+       {
+         return _M_call(__object, &__object,
+             std::forward<_ArgTypes>(__args)...);
+       }
 
     private:
       _Functor __pmf;
@@ -680,21 +662,21 @@ namespace std
   /// Implementation of @c mem_fn for const volatile member function pointers.
   template<typename _Res, typename _Class, typename... _ArgTypes>
     class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
-    : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*, 
+    : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
                                             _ArgTypes...>
     {
       typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
 
       template<typename _Tp>
-        _Res
-        _M_call(_Tp& __object, const volatile _Class *, 
-                _ArgTypes... __args) const
-        { return (__object.*__pmf)(__args...); }
+       _Res
+       _M_call(_Tp& __object, const volatile _Class *,
+               _ArgTypes... __args) const
+       { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       template<typename _Tp>
-        _Res
-        _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
-        { return ((*__ptr).*__pmf)(__args...); }
+       _Res
+       _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
+       { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
     public:
       typedef _Res result_type;
@@ -702,85 +684,100 @@ namespace std
       explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
 
       // Handle objects
-      _Res 
+      _Res
       operator()(const volatile _Class& __object, _ArgTypes... __args) const
-      { return (__object.*__pmf)(__args...); }
+      { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       // Handle pointers
-      _Res 
+      _Res
       operator()(const volatile _Class* __object, _ArgTypes... __args) const
-      { return (__object->*__pmf)(__args...); }
+      { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
 
       // Handle smart pointers, references and pointers to derived
       template<typename _Tp>
-        _Res operator()(_Tp& __object, _ArgTypes... __args) const
-        { return _M_call(__object, &__object, __args...); }
+       _Res operator()(_Tp& __object, _ArgTypes... __args) const
+       {
+         return _M_call(__object, &__object,
+             std::forward<_ArgTypes>(__args)...);
+       }
 
     private:
       _Functor __pmf;
     };
 
 
+  template<typename _Tp, bool>
+    struct _Mem_fn_const_or_non
+    {
+      typedef const _Tp& type;
+    };
+
+  template<typename _Tp>
+    struct _Mem_fn_const_or_non<_Tp, false>
+    {
+      typedef _Tp& type;
+    };
+
   template<typename _Res, typename _Class>
     class _Mem_fn<_Res _Class::*>
     {
       // This bit of genius is due to Peter Dimov, improved slightly by
       // Douglas Gregor.
       template<typename _Tp>
-        _Res&
-        _M_call(_Tp& __object, _Class *) const
-        { return __object.*__pm; }
+       _Res&
+       _M_call(_Tp& __object, _Class *) const
+       { return __object.*__pm; }
 
       template<typename _Tp, typename _Up>
-        _Res&
-        _M_call(_Tp& __object, _Up * const *) const
-        { return (*__object).*__pm; }
+       _Res&
+       _M_call(_Tp& __object, _Up * const *) const
+       { return (*__object).*__pm; }
 
       template<typename _Tp, typename _Up>
-        const _Res&
-        _M_call(_Tp& __object, const _Up * const *) const
-        { return (*__object).*__pm; }
+       const _Res&
+       _M_call(_Tp& __object, const _Up * const *) const
+       { return (*__object).*__pm; }
 
       template<typename _Tp>
-        const _Res&
-        _M_call(_Tp& __object, const _Class *) const
-        { return __object.*__pm; }
+       const _Res&
+       _M_call(_Tp& __object, const _Class *) const
+       { return __object.*__pm; }
 
       template<typename _Tp>
-        const _Res&
-        _M_call(_Tp& __ptr, const volatile void*) const
-        { return (*__ptr).*__pm; }
+       const _Res&
+       _M_call(_Tp& __ptr, const volatile void*) const
+       { return (*__ptr).*__pm; }
 
       template<typename _Tp> static _Tp& __get_ref();
 
       template<typename _Tp>
-        static __sfinae_types::__one __check_const(_Tp&, _Class*);
+       static __sfinae_types::__one __check_const(_Tp&, _Class*);
       template<typename _Tp, typename _Up>
-        static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
+       static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
       template<typename _Tp, typename _Up>
-        static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
+       static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
       template<typename _Tp>
-        static __sfinae_types::__two __check_const(_Tp&, const _Class*);
+       static __sfinae_types::__two __check_const(_Tp&, const _Class*);
       template<typename _Tp>
-        static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
+       static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
 
     public:
       template<typename _Tp>
-        struct _Result_type
+       struct _Result_type
        : _Mem_fn_const_or_non<_Res,
          (sizeof(__sfinae_types::__two)
           == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
-        { };
+       { };
 
       template<typename _Signature>
-        struct result;
+       struct result;
 
       template<typename _CVMem, typename _Tp>
-        struct result<_CVMem(_Tp)>
+       struct result<_CVMem(_Tp)>
        : public _Result_type<_Tp> { };
 
       template<typename _CVMem, typename _Tp>
-        struct result<_CVMem(_Tp&)>
+       struct result<_CVMem(_Tp&)>
        : public _Result_type<_Tp> { };
 
       explicit
@@ -806,9 +803,9 @@ namespace std
 
       // Handle smart pointers and derived
       template<typename _Tp>
-        typename _Result_type<_Tp>::type
-        operator()(_Tp& __unknown) const
-        { return _M_call(__unknown, &__unknown); }
+       typename _Result_type<_Tp>::type
+       operator()(_Tp& __unknown) const
+       { return _M_call(__unknown, &__unknown); }
 
     private:
       _Res _Class::*__pm;
@@ -817,6 +814,7 @@ namespace std
   /**
    *  @brief Returns a function object that forwards to the member
    *  pointer @a pm.
+   *  @ingroup functors
    */
   template<typename _Tp, typename _Class>
     inline _Mem_fn<_Tp _Class::*>
@@ -829,107 +827,83 @@ namespace std
    *  @brief Determines if the given type _Tp is a function object
    *  should be treated as a subexpression when evaluating calls to
    *  function objects returned by bind(). [TR1 3.6.1]
+   *  @ingroup binders
    */
   template<typename _Tp>
     struct is_bind_expression
-    { static const bool value = false; };
-
-  template<typename _Tp>
-    const bool is_bind_expression<_Tp>::value;
+    : public false_type { };
 
   /**
    *  @brief Determines if the given type _Tp is a placeholder in a
    *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
+   *  @ingroup binders
    */
   template<typename _Tp>
     struct is_placeholder
-    { static const int value = 0; };
-
-  template<typename _Tp>
-    const int is_placeholder<_Tp>::value;
+    : public integral_constant<int, 0>
+    { };
 
   /// The type of placeholder objects defined by libstdc++.
   template<int _Num> struct _Placeholder { };
 
+  _GLIBCXX_END_NAMESPACE_VERSION
+
   /** @namespace std::placeholders
    *  @brief ISO C++ 0x entities sub namespace for functional.
+   *  @ingroup binders
    *
    *  Define a large number of placeholders. There is no way to
    *  simplify this with variadic templates, because we're introducing
    *  unique names for each.
    */
-  namespace placeholders 
-  { 
-    namespace 
-    {
-      _Placeholder<1> _1;
-      _Placeholder<2> _2;
-      _Placeholder<3> _3;
-      _Placeholder<4> _4;
-      _Placeholder<5> _5;
-      _Placeholder<6> _6;
-      _Placeholder<7> _7;
-      _Placeholder<8> _8;
-      _Placeholder<9> _9;
-      _Placeholder<10> _10;
-      _Placeholder<11> _11;
-      _Placeholder<12> _12;
-      _Placeholder<13> _13;
-      _Placeholder<14> _14;
-      _Placeholder<15> _15;
-      _Placeholder<16> _16;
-      _Placeholder<17> _17;
-      _Placeholder<18> _18;
-      _Placeholder<19> _19;
-      _Placeholder<20> _20;
-      _Placeholder<21> _21;
-      _Placeholder<22> _22;
-      _Placeholder<23> _23;
-      _Placeholder<24> _24;
-      _Placeholder<25> _25;
-      _Placeholder<26> _26;
-      _Placeholder<27> _27;
-      _Placeholder<28> _28;
-      _Placeholder<29> _29;
-    } 
+  namespace placeholders
+  {
+  _GLIBCXX_BEGIN_NAMESPACE_VERSION
+    extern const _Placeholder<1> _1;
+    extern const _Placeholder<2> _2;
+    extern const _Placeholder<3> _3;
+    extern const _Placeholder<4> _4;
+    extern const _Placeholder<5> _5;
+    extern const _Placeholder<6> _6;
+    extern const _Placeholder<7> _7;
+    extern const _Placeholder<8> _8;
+    extern const _Placeholder<9> _9;
+    extern const _Placeholder<10> _10;
+    extern const _Placeholder<11> _11;
+    extern const _Placeholder<12> _12;
+    extern const _Placeholder<13> _13;
+    extern const _Placeholder<14> _14;
+    extern const _Placeholder<15> _15;
+    extern const _Placeholder<16> _16;
+    extern const _Placeholder<17> _17;
+    extern const _Placeholder<18> _18;
+    extern const _Placeholder<19> _19;
+    extern const _Placeholder<20> _20;
+    extern const _Placeholder<21> _21;
+    extern const _Placeholder<22> _22;
+    extern const _Placeholder<23> _23;
+    extern const _Placeholder<24> _24;
+    extern const _Placeholder<25> _25;
+    extern const _Placeholder<26> _26;
+    extern const _Placeholder<27> _27;
+    extern const _Placeholder<28> _28;
+    extern const _Placeholder<29> _29;
+  _GLIBCXX_END_NAMESPACE_VERSION
   }
 
+  _GLIBCXX_BEGIN_NAMESPACE_VERSION
+
   /**
    *  Partial specialization of is_placeholder that provides the placeholder
    *  number for the placeholder objects defined by libstdc++.
+   *  @ingroup binders
    */
   template<int _Num>
     struct is_placeholder<_Placeholder<_Num> >
-    { static const int value = _Num; };
-
-  template<int _Num>
-    const int is_placeholder<_Placeholder<_Num> >::value;
+    : public integral_constant<int, _Num>
+    { };
 
   /**
-   * Stores a tuple of indices. Used by bind() to extract the elements
-   * in a tuple. 
-   */
-  template<int... _Indexes>
-    struct _Index_tuple { };
-
-  /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
-  template<std::size_t _Num, typename _Tuple = _Index_tuple<> >
-    struct _Build_index_tuple;
-  template<std::size_t _Num, int... _Indexes> 
-    struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> >
-    : _Build_index_tuple<_Num - 1, 
-                         _Index_tuple<_Indexes..., sizeof...(_Indexes)> >
-    {
-    };
-
-  template<int... _Indexes>
-    struct _Build_index_tuple<0, _Index_tuple<_Indexes...> >
-    {
-      typedef _Index_tuple<_Indexes...> __type;
-    };
-
-  /** 
    * Used by _Safe_tuple_element to indicate that there is no tuple
    * element at this position.
    */
@@ -961,10 +935,9 @@ namespace std
    */
  template<int __i, typename _Tuple>
    struct _Safe_tuple_element
-   : _Safe_tuple_element_impl<__i, _Tuple, 
-                              (__i >= 0 && __i < tuple_size<_Tuple>::value)>
-   {
-   };
+   : _Safe_tuple_element_impl<__i, _Tuple,
+                             (__i >= 0 && __i < tuple_size<_Tuple>::value)>
+   { };
 
   /**
    *  Maps an argument to bind() into an actual argument to the bound
@@ -978,8 +951,8 @@ namespace std
    *  rest of the arguments.
    */
   template<typename _Arg,
-           bool _IsBindExp = is_bind_expression<_Arg>::value,
-           bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
+          bool _IsBindExp = is_bind_expression<_Arg>::value,
+          bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
     class _Mu;
 
   /**
@@ -997,9 +970,9 @@ namespace std
        * but not volatile-qualified. This might be a defect in the TR.
        */
       template<typename _CVRef, typename _Tuple>
-        result_type
-        operator()(_CVRef& __arg, const _Tuple&) const volatile
-        { return __arg.get(); }
+       result_type
+       operator()(_CVRef& __arg, _Tuple&) const volatile
+       { return __arg.get(); }
     };
 
   /**
@@ -1011,20 +984,12 @@ namespace std
     class _Mu<_Arg, true, false>
     {
     public:
-      template<typename _Signature> class result;
-
-      // Determine the result type when we pass the arguments along. This
-      // involves passing along the cv-qualifiers placed on _Mu and
-      // unwrapping the argument bundle.
-      template<typename _CVMu, typename _CVArg, typename... _Args>
-        class result<_CVMu(_CVArg, tuple<_Args...>)>
-       : public result_of<_CVArg(_Args...)> { };
-
       template<typename _CVArg, typename... _Args>
-        typename result_of<_CVArg(_Args...)>::type
-        operator()(_CVArg& __arg,
-                  const tuple<_Args...>& __tuple) const volatile
-        {
+       auto
+       operator()(_CVArg& __arg,
+                  tuple<_Args...>& __tuple) const volatile
+       -> decltype(__arg(declval<_Args>()...))
+       {
          // Construct an index tuple and forward to __call
          typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
            _Indexes;
@@ -1033,13 +998,14 @@ namespace std
 
     private:
       // Invokes the underlying function object __arg by unpacking all
-      // of the arguments in the tuple. 
+      // of the arguments in the tuple.
       template<typename _CVArg, typename... _Args, int... _Indexes>
-        typename result_of<_CVArg(_Args...)>::type
-        __call(_CVArg& __arg, const tuple<_Args...>& __tuple,
+       auto
+       __call(_CVArg& __arg, tuple<_Args...>& __tuple,
               const _Index_tuple<_Indexes...>&) const volatile
-        {
-         return __arg(get<_Indexes>(__tuple)...);
+       -> decltype(__arg(declval<_Args>()...))
+       {
+         return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
        }
     };
 
@@ -1055,8 +1021,8 @@ namespace std
       template<typename _Signature> class result;
 
       template<typename _CVMu, typename _CVArg, typename _Tuple>
-        class result<_CVMu(_CVArg, _Tuple)>
-        {
+       class result<_CVMu(_CVArg, _Tuple)>
+       {
          // Add a reference, if it hasn't already been done for us.
          // This allows us to be a little bit sloppy in constructing
          // the tuple that we pass to result_of<...>.
@@ -1065,14 +1031,15 @@ namespace std
            __base_type;
 
        public:
-         typedef typename add_lvalue_reference<__base_type>::type type;
+         typedef typename add_rvalue_reference<__base_type>::type type;
        };
 
       template<typename _Tuple>
-        typename result<_Mu(_Arg, _Tuple)>::type
-        operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile
-        {
-         return ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple);
+       typename result<_Mu(_Arg, _Tuple)>::type
+       operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
+       {
+         return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
+             ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
        }
     };
 
@@ -1088,16 +1055,16 @@ namespace std
       template<typename _Signature> struct result;
 
       template<typename _CVMu, typename _CVArg, typename _Tuple>
-        struct result<_CVMu(_CVArg, _Tuple)>
-        {
+       struct result<_CVMu(_CVArg, _Tuple)>
+       {
          typedef typename add_lvalue_reference<_CVArg>::type type;
        };
 
       // Pick up the cv-qualifiers of the argument
       template<typename _CVArg, typename _Tuple>
-        _CVArg&
-        operator()(_CVArg& __arg, const _Tuple&) const volatile
-        { return __arg; }
+       _CVArg&&
+       operator()(_CVArg&& __arg, _Tuple&) const volatile
+       { return std::forward<_CVArg>(__arg); }
     };
 
   /**
@@ -1109,10 +1076,14 @@ namespace std
     struct _Maybe_wrap_member_pointer
     {
       typedef _Tp type;
-      
+
       static const _Tp&
       __do_wrap(const _Tp& __x)
       { return __x; }
+
+      static _Tp&&
+      __do_wrap(_Tp&& __x)
+      { return static_cast<_Tp&&>(__x); }
     };
 
   /**
@@ -1124,12 +1095,36 @@ namespace std
     struct _Maybe_wrap_member_pointer<_Tp _Class::*>
     {
       typedef _Mem_fn<_Tp _Class::*> type;
-      
+
       static type
       __do_wrap(_Tp _Class::* __pm)
       { return type(__pm); }
     };
 
+  // Specialization needed to prevent "forming reference to void" errors when
+  // bind<void>() is called, because argument deduction instantiates
+  // _Maybe_wrap_member_pointer<void> outside the immediate context where
+  // SFINAE applies.
+  template<>
+    struct _Maybe_wrap_member_pointer<void>
+    {
+      typedef void type;
+    };
+
+  // std::get<I> for volatile-qualified tuples
+  template<size_t _Ind, typename... _Tp>
+    inline auto
+    __volget(volatile tuple<_Tp...>& __tuple)
+    -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
+    { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
+
+  // std::get<I> for const-volatile-qualified tuples
+  template<size_t _Ind, typename... _Tp>
+    inline auto
+    __volget(const volatile tuple<_Tp...>& __tuple)
+    -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
+    { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
+
   /// Type of the function object returned from bind().
   template<typename _Signature>
     struct _Bind;
@@ -1139,113 +1134,121 @@ namespace std
     : public _Weak_result_type<_Functor>
     {
       typedef _Bind __self_type;
-      typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 
-        _Bound_indexes;
+      typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
+       _Bound_indexes;
 
       _Functor _M_f;
       tuple<_Bound_args...> _M_bound_args;
 
       // Call unqualified
-      template<typename... _Args, int... _Indexes>
-        typename result_of<
-                   _Functor(typename result_of<_Mu<_Bound_args> 
-                            (_Bound_args, tuple<_Args...>)>::type...)
-                 >::type
-        __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
-        {
-          return _M_f(_Mu<_Bound_args>()
-                      (get<_Indexes>(_M_bound_args), __args)...);
-        }
+      template<typename _Result, typename... _Args, int... _Indexes>
+       _Result
+       __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
+       {
+         return _M_f(_Mu<_Bound_args>()
+                     (get<_Indexes>(_M_bound_args), __args)...);
+       }
 
       // Call as const
-      template<typename... _Args, int... _Indexes>
-        typename result_of<
-                   const _Functor(typename result_of<_Mu<_Bound_args> 
-                                    (const _Bound_args, tuple<_Args...>)
-                                  >::type...)>::type
-        __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
-        {
-          return _M_f(_Mu<_Bound_args>()
-                      (get<_Indexes>(_M_bound_args), __args)...);
-        }
+      template<typename _Result, typename... _Args, int... _Indexes>
+       _Result
+       __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
+       {
+         return _M_f(_Mu<_Bound_args>()
+                     (get<_Indexes>(_M_bound_args), __args)...);
+       }
 
       // Call as volatile
-      template<typename... _Args, int... _Indexes>
-        typename result_of<
-                   volatile _Functor(typename result_of<_Mu<_Bound_args> 
-                                    (volatile _Bound_args, tuple<_Args...>)
-                                  >::type...)>::type
-        __call(const tuple<_Args...>& __args, 
-               _Index_tuple<_Indexes...>) volatile
-        {
-          return _M_f(_Mu<_Bound_args>()
-                      (get<_Indexes>(_M_bound_args), __args)...);
-        }
+      template<typename _Result, typename... _Args, int... _Indexes>
+       _Result
+       __call_v(tuple<_Args...>&& __args,
+                _Index_tuple<_Indexes...>) volatile
+       {
+         return _M_f(_Mu<_Bound_args>()
+                     (__volget<_Indexes>(_M_bound_args), __args)...);
+       }
 
       // Call as const volatile
-      template<typename... _Args, int... _Indexes>
-        typename result_of<
-                   const volatile _Functor(typename result_of<_Mu<_Bound_args> 
-                                    (const volatile _Bound_args, 
-                                     tuple<_Args...>)
-                                  >::type...)>::type
-        __call(const tuple<_Args...>& __args, 
-               _Index_tuple<_Indexes...>) const volatile
-        {
-          return _M_f(_Mu<_Bound_args>()
-                      (get<_Indexes>(_M_bound_args), __args)...);
-        }
+      template<typename _Result, typename... _Args, int... _Indexes>
+       _Result
+       __call_c_v(tuple<_Args...>&& __args,
+                  _Index_tuple<_Indexes...>) const volatile
+       {
+         return _M_f(_Mu<_Bound_args>()
+                     (__volget<_Indexes>(_M_bound_args), __args)...);
+       }
 
      public:
-      explicit _Bind(_Functor __f, _Bound_args... __bound_args)
-        : _M_f(__f), _M_bound_args(__bound_args...) { }
-
-      // Call unqualified
       template<typename... _Args>
-        typename result_of<
-                   _Functor(typename result_of<_Mu<_Bound_args> 
-                            (_Bound_args, tuple<_Args...>)>::type...)
-                 >::type
-        operator()(_Args&... __args)
-        {
-          return this->__call(tie(__args...), _Bound_indexes());
-        }
+       explicit _Bind(const _Functor& __f, _Args&&... __args)
+       : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
+       { }
 
-      // Call as const
       template<typename... _Args>
-        typename result_of<
-                   const _Functor(typename result_of<_Mu<_Bound_args> 
-                            (const _Bound_args, tuple<_Args...>)>::type...)
-                 >::type
-        operator()(_Args&... __args) const
-        {
-          return this->__call(tie(__args...), _Bound_indexes());
-        }
+       explicit _Bind(_Functor&& __f, _Args&&... __args)
+       : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
+       { }
 
+      _Bind(const _Bind&) = default;
 
-      // Call as volatile
-      template<typename... _Args>
-        typename result_of<
-                   volatile _Functor(typename result_of<_Mu<_Bound_args> 
-                            (volatile _Bound_args, tuple<_Args...>)>::type...)
-                 >::type
-        operator()(_Args&... __args) volatile
-        {
-          return this->__call(tie(__args...), _Bound_indexes());
-        }
+      _Bind(_Bind&& __b)
+      : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
+      { }
 
+      // Call unqualified
+      template<typename... _Args, typename _Result
+       = decltype( std::declval<_Functor>()(
+             _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
+                                 std::declval<tuple<_Args...>&>() )... ) )>
+       _Result
+       operator()(_Args&&... __args)
+       {
+         return this->__call<_Result>(
+             std::forward_as_tuple(std::forward<_Args>(__args)...),
+             _Bound_indexes());
+       }
+
+      // Call as const
+      template<typename... _Args, typename _Result
+       = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
+                      typename add_const<_Functor>::type>::type>()(
+             _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
+                                 std::declval<tuple<_Args...>&>() )... ) )>
+       _Result
+       operator()(_Args&&... __args) const
+       {
+         return this->__call_c<_Result>(
+             std::forward_as_tuple(std::forward<_Args>(__args)...),
+             _Bound_indexes());
+       }
+
+      // Call as volatile
+      template<typename... _Args, typename _Result
+       = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
+                       typename add_volatile<_Functor>::type>::type>()(
+             _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
+                                 std::declval<tuple<_Args...>&>() )... ) )>
+       _Result
+       operator()(_Args&&... __args) volatile
+       {
+         return this->__call_v<_Result>(
+             std::forward_as_tuple(std::forward<_Args>(__args)...),
+             _Bound_indexes());
+       }
 
       // Call as const volatile
-      template<typename... _Args>
-        typename result_of<
-                   const volatile _Functor(typename result_of<_Mu<_Bound_args> 
-                            (const volatile _Bound_args, 
-                             tuple<_Args...>)>::type...)
-                 >::type
-        operator()(_Args&... __args) const volatile
-        {
-          return this->__call(tie(__args...), _Bound_indexes());
-        }
+      template<typename... _Args, typename _Result
+       = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
+                       typename add_cv<_Functor>::type>::type>()(
+             _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
+                                 std::declval<tuple<_Args...>&>() )... ) )>
+       _Result
+       operator()(_Args&&... __args) const volatile
+       {
+         return this->__call_c_v<_Result>(
+             std::forward_as_tuple(std::forward<_Args>(__args)...),
+             _Bound_indexes());
+       }
     };
 
   /// Type of the function object returned from bind<R>().
@@ -1256,130 +1259,315 @@ namespace std
     class _Bind_result<_Result, _Functor(_Bound_args...)>
     {
       typedef _Bind_result __self_type;
-      typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type 
-        _Bound_indexes;
+      typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
+       _Bound_indexes;
 
       _Functor _M_f;
       tuple<_Bound_args...> _M_bound_args;
 
+      // sfinae types
+      template<typename _Res>
+       struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
+      template<typename _Res>
+       struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
+
       // Call unqualified
-      template<typename... _Args, int... _Indexes>
-        _Result
-        __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>)
-        {
-          return _M_f(_Mu<_Bound_args>()
-                      (get<_Indexes>(_M_bound_args), __args)...);
-        }
+      template<typename _Res, typename... _Args, int... _Indexes>
+       _Result
+       __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
+           typename __disable_if_void<_Res>::type = 0)
+       {
+         return _M_f(_Mu<_Bound_args>()
+                     (get<_Indexes>(_M_bound_args), __args)...);
+       }
+
+      // Call unqualified, return void
+      template<typename _Res, typename... _Args, int... _Indexes>
+       void
+       __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
+           typename __enable_if_void<_Res>::type = 0)
+       {
+         _M_f(_Mu<_Bound_args>()
+              (get<_Indexes>(_M_bound_args), __args)...);
+       }
 
       // Call as const
-      template<typename... _Args, int... _Indexes>
-        _Result
-        __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const
-        {
-          return _M_f(_Mu<_Bound_args>()
-                      (get<_Indexes>(_M_bound_args), __args)...);
-        }
+      template<typename _Res, typename... _Args, int... _Indexes>
+       _Result
+       __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
+           typename __disable_if_void<_Res>::type = 0) const
+       {
+         return _M_f(_Mu<_Bound_args>()
+                     (get<_Indexes>(_M_bound_args), __args)...);
+       }
+
+      // Call as const, return void
+      template<typename _Res, typename... _Args, int... _Indexes>
+       void
+       __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
+           typename __enable_if_void<_Res>::type = 0) const
+       {
+         _M_f(_Mu<_Bound_args>()
+              (get<_Indexes>(_M_bound_args),  __args)...);
+       }
 
       // Call as volatile
-      template<typename... _Args, int... _Indexes>
-        _Result
-        __call(const tuple<_Args...>& __args, 
-               _Index_tuple<_Indexes...>) volatile
-        {
-          return _M_f(_Mu<_Bound_args>()
-                      (get<_Indexes>(_M_bound_args), __args)...);
-        }
+      template<typename _Res, typename... _Args, int... _Indexes>
+       _Result
+       __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
+           typename __disable_if_void<_Res>::type = 0) volatile
+       {
+         return _M_f(_Mu<_Bound_args>()
+                     (__volget<_Indexes>(_M_bound_args), __args)...);
+       }
+
+      // Call as volatile, return void
+      template<typename _Res, typename... _Args, int... _Indexes>
+       void
+       __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
+           typename __enable_if_void<_Res>::type = 0) volatile
+       {
+         _M_f(_Mu<_Bound_args>()
+              (__volget<_Indexes>(_M_bound_args), __args)...);
+       }
 
       // Call as const volatile
-      template<typename... _Args, int... _Indexes>
-        _Result
-        __call(const tuple<_Args...>& __args, 
-               _Index_tuple<_Indexes...>) const volatile
-        {
-          return _M_f(_Mu<_Bound_args>()
-                      (get<_Indexes>(_M_bound_args), __args)...);
-        }
+      template<typename _Res, typename... _Args, int... _Indexes>
+       _Result
+       __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
+           typename __disable_if_void<_Res>::type = 0) const volatile
+       {
+         return _M_f(_Mu<_Bound_args>()
+                     (__volget<_Indexes>(_M_bound_args), __args)...);
+       }
+
+      // Call as const volatile, return void
+      template<typename _Res, typename... _Args, int... _Indexes>
+       void
+       __call(tuple<_Args...>&& __args,
+              _Index_tuple<_Indexes...>,
+           typename __enable_if_void<_Res>::type = 0) const volatile
+       {
+         _M_f(_Mu<_Bound_args>()
+              (__volget<_Indexes>(_M_bound_args), __args)...);
+       }
 
     public:
       typedef _Result result_type;
 
-      explicit
-      _Bind_result(_Functor __f, _Bound_args... __bound_args)
-      : _M_f(__f), _M_bound_args(__bound_args...) { }
+      template<typename... _Args>
+       explicit _Bind_result(const _Functor& __f, _Args&&... __args)
+       : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
+       { }
+
+      template<typename... _Args>
+       explicit _Bind_result(_Functor&& __f, _Args&&... __args)
+       : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
+       { }
+
+      _Bind_result(const _Bind_result&) = default;
+
+      _Bind_result(_Bind_result&& __b)
+      : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
+      { }
 
       // Call unqualified
       template<typename... _Args>
-        result_type
-        operator()(_Args&... __args)
-        {
-          return this->__call(tie(__args...), _Bound_indexes());
-        }
+       result_type
+       operator()(_Args&&... __args)
+       {
+         return this->__call<_Result>(
+             std::forward_as_tuple(std::forward<_Args>(__args)...),
+             _Bound_indexes());
+       }
 
       // Call as const
       template<typename... _Args>
-        result_type
-        operator()(_Args&... __args) const
-        {
-          return this->__call(tie(__args...), _Bound_indexes());
-        }
+       result_type
+       operator()(_Args&&... __args) const
+       {
+         return this->__call<_Result>(
+             std::forward_as_tuple(std::forward<_Args>(__args)...),
+             _Bound_indexes());
+       }
 
       // Call as volatile
       template<typename... _Args>
-        result_type
-        operator()(_Args&... __args) volatile
-        {
-          return this->__call(tie(__args...), _Bound_indexes());
-        }
+       result_type
+       operator()(_Args&&... __args) volatile
+       {
+         return this->__call<_Result>(
+             std::forward_as_tuple(std::forward<_Args>(__args)...),
+             _Bound_indexes());
+       }
 
       // Call as const volatile
       template<typename... _Args>
-        result_type
-        operator()(_Args&... __args) const volatile
-        {
-          return this->__call(tie(__args...), _Bound_indexes());
-        }
+       result_type
+       operator()(_Args&&... __args) const volatile
+       {
+         return this->__call<_Result>(
+             std::forward_as_tuple(std::forward<_Args>(__args)...),
+             _Bound_indexes());
+       }
     };
 
-  /// Class template _Bind is always a bind expression.
+  /**
+   *  @brief Class template _Bind is always a bind expression.
+   *  @ingroup binders
+   */
   template<typename _Signature>
     struct is_bind_expression<_Bind<_Signature> >
-    { static const bool value = true; };
+    : public true_type { };
 
-  template<typename _Signature>
-    const bool is_bind_expression<_Bind<_Signature> >::value;
-
-  /// Class template _Bind_result is always a bind expression.
+  /**
+   *  @brief Class template _Bind is always a bind expression.
+   *  @ingroup binders
+   */
   template<typename _Result, typename _Signature>
     struct is_bind_expression<_Bind_result<_Result, _Signature> >
-    { static const bool value = true; };
+    : public true_type { };
 
-  template<typename _Result, typename _Signature>
-    const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value;
+  // Trait type used to remove std::bind() from overload set via SFINAE
+  // when first argument has integer type, so that std::bind() will
+  // not be a better match than ::bind() from the BSD Sockets API.
+  template<typename _Tp>
+    class __is_socketlike
+    {
+      typedef typename decay<_Tp>::type _Tp2;
+    public:
+      static const bool value =
+       is_integral<_Tp2>::value || is_enum<_Tp2>::value;
+    };
 
-  /// bind
-  template<typename _Functor, typename... _ArgTypes>
-    inline
-    _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
-    bind(_Functor __f, _ArgTypes... __args)
+  template<bool _SocketLike, typename _Func, typename... _BoundArgs>
+    struct _Bind_helper
     {
-      typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
+      typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
+       __maybe_type;
+      typedef typename __maybe_type::type __func_type;
+      typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
+    };
+
+  // Partial specialization for is_socketlike == true, does not define
+  // nested type so std::bind() will not participate in overload resolution
+  // when the first argument might be a socket file descriptor.
+  template<typename _Func, typename... _BoundArgs>
+    struct _Bind_helper<true, _Func, _BoundArgs...>
+    { };
+
+  /**
+   *  @brief Function template for std::bind.
+   *  @ingroup binders
+   */
+  template<typename _Func, typename... _BoundArgs>
+    inline typename
+    _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
+    bind(_Func&& __f, _BoundArgs&&... __args)
+    {
+      typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
+      typedef typename __helper_type::__maybe_type __maybe_type;
+      typedef typename __helper_type::type __result_type;
+      return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
+                          std::forward<_BoundArgs>(__args)...);
+    }
+
+  template<typename _Result, typename _Func, typename... _BoundArgs>
+    struct _Bindres_helper
+    {
+      typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
+       __maybe_type;
       typedef typename __maybe_type::type __functor_type;
-      typedef _Bind<__functor_type(_ArgTypes...)> __result_type;
-      return __result_type(__maybe_type::__do_wrap(__f), __args...);
-    } 
+      typedef _Bind_result<_Result,
+                          __functor_type(typename decay<_BoundArgs>::type...)>
+       type;
+    };
 
-  template<typename _Result, typename _Functor, typename... _ArgTypes>
+  /**
+   *  @brief Function template for std::bind<R>.
+   *  @ingroup binders
+   */
+  template<typename _Result, typename _Func, typename... _BoundArgs>
     inline
-    _Bind_result<_Result,
-                typename _Maybe_wrap_member_pointer<_Functor>::type
-                            (_ArgTypes...)>
-    bind(_Functor __f, _ArgTypes... __args)
+    typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
+    bind(_Func&& __f, _BoundArgs&&... __args)
     {
-      typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type;
-      typedef typename __maybe_type::type __functor_type;
-      typedef _Bind_result<_Result, __functor_type(_ArgTypes...)>
-       __result_type;
-      return __result_type(__maybe_type::__do_wrap(__f), __args...);
+      typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
+      typedef typename __helper_type::__maybe_type __maybe_type;
+      typedef typename __helper_type::type __result_type;
+      return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
+                          std::forward<_BoundArgs>(__args)...);
+    }
+
+  template<typename _Signature>
+    struct _Bind_simple;
+
+  template<typename _Callable, typename... _Args>
+    struct _Bind_simple<_Callable(_Args...)>
+    {
+      typedef typename result_of<_Callable(_Args...)>::type result_type;
+
+      template<typename... _Args2, typename = typename
+               enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
+        explicit
+        _Bind_simple(const _Callable& __callable, _Args2&&... __args)
+        : _M_bound(__callable, std::forward<_Args2>(__args)...)
+        { }
+
+      template<typename... _Args2, typename = typename
+               enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
+        explicit
+        _Bind_simple(_Callable&& __callable, _Args2&&... __args)
+        : _M_bound(std::move(__callable), std::forward<_Args2>(__args)...)
+        { }
+
+      _Bind_simple(const _Bind_simple&) = default;
+      _Bind_simple(_Bind_simple&&) = default;
+
+      result_type
+      operator()()
+      {
+        typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
+        return _M_invoke(_Indices());
+      }
+
+    private:
+
+      template<int... _Indices>
+        typename result_of<_Callable(_Args...)>::type
+        _M_invoke(_Index_tuple<_Indices...>)
+        {
+         // std::bind always forwards bound arguments as lvalues,
+         // but this type can call functions which only accept rvalues.
+          return std::forward<_Callable>(std::get<0>(_M_bound))(
+              std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
+        }
+
+      std::tuple<_Callable, _Args...> _M_bound;
+    };
+
+  template<typename _Func, typename... _BoundArgs>
+    struct _Bind_simple_helper
+    {
+      typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
+        __maybe_type;
+      typedef typename __maybe_type::type __func_type;
+      typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
+               __type;
+    };
+
+  // Simplified version of std::bind for internal use, without support for
+  // unbound arguments, placeholders or nested bind expressions.
+  template<typename _Callable, typename... _Args>
+    typename _Bind_simple_helper<_Callable, _Args...>::__type
+    __bind_simple(_Callable&& __callable, _Args&&... __args)
+    {
+      typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
+      typedef typename __helper_type::__maybe_type __maybe_type;
+      typedef typename __helper_type::__type __result_type;
+      return __result_type(
+          __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
+          std::forward<_Args>(__args)...);
     }
 
   /**
@@ -1387,14 +1575,11 @@ namespace std
    *  operator() is called with an empty target.
    *  @ingroup exceptions
    */
-  class bad_function_call : public std::exception { };
-
-  /**
-   *  The integral constant expression 0 can be converted into a
-   *  pointer to this type. It is used by the function template to
-   *  accept NULL pointers.
-   */
-  struct _M_clear_type;
+  class bad_function_call : public std::exception
+  {
+  public:
+    virtual ~bad_function_call() throw();
+  };
 
   /**
    *  Trait identifying "location-invariant" types, meaning that the
@@ -1403,11 +1588,9 @@ namespace std
    */
   template<typename _Tp>
     struct __is_location_invariant
-    : integral_constant<bool,
-                        (is_pointer<_Tp>::value
-                         || is_member_pointer<_Tp>::value)>
-    {
-    };
+    : integral_constant<bool, (is_pointer<_Tp>::value
+                              || is_member_pointer<_Tp>::value)>
+    { };
 
   class _Undefined_class;
 
@@ -1459,8 +1642,7 @@ namespace std
   template<typename _Tp>
     struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
     : __is_location_invariant<_Tp>
-    {
-    };
+    { };
 
   // Converts a reference to a function object into a callable
   // function object.
@@ -1494,11 +1676,11 @@ namespace std
       {
       protected:
        static const bool __stored_locally =
-        (__is_location_invariant<_Functor>::value
-         && sizeof(_Functor) <= _M_max_size
-         && __alignof__(_Functor) <= _M_max_align
-         && (_M_max_align % __alignof__(_Functor) == 0));
-       
+       (__is_location_invariant<_Functor>::value
+        && sizeof(_Functor) <= _M_max_size
+        && __alignof__(_Functor) <= _M_max_align
+        && (_M_max_align % __alignof__(_Functor) == 0));
+
        typedef integral_constant<bool, __stored_locally> _Local_storage;
 
        // Retrieve a pointer to the function object
@@ -1506,7 +1688,7 @@ namespace std
        _M_get_pointer(const _Any_data& __source)
        {
          const _Functor* __ptr =
-           __stored_locally? &__source._M_access<_Functor>()
+           __stored_locally? std::__addressof(__source._M_access<_Functor>())
            /* have stored a pointer */ : __source._M_access<_Functor*>();
          return const_cast<_Functor*>(__ptr);
        }
@@ -1535,14 +1717,14 @@ namespace std
        {
          __victim._M_access<_Functor>().~_Functor();
        }
-       
+
        // Destroying an object located on the heap.
        static void
        _M_destroy(_Any_data& __victim, false_type)
        {
          delete __victim._M_access<_Functor*>();
        }
-       
+
       public:
        static bool
        _M_manager(_Any_data& __dest, const _Any_data& __source,
@@ -1558,7 +1740,7 @@ namespace std
            case __get_functor_ptr:
              __dest._M_access<_Functor*>() = _M_get_pointer(__source);
              break;
-             
+
            case __clone_functor:
              _M_clone(__dest, __source, _Local_storage());
              break;
@@ -1571,13 +1753,13 @@ namespace std
        }
 
        static void
-       _M_init_functor(_Any_data& __functor, const _Functor& __f)
-       { _M_init_functor(__functor, __f, _Local_storage()); }
-       
+       _M_init_functor(_Any_data& __functor, _Functor&& __f)
+       { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
+
        template<typename _Signature>
          static bool
          _M_not_empty_function(const function<_Signature>& __f)
-          { return static_cast<bool>(__f); }
+         { return static_cast<bool>(__f); }
 
        template<typename _Tp>
          static bool
@@ -1596,12 +1778,12 @@ namespace std
 
       private:
        static void
-       _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
-       { new (__functor._M_access()) _Functor(__f); }
+       _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
+       { new (__functor._M_access()) _Functor(std::move(__f)); }
 
        static void
-       _M_init_functor(_Any_data& __functor, const _Functor& __f, false_type)
-       { __functor._M_access<_Functor*>() = new _Functor(__f); }
+       _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
+       { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
       };
 
     template<typename _Functor>
@@ -1625,7 +1807,7 @@ namespace std
              __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
              return is_const<_Functor>::value;
              break;
-             
+
            default:
              _Base::_M_manager(__dest, __source, __op);
            }
@@ -1641,7 +1823,7 @@ namespace std
       };
 
     _Function_base() : _M_manager(0) { }
-    
+
     ~_Function_base()
     {
       if (_M_manager)
@@ -1652,7 +1834,7 @@ namespace std
     bool _M_empty() const { return !_M_manager; }
 
     typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
-                                  _Manager_operation);
+                                 _Manager_operation);
 
     _Any_data     _M_functor;
     _Manager_type _M_manager;
@@ -1671,8 +1853,8 @@ namespace std
       static _Res
       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
       {
-        return (*_Base::_M_get_pointer(__functor))(
-            std::forward<_ArgTypes>(__args)...);
+       return (*_Base::_M_get_pointer(__functor))(
+           std::forward<_ArgTypes>(__args)...);
       }
     };
 
@@ -1686,8 +1868,8 @@ namespace std
       static void
       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
       {
-        (*_Base::_M_get_pointer(__functor))(
-            std::forward<_ArgTypes>(__args)...);
+       (*_Base::_M_get_pointer(__functor))(
+           std::forward<_ArgTypes>(__args)...);
       }
     };
 
@@ -1701,8 +1883,8 @@ namespace std
       static _Res
       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
       {
-        return __callable_functor(**_Base::_M_get_pointer(__functor))(
-              std::forward<_ArgTypes>(__args)...);
+       return __callable_functor(**_Base::_M_get_pointer(__functor))(
+             std::forward<_ArgTypes>(__args)...);
       }
     };
 
@@ -1716,32 +1898,32 @@ namespace std
       static void
       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
       {
-        __callable_functor(**_Base::_M_get_pointer(__functor))(
-            std::forward<_ArgTypes>(__args)...);
+       __callable_functor(**_Base::_M_get_pointer(__functor))(
+           std::forward<_ArgTypes>(__args)...);
       }
     };
 
-  template<typename _Class, typename _Member, typename _Res, 
-           typename... _ArgTypes>
+  template<typename _Class, typename _Member, typename _Res,
+          typename... _ArgTypes>
     class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
     : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
     {
       typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
-        _Base;
+       _Base;
 
      public:
       static _Res
       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
       {
-        return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
-            std::forward<_ArgTypes>(__args)...);
+       return mem_fn(_Base::_M_get_pointer(__functor)->__value)(
+           std::forward<_ArgTypes>(__args)...);
       }
     };
 
   template<typename _Class, typename _Member, typename... _ArgTypes>
     class _Function_handler<void(_ArgTypes...), _Member _Class::*>
     : public _Function_base::_Base_manager<
-                 _Simple_type_wrapper< _Member _Class::* > >
+                _Simple_type_wrapper< _Member _Class::* > >
     {
       typedef _Member _Class::* _Functor;
       typedef _Simple_type_wrapper<_Functor> _Wrapper;
@@ -1750,62 +1932,68 @@ namespace std
      public:
       static bool
       _M_manager(_Any_data& __dest, const _Any_data& __source,
-                 _Manager_operation __op)
+                _Manager_operation __op)
       {
-        switch (__op)
+       switch (__op)
          {
 #ifdef __GXX_RTTI
          case __get_type_info:
            __dest._M_access<const type_info*>() = &typeid(_Functor);
            break;
-#endif     
+#endif
          case __get_functor_ptr:
            __dest._M_access<_Functor*>() =
              &_Base::_M_get_pointer(__source)->__value;
            break;
-           
+
          default:
            _Base::_M_manager(__dest, __source, __op);
          }
-        return false;
+       return false;
       }
 
       static void
       _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
       {
        mem_fn(_Base::_M_get_pointer(__functor)->__value)(
-            std::forward<_ArgTypes>(__args)...);
+           std::forward<_ArgTypes>(__args)...);
       }
     };
 
-  /// class function
+  /**
+   *  @brief Primary class template for std::function.
+   *  @ingroup functors
+   *
+   *  Polymorphic function wrapper.
+   */
   template<typename _Res, typename... _ArgTypes>
     class function<_Res(_ArgTypes...)>
     : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
       private _Function_base
     {
       typedef _Res _Signature_type(_ArgTypes...);
-      
+
       struct _Useless { };
-      
+
     public:
       typedef _Res result_type;
-      
+
       // [3.7.2.1] construct/copy/destroy
-      
+
       /**
        *  @brief Default construct creates an empty function call wrapper.
        *  @post @c !(bool)*this
        */
-      explicit
-      function() : _Function_base() { }
-      
+      function() noexcept
+      : _Function_base() { }
+
       /**
-       *  @brief Default construct creates an empty function call wrapper.
+       *  @brief Creates an empty function call wrapper.
        *  @post @c !(bool)*this
        */
-      function(_M_clear_type*) : _Function_base() { }
-      
+      function(nullptr_t) noexcept
+      : _Function_base() { }
+
       /**
        *  @brief %Function copy constructor.
        *  @param x A %function object with identical call signature.
@@ -1825,11 +2013,11 @@ namespace std
        */
       function(function&& __x) : _Function_base()
       {
-        __x.swap(*this);
+       __x.swap(*this);
       }
 
       // TODO: needs allocator_arg_t
-      
+
       /**
        *  @brief Builds a %function that targets a copy of the incoming
        *  function object.
@@ -1847,10 +2035,10 @@ namespace std
        *  reference_wrapper<F>, this function will not throw.
        */
       template<typename _Functor>
-        function(_Functor __f,
-                 typename __gnu_cxx::__enable_if<
-                           !is_integral<_Functor>::value, _Useless>::__type
-                   = _Useless());
+       function(_Functor __f,
+                typename enable_if<
+                          !is_integral<_Functor>::value, _Useless>::type
+                  = _Useless());
 
       /**
        *  @brief %Function assignment operator.
@@ -1867,8 +2055,8 @@ namespace std
       function&
       operator=(const function& __x)
       {
-        function(__x).swap(*this);
-        return *this;
+       function(__x).swap(*this);
+       return *this;
       }
 
       /**
@@ -1885,8 +2073,8 @@ namespace std
       function&
       operator=(function&& __x)
       {
-        function(std::move(__x)).swap(*this);
-        return *this;
+       function(std::move(__x)).swap(*this);
+       return *this;
       }
 
       /**
@@ -1897,15 +2085,15 @@ namespace std
        *  The target of @c *this is deallocated, leaving it empty.
        */
       function&
-      operator=(_M_clear_type*)
+      operator=(nullptr_t)
       {
-        if (_M_manager)
+       if (_M_manager)
          {
            _M_manager(_M_functor, _M_functor, __destroy_functor);
            _M_manager = 0;
            _M_invoker = 0;
          }
-        return *this;
+       return *this;
       }
 
       /**
@@ -1925,26 +2113,24 @@ namespace std
        *  reference_wrapper<F>, this function will not throw.
        */
       template<typename _Functor>
-        typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
-                                       function&>::__type
-       operator=(_Functor __f)
+       typename enable_if<!is_integral<_Functor>::value, function&>::type
+       operator=(_Functor&& __f)
        {
-         function(__f).swap(*this);
+         function(std::forward<_Functor>(__f)).swap(*this);
          return *this;
        }
 
       /// @overload
       template<typename _Functor>
-        typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
-                                       function&>::__type
-       operator=(reference_wrapper<_Functor> __f)
+       typename enable_if<!is_integral<_Functor>::value, function&>::type
+       operator=(reference_wrapper<_Functor> __f) noexcept
        {
          function(__f).swap(*this);
          return *this;
        }
 
       // [3.7.2.2] function modifiers
-      
+
       /**
        *  @brief Swap the targets of two %function objects.
        *  @param f A %function with identical call signature.
@@ -1954,27 +2140,22 @@ namespace std
        */
       void swap(function& __x)
       {
-       _Any_data __old_functor = _M_functor;
-       _M_functor = __x._M_functor;
-       __x._M_functor = __old_functor;
-       _Manager_type __old_manager = _M_manager;
-       _M_manager = __x._M_manager;
-       __x._M_manager = __old_manager;
-       _Invoker_type __old_invoker = _M_invoker;
-       _M_invoker = __x._M_invoker;
-       __x._M_invoker = __old_invoker;
+       std::swap(_M_functor, __x._M_functor);
+       std::swap(_M_manager, __x._M_manager);
+       std::swap(_M_invoker, __x._M_invoker);
       }
 
       // TODO: needs allocator_arg_t
       /*
       template<typename _Functor, typename _Alloc>
-        void
-        assign(_Functor __f, const _Alloc& __a)
-        {
-          function(__f, __a).swap(*this);
-        }
+       void
+       assign(_Functor&& __f, const _Alloc& __a)
+       {
+         function(allocator_arg, __a,
+                  std::forward<_Functor>(__f)).swap(*this);
+       }
       */
-      
+
       // [3.7.2.3] function capacity
 
       /**
@@ -1985,7 +2166,7 @@ namespace std
        *
        *  This function will not throw an %exception.
        */
-      explicit operator bool() const
+      explicit operator bool() const noexcept
       { return !_M_empty(); }
 
       // [3.7.2.4] function invocation
@@ -2011,8 +2192,8 @@ namespace std
        *
        *  This function will not throw an %exception.
        */
-      const type_info& target_type() const;
-      
+      const type_info& target_type() const noexcept;
+
       /**
        *  @brief Access the stored target function object.
        *
@@ -2022,23 +2203,18 @@ namespace std
        *
        * This function will not throw an %exception.
        */
-      template<typename _Functor>       _Functor* target();
-      
+      template<typename _Functor>       _Functor* target() noexcept;
+
       /// @overload
-      template<typename _Functor> const _Functor* target() const;
+      template<typename _Functor> const _Functor* target() const noexcept;
 #endif
 
-      // deleted overloads
-      template<typename _Res2, typename... _ArgTypes2>
-       void operator==(const function<_Res2(_ArgTypes2...)>&) const = delete;
-      template<typename _Res2, typename... _ArgTypes2>
-       void operator!=(const function<_Res2(_ArgTypes2...)>&) const = delete;
-
     private:
       typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
       _Invoker_type _M_invoker;
   };
 
+  // Out-of-line member definitions.
   template<typename _Res, typename... _ArgTypes>
     function<_Res(_ArgTypes...)>::
     function(const function& __x)
@@ -2056,8 +2232,8 @@ namespace std
     template<typename _Functor>
       function<_Res(_ArgTypes...)>::
       function(_Functor __f,
-              typename __gnu_cxx::__enable_if<
-                       !is_integral<_Functor>::value, _Useless>::__type)
+              typename enable_if<
+                       !is_integral<_Functor>::value, _Useless>::type)
       : _Function_base()
       {
        typedef _Function_handler<_Signature_type, _Functor> _My_handler;
@@ -2066,7 +2242,7 @@ namespace std
          {
            _M_invoker = &_My_handler::_M_invoke;
            _M_manager = &_My_handler::_M_manager;
-           _My_handler::_M_init_functor(_M_functor, __f);
+           _My_handler::_M_init_functor(_M_functor, std::move(__f));
          }
       }
 
@@ -2076,13 +2252,7 @@ namespace std
     operator()(_ArgTypes... __args) const
     {
       if (_M_empty())
-        {
-#if __EXCEPTIONS
-          throw bad_function_call();
-#else
-          __builtin_abort();
-#endif
-        }
+       __throw_bad_function_call();
       return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
     }
 
@@ -2090,14 +2260,14 @@ namespace std
   template<typename _Res, typename... _ArgTypes>
     const type_info&
     function<_Res(_ArgTypes...)>::
-    target_type() const
+    target_type() const noexcept
     {
       if (_M_manager)
-        {
-          _Any_data __typeinfo_result;
-          _M_manager(__typeinfo_result, _M_functor, __get_type_info);
-          return *__typeinfo_result._M_access<const type_info*>();
-        }
+       {
+         _Any_data __typeinfo_result;
+         _M_manager(__typeinfo_result, _M_functor, __get_type_info);
+         return *__typeinfo_result._M_access<const type_info*>();
+       }
       else
        return typeid(void);
     }
@@ -2106,7 +2276,7 @@ namespace std
     template<typename _Functor>
       _Functor*
       function<_Res(_ArgTypes...)>::
-      target()
+      target() noexcept
       {
        if (typeid(_Functor) == target_type() && _M_manager)
          {
@@ -2125,7 +2295,7 @@ namespace std
     template<typename _Functor>
       const _Functor*
       function<_Res(_ArgTypes...)>::
-      target() const
+      target() const noexcept
       {
        if (typeid(_Functor) == target_type() && _M_manager)
          {
@@ -2149,13 +2319,13 @@ namespace std
    */
   template<typename _Res, typename... _Args>
     inline bool
-    operator==(const function<_Res(_Args...)>& __f, _M_clear_type*)
+    operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
     { return !static_cast<bool>(__f); }
 
   /// @overload
   template<typename _Res, typename... _Args>
     inline bool
-    operator==(_M_clear_type*, const function<_Res(_Args...)>& __f)
+    operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
     { return !static_cast<bool>(__f); }
 
   /**
@@ -2167,13 +2337,13 @@ namespace std
    */
   template<typename _Res, typename... _Args>
     inline bool
-    operator!=(const function<_Res(_Args...)>& __f, _M_clear_type*)
+    operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
     { return static_cast<bool>(__f); }
 
   /// @overload
   template<typename _Res, typename... _Args>
     inline bool
-    operator!=(_M_clear_type*, const function<_Res(_Args...)>& __f)
+    operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
     { return static_cast<bool>(__f); }
 
   // [20.7.15.2.7] specialized algorithms
@@ -2187,7 +2357,9 @@ namespace std
     inline void
     swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
     { __x.swap(__y); }
-}
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
 
 #endif // __GXX_EXPERIMENTAL_CXX0X__