4 ms·
People who think that lambdas may cause heap allocations are confusing lambdas with std::function. Lambdas are essentially syntactic sugar for an anonymous str
by MauranKilom 5y ago
People who think that lambdas may cause heap allocations are confusing lambdas with std::function.
Lambdas are essentially syntactic sugar for an anonymous struct holding references to/copies of captured variables and an operator() method with your code. Not 100% precise, but memory-wise that's how it works (and has to work). No dynamic memory allocation is involved here at all.
If you put a lambda (or any other callable) in a std::function, the std::function will copy that functor object, either into its own small-buffer-optimized in-place storage or (above a certain functor size) into heap-allocated memory.