From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 34C67385840B; Thu, 2 Nov 2023 08:01:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 34C67385840B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1698912068; bh=mb/JJL+aYRC0TAkw05MgBJLVi5w3qSi6OuC6xhaZOdc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=lPZQohuAR9GfurKOeu2qm7nXfzP2x2IWMjNiTOzv1Wmz4RuuwU6w5zW1rVSxb5gVL saSMMWY+nEI9ngu01wsWah2/s43QTmjbigeb5uBPRAFLoPg4xzgv26Ac08MnAYIHn9 tsiGnxZfsDLYmaDe6jk8/FnsyJuYd8Cuk4K6GevU= From: "federico at kircheis dot it" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/112335] missed optimization on reset and assign unique_ptr Date: Thu, 02 Nov 2023 08:01:07 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: federico at kircheis dot it X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112335 --- Comment #5 from Federico Kircheis --- Ok, the described case would be something like ---- std::unique_ptr t; __thread bool tt; inline s::~s() { if (tt) return; tt =3D true; t.reset(new s); tt =3D false; } std::unique_ptr t2; bar(t, t2); ---- The postcondition of p.reset() is that p =3D=3D nullptr. ---- void reset(pointer p =3D pointer()) noexcept; Preconditions: The expression get_deleter()(get()) is well-formed, has well-defined behavior, and does not throw exceptions. Effects: Assigns p to the stored pointer, and then if and only if the old v= alue of the stored pointer, old_p, was not equal to nullptr, calls get_deleter()(old_p). [Note: The order of these operations is significant because the call to get_deleter() may destroy *this. =E2=80=94 end note] Postconditions: get() =3D=3D p. [Note: The postcondition does not hold if t= he call to get_deleter() destroys *this since this->get() is no longer a valid expression. =E2=80=94 end note] ---- I do not think that the case you have in mind is supported by the standard,= but the postcondition does not hold with some deleters, so not sure if that cou= ld be relevant here=