查找最直接的名称声明的名称查找规则在哪里定义?

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'?
    }
}

我的问题不是选择了哪个 i,因为很明显它是本地的,而是在指定的标准中的哪个位置。

我能找到的最接近的规则是 [basic.lookup.unqual]p6,它说:

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.

但那里只是说名称必须在使用前的某个时间声明;这不是我要找的。同一段落中的示例让一切都更加清晰,因为它说明了按什么顺序搜索哪些范围,但它只是一个示例,因此不是主格。

[basic.lookup.unqual] 中的所有其他段落不适用于非成员函数。所以我的问题是在标准中的哪个地方指定了这个?

[basic.scope.declarative] 中我们有:

  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 ]

(强调我的。)

在你的

int a = i;

例如,i 必须引用本地 i,因为全局 i 实际上不在此处的范围内。

正如[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 [...]

但是如果一开始只有一个声明在范围内,我们选择哪种搜索顺序并不重要。