Garmaine Staff asked 2 years ago

I recently learned that member functions can be ref-qualified, which allows me to write

struct S {
    S& operator=(S const&) & // can only be used if the implicit object is an lvalue
      return *this; 

S operator+(S const &, S const &) { 
  return {}; 

thereby preventing users from doing things like

S s{};
s + s = S{}; // error

However, I see that std::string's member operator= does not do this. So the following code compiles with no warnings

std::string s;
s + s = s;

Is there a reason for allowing this?

If not, would it be possible to add the ref-qualifier in the future, or would that break existing code somehow?