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 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).

>   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.

-- 
Carlo Wood <carlo@alinoe.com>


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