当名称出现在函数的 declarator-id 之前时,查找规则是什么?
what are lookup rules when a name occured before function's declarator-id?
#include <iostream>
typedef int Name;
Name func(int){
return 0;
}
int main(){
}
考虑上面的代码,我在 [basic.lookup.unqual] 中找不到可以解释如何查找 Name
以定义 func
的项目符号。我将在这里引用一些潜在的规则:
A name used in global scope, outside of any function, class or user-declared namespace, shall be declared before its use in global scope.
A name used in a user-declared namespace outside of the definition of any function or class shall be declared before its use in that namespace or before its use in a namespace enclosing its namespace.
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
请注意强调的部分,看来我的情况不满足这些要点,因为函数定义有
的形式
function-definition:
attribute-specifier-seq(opt) decl-specifier-seq(opt) declarator virt-specifier-seq(opt) function-body
我来分析一下第一颗子弹。它说 在任何函数之外 ,但是根据 Function definitions rule
,我认为 Name
在函数(定义)之内,不满足项目符号 1。项目符号 2 与项目符号 1 的相似。项目符号 3 表示在函数的声明符 id 之后使用的名称 ,在我的例子中,Name
在函数的声明符之前使用-ID。那么这种情况下查找非限定名称Name
的规则是什么?
我的困惑:
在我的示例中,Name
、func
、(int)
和 { return 0;}
(函数体)都是该函数定义的一部分,因此:
- 什么是函数之外,比如我例子中的
func
,那个函数之外的区域在哪里?
- 什么是任何函数的定义之外,例如我示例中的
func
定义,该函数定义之外的区域在哪里?
I think that Name
is within the function(definition), bullet 1 isn't satisfied.
项目符号 1 没有说 "function(definition)"。它说 "outside of any function"。它没有指定声明或定义;仅仅 "outside of any function".
由于在 "function" 的内部或外部不是定义的概念,因此必须将其理解为简单的英语。函数原型是"outside of the function"吗?从视觉上看,暗示 inside/outside 区别的函数原型没有什么特别之处。相比之下,函数体的块作用域确实表明了 inside/outside 区别。
文字的意图当然是比较明显的;它在谈论功能体。该规则等同于 "function"、"class" 和 "namespace",它们都有一个定义名称范围的块。这是任何 inside/outside 区分的最合乎逻辑的地方,所以它似乎很明显是说全局范围由不在函数体范围内的所有内容组成,[=37= 的范围] 定义,或命名空间主体的范围。
所以这可以通过编辑更改轻松处理。
请注意,委员会将本节(以及该领域的其他内容)的措辞识别为 C++23 中的 somewhat defective, and there's a proposal for rewriting it into something more coherent。提案中的新措辞完全重写了这一部分。
#include <iostream>
typedef int Name;
Name func(int){
return 0;
}
int main(){
}
考虑上面的代码,我在 [basic.lookup.unqual] 中找不到可以解释如何查找 Name
以定义 func
的项目符号。我将在这里引用一些潜在的规则:
A name used in global scope, outside of any function, class or user-declared namespace, shall be declared before its use in global scope.
A name used in a user-declared namespace outside of the definition of any function or class shall be declared before its use in that namespace or before its use in a namespace enclosing its namespace.
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
请注意强调的部分,看来我的情况不满足这些要点,因为函数定义有
的形式function-definition:
attribute-specifier-seq(opt) decl-specifier-seq(opt) declarator virt-specifier-seq(opt) function-body
我来分析一下第一颗子弹。它说 在任何函数之外 ,但是根据 Function definitions rule
,我认为 Name
在函数(定义)之内,不满足项目符号 1。项目符号 2 与项目符号 1 的相似。项目符号 3 表示在函数的声明符 id 之后使用的名称 ,在我的例子中,Name
在函数的声明符之前使用-ID。那么这种情况下查找非限定名称Name
的规则是什么?
我的困惑:
在我的示例中,Name
、func
、(int)
和 { return 0;}
(函数体)都是该函数定义的一部分,因此:
- 什么是函数之外,比如我例子中的
func
,那个函数之外的区域在哪里? - 什么是任何函数的定义之外,例如我示例中的
func
定义,该函数定义之外的区域在哪里?
I think that
Name
is within the function(definition), bullet 1 isn't satisfied.
项目符号 1 没有说 "function(definition)"。它说 "outside of any function"。它没有指定声明或定义;仅仅 "outside of any function".
由于在 "function" 的内部或外部不是定义的概念,因此必须将其理解为简单的英语。函数原型是"outside of the function"吗?从视觉上看,暗示 inside/outside 区别的函数原型没有什么特别之处。相比之下,函数体的块作用域确实表明了 inside/outside 区别。
文字的意图当然是比较明显的;它在谈论功能体。该规则等同于 "function"、"class" 和 "namespace",它们都有一个定义名称范围的块。这是任何 inside/outside 区分的最合乎逻辑的地方,所以它似乎很明显是说全局范围由不在函数体范围内的所有内容组成,[=37= 的范围] 定义,或命名空间主体的范围。
所以这可以通过编辑更改轻松处理。
请注意,委员会将本节(以及该领域的其他内容)的措辞识别为 C++23 中的 somewhat defective, and there's a proposal for rewriting it into something more coherent。提案中的新措辞完全重写了这一部分。