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: unassig...@gcc.gnu.org
        ReportedBy: m...@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.

Reply via email to