gpt4 book ai didi

C++0x lambda vs block

转载 作者:IT老高 更新时间:2023-10-28 23:18:30 26 4
gpt4 key购买 nike

我今天在探索 C++0x,遇到了新的 lambda 特性。我的问题是这些与 blocks 有何不同(在使用方面)为什么一个人可能更喜欢另一个?

谢谢。

最佳答案

there is a a short syntax with C++0x lambdas to take every variable in scope by reference. ([&]) The type of a lambda is also unspecified, allowing potentially more optimal code.

Now, when you look at Apple blocks, it will require __block specifiers added to variables you want to modify (the very fact that this is required suggests the whole system is defective). Variables are taken by reference but then by value when the block exits the scope (and the copied context necessarily lives on the heap, it seems). A weird semantic that will only lead to broken designs, but probably makes people that love GC happy. Without saying this probably has quite the efficiency cost, of course, since this requires special indirections.

It is claimed the C++0x lambdas syntax would break compatibility with C programs, but I don't think that is true. There are probably other problems to integrate it with C, though, mainly the fact that C can't really deal with unspecified types and build type erasure.

Apple blocks is really just an ObjC feature they try to generalize to other languages. For C++, the system designed for that language is just so much better.

编辑:

为了恰本地给予信任,我从 http://www.rhinocerus.net/forum/language-c-moderated/558214-blocks-vs-c-lambdas.html 中获取了这些信息很久以前。该链接现在已失效;但是,最初的讨论似乎已存档 here ,感谢@stefan 找到它。

关于C++0x lambda vs block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7071437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com