title: Creating ostream manipulators for a specific class
body: <p>I have a class that is derived from ostream:</p>
  
  <pre><code>class my_ostream: public std::ostream
  {
      // ...
  }
  </code></pre>
  
  <p>I want to make a manipulator (for example <code>do_something</code>), that works specifically to this class, like this:</p>
  
  <pre><code>my_ostream s;
  s &lt;&lt; "some text" &lt;&lt; do_something &lt;&lt; "some more text";
  </code></pre>
  
  <p>I did the following:</p>
  
  <pre><code>std::ostream &amp;do_something(std::ostream &amp;os)
  {
      my_ostream *s = dynamic_cast&lt;my_ostream*&gt;(&amp;os);
      if (s != NULL)
      {
          // do something
      }
      return os;
  }
  </code></pre>
  
  <p>This works, but is rather ugly. I tried the following:</p>
  
  <pre><code>my_ostream &amp;do_something(my_ostream &amp;s)
  {
      // do something
      return s;
  }
  </code></pre>
  
  <p>This doesn't work. I also tried another approach:</p>
  
  <pre><code>class my_ostream: public std::ostream
  {
     // ...
     my_ostream &amp;operator&lt;&lt;(const do_something &amp; x)
     {
         // do something
         return *this;
     }
  }
  </code></pre>
  
  <p>This still doesn't work.</p>