【发布时间】:2019-10-06 12:25:06
【问题描述】:
我不知道如何命名标题,希望它是正确的......
我偶然发现了下面的 lambda 定义,但不明白语法,var = [=] 和 return [=] 的含义是什么?
也是下面ConstexprLambda()函数中的第二个问题,为什么我们不能必须调用add(1, 2)而不是add(1, 2)()为什么需要额外的()而在调用identity(123)时代码没有使用额外的()?
问题被放入代码的 cmets 中。
auto identity = [](int n) constexpr
{
return n;
};
constexpr auto add = [](int x, int y)
{
auto L = [=] // what is = [=]?
{
return x;
};
auto R = [=]
{
return y;
};
return [=] // what return [=] means here?
{
return L() + R();
};
};
void ConstexprLambda()
{
static_assert(identity(123) == 123);
static_assert(add(1, 2)() == 3); // why can't we just add(1,2) like above?
}
示例取自here
【问题讨论】: