简体   繁体   English

嵌套的Lambda捕获问题

[英]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: 我使用Visual Studio 2010编译器编译了它,它在嵌套的lambda上有编译器错误问题,捕获第一个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. 似乎编译器不喜欢我尝试在未来实例中捕获group.run()方法已捕获的变量。

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 ? 我总是需要复制捕获的变量,以便在嵌套的lambda上捕获相同的变量?

This is a known limitation of the Visual Studio 2010 C++ compiler. 这是Visual Studio 2010 C ++编译器的已知限制。 Here is the connect issue tracking it 这是跟踪它的连接问题

It's currently marked as fixed in the next version 它目前在下一版本中标记为已修复

It's not conformant to the final draft, but it is conformant to the wording at the time at which they were implemented- ie, it's not really a VS defect but neither is it exactly Standard. 这不符合性的最终草案,但它是否符合特定的措辞在它们被实施的情况,即,它不是一个真正的VS缺陷的时间,但也不是确切的标准。 The next version, colloquially known as vNext, will have an implementation updated to use the latest wording. 下一个版本,通俗地称为vNext,将更新实现以使用最新的措辞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM