title: C++ custom stream manipulator that changes next item on stream
body: <p>In C++, to print a number in hexadecimal you do this:</p>
  
  <pre><code>int num = 10;
  std::cout &lt;&lt; std::hex &lt;&lt; num; // =&gt; 'a'
  </code></pre>
  
  <p>I know I can create a manipulator that just adds stuff to the stream like so:</p>
  
  <pre><code>std::ostream&amp; windows_feed(std::ostream&amp; out)
  {
      out &lt;&lt; "\r\n";
      return out;
  }
  
  std::cout &lt;&lt; "Hello" &lt;&lt; windows_feed; // =&gt; "Hello\r\n"
  </code></pre>
  
  <p>However, how can I create a manipulator that, like 'hex', modifies items to come on the stream? As a simple example, how would I create the plusone manipulator here?:</p>
  
  <pre><code>int num2 = 1;
  std::cout &lt;&lt; "1 + 1 = " &lt;&lt; plusone &lt;&lt; num2; // =&gt; "1 + 1 = 2"
  
  // note that the value stored in num2 does not change, just its display above.
  std::cout &lt;&lt; num2; // =&gt; "1"
  </code></pre>