title: How can I support using streams in an exception handling macro in C++?
body: <p>I have a little error function that looks like this:</p>
  <pre><code>    template&lt;typename ErrorType&gt;
      void throwError(const std::string &amp;file,
                      const std::string &amp;function,
                      unsigned int line,
                      const std::string &amp;msg = &quot;&quot;) {
          std::ostringstream errMsg;
          errMsg &lt;&lt; file &lt;&lt; &quot;:&quot; &lt;&lt; line &lt;&lt; &quot;:&quot; &lt;&lt; function &lt;&lt; &quot;:&quot;
                 &lt;&lt; &quot;\nError: &quot; &lt;&lt; msg &lt;&lt; std::endl;
          std::cerr &lt;&lt; errMsg.str();
          throw ErrorType(errMsg.str());
      }
  </code></pre>
  <p>Then I have some macros that use the function:</p>
  <pre><code>#define INVALID_ARGUMENT_ERROR(msg) throwError&lt;std::invalid_argument&gt;(__FILE__, __func__, __LINE__, msg)
  #define LOGIC_ERROR(msg) throwError&lt;std::logic_error&gt;(__FILE__, __func__, __LINE__, msg)
  </code></pre>
  <p>So I can do:</p>
  <pre><code>if (condition == bad)
      LOGIC_ERROR(&quot;you did a bad&quot;);
  </code></pre>
  <p>But this is quite invonvenient when I want to add additional information in the error message, like values of numbers for example.</p>
  <p>What would be a good way of modifying this function so that it enables me to use a stream instead of a string? So I want do be able to do:</p>
  <pre><code>if (condition == bad)
      LOGIC_ERROR(&quot;you did a bad because condition \&quot;&quot; &lt;&lt; condition &lt;&lt; &quot; != &quot; &lt;&lt; bad);
  </code></pre>
  <p>I've tried changing the <code>std::string string msg</code> to a <code>std::ostringstream</code> which does not work.</p>