为什么在其他函数内部声明的函数不参与依赖参数的查找? [英] Why doesn't function declared inside other function participate in argument dependent lookup?
问题描述
考虑一个简单的示例:
template <class T>
struct tag { };
int main() {
auto foo = [](auto x) -> decltype(bar(x)) { return {}; };
tag<int> bar(tag<int>);
bar(tag<int>{}); // <- compiles OK
foo(tag<int>{}); // 'bar' was not declared in this scope ?!
}
tag<int> bar(tag<int>) { return {}; }
[gcc] 和
Both [gcc] and [clang] refuses to compile the code. Is this code ill-formed in some way?
推荐答案
来自不合格的查找规则([basic.lookup.unqual]):
From unqualified lookup rules ([basic.lookup.unqual]):
对于类
X
的成员,成员函数体[...]中使用的名称应在其中的一个中声明. 以下方式
—如果X
是本地类或本地类的嵌套类,则在块中定义类X
之前 包含类X
For the members of a class
X
, a name used in a member function body, [...], shall be declared in one of the following ways
— ifX
is a local class or is a nested class of a local class, before the definition of classX
in a block enclosing the definition of classX
您的通用lambda是main
中的局部类,因此要使用bar
,名称bar
必须事先出现在声明中.
Your generic lambda is a local class within main
, so to use bar
, the name bar
must appear in a declaration beforehand.
这篇关于为什么在其他函数内部声明的函数不参与依赖参数的查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!