I am playing around with C++ 20's Coroutines. The sample is compiled with clang++.
The compiler error I am facing is
error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream') and 'cppcoro::generator')
which is about the following line
std::cout << numbers << " ";
the full code snipped looks like this:
#include <thread>
#include <iostream>
#include <vector>
#include <cppcoro/generator.hpp>
using namespace std;
// coroutine
cppcoro::generator<int> generatorForNumbers(int begin, int inc = 1)
{
// for Schleife ohne Abbruchbedingung
for (int i = begin;; i += inc)
{
co_yield i;
}
}
int main()
{
auto numbers = generatorForNumbers(-10);
for (int i= 1; i <= 20; ++i)
{
std::cout << numbers << " ";
}
std::cout << "\n\n";
// inline works
for (auto n: generatorForNumbers(0, 5))
{
std::cout << n << " ";
}
std::cout << std::endl;
}
Executable code snipped can be found here: https://godbolt.org/z/4cxhqxPP7