[libcxx] r236953 - Fix for LWG Issue 2415: Inconsistency between unique_ptr and shared_ptr

Marshall Clow mclow.lists at gmail.com
Sun May 10 06:59:45 PDT 2015


Author: marshall
Date: Sun May 10 08:59:45 2015
New Revision: 236953

URL: http://llvm.org/viewvc/llvm-project?rev=236953&view=rev
Log:
Fix for LWG Issue 2415: Inconsistency between unique_ptr and shared_ptr

Modified:
    libcxx/trunk/include/memory
    libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp
    libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp

Modified: libcxx/trunk/include/memory
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/memory?rev=236953&r1=236952&r2=236953&view=diff
==============================================================================
--- libcxx/trunk/include/memory (original)
+++ libcxx/trunk/include/memory Sun May 10 08:59:45 2015
@@ -1639,7 +1639,7 @@ template <class _Traits, class _Tp>
 struct __rebind_alloc_helper
 {
 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
-	typedef typename _Traits::template rebind_alloc<_Tp>        type;
+    typedef typename _Traits::template rebind_alloc<_Tp>        type;
 #else
     typedef typename _Traits::template rebind_alloc<_Tp>::other type;
 #endif
@@ -4277,9 +4277,16 @@ shared_ptr<_Tp>::shared_ptr(unique_ptr<_
                             >::type)
     : __ptr_(__r.get())
 {
-    typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
-    __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
-    __enable_weak_this(__r.get());
+#if _LIBCPP_STD_VER > 11
+    if (__ptr_ == nullptr)
+        __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();
 }
 
@@ -4299,11 +4306,18 @@ shared_ptr<_Tp>::shared_ptr(unique_ptr<_
                             >::type)
     : __ptr_(__r.get())
 {
-    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());
+#if _LIBCPP_STD_VER > 11
+    if (__ptr_ == nullptr)
+        __cntrl_ = nullptr;
+    else
+#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();
 }
 

Modified: libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp?rev=236953&r1=236952&r2=236953&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.assign/unique_ptr_Y.pass.cpp Sun May 10 08:59:45 2015
@@ -67,7 +67,7 @@ int main()
             pB = std::move(pA);
             assert(B::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(pB.get() == ptrA);
         }
@@ -101,7 +101,7 @@ int main()
             pB = std::move(pA);
             assert(B::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(pB.get() == ptrA);
         }

Modified: libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp?rev=236953&r1=236952&r2=236953&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp Sun May 10 08:59:45 2015
@@ -58,6 +58,9 @@ int A::count = 0;
 void fn ( const std::shared_ptr<int> &) {}
 void fn ( const std::shared_ptr<B> &) { assert (false); }
 
+template <typename T>
+void assert_deleter ( T * ) { assert(false); }
+
 int main()
 {
     {
@@ -100,4 +103,13 @@ int main()
     throw_next = false;
     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
+    
 }





More information about the cfe-commits mailing list