http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59165

            Bug ID: 59165
           Summary: gcc looks up begin(), end() for for-range loops for
                    ints in namespace std
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thakis at chromium dot org

This compiles, but shouldn't:

$ cat gcc4.8bug.cc 
// builds with gcc4.8, but shouldn't
namespace std {
int* begin(int i) { return (int*)0; }
int* end(int i) { return (int*)0; }
}

int main() {
  for (int a : 10) { }
}
$ gcc-4.8.1 -c gcc4.8bug.cc  -std=c++11
# works


The standard says that begin() and end() for foreach loops should be looked up
in the associated namespace of the type of the expression (6.5.4p1)

"""otherwise, begin-expr and end-expr are begin(__range) and end(__range),
respectively, where begin and end are looked up in the associated namespaces
(3.4.2). [ Note: Ordinary unqualified lookup (3.4.1) is not performed. — end
note ]"""

10 has type int, which is a fundamental type, and hence doesn't have an
associated namespace. So this shouldn't compile. (It doesn't compile in clang.)



$ gcc-4.8.1 --version
gcc-4.8.1 (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Reply via email to