C++11 standard requirements on move constructor for std::vector
are (Table 99 — Allocator-aware container requirements):
X(rv)
X u(rv)
- move construction of allocator shall not exit via an exception
- post: u shall have the same elements as
rv
had before this construction; the value of get_allocator()
shall be the same as the value of rv.get_allocator()
before this construction.
- complexity: constant
Here is no requirements/guarantee on capacity. But we can make conclusion that constant complexity implicitly denies any reallocations. And I cannot see any other logical reason to change capacity except reallocation. So it shall be the same.
From the other point of view if the moved-from vector is empty, it is perfectly legal to just ignore it and default-construct itself. This would still be O(1), as it doesn't require any per-element constructs. (Thanks to Nicol Bolas for this issue).
Also implemenation possibly could shrink capacity to the size using hint
parameter of std::allocator::allocate
function:
pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
The use of hint
is unspecified, but intended as an aid to locality if an implementation so desires. So some sofisticated solution possibly could pass vector storage pointer as hint
and use realloc
on it to shrink capacity.
Conclusion: looks like standard doesn't guarantee capacity preserving on moving std::vector
, storage potentially could be shrinked.