From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 25E3F3836791; Fri, 16 Dec 2022 14:30:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25E3F3836791 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1671201045; bh=b1ZmBF4HBSIRtLirlPyasIc6Gh6FXNeKg4k2UFYGcFQ=; h=From:To:Subject:Date:From; b=tbFyicngENB75KD5F1O9cI4zb7mknYid3eoId0TWfIjaCnCWdQXHHvoZvynm4eLsd 534gMCfWrOoBeQ/024LfEeBE+DH12pGefIuKh+3+yrp430oczC59rhVjPqdMnVfNu4 sCTCIjFdExEUm8sOAJ3VUjwU4h06iuSiNggdePD8= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-4745] libstdc++: Fix self-move for std::weak_ptr [PR108118] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: d386d399205e68e02b357dfcd96b9a4b91e4f295 X-Git-Newrev: 92eb0adc14a5f84acce7e5bc780b81b1544b24aa Message-Id: <20221216143045.25E3F3836791@sourceware.org> Date: Fri, 16 Dec 2022 14:30:45 +0000 (GMT) List-Id: https://gcc.gnu.org/g:92eb0adc14a5f84acce7e5bc780b81b1544b24aa commit r13-4745-g92eb0adc14a5f84acce7e5bc780b81b1544b24aa Author: Jonathan Wakely 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 +#include + +void +test_self_move() +{ + std::shared_ptr sp(new int(66)); + std::weak_ptr wp(sp); + wp = std::move(wp); // PR libstdc++/108118 + std::shared_ptr sp2(wp); + VERIFY(sp2 == sp); +} + +int main() +{ + test_self_move(); +}