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
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.
It appears to be a Visual C++ bug, as GCC and Clang accept this capture. Here's a workaround:
modifiedVars.removeIf([modifiers, &isModifies](int varIndex) -> bool {
auto& isModifiesRedirect = isModifies;
return modifiers.none([varIndex, &isModifiesRedirect ](int line) -> bool {
return isModifiesRedirect (line, varIndex);
});
Note: I could only test this on VS2010. It might be fixed in VS2012. You may want to consider searching Microsoft Connect and submitting a new bug if it's not a known issue already.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With