I need to implement a C++ iostream manipulator. Reading here and there it seems that people uses 2 ways
using
ios_base::xalloc
andios_base::iword
implementing a derived class from iostream like example below.
I like the second way but probably it has cons that I cannot see or understand compared to the first way.
// Example of point 2
struct mystream : public iostream
{
ostream& o_;
mystream(ostream& o) : o_(o) {}
ostream& operator << (int a) {
// do something with o and a
o << "<A>" << a << "</A>";
return *this;
}
};
ostream mymanipulator(ostream& out) {
return mystream(out);
}
I found a very good implementation of way #2 in this post Custom manipulator for C++ iostream.
It looks to me that xalloc and iword
are more used to store some custom internal state for my custom stream to be used at some point.