I\'d like to use lambda functions to asynchronously call a method on a reference counted object:
void RunAsync(const std::function& f) { /* ..
std::function
probably won't be as fast as a custom functor until compilers implement some serious special treatment of the simple cases.
But the reference-counting problem is symptomatic of copying when move
is appropriate. As others have noted in the comments, MSVC doesn't properly implement move
. The usage you've described requires only moving, not copying, so the reference count should never be touched.
If you can, try compiling with GCC and see if the issue goes away.
Converting to a std::function
should only make a move of the lambda. If this isn't what's done, then there's arguably a bug in the implementation or specification of std::function
. In addition, in your above code, I can only see two copies of the original c
, one to create the lambda and another to create the std::function
from it. I don't see where the extra copy is coming from.