c++stlassignment-operatorstdref-qualifier

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?


Solution

  • 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.