为什么这个 C++ 闭包会产生不同的输出?

我正在尝试学习 C++ 中闭包的概念。我有以下代码。

std::function<void(void)> closureWrapper2()
{
    int x = 10;
    return [&x](){x += 1; std::cout << "Value in the closure: " << x << std::endl;};
}

int main()
{   
    std::function<void(void)> func2 = closureWrapper2();
    // std::cout << "---" << std::endl;
    func2();
    func2();
    func2();
}

输出

Value in the closure: 11
Value in the closure: 12
Value in the closure: 13

现在,如果我取消对 cout 语句的注释,我会得到以下输出。

输出

---
Value in the closure: 32765
Value in the closure: 32766
Value in the closure: 32767

谁能解释为什么在函数调用改变输出之前打印一些东西?

Q24529999 回答:为什么这个 C++ 闭包会产生不同的输出?

std::function<void(void)> closureWrapper2()
{
    int x = 10;
    return [&x](){x += 1; std::cout << "Value in the closure: " << x << std::endl;};
}

在该对象不再存在之后取消引用或使用对该对象的引用是未定义的行为(a)。这就是你在这里所做的。您捕获对 x引用,然后在 x 不复存在后尝试使用它。

它是 closureWrapper2() 内部的一个本地(自动存储持续时间)变量,因此在该函数退出时不再存在。

这可能看起来在没有 cout 行的情况下也能工作,但这并没有减少它的不确定性。放置 cout 行几乎肯定会修改最初存储 x 的堆栈,从而更改起始值。

您可以使用(在我的环境中)获得类似的效果:

void otherFn() { int abc = 97,def = 42,ghi = 9; std::cout << abc+def+ghi << '\n'; }

int main()
{
    std::function<void(void)> func2 = closureWrapper2();
    otherFn();
    func2();
    func2();
    func2();
}

这表明原始值肯定被 abc 中的 otherFn() 变量覆盖:

148
Value in the closure: 98
Value in the closure: 99
Value in the closure: 100

我不得不尝试不同数量的参数,因为 closureWrapper2()otherFn() 的堆栈帧很可能不同。调用 cout.operator<<() 很可能要经过 多个 的堆栈级别才能到达终点,因此更有可能覆盖原始值。


(a) 这是您问题的解决方案,当然:不要做未定义的行为 :-)

本文链接:https://www.f2er.com/2277.html

大家都在问