std::ios_base::iword
提供: cppreference.com
long& iword( int index ); |
||
まず、index
を有効なインデックスとするのに十分なようにプライベートな記憶域 (long の動的配列またはインデックス可能な別のデータ構造) を確保またはサイズ変更し、その後そのプライベートな記憶域のインデックス index
の long 要素への参照を返します。
この参照はこの ios_base
オブジェクトのあらゆる操作 (iword()
の別の呼び出しも含みます) によって無効化される可能性がありますが、格納された値は保���されるため、後に同じインデックスを使用して iword(index) から読み込むと同じ値を生成します (std::basic_ios::copyfmt() の次の呼び出しまでは)。 この値は任意の用途に使用できます。 要素のインデックスは xalloc() の以前の呼び出しによって取得したものでなければなりません。 さもなければ動作は未定義です。 新しい要素は 0 に初期化されます。
確保が失敗した場合は、 std::basic_ios<>::setstate(badbit) を呼びます。 これは std::ios_base::failure を投げる場合があります。
目次 |
[編集] ノート
iword の記憶域の一般的な用途はユーザ定義の入出力マニピュレータからユーザ定義の operator<<
や operator>>
または標準のストリームに設定されたユーザ定義の書式化ファセットに情報 (カスタム書式フラグなど) を渡すことです。
[編集] 引数
index | - | 要素のインデックス値 |
[編集] 戻り値
要素への参照。
[編集] 例外
badbit がセットされたときに std::ios_base::failure を投げるかもしれません。
[編集] 例
Run this code
#include <iostream> #include <string> struct Foo { static int foo_xalloc; std::string data; Foo(const std::string& s) : data(s) {} }; // allocates the iword storage for use with Foo objects int Foo::foo_xalloc = std::ios_base::xalloc(); // This user-defined operator<< prints the string in reverse if the iword holds 1 std::ostream& operator<<(std::ostream& os, Foo& f) { if(os.iword(Foo::foo_xalloc) == 1) return os << std::string(f.data.rbegin(), f.data.rend()); else return os << f.data; } // This I/O manipulator flips the number stored in iword between 0 and 1 std::ios_base& rev(std::ios_base& os) { os.iword(Foo::foo_xalloc) = !os.iword(Foo::foo_xalloc); return os; } int main() { Foo f("example"); std::cout << f << '\n' << rev << f << '\n' << rev << f << '\n'; }
出力:
example elpmaxe example
[編集] 関連項目
必要であればプライベートな記憶域をリサイズし、指定されたインデックスの void* 要素にアクセスします (パブリックメンバ関数) | |
[静的] |
pword() および iword() へのインデックスとして使用するのに安全な、プログラム全体で一意な整数を返します (パブリック静的メンバ関数) |