0

I am trying to come to terms with r-value references. I discovered that string's operator+ is always declared to return by value.

Why is operator+ defined to return by value:

basic_string operator+(basic_string&& lhs, const basic_string& rhs) {
   return move( lhs.append(rhs) );
}

Why is it not defined like the following:

basic_string&& operator+(basic_string&& lhs, const basic_string& rhs) {
   return move( lhs.append(rhs) );
}

Is this somehow invalid code?

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Shriram V
  • 1,500
  • 2
  • 11
  • 20
  • Because that would be much riskier? `const std::string & r = std::string("a") + std::string("b");` – Kerrek SB Nov 23 '15 at 13:35
  • From [this discussion](http://stackoverflow.com/questions/9963974/are-returned-locals-automatically-xvalues) I understand that the compilers are free to consider the return value as an rvalue anyway. – Peter - Reinstate Monica Nov 23 '15 at 13:36
  • @PeterA.Schneider: Nobody disputes that the return value should be an rvalue. The question is why it is a prvalue and not an xvalue. – Kerrek SB Nov 23 '15 at 14:20

0 Answers0