I have a situation where I have a std::future<some_type>
resulting from a call to API A, but need to supply API B with a std::future<void>
:
std::future<some_type> api_a();
void api_b(std::future<void>& depend_on_this_event);
In the absence of proposed functionality such as .then()
or when_all()
, is there any efficient way to throw away the value attached to a std::future<T>
and be left only with the underlying std::future<void>
representing the event's completion?
Something like the following could work but would be potentially inefficient:
auto f = api_a();
f.wait();
auto void_f = std::async(std::launch::defer, []{});
api_b(void_f);
The best you can get is probably this:
this is a generic version of @sbabbi's answer.
that allows for any target and dest type to work transparently.
The large downside to this approach is that the resulting future is a deferred future wrapping a (possibly async) future, which means that
.wait_for()
and.ready()
APIs do not work like async futures do. The returned future will never be ready until waited.So we can improve this marginally:
where at least if the future was already ready by the time we converted it, the returned future is also ready.
live example