从std :: future <void>转换为非标量std :: future <bool>引发的错误

cat*_*eof 1 c++ c++11 c++14

该方法validate(std::string& request, int& id)返回bool.

当我使用这种语法时,编译完成:

auto task_1 = std::async([&]{ validate(request, id); });
Run Code Online (Sandbox Code Playgroud)

但是,当我使用时std::future<bool>,编译失败:

std::future<bool> task_1 = std::async([&]{ Internal::validate(request, id); });
Run Code Online (Sandbox Code Playgroud)

错误是:

错误:请求从' std::future<void>' 转换为非标量类型' std::future<bool>'

这有什么不对?我想检查一下:

task_1.get() == true
Run Code Online (Sandbox Code Playgroud)

Yak*_*ont 5

更改

std::future<bool> task_1 = std::async([&]{ Internal::validate(request, id); });
Run Code Online (Sandbox Code Playgroud)

std::future<bool> task_1 = std::async([&]{ return Internal::validate(request, id); });
Run Code Online (Sandbox Code Playgroud)

你的lambda没有返回bool; 所以它不能用作未来的来源bool.

如果validate不归bool,那应该是future<void>.