C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] defect report: coexisting return_value and return_void in coroutines
From: Avi Kivity (avi_at_[hidden])
Date: 2020-10-11 08:39:53


The standard says ([dcl.fct.def.coroutine]):

6 The unqualified-ids return_­void and return_­value are looked up in
the scope of the promise type. If both are found, the program is ill-formed.

However, there are good reasons to want both:

std::future<void> yet_another();

std::future<void> my_coroutine() {

 Â Â Â  if (some_check) {

 Â Â Â Â Â Â Â  co_return;  // calls promise_type::return_void()

 Â Â Â  }

 Â Â Â  co_await something_else();

 Â Â Â  co_return yet_another(); // calls
promise_type::return_value(std::future<void>)

}

The last statement calls another coroutine, but this is not legal under
the current wording (though gcc accepts it).

I could rewrite the coroutine as follows:

std::future<void> my_coroutine() {

 Â Â Â  if (some_check) {

 Â Â Â Â Â Â Â  co_return;  // calls promise_type::return_void()

 Â Â Â  }

 Â Â Â  co_await something_else();

 Â Â Â  co_await yet_another(); // typically suspends the coroutine here

 Â Â Â  co_return; // optional

}

But this will cost something - typically the coroutine body has to be
suspended and awakened for co_await.

Remediation: allow return_value() and return_void() to coexist.


STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups