C++ Logo


Advanced search

Re: lvalue ref-qualified this-pointers in the standard library

From: Peter C++ <peter.cpp_at_[hidden]>
Date: Wed, 28 Jul 2021 19:04:12 +0200
actually what you propose was in the motivation of the paper introducing ref-qualification of member functions.

I started to teach to lvalue-ref qualify user-declared assignment operators.

but changing the standard library and the language might be too much of a change with not too much of a benefit that it would be worthwhile attempting. (I have thought proposing it)


sent from a mobile device so please excuse strange words due to autocorrection.
Peter Sommerlad
+41-79-432 23 32

> On 28 Jul 2021, at 18:34, Paul Raffer via Std-Proposals <std-proposals_at_[hidden]> wrote:
> Another proposal idea:
> I think in most setters (and other methods that modify an object like operator=) in the standard library (e.g. the setters real and imag in the complex class) the this-pointer should be lvalue ref-qualified, so that temporary objects can't be modyfied.
> The signature of real should look like this
> constexpr void real(T value) &;
> instead of this
> constexpr void real(T value);
> In C++20 code like this
> std::complex<double>{}.real(42.0);
> or this
> std::complex<double>{} = std::complex<double>{};
> would compile, but makes no sense.
> That could potentially break existing code, but only code that has bugs anyway. So that's a feature not a bug. Or am I wrong?
> Paul
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2021-07-28 12:04:18