C++ Logo

std-proposals

Advanced search

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

From: Nikl Kelbon <kelbonage_at_[hidden]>
Date: Tue, 24 Sep 2024 11:33:56 +0400
As its easy to make a mistake, for example its not obvious to most of
programmers, that returning such reference will not be error

        template<typename A>
        A&& await_transform(A&& awaitable) noexcept

And still programmer required to change all coroutine types just to add
possiblity to take its handle, which seems not very extensible, also it
makes more difficult to extend promise types, for example when i inherit
one, i must declare await_transform, but ONLY if inherited type has its own
await_transform, but

using Base::await_transform

will be error, so it forces to write many hacky code

вт, 24 сент. 2024 г. в 11:04, Lewis Baker via Std-Proposals <
std-proposals_at_[hidden]>:

> 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.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-09-24 07:34:09