std::operator+(std::basic_string)
Определён в заголовочном файле <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 с 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 с 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 с 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) | (с 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 с 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 с 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) | (с 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) |
(с C++11) (constexpr с 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) | (с C++11) (constexpr с 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) | (с C++11) (constexpr с 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) | (с C++11) (constexpr с 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) | (с 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) | (с C++11) (constexpr с 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) | (с C++11) (constexpr с 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) | (с C++11) (constexpr с 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) | (с C++26) |
Возвращает строку, содержащую символы из lhs, за которыми следуют символы из rhs. Эквивалентно:
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);, за исключением того, что оба объекта lhs и rhs остаются в допустимых, но неуточнённых состояниях. Если lhs и rhs имеют равные аллокаторы, реализация может переместить любой из них.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);
| Для результата используется следующий аллокатор: 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() Иными словами:
В каждом случае предпочтение отдаётся левому операнду, если оба операнда являются Для (8-16) все rvalue-операнды | (с C++11) |
Параметры
| lhs | - | строка, строковое представление(с C++26), символ или указатель на первый символ массива с завершающим нулём |
| rhs | - | строка, строковое представление(с C++26), символ или указатель на первый символ массива с завершающим нулём |
Возвращаемое значение
Строка, содержащая символы из lhs, за которыми следуют символы из rhs, с использованием определённого выше аллокатора(с C++11).
Примечания
Поскольку аллокатор, используемый результатом 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 Для цепочки вызовов // use my_favorite_allocator for the final result my_string(my_favorite_allocator) + meow + woof + cat() + dog(); Для более точного и переносимого управления аллокаторами следует использовать функции-члены, такие как | (с C++11) |
| Использование std::type_identity_t в качестве параметра перегрузок (4), (7), (12) и (16) гарантирует, что объект типа std::basic_string<CharT,Traits,Allocator> всегда можно объединить с объектом типа
| (с C++26) |
Пример
#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
}
Вывод:
Hello world! Hello Waterworld!
Отчёты об ошибках
Следующие отчёты об ошибках, изменяющие поведение, были применены ретроактивно к ранее опубликованным стандартам C++.
| DR | Применяется к | Поведение согласно опубликованной версии | Исправленное поведение |
|---|---|---|---|
| P1165R1 | C++11 | распространение аллокатора является непоследовательным и непредсказуемым | поведение стало более последовательным |
См. также
| добавляет символы в конец (открытая функция-член) |
|
| добавляет символы в конец (открытая функция-член) |
|
| вставляет символы (открытая функция-член) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/string/basic_string/operator%2B