What is the best practice to use when one needs to store a lambda as a class member so that its invocation can be deferred? More specifically, is it safe to store the lambda passed to the class deferred_lambda in the code listing below as a reference? If not, would it be safe if I were to store the the lambda in deferred_lambda as a value instead of as a reference?
Finally, can I expect to incur a performance penalty in comparison to a regular function call with g++ for storing the lambda as a class member in this way? That is, would using deferred_lambda.invoke() be slower than a call to operator() on some dummy struct that would implement the same operations?
With g++, I noticed that the size of the lambda increases as I use more captured variables. I suppose that this is to be expected, since to my understanding, the compiler internally generates a struct for the lambda that contains the necessary captured variables as members. This observation is what led to the question that I am now asking, since storing lambdas by value may be more expensive in terms of time and memory than storing references to them would.
template <class Func>
class deferred_lambda
{
Func& func_;
public:
deferred_lambda(Func func) : func_(func) {}
void invoke() { func_(); }
};
template <class Func>
deferred_lambda<Func> defer_lambda(Func func)
{
return deferred_lambda(func);
}
void foo()
{
int a, b, c;
auto x = defer_lambda([&]() { a = 1; b = 2; c = 3; });
}
No. That would be a dangling reference after
defereed_lambda()has finished.Yes. But you still have to ensure variables captured by reference still live when executing the lambda.
Probably not, there’s no reason for it.
It still has to be stored somewhere…