functional (function::function): Move construct target.
authorJonathan Wakely <jwakely.gcc@gmail.com>
Tue, 15 Dec 2009 17:42:47 +0000 (17:42 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 15 Dec 2009 17:42:47 +0000 (17:42 +0000)
2009-12-15  Jonathan Wakely  <jwakely.gcc@gmail.com>

* include/std/functional (function::function): Move construct target.
(function::operator=): Use perfect forwarding for argument.
(function::operator()): Use new __throw_bad_function_call.
* include/bits/functexcept.h (__throw_bad_function_call): Declare.
* src/functexcept.cc (__throw_bad_function_call): Define.
* config/abi/pre/gnu.ver: Add new symbol.
* testsuite/20_util/function/cons/move_target.cc: New.
* testsuite/20_util/function/assign/move_target.cc: New.

From-SVN: r155261

libstdc++-v3/ChangeLog
libstdc++-v3/config/abi/pre/gnu.ver
libstdc++-v3/include/bits/functexcept.h
libstdc++-v3/include/std/functional
libstdc++-v3/src/functexcept.cc
libstdc++-v3/testsuite/20_util/function/assign/move_target.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/function/cons/move_target.cc [new file with mode: 0644]

index cb9ce55..4605d04 100644 (file)
@@ -1,3 +1,14 @@
+2009-12-15  Jonathan Wakely  <jwakely.gcc@gmail.com>
+
+       * include/std/functional (function::function): Move construct target.
+       (function::operator=): Use perfect forwarding for argument.
+       (function::operator()): Use new __throw_bad_function_call.
+       * include/bits/functexcept.h (__throw_bad_function_call): Declare.
+       * src/functexcept.cc (__throw_bad_function_call): Define.
+       * config/abi/pre/gnu.ver: Add new symbol.
+       * testsuite/20_util/function/cons/move_target.cc: New.
+       * testsuite/20_util/function/assign/move_target.cc: New.
+
 2009-12-15  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/42379
index 4ed1cfe..c40288a 100644 (file)
@@ -1068,6 +1068,8 @@ GLIBCXX_3.4.14 {
     _ZNSs18_S_construct_aux_2*;
     _ZNSbIwSt11char_traitsIwESaIwEE18_S_construct_aux_2*;
 
+    _ZSt25__throw_bad_function_callv;
+
 } GLIBCXX_3.4.13;
 
 # Symbols in the support library (libsupc++) have their own tag.
index 6217481..5b2e9fc 100644 (file)
@@ -91,6 +91,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   void
   __throw_future_error(int) __attribute__((__noreturn__));
 
+  // Helpers for exception objects in <functional>
+  void
+  __throw_bad_function_call() __attribute__((__noreturn__));
+
 _GLIBCXX_END_NAMESPACE
 
 #endif
index eb10b34..19503bd 100644 (file)
@@ -1571,8 +1571,8 @@ 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
@@ -1595,13 +1595,13 @@ namespace std
          { return true; }
 
       private:
-       static void
-       _M_init_functor(_Any_data& __functor, const _Functor& __f, true_type)
-       { new (__functor._M_access()) _Functor(__f); }
+        static void
+       _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>
@@ -1927,9 +1927,9 @@ namespace std
       template<typename _Functor>
         typename __gnu_cxx::__enable_if<!is_integral<_Functor>::value,
                                        function&>::__type
-       operator=(_Functor __f)
+       operator=(_Functor&& __f)
        {
-         function(__f).swap(*this);
+         function(std::forward<_Functor>(__f)).swap(*this);
          return *this;
        }
 
@@ -1969,9 +1969,10 @@ namespace std
       /*
       template<typename _Functor, typename _Alloc>
         void
-        assign(_Functor __f, const _Alloc& __a)
+        assign(_Functor&& __f, const _Alloc& __a)
         {
-          function(__f, __a).swap(*this);
+          function(allocator_arg, __a,
+                   std::forward<_Functor>(__f)).swap(*this);
         }
       */
       
@@ -2066,7 +2067,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 +2077,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)...);
     }
 
index 352a6b7..d47eccb 100644 (file)
@@ -29,6 +29,7 @@
 #include <ios>
 #include <system_error>
 #include <future>
+#include <functional>
 
 #ifdef _GLIBCXX_USE_NLS
 # include <libintl.h>
@@ -104,6 +105,9 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   __throw_future_error(int __i)
   { throw future_error(future_errc(__i)); }
 
+  void
+  __throw_bad_function_call()
+  { throw bad_function_call(); }
 #else
   void
   __throw_bad_exception(void)
@@ -169,6 +173,10 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   __throw_future_error(int)
   { std::abort(); }
 
+  void
+  __throw_bad_function_call()
+  { std::abort(); }
+
 #endif //__EXCEPTIONS
 
 _GLIBCXX_END_NAMESPACE
diff --git a/libstdc++-v3/testsuite/20_util/function/assign/move_target.cc b/libstdc++-v3/testsuite/20_util/function/assign/move_target.cc
new file mode 100644 (file)
index 0000000..0a1c189
--- /dev/null
@@ -0,0 +1,47 @@
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2009 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
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <functional>
+
+struct moveable
+{
+  moveable() = default;
+  ~moveable() = default;
+  // target object must be CopyConstructible,
+  // but should not be copied during this test
+  moveable(const moveable& c) { throw "copied"; }
+  moveable& operator=(const moveable&) = delete;
+  moveable(moveable&&) { }
+
+  void operator()() const { }
+};
+
+void test01()
+{
+  std::function<void ()> f;
+  f = moveable();
+  f();
+}
+
+int main()
+{
+  test01();
+
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/20_util/function/cons/move_target.cc b/libstdc++-v3/testsuite/20_util/function/cons/move_target.cc
new file mode 100644 (file)
index 0000000..2396ca1
--- /dev/null
@@ -0,0 +1,46 @@
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2009 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
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <functional>
+
+struct moveable
+{
+  moveable() = default;
+  ~moveable() = default;
+  // target object must be CopyConstructible,
+  // but should not be copied during this test
+  moveable(const moveable& c) { throw "copied"; }
+  moveable& operator=(const moveable&) = delete;
+  moveable(moveable&&) { }
+
+  void operator()() const { }
+};
+
+void test01()
+{
+  std::function<void ()> f = moveable();
+  f();
+}
+
+int main()
+{
+  test01();
+
+  return 0;
+}