方法validate(std::string& request, int& id)
返回 bool
.
当我使用这种语法时,编译完成:
auto task_1 = std::async([&]{ validate(request, id); });
但是,当我使用 std::future<bool>
,编译失败:
std::future<bool> task_1 = std::async([&]{ Internal::validate(request, id); });
错误是:
error: conversion from ‘
std::future<void>
’ to non-scalar type ‘std::future<bool>
’ requested
这里有什么问题?我想检查一下:
task_1.get() == true
最佳答案
改变
std::future<bool> task_1 = std::async([&]{ Internal::validate(request, id); });
到
std::future<bool> task_1 = std::async([&]{ return Internal::validate(request, id); });
您的 lambda 没有返回 bool
;所以它不能用作 bool
的 future 来源.
如果validate
不返回 bool
, 它应该是 future<void>
.
关于c++ - 从 std::future<void> 到非标量 std::future<bool> 的转换引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50646366/