首先,與普通函數的參數列表一致。如果不需要參數傳遞,則可以連同括號“()”一起省略。
可以在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");
}