C++ Logo

std-proposals

Advanced search

Re: [std-proposals] this return type

From: Jan Schultke <janschultke_at_[hidden]>
Date: Sat, 8 Apr 2023 00:37:34 +0200
> IIUC, your example intends that the `this` keyword would be used both in `Base` and also in `B`, is that right?
> https://www.godbolt.org/z/xWdcaoxE6
Yes, that is correct, the Compiler Explorer link is what I envision.

(1) decltype(Derived().f()) would be Derived&
It is already permitted to return a reference to the derived type when
overriding, and I don't see any issues with returning the derived type
when possible. However, if Derived didn't override the function,
result would be Base&, otherwise the type would depend on the
arguments, which would break ABI when upgrading from returning Base&
to this. The compiler would still know that it is a Derived& under the
hood and optimizations would be possible.

(2) Yes. Keep in mind that returning this is exactly equivalent to
returning a reference to the self-type, with qualifications applied.
Without the extra feature of making return statements implicit, this
proposal could be implemented via [[returns_this]] annotation (where
the program is IFNDR if we don't return a self-reference).

(3) You could allow it, but there are no obvious benefits here. It
would be better to make this ill-formed.

(4) The program would be ill-formed if a return statement in a
this-returning function is not of the form return *this;
However, this approach is *very* fishy if the function is
rvalue-reference qualified. It would require ignoring that the
returned reference cannot normally bind to the return type of the
function.

(5) Yes. This would be ergonomic for developers who prefer to always
use trailing return types.

(6) No, this leads to parsing issues as mentioned, and the use case
would be extremely uncommon.
this return types acting as mostly an annotation and not producing
distinct types seems better.

(7) Good catch, I haven't thought about this case much yet. I will
need to look into it further.

(8) I also think it's unlikely that this results in developer mistakes
due to the edit distance.
But what I fear is that developers get accustomed to implicit return
statements and forget them more frequently when downgrading to an
older standard.


Thanks a lot for your feedback

Received on 2023-04-07 22:37:47