<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Where is the sanity in the C++ std library?</title>
	<atom:link href="http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/</link>
	<description>Just another Blog.mozilla.com weblog</description>
	<lastBuildDate>Wed, 11 Nov 2009 17:56:44 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Luke Wagner</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18131</link>
		<dc:creator>Luke Wagner</dc:creator>
		<pubDate>Wed, 09 Jul 2008 06:13:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18131</guid>
		<description>That is also odd.  It appears that stringstream::str() has been defined to be like  fstream::open(): both change the contents of the underlying buffer, and both do not modify the &#039;put&#039; pointer unless the at-end bit is set.  Thus, the following gives the desired results:

stringstream ss(ios_base::in &#124; ios_base::out &#124; ios_base::ate);
ss.str(&quot;foo&quot;);

So really, all this confusion has been caused by the fact that ios_base::ate is not a default flag for ostreams.  I wonder if there is a good reason not to have it set, because it seems more intuitive.</description>
		<content:encoded><![CDATA[<p>That is also odd.  It appears that stringstream::str() has been defined to be like  fstream::open(): both change the contents of the underlying buffer, and both do not modify the &#8216;put&#8217; pointer unless the at-end bit is set.  Thus, the following gives the desired results:</p>
<p>stringstream ss(ios_base::in | ios_base::out | ios_base::ate);<br />
ss.str(&#8221;foo&#8221;);</p>
<p>So really, all this confusion has been caused by the fact that ios_base::ate is not a default flag for ostreams.  I wonder if there is a good reason not to have it set, because it seems more intuitive.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tglek</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18123</link>
		<dc:creator>tglek</dc:creator>
		<pubDate>Tue, 08 Jul 2008 23:34:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18123</guid>
		<description>Luke,
This seems somewhat reasonable, but as I put in the update, same behavior occurs with stringstream ss;ss.str(&quot;foo&quot;). That doesn&#039;t seem right as there are no more flags to change the behavior</description>
		<content:encoded><![CDATA[<p>Luke,<br />
This seems somewhat reasonable, but as I put in the update, same behavior occurs with stringstream ss;ss.str(&#8221;foo&#8221;). That doesn&#8217;t seem right as there are no more flags to change the behavior</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke Wagner</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18122</link>
		<dc:creator>Luke Wagner</dc:creator>
		<pubDate>Tue, 08 Jul 2008 23:27:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18122</guid>
		<description>Ah, that is confusing.  The issue isn&#039;t with stringstream::str() at all, but with the stringstream constructor.  If you run:

stringstream s1(&quot;foo&quot;), s2;
s2 &lt;&lt; &quot;foo&quot;;
cout &lt;&lt; s1.tellp() &lt;&lt; &#039;,&#039; &lt;&lt; s2.tellp() &lt;&lt; &#039;\n&#039;;

you can see the discrepancy: the &#039;put&#039; pointer is being set to the first character by the constructor.  

I thought this might be a bug, but 27.7.1.1 explicitly says this is the what should happen *unless* the ios_base::ate (at-end) bit is set in the constructor args.  (The default is ios_base::in &#124; ios_base::out).  In retrospect, this makes sense because it is the same behavior you would get when you open a fstream for output.  Thus, the following constructor call gives you achieves the intended goal of the snippet in the post:

stringstream ss(&quot;foo&quot;, ios_base::in &#124; ios_base::out &#124; ios_base::ate);</description>
		<content:encoded><![CDATA[<p>Ah, that is confusing.  The issue isn&#8217;t with stringstream::str() at all, but with the stringstream constructor.  If you run:</p>
<p>stringstream s1(&#8221;foo&#8221;), s2;<br />
s2 &lt;&lt; &#8220;foo&#8221;;<br />
cout &lt;&lt; s1.tellp() &lt;&lt; &#8216;,&#8217; &lt;&lt; s2.tellp() &lt;&lt; &#8216;\n&#8217;;</p>
<p>you can see the discrepancy: the &#8216;put&#8217; pointer is being set to the first character by the constructor.  </p>
<p>I thought this might be a bug, but 27.7.1.1 explicitly says this is the what should happen *unless* the ios_base::ate (at-end) bit is set in the constructor args.  (The default is ios_base::in | ios_base::out).  In retrospect, this makes sense because it is the same behavior you would get when you open a fstream for output.  Thus, the following constructor call gives you achieves the intended goal of the snippet in the post:</p>
<p>stringstream ss(&#8221;foo&#8221;, ios_base::in | ios_base::out | ios_base::ate);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nico</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18120</link>
		<dc:creator>Nico</dc:creator>
		<pubDate>Tue, 08 Jul 2008 20:24:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18120</guid>
		<description>Joe, excellent point. The correct explanation is that stringstream&#039;s constructor does initialize its buffer with the constructor&#039;s argument (&quot;foo&quot;) but does not set the seek position to something else than 0. This is completely independent of str(), and my previous explanation was wrong.

See also http://codepad.org/eW0uqVZD .</description>
		<content:encoded><![CDATA[<p>Joe, excellent point. The correct explanation is that stringstream&#8217;s constructor does initialize its buffer with the constructor&#8217;s argument (&#8221;foo&#8221;) but does not set the seek position to something else than 0. This is completely independent of str(), and my previous explanation was wrong.</p>
<p>See also <a href="http://codepad.org/eW0uqVZD" rel="nofollow">http://codepad.org/eW0uqVZD</a> .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tglek</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18119</link>
		<dc:creator>tglek</dc:creator>
		<pubDate>Tue, 08 Jul 2008 20:21:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18119</guid>
		<description>Nico,
Thank you for the excellent answer. Seems odd that str(string&amp;) doesn&#039;t imply a .seekp, but I&#039;ll live with that now that you&#039;ve uncovered the reason for this mess.</description>
		<content:encoded><![CDATA[<p>Nico,<br />
Thank you for the excellent answer. Seems odd that str(string&amp;) doesn&#8217;t imply a .seekp, but I&#8217;ll live with that now that you&#8217;ve uncovered the reason for this mess.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18118</link>
		<dc:creator>Joe</dc:creator>
		<pubDate>Tue, 08 Jul 2008 20:11:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18118</guid>
		<description>But Nico,

str() is called after the initial operator&lt;&lt; as well. Shouldn&#039;t that also reset the stream position to the beginning?</description>
		<content:encoded><![CDATA[<p>But Nico,</p>
<p>str() is called after the initial operator&lt;&lt; as well. Shouldn&#8217;t that also reset the stream position to the beginning?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nico</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18115</link>
		<dc:creator>Nico</dc:creator>
		<pubDate>Tue, 08 Jul 2008 19:56:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18115</guid>
		<description>Dear lazyglek,

str() resets the stream position. Add

    ss.seekp(0, ios_base::end);

after a call to str() to reset te stream position back to the end: http://codepad.org/TBd4o9aT</description>
		<content:encoded><![CDATA[<p>Dear lazyglek,</p>
<p>str() resets the stream position. Add</p>
<p>    ss.seekp(0, ios_base::end);</p>
<p>after a call to str() to reset te stream position back to the end: <a href="http://codepad.org/TBd4o9aT" rel="nofollow">http://codepad.org/TBd4o9aT</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tglek</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18112</link>
		<dc:creator>tglek</dc:creator>
		<pubDate>Tue, 08 Jul 2008 19:14:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18112</guid>
		<description>jmdesp,
you suspect wrong :)</description>
		<content:encoded><![CDATA[<p>jmdesp,<br />
you suspect wrong <img src='http://blog.mozilla.com/tglek/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jmdesp</title>
		<link>http://blog.mozilla.com/tglek/2008/07/08/where-is-the-sanity-in-the-c-std-library/comment-page-1/#comment-18111</link>
		<dc:creator>jmdesp</dc:creator>
		<pubDate>Tue, 08 Jul 2008 19:11:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mozilla.com/tglek/?p=69#comment-18111</guid>
		<description>I strongly suspect

string s(&quot;foo&quot;);
stringstream ss(s);

would give you the expected behaviour.

Do you see where the difference lies ?

Yes, it would probably make more sense if &quot;ss &lt;&lt; “bar”;&quot; did throw an exception in the original version.</description>
		<content:encoded><![CDATA[<p>I strongly suspect</p>
<p>string s(&#8221;foo&#8221;);<br />
stringstream ss(s);</p>
<p>would give you the expected behaviour.</p>
<p>Do you see where the difference lies ?</p>
<p>Yes, it would probably make more sense if &#8220;ss &lt;&lt; “bar”;&#8221; did throw an exception in the original version.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
