Commit 38a9e5a6 by Jonathan Wakely Committed by Jonathan Wakely

PR69321 fix any_cast<T>(any*) for non-copyable T

	PR libstdc++/69321
	* include/experimental/any (__any_caster): Avoid instantiating
	manager function for types that can't be stored in any.
	* include/std/any (__any_caster): Likewise.
	* testsuite/20_util/any/misc/any_cast.cc: Test non-copyable type.
	* testsuite/experimental/any/misc/any_cast.cc: Likewise.

From-SVN: r244678
parent 01334be4
2017-01-20 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/69321
* include/experimental/any (__any_caster): Avoid instantiating
manager function for types that can't be stored in any.
* include/std/any (__any_caster): Likewise.
* testsuite/20_util/any/misc/any_cast.cc: Test non-copyable type.
* testsuite/experimental/any/misc/any_cast.cc: Likewise.
PR libstdc++/64903
* include/bits/stl_algo.h (is_partitioned): Use increment instead of
std::advance.
......
......@@ -415,7 +415,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
void* __any_caster(const any* __any)
{
if (__any->_M_manager != &any::_Manager<decay_t<_Tp>>::_S_manage)
struct _None { };
using _Up = decay_t<_Tp>;
using _Vp = conditional_t<is_copy_constructible<_Up>::value, _Up, _None>;
if (__any->_M_manager != &any::_Manager<_Vp>::_S_manage)
return nullptr;
any::_Arg __arg;
__any->_M_manager(any::_Op_access, __any, &__arg);
......
......@@ -511,11 +511,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
void* __any_caster(const any* __any)
{
if (__any->_M_manager != &any::_Manager<decay_t<_Tp>>::_S_manage)
return nullptr;
any::_Arg __arg;
__any->_M_manager(any::_Op_access, __any, &__arg);
return __arg._M_obj;
if constexpr (is_copy_constructible_v<decay_t<_Tp>>)
{
if (__any->_M_manager == &any::_Manager<decay_t<_Tp>>::_S_manage)
{
any::_Arg __arg;
__any->_M_manager(any::_Op_access, __any, &__arg);
return __arg._M_obj;
}
}
return nullptr;
}
/**
......
......@@ -118,10 +118,23 @@ void test04()
ExplicitCopy ec2{any_cast<ExplicitCopy>(std::move(x))};
}
void test05()
{
// PR libstdc++/69321
struct noncopyable {
noncopyable(noncopyable const&) = delete;
};
any a;
auto p = any_cast<noncopyable>(&a);
VERIFY( p == nullptr );
}
int main()
{
test01();
test02();
test03();
test04();
test05();
}
......@@ -105,9 +105,22 @@ void test03()
MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md)));
}
void test04()
{
// PR libstdc++/69321
struct noncopyable {
noncopyable(noncopyable const&) = delete;
};
any a;
auto p = any_cast<noncopyable>(&a);
VERIFY( p == nullptr );
}
int main()
{
test01();
test02();
test03();
test04();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment