std::string str1 = "hello";
std::string &&str2 = std::move(str1);
std::string str3(str2);
std::cout << str1 << std::endl;
I think at line 3 that str3 will steal str1, because std::string(std::string &&str) (or sth. like that) will be invoked. But the result is:
test_move()
hello
test_move()
I want to know why the content str1 is still there after line 3.
std::string str1 = "hello";
std::string &&str2 = std::move(str1);
std::string str3(str2);
std::cout << str1 << std::endl;
std::string str4(std::move(str1));
std::cout << str1 << std::endl;
I try to move str1 directly to str4, and the result is:
test_move()
hello
test_move()
So I can't figure out the difference between the situation of str4 and str3. In my eyes they are all the same, all takes a rvalue ref as parameter, but move semantics takes place only on str4.Why?