如何转换std :: future< T> to std :: future< void&gt ;? [英] How to convert std::future<T> to std::future<void>?

查看:220
本文介绍了如何转换std :: future< T> to std :: future< void&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况,其中我有一个 std :: future< some_type> 从调用API A产生,但需要提供API B与 std :: future< void>

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);

如果没有建议的功能,例如 .then() when_all(),是否有任何有效的方法扔掉附加到 std :: future< T& code>并且只留下表示事件完成的底层 std :: future< void>

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);


推荐答案

p>

The best you can get is probably this:

auto f = api_a();
auto void_f = std::async(std::launch::deferred,[fut = std::move(f)]{ fut.wait();});
api_b(void_f);

这篇关于如何转换std :: future&lt; T&gt; to std :: future&lt; void&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆