From a0ae09085825ac362c2e94a368b3c386cf8fb02b Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Wed, 23 Oct 2024 12:43:39 -0400 Subject: [PATCH] [libc++] Refactor vector::push_back to use vector::emplace 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. --- libcxx/include/vector | 45 ++----------------------------------------- 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/libcxx/include/vector b/libcxx/include/vector index bfbf1ea1cfc9f..ce412a829ea8f 100644 --- a/libcxx/include/vector +++ b/libcxx/include/vector @@ -689,9 +689,9 @@ public: return std::__to_address(this->__begin_); } - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x); + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x) { emplace_back(__x); } - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x); + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x) { emplace_back(std::move(__x)); } template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI @@ -898,9 +898,6 @@ private: __annotate_shrink(__old_size); } - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __push_back_slow_path(_Up&& __x); - template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __emplace_back_slow_path(_Args&&... __args); @@ -1491,44 +1488,6 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::shrink_to_fit() _NOE } } -template -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer -vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x) { - allocator_type& __a = this->__alloc(); - __split_buffer __v(__recommend(size() + 1), size(), __a); - // __v.push_back(std::forward<_Up>(__x)); - __alloc_traits::construct(__a, std::__to_address(__v.__end_), std::forward<_Up>(__x)); - __v.__end_++; - __swap_out_circular_buffer(__v); - return this->__end_; -} - -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void -vector<_Tp, _Allocator>::push_back(const_reference __x) { - pointer __end = this->__end_; - if (__end < this->__end_cap()) { - __construct_one_at_end(__x); - ++__end; - } else { - __end = __push_back_slow_path(__x); - } - this->__end_ = __end; -} - -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void vector<_Tp, _Allocator>::push_back(value_type&& __x) { - pointer __end = this->__end_; - if (__end < this->__end_cap()) { - __construct_one_at_end(std::move(__x)); - ++__end; - } else { - __end = __push_back_slow_path(std::move(__x)); - } - this->__end_ = __end; -} - template template _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer