Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've all pretty much nailed this already. But it's a little challenging to follow. So let me take a stab at summarizing what you've said...</p> <hr> <p>That difficulties here are that:</p> <ul> <li><p>We are playing with a temporary <code>ostringstream</code> object, so taking addresses is contra-indicated.</p></li> <li><p>Because it's a temporary, we cannot trivially convert to an <code>ostream</code> object through casting.</p></li> <li><p>Both the constructor [obviously] and <code>str()</code> are class <code>ostringstream</code> methods. (Yes, we need to use <code>.str()</code>. Using the <code>ostringstream</code> object directly would wind up invoking <code>ios::operator void*()</code>, returning a pointer-like good/bad value and not a string object.)</p></li> <li><p><code>operator&lt;&lt;(...)</code> exists as both inherited <code>ostream</code> methods and global functions. In all cases it returns an <code>ostream&amp;</code> reference.</p></li> <li><p>The choices here for <code>ostringstream()&lt;&lt;"foo"</code> are the inherited method <code>ostream::operator&lt;&lt;(void* )</code> and the global function <code>operator&lt;&lt;(ostream&amp;,const char* )</code>. The inherited <code>ostream::operator&lt;&lt;(void* )</code> wins out because we can't convert to an <code>ostream</code> object reference to invoke the global function. [Kudos to <em>coppro</em>!]</p></li> </ul> <hr> <p>So, to pull this off, we need to:</p> <ul> <li>Allocate a temporary <code>ostringstream</code>.</li> <li>Convert it to an <code>ostream</code>.</li> <li>Append data.</li> <li>Convert it back to an <code>ostringstream</code>.</li> <li>And invoke <code>str()</code>.</li> </ul> <hr> <p><strong><em>Allocating:</em></strong> <code>ostringstream()</code>.</p> <p><strong><em>Converting:</em></strong> There are several choices. Others have suggested:</p> <ul> <li><code>ostringstream() &lt;&lt; std::string() // Kudos to *David Norman*</code></li> <li><code>ostringstream() &lt;&lt; std::dec // Kudos to *cadabra*</code></li> </ul> <p><strong><em>Or we could use:</em></strong></p> <ul> <li><code>ostringstream() . seekp( 0, ios_base::cur )</code></li> <li><code>ostringstream() . write( "", 0 )</code></li> <li><code>ostringstream() . flush()</code></li> <li><code>ostringstream() &lt;&lt; flush</code></li> <li><code>ostringstream() &lt;&lt; nounitbuf</code></li> <li><code>ostringstream() &lt;&lt; unitbuf</code></li> <li><code>ostringstream() &lt;&lt; noshowpos</code></li> <li>Or any other standard manipulator. [<code>#include &lt;iomanip&gt;</code>] <a href="http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C.html" rel="noreferrer">Reference: See <em>"Insert data with format"</em> 1/3 of the way down on this webpage.</a></li> </ul> <p><strong><em>We cannot use:</em></strong></p> <ul> <li><strike><code>operator&lt;&lt;( ostringstream(), "" )</code></strike></li> <li><strike><code>(ostream &amp;) ostringstream()</code></strike></li> </ul> <p><strong><em>Appending:</em></strong> Straightforward now.</p> <p><strong><em>Converting back:</em></strong> We could just use <code>(ostringstream&amp;)</code>. But a <code>dynamic_cast</code> would be safer. In the unlikely event <code>dynamic_cast</code> returned <code>NULL</code> (it shouldn't), the following <code>.str()</code> will trigger a coredump.</p> <p><strong><em>Invoking <code>str()</code>:</em></strong> Guess.</p> <hr> <p>Putting it all together.</p> <pre><code>#define FORMAT(ITEMS) \ ( ( dynamic_cast&lt;ostringstream &amp;&gt; ( \ ostringstream() . seekp( 0, ios_base::cur ) &lt;&lt; ITEMS ) \ ) . str() ) </code></pre> <hr> <p>References:</p> <ul> <li><a href="http://www.cplusplus.com/reference/iostream/" rel="noreferrer">IOstream Library</a></li> <li><a href="http://www.cplusplus.com/reference/iostream/ostringstream/" rel="noreferrer"><code>ostringstream</code></a></li> <li><a href="http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/" rel="noreferrer"><code>ostream::operator&lt;&lt;()</code></a> <br><br></li> <li><a href="http://www.cplusplus.com/doc/tutorial/typecasting.html" rel="noreferrer">Type Casting Tutorial</a></li> <li><a href="http://en.wikibooks.org/wiki/C%2B%2B_Programming/Type_Casting" rel="noreferrer">Wiki: Type Casting</a></li> </ul> <p>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload