C++ Logo

std-proposals

Advanced search

Re: Make 'this' a reference

From: Jake Arkinstall <jake.arkinstall_at_[hidden]>
Date: Thu, 5 Mar 2020 08:59:10 +0000
I'd choose a different new keyword rather than add a new rule on 'this'
which is bound to have some confusing edge cases (especially when passing
this to template functions - does it resolve as a pointer? A reference? A
special object that tries to behave as both?).

I'd personally go with self *≡* *this, such that self.member = this->member.

On Thu, 5 Mar 2020, 08:50 Marian Darius via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> It feels weird that inside a class the keyword 'this' is a pointer
> to the current object, when it has reference semantics:
> - it is not re-assignable
> - it is not null (as not null as a reference can be)
>
> It seems a natural fit for 'this' to be a reference, making the
> meaning of '&&', 'const&&' and 'const&' qualified methods
> a little more intuitive (that becomes the type of 'this' in that
> method).
>
> The biggest problem with changing 'this' to a reference is
> backwards-compatibility. But we already have a feature in
> this language that we can look at to solve this issue: function
> references convert to function pointers back-and-forth implicitly.
> Here is a simple example: https://compiler-explorer.com/z/EdVLD4
>
> So we could make 'this' a similar style of reference that implicitly
> converts to a pointer when needed (for example, when using '->'
> operator or passing to a function taking a pointer).
>
> Disclaimer: I am not an expert, and this is not a well thought-out
> proposal. I just want some feedback on the idea and on whether I
> should maybe write a formal proposal for it.
>
> Is this something that interests people? It feels like it might make
> the language easier to use and understand, especially to beginners.
>
> Was this proposed before? If so, what happened to that proposal?
> Is there something hopelessly broken that I didn't think of that can
> make this unfeasible?
>
> Darius
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2020-03-05 03:02:04