Date: Thu, 5 Mar 2020 12:24:49 +0300
On 2020-03-05 11:49, Marian Darius via Std-Proposals 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).
Also, let's not forget `operator->*`.
Other points of breakage are `typeid(this)`, `decltype(this)`, `auto p =
this` or passing it as a templated argument to a function and inspecting
the type (e.g. using type traits). Honestly, I don't know if it is
possible to work around that, other than to keep `this` a pointer but
allow to use `operator.` and `operator.*` with it. Which I don't like.
> 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?
In general, forgetting about the breakage for a moment, I'd like `this`
to be a reference. But there is one use case where it makes more sense
to have it as a pointer: `delete this`. Yes, you could take an address,
but that is an additional complication, especially when you override
`operator&`. Also, personally I consider dangling references worse than
dangling pointers because in general we assume references are valid
barring bugs. Having `this` behave both as a reference and a pointer
kind of solves it, although it creates one more piece of the "weird
stuff" in the language.
BTW, the implicit conversion of function pointers is most likely out of
the necessity for compatibility with C. In C++ we got member functions,
and taking address explicitly is mandatory for those. Personally, I
don't like the implicit conversion as I believe it hides the intent of
the code while not saving much. It is possible that others, including
the committee, feels that way.
> 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).
Also, let's not forget `operator->*`.
Other points of breakage are `typeid(this)`, `decltype(this)`, `auto p =
this` or passing it as a templated argument to a function and inspecting
the type (e.g. using type traits). Honestly, I don't know if it is
possible to work around that, other than to keep `this` a pointer but
allow to use `operator.` and `operator.*` with it. Which I don't like.
> 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?
In general, forgetting about the breakage for a moment, I'd like `this`
to be a reference. But there is one use case where it makes more sense
to have it as a pointer: `delete this`. Yes, you could take an address,
but that is an additional complication, especially when you override
`operator&`. Also, personally I consider dangling references worse than
dangling pointers because in general we assume references are valid
barring bugs. Having `this` behave both as a reference and a pointer
kind of solves it, although it creates one more piece of the "weird
stuff" in the language.
BTW, the implicit conversion of function pointers is most likely out of
the necessity for compatibility with C. In C++ we got member functions,
and taking address explicitly is mandatory for those. Personally, I
don't like the implicit conversion as I believe it hides the intent of
the code while not saving much. It is possible that others, including
the committee, feels that way.
Received on 2020-03-05 03:27:37