@@ -364,7 +364,38 @@ template<typename T, typename Alloc>
364364{
365365 if (n > capacity ())
366366 {
367- allocate_and_copy (n, begin (), end (), m_storage);
367+ // compute the new capacity after the allocation
368+ size_type new_capacity = n;
369+
370+ // do not exceed maximum storage
371+ new_capacity = thrust::min THRUST_PREVENT_MACRO_SUBSTITUTION <size_type>(new_capacity, max_size ());
372+
373+ // create new storage
374+ storage_type new_storage (copy_allocator_t (), m_storage, new_capacity);
375+
376+ // record how many constructors we invoke in the try block below
377+ iterator new_end = new_storage.begin ();
378+
379+ try
380+ {
381+ // construct copy all elements into the newly allocated storage
382+ new_end = m_storage.uninitialized_copy (begin (), end (), new_storage.begin ());
383+ } // end try
384+ catch (...)
385+ {
386+ // something went wrong, so destroy & deallocate the new storage
387+ new_storage.destroy (new_storage.begin (), new_end);
388+ new_storage.deallocate ();
389+
390+ // rethrow
391+ throw ;
392+ } // end catch
393+
394+ // call destructors on the elements in the old storage
395+ m_storage.destroy (begin (), end ());
396+
397+ // record the vector's new state
398+ m_storage.swap (new_storage);
368399 } // end if
369400} // end vector_base::reserve()
370401
@@ -877,13 +908,13 @@ template<typename T, typename Alloc>
877908 new_end = m_storage.uninitialized_copy (begin (), end (), new_storage.begin ());
878909
879910 // construct new elements to insert
880- m_storage .default_construct_n (new_end, n);
911+ new_storage .default_construct_n (new_end, n);
881912 new_end += n;
882913 } // end try
883914 catch (...)
884915 {
885916 // something went wrong, so destroy & deallocate the new storage
886- m_storage .destroy (new_storage.begin (), new_end);
917+ new_storage .destroy (new_storage.begin (), new_end);
887918 new_storage.deallocate ();
888919
889920 // rethrow
0 commit comments