std::ios_base::xalloc
提供: cppreference.com
static int xalloc(); |
||
iword() および pword() を呼ぶことによって std::ios_base
のプライベートな記憶域の1個の long と1個の void* の要素にアクセスするために使用できる一意 (プログラム全体で) なインデックス値を返します。 xalloc
の呼び出しはメモリを確保しません。
この関数���スレッドセーフです。 複数のスレッドによる並行アクセスはデータ競合を発生しません。 (C++14以上)
実質的に、 return index++; を実行したかのように、 std::ios_base
のプライベート static データメンバをインクリメントします (index
がその static メンバの名前だとした場合)。 (複数のスレッドによる並行アクセスをサポートするために std::atomic であるかもしれません。 または、そうでなければ、同期されます。) (C++14以上)
目次 |
[編集] 引数
(なし)
[編集] 戻り値
pword や iword のインデックスとして使用するための一意な整数。
[編集] 例
派生ストリームオブジェクトの実行時型識別のために基底クラスの pword の記憶域を使用します。
Run this code
#include <iostream> template<class charT, class traits = std::char_traits<charT> > class mystream : public std::basic_ostream<charT, traits> { public: static const int 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> const int 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 pword template<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]
[編集] 関連項目
必要であればプライベートな記憶域をリサイズし、指定されたインデックスの void* 要素にアクセスします (パブリックメンバ関数) | |
必要であればプライベートな記憶域をリサイズし、指定されたインデックスの long 要素にアクセスします (パブリックメンバ関数) |