在线文档教程
C++
输入/输出 | Input/output

std::ios_base::xalloc

科技促进发展:监督办[医]基地::xalloc

static int xalloc(

返回一个唯一的%28程序范围%29索引值,该索引值可用于访问long还有一个void*的私有存储中的元素。std::ios_base打电话iword()pword().呼吁xalloc不分配内存。

此函数是线程安全的;多个线程的并发访问不会导致数据竞争.。%28自C++14%29。

的私有静态数据成员。std::ios_base,就像通过执行return index++;,如果index是静态成员%28的名称,它可能是std::atomic以支持多线程的并发访问,或自C++14%29以来同步的%29%28。

参数

%280%29

返回值

作为pword/iword索引的唯一整数。

将基类pword存储用于派生流对象的运行时类型标识。

二次

#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]

二次

另见

pwordresizes the private storage if necessary and access to the void* element at the given index (public member function)
iwordresizes the private storage if necessary and access to the long element at the given index (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/IOS[医]碱/xalloc