GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_string::operator= - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. operator=

最終更新日時: 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オブジェクトからの変換。以下と等価。
    basic_string_view<charT, traits> sv = t;
    return assign(sv);
    

テンプレートパラメータ制約

  • (6) : 以下の両方を満たしていること
    • is_convertible_v<const T&, basic_string_view<charT, traits>> == true
    • is_convertible_v<const T&, const charT*> == false

効果

コピーを行った場合と、ムーブ代入を行った場合で効果が異なる

メンバ関数 コピーの場合 ムーブ代入の場合
data() str.data() をコピーした領域の先頭ポインタ str.data()
size() str.size() と同じ値 str.size()と同じ値
capacity() size() 以上の値 size() 以上の値

戻り値

*this

例外

ムーブ代入の場合は例外を投げない。

例

#include <iostream>
#include <string>
#include <string_view>

int main()
{
  std::string s;

  // (1) basic_string左辺値の代入
  {
    std::string r = "hello";
    s = r;
  }

  // (2) basic_string右辺値の代入
  {
    s = std::string("hello");
  }

  // (3) 文字配列の代入
  {
    s = "hello";
  }

  // (4) 文字の代入
  {
    s = 'a';
  }

  // (5) 文字の初期化子リストを代入
  {
    s = {'h', 'e', 'l', 'l', 'o'};
  }

  // (6) std::basic_string_viewを代入
  {
    s = std::string_view{"Hello World"}.substr(0, 5);
  }

  std::cout << s << std::endl;
}

出力

Hello

参照

  • N2679 Initializer Lists for Standard Containers(Revision 1)
  • P0254R2 Integrating std::string_view and std::string
  • N4258 Cleaning-up noexcept in the Library, Rev 3
  • LWG Issue 2946. LWG 2758's resolution missed further corrections
    • 意図しない暗黙変換防止のためにstring_viewを受けるオーバーロード(6)の引数型をconst T&に変更
  • P2166R1 A Proposal to Prohibit std::basic_string and std::basic_string_view construction from nullptr.
    • C++23での、nullptr_tをとる代入演算子のdelete宣言追加
  • P0980R1 Making std::string constexpr
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.