From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14032 invoked by alias); 30 Aug 2012 23:08:53 -0000 Received: (qmail 14019 invoked by uid 22791); 30 Aug 2012 23:08:52 -0000 X-SWARE-Spam-Status: No, hits=-3.6 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; Thu, 30 Aug 2012 23:08:36 +0000 From: "mrks at koios dot de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/54430] New: [C++11] Range Based For Loop lhs scoping issue Date: Thu, 30 Aug 2012 23:08:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: mrks at koios dot de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: 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: 2012-08/txt/msg02023.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54430 Bug #: 54430 Summary: [C++11] Range Based For Loop lhs scoping issue Classification: Unclassified Product: gcc Version: 4.7.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned@gcc.gnu.org ReportedBy: mrks@koios.de The scope of the iterating variable begins too early, i.e. it is already available in the expression of the rhs. This is a problem if the rhs expression includes the same identifier which is shadowed by this issue. Simple case: int i[] = { }; for (int i : i); fails to compile because the rhs 'i' will be the same as the lhs 'i' which is not a valid expression for the range-based for-loop. The range-based for-loop is equivalent to some for-construct. According to the standard the above should compile because the scope of the lhs begins inside the body of this substituted for-loop. Just for completeness: The Evil case: class MyType { std::vector vec; public: const std::vector& foo() { return vec; } }; MyType * t = new MyType; for (MyType * t : t->foo()); this will not refuse to compile since everything is well-formed. The real problem is that foo isn't called on the previously defined t but on the new uninitialized t.