std::operator+(std::basic_string)
| ヘッダ <string> で定義 |
||
template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(1) | (C++20 以降 constexpr) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(2) | (C++20 以降 constexpr) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(3) | (C++20 以降 constexpr) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> |
(4) | (C++26以降) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(5) | (C++20 以降 constexpr) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(6) | (C++20 以降 constexpr) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> |
(7) | (C++26以降) |
template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(8) | (C++11以降) (C++20 以降 constexpr) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(9) | (C++11以降) (C++20 以降 constexpr) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(10) | (C++11以降) (C++20 以降 constexpr) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(11) | (C++11以降) (C++20 以降 constexpr) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> |
(12) | (C++26以降) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(13) | (C++11以降) (C++20 以降 constexpr) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(14) | (C++11以降) (C++20 以降 constexpr) |
| template< class CharT, class Traits, class Alloc > std::basic_string<CharT,Traits,Alloc> |
(15) | (C++11以降) (C++20 以降 constexpr) |
template< class CharT, class Traits, class Alloc > constexpr std::basic_string<CharT,Traits,Alloc> |
(16) | (C++26以降) |
lhs に続く 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以降)。
注釈ステートフルアロケータが関与する場合(例えば 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() + /* ... */; // 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以降) |
|
オーバーロード (4)、(7)、(12)、および (16) のパラメータとして std::type_identity_t を使用することで、オーバーロード解決の規則に従って、std::basic_string<CharT, Traits, Allocator> 型のオブジェクトを、std::basic_string_view<CharT, Traits> への暗黙の変換を持つ型
|
(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 | アロケータの伝播は行き当たりばったりで一貫性がない | より一貫性を持たせた |
[編集] 関連項目
| 末尾に文字を追加する (public member function) | |
| 末尾に文字を追加する (public member function) | |
| 文字を挿入する (public member function) |