std::operator+(std::basic_string)
| Definiert im Header <string>
|
||
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs );
|
(1) | (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
const CharT* rhs );
|
(2) | (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
CharT rhs );
|
(3) | (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
constexpr std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
std::type_identity_t<std::basic_string_view<CharT,Traits>> rhs );
|
(4) | (seit C++26) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const CharT* lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs );
|
(5) | (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( CharT lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs );
|
(6) | (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
constexpr std::basic_string<CharT,Traits,Alloc>
operator+( std::type_identity_t<std::basic_string_view<CharT,Traits>> lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs );
|
(7) | (seit C++26) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(8) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
const std::basic_string<CharT,Traits,Alloc>& rhs );
|
(9) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
const CharT* rhs );
|
(10) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
CharT rhs );
|
(11) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
constexpr std::basic_string<CharT,Traits,Alloc>
operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,
std::type_identity_t<std::basic_string_view<CharT,Traits>> rhs );
|
(12) | (seit C++26) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(13) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( const CharT* lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(14) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
std::basic_string<CharT,Traits,Alloc>
operator+( CharT lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(15) | (seit C++11) (constexpr seit C++20) |
template< class CharT, class Traits, class Alloc >
constexpr std::basic_string<CharT,Traits,Alloc>
operator+( std::type_identity_t<std::basic_string_view<CharT,Traits>> lhs,
std::basic_string<CharT,Traits,Alloc>&& rhs );
|
(16) | (seit C++26) |
Gibt eine Zeichenkette zurück, die Zeichen aus lhs gefolgt von den Zeichen aus rhs enthält. Äquivalent zu:
std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;std::basic_string<CharT, Traits, Allocator> r = lhs; r.push_back(rhs); return r;std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(r.begin(), lhs); return r;std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;lhs.append(rhs); return std::move(lhs); außer dass sowohl lhs als auch rhs in einem gültigen, aber nicht spezifizierten Zustand belassen werden. Wenn lhs und rhs gleiche Allokatoren haben, kann die Implementierung von einem von beiden verschieben.lhs.append(rhs); return std::move(lhs);lhs.push_back(rhs); return std::move(lhs);lhs.append(rhs); return std::move(lhs);rhs.insert(0, lhs); return std::move(rhs);rhs.insert(rhs.begin(), lhs); return std::move(rhs);rhs.insert(0, lhs); return std::move(rhs);|
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) werden alle rvalue |
(seit C++11) |
Parameter
| lhs | - | string , string view (since C++26) , Zeichen oder Zeiger auf das erste Zeichen in einer nullterminierten Zeichenfolge |
| rhs | - | string , string view (since C++26) , Zeichen oder Zeiger auf das erste Zeichen in einer nullterminierten Zeichenfolge |
Rückgabewert
Ein String, der die Zeichen aus lhs gefolgt von den Zeichen aus rhs , unter Verwendung des wie oben bestimmten Allokators (since C++11) , enthält.
Hinweise
Da der vom Ergebnis 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 Steuerung von Allokatoren sollten Member-Funktionen wie
|
(seit C++11) |
|
Die Verwendung von
std::
type_identity_t
als Parameter in Überladungen
(
4
)
,
(
7
)
,
(
12
)
und
(
16
)
stellt sicher, dass ein Objekt vom Typ
std::
basic_string
<
CharT, Traits, Allocator
>
stets mit einem Objekt eines Typs
|
(seit C++26) |
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; // Überladung (4), dann (1) #else std::cout << s1 + std::string(water) + s2 << end; // OK, aber weniger effizient #endif }
Ausgabe:
Hello world! Hello Waterworld!
Fehlerberichte
Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | Angewendet auf | Verhalten wie veröffentlicht | Korrektes Verhalten |
|---|---|---|---|
| P1165R1 | C++11 | Allokator-Propagation ist planlos und inkonsistent | konsistenter gestaltet |
Siehe auch
|
Fügt Zeichen am Ende an
(öffentliche Elementfunktion) |
|
|
Fügt Zeichen am Ende an
(öffentliche Elementfunktion) |
|
|
Fügt Zeichen ein
(öffentliche Elementfunktion) |