gpt4 book ai didi

closures - 闭包和延续之间的区别

转载 作者:行者123 更新时间:2023-12-02 21:46:19 24 4
gpt4 key购买 nike

有人可以解释一下闭包和延续之间的区别吗?维基百科中的相应文章并没有真正比较两者之间的差异。

最佳答案

闭包是一个从声明它的环境中捕获数据的函数。

int myVar = 0;
auto foo = [&] () { myVar++; }; <- This lambda forms a closure by capturing myVar
foo();
assert(myVar == 1);

延续是一个更抽象的概念,指的是之后应该执行哪些代码。它可以使用闭包来实现。

myTask = Task([] () { something(); });
myTask.then([=] () { myFoo.bar(); }); // This closure is the continuation of the task
myTask.run();

关于closures - 闭包和延续之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11701486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com