繁体   English   中英

Clang 中 C++20 协程支持的状态如何?

[英]What is the status of C++20 coroutines support in Clang?

根据 cppreference.com ( https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features ) 自版本 8 以来,Clang 部分支持 C++20 协程:

Clang 协程支持

但是如果在 Clang 主干(即将发布的版本 13)中,我写

#include <coroutine>

它导致错误( https://gcc.godbolt.org/z/rTfjbarKz ):

/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/coroutine:334:2: error: "the coroutine header requires -fcoroutines"
#error "the coroutine header requires -fcoroutines"

如果我在命令行中添加-fcoroutines标志,那么 Clang 会抱怨( https://gcc.godbolt.org/z/qMrv6nMzE ):

clang-13: error: unknown argument: '-fcoroutines'

有什么办法可以在 Clang 中开始使用 C++20 协程吗?

请注意,第一个错误出现在 GCC 标准库中,由此可以推断-fcoroutines选项适用于 GCC 而不是 Clang。

要使用 Clang libc++ 构建,您需要添加选项-stdlib=libc++ 但这将导致找不到<coroutine>头文件。

由于 Clang 协程仍处于“实验”阶段,因此您必须包含<experimental/coroutine>

所以有两件事你需要改变:

  • 使用 Clang libc++ ( -stdlib=libc++ )
  • 包含实验头文件( #include <experimental/coroutine>

还要注意,由于协程是实验性的,头文件中定义的符号将在std::experimental命名空间中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM