There are many missed opportunities in C ++ move semantics. I would like to understand the rationale for these reasons and why the standard is not more aggressive in determining when a variable should be moved in the following cases:
string f() { string s; return s + " "; }
This calls operator+(const string&, const char*) , not operator+(string&&, const char*) , I believe because s is an lvalue. Could the standard say that with the last use of a local variable in a function, the variable should be considered movable?
I think a somewhat similar example:
struct A { A(string&&); }; string g() { string s; return s;
g uses move semantics to move data from s to the return value, but h does not compile because s does not move to h . I believe this is because the standard has a special case for g , which says that if you return a local variable of the same type as the return type, the variable moves. Why is it not a rule that if you return a local variable, it moves regardless of its type?
source share