|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4338" status="New"> |
| 5 | +<title>sender unaware coroutines should be able to <code>co_await</code> a <code>task</code></title> |
| 6 | +<section><sref ref="[task.class]"/></section> |
| 7 | +<submitter>Dietmar Kühl</submitter> |
| 8 | +<date>31 Aug 2025</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +The <code>task</code> type doesn't have an <code>operator |
| 14 | +co_await()</code>. Thus, a <code>task</code> can't be |
| 15 | +<code>co_await</code>ed directly from a coroutine. Using |
| 16 | +<code>as_awaitable</code> on the <code>task</code> object also |
| 17 | +doesn't work because this function requires a reference to the |
| 18 | +promise as second argument. |
| 19 | +</p> |
| 20 | +<p> |
| 21 | +<code>task</code> could define an <code>operator co_await()</code> that |
| 22 | +returns an awaitable object or directly provide an awaiter |
| 23 | +interface. There are, however, two complications: |
| 24 | +</p> |
| 25 | +<ol> |
| 26 | +<li>For scheduler affinity the <code>task</code> needs to get a |
| 27 | +scheduler from the object starting the <code>task</code>. While |
| 28 | +the promise can be obtained from the coroutine handle passed to |
| 29 | +<code>await_suspend()</code> these normally wouldn't have an |
| 30 | +associated environment supporting a <code>get_scheduler</code> query. |
| 31 | +</li> |
| 32 | +<li> |
| 33 | +The approach to reporting cancellation is to call a function |
| 34 | +<code>unhandled_stopped()</code> on the promise type which is |
| 35 | +generally not available. It could be argued that cancellation isn't |
| 36 | +really a problem because the it is unlikely that the environment |
| 37 | +associated with the promise supports a <code>get_stop_token</code> |
| 38 | +query. |
| 39 | +</li> |
| 40 | +</ol> |
| 41 | +<p> |
| 42 | +It is worth noting that senders in general do not support an |
| 43 | +<code>operator co_await()</code>, i.e., other senders are also not |
| 44 | +awaitable directly. On the other hand, <code>task</code> could |
| 45 | +become the generic adapater to make senders awaitable from |
| 46 | +coroutines. |
| 47 | +</p> |
| 48 | +</discussion> |
| 49 | + |
| 50 | +<resolution> |
| 51 | +<p> |
| 52 | +</p> |
| 53 | +</resolution> |
| 54 | + |
| 55 | +</issue> |
0 commit comments