I am using a function (not mine, from a library I don't control) with a signature similar to this:
template<typename T, typename F> void do_async(T const&, F); // the second parameter is a callable and it usually used like this
do_async(obj, [this](){ ... }); I wrote a function similar to this that always uses the same callback lambda:
template<typename T> void do_async_wrapper(T obj) { do_async(obj, [this](){ ... }); } My problems stem from the fact that since the obj is passed as a reference, it has to be kept alive until the callback is called. My function is supposed to take care of that so the caller doesn't have to worry about object lifetime. That is why it accepts by value.
I would like to do something like this in order to keep it alive:
template<typename T> void do_async_wrapper(T obj) { do_async(obj, [this, obj{std::move(obj)}](){ ... }); } Obviously this wont (always) work as-is since the reference points to the function-local one which doesn't exist anymore, while the one kept alive is a (move-)copy of it.
Any suggestions?
do_asyncdoing with that parameter? Are you sure it doesn't make a copy internally?std::shared_ptr.std::shared_ptr, even with the dynamic allocation.