Given class X
below (special member functions other than the one explicitly defined are not relevant for this experiment):
struct X
{
X() { }
X(int) { }
X(X const&) { std::cout << "X(X const&)" << std::endl; }
X(X&&) { std::cout << "X(X&&)" << std::endl; }
};
The following program creates a vector of objects of type X
and resizes it so that its capacity is exceeded and reallocation is forced:
#include <iostream>
#include <vector>
int main()
{
std::vector<X> v(5);
v.resize(v.capacity() + 1);
}
Since class X
provides a move constructor, I would expect the previous content of the vector to be moved into the new storage after reallocation. Quite surprisingly, that does not seem to be the case, and the output I get is:
X(X const&)
X(X const&)
X(X const&)
X(X const&)
X(X const&)
Why?