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.