Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Lambda capture issue

I have compiled this using Visual Studio 2010 compiler and it has compiler error issues on nested lambda capturing the variables captured already by the first lambda:

    Functor& fn, const WorkData& workData

    group.run([&fn, workData](){
    async_future<ProcessorResult> future([&fn, workData]() -> ProcessorResult{
    ProcessorResult result = fn.Process(workData);

    return result;
    });
});

I get:

**error C3480**: '`anonymous-namespace'::<lambda3>::fn': a lambda capture variable must be from an enclosing function scope

It seems that the compiler does not like that I try to capture inside the future instance the variables captured already by the group.run() method.

If I create local copies it works:

    group.run([&fn, workData](){
    Functor& fnlocal = fn;
    WorkData workDatalocal = workData;

    async_future<ProcessorResult> future([&fnlocal, workDatalocal]() -> ProcessorResult{
    ProcessorResult result = fnlocal.Process(workDatalocal);

    return result;
    });
});

Is this behavior conformant ? I always need to make copies of captured variables in order to capture the same variables on a nested lambda ?

like image 861
Ghita Avatar asked Mar 31 '12 20:03

Ghita


2 Answers

This is a known limitation of the Visual Studio 2010 C++ compiler. Here is the connect issue tracking it

  • https://connect.microsoft.com/VisualStudio/feedback/details/725134/nested-lambda-expressions-can-not-capture-entities-of-enclosing-lambda-expressions

It's currently marked as fixed in the next version

like image 82
JaredPar Avatar answered Oct 31 '22 20:10

JaredPar


It's not conformant to the final draft, but it is conformant to the wording at the time at which they were implemented- i.e., it's not really a VS defect but neither is it exactly Standard. The next version, colloquially known as vNext, will have an implementation updated to use the latest wording.

like image 4
Puppy Avatar answered Oct 31 '22 18:10

Puppy