嵌套Lambda捕获问题 [英] Nested Lambda capture issue

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

问题描述

我使用Visual Studio 2010编译器编译,并且在嵌套lambda捕获已经由第一个lambda捕获的变量编译器错误问题:

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;
    });
});

我得到:

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

似乎编译器不喜欢我试图在未来的实例中捕获已经由group.run()方法捕获的变量。

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;
    });
});

这种行为是否符合?我总是需要复制捕获的变量,以捕获嵌套lambda上的相同变量。

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

推荐答案

限制Visual Studio 2010 C ++编译器。这是跟踪问题的连接问题

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

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

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