Home > database >  Lambda functions with "=" capture and memory usage
Lambda functions with "=" capture and memory usage

Time:02-05

In my mind when I create a lambda [=]{...} all variables from parent function clones to the lambda.

So the following code will use too much memory because variables a...z will be copied to lambda function:

void foo() {
    long double a = 0.123456789;
    long double b = 0.123456789;
    long double c = 0.123456789;
    //.... 
    long double z = 0.123456789;

    auto val = [=]() {return a z;};
}

Is not it?

CodePudding user response:

[=] will cause only the variables that are actually used in the lambda to be captured by it.

In your case val will have a copy of a and z. Assuming there is no padding (which there shouldn't be), then sizeof(val) == 2*sizeof(long double).

  •  Tags:  
  • Related