[英]Nested Lambda Capture in C++
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: 问题是Visual Studio抱怨:
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 更新 :我正在为此项目使用VS2010
It appears to be a Visual C++ bug, as GCC and Clang accept this capture. 这似乎是Visual C ++错误,因为GCC和Clang接受了此捕获。 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. 注意:我只能在VS2010上对此进行测试。 It might be fixed in VS2012. 在VS2012中可能已修复。 You may want to consider searching Microsoft Connect and submitting a new bug if it's not a known issue already. 如果尚未发现已知问题,则可能要考虑搜索Microsoft Connect并提交新的错误。
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. 如果您使用的是Visual Studio 2010,则您的代码可能会触发一个错误,该错误不允许您捕获嵌套lambda中的变量。
Try using a default capture mode (eg [&] instead) as a workaround. 尝试使用默认的捕获模式(例如[&]代替)作为解决方法。
This bug is fixed in VS2012. 此错误已在VS2012中修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.