Reputation: 3032
I'm bit confused why the std::move(std::string)
not making the passed std::string
argument to an empty state (I mean std::string
size as 0 and its internal buffer to point to nullptr
after call to std::move(std::string)
). This is a sample code
#include <iostream>
#include <string>
void print(std::string& str) {
std::cout << "lref\n";
std::cout << str << "\n" << std::endl;
}
void print(const std::string& str) {
std::cout << "const lref\n";
std::cout << str << "\n" << std::endl;
}
void print(std::string&& str) {
std::cout << "rref\n";
std::cout << str << "\n" << std::endl;
}
int main() {
std::string str_a = "Hello, ";
std::string str_b = "world!";
print(str_a);
print(str_b);
print(str_a + str_b);
print(std::move(str_a));
print(str_a); // was expecting str_a to be empty but still prints Hello,
return 0;
}
Upvotes: 2
Views: 767
Reputation: 32233
std::move
is not moving anything. It cast its argument to an rvalue-reference
. To actually move you need to pass str
as rvalue-reference
to move constructor(or move assignment) of another string as below.
std::string new_str(std::move(str));
print(str);
Upvotes: 5