Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I make lambdas static?

Tags:

c++11

lambda

If I have a function that defines a lambda, will the lamda be 'constructed' every time the function is called? Should I make it static to prevent that?

void func(int x)
{
    static auto lambda = [&x](int y) -> bool {
        // ...
    };
}
like image 785
Paul Manta Avatar asked Aug 02 '11 06:08

Paul Manta


1 Answers

No, don't make it static, as it captures a local variable by reference.

I have no idea what the cost of constructing a lambda is. If you suspect it to be a performance problem: benchmark.

like image 195
Henrik Avatar answered Oct 04 '22 21:10

Henrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!