From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B8D7D3858404; Wed, 29 Sep 2021 05:39:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8D7D3858404 From: "federico.kircheis at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/102482] Winit-list-lifetime false positive for temporaries with std::initializer_list Date: Wed, 29 Sep 2021 05:39:21 +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: 11.2.1 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: federico.kircheis at gmail dot com 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Sep 2021 05:39:21 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102482 --- Comment #4 from Federico Kircheis = --- Note that following equivalent snippet ---- #include struct span { span(std::initializer_list il) noexcept : begin(nullptr), size(il.size()) { begin =3D il.begin();} const int* begin; std::size_t size; }; ---- does not trigger the warning. > And the fabulous manual [...] Mhm, I should have done a little more research before opening this ticket. > * When a list constructor stores the "begin" pointer from the > "initializer_list" argument, this doesn't extend the lifetime of > the array, so if a class variable is constructed from a temporary > "initializer_list", the pointer is left dangling by the end of the > variable declaration statement. The first statement is correct (does not extend lifetime), but the second is not always (the pointer is not always left dangling). Also following snippet generates the warning: ---- #include struct span { span(std::initializer_list& il) noexcept : begin(il.begin()), size(il.size()) {} const int* begin; std::size_t size; }; ---- but I currently cannot imagine a scenario where this would dangle, as one cannot write "span({1,2,3})"=