Home / プログラミング / C++ / C++11 既存ライブラリ変更点 / 入出力
入出力

C++03規格時点で存在した入出力ライブラリに対するC++11規格での変更点をまとめています。

概要

当文書では、入出力(ストリーム)に関する機能を提供するヘッダファイル群について、C++11規格での変更点をまとめている。

他の多くの標準ライブラリ定義クラスと異なり、ほとんどの入出力クラスには継承関係が存在する。
クラス間の継承ツリーについては下記ページ冒頭の図を参照のこと。

基本的に、親クラスにおける変更点はその派生先となる子孫クラスにも引き継がれる。
そのため当文書では、ヘッダファイルをアルファベット順ではなく継承ツリーのルート側から順に並べている。

<ios>

ios_base クラス

  • コピーコンストラクタおよびコピー代入演算子が private 宣言から public の delete 指定になった。

ios_base::failure クラス

  • 継承元クラスが exception から system_error に変更された。

  • コンストラクタの定義が変更された。

    // C++03
    explicit failure(const string& msg);
    // C++11
    explicit failure(const string& msg, const error_code& err_code = io_errc::stream);
    explicit failure(const char* msg, const error_code& err_code = io_errc::stream);
  • デストラクタとメンバ関数 what のオーバライド定義が削除された。

basic_ios クラステンプレート

ios_base クラスを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // bool 型への明示的型変換。(C++11規格により if 文などでは暗黙の型変換が行われる。)
    // エラーフラグが1つもセットされていなければ true を、そうでなければ false を返す。
    explicit operator bool() const;
    // src の内容を自身にムーブする。
    protected: void move(basic_ios& src);
    protected: void move(basic_ios&& src);
    // 2つの basic_ios 間で内容を入れ替える。
    protected: void swap(basic_ios& other) noexcept;
    // ストリームバッファを設定する。
    protected: void set_rdbuf(basic_streambuf<char_type, traits_type>* buf);
  • コピーコンストラクタおよびコピー代入演算子が delete 指定で定義された。

  • メンバ関数 operator void* が削除された。(メンバ関数 operator bool が同等の役割を引き継ぐ)

<ios> その他

  • 列挙クラス型 io_errc が定義された。

  • 下記のフリー関数が定義された。

    // iostream 関連のエラーコードカテゴリを表す値を取得する。
    // system_error 派生クラス型変数 e について e.code().category() との比較に用いる。
    const error_category& iostream_category();
    // 実数型の文字列フォーマットに16進数浮動小数点を用いるようにするマニピュレータ。
    // cout << hexfloat << 0.1; などのように用いる。
    ios_base& hexfloat(ios_base& s);
    // 実数型の文字列フォーマットを既定の設定に戻すマニピュレータ。
    // cout << defaultfloat << 0.1; などのように用いる。
    ios_base& defaultfloat(ios_base& s);
  • streamoff 型の定義が変更された。

    • C++03:処理系依存の型。
    • C++11:システムがサポートする最大ファイルサイズを表現可能な符号付き整数型。(通常は long long

<istream>

basic_istream クラステンプレート

basic_ios クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_istream 間で内容を入れ替える。
    void swap(basic_istream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_istream& operator=(const basic_istream&) = delete;
    basic_istream& operator=(basic_istream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    basic_istream(const basic_istream&) = delete;
    protected: basic_istream(basic_istream&& src);
  • 下記のメンバ関数オーバロードが定義された。

    basic_istream& operator>>(long long& value);
    basic_istream& operator>>(unsigned long long& value);
  • メンバ関数 putbackungetseekg 呼び出し時に eofbit フラグがセットされていた場合の動作が変更された。

    • C++03:処理に失敗する。 failbit フラグのセットのみを行う。
    • C++11: eofbit フラグをクリアし、通常通りの処理を行う。

basic_istream::sentry クラス

  • コピーコンストラクタおよびコピー代入演算子が private 宣言から public の delete 指定になった。

  • bool 型への型変換メンバ関数に explicit が付けられ、暗黙の型変換が抑止された。

basic_iostream クラステンプレート

basic_istream クラステンプレートおよび basic_ostream クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_iostream 間で内容を入れ替える。
    void swap(basic_iostream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_iostream& operator=(const basic_iostream&) = delete;
    basic_iostream& operator=(basic_iostream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    basic_iostream(const basic_iostream&) = delete;
    protected: basic_iostream(basic_iostream&& src);
  • 多重継承で定義が曖昧だった下記のメンバ型が明示的に定義された。

    • char_type
    • traits_type
    • int_type
    • pos_type
    • off_type

<istream> その他

  • 下記のフリー関数オーバロードが定義された。

    template<class CharT, class Traits, class T>
    basic_istream<CharT, Traits>& operator>>(basic_istream<CharT, Traits>&& s, T& value);

<ostream>

basic_ostream クラステンプレート

basic_ios クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_ostream 間で内容を入れ替える。
    void swap(basic_ostream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_ostream& operator=(const basic_ostream&) = delete;
    basic_ostream& operator=(basic_ostream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    basic_ostream(const basic_ostream&) = delete;
    protected: basic_ostream(basic_ostream&& src);
  • 下記のメンバ関数オーバロードが定義された。

    basic_ostream& operator<<(long long value);
    basic_ostream& operator<<(unsigned long long value);

basic_ostream::sentry クラス

  • コピーコンストラクタおよびコピー代入演算子が private 宣言から public の delete 指定になった。

  • bool 型への型変換メンバ関数に explicit が付けられ、暗黙の型変換が抑止された。

<ostream> その他

  • 下記のフリー関数オーバロードが定義された。

    template<class CharT, class Traits, class T>
    basic_ostream<CharT, Traits>& operator<<(
        basic_ostream<CharT, Traits>&& s,
        const T& value);

<streambuf>

basic_streambuf クラステンプレート

  • 下記のメンバ関数が定義された。

    // 2つの basic_streambuf 間で内容を入れ替える。
    protected: void swap(basic_streambuf& other);
    // 代入演算子。
    protected: basic_streambuf& operator=(const basic_streambuf& src);
  • 下記のコンストラクタオーバロードが定義された。

    protected: basic_streambuf(const basic_streambuf&);

<iostream>

<fstream>

basic_ifstream クラステンプレート

basic_istream クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_ifstream 間で内容を入れ替える。
    void swap(basic_ifstream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_ifstream& operator=(const basic_ifstream&) = delete;
    basic_ifstream& operator=(basic_ifstream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    explicit basic_ifstream(const string& path, ios_base::openmode mode = ios_base::in);
    basic_ifstream(const basic_ifstream&) = delete;
    basic_ifstream(basic_ifstream&& src);
  • 下記のメンバ関数オーバロードが定義された。

    void open(const string& path, ios_base::openmode mode = ios_base::in);
  • メンバ関数 is_open の定義が変更され、 const 修飾された。

basic_ofstream クラステンプレート

basic_ostream クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_ofstream 間で内容を入れ替える。
    void swap(basic_ofstream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_ofstream& operator=(const basic_ofstream&) = delete;
    basic_ofstream& operator=(basic_ofstream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    explicit basic_ofstream(const string& path, ios_base::openmode mode = ios_base::out);
    basic_ofstream(const basic_ofstream&) = delete;
    basic_ofstream(basic_ofstream&& src);
  • 下記のメンバ関数オーバロードが定義された。

    void open(const string& path, ios_base::openmode mode = ios_base::out);
  • メンバ関数 is_open の定義が変更され、 const 修飾された。

basic_fstream クラステンプレート

basic_iostream クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_fstream 間で内容を入れ替える。
    void swap(basic_fstream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_fstream& operator=(const basic_fstream&) = delete;
    basic_fstream& operator=(basic_fstream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    explicit basic_fstream(
        const string& path,
        ios_base::openmode mode = ios_base::in | ios_base::out);
    basic_fstream(const basic_fstream&) = delete;
    basic_fstream(basic_fstream&& src);
  • 下記のメンバ関数オーバロードが定義された。

    void open(
        const string& path,
        ios_base::openmode mode = ios_base::in | ios_base::out);
  • メンバ関数 is_open の定義が変更され、 const 修飾された。

basic_filebuf クラステンプレート

basic_streambuf クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_filebuf 間で内容を入れ替える。
    void swap(basic_filebuf& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_filebuf& operator=(const basic_filebuf&) = delete;
    basic_filebuf& operator=(basic_filebuf&& src);
  • 下記のコンストラクタオーバロードが定義された。

    basic_filebuf(const basic_filebuf&) = delete;
    basic_filebuf(basic_filebuf&& src);
  • 下記のメンバ関数オーバロードが定義された。

    void open(const string& path, ios_base::openmode mode);

<fstream> その他

  • フリー関数 swap<utility>)のオーバロードが定義された。

    template<class CharT, class Traits>
    void swap(basic_ifstream<CharT, Traits>& a, basic_ifstream<CharT, Traits>& b);
    template<class CharT, class Traits>
    void swap(basic_ofstream<CharT, Traits>& a, basic_ofstream<CharT, Traits>& b);
    template<class CharT, class Traits>
    void swap(basic_fstream<CharT, Traits>& a, basic_fstream<CharT, Traits>& b);
    template<class CharT, class Traits>
    void swap(basic_filebuf<CharT, Traits>& a, basic_filebuf<CharT, Traits>& b);

<sstream>

basic_istringstream クラステンプレート

basic_istream クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_istringstream 間で内容を入れ替える。
    void swap(basic_istringstream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_istringstream& operator=(const basic_istringstream&) = delete;
    basic_istringstream& operator=(basic_istringstream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    basic_istringstream(const basic_istringstream&) = delete;
    basic_istringstream(basic_istringstream&& src);

basic_ostringstream クラステンプレート

basic_ostream クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_ostringstream 間で内容を入れ替える。
    void swap(basic_ostringstream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_ostringstream& operator=(const basic_ostringstream&) = delete;
    basic_ostringstream& operator=(basic_ostringstream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    basic_ostringstream(const basic_ostringstream&) = delete;
    basic_ostringstream(basic_ostringstream&& src);

basic_stringstream クラステンプレート

basic_iostream クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_stringstream 間で内容を入れ替える。
    void swap(basic_stringstream& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_stringstream& operator=(const basic_stringstream&) = delete;
    basic_stringstream& operator=(basic_stringstream&& src);
  • 下記のコンストラクタオーバロードが定義された。

    basic_stringstream(const basic_stringstream&) = delete;
    basic_stringstream(basic_stringstream&& src);

basic_stringbuf クラステンプレート

basic_streambuf クラステンプレートを継承しており、それらの変更点も引き継ぐ。

  • 下記のメンバ関数が定義された。

    // 2つの basic_stringbuf 間で内容を入れ替える。
    void swap(basic_stringbuf& other);
    // 代入演算子。コピーはできないがムーブは可能。
    basic_stringbuf& operator=(const basic_stringbuf&) = delete;
    basic_stringbuf& operator=(basic_stringbuf&& src);
  • 下記のコンストラクタオーバロードが定義された。

    basic_stringbuf(const basic_stringbuf&) = delete;
    basic_stringbuf(basic_stringbuf&& src);

<sstream> その他

  • フリー関数 swap<utility>)のオーバロードが定義された。

    template<class CharT, class Traits, class Alloc> void swap(
        basic_istringstream<CharT, Traits, Alloc>& a,
        basic_istringstream<CharT, Traits, Alloc>& b);
    template<class CharT, class Traits, class Alloc> void swap(
        basic_ostringstream<CharT, Traits, Alloc>& a,
        basic_ostringstream<CharT, Traits, Alloc>& b);
    template<class CharT, class Traits, class Alloc> void swap(
        basic_stringstream<CharT, Traits, Alloc>& a,
        basic_stringstream<CharT, Traits, Alloc>& b);
    template<class CharT, class Traits, class Alloc> void swap(
        basic_stringbuf<CharT, Traits, Alloc>& a,
        basic_stringbuf<CharT, Traits, Alloc>& b);

<iomanip>

文中で型名が unspecified となっているものは処理系依存の型であることを表す。

  • 下記の関数が定義された。

    // 通貨フォーマットの入力を受け取るマニピュレータ。
    // intl が true ならば国際通貨文字列フォーマット("USD", "JPY" 等)を要求する。
    // double value; cin >> get_money(value); などのように用いる。
    template<class MoneyT>
    unspecified get_money(MoneyT& value, bool intl = false);
    // 通貨フォーマットでの出力を行うマニピュレータ。
    // intl が true ならば国際通貨文字列フォーマットで出力する。
    // cout << put_money(100); などのように用いる。
    template<class MoneyT>
    unspecified put_money(const MoneyT& value, bool intl = false);
    // 時刻フォーマットの入力を受け取るマニピュレータ。
    // tm value; cin >> get_time(&value, "%Y/%m/%d %H:%M:%S"); などのように用いる。
    template<class CharT>
    unspecified get_time(tm* value, const CharT* format);
    // 時刻フォーマットでの出力を行うマニピュレータ。
    // extern tm value; cout << put_time(&value, "%Y/%m/%d"); などのように用いる。
    template<class CharT>
    unspecified put_time(const tm* value, const CharT* format);

<iosfwd>

  • 下記の前方宣言が追加された。

    template<> class char_traits<char16_t>;
    template<> class char_traits<char32_t>;