C++ lambda copy value in capture-list

后端 未结 3 702
慢半拍i
慢半拍i 2021-01-04 03:05

I have a program as below:

int main()
{
    int val = 4;
    auto add = [val](int a)->int{
        val += 2;
        return a+val;
    };
    cout <<         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 03:36

    The operator () of a lambda is implicitly const unless the lambda is declared mutable - and you can't modify the data members in a const member function. This happens regardless of the type of the capture.

提交回复
热议问题