std::ios_base:: pword
|
void
*
&
pword
(
int
index
)
;
|
||
먼저, private 저장소(동적 배열 void * 또는 다른 인덱스 가능한 데이터 구조)를 index 가 유효한 인덱스가 되도록 충분히 할당하거나 크기를 조정한 후, private 저장소의 void * 요소 중 index 인덱스를 가진 요소에 대한 참조를 반환합니다.
이 참조는 이
ios_base
객체에 대한 모든 연산,
pword()
에 대한 또 다른 호출을 포함하여 무효화될 수 있지만,
저장된 값들은 유지됩니다. 따라서 동일한 인덱스로
pword
(
index
)
를 나중에 읽으면
std::basic_ios::copyfmt()
에 대한 다음 호출까지 동일한 값을 생성합니다. 이 값은 어떤 목적으로든 사용될 수 있습니다.
요소의 인덱스는 반드시
xalloc()
로 얻어야 하며, 그렇지 않으면 동작이 정의되지 않습니다.
새로운 요소들은
null pointer
로 초기화됩니다.
함수가 실패할 경우(할당 실패로 인한 가능성 있음) 그리고
*
this
가
basic_ios<>
객체 또는 하위 객체의 기본 클래스 하위 객체인 경우,
std::
basic_ios
<>
::
setstate
(
badbit
)
를 호출하며, 이는
std::ios_base::failure
를 발생시킬 수 있습니다.
목차 |
매개변수
| 인덱스 | - | 요소의 인덱스 값 |
반환값
요소에 대한 참조.
예외
badbit를 설정할 때 std::ios_base::failure 를 throw할 수 있습니다.
참고 사항
pword
에 저장된 포인터들에 관리가 필요한 경우,
register_callback()
을 사용하여 필요에 따라 딥 카피 또는 할당 해제를 수행하는 핸들러를 설치할 수 있습니다.
예제
파생된 스트림 객체의 런타임 타입 식별을 위해 기본 클래스 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 36 | C++98 |
참조가 무효화되면 저장된 값이
유지되지 않을 수 있음 |
저장된 값은 다음
copyfmt()
호출까지
유지됨 |
| LWG 41 | C++98 |
실패 시 함수 자체가 badbit를 설정하지만,
ios_base
는 해당 인터페이스를 제공하지 않음
|
badbit는
basic_ios
에 의해 설정됨
(만약 * this 가 해당 기본 클래스 하위 객체인 경우) |
참고 항목
|
필요한 경우 전용 저장소의 크기를 조정하고 지정된 인덱스의
long
요소에 접근합니다
(public member function) |
|
|
[static]
|
pword()
및
iword()
의 인덱스로 사용하기 안전한 프로그램 전체에서 고유한 정수를 반환합니다
(public static member function) |