Title
vector::insert invalidates end()?
Status
nad
Section
[vector.modifiers]
Submitter
Marc Glisse

Created on 2014-10-21.00:00:00 last changed 94 months ago

Messages

Date: 2017-03-15.00:00:00

[ 2017-03-04, Kona ]

NAD. The problem description is incorrect; it is a violation of table entry 87 a.insert(p, i, j) - "i and j are not iterators into a".

Date: 2014-10-21.00:00:00

this issue is based on the discussion here.

[vector.modifiers] says about vector::insert: "If no reallocation happens, all the iterators and references before the insertion point remain valid." This doesn't seem to guarantee anything about the iterator at the point of insertion.

The question comes from people asking if the following is valid, assuming a sufficient call to reserve() was done first:

v.insert(v.end(), v.begin(), v.end());

It could fail for an implementation using a sentinel for the end of the vector, but I don't know of any (it would be quite inconvenient). And for any implementation using a simple position as iterator (pointer (possibly in a wrapper), or base+offset), this is needlessly restrictive. The fact that this alternative:

v.insert(v.end(), &v[0], &v[0]+v.size())

is arguably valid (again assuming a large enough reserve()) makes it a bit confusing that the first version isn't ([sequence.reqmts] has a precondition that iterator arguments to insert() do not point into the sequence, but vector::insert is more refined and seems to give enough guarantees that it cannot fail).

Then we might as well say that vector iterators act as positions, and that after a reallocation-free operation an iterator points to the same position, whatever may be there now…

History
Date User Action Args
2017-03-14 03:14:09adminsetmessages: + msg9102
2017-03-14 03:14:09adminsetstatus: new -> nad
2014-10-21 00:00:00admincreate