Topic: std::vector::resize and std::vector::insert (was std::vector::resize)
Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Mon, 22 Dec 2003 06:22:39 +0000 (UTC) Raw View
John Potter wrote:
> On Sun, 21 Dec 2003 05:11:46 +0000 (UTC), kanze@alex.gabi-soft.fr (James
> Kanze) wrote:
>>What if I write something like:
>> v.insert( v.end(), v.begin(), v.end() ) ;
>>?
> You violate the preconditions of table 67. I think that is called
> undefined behavior.
good point, but what about
v.insert( v.end(), 100, v[0] );
?
Notice that this overload of insert() takes a const reference.
What strikes me is the difference between the signatures of that
overload of insert() and resize(). That's even more curious as resize()
is defined by standard in terms of insert()!
This raises another issue: as it is defined, resize() always makes a
copy of the value even in case when it's not necessary, i.e.: if the
vector doesn't need to grow. So, a better solution, in my opinion, would
be to reverse the signatures, that is, instead of:
void insert(iterator position, size_type n, const T& x);
void resize(size_type sz, T c = T());
define
void insert(iterator position, size_type n, T x);
void resize(size_type sz, const T& c = T());
This would remove the objection on insert() *and* on resize() as the
latter is implemented in terms of the former. As a plus, it will avoid
the unnecessary copy in resize().
Of course, we may also accept James' objection and simply require the
implementation to behave correctly in this case. In this case resize()
could be safely defined taking a const reference, as there would be no
longer any reason to require the extra copy.
Is this issue worth a DR?
Alberto
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: Tue, 23 Dec 2003 02:06:17 +0000 (UTC) Raw View
On Mon, 22 Dec 2003 06:22:39 +0000 (UTC), AlbertoBarbati@libero.it
(Alberto Barbati) wrote:
> John Potter wrote:
> > On Sun, 21 Dec 2003 05:11:46 +0000 (UTC), kanze@alex.gabi-soft.fr (James
> > Kanze) wrote:
> >>What if I write something like:
> >> v.insert( v.end(), v.begin(), v.end() ) ;
> >>?
> > You violate the preconditions of table 67. I think that is called
> > undefined behavior.
> good point, but what about
> v.insert( v.end(), 100, v[0] );
Isn't one enough?
v.insert( v.end(), v[0] );
same as:
v.insert( v.end(), *v.begin() );
same as:
v.insert( v.end(), v.begin(), v.begin() + 1 );
which is undefined behavior.
> Notice that this overload of insert() takes a const reference.
> What strikes me is the difference between the signatures of that
> overload of insert() and resize(). That's even more curious as resize()
> is defined by standard in terms of insert()!
Maybe that's why it takes a value parameter? Note that there is no
restriction on the iterators to assign(pos, first, last) until you
check the effects which are in terms of insert which does restrict
the iterators.
> Of course, we may also accept James' objection and simply require the
> implementation to behave correctly in this case. In this case resize()
> could be safely defined taking a const reference, as there would be no
> longer any reason to require the extra copy.
> Is this issue worth a DR?
It would make the intent clear at least. OTOH are these any more than
silly exercises that would never exist in production code? I guess that
an implementer would care though.
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]