With the new C++ coroutines, is it possible to define a coroutine class that I can use to yield from functions without returning a value? Or does yield always have to return something? I'm just interested in the yielding functionality, not a yielded value.
I want this:
Coro Fn() {
if (...) co_yield;
co_return;
}
Not this:
Coro Fn() {
if (...) co_yield dummyValue;
co_return dymmyValue;
}
No.
Per [expr.yield] the grammar of a yield expression (co_yield
) looks like this:
yield-expression:
co_yield
assignment-expression
co_yield
braced-init-list
You can yield a braced-init-list, which, along with an empty class, could be useful:
#include <generator>
struct empty{};
std::generator<empty> foo() {
for (int i = 0; i < 10; ++i)
co_yield {};
}
int main()
{
foo();
}
Demo (gcc, C++23)