在成员函数内的lambda捕获列表中使用成员变量

viv*_*vek 127 c++ lambda visual-studio-2010 c++11

以下代码使用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;
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

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
Run Code Online (Sandbox Code Playgroud)

所以,

1>哪个编译器是对的?

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

Xeo*_*Xeo 140

我相信VS2010这次是正确的,我会检查我是否有标准方便,但目前我没有.

现在,它与错误消息完全相同:您无法捕获lambda的封闭范围之外的内容. grid不在封闭范围内,但是this(每次访问grid实际上都发生this->grid在成员函数中).对于你的用例,捕获this工作,因为你将立即使用它,你不想复制grid

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

但是,如果要存储网格并将其复制以供以后访问(puzzle对象可能已被销毁),则需要制作中间本地副本:

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

†我正在简化 - 谷歌"达到范围"或参见§5.1.2所有的血腥细节.

  • 该解决方案可能会制作一个不必要的"网格"副本,尽管它可能会被优化掉.更短更好的是:`auto&tmp = grid;`等 (4认同)
  • 如果您有C ++ 14,则可以执行[[grid = grid](){std :: cout &lt;&lt; grid [0] [0] &lt;&lt;“ \ n”; }`以避免多余的副本 (4认同)
  • 可以`tmp`是一个`const&`到`grid`来减少复制?我们仍然需要至少一个副本,副本到lambda(`[tmp]`),但不需要第二个副本. (3认同)

Tra*_*s3r 82

备选方案摘要:

捕获this:

auto lambda = [this](){};
Run Code Online (Sandbox Code Playgroud)

使用对该成员的本地引用:

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

C++ 14:

auto lambda = [ grid = grid](){}; // capture grid by copy
auto lambda = [&grid = grid](){}; // capture grid by ref
Run Code Online (Sandbox Code Playgroud)

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

  • 有趣的是,只有使用初始化器语法显式使用捕获才适用于此(即在C++ 14中只做`[&grid]`仍然不起作用).很高兴知道这个! (5认同)
  • 很好的总结。我发现 C++14 语法非常方便 (3认同)

Mic*_*ker 21

我相信,你需要捕捉this.

  • 您可以,但只能以迂回的方式:您必须制作本地副本,并在lambda中捕获*.这只是lambdas的规则,你无法捕捉到封闭范围之外的僵硬. (9认同)

dla*_*nod 13

限制lambda范围而不是让它访问整体的另一种方法this是传入对成员变量的本地引用,例如

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