简体   繁体   中英

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

According to cppreference.com ( https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features ) Clang has partial support of C++20 coroutines since version 8:

Clang 协程支持

But if in Clang trunk (which is upcoming version 13) I write

#include <coroutine>

it results in the error( 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"

And if I add -fcoroutines flag in the command line, then Clang complains( https://gcc.godbolt.org/z/qMrv6nMzE ):

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

Is there any way to start using C++20 coroutines in Clang?

Note that the first error is in the GCC standard library, and from that it can be deduced that the -fcoroutines option is for GCC not Clang.

To build with the Clang libc++ you need to add the option -stdlib=libc++ instead. But that will lead to the <coroutine> header file not being found instead.

Since Clang coroutines is still in the "experimental" stage you have to include <experimental/coroutine> .

So there are two things you need to change:

  • Use the Clang libc++ ( -stdlib=libc++ )
  • Include the experimental header file ( #include <experimental/coroutine> )

Also note that since coroutines are experimental, the symbols defined in the header file will be in the std::experimental namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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