@@ -1250,30 +1250,29 @@ vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _Inpu
12501250 difference_type __off = __position - begin ();
12511251 pointer __p = this ->__begin_ + __off;
12521252 pointer __old_last = this ->__end_ ;
1253- for (; this ->__end_ != this ->__cap_ && __first != __last; ++__first) {
1253+ for (; this ->__end_ != this ->__cap_ && __first != __last; ++__first)
12541254 __construct_one_at_end (*__first);
1255+
1256+ if (__first == __last)
1257+ (void )std::rotate (__p, __old_last, this ->__end_ );
1258+ else {
1259+ __split_buffer<value_type, allocator_type&> __v (__alloc_);
1260+ auto __guard = std::__make_exception_guard (
1261+ _AllocatorDestroyRangeReverse<allocator_type, pointer>(__alloc_, __old_last, this ->__end_ ));
1262+ __v.__construct_at_end_with_sentinel (std::move (__first), std::move (__last));
1263+ __split_buffer<value_type, allocator_type&> __merged (__recommend (size () + __v.size ()), __off, __alloc_);
1264+ std::__uninitialized_allocator_relocate (
1265+ __alloc_, std::__to_address (__old_last), std::__to_address (this ->__end_ ), std::__to_address (__merged.__end_ ));
1266+ __guard.__complete (); // Release the guard once objects in [__old_last_, __end_) have been successfully relocated.
1267+ __merged.__end_ += this ->__end_ - __old_last;
1268+ this ->__end_ = __old_last;
1269+ std::__uninitialized_allocator_relocate (
1270+ __alloc_, std::__to_address (__v.__begin_ ), std::__to_address (__v.__end_ ), std::__to_address (__merged.__end_ ));
1271+ __merged.__end_ += __v.size ();
1272+ __v.__end_ = __v.__begin_ ;
1273+ __p = __swap_out_circular_buffer (__merged, __p);
12551274 }
1256- __split_buffer<value_type, allocator_type&> __v (this ->__alloc_ );
1257- if (__first != __last) {
1258- #if _LIBCPP_HAS_EXCEPTIONS
1259- try {
1260- #endif // _LIBCPP_HAS_EXCEPTIONS
1261- __v.__construct_at_end_with_sentinel (std::move (__first), std::move (__last));
1262- difference_type __old_size = __old_last - this ->__begin_ ;
1263- difference_type __old_p = __p - this ->__begin_ ;
1264- reserve (__recommend (size () + __v.size ()));
1265- __p = this ->__begin_ + __old_p;
1266- __old_last = this ->__begin_ + __old_size;
1267- #if _LIBCPP_HAS_EXCEPTIONS
1268- } catch (...) {
1269- erase (__make_iter (__old_last), end ());
1270- throw ;
1271- }
1272- #endif // _LIBCPP_HAS_EXCEPTIONS
1273- }
1274- __p = std::rotate (__p, __old_last, this ->__end_ );
1275- insert (__make_iter (__p), std::make_move_iterator (__v.begin ()), std::make_move_iterator (__v.end ()));
1276- return begin () + __off;
1275+ return __make_iter (__p);
12771276}
12781277
12791278template <class _Tp , class _Allocator >
0 commit comments