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.
精彩评论