If I have an existing non-trivial variable and I want to re-assign it with new contents that I will declare on the same line as the assignment, should I use move semantics?
My question comes from the following scenario:
std::vector<string> existing = { ... };
int main(int argc, char *argv[]){
const char *bunch_of_strings = ... ;
std::stringstream ss(bunch_of_string);
existing = std::move(std::vector<std::string>(std::istream_iterator<std::string>(ss), {}));
}
should I be doing this, will the compiler optimize it similarly if I don't, or is it just better not to?
std::move is redundant there. The purpose of move is to treat a variable as a temporary (more accurately, an rvalue) when it's not (or might not be). If it's already an rvalue, it will certainly be moved if possible anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With