- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
注意:即使在 C++17 中,以下内容也是非法的!
#include <thread>
#include <future>
#include <experimental/future>
using namespace std;
int step1(experimental::future<int>)
{
return {};
}
int step2(experimental::future<int>)
{
return {};
}
int step3(experimental::future<int>)
{
return {};
}
int main()
{
return async([](){ return {}; })
.then(step1)
.then(step2)
.then(step3)
.get();
}
C++1z 提供了两种future
:
std::future
std:experimental::future
但是,std::async
只返回std::future
,所以上面的代码是非法的。如果 std::async
返回 std:experimental::future
,那么就可以了。
我的问题是:
有没有一种方法可以将 std::async
与 std::experimental::future
一起使用,从而使上述代码在 C++1z 下合法?
最佳答案
Is there a way to use
std::async
withstd::experimental::future
that makes the code above legal under C++1z?
没有。 std::async
返回 std::future<T>
尽管有名称,但它是与 std::experimental::future<T>
完全无关的类型.
您必须编写自己的 async
版本这给了你一种新的 future
.一个简化的版本是这样的:
template <class F, class... Args,
class R = std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>>
std::experimental::future<R> new_async(F&& f, Args&&... args)
{
std::experimental::promise<R> p;
auto fut = p.get_future();
std::thread thread([p=std::move(p), f=std::forward<F>(f),
args=std::tuple<std::decay_t<Args>...>(std::forward<Args>(args)...)] () mutable
{
try
{
if constexpr(std::is_void_v<R>)
{
std::apply(std::move(f), std::move(args));
p.set_value();
}
else
{
p.set_value(std::apply(std::move(f), std::move(args)));
}
}
catch(...)
{
p.set_exception(std::current_exception());
}
});
thread.detach();
return fut;
}
这不支持其他启动策略,例如 async
确实如此,但这只是一个开始。
关于c++ - 有没有办法将 std::async 与 std::experimental::future 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42831639/
使用 FFMPEG 将 flv 转换为 mp4 时,显示以下错误 [aac @ 0x2b4b640] The encoder 'aac' is experimental but experimenta
尝试在 IntelliJ 中构建 Kotlin/Ktor 应用程序时,出现多个表单警告 Warning:(276, 6) Kotlin: This class can only be used wit
我在 中编码C++ 在 Visual Studio (Windows 10)并收到此错误: #error The header providing std::experimental::filesy
我有一段代码使用了很多实验性的功能(when,smartmatch,given),这不是我的代码,并且我不想看到很多有关实验性功能的警告。因此,我在此代码中添加了no warnings 'experi
是否可以通过保留一个计数器来查看算法经过了多少次迭代来完成,或者是否需要记录持续时间? 最佳答案 目前接受的不会给你任何理论估计,除非你能以某种方式用一个近似它们的函数来拟合实验测量的时间。这个答案为
Kotlin 协程可以在生产中使用,它们的实验状态意味着什么? 最佳答案 更新:从 Kotlin 1.3 开始,Kotlin 协程不再是实验性的。 Kotlin 协程可以而且应该在生产环境中使用。这是
我正在使用Ubuntu 16.04。我有gcc-9.2.0,但是我在/usr/local/include/c++/9.2.0/experimental中找不到原子头文件,我想我在编译gcc时必须启用它
我想在 Android Studio 中根据口味设置我的 appName。我使用 com.android.tools.build:gradle-experimental:0.4.0 插件,所以我不能使
我想在我的 cpp 多线程代码中使用 std::experimental::barrier。但即使我写这样的代码: #include #include #include int main ()
我不是 100% 确定以下代码在语义上是正确的: #include #include int main() { std::string str = "lvalue string";
我正在尝试创建一个管道实现,该实现将公开一个接口(interface)以链接对某些给定数据的多个操作,同时保持所述数据的不变性。这里需要注意的是,操作(显然只是纯函数)应该能够改变数据的类型(例如,从
开发环境为 OS X 10.10.3, Perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-thre
我试着做 github std::simd 上给出的例子但我的矢量化版本最终慢了 2-3 倍。如何正确使用? documentary缺乏认真的文档和进一步的示例用法。没有列出构造函数等。我确定我可能以
是否可以避免使用 @ExperimentalTime 注释我的代码的每一部分? ?环顾四周后,我在我的 gradle 构建中尝试了这个: withType().all { kotlinOpti
我正在尝试构建使用std::experimental::optional的项目。对于Linux,它可以很好地编译,但是对于android / ndk,编译器会抛出: error: no member
我的 Perl 程序抛出了一些警告,但我还没有在互联网上搜索解决方案。有什么方法可以重写以下代码片段,这样就不会抛出警告吗? “引用键是实验性的......”: foreach my $key ( k
我正在尝试使用 Cake-Plist 插件,但收到一个错误消息,指出动态尚未在正在使用的 Roslyn 版本中实现。然后从别人的建议中我被告知尝试 -Experimental 开关。使用开关时,我在尝
编译包含 的代码时出现错误. main.cpp 中的代码: #include int main() { } 编译这个(clang 版本是 3.9): clang++ main.cpp -o mai
我在 gcc 6.3.1 中使用实验性 std::filesystem 实现,遇到了一些关于 std::experimental::filesystem::directory_iterator #in
我使用 autobind-decorator bind 上下文到 this。 但是我得到了警告/错误 Experimental decorator usage(Decorators 是一个可能会改变的
我是一名优秀的程序员,十分优秀!