difference between closures and continuations

北城余情 提交于 2019-12-04 21:39:19

问题


Can someone please explain the difference between closures and continuations? The corresponding articles in wikipedia do not really compare the differences between the two.


回答1:


A closure is a function that captures data from the environment on which it was declared.

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

A continuation is a more abstract concept, and refers to what code should be executed afterwards. It can be implemented using a closure.

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


来源:https://stackoverflow.com/questions/11701486/difference-between-closures-and-continuations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!