public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/54430] New: [C++11] Range Based For Loop lhs scoping issue
@ 2012-08-30 23:08 mrks at koios dot de
  2014-03-28 14:15 ` [Bug c++/54430] [C++11] For-Loop: Scope of iterating variable begins too early tclamb at ufl dot edu
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: mrks at koios dot de @ 2012-08-30 23:08 UTC (permalink / raw)
  To: gcc-bugs

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<MyType*> vec;
  public:
    const std::vector<MyType*>& 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.


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2015-10-05  8:53 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-30 23:08 [Bug c++/54430] New: [C++11] Range Based For Loop lhs scoping issue mrks at koios dot de
2014-03-28 14:15 ` [Bug c++/54430] [C++11] For-Loop: Scope of iterating variable begins too early tclamb at ufl dot edu
2014-03-28 14:16 ` ppluzhnikov at google dot com
2014-03-28 14:17 ` ppluzhnikov at google dot com
2014-03-28 17:29 ` redi at gcc dot gnu.org
2015-08-09  7:49 ` akim.demaille at gmail dot com
2015-08-09  7:51 ` akim.demaille at gmail dot com
2015-08-17  0:52 ` miyuki at gcc dot gnu.org
2015-09-15 10:16 ` redi at gcc dot gnu.org
2015-10-01 19:22 ` ville at gcc dot gnu.org
2015-10-01 19:24 ` ville.voutilainen at gmail dot com
2015-10-05  8:53 ` paolo.carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).