在C ++中嵌套Lambda捕获 [英] Nested Lambda Capture in C++

查看:1103
本文介绍了在C ++中嵌套Lambda捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西:

// 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
}

问题是Visual Studio抱怨:

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 :我正在为此项目使用VS2010

UPDATE: I am using VS2010 for this project

推荐答案

如果您使用Visual Studio 2010,触发一个错误,它不允许捕获嵌套lambda中的变量。

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.

此错误在VS2012中已修复。

This bug is fixed in VS2012.

这篇关于在C ++中嵌套Lambda捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆