boost::function 和 boost::bind 很酷,但是 boost::lambda 真正酷的地方是什么?

发布于 2024-08-19 20:47:07 字数 914 浏览 5 评论 0原文

Effective C++ Meyers 对于广义函子和绑定有这样的说法:

我找到了 tr1::function 可以让你做什么 太神奇了,让我兴奋不已 超过。如果你没有刺痛感,可能会 因为你正盯着 ……的定义并想知道是什么 继续......

我同意他关于绑定和功能的观点。关于 lambda,我了解 lambda 的作用以及它是如何做到的,但是有人可以发布一本书风格的令人兴奋的片段或口头概述为什么 lambda 应该(用迈耶斯的术语)让我大吃一惊吗?我问这个问题是因为 C++ 中使用占位符语法的每个区域对我来说似乎都是一种 hack(是的,我对函数方法了解得足够多,所以请不要基础知识),我同意它的使用方式在绑定和 MPL 中;然而,就 lambda 而言,我只是希望它是合理的,这样我就可以决定是否应该将其输入我的曲目中。

-- 编辑 --

这个答案提到了使用内联创建函子只是占位符语法,他提到了高级用法,这可能就是我所追求的......在高级用法中它仍然只是函子的内联创建吗?

On Page 175 Paragraph 1 of Effective C++ Meyers has this to say about generalized functors and binding:

I find what tr1::function lets you do
so amazing, it makes me tingle all
over. If you're not tingling , it may
be because you're staring at the
definition of ... and wondering what's
going on with the ....

And I agree with him on bind and function. About lambda, Well I understand what lambda does and how it does it, but could someone post a book style mind-blowing snippet or a verbal outline of why lambda is supposed to (in Meyers' terminology) blow my socks off ? I ask because each area of C++ where the placeholder syntax is used seems like a hack to me (yes, I know enough about the functional method, so please no basics), I agree with the way it's used in bind and MPL; However, in the case of lambda I just want it justified so I can decide weather I should enter it into my repertoire.

-- edit --

This SO answer mentions the inlined creation of a functor using just placedholder syntax, he mentions advanced usage, and this is probably what I am after... in advanced usage is it still just inlined creation of functors ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝蛊 2024-08-26 20:47:07

根据上面留下的评论以及问题中的链接,以下是我接受的答案(社区维基):

  1. Boost.Lambda 满足了内联函子创建的目的(这是我喜欢的术语)。此功能可以通过 Function + Bind 来实现,但它比需要的更详细,并且对于简单函子来说,这是不必要的 - 例如,上面注释中显示的排序。

  2. 函数绑定对和 Lambda 之间显然存在语义重叠——这是一个历史产物,并且因为 Lambda 有其存在的理由,所以它存在于 Boost 中。

Based on the comments left above, and the link in the question, the following is the answer I accept (community wiki) :

  1. Boost.Lambda fills the purpose of inline functor creation (that's the term I like). This functionality can be filled by Function + Bind, but it is more verbose than it needs to be, and for simple functors this is unnecessary — e.g., the sort shown in the comments above.

  2. There is obviously semantic overlap between the Function-Bind pair and Lambda — this is a historical artifact, and because Lambda has its raison d'être, it exists in Boost.

风吹雪碎 2024-08-26 20:47:07

它的“酷”之处在于,与boost foreach和boost参数一样,将语法注入/扩展到C++语言中,即它模拟匿名函数直接作为参数。

What is "cool" about it is that, as with boost foreach and boost parameter, injects/extends syntax into C++ which is not in the language, ie it emulates anonymous functions directly as parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文