Skip to content

Commit 8a7318e

Browse files
authored
[libc++] Refactor vector::push_back to use vector::emplace (#113481)
This removes some duplicate code. I suspect this was originally written that way because vector::emplace didn't exist in C++03 mode, which stopped being relevant when Clang implemented rvalue references in C++03.
1 parent f3131c9 commit 8a7318e

File tree

1 file changed

+2
-43
lines changed

1 file changed

+2
-43
lines changed

libcxx/include/vector

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,9 @@ public:
689689
return std::__to_address(this->__begin_);
690690
}
691691

692-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
692+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x) { emplace_back(__x); }
693693

694-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
694+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x) { emplace_back(std::move(__x)); }
695695

696696
template <class... _Args>
697697
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
@@ -898,9 +898,6 @@ private:
898898
__annotate_shrink(__old_size);
899899
}
900900

901-
template <class _Up>
902-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __push_back_slow_path(_Up&& __x);
903-
904901
template <class... _Args>
905902
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __emplace_back_slow_path(_Args&&... __args);
906903

@@ -1491,44 +1488,6 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::shrink_to_fit() _NOE
14911488
}
14921489
}
14931490

1494-
template <class _Tp, class _Allocator>
1495-
template <class _Up>
1496-
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
1497-
vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x) {
1498-
allocator_type& __a = this->__alloc();
1499-
__split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
1500-
// __v.push_back(std::forward<_Up>(__x));
1501-
__alloc_traits::construct(__a, std::__to_address(__v.__end_), std::forward<_Up>(__x));
1502-
__v.__end_++;
1503-
__swap_out_circular_buffer(__v);
1504-
return this->__end_;
1505-
}
1506-
1507-
template <class _Tp, class _Allocator>
1508-
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
1509-
vector<_Tp, _Allocator>::push_back(const_reference __x) {
1510-
pointer __end = this->__end_;
1511-
if (__end < this->__end_cap()) {
1512-
__construct_one_at_end(__x);
1513-
++__end;
1514-
} else {
1515-
__end = __push_back_slow_path(__x);
1516-
}
1517-
this->__end_ = __end;
1518-
}
1519-
1520-
template <class _Tp, class _Allocator>
1521-
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void vector<_Tp, _Allocator>::push_back(value_type&& __x) {
1522-
pointer __end = this->__end_;
1523-
if (__end < this->__end_cap()) {
1524-
__construct_one_at_end(std::move(__x));
1525-
++__end;
1526-
} else {
1527-
__end = __push_back_slow_path(std::move(__x));
1528-
}
1529-
this->__end_ = __end;
1530-
}
1531-
15321491
template <class _Tp, class _Allocator>
15331492
template <class... _Args>
15341493
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer

0 commit comments

Comments
 (0)