Rakete1111
Rakete1111

Reputation: 48928

Where is name lookup rule defined that finds the most immediate declaration of a name?

int i;
void f()
{
    int i{};
    {
        int a = i; // local or global 'i'?
    }
}

My question is not which i gets chosen, as it's clear that it's the local one, but rather, where in the standard that is specified.

The closest rule I could find is [basic.lookup.unqual]p6, which says:

In the definition of a function that is a member of namespace N, a name used after the function's declarator-id shall be declared before its use in the block in which it is used or in one of its enclosing blocks ([stmt.block]) or shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N's enclosing namespaces.

But there it just says that the name has to be declared sometime before the use; it's not what I'm looking for. The example in the same paragraph makes everything clearer as it says what scopes are searched in what order, but it's an example and as such not nominative.

Every other paragraph in [basic.lookup.unqual] doesn't apply to non-member functions. So my question is where in the standard is this specified?

Upvotes: 2

Views: 67

Answers (1)

melpomene
melpomene

Reputation: 85757

In [basic.scope.declarative] we have:

  1. Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity. In general, each particular name is valid only within some possibly discontiguous portion of program text called its scope. To determine the scope of a declaration, it is sometimes convenient to refer to the potential scope of a declaration. The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case, the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.

  2. [ Example: In

    int j = 24;
    int main() {
      int i = j, j;
      j = 42;
    }
    

    the identifier j is declared twice as a name (and used twice). The declarative region of the first j includes the entire example. The potential scope of the first j begins immediately after that j and extends to the end of the program, but its (actual) scope excludes the text between the , and the }. The declarative region of the second declaration of j (the j immediately before the semicolon) includes all the text between { and }, but its potential scope excludes the declaration of i. The scope of the second declaration of j is the same as its potential scope. — end example ]

(Emphasis mine.)

In your

int a = i;

example, i must refer to the local i because the global i is literally not in scope here.

As it says at the beginning of [basic.lookup.unqual]:

In all the cases listed in [basic.lookup.unqual], the scopes are searched for a declaration in the order listed in each of the respective categories [...]

But it doesn't matter which search order we choose if only one declaration is in scope in the first place.

Upvotes: 4

Related Questions