std::ios_base::pword
提供: cppreference.com
void*& pword(int index ); | ||
まず、 index
を有効なインデックスとするのに十分なようにプライベートな記憶域 (void* の動的配列またはインデックス可能な別のデータ構造) を確保またはサイズ変更し、その後そのプライベートな記憶域のインデックス index
の void* 要素への参照を返します。
この参照はこの ios_base
オブジェクトのあらゆる操作 (pword()
の別の呼び出しも含みます) によって無効化される可能性がありますが、格納された値は保持されるため、後に同じインデックスを使用して pword(index) から読み込むと同じ値を生成します (std::basic_ios::copyfmt() の次の呼び出しまでは)。 この値は任意の用途に使用できます。 要素のインデックスは xalloc() の以前の呼び出しによって取得したものでなければなりません。 さもなければ動作は未定義です。 新しい要素は NULL に初期化されます。
確保が失敗した場合は、 std::basic_ios<>::setstate(badbit) を呼びます。 これは std::ios_base::failure を投げる場合があります。
目次 |
[編集]引数
index | - | 要素のインデックス値 |
[編集]戻り値
要素への参照。
[編集]例外
badbit がセットされたときに std::ios_base::failure を投げるかもしれません。
[編集]ノート
pword
に格納されるポインタが管理を必要とする場合は、必要に応じてディープコピーや解放を行うハンドラをインストールするために register_callback() を使用できます。
[編集]例
派生ストリームオブジェクトの実行時型識別のために基底クラスの pword の記憶域を使用します。
Run this code
#include <iostream> template<class charT, class traits =std::char_traits<charT>>class mystream :publicstd::basic_ostream<charT, traits>{public:staticconstint xindex; mystream(std::basic_ostream<charT, traits>& ostr):std::basic_ostream<charT, traits>(ostr.rdbuf()){ this->pword(xindex)= this;} void myfn(){*this <<"[special handling for mystream]";}}; // each specialization of mystream obtains a unique index from xalloc()template<class charT, class traits>constint mystream<charT, traits>::xindex= std::ios_base::xalloc(); // This I/O manipulator will be able to recognize ostreams that are mystreams// by looking up the pointer stored in pwordtemplate<class charT, class traits>std::basic_ostream<charT,traits>& mymanip(std::basic_ostream<charT,traits>& os){if(os.pword(mystream<charT,traits>::xindex)==&os)static_cast<mystream<charT,traits>&>(os).myfn();return os;} int main(){std::cout<<"cout, narrow-character test "<< mymanip <<'\n'; mystream<char> myout(std::cout); myout <<"myout, narrow-character test "<< mymanip <<'\n'; std::wcout<<"wcout, wide-character test "<< mymanip <<'\n'; mystream<wchar_t> mywout(std::wcout); mywout <<"mywout, wide-character test "<< mymanip <<'\n';}
出力:
cout, narrow-character test myout, narrow-character test [special handling for mystream] wcout, wide-character test mywout, wide-character test [special handling for mystream]
[編集]関連項目
必要であればプライベートな記憶域をリサイズし、指定されたインデックスの long 要素にアクセスします (パブリックメンバ関数) | |
[静的] | pword() および iword() へのインデックスとして使用するのに安全な、プログラム全体で一意な整数を返します (パブリック静的メンバ関数) |