C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] Make 'this' a reference
From: Marian Darius (marian.darius98_at_[hidden])
Date: 2020-03-05 02:49:51


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 list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups