C++ 在成员函数内的 lambda 捕获列表中使用成员变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7895879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using member variable in lambda capture list inside a member function
提问by vivek
The following code compiles with gcc 4.5.1 but not with VS2010 SP1:
以下代码使用 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;
}
This is the error:
这是错误:
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
So,
所以,
1> which compiler is right?
1> 哪个编译器是对的?
2> How can I use member variables inside a lambda in VS2010?
2> 如何在 VS2010 的 lambda 中使用成员变量?
回答by Xeo
I believe VS2010 to be right this time, and I'd check if I had the standard handy, but currently I don't.
我相信这次 VS2010 是正确的,我会检查我是否有方便的标准,但目前我没有。
Now, it's exactly like the error message says: You can't capture stuff outside of the enclosing scope of the lambda.?grid
is not in the enclosing scope, but this
is (every access to grid
actually happens as this->grid
in member functions). For your usecase, capturing this
works, since you'll use it right away and you don't want to copy the grid
现在,它就像错误消息所说的那样:您无法捕获 lambda 封闭范围之外的内容。? grid
不在封闭范围内,而是this
(每次访问grid
实际上都发生this->grid
在成员函数中)。对于您的用例,捕获this
工作,因为您将立即使用它并且您不想复制grid
auto lambda = [this](){ std::cout << grid[0][0] << "\n"; }
If however, you want to store the grid and copy it for later access, where your puzzle
object might already be destroyed, you'll need to make an intermediate, local copy:
但是,如果您想存储网格并复制它以供以后访问,您的puzzle
对象可能已经被销毁,您需要制作一个中间的本地副本:
vector<vector<int> > tmp(grid);
auto lambda = [tmp](){}; // capture the local copy per copy
? I'm simplifying - Google for "reaching scope" or see §5.1.2 for all the gory details.
? 我正在简化 - 谷歌“达到范围”或查看第 5.1.2 节了解所有血腥细节。
回答by Trass3r
Summary of the alternatives:
备选方案摘要:
capture this
:
捕获this
:
auto lambda = [this](){};
use a local reference to the member:
使用对成员的本地引用:
auto& tmp = grid;
auto lambda = [ tmp](){}; // capture grid by (a single) copy
auto lambda = [&tmp](){}; // capture grid by ref
C++14:
C++14:
auto lambda = [ grid = grid](){}; // capture grid by copy
auto lambda = [&grid = grid](){}; // capture grid by ref
example: https://godbolt.org/g/dEKVGD
回答by Michael Krelin - hacker
I believe, you need to capture this
.
我相信,您需要捕获this
.
回答by dlanod
An alternate method that limits the scope of the lambda rather than giving it access to the whole this
is to pass in a local reference to the member variable, e.g.
限制 lambda 的范围而不是让它访问整个的另一种方法this
是传递对成员变量的局部引用,例如
auto& localGrid = grid;
int i;
for_each(groups.cbegin(),groups.cend(),[localGrid,&i](pair<int,set<int>> group){
i++;
cout<<i<<endl;
});