This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: About std::vector::resize().


On Thu, 2004-05-27 at 06:03, Carlo Wood wrote:
> On Wed, May 26, 2004 at 08:40:51PM +0530, Dhruv Matani wrote:
> > This should do the trick.
> > 
> > It's not exception safe, and stuff, but then again, how many times does
> > our default constructed vector throw ;-)
> 
> I am afraid this is incorrect, the swap is used on the _elements_
> of a vector - and therefore has arbitrary type.  You cannot assume
> it has a method 'swap' (or assume the default constructor doesn't throw).

This is not the full version, because your patch will have to be
patched, and the functions will have to be specialized for vector<T>,
list<T>, map<T>, etc... where we may assume that.

For vector, list, etc... we are guaranteed.
1). O(1) swap member does exist.
2). Default ctor will not throw (only for vector). However, for node
based containers with dummy nodes, like map, list, etc... they may.

> 
> >   template<typename _InputIterator, typename _ForwardIterator>
> >     inline _ForwardIterator
> >     __uninitialized_copy_swap(_InputIterator __first, _InputIterator
> > 	__last, _ForwardIterator __result)
> >     {
> >       _ForwardIterator __cur = __result;
> >       typename 
> > 	std::iterator_traits<_InputIterator>::value_type 
> > 	__def_const;
> > 
> >       for ( ; __first != __last; ++__first, ++__cur)
> >       {
> >         std::_Construct(&*__cur, __def_const);
> > 	__cur->swap(*__first);
> >       }
> >       return __cur;
> >     }
> 
> You can savely use std::swap(*__cur, *__first);
> though.  In the case of vectors that will be equivalent.
-- 
        -Dhruv Matani.
http://www.geocities.com/dhruvbird/

Proud to be a Vegetarian.
http://www.vegetarianstarterkit.com/
http://www.vegkids.com/vegkids/index.html



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]