When having two instances of std::vector
with a primitive data type, having same size and capacity, is there a guarantee that copying via the copy assignment operator will not re-allocate the target vector?
Example:
const int n = 3;
std::vector<int> a, b;
// ensure defined capacity
a.reserve(n);
b.reserve(n);
// make same size
a.resize(n);
b.resize(n);
// set some values for a
for (int i = 0; i < n; i++)
{
a[i] = i;
}
// copy a to b: allocation free?
b = a;
I've only found "Otherwise, the memory owned by *this may be reused when possible." (since C++11) on cppreference.com. I was hoping for a "must" instead of "may".
If there should be a positive answer for a more general case such as "same size is enough", even better.
If there should be no guarantee, this case could be an answer to Copying std::vector: prefer assignment or std::copy?, when std::copy
would be preferred.