C++ Logo

std-proposals

Advanced search

Re: Do we need a way to take *this by value, for coroutines?

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Mon, 8 Nov 2021 10:30:37 -0500
On Mon, Nov 8, 2021 at 9:42 AM Ville Voutilainen via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Mon, 8 Nov 2021 at 15:30, Phil Endecott via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> > Quoting Marcin's example:
> >
> > text_generator_t text(this F self) //no `&`!
> > {
> > for (auto c: *(self.p)) co_yield c;
> > }
> >
> > I do wonder why it needs to introduce the parameter name "self",
> > and can't simply keep the concise syntax in the body, i.e.
> >
> > text_generator_t text(F this)
> > {
> > for (auto c: *p) co_yield c; // p is this.p
> > }
>
> Because a lambda can have both a deduced-this for the lambda and a
> captured this from a surrounding
> class. The solution to that possible confusion is that a deduced-this
> is never named 'this'.
>

Also, because `this` is not the "name" of the parameter; it's a modifier
that changes the calling convention, at both the API and ABI level.
    struct F {
        void one(this F); // called as `f.one()`
        void two(F); // called as `f.two(f)`
    };
produce different codegen. Neither of those parameters is "named" in the
function declaration's parameter-list; but one is marked as a "this F"
parameter while the other is a plain old "F" parameter.

Also, perhaps because the committee looked at your proposed syntax
    text_generator_t text(F this)
and decided "F this"? :P

–Arthur

Received on 2021-11-08 09:30:50