闭包和延续之间的区别

时间:2012-07-28 13:36:26

标签: closures continuations

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

1 个答案:

答案 0 :(得分:7)

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

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();
相关问题