问题
Consider the following simple example:
#include <iostream>
int a=5;//1
extern int a;//2
int main(){ cout << a; }
The standard said that (sec. 3.4/1):
Name lookup shall find an unambiguous declaration for the name
and (sec. 3.4.1/1):
name lookup ends as soon as a declaration is found for the name.
Question: What declaration (1 or 2) will be found in my case and why?
回答1:
That clause says that name lookup stops when it hits int a=5;
There is only one name here, a in the global namespace. It's not ambiguous because there is only one a, it doesn't matter if there are multiple declarations of a. Two declarations, one name. (The "ambiguous" case can only occur for class member name lookup, which is more fully described in that section).
I get the sense from your wording that you are expecting there to be some sort of different behaviour depending on whether 1 or 2 satisfies this clause; but there isn't.
来源:https://stackoverflow.com/questions/23966473/declaration-found-during-unqualified-name-lookup