Consider that I have a pseudo code like
std::vector< Something > myVector;
while (someCondition) {
// Populate vector
functionThatNeedsCopyOfVector(myVector);
myVector.clear()
};
The key point is that this snippet fulfills the pattern of "reusing" the same vector object multiple times where it always fills it up with a few elements and then passes a copy of the vector somewhere, before clearing it and starting all over again.
Thinking about this I though that in principle it should be possible to optimize this snippet to
std::vector< Something > myVector;
while (someCondition) {
// Populate vector
functionThatNeedsCopyOfVector(std::move(myVector));
};
(Assuming there is an overload of functionThatNeedsCopyOfVector
that accepts rvalue references instead)
However this would clearly be a case of use-after-move which is UB (per se). However I was wondering whether the standard might dictate how vector's move constructor behaves exactly and if that would for instance contain something that would make the original vector equal to a freshly constructed vector (because the internal data structure was moved)?
If there was such a guarantee, then the "move-snippet" should not be UB, right?
And in case it is indeed UB: Is there a pattern that could be used instead that avoids the (seemingly) unneeded copy when one is to clear it afterwards anyways?