[Bug c++/103583] Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Dec 6 14:35:55 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103583

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I think the implementation doesn't do anything special to handle this case. It
just looks for the members a.begin() and a.end() and then if they aren't found,
it goes ahead with rewriting the code to use begin(a) and end(a), and any
errors that result from that are shown straight to the user.

Another option would be to do lookup for begin(a) and end(a) with errors
suppressed, and if they aren't found, issue a custom diagnostic saying
something like "no suitable begin/end pair available for range-based 'for'
loop".


Clang seems to do something similar:

f.C:6:18: error: invalid range expression of type 'A'; no viable 'end' function
available
    for (auto it : a) { }
                 ^ ~


More information about the Gcc-bugs mailing list