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

Why does this C++ closure produce different outputs?

我正在尝试学习 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

任何人都可以解释为什么在函数调用之前打印一些东西会改变输出吗?

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

这说明原来的值肯定被otherFn()中的abc变量覆盖了:

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

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


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