1

I want to understand the namespace qualified name lookup rule. I'm trying to do that on the example:

namespace A
{
    int a=::b; //error: ‘::b’ has not been declared
}
int b=6;

There is a quote which I rely in my reasoning (3.4.3.2/2 N3797):

For a namespace X and name m, the namespace-qualified lookup set S(X, m) is defined as follows: Let S (X, m) be the set of all declarations of m in X and the inline namespace set of X (7.3.1). If S (X, m) is not empty, S(X, m) is S (X, m); otherwise, S(X, m) is the union of S(Ni , m) for all namespaces Ni nominated by using-directives in X and its inline namespace set.

Let X be a global scope. We have that S(X,b)={int b=6}. This implies that name lookup must be success. But in fact, the program is ill-formed. Might I don't understood this rule correctly?

  • The catch is literally in the text that you bolded. The name lookup *is* a success. However, `b` hasn't yet been added to the "set of all declarations in the global namespace". – Lilshieste Jun 04 '14 at 18:14
  • @Lilshieste But we can use a class data member inside a member function definition appeared before actual data member declaration. –  Jun 04 '14 at 18:19
  • But that is an issue of scope; not name lookup. (And 3.3.7/1 is what provides that capability for class types.) – Lilshieste Jun 04 '14 at 18:34
  • @Lilshieste I don't agree because unqualified name lookup described as it is corresponding to the member's class scope. This concepts are interrelated (not formally). –  Jun 05 '14 at 02:47
  • 3.4.1/1 mentions that "the scopes are searched for a declaration...". For class scope, order of declaration doesn't matter, per 3.3.7/1. For global scope, order of declaration *does* matter. But in both cases, declaration **must** occur before name lookup. – Lilshieste Jun 05 '14 at 11:57

1 Answers1

0

b is in global scope but it must be declare before where you are using it. like

int b=6;
namespace A
{
    int a=::b; 
}

(N 3690 Draft) 3.4.1 Unqualified name lookup 4. A name used in global scope, outside of any function, class or user-declared namespace, shall be declared before its use in global scope.

rajenpandit
  • 1,265
  • 1
  • 15
  • 21