C 使用 lambda 表达式吗?
如果有的话,你如何使用它? (语法)
另外,为什么 C 支持或不支持 lambda 表达式?
And, if it does, how do you use one? (syntax)
Also, why does or why doesn't C support lambda expressions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,C 不支持 lambda 表达式。
如果您愿意使用 C++,Boost 有一个模拟 lambda 的库。此外,C++0x 还将内置对 lambda 表达式的支持。
当时 C 语言对 lambda 表达式支持的需求并不大,因此该语言不支持它。
No, C has no support for lambda expressions.
If you're willing to use C++, Boost has a library that emulates lambdas. Also, C++0x will have built-in support for lambda expressions.
There wasn't a huge demand for lambda expression support in C at the time, so the language didn't support it.
C 不支持 lambda 表达式,也不支持任何其他方式(在语言标准内)动态创建函数——根据标准,所有函数都是在编译时创建的。我想原因是要保持该语言小巧、简单、精简且快速,几乎不需要任何“运行时库”支持——这对于广泛用于编程操作系统、设备驱动程序、嵌入式应用程序和应用程序的语言至关重要。等等。
C does not support lambda expressions, nor any other ways (within the language's standard) to dynamically create functions -- all functions, per the standard, are created at compile time. I guess the reason is to keep the language small, simple, lean, and very fast, with hardly any "runtime library" support necessary -- crucial for a language that's so widely used in programming operating systems, device drivers, embedded applications, and so forth.
不,C 没有 lambda 表达式(或任何其他创建闭包的方法)。
这很可能是因为 C 是一种低级语言,它避免了可能导致性能不佳和/或使语言或运行时系统更加复杂的功能。
No, C doesn't have lambda expressions (or any other way to create closures).
This is likely so because C is a low-level language that avoids features that might have bad performance and/or make the language or run-time system more complex.
我今天看到了这个: https://github.com/wd5gnr/clambda/blob /master/clambda2.c
请参阅上面链接的代码:
并且在 gcc 中工作正常。
I saw this today: https://github.com/wd5gnr/clambda/blob/master/clambda2.c
See here the code of the link above:
And works fine in gcc.