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.
I wouldn't recommend either of those things.
You haven't exactly told us how you would use the data stored in the stream, but it's a bit unintuitive to set
iword()
each and every time you want to write.Normally you don't want to inherit from the stream base classes. The only case in which it might be useful is when you're wrapping a custom stream around a stream buffer, but that's usually for convenience.
Another problem is that your inserter returns
std::ostream
, meaning that when chaining operators you'll only be writing to thestd::ostream
base object on the second write:The idiomatic solution is to customize a
std::num_put<char>
facet for your stream's locale. This way you wrap the functionality directly under the hood of the stream so that it doesn't change the way the user uses the stream.