C++ Logo

std-proposals

Advanced search

lvalue ref-qualified this-pointers in the standard library

From: Paul Raffer <paul.raffer_at_[hidden]>
Date: Wed, 28 Jul 2021 16:33:45 +0000
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

Received on 2021-07-28 11:33:50