Agreed with Neil on principle.
You want to change the behavior of the buffer, because that is the only way to extend iostreams. endl
does this:
flush(__os.put(__os.widen('\n')));
widen
returns a single character, so you can't put your string in there. put
calls putc
which is not a virtual function and only occasionally hooks to overflow
. You can intercept at flush
, which calls the buffer's sync
. You would need to intercept and change all newline characters as they are overflow
ed or manually sync
ed and convert them to your string.
Designing an override buffer class is troublesome because basic_streambuf
expects direct access to its buffer memory. This prevents you from easily passing I/O requests to a preexisting basic_streambuf
. You need to go out on a limb and suppose you know the stream buffer class, and derive from it. (cin
and cout
are not guaranteed to use basic_filebuf
, far as I can tell.) Then, just add virtual overflow
and sync
. (See §27.5.2.4.5/3 and 27.5.2.4.2/7.) Performing the substitution may require additional space so be careful to allocate that ahead of time.
- OR -
Just declare a new endl
in your own namespace, or better, a manipulator which isn't called endl
at all!