Coroutine debugging in c++

Viewed 530

I am trying to test out the coroutine feature of c++20 I have included the necessary flags, but when I #include <coroutine> I get the following error:

cannot open source file "coroutine". I'm sure it's a stupid error!

Code:

// lazyGenerator.cpp

#include <iostream>
#include <vector>
#include <coroutine>  // <---- the immediate error, which causes generator and co-yield not to work

using namespace std; 

generator<int> generatorForNumbers(int begin, int inc = 1) {
  
  for (int i = begin;; i += inc) {
    co_yield i;
  }
  
}

int main() {

    std::cout << std::endl;

    const auto numbers= generatorForNumbers(-10);                   // (2)
  
    for (int i= 1; i <= 20; ++i) std::cout << numbers << " ";       // (4)
  
    std::cout << "\n\n";
                                                         
    for (auto n: generatorForNumbers(0, 5)) std::cout << n << " ";  // (3)

    std::cout << "\n\n";

}

In tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++2a", // <---- changed from -std==c++21 by suggestion
                "-fcoroutines",
                "-stdlib=libc++",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Result of running clang++ -v

clang version 11.1.0
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

Error for coroutine with c++2a flag

/Users/lukeanglin/Desktop/C++/debug/hello_world.cpp:5:10: fatal error: 'coroutine' file not found
#include <coroutine> 
         ^~~~~~~~~~~
1 error generated.
The terminal process "/bin/zsh '-c', '/usr/bin/clang++ -std=c++2a -stdlib=libc++ -g /Users/lukeanglin/Desktop/C++/debug/hello_world.cpp -o /Users/lukeanglin/Desktop/C++/debug/hello_world'" terminated with exit code: 1.

Error if I run the c++20 flag:

error: invalid value 'c++20' in '-std=c++20'
note: use 'c++98' or 'c++03' for 'ISO C++ 1998 with amendments' standard
note: use 'gnu++98' or 'gnu++03' for 'ISO C++ 1998 with amendments and GNU extensions' standard
note: use 'c++11' for 'ISO C++ 2011 with amendments' standard
note: use 'gnu++11' for 'ISO C++ 2011 with amendments and GNU extensions' standard
note: use 'c++14' for 'ISO C++ 2014 with amendments' standard
note: use 'gnu++14' for 'ISO C++ 2014 with amendments and GNU extensions' standard
note: use 'c++17' for 'ISO C++ 2017 with amendments' standard
note: use 'gnu++17' for 'ISO C++ 2017 with amendments and GNU extensions' standard
note: use 'c++2a' for 'Working draft for ISO C++ 2020' standard
note: use 'gnu++2a' for 'Working draft for ISO C++ 2020 with GNU extensions' standard
The terminal process "/bin/zsh '-c', '/usr/bin/clang++ -std=c++20 -stdlib=libc++ -g /Users/lukeanglin/Desktop/C++/debug/hello_world.cpp -o /Users/lukeanglin/Desktop/C++/debug/hello_world'" terminated with exit code: 1.
1 Answers

At the time of writing, the current release of Clang does not fully implement the coroutines as described here.

However, the last release support a old coroutine TS that has been superseded since C++20. If you want just to play with coroutines temporary, you can enable it by using the compilation flags -std=c++20 -stdlib=libc++ and by replacing your include with #include <experimental/coroutine>. I you want a non-experimental version, you need to wait.

Related