c++ - 从 std::future<void> 到非标量 std::future<bool> 的转换引发错误

标签 c++ c++11 c++14

方法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/

相关文章:

c++ - VS C++ 程序仅在从文件夹运行 .exe 时才有效? [不是VS调试]

c++ - 除了可访问性,访问说明符还有什么作用?

c++ - constexpr 上下文中 std::array 指针的 size()

c++ - 使用 range for on a boost FUSION 序列

c++ - 错误的 "value computed is not used"警告

c++ - 从容器中添加和删除自身

c++ - 在 AMD CCC 中启用 OpenGL 三重缓冲 + vsync 会破坏我们的应用程序

c++ - 数组是什么类型?

c++ - gcc6.x 中的 constexpr 优化错误?

c# - 用于良好编程实践的 Visual Studio 工具