以下代码使用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中使用成员变量?
我相信VS2010这次是正确的,我会检查我是否有标准方便,但目前我没有。
现在,它就像错误消息所说的那样:你无法捕获lambda的封闭范围之外的东西。†grid
不在封闭范围内,但this
是(每次访问grid
实际上都发生在成员函数中的this->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所有的血腥细节。
捕获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
我相信,你需要捕捉this
。
限制lambda范围而不是让它访问整个this
的另一种方法是传入对成员变量的本地引用,例如:
auto& localGrid = grid;
int i;
for_each(groups.cbegin(),groups.cend(),[localGrid,&i](pair<int,set<int>> group){
i++;
cout<<i<<endl;
});
要在VS2010中使用Lambda表达式中的成员变量,依此类推,您需要在捕获列表中捕获此指针。
请参考下面的示例代码:
class A
{
public:
std::map<string, TicketInfo> m_mapImanFileUIDTicket;
void Myfunction();
};
void A::Myfunction()
{
std::string Filename;
NameRef namedRefOutput;
auto lambdaGetReadTicket = [FileName, namedRefOutput, this]()
{
TicketInfo Info;
Info.fileName = FileName;
Info.ticket = namedRefOutput.ticket;
m_mapImanFileUIDTicket[FileName] = Info; //This member variable defined in class
}
}