[英]Set value of temporary std::future
I have a function which returns a std::pair<std::future<bool>,std::future_status>
.我有一个 function ,它返回一个
std::pair<std::future<bool>,std::future_status>
。 In some cases I want future.get() to always return true and I was wondering if there was a legit way of doing it.在某些情况下,我希望future.get() 始终返回true,我想知道是否有合法的方法。 Currently I am doing something horrendous:
目前我正在做一些可怕的事情:
std::future<bool> tmp = std::async(std::launch::async,[]()->bool{return true;});
return std::make_pair(std::move(tmp), std::future_status::ready);
Does anybody know a better way of setting the value of tmp to true?有人知道将 tmp 的值设置为 true 的更好方法吗?
I'm not sure what effect you're trying to achieve, but you can use a std::promise
to directly set the value of a std::future
:我不确定您要达到什么效果,但是您可以使用
std::promise
直接设置std::future
的值:
#include<future>
std::promise<bool> tmpPromise; //default construct promise
std::future<bool> tmp = tmpPromise.get_future(); //get future associated with promise
tmpPromise.set_value(true); //set future value
return std::make_pair(tmp, std::future_status::ready);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.