最終更新日時:
が更新

履歴 編集

function
<fstream>

std::basic_filebuf::operator=(C++11)

basic_filebuf& operator=(const basic_filebuf&) = delete; // (1) C++11
basic_filebuf& operator=(basic_filebuf&& rhs);           // (2) C++11

概要

  • (1) : コピー代入。この演算子はdelete定義されており、basic_filebufオブジェクトはコピーできない
  • (2) : ムーブ代入。rhsが管理していたファイルの所有権を*thisに移動する

効果

  • (2) : close()を呼び出したのち、rhsからムーブ代入する

事後条件

  • (2) : *thisは、rhsからムーブ構築された場合と同じ観測可能な状態を持つ。詳細はコンストラクタを参照
    • rhsは、いかなるファイルも参照しない状態(is_open()== false)となる

戻り値

  • (2) : *this

#include <iostream>
#include <fstream>
#include <utility>

int main()
{
  {
    std::filebuf out;
    out.open("test.txt", std::ios_base::out);
    out.sputn("Hello", 5);
  }

  std::filebuf a;
  a.open("test.txt", std::ios_base::in);

  std::filebuf b;
  b = std::move(a); // aが開いていたファイルをbに移動する

  std::cout << std::boolalpha
            << a.is_open() << ' '
            << b.is_open() << std::endl;

  std::cout << static_cast<char>(b.sbumpc()) << std::endl;
}

出力

false true
H

バージョン

言語

  • C++11

関連項目