Is it recommended to std::move a string into containers that is going to be overwritten?

Viewed 267

I have the following code

std::vector<std::string> lines;
std::string currentLine;

while(std::getline(std::cin, currentLine)) {
  // // option 1
  // lines.push_back(std::move(currentLine));

  // // option 2
  // lines.push_back(currentLine);
}

I see different costs for the two

  1. The first approach will clear currentLine, making the getline need to allocate a new buffer for the string. But it will use the buffer for the vector instead.

  2. The second approach will make getline be able to reuse the buffer, and require a new buffer allocation for the in-vector string.

In such situations, is there a "better" way? Can the compiler optimize the one or other approach more efficiently? Or are there clever string implementations that make one option way more performant than the other?

1 Answers
Related