c++ - 条件 std::future 和 std::async

标签 c++ c++11 asynchronous future

我需要做有条件的行为。

std::future<int> f = pointer ? std::async(&Class::method, ptr) : 0;

// ... Some code

x = f.get();

所以我想将 ptr->method() 调用的异步结果分配给 x result 或如果 ptrnullptr 则为 0 .

上面的代码可以吗?我可以做类似的事情吗(将“int”分配给“std::futture”?或者也许有更好的解决方案?

最佳答案

std::future没有转换 constructor所以你的代码无效(如果你真的尝试编译代码,你会注意到)。

你可以做的是使用默认构建的 future ,然后检查它是否是 valid在你使用 future 之前。

关于c++ - 条件 std::future 和 std::async,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36833124/

相关文章:

c++ - 错误 LNK2001 : unresolved external symbol with DLL

c++ - 在构造函数中分配给 *this

jquery - 异步谷歌分析虚假综合浏览量跟踪不适用于指向网站外部的链接

asynchronous - 将异步任务的完成/进度监视器与 session 相关联

c# - 如何在异步方法中管理互斥体

c++ - 这属于什么 C++ 风格的语法

c++ - 面部特征检测与追踪

c++ - 如何减少只有一组有限字符的字符串使用的磁盘空间?

c++ - 格式化 QTableView 显示的日期/时间值

c++ - this_thread::sleep_for 影响其他线程