I have a std::vector<std::string>
to be re-used in a loop. Is it ok to std::move
elements out? If I moved the ith element out, then ith slot goes into an undefined but valid state, but what about the vector? Is its state still defined and valid? Also, can I clear()
and then reuse the vector in the next iteration?
EDIT: please read the question before you flag for duplication. I'm asking the state of v2
after doing std::move(v2[0])
, not std::move(v2)
. Also I reuse v2
after I did v2.clear()
. How is that similar to the suggested duplication?
EDIT: code example:
struct Foo {
string data;
/* other data memebers */
void workOnData();
}
std::vector<std::string> buffer;
Foo foo;
while (1) {
buffer.clear();
loadData(buffer); // push data to buffer, at least one element in buffer guaranteed
foo.data.assign(std::move(buffer[0])); // please don't ask is Foo necessary or why workOnData has to be a member method. THIS IS A SIMPLIFIED EXAMPLE!
foo.workOnData();
}