std::operator+(std::basic_string)
| Definiert in Header <string> |
||
template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(1) | (constexpr seit C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(2) | (constexpr seit C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(3) | (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> |
(4) | (seit C++26) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(5) | (constexpr seit C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(6) | (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> |
(7) | (seit C++26) |
template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(8) | (seit C++11) (constexpr seit C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(9) | (seit C++11) (constexpr seit C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(10) | (seit C++11) (constexpr seit C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(11) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> |
(12) | (seit C++26) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(13) | (seit C++11) (constexpr seit C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(14) | (seit C++11) (constexpr seit C++20) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(15) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> |
(16) | (seit C++26) |
Gibt einen String zurück, der die Zeichen von lhs gefolgt von den Zeichen von rhs enthält. Äquivalent zu
|
Der für das Ergebnis verwendete Allokator ist 1-4) std::allocator_traits<Alloc>::select_on_container_copy_construction(lhs.get_allocator())
5-7) std::allocator_traits<Alloc>::select_on_container_copy_construction(rhs.get_allocator())
8-12) lhs.get_allocator()
13-16) rhs.get_allocator()
Mit anderen Worten
In jedem Fall wird der linke Operand bevorzugt, wenn beide Für (8-16) bleiben alle rvalue |
(seit C++11) |
Inhalt |
[bearbeiten] Parameter
| lhs | - | String, String-Ansicht(seit C++26), Zeichen oder Zeiger auf das erste Zeichen eines null-terminierten Arrays |
| rhs | - | String, String-Ansicht(seit C++26), Zeichen oder Zeiger auf das erste Zeichen eines null-terminierten Arrays |
[bearbeiten] Rückgabewert
Ein String, der die Zeichen von lhs gefolgt von den Zeichen von rhs enthält, unter Verwendung des wie oben ermittelten Allokators(seit C++11).
Anmerkungen
Da der von den Ergebnissen von 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 Für eine Kette von // use my_favorite_allocator for the final result my_string(my_favorite_allocator) + meow + woof + cat() + dog(); Für eine bessere und portable Kontrolle über Allokatoren sollten Memberfunktionen wie |
(seit C++11) |
|
Die Verwendung von std::type_identity_t als Parameter in den Überladungen (4), (7), (12) und (16) stellt sicher, dass ein Objekt vom Typ std::basic_string<CharT, Traits, Allocator> gemäß den Regeln der Überladungsauflösung immer an ein Objekt eines Typs
|
(seit C++26) |
[bearbeiten] Beispiel
#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 }
Ausgabe
Hello world! Hello Waterworld!
[bearbeiten] Fehlerberichte
Die folgenden Verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | angewendet auf | Verhalten wie veröffentlicht | Korrigiertes Verhalten |
|---|---|---|---|
| P1165R1 | C++11 | Allokator-Propagierung ist laienhaft und inkonsistent | konsistenter gemacht |
[bearbeiten] Siehe auch
| hängt Zeichen am Ende an (public member function) | |
| hängt Zeichen am Ende an (public member function) | |
| fügt Zeichen ein (public member function) |