@@ -1264,32 +1264,31 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Alloca
12641264vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last) {
12651265 difference_type __off = __position - begin();
12661266 pointer __p = this->__begin_ + __off;
1267- allocator_type& __a = this->__alloc() ;
1267+ allocator_type& __a = this->__alloc_ ;
12681268 pointer __old_last = this->__end_;
1269- for (; this->__end_ != this->__end_cap() && __first != __last; ++__first) {
1269+ for (; this->__end_ != this->__cap_ && __first != __last; ++__first)
12701270 __construct_one_at_end(*__first);
1271+
1272+ if (__first == __last)
1273+ (void)std::rotate(__p, __old_last, this->__end_);
1274+ else {
1275+ __split_buffer<value_type, allocator_type&> __v(__a);
1276+ auto __guard = std::__make_exception_guard(
1277+ _AllocatorDestroyRangeReverse<allocator_type, pointer>(__a, __old_last, this->__end_));
1278+ __v.__construct_at_end_with_sentinel(std::move(__first), std::move(__last));
1279+ __split_buffer<value_type, allocator_type&> __merged(__recommend(size() + __v.size()), __off, __a);
1280+ std::__uninitialized_allocator_relocate(
1281+ __a, std::__to_address(__old_last), std::__to_address(this->__end_), std::__to_address(__merged.__end_));
1282+ __guard.__complete(); // Release the guard once objects in [__old_last_, __end_) have been successfully relocated.
1283+ __merged.__end_ += this->__end_ - __old_last;
1284+ this->__end_ = __old_last;
1285+ std::__uninitialized_allocator_relocate(
1286+ __a, std::__to_address(__v.__begin_), std::__to_address(__v.__end_), std::__to_address(__merged.__end_));
1287+ __merged.__end_ += __v.size();
1288+ __v.__end_ = __v.__begin_;
1289+ __p = __swap_out_circular_buffer(__merged, __p);
12711290 }
1272- __split_buffer<value_type, allocator_type&> __v(__a);
1273- if (__first != __last) {
1274- #if _LIBCPP_HAS_EXCEPTIONS
1275- try {
1276- #endif // _LIBCPP_HAS_EXCEPTIONS
1277- __v.__construct_at_end_with_sentinel(std::move(__first), std::move(__last));
1278- difference_type __old_size = __old_last - this->__begin_;
1279- difference_type __old_p = __p - this->__begin_;
1280- reserve(__recommend(size() + __v.size()));
1281- __p = this->__begin_ + __old_p;
1282- __old_last = this->__begin_ + __old_size;
1283- #if _LIBCPP_HAS_EXCEPTIONS
1284- } catch (...) {
1285- erase(__make_iter(__old_last), end());
1286- throw;
1287- }
1288- #endif // _LIBCPP_HAS_EXCEPTIONS
1289- }
1290- __p = std::rotate(__p, __old_last, this->__end_);
1291- insert(__make_iter(__p), std::make_move_iterator(__v.begin()), std::make_move_iterator(__v.end()));
1292- return begin() + __off;
1291+ return __make_iter(__p);
12931292}
12941293
12951294template <class _Tp, class _Allocator>
0 commit comments