From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21220 invoked by alias); 25 Sep 2011 14:23:06 -0000 Received: (qmail 21211 invoked by uid 22791); 25 Sep 2011 14:23:05 -0000 X-SWARE-Spam-Status: No, hits=-2.9 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 25 Sep 2011 14:22:52 +0000 From: "schaub.johannes at googlemail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/48562] [C++0x] warn about uses of initializer_list that will lead to dangling pointers Date: Sun, 25 Sep 2011 14:53:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: schaub.johannes at googlemail dot com X-Bugzilla-Status: ASSIGNED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: paolo.carlini at oracle dot com X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-09/txt/msg01837.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48562 --- Comment #6 from Johannes Schaub 2011-09-25 14:22:33 UTC --- (In reply to comment #5) > Johannes, sorry about the dumb question: now I understand the issue decently > well - and after all boils down to adding a warning - but I'm not sure to > understand your code snippet: is it meant to crash at runtime? Trigger valgrind > errors? In the C++11 spec, it is said that the lifetime of the backing-up array is the same as the lifetime of the initializer_list object which was initialized by the array (not considering the DRs and their resolution that Jason has pointed to). My code was just meant to test whether GCC obeys those rules. struct X { X(int) { cout << "+"; } X(X const&) { cout << "+"; } ~X() { cout << "-"; } }; auto *p = new initalizer_list{1, 2, 3}; // ... not at this delete p; // C++11 requires "now" at this point ... (again not considering those DRs that revise these rules). I think that a warning against "({...})" would be useful too // fine initializer_list a{1, 2, 3}; // this is bad initializer_list b({1, 2, 3}); Second one is bad because it will destroy the array after initializing 'b', and won't lengthen the lifetime (because it will use the copy/move constructor).