std::basic_string<CharT,Traits,Allocator>::resize_and_overwrite

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Operations
basic_string::resize_and_overwrite
(C++23)
Search
Constants
Deduction guides (C++17)
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversion
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
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().

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

See also

changes the number of characters stored
(public member function)
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)