C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Allowing coroutine_handle::from_promise to accept a pointer-interconvertible object

From: Aaron Jacobs <jacobsa_at_[hidden]>
Date: Wed, 4 Jan 2023 14:31:28 +1100
On Wed, Jan 4, 2023 at 12:50 PM Edward Catmur <ecatmur_at_[hidden]> wrote:
> If I've understood correctly, this is a minimal example of what you
> want to work:

Yep, that's a good encapsulation, thanks.


> Presumably it would be possible to do this in a compliant
> manner by storing both a `promise_base*` and a fully-erased
> `std::coroutine_handle<>`. But that would be wasteful even if indeed
> it does work.

Yes, that's right: I want to make use of the linked list that already exists
without needing to spend additional code size and CPU time on maintaining the
redundant links in the list.


> Perhaps an alternative would be for the library to perform the
> conversion e.g. with a suitably constrained converting constructor
> `std::coroutine_handle<P>::coroutine_handle(coroutine_handle<Q>)`.

Yeah, that's even better. I was going for the minimal edit, but either of these
would be even better in my opinion:

    template <typename P>
    struct coroutine_handle {
      // Converting constructor, with suitable overload
      // restrictions.
      template <typename Q>
        requires (is_pointer_interconvertible_base_of<P, Q> &&
                  alignof(P) == alignof(Q))
      coroutine_handle(coroutine_handle<Q>);

      // Or ditto with from_promise.
      template <typename Q>
        requires (is_pointer_interconvertible_base_of<P, Q> &&
                  alignof(P) == alignof(Q))
      coroutine_handle from_promise(Q&);
    };

Perhaps the converting constructor is better.

Received on 2023-01-04 03:31:55