名前空間
変種
操作

std::operator+(std::basic_string)

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
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 の文字を含む文字列を返します。以下と等価です。

1,2) std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;
3) std::basic_string<CharT, Traits, Allocator> r = lhs; r.push_back(rhs); return r;
4) std::basic_string<CharT, Traits, Allocator> r = lhs; r.append(rhs); return r;
5) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;
6) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(r.begin(), lhs); return r;
7) std::basic_string<CharT, Traits, Allocator> r = rhs; r.insert(0, lhs); return r;
8) lhs.append(rhs); return std::move(lhs);。ただし、lhsrhs はどちらも有効だが未指定の状態になります。lhsrhs のアロケータが同じ場合、実装はどちらかからムーブできます。
9,10) lhs.append(rhs); return std::move(lhs);
11) lhs.push_back(rhs); return std::move(lhs);
12) lhs.append(rhs); return std::move(lhs);
13,14) rhs.insert(0, lhs); return std::move(rhs);
15) rhs.insert(rhs.begin(), lhs); return std::move(rhs);
16) 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()

言い換えれば

  • 一方のオペランドが basic_string rvalue の場合、そのアロケータが使用されます。
  • それ以外の場合、lvalue basic_string オペランドのアロケータに対して select_on_container_copy_construction が使用されます。

どちらも同じ値カテゴリの basic_string である場合、左オペランドが優先されます。

(8-16) の場合、すべての rvalue basic_string オペランドは有効だが未指定の状態になります。

(C++11以降)

目次

[編集] パラメータ

lhs - 文字列、文字列ビュー(C++26以降)、文字、またはヌル終端配列の最初の文字へのポインタ
rhs - 文字列、文字列ビュー(C++26以降)、文字、またはヌル終端配列の最初の文字へのポインタ

[編集] 戻り値

lhs に続く rhs の文字を含む文字列を返します。アロケータは上記のように決定されます(C++11以降)

注釈

ステートフルアロケータが関与する場合(例えば std::pmr::string が使用される場合など)(C++17以降)operator+ は細心の注意を払って使用する必要があります。P1165R1 以前は、結果に使用されるアロケータは歴史的経緯によって決定され、明確な理由なくオーバーロードによって異なる可能性がありました。さらに、(1-5) の場合、アロケータの伝播動作は主要な標準ライブラリ実装間で異なり、標準に示されている動作とも異なります。

operator+ の結果が使用するアロケータは値カテゴリに影響されるため、operator+ はアロケータの伝播に関して結合性を持っていません。

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

operator+ の呼び出しチェーンの場合、最終的な結果に使用されるアロケータは、目的のアロケータを持つ rvalue basic_string を前置することで制御できます。

// use my_favorite_allocator for the final result
my_string(my_favorite_allocator) + meow + woof + cat() + dog();

アロケータをより良く移植性のある方法で制御するには、目的のアロケータで構築された結果文字列に対して、appendinsertoperator+= などのメンバー関数を使用する必要があります。

(C++11以降)

オーバーロード (4)(7)(12)、および (16) のパラメータとして std::type_identity_t を使用することで、オーバーロード解決の規則に従って、std::basic_string<CharT, Traits, Allocator> 型のオブジェクトを、std::basic_string_view<CharT, Traits> への暗黙の変換を持つ型 T のオブジェクトに常に連結できること、またその逆も可能であることを保証します。

機能テストマクロ 規格 機能
__cpp_lib_string_view 202403 (C++26) 文字列と文字列ビューの連結、オーバーロード (4)(7)(12)(16)
(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) [編集]
English 日本語 中文(简体) 中文(繁體)