Possible Duplicate:
“Roll-Back” or Undo Any Manipulators Applied To A Stream Without Knowing What The Manipulators Were
Consider the following code
int temp=256;
cout<<temp<<endl;
cout<<hex<<temp<<endl;
cout<<temp<<endl;
The output is “256”,”100″ and “100” respectively.
Is it possible to make the ‘hex’ flag non-binding?
I do not wish to write ‘dec’ explicitly.
Not for the standard manipulators. But in practice, you probably
shouldn’t be using the standard manipulators (other than perhaps as an
example); they correspond to physical markup, and in an application, you
want to use logical markup. You want to write something like
, where temperature is an application specific manipulator, defining
(globally) how temperatures are supposed to be output. That way, if the
specification changes, and requires a different format for temperatures,
you only have to change it in one place. Debugging output is somewhat
of an exception here, but even there, it’s much easier to write
something like:
than
(And you will probably use somthing like
HexFmtoften enough tojustify having it in your toolbox.)
Manipulators which you write yourself can be made to restore the
previous state, at least at the end of the full expression. All of my
manipulators derive from the following class:
StateSavingManip.hh:
StateSavingManip.cc:
Which allows something simple like: