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: Wed, 4 Jan 2023 01:19:29 -0500
On Tue, Jan 3, 2023 at 11:27 PM Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Tue, Jan 3, 2023 at 8:50 PM Edward Catmur <ecatmur_at_[hidden]> wrote:
>>
>> On Tue, 3 Jan 2023 at 23:33, Aaron Jacobs via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>>
>>> On Wed, Jan 4, 2023 at 2:18 AM Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]> wrote:
>>> > 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`.
>>>
>>> I agree with Jason: while you are totally right in a practical sense (and that
>>> is why there is no compatibility issue with existing implementations), the
>>> standard says this is not allowed, because a reference to a base class
>>> sub-object of a coroutine promise is not a reference to a coroutine promise,
>>> even if they happen to have the same address This is also what @timsong-cpp
>>> said in https://github.com/cplusplus/draft/issues/6039.
>
>
> Well, Tim Song and Jens Maurer certainly are more authoritative than me on core-wording subjects, but FWIW, I would say that Ed's code above is perfectly well-defined according to the wording you quoted from the current Standard. Tim writes:
>
> > the parameter refers to the PromiseBase subobject of *this, and not the promise object iself. Since the PromiseBase object is not the promise object of any coroutine, it follows that the precondition is not met
>
> But in fact the "promise object of the coroutine" literally is that very `Promise<Result>` object, which is-a `PromiseBase` object.

But is it though? I asked this very question on the discussion ML
(https://lists.isocpp.org/std-discussion/2022/12/1956.php), and the
answer I got was that it was not.

>From a conceptual OOP standpoint, you're right. But from a "what the
standard actually says" standpoint, a base class subobject is a
*subobject* of the derived class object.

> Every `Promise<Result>` object is by definition a `PromiseBase` object, and every pointer to a `Promise<Result>` object is a pointer to a `PromiseBase` object, and this particular `Promise<Result>` object (pointed to by the `PromiseBase`-pointer you have) literally is the promise object of a coroutine. I can't see any rationale by which someone could point to an object in an execution of this particular C++ program and say, "Look, this object is-a PromiseBase, but simultaneously this same object is not the promise object of any coroutine." There's only one object here!
>
> We may at this point be in violent agreement. I think the current wording clearly permits what-you-are-doing, but it seems that there are people out there who think it doesn't, and some of those people might influence vendors such that what-you-are-doing actually stops working at some point in the future (because those other people told the vendor that it was okay to break). That would be bad. So, if the wording needs to be amended to make it clear that what-you-are-doing is literally fine and okay... okay, let's amend the wording to make that clear. :)
>
> –Arthur
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-01-04 06:19:39