为什么 lambda 被认为是一个表达式(或者:C++ 中的表达式是什么)?

Why is a lambda considered an expression (Or: What constitutes an expression in C++)?

Lambdas are considered expressions. According to cppreference an expression is "a sequence of operators and their operands, that specifies a computation." An expression also has a value category and a type: "The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type [...]" The unique class type is clear to me and I presume it is an prvalue whose evaluation "initializes an object" rather than "computes the value of an operand of a built-in operator" (Value Categories).

维基百科上的文章 Expression (Computer Science) 说:“[...] 表达式是编程语言中的句法实体,可以对其进行计算以确定其值。”

我仍然对 lambda 表达式的计算结果(即它的值)感到困惑。 例如,考虑以下简单的一元谓词 lambda:

[](auto val) { return val % 2 == 0; }

这个表达式的值是多少?它是唯一 class 类型的对象吗? cppreference 定义中提到的计算在哪里?

What's the value of this expression? Is it an object of the unique class type?

是的,正是这样。

Where is the computation that is mentioned in the cppreference definition?

与可以说 1std::vector<int>{} 计算值的方式相同,lambda 表达式也是如此。

它们本身什么都不做。 vast 大多数表达式要么是其他表达式的操作数(包括作为函数调用表达式的参数),要么是变量的初始化器,要么在求值时有一些副作用。

结果是

value 'category',而不是 value

仔细阅读这条语句:lambda 表达式是 prvalue 表达式 unique unnamed non-union non-aggregate class类型,称为闭包类型.

类型:闭包类型
值类别: prvalue

例如

//func has the type of the prvalue expression (closure type)
auto func = [](auto val) { return val % 2 == 0; };

//result has the type of the 'value' returned by function-call operator, 
//here, the type is bool and the value is: (42 % 2 == 0)
auto result = func(42);