This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of CD1 status.
Section: 184.108.40.206 [vector.capacity], 220.127.116.11 [vector.modifiers] Status: CD1 Submitter: Anthony Williams Opened: 2001-07-13 Last modified: 2017-02-03
Priority: Not Prioritized
View all other issues in [vector.capacity].
View all issues with CD1 status.
There is an apparent contradiction about which circumstances can cause a reallocation of a vector in Section 18.104.22.168 [vector.capacity] and section 22.214.171.124 [vector.modifiers].
126.96.36.199 [vector.capacity],p5 says:
Notes: Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence. It is guaranteed that no reallocation takes place during insertions that happen after a call to reserve() until the time when an insertion would make the size of the vector greater than the size specified in the most recent call to reserve().
Which implies if I do
std::vector<int> vec; vec.reserve(23); vec.reserve(0); vec.insert(vec.end(),1);
then the implementation may reallocate the vector for the insert, as the size specified in the previous call to reserve was zero.
However, the previous paragraphs (188.8.131.52 [vector.capacity], p1-2) state:
(capacity) Returns: The total number of elements the vector can hold without requiring reallocation
...After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens; and equal to the previous value of capacity() otherwise...
This implies that vec.capacity() is still 23, and so the insert() should not require a reallocation, as vec.size() is 0. This is backed up by 184.108.40.206 [vector.modifiers], p1:
(insert) Notes: Causes reallocation if the new size is greater than the old capacity.
Though this doesn't rule out reallocation if the new size is less than the old capacity, I think the intent is clear.
Change the wording of 220.127.116.11 [vector.capacity] paragraph 5 to:
Notes: Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence. It is guaranteed that no reallocation takes place during insertions that happen after a call to reserve() until the time when an insertion would make the size of the vector greater than the value of capacity().
[Redmond: original proposed resolution was modified slightly. In the original, the guarantee was that there would be no reallocation until the size would be greater than the value of capacity() after the most recent call to reserve(). The LWG did not believe that the "after the most recent call to reserve()" added any useful information.]
There was general agreement that, when reserve() is called twice in succession and the argument to the second invocation is smaller than the argument to the first, the intent was for the second invocation to have no effect. Wording implying that such cases have an effect on reallocation guarantees was inadvertant.