對於C++中可變lambda表達式
在《C++primer》中,可變lambda 默認情況下,對於一個值被拷貝的變量,lambda不會改變其值。如果我們希望能改變一個被捕
獲的變量的值,就必須在參數列表後加上關鍵字mutable。因此,可變lambda不能省略參數列表。
void f3()
{
size_t v1 = 42; // local variable
// f can change the value of the variables it captures.
auto f = [v1] () mutable {return ++v1;};
v1 = 0;
auto j = f(); // j is 43
cout<<j<<endl;
return 0;
}
當去掉參數列表的是會報錯