Consider the following code:
#include <iostream>
#include <string>
struct my_struct {
void func(std::string&& str) {
str_ = str;
}
std::string str_;
};
int main() {
my_struct s;
std::string str("Hello");
s.func(std::move(str));
std::cout << str << std::endl;
std::cout << s.str_ << std::endl;
}
Why do I need an extra std::move
in my_struct::func
in order to invoke the move assignment operator of std::string
? What would the extra std::move
exactly do? I thought it would just cast the given type to its rvalue reference counterpart?