re PR libstdc++/49668 ([C++0x] std::thread does not forward its args as rvalues)
[gcc.git] / libstdc++-v3 / include / std / functional
index 40cf87030b5f0629484d8ef1572a898199a33d87..df3f9ceb7b43248a94be5430368f6289e945b4e9 100644 (file)
@@ -437,28 +437,28 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
     public:
       typedef _Tp type;
 
-      reference_wrapper(_Tp& __indata)
+      reference_wrapper(_Tp& __indata) noexcept
       : _M_data(std::__addressof(__indata))
       { }
 
       reference_wrapper(_Tp&&) = delete;
 
-      reference_wrapper(const reference_wrapper<_Tp>& __inref):
-      _M_data(__inref._M_data)
+      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;
       }
 
-      operator _Tp&() const
+      operator _Tp&() const noexcept
       { return this->get(); }
 
       _Tp&
-      get() const
+      get() const noexcept
       { return *_M_data; }
 
       template<typename... _Args>
@@ -473,13 +473,13 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
   /// 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.
   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>
@@ -491,13 +491,13 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
   /// 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()); }
 
   // @} group functors
@@ -1210,7 +1210,8 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
 
       // Call as const
       template<typename... _Args, typename _Result
-       = decltype( std::declval<const _Functor>()(
+       = 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
@@ -1223,7 +1224,8 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
 
       // Call as volatile
       template<typename... _Args, typename _Result
-       = decltype( std::declval<volatile _Functor>()(
+       = 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
@@ -1236,7 +1238,8 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
 
       // Call as const volatile
       template<typename... _Args, typename _Result
-       = decltype( std::declval<const volatile _Functor>()(
+       = 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
@@ -1496,6 +1499,77 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
                           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)...);
+    }
+
   /**
    *  @brief Exception class thrown when class template function's
    *  operator() is called with an empty target.
@@ -1910,13 +1984,15 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
        *  @brief Default construct creates an empty function call wrapper.
        *  @post @c !(bool)*this
        */
-      function() : _Function_base() { }
+      function() noexcept
+      : _Function_base() { }
 
       /**
        *  @brief Creates an empty function call wrapper.
        *  @post @c !(bool)*this
        */
-      function(nullptr_t) : _Function_base() { }
+      function(nullptr_t) noexcept
+      : _Function_base() { }
 
       /**
        *  @brief %Function copy constructor.
@@ -2047,7 +2123,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
       /// @overload
       template<typename _Functor>
        typename enable_if<!is_integral<_Functor>::value, function&>::type
-       operator=(reference_wrapper<_Functor> __f)
+       operator=(reference_wrapper<_Functor> __f) noexcept
        {
          function(__f).swap(*this);
          return *this;
@@ -2090,7 +2166,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
        *
        *  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
@@ -2116,7 +2192,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
        *
        *  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.
@@ -2127,10 +2203,10 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
        *
        * 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
 
     private:
@@ -2184,7 +2260,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
   template<typename _Res, typename... _ArgTypes>
     const type_info&
     function<_Res(_ArgTypes...)>::
-    target_type() const
+    target_type() const noexcept
     {
       if (_M_manager)
        {
@@ -2200,7 +2276,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
     template<typename _Functor>
       _Functor*
       function<_Res(_ArgTypes...)>::
-      target()
+      target() noexcept
       {
        if (typeid(_Functor) == target_type() && _M_manager)
          {
@@ -2219,7 +2295,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
     template<typename _Functor>
       const _Functor*
       function<_Res(_ArgTypes...)>::
-      target() const
+      target() const noexcept
       {
        if (typeid(_Functor) == target_type() && _M_manager)
          {
@@ -2243,13 +2319,13 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
    */
   template<typename _Res, typename... _Args>
     inline bool
-    operator==(const function<_Res(_Args...)>& __f, nullptr_t)
+    operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
     { return !static_cast<bool>(__f); }
 
   /// @overload
   template<typename _Res, typename... _Args>
     inline bool
-    operator==(nullptr_t, const function<_Res(_Args...)>& __f)
+    operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
     { return !static_cast<bool>(__f); }
 
   /**
@@ -2261,13 +2337,13 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
    */
   template<typename _Res, typename... _Args>
     inline bool
-    operator!=(const function<_Res(_Args...)>& __f, nullptr_t)
+    operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
     { return static_cast<bool>(__f); }
 
   /// @overload
   template<typename _Res, typename... _Args>
     inline bool
-    operator!=(nullptr_t, 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