https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125006
Bug ID: 125006
Summary: Using range based for loop produces bad diagnostic if
argument dependent lookup fails but unqualified lookup
succeeds
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: luigighiron at gmail dot com
Target Milestone: ---
The following program demonstrates a strange diagnostic:
int*begin(int){return 0;}
int*end(int){return 0;}
int main(){
for(int x:0);
}
Error (<source> is the name of the file on godbolt):
<source>:4:15: error: 'begin' was not declared in this scope; did you mean
'begin'?
4 | for(int x:0);
| ^
| begin
<source>:1:5: note: 'begin' declared here
1 | int*begin(int){return 0;}
| ^~~~~
The same error is repeated but for end too. A less awkward example would be to
use a type defined inside of another namespace, but to still have begin and end
defined in the global namespace. It seems unhelpful to mention changing the
name to begin when that is what the program is already using. More broadly, it
seems strange to suggest changing the name begin to something else when that is
not possible to do:
struct S{};
int*begi(S){return 0;}
int*end(S){return 0;}
int main(){
for(int x:S());
}
Error:
<source>:5:17: error: 'begin' was not declared in this scope; did you mean
'begi'?
5 | for(int x:S());
| ^
| begi
begi is correctly interpreted as a typo for begin, but there is no way to make
the range based for loop use that name instead.