Skip to content

Latest commit

 

History

History
80 lines (61 loc) · 1.96 KB

File metadata and controls

80 lines (61 loc) · 1.96 KB

operator=

  • fstream[meta header]
  • std[meta namespace]
  • basic_filebuf[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
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;
}
  • std::filebuf[link /reference/fstream/basic_filebuf.md]
  • out.open[link open.md]
  • a.open[link open.md]
  • a.is_open()[link is_open.md]
  • b.is_open()[link is_open.md]
  • out.sputn[link /reference/streambuf/basic_streambuf/sputn.md]
  • b.sbumpc()[link /reference/streambuf/basic_streambuf/sbumpc.md]
  • std::move[link /reference/utility/move.md]

出力

false true
H

バージョン

言語

  • C++11

関連項目