public member function
<ios> <iostream>
Get pointer element of extensible array
Returns a reference to the object of type void*
which corresponds to index idx in the internal extensible array.
If idx is an index to a new element and the internal extensible array is not long enough (or is not yet allocated), the function extends it (or allocates it) with as many elements initialized to null pointers as necessary.
The reference returned is guaranteed to be valid at least until another operation is performed on the stream object, including another call to pword. Once another operation is performed, the reference may become invalid, although a subsequent call to this same function with the same idx argument returns a reference to the same value within the internal extensible array.
The internal extensible array is a general-purpose array of objects of type long
(if accessed with member iword) or void*
(if accessed with member pword). Libraries may implement this array in diverse ways: iword and pword may or may not share a unique array, and they may not even be arrays, but some other data structure.
Parameters
- idx
- An index value for an element of the internal extensible array.
Some implementations expect idx to be a value previously returned by member xalloc.
Return Value
A reference to the element in the internal extensible array whose index is idx.
This value is returned as a reference to an object of type void*
.
On failure, a valid void*&
initialized to 0
is returned, and (if the stream object inherits from basic_ios) the badbit state flag is set.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
// pword example
#include <iostream> // std::ios, std::cout, std::cerr, std::clog
const int name_index = std::ios::xalloc();
// stores pointer in extensible array:
void SetStreamName (std::ios& stream, const char* name) {
stream.pword(name_index) = const_cast<char*>(name);
}
// custom manipulator that uses stored pointer:
std::ostream& StreamName (std::ostream& os) {
const char* name = static_cast<const char*>(os.pword(name_index));
if (name) os << name;
else os << "(unknown)";
}
int main()
{
SetStreamName(std::cout, "standard output stream");
SetStreamName(std::cerr, "standard error stream");
std::cout << StreamName << '\n';
std::cerr << StreamName << '\n';
std::clog << StreamName << '\n';
return 0;
}
| |
Possible output:
standard output stream
standard error stream
(unknown)
|
Data races
May modify the stream object. The returned value may also be used to modify it.
Concurrent access to the same stream object may cause data races.
Exception safety
Basic guarantee: if an exception is thrown, the stream is in a valid state.
See also
- ios_base::xalloc
- Get new index for extensible array [static] (public static member function
)
- ios_base::iword
- Get integer element of extensible array (public member function
)