Concisely, robustly, temporarily stashing c++ iostream formats I got tired of the boilerplate and tedium restoring formatting context, so I made an RAII stasher that relies on the destroy-at-end-of-full-statement temporary semantics. With C++17 I can get it down to a single type, no helpers:

#include &lt;iostream&gt;
template&lt; template&lt;typename, class&gt; class streamtmp, typename charT, class traits &gt;
struct fmtstash {
    typedef streamtmp&lt;charT,traits&gt; streamtype;
    streamtype &amp;from;
    std::ios_base::fmtflags flags;
    std::streamsize width;
    std::streamsize precision;
    charT fill;
    fmtstash(streamtype &amp;str)
        : from(str), flags(str.flags()), width(str.width())
        , precision(str.precision()), fill(str.fill())  {}
    ~fmtstash() { from.flags(flags), from.width(width), from.precision(precision), from.fill(fill); }
    template&lt;typename T&gt; streamtype  &amp;operator&lt;&lt;(const T &amp;rhs) { return from &lt;&lt; rhs; }
    template&lt;typename T&gt; streamtype  &amp;operator&gt;&gt;(      T &amp;rhs) { return from &gt;&gt; rhs; }
};

int main(int c, char **v)
{
    using namespace std;
    fmtstash(cout) &lt;&lt; hex &lt;&lt; 51901 &lt;&lt;'\n';
    cout &lt;&lt; 51901 &lt;&lt;'\n';

    return 0;
}


which produces about the easiest-to-write-and-read formatting I can manage.

Am I missing any bets here? Shorter or more robust is what I'm after.

The biggest improvement I found since I first wrote it is to use from &lt;&lt; rhs instead of the from.operator&lt;&lt;(rhs) I had, since user-defined stream insert/extractors have to be free functions and of course an explicit member callout breaks those.

I could make the template args simpler, just template&lt;class streamtmp&gt;, the full breakout is a holdover from when I had the internal reference as ios_base&lt;charT,traits&gt; &amp;from; instead of the full original type, but I ran across a broken inserter that wanted to be first or only and took an ostream reference as its left operand.

And of course the boost savers don't supply the inserter and extractor templates at all.  Is this an oversight or is there some argument against using the destroy-at-end-of-full-expression semantics this way?
One tiny improvement I'd suggest is that the stored state can all be declared const:

const std::ios_base::fmtflags flags;
const std::streamsize width;
const std::streamsize precision;
const charT fill;


There are no implications for assignability of fmtstash objects, as we already had a reference member.