Closure and Lambda Expression
Sunday, 27. August 2017 03:36PM
#closure和lambda expression
参考:EMC++ chapter 6
lambda expression简称lambda
- A lambda expression is just that: an expression. It’s part of the source code. In
~~~
std::find_if(container.begin(), container.end(),
{ return 0 < val && val < 10; });
~~~
the highlighted expression is the lambda.(上段代码第二行[](int val) { return 0 < val && val < 10; }
)
lambda表达式只是一个表达式,是源代码的一部分。
- A closure is the runtime object created by a lambda. Depending on the capture
mode, closures hold copies of or references to the captured data.
一个闭包(closure)是指某个由lambda创建出的运行时对象。取决于引导器的变量捕捉模式,闭包或是持有被捕捉变量的拷贝或是引用。(jtpan: 本质上是个函数对象)
- A closure class is a class from which a closure is instantiated. Each lambda causes
compilers to generate a unique closure class. The statements inside a lambda
become executable instructions in the member functions of its closure class.
一个闭包类(closure class)就是指闭包所实例化的那个类。每一个lambda会引发编译器生成一个唯一的闭包类。而在lambda内部的述句变成了闭包类的成员函数中可执行的指令。