libstdc++: Fix assigning nullptr to std::atomic<shared_ptr<T>> (LWG 3893)
authorJonathan Wakely <jwakely@redhat.com>
Wed, 22 Mar 2023 21:54:24 +0000 (21:54 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 22 Mar 2023 23:13:40 +0000 (23:13 +0000)
LWG voted this to Tentatively Ready recently.

libstdc++-v3/ChangeLog:

* include/bits/shared_ptr_atomic.h (atomic::operator=(nullptr_t)):
Add overload, as per LWG 3893.
* testsuite/20_util/shared_ptr/atomic/atomic_shared_ptr.cc:
Check assignment from nullptr.

libstdc++-v3/include/bits/shared_ptr_atomic.h
libstdc++-v3/testsuite/20_util/shared_ptr/atomic/atomic_shared_ptr.cc

index d142616f485f2259ad8432e9256f381a9968766c..2295b48e7326cbc4cc09b08c9d732e9965b51af8 100644 (file)
@@ -650,6 +650,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator=(shared_ptr<_Tp> __desired) noexcept
       { _M_impl.swap(__desired, memory_order_seq_cst); }
 
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // 3893. LWG 3661 broke atomic<shared_ptr<T>> a; a = nullptr;
+      void
+      operator=(nullptr_t) noexcept
+      { store(nullptr); }
+
       shared_ptr<_Tp>
       exchange(shared_ptr<_Tp> __desired,
               memory_order __o = memory_order_seq_cst) noexcept
index a1902745a3ef4ae9452846d472a8a52817493ac1..54cf2621ea1d26ea3e9d7e1c977834306ea0ef0d 100644 (file)
@@ -145,6 +145,14 @@ test_counting()
   VERIFY( counter == 2 );
 }
 
+void
+test_lwg3893()
+{
+  // LWG 3893. LWG 3661 broke atomic<shared_ptr<T>> a; a = nullptr;
+  std::atomic<std::shared_ptr<int>> a;
+  a = nullptr;
+}
+
 int
 main()
 {
@@ -152,4 +160,5 @@ main()
   test_atomic_shared_ptr();
   test_wait_notify();
   test_counting();
+  test_lwg3893();
 }