std::basic_string<CharT,Traits,Allocator>::resize_and_overwrite
| template< class Operation > constexpr void resize_and_overwrite( size_type count, Operation op ); |
(since C++23) | |
Resizes the string to contain at most count characters, using the user-provided operation op to modify the possibly indeterminate contents and set the length.
This function performs following steps:
- Obtains a contiguous storage that contains count + 1 characters, and makes its first
kcharacter equal to the firstkcharacters of *this, wherekis the smaller ofcountand the result of this->size() before the call toresize_and_overwrite. Letpdenote the pointer to the first character in the storage.- The equality is determined as if by checking this->compare(0, k, p, k) == 0.
- The characters in
[p + k, p + count]may have indeterminate values.
- Evaluates std::move(op)(p, count). Let
rbe the return value of std::move(op)(p, count). - Replaces the contents of *this with
[p, p + r)(which sets the length of *this tor). Invalidates all pointers and references to the range[p, p + count].
The program is ill-formed if r does not have an integer-like type. The behavior is undefined if std::move(op)(p, count) throws an exception or modifies p or count, r is not in the range [0, count], or any character in range [p, p + r) has an indeterminate value.
Implementations are recommended to avoid unnecessary copies and allocations by, e.g., making p equal to the pointer to beginning of storage of characters allocated for *this after the call, which can be identical to the existing storage of *this if count is less than or equal to this->capacity().
Parameters
| count | - | the maximal possible new size of the string |
| op | - | the function object used for setting the new contents of the string |
Return value
(none)
Exceptions
std::length_error if count > max_size().
Any exceptions thrown by corresponding Allocator.
If an exception is thrown from std::move(op)(p, count), the behavior is undefined. Otherwise, if an exception is thrown, this function has no effect.
Notes
resize_and_overwrite invalidates all iterators, pointers, and references into *this, regardless whether reallocation occurs. Implementations may assume that the contents of the string are not aliased after the call to resize_and_overwrite.
Example
| This section is incomplete Reason: no example |
See also
| changes the number of characters stored (public member function) | |
| (C++14)(C++20) |
creates a unique pointer that manages a new object (function template) |
| creates a shared pointer that manages a new object (function template) | |
| creates a shared pointer that manages a new object allocated using an allocator (function template) |