Think of lambda as a small object with a function call operator:
int foo = 1000; auto f = [=]() ->int { return foo; };
somewhat equivalent:
class FooLambda { int foo; public: FooLambda(int foo) : foo(foo) {} int operator()(){ return foo; } };
So, you see that the function object itself can be embedded if it is displayed in the same translation unit as it is (and, possibly, if not using compilers more than a compiler). Since your invoke is a template, it knows the actual type of lamdba, and you do not force it to jump over function bypass pointers, which is a big nesting inhibitor.
Taking the called object by value or reference in invoke determines whether the captured variables are local to the function body or not, which may matter if this means that they will be in the cache.
Bobtfish
source share