title: How does QDebug() << stuff; add a newline automatically?
body: <p>I'm trying to implement my own <code>qDebug()</code> style debug-output stream, this is basically what I have so far:</p>
  
  <pre><code>struct debug
  {
  #if defined(DEBUG)
      template&lt;typename T&gt;
      std::ostream&amp; operator&lt;&lt;(T const&amp; a) const
      {
          std::cout &lt;&lt; a;
          return std::cout;
      }
  #else
      template&lt;typename T&gt;
      debug const&amp; operator&lt;&lt;(T const&amp;) const
      {
          return *this;
      }
  
      /* must handle manipulators (endl) separately:
       * manipulators are functions that take a stream&amp; as argument and return a
       * stream&amp;
       */
      debug const&amp; operator&lt;&lt;(std::ostream&amp; (*manip)(std::ostream&amp;)) const
      {
          // do nothing with the manipulator
          return *this;
      }
  #endif
  };
  </code></pre>
  
  <p>Typical usage:</p>
  
  <pre><code>debug() &lt;&lt; "stuff" &lt;&lt; "more stuff" &lt;&lt; std::endl;
  </code></pre>
  
  <p>But I'd like not to have to add std::endl;</p>
  
  <p>My question is basically, how can I tell when the return type of <code>operator&lt;&lt;</code> isn't going to be used by another <code>operator&lt;&lt;</code> (and so append <code>endl</code>)?</p>
  
  <p>The only way I can think of to achieve anything like this would be to create a list of things to print with associated with each temporary object created by <code>qDebug()</code>, then to print everything, along with trailing newline (and I could do clever things like inserting spaces) in <code>~debug()</code>, but obviously this is not ideal since I don't have a guarantee that the temporary object is going to be destroyed until the end of the scope (or do I?).</p>