Nested Lambda Capture in C++
c++, c++11, lambda, visual-c++, visual-studio-2010
Solution
If you're using Visual Studio 2010, your code could be triggering a bug which doesn't allow you to capture a variable in a nested lambda.
Try using a default capture mode (e.g. [&] instead) as a workaround.
This bug is fixed in VS2012.
Problem
I have something like: ``` // think of Synonym as a set/vector of values // the purpose of this function is to filter out elements from the 2 synonyms/sets, // that are not related (similar to SQL inner join) - modifier modifies vars void Clauses::modifies(Synonym& modifiers, Synonym& modifiedVars, UnaryPredicate isModifies) { // filter out any modifiers that does not modify (is related to) any of the variables in modifiedVar (left join) modifiers.removeIf([modifiedVars, &isModifies](int line) -> bool { return modifiedVars.none([line, &isModifies](int v) -> bool { return isModifies(line, v); }); }); // filter out any candidate modifiedVars that is not modified by any modifiers (right join) modifiedVars.removeIf([modifiers, &isModifies](int varIndex) -> bool { return modifiers.none([varIndex, &isModifies](int line) -> bool { return isModifies(line, varIndex); }); }); // result is an something like an SQL inner join } ``` Problem is Visual Studio complains that: ``` Error 1 error C3480: 'PQL::Clauses::`anonymous-namespace'::<lambda1>::isModifies': a lambda capture variable must be from an enclosing function scope h:\dropbox\sch\cs3202\spa_cpp\spa\pql.cpp 78 Error 2 error C2665: 'PQL::Clauses::`anonymous-namespace'::<lambda3>::<lambda3>' : none of the 2 overloads could convert all the argument types h:\dropbox\sch\cs3202\spa_cpp\spa\pql.cpp 78 ... ``` Originally, the code does not pass the predicates/conditions as references but reading somewhere I thought I needed it, but it didn't seem to change anything ``` modifiers.removeIf([modifiedVars, isModifies] ... ``` UPDATE: I am using VS2010 for this project