【问题标题】:Can't capture a static variable in a lambda [duplicate]无法在 lambda 中捕获静态变量 [重复]
【发布时间】:2018-02-06 09:32:45
【问题描述】:

这看起来很奇怪,我可以捕获静态变量,但前提是该变量未在捕获列表中指定,即它隐式捕获它。

int main()
{
    int captureMe = 0;
    static int captureMe_static = 0;

    auto lambda1 = [&]() { captureMe++; };  // Works, deduced capture
    auto lambda2 = [&captureMe]() { captureMe++; }; // Works, explicit capture
    auto lambda3 = [&] () { captureMe_static++; };  // Works, capturing static int implicitly
    auto lambda4 = [&captureMe_static] { captureMe_static++; }; // Capturing static in explicitly:  
                                                                // Error: A variable with static storage duration 
                                                                // cannot be captured in a lambda

    // Also says "identifier in capture must be a variable with automatic storage duration declared
    // in the reaching scope of the lambda

    lambda1(); lambda2(); lambda3();    // All work fine

    return 0;
}

我不明白,第三次和第四次捕获应该是等效的,不是吗?在第三个中,我没有捕获具有“自动存储持续时间”的变量

编辑:我认为这个问题的答案是它永远不会捕获静态变量,所以:

auto lambda = [&] { captureMe_static++; };   // Ampersand says to capture any variables, but it doesn't need to capture anything so the ampersand is not doing anything
auto lambda = [] { captureMe_static++; };  // As shown by this, the static doesn't need to be captured, and can't be captured according to the rules.

【问题讨论】:

    标签: c++ lambda static capture


    【解决方案1】:

    这不是你想要的吗?

    static int dont_capture_me = 0;
    
    auto foo = [] { dont_capture_me++; };
    

    当然,您可以显式存储引用,但它们本质上是等价的。

    auto bar = [&reference = dont_capture_me] { reference++; };
    

    【讨论】:

      【解决方案2】:

      不需要捕获具有静态存储持续时间的变量,因此无法捕获。您可以简单地在 lambda 中使用它。

      对于自动变量存在一个问题:虽然在其他语言中,闭包只会在封闭范围内存储对变量的引用,但在 C++ 中,lambda 没有能力延长自动变量的生命周期,这可能因此超出范围,在 lambda 中留下一个悬空引用。出于这个原因,C++ 允许您选择是通过复制还是通过引用来捕获自动变量。但是如果变量是静态的,那么这个问题就不会出现; lambda 的行为就像它通过引用捕获它一样。

      如果您确实想通过 value 捕获静态变量,请使用 C++14 init-capture 语法。

      【讨论】:

      • "不需要捕获,因此无法捕获。"我在这里看不到逻辑。如果它无缝地符合 C++ 语言规则,为什么要禁止显式静态变量捕获
      • @sfk92fksdf 我不能告诉你。委员会中的某个人可能知道答案。
      • @sfk92fksdf 有你要找的答案:stackoverflow.com/a/13828144/2865757
      • @Zefick,谢谢,这也是 OP 问题的实际答案 %)
      • 当使用空捕获列表创建 lambda 时,我能够访问静态变量,因此您不必捕获它是真的。所以我想,在我执行 [&] 并且能够访问静态变量的情况下,它并没有捕获它,它只是可以访问它。
      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多