gpt4 book ai didi

c++ - 在 lambda 中调用函数时 ADL 失败

转载 作者:搜寻专家 更新时间:2023-10-31 02:08:24 24 4
gpt4 key购买 nike

我正在尝试从作为在特定成员函数中运行 std::for_each 的一部分的 lambda 调用函数。这里的例子: https://godbolt.org/g/KCBLjL

namespace A {
struct Foo {
int a;
int b;
};

void dump(const A::Foo& v) {
std::cout << v.a << v.b << std::endl;
}
} // ns A

class B {
public:
void dump() const {
(void)std::for_each(std::begin(foo), std::end(foo),
[](const A::Foo &f){
dump(f); // <- fails here, I expected ADL to kick in
});
}
private:
std::vector<A::Foo> foo = { A::Foo{}, A::Foo{} };
};

获得:

<source>: In lambda function:
21 : <source>:21:19: error: no matching function for call to
'B::dump(const A::Foo&)'
dump(f);
^
18 : <source>:18:10: note: candidate: void B::dump() const
void dump() const {

为什么ADL在这里失败了?我假设编译器会找到 B::dump 但也因为 A::Foo 在同一个命名空间中有 dump(const Foo&) ,编译器会将其添加为一个选项。但它不起作用。使用 -std=c++17 编译

最佳答案

[basic.lookup.argdep]/3 ,强调我的:

Let X be the lookup set produced by unqualified lookup and let Y be the lookup set produced by argument dependent lookup (defined as follows). If X contains

  • a declaration of a class member, or
  • a block-scope function declaration that is not a using-declaration, or
  • a declaration that is neither a function nor a function template

then Y is empty.

关于c++ - 在 lambda 中调用函数时 ADL 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47620649/

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