std::operator+(std::basic_string) - cppreference.com
| Defined in header |
||
|
(1) | (constexpr since C++20) |
|
|
(2) | (constexpr since C++20) |
|
|
(3) | (constexpr since C++20) |
|
(4) | (since C++26) |
|
|
(5) | (constexpr since C++20) |
|
|
(6) | (constexpr since C++20) |
|
(7) | (since C++26) |
|
(8) | (since C++11) (constexpr since C++20) |
|
|
(9) | (since C++11) (constexpr since C++20) |
|
|
(10) | (since C++11) (constexpr since C++20) |
|
|
(11) | (since C++11) (constexpr since C++20) |
|
(12) | (since C++26) |
|
|
(13) | (since C++11) (constexpr since C++20) |
|
|
(14) | (since C++11) (constexpr since C++20) |
|
|
(15) | (since C++11) (constexpr since C++20) |
|
(16) | (since C++26) |
Returns a string containing characters from lhs followed by the characters from rhs. Equivalent to:
1,2) std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;
3) std::basic_string<CharT, Traits, Allocator> r = lhs; r.push_back(rhs); return r;
4) std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;
5) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;
6) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(r.begin(), lhs); return r;
7) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;
8) lhs.append(rhs); return std::move(lhs); except that both lhs and rhs are left in valid but unspecified states. If lhs and rhs have equal allocators, the implementation can move from either.
9,10) lhs.append(rhs); return std::move(lhs);
11) lhs.push_back(rhs); return std::move(lhs);
12) lhs.append(rhs); return std::move(lhs);
13,14) rhs.insert(0, lhs); return std::move(rhs);
15) rhs.insert(rhs.begin(), lhs); return std::move(rhs);
16) rhs.insert(0, lhs); return std::move(rhs);
|
The allocator used for the result is: 1-4) 5-7) 8-12) 13-16) In other words:
In each case, the left operand is preferred when both are For (8-16), all rvalue |
(since C++11) |
Parameters
| lhs | - | string, string view(since C++26), character, or pointer to the first character in a null-terminated array |
| rhs | - | string, string view(since C++26), character, or pointer to the first character in a null-terminated array |
Return value
A string containing characters from lhs followed by the characters from rhs, using the allocator determined as above(since C++11).
Notes
Because the allocator used by the result of using my_string = std::basic_string<char, std::char_traits<char>, my_allocator<char>>; my_string cat(); const my_string& dog(); my_string meow = /* ... */, woof = /* ... */; meow + cat() + /* ... */; // uses select_on_container_copy_construction on meow's allocator woof + dog() + /* ... */; // uses allocator of dog()'s return value instead meow + woof + meow; // uses select_on_container_copy_construction on meow's allocator meow + (woof + meow); // uses SOCCC on woof's allocator instead For a chain of // use my_favorite_allocator for the final result my_string(my_favorite_allocator) + meow + woof + cat() + dog(); For better and portable control over allocators, member functions like |
(since C++11) |
|
The usage of
|
(since C++26) |
Example
#include <iostream> #include <string> #include <string_view> int main() { std::string s1 = "Hello"; std::string s2 = "world"; const char* end = "!\n"; std::cout << s1 + ' ' + s2 + end; std::string_view water{" Water"}; #if __cpp_lib_string_view >= 202403 std::cout << s1 + water + s2 << end; // overload (4), then (1) #else std::cout << s1 + std::string(water) + s2 << end; // OK, but less efficient #endif }
Output:
Hello world! Hello Waterworld!
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P1165R1 | C++11 | allocator propagation is haphazard and inconsistent | made more consistent |