C++ Logo

std-proposals

Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Tue, 3 Jan 2023 14:08:02 -0500
On Tue, Jan 3, 2023 at 10:18 AM Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Mon, Jan 2, 2023 at 7:24 PM Aaron Jacobs via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> Hello all,
>>
>> While working on a C++ coroutine library for use at Google I've found that some
>> important functionality is hindered by a precondition on
>> `std::coroutine_handle<Promise>::from_promise` that seems to be very slightly
>> stronger than necessary, and I'm seeking feedback on a proposal to weaken it
>> accordingly. I think this can be done while keeping existing implementations
>> conforming.
>>
>> [coroutine.handle.con] currently lists the following precondition for
>> `from_promise(Promise& p)`:
>>
>> > Preconditions: `p` is a reference to a promise object of a coroutine.
>>
>> I propose changing it to something like the following (feedback on wording
>> welcome):
>>
>> > Preconditions: p is a reference to an object that is
>> > pointer-interconvertible with the promise object of a coroutine, and
>> > has the same alignment as the promise.
>
>
> Just looking at this proposed wording, it doesn't seem like your proposal does anything. A pointer that points to "an object pointer-interconvertible with X, with the same alignment as X" is exactly the same, machine-wise, as a pointer that points to "an X" — all you have to do is cast it to (X*) before passing it to `from_promise`. There's certainly nothing wrong or undefined about taking a pointer-that-physically-points-to-an-X-object and casting it to type pointer-to-X.

"Machine-wise" is irrelevant when it comes to the standard. A pointer
to an object X which is pointer-interconvertible to object Y is not a
pointer to object Y. If the standard has a precondition that you
provide a pointer/reference to object Y, a pointer/reference to an
object that is pointer-interconvertible to Y doesn't satisfy the
requirement. And therefore, UB results.

Received on 2023-01-03 19:08:13