我正在为C++ 20设置MacBook,但无法编译代码。我已经安装了最新的Xcode,llvm和gcc。这是我要编译的代码

#include <chrono>
#include <experimental/coroutine>
#include <future>
#include <iostream>

using namespace std;

generator<int> getInts(int first, int last) {
  for (auto i = first; i <= last; ++i) {
    co_yield i;
  }
}

int main() {
  for (auto i : getInts(5, 10)) {
    std::cout << i << " ";
  }
}

但是我得到以下错误:
$ g++ gen.cpp -std=c++2a
In file included from gen.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/experimental/coroutine:66:5: warning:
      <experimental/coroutine> cannot be used with this compiler [-W#warnings]
#   warning <experimental/coroutine> cannot be used with this compiler
    ^

感谢您提供任何有关如何解决此编译问题的见解。

最佳答案

Coroutines TS尚未获得C++ 20的投票(实际上,已经有3票独立通过,但在所有投票中均未获得共识),因此即使GCC实现了该投票(which it doesn't),也不会通过以下方式激活它: -std=c++2a开关。

现在,Clang确实实现了Coroutines TS,您必须使用-fcoroutines-ts打开它。显然,您必须使用libc++。

关于c++ - 如何在Mac上使用c++ 2a编译生成器和协程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53899340/

10-09 16:14
查看更多