Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Stringstream - copying with rd_buf changes const object
    primarykey
    data
    text
    <p>Given one stringstream object, I would like to copy it to another. According to :</p> <p><a href="https://stackoverflow.com/questions/3442520/how-copy-from-one-stringstream-object-to-another-in-c">how copy from one stringstream object to another in C++?</a></p> <p>(among others), the proper way to do this is to use the <code>rdbuf()</code> member. However, this seems to cause issues, particularly if the stringstream is const.</p> <p>For example:</p> <pre><code>void DoSomething(stringstream &amp; ss) { stringstream sstmp; cout &lt;&lt; "pos 1: " &lt;&lt; ss.tellg() &lt;&lt; "\n"; sstmp &lt;&lt; ss.rdbuf(); cout &lt;&lt; "pos 2: " &lt;&lt; ss.tellg() &lt;&lt; "\n"; // sstmp.rdbuf()-&gt;pubseekpos(0, ios_base::in | ios_base::out); cout &lt;&lt; "String: " &lt;&lt; sstmp.str() &lt;&lt; "\n"; } int main(void) { stringstream ss; ss &lt;&lt; "Hello"; DoSomething(ss); DoSomething(ss); return 0; } </code></pre> <p>The output is</p> <pre><code>pos 1: 0 pos 2: 5 String: Hello pos 1: 5 pos 2: 5 String: </code></pre> <p>The first call to <code>DoSomething()</code> changed the internal pointer, and seems to do so even if the <code>stringstream</code> is const (left out here as even <code>tellg()</code> is not const). The way to remedy this is with <code>seekg()</code>, but that can't be called on const streams. I attempted another way (commented out in the code) but without success.</p> <p>So what can be done to either:</p> <ol> <li><p>Copy a (const) <code>stringstream</code> without really changing it, or</p></li> <li><p>reset the position on a const object?</p></li> </ol> <p>Note: Compiled using gcc 4.8.1 on Arch Linux</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.
 

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