C++0x lambda 和 operator()、闭包和函子之间的区别
我相信我已经了解了这些构造的一般要点,但我看不到它们在 C++ 中的用途。我已经在 SO 和其他地方阅读了之前关于该主题的文章,但我不明白为什么它们应该是一个新的语言功能。
我想回答的问题是
lambda 和接受函数/函子的模板参数之间有什么区别。
闭包只是一个具有某些设置对象状态(范围?)的函子吗?
- 这些构造的“杀手级应用程序”是什么?或者也许是典型的用例?
I'm confident I get the general gist of the constructs, but I can't see the purpose of them in c++. I have read the previous posts on the topic here on SO and elsewhere, but I fail to see why they should be a new language feature.
The things I would like answered is thusly
What is the difference between a lambda and a template argument accepting a function/functor.
Is a closure just a functor with some set object state (scope?)?
- What is the "killer app" for these constructs? or perhaps the typical use case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Lambda 实际上只是函子的语法糖。您可以自己完成这一切:定义一个新类,创建成员变量来保存捕获的值和引用,将它们连接到构造函数中,编写
operator()()
,最后创建一个实例并通过它。或者您可以使用 lambda,其代码量是原来的 1/10,并且工作原理相同。不捕获的 Lambda 可以转换为函数指针。所有 lambda 都可以转换为 std::function ,或者获取自己的独特类型,该类型在接受函子的模板化算法中效果很好。
Lambdas are really just syntactic sugar for a functor. You could do it all yourself: defining a new class, making member variables to hold the captured values and references, hooking them up in the constructor, writing
operator()()
, and finally creating an instance and passing it. Or you could use a lambda that's 1/10 as much code and works the same.Lambdas which don't capture can be converted to function pointers. All lambdas can be converted to
std::function
, or get their own unique type which works well in templated algorithms accepting a functor.好吧,您实际上是在问一堆不同的问题,可能是因为您不完全熟悉术语。我会尽力回答所有。
lambda 和“operator()”有什么区别? - 让我们将其改写为:“lambda 和带有 operator() 的对象之间有什么区别?”
基本上什么都没有。主要区别在于 lambda 表达式创建函数对象,而带有运算符() 的对象是函数对象。最终结果足够相似,可以认为是相同的,可以使用 (params) 语法调用实体。
闭包和函子有什么区别?这也是比较混乱的。请查看此链接:
http://en.wikipedia.org/wiki/Closure_(computer_programming)< /a>
http://en.wikipedia.org/wiki/Closure_(computer_programming) #C.2B.2B
因此,正如您所看到的,闭包是一种在某个范围内定义的“函子”,以便它吸收该范围内可用的变量。换句话说,它是一个在程序运行期间动态构建的函数,并且该构建过程由包含该函数的作用域的运行时值进行参数化。因此,在 C++ 中,闭包是使用构建 lambda 的函数内的值的 lambda。
lambda 和接受函数/函子的模板参数有什么区别? ——这又让人困惑了。区别在于,它们确实没有任何相似之处。接受函数/函子的模板“参数”已经是混淆的措辞,所以我假设“参数”你的意思是“函数”,因为参数不接受任何东西。在这种情况下,虽然 lambda 可以接受仿函数作为参数,但它不能被模板化。第二,通常 lambda 是作为参数传递给接受函子参数的函数的。
闭包只是一个具有某些设置对象状态(范围?)的函子吗?
正如您通过上面的链接看到的那样,没有。事实上,闭包甚至没有状态,真的。闭包是基于构建它的其他实体的状态构建的,在该函子中,虽然这不是状态,但它是对象的构造。
这些构造的“杀手级应用程序”是什么?或者也许是典型的用例?
我会将其改写为“为什么这些东西有用?”
好吧,一般来说,如果任何对象具有operator(),那么将其视为函数的能力对于整个数组都非常有用。其一,它允许我们通过使用对象或自由函数来扩展任何 stdlib 算法的行为。它所具有的巨大用途是无法计数的。
更具体地说,就 lambda 表达式而言,它们只是让这个过程变得更容易。对象定义带来的限制使得使用 stdlib 算法的过程在某些情况下效率稍低(从开发使用的角度来看,而不是程序效率)。一方面,当时至少任何作为参数传递给模板的对象都必须在外部定义。我相信这也在改变,但仍然......必须创建整个对象只是为了执行基本的事情,只在一个地方使用,很不方便。 Lambda 表达式允许该定义在其使用的范围内变得非常简单,等等......
Ok, you're actually asking a bunch of different questions, possibly because you are not fully familiar with terminology. I'll try to answer all.
What's the difference between a lambda and "operator()"? - Let's reword this to, "What's the difference between a lambda and object with operator()?"
Basically, nothing. The main difference is that a lambda expression creates a functional object while an object with an operator() IS a functional object. The end result is similar enough to consider the same though, an entity that can be invoked with the (params) syntax.
What's the difference between a closure and a functor? This is also rather confused. Please review this link:
http://en.wikipedia.org/wiki/Closure_(computer_programming)
http://en.wikipedia.org/wiki/Closure_(computer_programming)#C.2B.2B
So, as you can see, a closure is a kind of "functor" that is defined within a scope such that it absorbs the variables available to it within that scope. In other words, its a function that is built on the fly, during the operation of the program and that building process is parameterized by the runtime values of the scope containing it. So, in C++ closures are lambdas that use values within the function building the lambda.
What is the difference between a lambda and a template argument accepting a function/functor? - This is again confused. The difference is that they are nothing alike, really. A template "argument" accepting a function/functor is already confused wording so I'll assume by "argument" you mean "function", because arguments don't accept anything. In this case, although a lambda can accept a functor as an argument, it can't be templated, one. Two, generally the lambda is the one being passed as an argument to a function accepting a functor argument.
Is a closure just a functor with some set object state (scope?)?
As you can see by the above link, no. In fact, a closure doesn't even have state, really. A closure is built based UPON the state of some other entity that built it, within that functor though this isn't state, it's the very construction of the object.
What is the "killer app" for these constructs? or perhaps the typical use case?
I'll reword that to, "Why are these things useful?"
Well, in general the ability to treat any object as a function if it has operator() is extremely useful for a whole array of things. For one, it allows us to extend the behavior of any stdlib algorithm by using either objects or free functions. It's impossible to inummerate the vast supply of usefulnesses this has.
More specifically speaking of lambda expressions, they simply make this process yet easier. The limitations imposed by object definitions made the process of using stdlib algorithms slightly inefficient in some cases (from a development use perspective, not program efficiency). For one thing, at the time at least any object passed as a parameter to a template had to be externally defined. I believe that's also changing, but still...having to create entire objects just to perform basic things, that only get used in one place, is inconvenient. Lambda expressions allow that definition to be quite easy, within the scope of the place its being used, etc, etc...