TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Objective-C Blocks vs. C++0x Lambdas: Fight

119 点作者 leakybucket将近 14 年前

6 条评论

comex将近 14 年前
&#62; One place where useful optimizations could be made are inline functions which take block parameters, since the optimizer is able to improve the inlined code based on the calling code. However, as far as I know, no current blocks-capable compilers perform any of these optimizations, although I haven't investigated it thoroughly.<p>&#62; [...]<p>&#62; for_each is a template function, which means that it gets specialized for this particular type. This makes it an excellent candidate for inlining, and it's likely that an optimizing compiler will end up generating code for the above which would be just as good as the equivalent for loop.<p>This is somewhat unfair, as it seems to reflect the common misconception that (e.g.) C++ sort is faster than C qsort because it uses templates, when in fact qsort would be just as fast if its implementation were written in the .h file, as C++ sort's is. Compilers are perfectly capable of inlining calls to function pointers.<p>Calls to blocks should be able to be inlined too, but I guess they're still a new feature; I did a quick test, and it seems that gcc cannot inline them, but llvm-gcc and clang can.<p>In this case, the article suggests:<p><pre><code> [array do:^(id obj) { NSLog(@"Obj is %@", obj); }]; </code></pre> Objective-C message calls are never inlined since they are dynamic, but if you write something like:<p><pre><code> static void for_each(NSArray *array, void (^callback)(id)) { for(id obj in array) { callback(obj); } } [...] for_each(array, ^(id obj){ NSLog(@"%@", obj); }); </code></pre> llvm-gcc and clang are able to generate code equivalent to doing the for loop directly.
评论 #2619295 未加载
mustpax将近 14 年前
This really captures the core difference between the two languages. C++ is overly versatile to the point of being cumbersome whereas Obj-C imposes sensible limitations for the sake of handling the common use cases much better.
评论 #2618555 未加载
calloc将近 14 年前
I don't necessarily consider this a fight at all, I think each has their strengths and weaknesses. I much prefer the C++ method, however when programming in Objective-C using blocks just feels more natural.
评论 #2617990 未加载
评论 #2617971 未加载
评论 #2618149 未加载
saurik将近 14 年前
Objective-C's blocks would be a million times more interesting to me if they allowed for runtime type inspection: given that they are already fully fledged Objective-C objects, the fact that they don't have some kind of "methodSignature" selector that returns the type code is simply confusing. If they had this feature, then they would be fully usable from dynamic languages (such as my Cycript, but also things like PyObjC, Nu, etc.) without having to manually specify the types everywhere (which is error prone and annoying, especially if all of the rest of your code is working just fine with no types specified at all). Without this feature, they seem to just be a really limited version of C++0x lambdas tied to Objective-C's reference counting semantics.
Johngibb将近 14 年前
Boy, do I prefer how lambdas in C# work.
stonemetal将近 14 年前
The article is more interesting than the juvenile title makes it sound.<p>tldr: Lambdas provide more flexibility but are more complicated to use. Blocks integrate with objective-C better and are simpler to use.
评论 #2618082 未加载
评论 #2618113 未加载