Index: include/string =================================================================== --- include/string +++ include/string @@ -960,7 +960,7 @@ void resize(size_type __n, value_type __c); _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());} - void reserve(size_type __res_arg); + _LIBCPP_INLINE_VISIBILITY void reserve(size_type __res_arg); _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n); _LIBCPP_INLINE_VISIBILITY @@ -3136,6 +3136,7 @@ } template +inline void basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg) { @@ -3143,6 +3144,10 @@ this->__throw_length_error(); size_type __cap = capacity(); size_type __sz = size(); +#if _LIBCPP_STD_VER > 17 + if (__res_arg <= __cap) + return; +#endif __res_arg = _VSTD::max(__res_arg, __sz); __res_arg = __recommend(__res_arg); if (__res_arg != __cap) Index: test/std/strings/basic.string/string.capacity/reserve.pass.cpp =================================================================== --- test/std/strings/basic.string/string.capacity/reserve.pass.cpp +++ test/std/strings/basic.string/string.capacity/reserve.pass.cpp @@ -30,6 +30,10 @@ assert(s == s0); assert(s.capacity() <= old_cap); assert(s.capacity() >= s.size()); +#if TEST_STD_VER > 17 + // reserve never shrinks as of P0966 + assert(s.capacity() == old_cap); +#endif } template @@ -46,7 +50,9 @@ assert(s.capacity() >= res_arg); assert(s.capacity() >= s.size()); #if TEST_STD_VER > 17 - assert(s.capacity() >= old_cap); // resize never shrinks as of P0966 + // reserve never shrinks as of P0966 + if (res_arg <= old_cap) + assert(s.capacity() == old_cap); #endif } #ifndef TEST_HAS_NO_EXCEPTIONS @@ -97,6 +103,24 @@ test(s, 1000); test(s, S::npos); } + { + S s; + test(s, 50); + test(s, 10); + test(s, 5); + } +#if TEST_STD_VER > 17 + { + S s; + s.reserve(100); + typename S::size_type old_cap = s.capacity(); + s.reserve(5); + // reserve never shrinks as of P0966 + assert(s.capacity() == old_cap); + s.reserve(old_cap - 1); + assert(s.capacity() == old_cap); + } +#endif } #if TEST_STD_VER >= 11 { @@ -129,6 +153,24 @@ test(s, 1000); test(s, S::npos); } + { + S s; + test(s, 50); + test(s, 10); + test(s, 5); + } +#if TEST_STD_VER > 17 + { + S s; + s.reserve(100); + typename S::size_type old_cap = s.capacity(); + s.reserve(5); + // reserve never shrinks as of P0966 + assert(s.capacity() == old_cap); + s.reserve(old_cap - 1); + assert(s.capacity() == old_cap); + } +#endif } #endif }