functional (function::function): Move construct target.

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
This commit is contained in:
Jonathan Wakely 2009-12-15 17:42:47 +00:00 committed by Jonathan Wakely
parent 2a71eb48e4
commit ec903a9c42
7 changed files with 132 additions and 19 deletions

View 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

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

View 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

View 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)...);
}

View 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

View File

@ -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;
}

View File

@ -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;
}