Fix for LWG Issue 2415: Inconsistency between unique_ptr and shared_ptr

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@236953 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2015-05-10 13:59:45 +00:00
parent 928735abf1
commit 0ad232a882
3 changed files with 37 additions and 11 deletions

View File

@ -1639,7 +1639,7 @@ template <class _Traits, class _Tp>
struct __rebind_alloc_helper struct __rebind_alloc_helper
{ {
#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
typedef typename _Traits::template rebind_alloc<_Tp> type; typedef typename _Traits::template rebind_alloc<_Tp> type;
#else #else
typedef typename _Traits::template rebind_alloc<_Tp>::other type; typedef typename _Traits::template rebind_alloc<_Tp>::other type;
#endif #endif
@ -4277,9 +4277,16 @@ shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
>::type) >::type)
: __ptr_(__r.get()) : __ptr_(__r.get())
{ {
typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk; #if _LIBCPP_STD_VER > 11
__cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>()); if (__ptr_ == nullptr)
__enable_weak_this(__r.get()); __cntrl_ = nullptr;
else
#endif
{
typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
__enable_weak_this(__r.get());
}
__r.release(); __r.release();
} }
@ -4299,11 +4306,18 @@ shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
>::type) >::type)
: __ptr_(__r.get()) : __ptr_(__r.get())
{ {
typedef __shared_ptr_pointer<_Yp*, #if _LIBCPP_STD_VER > 11
reference_wrapper<typename remove_reference<_Dp>::type>, if (__ptr_ == nullptr)
allocator<_Yp> > _CntrlBlk; __cntrl_ = nullptr;
__cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>()); else
__enable_weak_this(__r.get()); #endif
{
typedef __shared_ptr_pointer<_Yp*,
reference_wrapper<typename remove_reference<_Dp>::type>,
allocator<_Yp> > _CntrlBlk;
__cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
__enable_weak_this(__r.get());
}
__r.release(); __r.release();
} }

View File

@ -67,7 +67,7 @@ int main()
pB = std::move(pA); pB = std::move(pA);
assert(B::count == 0); assert(B::count == 0);
assert(A::count == 0); assert(A::count == 0);
assert(pB.use_count() == 1); // assert(pB.use_count() == 1); // no longer true due to LWG 2415
assert(pA.get() == 0); assert(pA.get() == 0);
assert(pB.get() == ptrA); assert(pB.get() == ptrA);
} }
@ -101,7 +101,7 @@ int main()
pB = std::move(pA); pB = std::move(pA);
assert(B::count == 0); assert(B::count == 0);
assert(A::count == 0); assert(A::count == 0);
assert(pB.use_count() == 1); // assert(pB.use_count() == 1); // no longer true due to LWG 2415
assert(pA.get() == 0); assert(pA.get() == 0);
assert(pB.get() == ptrA); assert(pB.get() == ptrA);
} }

View File

@ -58,6 +58,9 @@ int A::count = 0;
void fn ( const std::shared_ptr<int> &) {} void fn ( const std::shared_ptr<int> &) {}
void fn ( const std::shared_ptr<B> &) { assert (false); } void fn ( const std::shared_ptr<B> &) { assert (false); }
template <typename T>
void assert_deleter ( T * ) { assert(false); }
int main() int main()
{ {
{ {
@ -100,4 +103,13 @@ int main()
throw_next = false; throw_next = false;
fn(std::unique_ptr<int>(new int)); fn(std::unique_ptr<int>(new int));
} }
#if __cplusplus >= 201402L
// LWG 2415
{
std::unique_ptr<int, void (*)(int*)> p(nullptr, assert_deleter<int>);
std::shared_ptr<int> p2(std::move(p)); // should not call deleter when going out of scope
}
#endif
} }