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

3 c++ clang++ c++20 c++-coroutine

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

Clang 协程支持

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

#include <coroutine>
Run Code Online (Sandbox Code Playgroud)

它导致错误(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"
Run Code Online (Sandbox Code Playgroud)

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

clang-13: error: unknown argument: '-fcoroutines'
Run Code Online (Sandbox Code Playgroud)

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

Som*_*ude 8

请注意,第一个错误出现在 GCC 标准库中,从中可以推断出该-fcoroutines选项是针对 GCC 而不是 Clang。

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

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

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

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

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