【问题标题】:Using member variable in lambda capture list inside a member function在成员函数内的 lambda 捕获列表中使用成员变量
【发布时间】:2011-12-15 07:16:31
【问题描述】:

以下代码使用 gcc 4.5.1 编译,但不使用 VS2010 SP1:

#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <set>
#include <algorithm>

using namespace std;
class puzzle
{
        vector<vector<int>> grid;
        map<int,set<int>> groups;
public:
        int member_function();
};

int puzzle::member_function()
{
        int i;
        for_each(groups.cbegin(),groups.cend(),[grid,&i](pair<int,set<int>> group){
                i++;
                cout<<i<<endl;
        });
}
int main()
{
        return 0;
}

这是错误:

error C3480: 'puzzle::grid': a lambda capture variable must be from an enclosing function scope
warning C4573: the usage of 'puzzle::grid' requires the compiler to capture 'this' but the current default capture mode does not allow it

所以,

1>哪个编译器是对的?

2> 如何在 VS2010 的 lambda 中使用成员变量?

【问题讨论】:

  • 注意:应该是pair&lt;const int, set&lt;int&gt; &gt;,这是地图的实际配对类型。它也应该是对 const 的引用。
  • 相关;很有帮助:thispointer.com/…
  • 使用 [&] 通过引用捕获。

标签: c++ visual-studio-2010 lambda c++11


【解决方案1】:

我相信这次 VS2010 是对的,我会检查我是否有手边的标准,但目前我没有。

现在,就像错误消息所说的那样:您无法捕获 lambda 封闭范围之外的东西。grid 不在封闭范围内,但 this是(对grid 的每次访问实际上都在成员函数中以this-&gt;grid 的形式发生)。对于您的用例,捕获 this 有效,因为您将立即使用它并且您不想复制 grid

auto lambda = [this](){ std::cout << grid[0][0] << "\n"; }

但是,如果您想要存储网格并将其复制以供以后访问,而您的 puzzle 对象可能已经被销毁,您需要制作一个中间的本地副本:

vector<vector<int> > tmp(grid);
auto lambda = [tmp](){}; // capture the local copy per copy

† 我正在简化 - 谷歌“达到范围”或查看 §5.1.2 了解所有血腥细节。

【讨论】:

  • 对我来说似乎很有限。我不明白为什么编译器需要阻止这种事情。它适用于 bind,尽管 ostream 左移运算符的语法很糟糕。
  • 可以tmp 成为const &amp;grid 以减少复制吗?我们仍然需要至少一个副本,即复制到 lambda ([tmp]) 中,但不需要第二个副本。
  • 该解决方案可能会制作一个不必要的额外副本 grid,尽管它可能会被优化掉。更短更好的是:auto&amp; tmp = grid; 等。
  • 如果你有 C++14 可用,你可以使用[grid = grid](){ std::cout &lt;&lt; grid[0][0] &lt;&lt; "\n"; } 来避免额外的复制
  • 它似乎已在 gcc 4.9 中修复(以及 gcc 5.4)error: capture of non-variable ‘puzzle::grid’
【解决方案2】:

备选方案总结:

捕获this

auto lambda = [this](){};

使用对成员的本地引用:

auto& tmp = grid;
auto lambda = [ tmp](){}; // capture grid by (a single) copy
auto lambda = [&tmp](){}; // capture grid by ref

C++14:

auto lambda = [ grid = grid](){}; // capture grid by copy
auto lambda = [&grid = grid](){}; // capture grid by ref

示例:https://godbolt.org/g/dEKVGD

【讨论】:

  • 有趣的是,只有显式使用带有初始化语法的捕获才能实现这一点(即在 C++14 中,只需执行 [&amp;grid] 仍然不起作用)。很高兴知道这一点!
  • 很好的总结。我觉得 C++14 语法很方便
【解决方案3】:

我相信,你需要捕获this

【讨论】:

  • 这是正确的,它会捕获this指针,你仍然可以直接引用grid。问题是,如果你想复制网格怎么办?这不允许你这样做。
  • 你可以,但只能以迂回的方式:你必须制作一个本地副本,并在 lambda 中捕获 that。这只是 lambdas 的规则,你不能在封闭范围之外捕获僵硬的东西。
  • 当然可以复制。我的意思是你当然不能复制捕获它。
  • 我所描述的内容是通过中间本地副本进行副本捕获 - 请参阅我的答案。除此之外,我不知道任何复制捕获成员变量的方法。
  • 当然,它确实复制捕获,但不是成员。它涉及两个副本,除非编译器比平时更聪明,我猜。
【解决方案4】:

另一种限制 lambda 范围而不是让其访问整个 this 的方法是传入对成员变量的局部引用,例如

auto& localGrid = grid;
int i;
for_each(groups.cbegin(),groups.cend(),[localGrid,&i](pair<int,set<int>> group){
            i++;
            cout<<i<<endl;
   });

【讨论】:

  • 我喜欢你的想法:使用假引用变量并将其传递给捕获列表 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
相关资源
最近更新 更多