为什么在其他函数内部声明的函数不参与依赖参数的查找? [英] Why doesn't function declared inside other function participate in argument dependent lookup?

查看:67
本文介绍了为什么在其他函数内部声明的函数不参与依赖参数的查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个简单的示例:

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
— if X is a local class or is a nested class of a local class, before the definition of class X in a block enclosing the definition of class X

您的通用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆