[C++0x patch] move construct std::function target

Jonathan Wakely jwakely.gcc@gmail.com
Tue Dec 15 17:43:00 GMT 2009


This patch implements the requirement that std::function
move-constructs its target object, as well as my proposed resolution
to LWG issue 1288, see
http://home.roadrunner.com/~hinnant/issue_review/lwg-active.html#1288

I've added __throw_bad_function_call, which I'm also going to use in
std::packaged_task in another patch coming soon.


	* 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.


Tested x86_64/Linux and committed.
-------------- next part --------------
Index: include/std/functional
===================================================================
--- include/std/functional	(revision 155260)
+++ include/std/functional	(working copy)
@@ -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: include/bits/functexcept.h
===================================================================
--- include/bits/functexcept.h	(revision 155260)
+++ include/bits/functexcept.h	(working copy)
@@ -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: src/functexcept.cc
===================================================================
--- src/functexcept.cc	(revision 155260)
+++ src/functexcept.cc	(working copy)
@@ -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
Index: config/abi/pre/gnu.ver
===================================================================
--- config/abi/pre/gnu.ver	(revision 155260)
+++ config/abi/pre/gnu.ver	(working copy)
@@ -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: testsuite/20_util/function/cons/move_target.cc
===================================================================
--- testsuite/20_util/function/cons/move_target.cc	(revision 0)
+++ testsuite/20_util/function/cons/move_target.cc	(revision 0)
@@ -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;
+}
Index: testsuite/20_util/function/assign/move_target.cc
===================================================================
--- testsuite/20_util/function/assign/move_target.cc	(revision 0)
+++ testsuite/20_util/function/assign/move_target.cc	(revision 0)
@@ -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;
+}


More information about the Libstdc++ mailing list