CKTIS / C++
std::basic_string::operator+= — appending to a string
std::basic_string::operator+= appends characters to an existing string and returns a reference to it. It accepts another string, a single character, a null-terminated string, an initializer list, or a string-view-compatible type.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
constexpr basic_string& operator+=(const basic_string& str);
template<class T> constexpr basic_string& operator+=(const T& t);
constexpr basic_string& operator+=(const charT* s);
constexpr basic_string& operator+=(charT c);
constexpr basic_string& operator+=(std::initializer_list<charT> il);std::basic_string::operator+= modifies its left operand by adding content at the end. For std::string, charT is char; include <string>. The result has type basic_string& and refers to the same object, allowing expressions such as (text += "a") += "b".
The five overloads accept const basic_string&, const charT*, charT, std::initializer_list<charT>, and const T&. The last participates in overload resolution only when const T& is convertible to std::basic_string_view<charT, traits> but not to const charT*. The operations correspond to append calls; the character overload appends exactly one character.
The pointer overload requires a valid null-terminated string and stops copying before the first null character; do not pass a null pointer. String and view overloads preserve embedded null characters through their explicit lengths. To append a buffer with a known size, use append(data, count).
Appending may allocate storage and invalidate references, pointers, and iterators to elements. Exceeding max_size() throws std::length_error; if the operation throws, the string object remains unchanged. The supplied sources present the evolving standard draft, not a frozen C++23 text.
Examples
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string text = "C";
text += std::string("++");
text += ' ';
text += "is";
text += std::string_view(" useful");
(text += {'!', '!'}) += '\n';
std::cout << text;
}
Result
C++ is useful!!
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.