gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:52:29 28 4
gpt4 key购买 nike

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

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

关于c++ - 查找最直接的名称声明的名称查找规则在哪里定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574502/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com