GeezerVison is showing cpprefjp.github.io — std::basic_string::operator= - cpprefjp C++日本語リファレンス
最終更新日時:
2026年08月25日 12時11分17秒 (JST)
Akira Takahashi
が更新
履歴
編集
function
<string>
std:: basic_string:: operator=
basic_string & operator = ( const basic_string & str ); // (1) C++98
constexpr basic_string & operator = ( const basic_string & str ); // (1) C++20
basic_string & operator = ( basic_string && str ) noexcept ; // (2) C++11
basic_string & operator = ( basic_string && str ) noexcept
( allocator_traits < Allocator >:: propagate_on_container_move_assignment :: value
|| allocator_traits < Allocator >:: is_always_equal :: value ); // (2) C++17
constexpr basic_string & operator = ( basic_string && str ) noexcept
( allocator_traits < Allocator >:: propagate_on_container_move_assignment :: value
|| allocator_traits < Allocator >:: is_always_equal :: value ); // (2) C++20
basic_string & operator = ( const charT * s ); // (3) C++98
constexpr basic_string & operator = ( const charT * s ); // (3) C++20
basic_string & operator = ( charT c ); // (4) C++98
constexpr basic_string & operator = ( charT c ); // (4) C++20
basic_string & operator = ( initializer_list < charT > il ); // (5) C++11
constexpr basic_string & operator = ( initializer_list < charT > il ); // (5) C++20
// string_view を引数に取るオーバーロード
template < class T >
basic_string & operator = ( const T & t ); // (6) C++17
template < class T >
constexpr basic_string & operator = ( const T & t ); // (6) C++20
basic_string & operator = ( nullptr_t ) = delete ; // (7) C++23
概要
(1) : str を *this へコピーする。*this と str が同一オブジェクトである場合は何も行わない。
(2) : str から *this へデータの所有権を移動する。*this と str が同一オブジェクトである場合は何も行わない。
(3) : *this = basic_string(s); と等価。
(4) : *this = basic_string(1, c); と等価。
(5) : *this = basic_string(il); と等価。
(6) : std::basic_string_view オブジェクトからの変換。以下と等価。
テンプレートパラメータ制約
効果
コピーを行った場合と、ムーブ代入を行った場合で効果が異なる
戻り値
*this
例外
ムーブ代入の場合は例外を投げない。
例
出力
Hello
参照