C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Where await_transform = default?

From: Lewis Baker <lewissbaker_at_[hidden]>
Date: Tue, 24 Sep 2024 16:34:16 +0930
On Tue, Sep 24, 2024 at 3:42 PM Nikl Kelbon via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Just where is it? Example:
> You want to get handle from corouitne, writing awaiter:
>
> struct get_handle {
> ??? handle;
> await_ready();
> await_suspend(...);
> ??? await_resume { return hadle }
> }
>
> So, code must know type of handle where its called. Only way to do it -
> write await_transform in coroutine type.
>
> So, programmer may write
>
> each_of_my_coroutine_promises {
> ...
> auto await_tranfsorm(get_handle_tag) { ... }
> ...
> }
>
> But now ALL await expressions are broken! Because compiler tries to call
> await_transform for them, even if it is not a suitable function.
>
> And its not fixable now. Programmer may try to emulate behavior of
> standard (implicit) await transform, but it cannot 100% copy behavior of
> compiler-inlined version, its verbose, its error-phone, it produces worse
> code generation and force coroutine frame.
>
> So, where await_transform() = default ?
>

The usual approach is to define an overload of await_transform as follows
alongside the other await_transform overloads:

template<typename T>
T&& await_transform(T&& x) noexcept {
  return std::forward<T>(x);
}

In optimised builds this should be completely optimised out.

For example: https://godbolt.org/z/1EYozcfPG
This shows that the generated code is identical between a promise that has
the above await_transform and one that has no await_transform when compiled
with the latest clang compiler.

Perhaps you could provide a concrete code-snippet that demonstrates the
problem more clearly?

Regards,
Lewis.

Received on 2024-09-24 07:04:30