首先,与普通函数的参数列表一致。如果不需要参数传递,则可以连同括号“()”一起省略。
可以在lambda表达式末尾用括号添加参数列表的参数,例子如下:
void TestLambda()
{
int a = 1;
int b = 2;
auto funcAdd = [=, &b](int c, int d)
{
return b += a + c + d;
}(123, 321);
std::cout << funcAdd << std::endl;
system("pause");
}
上个例子中,引用方式捕获b,并在末尾括号内赋值了c与d的值。最后结果如下:
或者在使用lambda表达式时再进行传参:
void TestLambda()
{
int a = 1;
int b = 2;
auto funcAdd = [=, &b](int c, int d)
{
return b += a + c + d;
};
std::cout << funcAdd(12, 21) << std::endl;
system("pause");
}