public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r13-4745] libstdc++: Fix self-move for std::weak_ptr [PR108118]
Date: Fri, 16 Dec 2022 14:30:45 +0000 (GMT)	[thread overview]
Message-ID: <20221216143045.25E3F3836791@sourceware.org> (raw)

https://gcc.gnu.org/g:92eb0adc14a5f84acce7e5bc780b81b1544b24aa

commit r13-4745-g92eb0adc14a5f84acce7e5bc780b81b1544b24aa
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Dec 15 09:52:48 2022 +0000

    libstdc++: Fix self-move for std::weak_ptr [PR108118]
    
    I think an alternative fix would be something like:
    
      _M_ptr = std::exchange(rhs._M_ptr, nullptr);
      _M_refcount = std::move(rhs._M_refcount);
    
    The standard's move-and-swap implementation generates smaller code at
    all levels except -O0 and -Og, so it seems simplest to just do what the
    standard says.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/108118
            * include/bits/shared_ptr_base.h (weak_ptr::operator=):
            Implement as move-and-swap exactly as specified in the standard.
            * testsuite/20_util/weak_ptr/cons/self_move.cc: New test.

Diff:
---
 libstdc++-v3/include/bits/shared_ptr_base.h           |  4 +---
 .../testsuite/20_util/weak_ptr/cons/self_move.cc      | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index d9230b72203..c22b397a194 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -2049,9 +2049,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __weak_ptr&
       operator=(__weak_ptr&& __r) noexcept
       {
-	_M_ptr = __r._M_ptr;
-	_M_refcount = std::move(__r._M_refcount);
-	__r._M_ptr = nullptr;
+	__weak_ptr(std::move(__r)).swap(*this);
 	return *this;
       }
 
diff --git a/libstdc++-v3/testsuite/20_util/weak_ptr/cons/self_move.cc b/libstdc++-v3/testsuite/20_util/weak_ptr/cons/self_move.cc
new file mode 100644
index 00000000000..c890d2ba94d
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/weak_ptr/cons/self_move.cc
@@ -0,0 +1,19 @@
+// { dg-do run { target c++11 } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void
+test_self_move()
+{
+  std::shared_ptr<int> sp(new int(66));
+  std::weak_ptr<int> wp(sp);
+  wp = std::move(wp); // PR libstdc++/108118
+  std::shared_ptr<int> sp2(wp);
+  VERIFY(sp2 == sp);
+}
+
+int main()
+{
+  test_self_move();
+}

                 reply	other threads:[~2022-12-16 14:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221216143045.25E3F3836791@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).