This program tries to move a string out of a function and use it for the construction of another string:
#include
#include
#i
That Get_String function binds an rvalue reference to a function-local object. Rvalue references are useful for things that are about to be destroyed, but just as bad as lvalue references for things that have already been destroyed.
To move a local object out of a function, you just return by class type:
std::string Get_String(void) {
std::string str{"hello world"};
return str;
}
If the compiler doesn't manage to eliminate the copy/move entirely, then the return value that the caller gets will be constructed using a move constructor, not a copy constructor, as long as the return expression is:
str above), orstd::move(something)(So you could still have return std::move(str); to be explicit, but it's not necessary here.)