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) | (C++20 起为 constexpr) |
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) | (C++20 起为 constexpr) |
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) | (C++20 起为 constexpr) |
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) | (C++20 起为 constexpr) |
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) | (C++20 起为 constexpr) |
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 起) (C++20 起为 constexpr) |
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 起) (C++20 起为 constexpr) |
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 起) (C++20 起为 constexpr) |
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 起) (C++20 起为 constexpr) |
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 起) (C++20 起为 constexpr) |
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 起) (C++20 起为 constexpr) |
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 起) (C++20 起为 constexpr) |
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) 将所有右值 |
(C++11 起) |
参数
| lhs | - | 字符串、字符串视图(C++26 起)、字符或指向空终止字符序列首字符的指针 |
| rhs | - | 字符串、字符串视图(C++26 起)、字符或指向空终止字符序列首字符的指针 |
返回值
含有来自 lhs 的字符后随来自 rhs 的字符的字符串,使用如上确定的分配器(C++11 起)。
注解涉及有状态分配器时(例如用 std::pmr::string 时)(C++17 起),应该谨慎使用
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() + /*...*/; // 使用 meow 的分配器上的 select_on_container_copy_construction
woof + dog() + /*...*/; // 转而使用 dog() 的返回值的分配器
meow + woof + meow; // 使用 meow 的分配器上的 SOCCC
meow + (woof + meow); // 转而使用 woof 的分配器上的 select_on_container_copy_construction
对于 // 令最终结果使用 my_favorite_allocator
my_string(my_favorite_allocator) + meow + woof + cat() + dog();
为了更好且可移植地对分配器进行控制,应该在以所欲分配器构造的结果字符串上,使用 |
(C++11 起) |
|
根据重载决议规则,使用
|
(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; // 重载 (4),然后重载 (1)
#else
std::cout << s1 + std::string(water) + s2 << end; // OK,但较低效
#endif
}
输出:
Hello world!
Hello Waterworld!
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| P1165R1 | C++11 | 分配器传播混乱且不一致 | 使之更为一致 |
参阅
| 后附字符到结尾 (公开成员函数) | |
| 后附字符到结尾 (公开成员函数) | |
| 插入字符 (公开成员函数) |