Skip to content

Commit 317e9e4

Browse files
committed
Simplfy emplace_back and __construct_at_end for vector
1 parent 2337789 commit 317e9e4

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

libcxx/include/__vector/vector.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,8 @@ vector<_Tp, _Allocator>::__recommend(size_type __new_size) const {
879879
template <class _Tp, class _Allocator>
880880
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(size_type __n) {
881881
_ConstructTransaction __tx(*this, __n);
882-
const_pointer __new_end = __tx.__new_end_;
883-
for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
884-
__alloc_traits::construct(this->__alloc_, std::__to_address(__pos));
882+
for (; __tx.__pos_ != __tx.__new_end_; ++__tx.__pos_) {
883+
__alloc_traits::construct(this->__alloc_, std::__to_address(__tx.__pos_));
885884
}
886885
}
887886

@@ -895,9 +894,8 @@ template <class _Tp, class _Allocator>
895894
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
896895
vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
897896
_ConstructTransaction __tx(*this, __n);
898-
const_pointer __new_end = __tx.__new_end_;
899-
for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
900-
__alloc_traits::construct(this->__alloc_, std::__to_address(__pos), __x);
897+
for (; __tx.__pos_ != __tx.__new_end_; ++__tx.__pos_) {
898+
__alloc_traits::construct(this->__alloc_, std::__to_address(__tx.__pos_), __x);
901899
}
902900
}
903901

@@ -1101,16 +1099,12 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 inline
11011099
void
11021100
#endif
11031101
vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
1104-
pointer __end = this->__end_;
1105-
if (__end < this->__cap_) {
1102+
if (this->__end_ < this->__cap_)
11061103
__construct_one_at_end(std::forward<_Args>(__args)...);
1107-
++__end;
1108-
} else {
1109-
__end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
1110-
}
1111-
this->__end_ = __end;
1104+
else
1105+
(void)__emplace_back_slow_path(std::forward<_Args>(__args)...);
11121106
#if _LIBCPP_STD_VER >= 17
1113-
return *(__end - 1);
1107+
return *(this->__end_ - 1);
11141108
#endif
11151109
}
11161110

0 commit comments

Comments
 (0)