Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P0966R1 string::reserve() should not shrink #176

Merged
merged 2 commits into from Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions stl/inc/xstring
Expand Up @@ -3616,6 +3616,25 @@ public:
return _Mypair._Myval2._Myres;
}

#if _HAS_CXX20
void reserve(_CRT_GUARDOVERFLOW const size_type _Newcap) { // determine new minimum length of allocated storage
if (_Mypair._Myval2._Myres < _Newcap) { // reallocate to grow
NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
const size_type _Old_size = _Mypair._Myval2._Mysize;
_Reallocate_grow_by(
_Newcap - _Old_size, [](_Elem* const _New_ptr, const _Elem* const _Old_ptr, const size_type _Old_size) {
_Traits::copy(_New_ptr, _Old_ptr, _Old_size + 1);
});

_Mypair._Myval2._Mysize = _Old_size;
return;
}

// ignore requests to reserve to [0, _Myres]
}

NathanSWard marked this conversation as resolved.
Show resolved Hide resolved

_CXX20_DEPRECATE_STRING_RESERVE_DEFAULT_ARGUMENT void reserve() {} // no-op
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
#else // _HAS_CXX20
void reserve(_CRT_GUARDOVERFLOW const size_type _Newcap = 0) { // determine new minimum length of allocated storage
if (_Mypair._Myval2._Mysize > _Newcap) { // requested capacity is not large enough for current size, ignore
return; // nothing to do
Expand Down Expand Up @@ -3644,6 +3663,7 @@ public:

// ignore requests to reserve to [_BUF_SIZE, _Myres)
}
#endif // _HAS_CXX20

_NODISCARD bool empty() const noexcept {
return size() == 0;
Expand Down
16 changes: 15 additions & 1 deletion stl/inc/yvals_core.h
Expand Up @@ -799,7 +799,21 @@
#define _DEPRECATE_STDEXT_HASH_UPPER_BOUND
#endif // ^^^ warning disabled ^^^

NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
// next warning number: STL4024
// P0966R1 [depr.string.capacity]
#if _HAS_CXX20 && !defined(_SILENCE_CXX20_STRING_RESERVE_DEFAULT_ARGUMENT_DEPRECATION_WARNING) \
NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
&& !defined(_SILENCE_ALL_CXX20_DEPRECATION_WARNINGS)
#define _CXX20_DEPRECATE_STRING_RESERVE_DEFAULT_ARGUMENT \
[[deprecated("warning STL4024: " \
"std::string::reserve() with the default arument 0 is deprecated in C++20. " \
NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
"If the intention is to shrink the string's capacity, use std::string::shrink_to_fit(). Otherwise, "\
NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
"provide an argument to std::string::reserve(). " \
"You can define _SILENCE_CXX20_STRING_RESERVE_DEFAULT_ARGUMENT_DEPRECATION_WARNING" \
NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
"or _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS to acknowledge that you have received this warning.")]]
#else // ^^^ warning enabled / warning disabled vvv
#define _CXX20_DEPRECATE_STRING_RESERVE_DEFAULT_ARGUMENT
#endif // ^^^ warning disabled ^^^

// next warning number: STL4025


// LIBRARY FEATURE-TEST MACROS
Expand Down