1

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?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • #2 is a declaration, #1 is definition. – Rakib May 31 '14 at 04:47
  • 2
    @RakibulHasan every definition is declaration (see 3.1)). – St.Antario May 31 '14 at 04:48
  • yes. I just tried to focus on that on definition . more accurate would be #1 is declaration+definition. – Rakib May 31 '14 at 04:50
  • @RakibulHasan I don't think so that it is make a sense to name #1 as declaration+definition because set of all definitions is a subset of all declarations. But my questions is not about it. I want to clarify what exactly declaration will be found in my case? I read clause 3.4.1 about unqualified name lookup, but don't understand that yet. Can you clarify this moment? – St.Antario May 31 '14 at 04:56
  • if compiler does not reorder statements, it should find `int a` first, and according to the first statement, it should stop immediately. – Rakib May 31 '14 at 05:11

1 Answers1

2

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.

M.M
  • 138,810
  • 21
  • 208
  • 365