From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 0BC4B3858C55; Sun, 3 Mar 2024 08:26:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0BC4B3858C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709454387; bh=63lrZZpxeF7icAyiIwl279568kwcrlRpeOPGVhlLtAQ=; h=From:To:Subject:Date:From; b=KlFLeanIDO7JgzBDPtg9b6BQtWB7kBORso/ddETAz75YkmiPpQnHcI0g79trqhvcx f52lVust6frep3d4aZBLcKOw1HGneMyY8C6AJfEj/MC3yzfL/e+HJC0CMXeBJ4OIrn 3vu4cU3T/WLoayCaB8qIf1lMEs7uqXj7Xio2CVmo= From: "dilyan.palauzov at aegee dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/114220] New: False positive warning: possibly dangling reference to a temporary [-Wdangling-reference] Date: Sun, 03 Mar 2024 08:26:20 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dilyan.palauzov at aegee dot org 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: 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=3D114220 Bug ID: 114220 Summary: False positive warning: possibly dangling reference to a temporary [-Wdangling-reference] Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: dilyan.palauzov at aegee dot org Target Milestone: --- Created attachment 57598 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D57598&action=3Dedit The inlined programme This code #include #include static const std::unordered_map hashTable1 =3D {{2, "u"}}; static const std::unordered_map hashTable2 =3D {{= "u", "u"}}; const std::string& z1(int m); const std::string& z1(int m) { return hashTable1.at(m); } const std::string& z2(const std::string& m); const std::string& z2(const std::string& m) { return hashTable2.at(m); } const std::string& z3(); const std::string& z3() { return hashTable2.at("u"); } int main() { const std::string& b1 { z1(2) }; const std::string& b2 { z2("u") }; const std::string& b3 { z3() }; const std::string& f1 =3D hashTable1.at(2); const std::string& f2 =3D hashTable2.at("u"); printf("%s %s %s %s %s\n", b1.c_str(), b2.c_str(), f1.c_str(), f2.c_str(), b3.c_str()); } produces with g++ (GCC) 13.2.1 20240302: $ g++ -Wall -Wextra -o a a.cpp a.c: In function =E2=80=98int main()=E2=80=99: a.c:16:22: warning: possibly dangling reference to a temporary [-Wdangling-reference] 16 | const std::string& b2 { z2("u") }; | ^~ a.c:16:29: note: the temporary was destroyed at the end of the full express= ion =E2=80=98z2(std::__cxx11::basic_string(((const char*)"u"), std::allocator()))=E2=80=99 16 | const std::string& b2 { z2("u") }; | ~~^~~~~ So std::string as first template parameter is problematic (b2), when only t= he second template parameter is relevant (b3), but int as first template param= eter is fine (b1). I think this is a bug.=