Say that I have this class
struct Test {
std::string a;
void setA(std::string&& input) {
a = input;
input = "";
}
}
Here I move the content of input
into a
and then I leave input
on a safe destructable state. This is the classic use of the move semantic where I can avoid copies.
Now say that I have this class
struct Test {
std::string a;
void setA(std::string&& input) {
DoSomeWork(input);
}
void DoSomeWork(std::string&& other) { /* ... */}
}
Is this still correct? or should I use DoSomeWork(std::move(input));
? I do not know if the move is required or not in this case.
Note. In case 1 I receive an rvalue reference as input and I use the classic approach.
void setA(std::string&& input) {
a = input; //input is an rvalue referece and I "transfer" its content into a
input = ""; //maybe useless but in some books (including c++ primer) I've seen that it's good practice to "reset" the moved-from object and leave it in a destructable state!
I understand that. What I cannot understand is:
void setA(std::string&& input) {
//recall that DoSomeWork accepts a std::string&&
DoSomeWork(input);
}
Here if I want to pass input
to the function and move it I do not know if std::move
is required. I already have an rvalue reference so is the move process automatic? or the std::move
call is required?
I hope that my question is clear. Rosanna
Aucun commentaire:
Enregistrer un commentaire