【发布时间】:2011-12-15 07:16:31
【问题描述】:
以下代码使用 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 中使用成员变量?
【问题讨论】:
-
注意:应该是
pair<const int, set<int> >,这是地图的实际配对类型。它也应该是对 const 的引用。 -
相关;很有帮助:thispointer.com/…
-
使用 [&] 通过引用捕获。
标签: c++ visual-studio-2010 lambda c++11