Created on 1999-06-28.00:00:00 last changed 161 months ago
[ The following was the C++98/03 rationale and does not necessarily apply to the proposed resolution in the C++0X time frame: ]
The LWG believes this was an intentional design decision and so is not a defect. It may be worth revisiting for the next standard.
Proposed resolution:
[sequence.reqmts] Table 83 change return type from void to iterator for the following rows:
Table 83 — Sequence container requirements (in addition to container) Expression Return type Assertion/note pre-/post-condition a.insert(p,n,t) voiditeratorInserts n copies of t before p. a.insert(p,i,j) voiditeratorEach iterator in the range [i,j) shall be dereferenced exactly once. pre: i and j are not iterators into a. Inserts copies of elements in [i, j) before p a.insert(p,il) voiditeratora.insert(p, il.begin(), il.end()).
Add after p6 [sequence.reqmts]:
-6- ...
The iterator returned from a.insert(p,n,t) points to the copy of the first element inserted into a, or p if n == 0.
The iterator returned from a.insert(p,i,j) points to the copy of the first element inserted into a, or p if i == j.
The iterator returned from a.insert(p,il) points to the copy of the first element inserted into a, or p if il is empty.
p2 [deque] Update class definition, change return type from void to iterator:
voiditerator insert(const_iterator position, size_type n, const T& x); template <class InputIterator>voiditerator insert(const_iterator position, InputIterator first, InputIterator last);voiditerator insert(const_iterator position, initializer_list<T>);
[deque.modifiers] change return type from void to iterator on following declarations:
voiditerator insert(const_iterator position, size_type n, const T& x); template <class InputIterator>voiditerator insert(const_iterator position, InputIterator first, InputIterator last);
Add the following (missing) declaration
iterator insert(const_iterator position, initializer_list<T>);
[forwardlist] Update class definition, change return type from void to iterator:
voiditerator insert_after(const_iterator position, initializer_list<T> il);voiditerator insert_after(const_iterator position, size_type n, const T& x); template <class InputIterator>voiditerator insert_after(const_iterator position, InputIterator first, InputIterator last);
p8 [forwardlist.modifiers] change return type from void to iterator:
voiditerator insert_after(const_iterator position, size_type n, const T& x);
Add paragraph:
Returns: position.
p10 [forwardlist.modifiers] change return type from void to iterator:
template <class InputIterator>voiditerator insert_after(const_iterator position, InputIterator first, InputIterator last);
Add paragraph:
Returns: position.
p12 [forwardlist.modifiers] change return type from void to iterator on following declarations:
voiditerator insert_after(const_iterator position, initializer_list<T> il);
change return type from void to iterator on following declarations:
p2 [list] Update class definition, change return type from void to iterator:
voiditerator insert(const_iterator position, size_type n, const T& x); template <class InputIterator>voiditerator insert(const_iterator position, InputIterator first, InputIterator last);voiditerator insert(const_iterator position, initializer_list<T>);
[list.modifiers] change return type from void to iterator on following declarations:
voiditerator insert(const_iterator position, size_type n, const T& x); template <class InputIterator>voiditerator insert(const_iterator position, InputIterator first, InputIterator last);
Add the following (missing) declaration
iterator insert(const_iterator position, initializer_list<T>);
p2 [vector]
Update class definition, change return type from void to iterator:
voiditerator insert(const_iterator position, T&& x);voiditerator insert(const_iterator position, size_type n, const T& x); template <class InputIterator>voiditerator insert(const_iterator position, InputIterator first, InputIterator last);voiditerator insert(const_iterator position, initializer_list<T>);
[vector.modifiers] change return type from void to iterator on following declarations:
voiditerator insert(const_iterator position, size_type n, const T& x); template <class InputIterator>voiditerator insert(const_iterator position, InputIterator first, InputIterator last);
Add the following (missing) declaration
iterator insert(const_iterator position, initializer_list<T>);
p1 [vector.bool] Update class definition, change return type from void to iterator:
voiditerator insert (const_iterator position, size_type n, const bool& x); template <class InputIterator>voiditerator insert(const_iterator position, InputIterator first, InputIterator last);voiditerator insert(const_iterator position, initializer_list<bool> il);
p5 [basic.string] Update class definition, change return type from void to iterator:
voiditerator insert(const_iterator p, size_type n, charT c); template<class InputIterator>voiditerator insert(const_iterator p, InputIterator first, InputIterator last);voiditerator insert(const_iterator p, initializer_list<charT>);
p13 [string.insert] change return type from void to iterator:
voiditerator insert(const_iterator p, size_type n, charT c);
Add paragraph:
Returns: an iterator which refers to the copy of the first inserted character, or p if n == 0.
p15 [string.insert] change return type from void to iterator:
template<class InputIterator>voiditerator insert(const_iterator p, InputIterator first, InputIterator last);
Add paragraph:
Returns: an iterator which refers to the copy of the first inserted character, or p if first == last.
p17 [string.insert] change return type from void to iterator:
voiditerator insert(const_iterator p, initializer_list<charT> il);
Add paragraph:
Returns: an iterator which refers to the copy of the first inserted character, or p if il is empty.
[ 2009-07 Frankfurt ]
Q: why isn't this change also proposed for associative containers?
A: The returned iterator wouldn't necessarily point to a contiguous range.
Moved to Ready.
[ Post Summit Alisdair adds: ]
In addition to the original rationale for C++03, this change also gives a consistent interface for all container insert operations i.e. they all return an iterator to the (first) inserted item.
Proposed wording provided.
[ Summit: ]
Reopened by Alisdair.
Suppose that c and c1 are sequential containers and i is an iterator that refers to an element of c. Then I can insert a copy of c1's elements into c ahead of element i by executing
c.insert(i, c1.begin(), c1.end());
If c is a vector, it is fairly easy for me to find out where the newly inserted elements are, even though i is now invalid:
size_t i_loc = i - c.begin(); c.insert(i, c1.begin(), c1.end());
and now the first inserted element is at c.begin()+i_loc and one
past the last is at c.begin()+i_loc+c1.size().
But what if c is a list? I can still find the location of one
past the last inserted element, because i is still valid.
To find the location of the first inserted element, though,
I must execute something like
for (size_t n = c1.size(); n; --n) --i;
because i is now no longer a random-access iterator.
Alternatively, I might write something like
bool first = i == c.begin(); list<T>::iterator j = i; if (!first) --j; c.insert(i, c1.begin(), c1.end()); if (first) j = c.begin(); else ++j;
which, although wretched, requires less overhead.
But I think the right solution is to change the definition of insert
so that instead of returning void, it returns an iterator that refers
to the first element inserted, if any, and otherwise is a copy of its
first argument.
History | |||
---|---|---|---|
Date | User | Action | Args |
2011-08-23 20:07:26 | admin | set | status: wp -> c++11 |
2010-10-21 18:28:33 | admin | set | messages: + msg1689 |
2010-10-21 18:28:33 | admin | set | messages: + msg1688 |
2010-10-21 18:28:33 | admin | set | messages: + msg1687 |
2010-10-21 18:28:33 | admin | set | messages: + msg1686 |
2010-10-21 18:28:33 | admin | set | messages: + msg1685 |
1999-06-28 00:00:00 | admin | create |