< cpp‎ | string‎ | basic string
Member functions
Element access
Deduction guides (C++17)
Non-member functions
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversion
Helper classes
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:

  1. Obtains a contiguous storage that contains count + 1 characters, and makes its first k character equal to the first k characters of *this, where k is the smaller of count and the result of this->size() before the call to resize_and_overwrite. Let p denote 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.
  2. Evaluates std::move(op)(p, count). Let r be the return value of std::move(op)(p, count).
  3. Replaces the contents of *this with [p, p + r) (which sets the length of *this to r). 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().


[edit] Parameters

count - the maximal possible new size of the string
op - the function object used for setting the new contents of the string

[edit] Return value


[edit] 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.

[edit] 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.

[edit] Example

[edit] See also

changes the number of characters stored
(public member function) [edit]
creates a unique pointer that manages a new object
(function template) [edit]
creates a shared pointer that manages a new object
(function template) [edit]
creates a shared pointer that manages a new object allocated using an allocator
(function template) [edit]