Declaration found during unqualified name lookup

折月煮酒 提交于 2020-02-05 07:06:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!