Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ref-qualifiers for the assignment operator of standard library types

I was wondering, is there a reason the assignment operator of standard types is not lvalue ref-qualified? None of them are.

Because of that, we can write things such as this:

std::string{} = "42";
std::string s = "hello " + std::string{"world"} = "oops!";

std::vector<int> v = { 1,2,3 };
std::move(v) = { 4,5,6 };

If the assignment operator was lvalue ref-qualified all of these examples would not compile.

Is it because there's a lot of things to modify (but then so it was for noexcept) and nobody wrote a proposal for? I don't think people write code like this but shouldn't the library be designed so that it doesn't even allow it?

like image 207
Marius Bancila Avatar asked Oct 16 '25 22:10

Marius Bancila


1 Answers

Your suggestion was proposed in 2009, and ultimately rejected in Frankfurt that year over "concerns about backwards compatibility".

It would have been a breaking change, and we don't like those.

The existing prohibition against assigning to rvalues of built-in types is only of limited real value anyway, so the cost of potentially breaking existing code was almost certainly deemed to be "not worth it".

Would the library be designed in this manner if we had a clean slate? Perhaps.

like image 140
Lightness Races in Orbit Avatar answered Oct 19 '25 13:10

Lightness Races in Orbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!