Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 973
Jiew Meng Avatar asked Feb 22 '13 09:02

Jiew Meng


2 Answers

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.

like image 65
Alex Korban Avatar answered Oct 27 '22 05:10

Alex Korban


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.

like image 5
Andrei Tita Avatar answered Oct 27 '22 07:10

Andrei Tita