i ran the code as shown and i am confused as to why a+b is a rvalue. From what i know, rvalue should only have a data or address and in such a case std::string should be a lvalue isn't it?
void foo(const std::string&& input){ std::cout<<"RVALUE "<<input<<std::endl; } void foo(const std::string& input){ std::cout<<"LVALUE "<<input<<std::endl; } int main(){ std::string a = "Tom"; std::string b = "Cruise"; std::string c = a + b; foo(a+b); foo(c); } OUTPUT
RVALUE TomCruise LVALUE TomCruise
a + bon the left hand side of an assignment statement? Would it make sense to say(a + b) = "anotherString";? No. The fully elaborated rules for what goes in which value category when are complex, but thinking about the left and right hand side of assignment statements is a good start.a + bcreates a temporary, which is then assigned tocor passed tofoo(). That temporary is an rvalueoperator=is also a member function, though special.)