C++ 如何才能使用连续传递风格?
假设在 C++ 中,您对递归函数进行了太多递归调用并收到堆栈溢出错误。
您将如何以连续传递的方式重写它以避免堆栈溢出?
我在 C++ 中想象这个有点困难。
Suppose in C++ you're doing too many recursive calls on a recursive function and getting a stack overflow error.
How would you rewrite this in a continuation-passing style to avoid the stack overflow?
I'm having a slight difficulty picturing this in C++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这是一个相当开放式的问题,但是 Eric Lippert 写了一个(实际上是两个)相当关于这个主题的长系列。不完全是正确的语言,但它应该仍然很有帮助并给出总体思路。
虽然在 C++ 中实现 CPS 似乎只是为了修复单个递归函数而需要做很多工作,但当您可以使用某种算法使函数与队列迭代时(您仍然使用基本相同的数据量,但堆远远不够)限制较少)。
Well that's a rather open ended question, but Eric Lippert wrote a (well two actually) rather long series about exactly this topic. Not exactly the right language, but it should be rather helpful still and give the general idea.
Though implementing CPS in C++ seems like a lot of work just to fix a single recursive function, when you can just use some algorithm to make the function iterative with a queue (you still use basically the same amount of data, but the heap is far less restricted).