开发者

Should I make lambdas static?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜