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

标签 c++ c++20 clang++ c++-coroutine

根据 cppreference.com ( https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features ) Clang 从版本 8 开始部分支持 C++20 协程:
Clang coroutine support
但是如果在 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 中。命名空间。

    关于c++ - Clang 中 C++20 协程支持的状态如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68540816/

    相关文章:

    c++ - 如何通过创建类型特征来对 NTTP 类进行分类?

    c++ - gcc 允许未初始化的 constexpr

    c++ - 谷歌模拟析构函数

    c++ - "Read-Only Region"Visual Studio 2008 选项

    使用概念具有特定值类型的任何容器的 C++ 迭代器

    templates - 模板化类中的构造函数继承 (C++11)

    c++ - 如何修复 lib libstdc+ +'s ` std::variant`(来自 GCC 9.1)不适用于 Clang 8?

    C++ copy c'tor 现在开始行动了。不清楚为什么

    c++ - QTextDocument:获取特定页面的内容

    c++ - 为什么此分配器不适用于 `std::allocate_shared` ?奇怪的模板替换错误