Reputation: 38173
As the title says:
Does the standard guarantee, that std::string::resize will not do anything, if the new size is the same as the old one?
Of course, I can test it, but it could be UB, so testing will not work here.
In cppreference.com and cplusplus.com, the doc says nothing about this case
Example:
std::string s( "asd" );
// s.size() is 3
s.resize( 3 ); // will this affect **somehow** the string?
Upvotes: 1
Views: 413
Reputation: 29966
ISO/IEC 14882:2011(E) § 21.4.4:
void resize(size_type n, charT c);
Requires: n <= max_size()
Throws: length_error if n > max_size().
Effects: Alters the length of the string designated by *this as follows:
Upvotes: 1
Reputation: 92271
No, there are no guarantees. An implementation could reallocate the string to a new buffer, to reduce its capacity()
.
The standard says:
Effects: Alters the length of the string designated by
*this
as follows:— If
n <= size()
, the function replaces the string designated by*this
with a string of length n whose elements are a copy of the initial elements of the original string designated by*this
.— If
n > size()
, the function replaces the string designated by*this
with a string of length n whose firstsize()
elements are a copy of the original string designated by*this
, and whose remaining elements are all initialized to c.
Upvotes: 4