Paul Manta
Paul Manta

Reputation: 31567

Should I make lambdas static?

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 {
        // ...
    };
}

Upvotes: 6

Views: 194

Answers (1)

Henrik
Henrik

Reputation: 23324

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.

Upvotes: 6

Related Questions