[libcxx-commits] [libcxx] [libc++] Don't pass the sentinel to vector::__{insert, init}_with_size (PR #224583)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Sep 18 02:55:53 PDT 2026
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/224583
>From 9157af9fe1877d100ede1f895764d57836d01e5f Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Fri, 18 Sep 2026 11:48:44 +0200
Subject: [PATCH] [libc++] Don't pass the sentinel to
vector::__{insert,init}_with_size
---
libcxx/include/__vector/vector.h | 64 ++++++++++++++------------------
1 file changed, 27 insertions(+), 37 deletions(-)
diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index d37a0c21ebcd5d..a93b890e90de1d 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -209,8 +209,7 @@ class _LIBCPP_WARN_UNUSED vector {
is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
int> = 0>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_ForwardIterator __first, _ForwardIterator __last) {
- size_type __n = static_cast<size_type>(std::distance(__first, __last));
- __init_with_size(__first, __last, __n);
+ __init_with_size(__first, std::distance(__first, __last));
}
template <
@@ -221,8 +220,7 @@ class _LIBCPP_WARN_UNUSED vector {
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a)
: __layout_(__a) {
- size_type __n = static_cast<size_type>(std::distance(__first, __last));
- __init_with_size(__first, __last, __n);
+ __init_with_size(__first, std::distance(__first, __last));
}
#if _LIBCPP_STD_VER >= 23
@@ -230,9 +228,7 @@ class _LIBCPP_WARN_UNUSED vector {
_LIBCPP_HIDE_FROM_ABI constexpr vector(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
: __layout_(__a) {
if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
- auto __n = static_cast<size_type>(ranges::distance(__range));
- __init_with_size(ranges::begin(__range), ranges::end(__range), __n);
-
+ __init_with_size(ranges::begin(__range), ranges::distance(__range));
} else {
__init_with_sentinel(ranges::begin(__range), ranges::end(__range));
}
@@ -263,12 +259,12 @@ class _LIBCPP_WARN_UNUSED vector {
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(const vector& __x)
: __layout_(__alloc_traits::select_on_container_copy_construction(__x.__layout_.__alloc())) {
- __init_with_size(__x.__layout_.__begin_ptr(), __x.__layout_.__end_ptr(), __x.size());
+ __init_with_size(__x.__layout_.__begin_ptr(), __x.size());
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
: __layout_(__a) {
- __init_with_size(__x.__layout_.__begin_ptr(), __x.__layout_.__end_ptr(), __x.size());
+ __init_with_size(__x.__layout_.__begin_ptr(), __x.size());
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(const vector& __other) {
@@ -291,13 +287,13 @@ class _LIBCPP_WARN_UNUSED vector {
#ifndef _LIBCPP_CXX03_LANG
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(initializer_list<value_type> __il) {
- __init_with_size(__il.begin(), __il.end(), __il.size());
+ __init_with_size(__il.begin(), __il.size());
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
vector(initializer_list<value_type> __il, const allocator_type& __a)
: __layout_(__a) {
- __init_with_size(__il.begin(), __il.end(), __il.size());
+ __init_with_size(__il.begin(), __il.size());
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(initializer_list<value_type> __il) {
@@ -515,7 +511,7 @@ class _LIBCPP_WARN_UNUSED vector {
if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
auto __len = ranges::distance(__range);
if (__len <= static_cast<difference_type>(__layout_.__remaining_capacity())) {
- __construct_at_end(ranges::begin(__range), ranges::end(__range), __len);
+ __construct_at_end(ranges::begin(__range), __len);
} else {
_SplitBuffer __buffer(__recommend(size() + __len), size(), __layout_.__alloc());
__buffer.__construct_at_end_with_size(ranges::begin(__range), __len);
@@ -565,7 +561,7 @@ class _LIBCPP_WARN_UNUSED vector {
int> = 0>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) {
- return __insert_with_size<_ClassicAlgPolicy>(__position, __first, __last, std::distance(__first, __last));
+ return __insert_with_size<_ClassicAlgPolicy>(__position, __first, std::distance(__first, __last));
}
#if _LIBCPP_STD_VER >= 23
@@ -573,8 +569,7 @@ class _LIBCPP_WARN_UNUSED vector {
_LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
auto __n = static_cast<size_type>(ranges::distance(__range));
- return __insert_with_size<_RangeAlgPolicy>(__position, ranges::begin(__range), ranges::end(__range), __n);
-
+ return __insert_with_size<_RangeAlgPolicy>(__position, ranges::begin(__range), __n);
} else {
return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
}
@@ -679,14 +674,13 @@ class _LIBCPP_WARN_UNUSED vector {
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
- template <class _InputIterator, class _Sentinel>
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
- __init_with_size(_InputIterator __first, _Sentinel __last, size_type __n) {
+ template <class _InputIterator>
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __init_with_size(_InputIterator __first, size_type __n) {
auto __guard = std::__make_exception_guard(__destroy_vector(*this));
if (__n > 0) {
__vallocate(__n);
- __construct_at_end(std::move(__first), std::move(__last), __n);
+ __construct_at_end(std::move(__first), __n);
}
__guard.__complete();
@@ -736,13 +730,12 @@ class _LIBCPP_WARN_UNUSED vector {
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
- template <class _AlgPolicy, class _Iterator, class _Sentinel>
+ template <class _AlgPolicy, class _Iterator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
- __insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n);
+ __insert_with_size(const_iterator __position, _Iterator __first, difference_type __n);
- template <class _InputIterator, class _Sentinel>
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
- __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
+ template <class _InputIterator>
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(_InputIterator __first, size_type __n);
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator __make_iter(pointer __p) _NOEXCEPT {
#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
@@ -925,9 +918,8 @@ vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
}
template <class _Tp, class _Allocator>
-template <class _InputIterator, class _Sentinel>
-_LIBCPP_CONSTEXPR_SINCE_CXX20 void
-vector<_Tp, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel, size_type __n) {
+template <class _InputIterator>
+_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(_InputIterator __first, size_type __n) {
_ConstructTransaction __tx(*this, __n);
__tx.__pos_ = std::__uninitialized_allocator_copy_n(this->__layout_.__alloc(), std::move(__first), __n, __tx.__pos_);
}
@@ -949,8 +941,7 @@ vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_
if (__a == __x.__layout_.__alloc()) {
__layout_.__move_assign_without_allocator(__x.__layout_);
} else {
- typedef move_iterator<iterator> _Ip;
- __init_with_size(_Ip(__x.begin()), _Ip(__x.end()), __x.size());
+ __init_with_size(move_iterator<iterator>(__x.begin()), __x.size());
}
}
@@ -973,13 +964,13 @@ vector<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __l
template <class _Tp, class _Allocator>
template <class _AlgPolicy, class _Iterator, class _Sentinel>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
-vector<_Tp, _Allocator>::__assign_with_size(_Iterator __first, _Sentinel __last, difference_type __n) {
+vector<_Tp, _Allocator>::__assign_with_size(_Iterator __first, _Sentinel, difference_type __n) {
size_type __new_size = static_cast<size_type>(__n);
if (__new_size <= capacity()) {
auto const __size = size();
if (__new_size > __size) {
auto __mid = std::__copy_n<_AlgPolicy>(std::move(__first), __size, this->__layout_.__begin_ptr()).__in_;
- __construct_at_end(std::move(__mid), std::move(__last), __new_size - __size);
+ __construct_at_end(std::move(__mid), __new_size - __size);
} else {
pointer __m = std::__copy_n<_AlgPolicy>(std::move(__first), __n, this->__layout_.__begin_ptr()).__out_;
this->__destruct_at_end(__m);
@@ -987,7 +978,7 @@ vector<_Tp, _Allocator>::__assign_with_size(_Iterator __first, _Sentinel __last,
} else {
__vdeallocate();
__vallocate(__recommend(__new_size));
- __construct_at_end(std::move(__first), std::move(__last), __new_size);
+ __construct_at_end(std::move(__first), __new_size);
}
}
@@ -1193,10 +1184,9 @@ vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _Inpu
}
template <class _Tp, class _Allocator>
-template <class _AlgPolicy, class _Iterator, class _Sentinel>
+template <class _AlgPolicy, class _Iterator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
-vector<_Tp, _Allocator>::__insert_with_size(
- const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n) {
+vector<_Tp, _Allocator>::__insert_with_size(const_iterator __position, _Iterator __first, difference_type __n) {
pointer __p = this->__layout_.__begin_ptr() + (__position - begin());
if (__n > 0) {
if (__n <= static_cast<difference_type>(__layout_.__remaining_capacity())) {
@@ -1206,13 +1196,13 @@ vector<_Tp, _Allocator>::__insert_with_size(
if (__n > __dx) {
#if _LIBCPP_STD_VER >= 23
if constexpr (!forward_iterator<_Iterator>) {
- __construct_at_end(std::move(__first), std::move(__last), __n);
+ __construct_at_end(std::move(__first), __n);
std::rotate(__p, __old_last, __end);
} else
#endif
{
_Iterator __m = std::next(__first, __dx);
- __construct_at_end(__m, __last, __n - __dx);
+ __construct_at_end(__m, __n - __dx);
if (__dx > 0) {
__move_range(__p, __old_last, __p + __n);
__insert_assign_n_unchecked<_AlgPolicy>(__first, __dx, __p);
More information about the libcxx-commits
mailing list