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.

  1. should pass g or &g in capture field of lambda ?
  2. 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

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -