开发者

Should I make lambdas static?

开发者 https://www.devze.com 2023-03-25 11:15 出处:网络
If I have a function that defines a lambda, will the lamda be \'cons开发者_JAVA百科tructed\' every time the function is called? Should I make it static to prevent that?

If I have a function that defines a lambda, will the lamda be 'cons开发者_JAVA百科tructed' 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 {
        // ...
    };
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消