C++ Logo

std-proposals

Advanced search

Re: Delay the judgement for coroutine function after the instantiation of template entity.

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 20 Jan 2021 12:32:34 -0500
On Wed, Jan 20, 2021 at 11:12 AM Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Wed, Jan 20, 2021 at 9:21 AM Omry Noam via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> What if we specialized a function template templated on bool, to have one version that's a coroutine, and one version that's a normal function?
>> Maybe something like: https://godbolt.org/z/7TEnb6
>> (Code adapted from cppreference as an example)
>
>
> I believe that's well-formed, but I'm pretty sure OP's main goal is to avoid writing the code twice.
>
> I do agree that it would be useful if Chuanqi provided a more fleshed-out and realistic example. When the body of the function is only one line, it's easy to say "just write it twice." If the function were more complicated, it would be more obvious why "write everything twice" wouldn't be an acceptable solution.
> But I personally can't come up with such an example off the top of my head; I'm not good enough at coroutine stuff.

I'm curious to see a complex example too. Especially since the main
point of a coroutine is its ability to `co_await` on other processes,
and that's not something that's very easy to `if constexpr` your way
around. Especially since the variables created in an `if constexpr`
block are local to that block, so you can't exactly do this:

```
if constexpr(UseCoro)
{
  auto value = co_await(coro_expr);
}
else
{
  auto value = regular_expr
}

//Use `value`
```

You'd have to use the `value` within those blocks. So whatever use of
the result that exists would necessarily need to be duplicated. Or
copied out into some shared variable or something.

I'm also curious to know what situations they find coroutines
performing poorly for.

Received on 2021-01-20 11:32:46