名前空間
変種
操作

std::operator+(std::basic_string)

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
ヘッダ <string> で定義
(1)
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 );
(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,

                   const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20以上)
(2)
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 );
(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,

                   const CharT* rhs );
(C++20以上)
(3)
template<class CharT, class Traits, class Alloc>

    std::basic_string<CharT,Traits,Alloc>
        operator+( const std::basic_string<CharT,Traits,Alloc>& lhs,

                   CharT rhs );
(C++20未満)
template<class CharT, class Traits, class Alloc>

    constexpr std::basic_string<CharT,Traits,Alloc>
        operator+( std::const basic_string<CharT,Traits,Alloc>& lhs,

                   CharT rhs );
(C++20以上)
(4)
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 );
(C++20未満)
template< class CharT, class Traits, class Alloc >

    constexpr std::basic_string<CharT,Traits,Alloc>
        operator+( const CharT* lhs,

                   const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20以上)
(5)
template< class CharT, class Traits, class Alloc >

    std::basic_string<CharT,Traits,Alloc>
        operator+( CharT lhs,

                   const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20未満)
constexpr std::basic_string<CharT,Traits,Alloc>

        operator+( CharT lhs,

                   const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20以上)
(6)
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 );
(C++11以上)
(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::basic_string<CharT,Traits,Alloc>&& rhs );
(C++20以上)
(7)
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 );
(C++11以上)
(C++20未満)
template< class CharT, class Traits, class Alloc >

    constexpr std::basic_string<CharT,Traits,Alloc>
        operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

                   const std::basic_string<CharT,Traits,Alloc>& rhs );
(C++20以上)
(8)
template< class CharT, class Traits, class Alloc >

    std::basic_string<CharT,Traits,Alloc>
        operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

                   const CharT* rhs );
(C++11以上)
(C++20未満)
template< class CharT, class Traits, class Alloc >

    constexpr std::basic_string<CharT,Traits,Alloc>
        operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

                   const CharT* rhs );
(C++20以上)
(9)
template< class CharT, class Traits, class Alloc >

    std::basic_string<CharT,Traits,Alloc>
        operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

                   CharT rhs );
(C++11以上)
(C++20未満)
template< class CharT, class Traits, class Alloc >

    constexpr std::basic_string<CharT,Traits,Alloc>
        operator+( std::basic_string<CharT,Traits,Alloc>&& lhs,

                   CharT rhs );
(C++20以上)
(10)
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 );
(C++11以上)
(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::basic_string<CharT,Traits,Alloc>&& rhs );
(C++20以上)
(11)
template< class CharT, class Traits, class Alloc >

    std::basic_string<CharT,Traits,Alloc>
        operator+(const CharT* lhs,

                  std::basic_string<CharT,Traits,Alloc>&& rhs );
(C++11以上)
(C++20未満)
template< class CharT, class Traits, class Alloc >

    constexpr std::basic_string<CharT,Traits,Alloc>
        operator+(const CharT* lhs,

                  std::basic_string<CharT,Traits,Alloc>&& rhs );
(C++20以上)
(12)
template< class CharT, class Traits, class Alloc >

    std::basic_string<CharT,Traits,Alloc>
        operator+( CharT lhs,

                   std::basic_string<CharT,Traits,Alloc>&& rhs );
(C++11以上)
(C++20未満)
template< class CharT, class Traits, class Alloc >

    constexpr std::basic_string<CharT,Traits,Alloc>
        operator+( CharT lhs,

                   std::basic_string<CharT,Traits,Alloc>&& rhs );
(C++20以上)

lhs の文字列の後に rhs の文字列が続いた内容を持つ文字列を返します。

結果に対して使用されるアロケ��タは以下の通りです。

1-3) std::allocator_traits<Alloc>::select_on_container_copy_construction(lhs.get_allocator())
4-5) std::allocator_traits<Alloc>::select_on_container_copy_construction(rhs.get_allocator())
6-9) lhs.get_allocator()
10-12) rhs.get_allocator()

別の言い方をすると、一方の被演算子が basic_string の右辺値である場合は、そのアロケータが使用されます。 そうでなければ、左辺値の basic_string 被演算子のアロケータに対して select_on_container_copy_construction が使用されます。 いずれの場合でも、どちらも同じ値カテゴリの basic_string であるときは、左の被演算子が優先されます。

(6-12) の場合、すべての右辺値の basic_string 被演算子は、有効だけれども未規定な状態になります。

(C++11以上)

目次

[編集] 引数

lhs - 文字列、文字、またはヌル終端配列の最初の文字を指すポインタ
rhs - 文字列、文字、またはヌル終端配列の最初の文字を指すポインタ

[編集] 戻り値

上で述べたように決定されたアロケータを使用した、 (C++11以上) lhs の文字列の後に rhs の文字列が続いた内容を持つ文字列。

ノート

operator+ は、ステートフルなアロケータが関係する場合 (std::pmr::string を使用するときなど) (C++17以上) は、多大な注意を払って使用するべきです。 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() + /*...*/; // meow のアロケータに対する SOCCC を使用します。
woof + dog() + /*...*/; // dog() の戻り値のアロケータを使用します。
 
meow + woof + meow; // meow のアロケータに対する SOCCC を使用します。
meow + (woof + meow); // woof のアロケータに対する SOCCC を使用します。
 
// 注: SOCCC = select_on_container_copy_construction

operator+ の呼び出しをチェインする場合、最終結果に対して使用されるアロケータは所望のアロケータを持つ右辺値の basic_string を先頭に追加することによって制御できます。

// 最終結果に対して my_favorite_allocator を使用します。
my_string(my_favorite_allocator) + meow + woof + cat() + dog();

アロケータの良好かつ移植性のある制御のためには、所望のアロケータを用いて構築した結果文字列に対して append()insert() および operator+=() のようなメンバ関数を使用するべきです。

(C++11以上)

[編集]

#include <iostream>
#include <string>
 
int main()
{
    std::string s1 = "Hello";
    std::string s2 = "world";
    std::cout << s1 + ' ' + s2 + "!\n";
}

出力:

Hello world!

[編集] 欠陥報告

以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。

DR 適用先 発行時の動作 正しい動作
P1165R1 C++11 allocator propagation is haphazard and inconsistent made more consistent

[編集] 関連項目

文字を末尾に追加します
(パブリックメンバ関数) [edit]
文字を末尾に追加します
(パブリックメンバ関数) [edit]
文字を挿入します
(パブリックメンバ関数) [edit]