How to pass a c++ functor rvalue reference to a capture of a lambda ? -
i have following function
template<class function> void f(function&& g) { h(..., [&g](){g();}); } it's function f accepting function, lambda or functor argument. inside calls function h pass lambda argument calling g , receives g capture.
- should pass
gor&gin capture field of lambda ? - will functor copied above code ?
if capture g reference, is, syntax &g shown in snippet, no copy performed. preferred way. should copy if lambda might called after f finishes, potentially implies destruction of object g refers to.
in case, forwarding cheaper though:
template<class function> void f(function&& g) { h(…, [g=std::forward<function>(g)] {g();}); }
Comments
Post a Comment