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所有的血腥细节.
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
Mic*_*ker 21
我相信,你需要捕捉this
.
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)
归档时间: |
|
查看次数: |
128215 次 |
最近记录: |