Skip to content

Commit 89af9e2

Browse files
committed
Fix shrink_to_fit for vector<bool>
1 parent 79e859e commit 89af9e2

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,13 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::reserve(size_type _
841841

842842
template <class _Allocator>
843843
_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT {
844-
if (__external_cap_to_internal(size()) > __cap_) {
844+
if (__external_cap_to_internal(size()) < __cap_) {
845845
#if _LIBCPP_HAS_EXCEPTIONS
846846
try {
847847
#endif // _LIBCPP_HAS_EXCEPTIONS
848-
vector(*this, allocator_type(__alloc_)).swap(*this);
848+
vector __v(*this, allocator_type(__alloc_));
849+
if (__v.__cap_ < __cap_)
850+
__v.swap(*this);
849851
#if _LIBCPP_HAS_EXCEPTIONS
850852
} catch (...) {
851853
}

0 commit comments

Comments
 (0)