[英]Using member variable in lambda capture list inside a member function
以下代碼使用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
例如: https : //godbolt.org/g/dEKVGD
我相信,你需要抓住this
。
限制拉姆達的范圍,而不是給它訪問整個的另一種方法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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.