The post here points out that std::string
's member operator=
is not lvalue ref-qualified. That allows us to write code such as this:
std::string() = "Hello";
The linked post asks why this is allowed. The answer is backwards compatibility:
Changing the definition of something in the standard library is not something to be undertaken lightly, as it could needlessly break existing code.
This is not unreasonable. Perhaps someone has written code such as
// Does useful stuff
// Does useful stuff
std::string{} = "Hello"; // Does nothing
// Does useful stuff
// Does useful stuff
by accident. Making std::string::operator=
lvalue ref-qualified would prevent this old code from compiling, even if the program performs exactly as the programmer wants it to.
In the linked post, the user asks for a use-case, but one is not given. That is the question of this post.
The question
Is there a line of code that both
- would not compile with
std::string::operator=
lvalue ref-qualified
and
- if deleted would change the behavior of the program it is located in?
Note
Of course, by line of code, I do not mean code such as
std::string{} = "Hello"; ++i;