title: Turning temporary stringstream to c_str() in single statement
body: <p>Consider the following function:</p>
  
  <pre><code>void f(const char* str);
  </code></pre>
  
  <p>Suppose I want to generate a string using stringstream and pass it to this function.  If I want to do it in one statement, I might try:</p>
  
  <pre><code>f((std::ostringstream() &lt;&lt; "Value: " &lt;&lt; 5).str().c_str()); // error
  </code></pre>
  
  <p>This gives an error: 'str()' is not a member of 'basic_ostream'.  OK, so operator&lt;&lt; is returning ostream instead of ostringstream - how about casting it back to an ostringstream?</p>
  
  <p>1) Is this cast safe?</p>
  
  <pre><code>f(static_cast&lt;std::ostringstream&amp;&gt;(std::ostringstream() &lt;&lt; "Value: " &lt;&lt; 5).str().c_str()); // incorrect output
  </code></pre>
  
  <p>Now with this, it turns out for the operator&lt;&lt;("Value: ") call, it's actually calling ostream's operator&lt;&lt;(void*) and printing a hex address.  This is wrong, I want the text.</p>
  
  <p>2) Why does operator&lt;&lt; on the temporary std::ostringstream() call the ostream operator?  Surely the temporary has a type of 'ostringstream' not 'ostream'?</p>
  
  <p>I can cast the temporary to force the correct operator call too!</p>
  
  <pre><code>f(static_cast&lt;std::ostringstream&amp;&gt;(static_cast&lt;std::ostringstream&amp;&gt;(std::ostringstream()) &lt;&lt; "Value: " &lt;&lt; 5).str().c_str());
  </code></pre>
  
  <p><em>This appears to work</em> and passes "Value: 5" to f().</p>
  
  <p>3) Am I relying on undefined behavior now?  The casts look unusual.</p>
  
  <hr>
  
  <p>I'm aware the best alternative is something like this:</p>
  
  <pre><code>std::ostringstream ss;
  ss &lt;&lt; "Value: " &lt;&lt; 5;
  f(ss.str().c_str());
  </code></pre>
  
  <p>...but I'm interested in the behavior of doing it in one line.  Suppose someone wanted to make a (dubious) macro:</p>
  
  <pre><code>#define make_temporary_cstr(x) (static_cast&lt;std::ostringstream&amp;&gt;(static_cast&lt;std::ostringstream&amp;&gt;(std::ostringstream()) &lt;&lt; x).str().c_str())
  
  // ...
  
  f(make_temporary_cstr("Value: " &lt;&lt; 5));
  </code></pre>
  
  <p>Would this function as expected?</p>