Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can co_yield return nothing?

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;
}
like image 354
Dess Avatar asked May 03 '26 21:05

Dess


1 Answers

No, some expression must follow the co_yield operator. However, you do not need to use the yielded value. You can simply ignore it.

For example, yield a message:

co_yield "ignore"; 

And add this to your promise_type:

auto yield_value(const char*) {
   return std::suspend_always{};
}
like image 145
dwto Avatar answered May 05 '26 12:05

dwto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!