闭包和连续的区别 [英] difference between closures and continuations

查看:138
本文介绍了闭包和连续的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

closure是一个函数,

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

这篇关于闭包和连续的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆