在 C++11 中,"lambda function" 与 "lambda expression" 和 "closure" 相同吗?
In C++11, is "lambda function" same with "lambda expression" and "closure"?
在 cppreference 网站上,当谈到 direct_initialization 时,它说它在闭包参数捕获中工作,如...{...}
我觉得这个应该是"lambda function",可是为什么又叫"closure"呢?
查了C++11标准,"closure"项第一次出现在section 5.1.2 (lambda expression),里面说:
The evaluation of a lambda-expression results in a prvalue temporary (12.2). This temporary is called the
closure object. A lambda-expression shall not appear in an unevaluated operand (Clause 5). [ Note: A
closure object behaves like a function object (20.8). — end note ]
那么如何理解"evalute lamdba expression"呢?这是 "evaluation" 在编译或运行时发生的事情吗?
在 C++11 中,项目 "lambda expression"、"lambda function" 和 "closure" 之间是否存在差异?
我不是在谈论像 Closure 这样的其他编程语言,只关注 C++11。
谢谢。
Lambda 表达式与其他表达式一样,默认在运行时计算。计算表达式 1 + 2
给出一个 int
其值为 3。计算 lambda 表达式 [] { return 0; }
给出一个闭包对象,它也是一个函数对象,operator()()
是一个函数returns 0.
简而言之,闭包对象是一个运行时对象,就像 C++ 中的任何其他对象一样,lambda 表达式是一个结果为闭包对象的表达式。
什么是闭包(引自wikipedia):
In programming languages, closures (also lexical closures or function
closures) are a technique for implementing lexically scoped name
binding in languages with first-class functions. Operationally, a
closure is a record storing a function[a] together with an
environment: a mapping associating each free variable of
the function (variables that are used locally, but defined in an
enclosing scope) with the value or storage location to which the name
was bound when the closure was created[b]. A closure,
unlike a plain function, allows the function to access those captured
variables through the closure's reference to them, even when the
function is invoked outside their scope.
[a] The function may be stored as a reference to a function,
such as a function pointer.
[b] These names most frequently refer to values, mutable
variables, or functions, but can also be other entities such as
constants, types, classes, or labels.
什么是lambda表达式(引自wikipedia):
In computer programming, an anonymous function (function literal,
lambda abstraction) is a function definition that is not bound to an
identifier.
Since C++11, C++ supports anonymous functions, called lambda
expressions, which have the form:
[capture](parameters) -> return_type { function_body }
An example lambda function is defined as follows:
[](int x, int y) -> int { return x + y; }
Since C++11, C++ also supports closures. Closures are defined between
square brackets [and] in the declaration of lambda expression. The
mechanism allows these variables to be captured by value or by
reference. The following table demonstrates this:
[] //no variables defined. Attempting to use any external variables in the lambda is an error.
[x, &y] //x is captured by value, y is captured by reference
[&] //any external variable is implicitly captured by reference if used
[=] //any external variable is implicitly captured by value if used
[&, x] //x is explicitly captured by value. Other variables will be captured by reference
[=, &z] //z is explicitly captured by reference. Other variables will be captured by value
简历
术语 Lambda 表达式 和 Lambda 函数 可互换使用,表示匿名函数对象的 definition/declaration 为:
[capture](parameters) -> return_type { function_body }
术语 闭包 我们指的是 运行-time 函数对象,由 lambda 表达式的计算创建.
现在要在编译时计算 lambda,这将要求 lambda 是常量表达式。不幸的是,lambda 不是 constexpr
,因此无法在编译时求值。但是,有一份提交给委员会 N4487 的提案表明,随着一些限制的取消,我们可以拥有 constexpr
lambda。也就是说,将来我们可能会有 constexpr
可以在编译时求值的 lambda。
在 cppreference 网站上,当谈到 direct_initialization 时,它说它在闭包参数捕获中工作,如...{...} 我觉得这个应该是"lambda function",可是为什么又叫"closure"呢?
查了C++11标准,"closure"项第一次出现在section 5.1.2 (lambda expression),里面说:
The evaluation of a lambda-expression results in a prvalue temporary (12.2). This temporary is called the
closure object. A lambda-expression shall not appear in an unevaluated operand (Clause 5). [ Note: A
closure object behaves like a function object (20.8). — end note ]
那么如何理解"evalute lamdba expression"呢?这是 "evaluation" 在编译或运行时发生的事情吗? 在 C++11 中,项目 "lambda expression"、"lambda function" 和 "closure" 之间是否存在差异?
我不是在谈论像 Closure 这样的其他编程语言,只关注 C++11。 谢谢。
Lambda 表达式与其他表达式一样,默认在运行时计算。计算表达式 1 + 2
给出一个 int
其值为 3。计算 lambda 表达式 [] { return 0; }
给出一个闭包对象,它也是一个函数对象,operator()()
是一个函数returns 0.
简而言之,闭包对象是一个运行时对象,就像 C++ 中的任何其他对象一样,lambda 表达式是一个结果为闭包对象的表达式。
什么是闭包(引自wikipedia):
In programming languages, closures (also lexical closures or function closures) are a technique for implementing lexically scoped name binding in languages with first-class functions. Operationally, a closure is a record storing a function[a] together with an environment: a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or storage location to which the name was bound when the closure was created[b]. A closure, unlike a plain function, allows the function to access those captured variables through the closure's reference to them, even when the function is invoked outside their scope.
[a] The function may be stored as a reference to a function, such as a function pointer.
[b] These names most frequently refer to values, mutable variables, or functions, but can also be other entities such as constants, types, classes, or labels.
什么是lambda表达式(引自wikipedia):
In computer programming, an anonymous function (function literal, lambda abstraction) is a function definition that is not bound to an identifier.
Since C++11, C++ supports anonymous functions, called lambda expressions, which have the form:
[capture](parameters) -> return_type { function_body }
An example lambda function is defined as follows:
[](int x, int y) -> int { return x + y; }
Since C++11, C++ also supports closures. Closures are defined between square brackets [and] in the declaration of lambda expression. The mechanism allows these variables to be captured by value or by reference. The following table demonstrates this:
[] //no variables defined. Attempting to use any external variables in the lambda is an error. [x, &y] //x is captured by value, y is captured by reference [&] //any external variable is implicitly captured by reference if used [=] //any external variable is implicitly captured by value if used [&, x] //x is explicitly captured by value. Other variables will be captured by reference [=, &z] //z is explicitly captured by reference. Other variables will be captured by value
简历
术语 Lambda 表达式 和 Lambda 函数 可互换使用,表示匿名函数对象的 definition/declaration 为:
[capture](parameters) -> return_type { function_body }
术语 闭包 我们指的是 运行-time 函数对象,由 lambda 表达式的计算创建.
现在要在编译时计算 lambda,这将要求 lambda 是常量表达式。不幸的是,lambda 不是 constexpr
,因此无法在编译时求值。但是,有一份提交给委员会 N4487 的提案表明,随着一些限制的取消,我们可以拥有 constexpr
lambda。也就是说,将来我们可能会有 constexpr
可以在编译时求值的 lambda。