"Roll-Back" or Undo Any Manipulators Applied To A Stream Without Knowing What The Manipulators Were
<p>If I apply an arbitrary number of manipulators to a stream, is there a way to undo the application of those manipulators in a generic way?</p>

<p>For example, consider the following:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;

using namespace std;

int main()
{
    cout &lt;&lt; "Hello" &lt;&lt; hex &lt;&lt; 42 &lt;&lt; "\n";
    // now i want to "roll-back" cout to whatever state it was in
    // before the code above, *without* having to know 
    // what modifiers I added to it

    // ... MAGIC HAPPENS! ...

    cout &lt;&lt; "This should not be in hex: " &lt;&lt; 42 &lt;&lt; "\n";
}
</code></pre>

<p>Suppose I want to add code at <code>MAGIC HAPPENS</code> that will revert the state of the stream manipulators to whatever it was before I did <code>cout &lt;&lt; hex</code>. <em>But</em> I don't know what manipulators I added.  How can I accomplish this?</p>

<p>In other words, I'd like to be able to write something like this (psudocode/fantasy code):</p>

<pre><code>std::something old_state = cout.current_manip_state();
cout &lt;&lt; hex;
cout.restore_manip_state(old_state);
</code></pre>

<p>Is this possible?</p>

<h2>EDIT:</h2>

<p>In case you're curious, I'm interested in doing this in a custom <code>operator&lt;&lt;()</code> I'm writing for a complex type.  The type is a kind of discriminated union, and different value types will have different manips applied to the stream.</p>

<h2>EDIT2:</h2>

<p>Restriction:  I cannot use Boost or any other 3rd party libraries.  Solution must be in standard C++.</p>
