Namespaces
Variants

std::ios_base:: xalloc

From cppreference.net
static int xalloc ( ) ;

프로그램 전체에서 고유한 인덱스 값을 반환하며, 이 값은 long 타입 하나와 void * 타입 하나의 요소에 접근하기 위해 std::ios_base 의 private 저장소에서 iword() pword() 를 호출할 때 사용할 수 있습니다. xalloc 호출은 메모리를 할당하지 않습니다.

이 함수는 스레드 안전합니다: 다중 스레드의 동시 접근이 데이터 경쟁을 발생시키지 않습니다.

(since C++11)

다음 사용 가능한 고유 인덱스를 효과적으로 증가시킵니다.

목차

반환값

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]

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2143 C++11 xalloc 는 스레드 안전하지 않았음 스레드 안전하게 변경됨

참고 항목

필요 시 전용 저장 공간의 크기를 조정하고 지정된 인덱스의 void * 요소에 접근합니다
(public member function)
필요 시 전용 저장 공간의 크기를 조정하고 지정된 인덱스의 long 요소에 접근합니다
(public member function)