首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::ios_base::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存储用于派生流对象的运行时类型标识。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
cout, narrow-character test 
myout, narrow-character test [special handling for mystream]
wcout, wide-character test 
mywout, wide-character test [special handling for mystream]

二次

另见

pword

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

iword

resizes the private storage if necessary and access to the long element at the given index (public member function)

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com