<?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: I Didn&#8217;t Order That, So Why Is It On My Bill, Episode 2</title>
	<atom:link href="http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/</link>
	<description>serious code</description>
	<lastBuildDate>Sat, 31 Jul 2010 18:07:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: dv</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-906</link>
		<dc:creator>dv</dc:creator>
		<pubDate>Thu, 07 Jan 2010 15:08:15 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-906</guid>
		<description>Update: I was mistaken about the LHS-RHS-preferences. C++ always prefers the LHS version over the RHS one. For some reason, the RHS one was preferred in my tests. This means the crazy cast is indeed necessary.</description>
		<content:encoded><![CDATA[<p>Update: I was mistaken about the LHS-RHS-preferences. C++ always prefers the LHS version over the RHS one. For some reason, the RHS one was preferred in my tests. This means the crazy cast is indeed necessary.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dv</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-905</link>
		<dc:creator>dv</dc:creator>
		<pubDate>Thu, 07 Jan 2010 15:01:58 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-905</guid>
		<description>Please note that your code makes a big mistake: it always invokes the LHS version of the [] operator. In other words, the expression &quot;the_copy[0];&quot; is NOT a simple read. You should use &quot;char c = the_copy[0];&quot; instead, since this uses the RHS operator. This one cannot write, so it does not have to copy.

LHS version: char &amp; operator[](size_type const index)
RHS version: char const &amp; operator[](size_type const index) const

LHS examples:
the_copy[0];
the_copy[0] = &#039;a&#039;;

RHS example:
std::cout &lt;&lt; the_copy[0];

What &quot;const_cast(the_base)[0];&quot; does is simply force the_base to be const, so that the only matching [] operator is the RHS one.

As for COW in gcc 4.x, it does exist. Just look into the basic_string header. However, they use atomic operations instead of mutexes/critical sections.</description>
		<content:encoded><![CDATA[<p>Please note that your code makes a big mistake: it always invokes the LHS version of the [] operator. In other words, the expression &#8220;the_copy[0];&#8221; is NOT a simple read. You should use &#8220;char c = the_copy[0];&#8221; instead, since this uses the RHS operator. This one cannot write, so it does not have to copy.</p>
<p>LHS version: char &amp; operator[](size_type const index)<br />
RHS version: char const &amp; operator[](size_type const index) const</p>
<p>LHS examples:<br />
the_copy[0];<br />
the_copy[0] = &#8216;a&#8217;;</p>
<p>RHS example:<br />
std::cout &lt;&lt; the_copy[0];</p>
<p>What &quot;const_cast(the_base)[0];&#8221; does is simply force the_base to be const, so that the only matching [] operator is the RHS one.</p>
<p>As for COW in gcc 4.x, it does exist. Just look into the basic_string header. However, they use atomic operations instead of mutexes/critical sections.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-872</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 23 Sep 2009 23:07:42 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-872</guid>
		<description>I understand that C++0x adds some things to strings so that CoW will no longer be feasible.</description>
		<content:encoded><![CDATA[<p>I understand that C++0x adds some things to strings so that CoW will no longer be feasible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ridiculous_fish</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-871</link>
		<dc:creator>ridiculous_fish</dc:creator>
		<pubDate>Fri, 18 Sep 2009 19:36:39 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-871</guid>
		<description>Heh, didn&#039;t even realize I was justifying.  Left-align, activate!</description>
		<content:encoded><![CDATA[<p>Heh, didn&#8217;t even realize I was justifying.  Left-align, activate!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leonardo Boiko</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-870</link>
		<dc:creator>Leonardo Boiko</dc:creator>
		<pubDate>Fri, 18 Sep 2009 19:26:19 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-870</guid>
		<description>Justification simply doesn’t work on the Web.  Good justification needs sophisticated hyphenation algorithms, language-specific information, and mechanisms for fine-tuning, a la LaTeX.  Our current crop of *standards* don’t support any of that, never mind the browsers.  One should just use left-align, at least until 2077 when CSS3 decides to define hyphenation support.  Otherwise, you end up with awfully stretched inter-word spacing or even intra-word spacing, depending on your window size, font, and browser.</description>
		<content:encoded><![CDATA[<p>Justification simply doesn’t work on the Web.  Good justification needs sophisticated hyphenation algorithms, language-specific information, and mechanisms for fine-tuning, a la LaTeX.  Our current crop of *standards* don’t support any of that, never mind the browsers.  One should just use left-align, at least until 2077 when CSS3 decides to define hyphenation support.  Otherwise, you end up with awfully stretched inter-word spacing or even intra-word spacing, depending on your window size, font, and browser.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-868</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Fri, 18 Sep 2009 10:07:48 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-868</guid>
		<description>You fail. This is what const_iterators are for.</description>
		<content:encoded><![CDATA[<p>You fail. This is what const_iterators are for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rmn</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-867</link>
		<dc:creator>rmn</dc:creator>
		<pubDate>Fri, 18 Sep 2009 08:47:28 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-867</guid>
		<description>Michael Pyne, this code will not achieve the desired behaviour. C++ does not choose which function to invoke by inspecting the context; it does not matter that the return value is conse char&amp; and not just char&amp;. The invoked function will be the non const operator[] (and not operator[] const), since the string itself is not const.
What you could do to get the desired behavior is use the code in Anonymous&#039;s comment, which makes the call to operator[] through a const string.

Regards,
Roman.</description>
		<content:encoded><![CDATA[<p>Michael Pyne, this code will not achieve the desired behaviour. C++ does not choose which function to invoke by inspecting the context; it does not matter that the return value is conse char&amp; and not just char&amp;. The invoked function will be the non const operator[] (and not operator[] const), since the string itself is not const.<br />
What you could do to get the desired behavior is use the code in Anonymous&#8217;s comment, which makes the call to operator[] through a const string.</p>
<p>Regards,<br />
Roman.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Pyne</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-866</link>
		<dc:creator>Michael Pyne</dc:creator>
		<pubDate>Fri, 18 Sep 2009 00:49:11 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-866</guid>
		<description>Another method which is common (and which I&#039;m surprised didn&#039;t work when I tested it) is if you want only a read-only reference, to use a const return variable.  i.e.:

const char&amp; first = the_base[2];

This allows the string class to use it&#039;s const member function
const char&amp; operator[](int) const;

to fulfill the request with the assurance that the recipient of the char won&#039;t do anything tricky.  GNU STL doesn&#039;t seem to take advantage of that here though, as this case is still as slow as the other COW-breakers.

(I mention this because it&#039;s used all the time in Qt, where it works as designed. ;)</description>
		<content:encoded><![CDATA[<p>Another method which is common (and which I&#8217;m surprised didn&#8217;t work when I tested it) is if you want only a read-only reference, to use a const return variable.  i.e.:</p>
<p>const char&amp; first = the_base[2];</p>
<p>This allows the string class to use it&#8217;s const member function<br />
const char&amp; operator[](int) const;</p>
<p>to fulfill the request with the assurance that the recipient of the char won&#8217;t do anything tricky.  GNU STL doesn&#8217;t seem to take advantage of that here though, as this case is still as slow as the other COW-breakers.</p>
<p>(I mention this because it&#8217;s used all the time in Qt, where it works as designed. <img src='http://ridiculousfish.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rmn</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-865</link>
		<dc:creator>rmn</dc:creator>
		<pubDate>Thu, 17 Sep 2009 21:15:44 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-865</guid>
		<description>Hi fish,
As far as I know, the standard does not deman std::string to use COW - that&#039;s just what your implementation does. That&#039;s why the interface is that way. It&#039;s alot more user friendly as well.

Thanks for the reply.</description>
		<content:encoded><![CDATA[<p>Hi fish,<br />
As far as I know, the standard does not deman std::string to use COW &#8211; that&#8217;s just what your implementation does. That&#8217;s why the interface is that way. It&#8217;s alot more user friendly as well.</p>
<p>Thanks for the reply.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tod(D)</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/comment-page-1/#comment-864</link>
		<dc:creator>tod(D)</dc:creator>
		<pubDate>Thu, 17 Sep 2009 20:53:55 +0000</pubDate>
		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161#comment-864</guid>
		<description>As others have said, copy-on-write performs terribly if you try to make it thread safe (and who wants a low level thing like std::string that isn&#039;t threadsafe?), one of the reasons it has dropped from most standard libraries and effectively rendered illegal in c++0x (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2668.htm).

If you need to accomplish fast, low overhead copies, the flyweight pattern (http://www.boost.org/doc/libs/1_40_0/libs/flyweight/doc/index.html) performs better and is far more extensible than just rendering all strings immutable.</description>
		<content:encoded><![CDATA[<p>As others have said, copy-on-write performs terribly if you try to make it thread safe (and who wants a low level thing like std::string that isn&#8217;t threadsafe?), one of the reasons it has dropped from most standard libraries and effectively rendered illegal in c++0x (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2668.htm" rel="nofollow">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2668.htm</a>).</p>
<p>If you need to accomplish fast, low overhead copies, the flyweight pattern (<a href="http://www.boost.org/doc/libs/1_40_0/libs/flyweight/doc/index.html" rel="nofollow">http://www.boost.org/doc/libs/1_40_0/libs/flyweight/doc/index.html</a>) performs better and is far more extensible than just rendering all strings immutable.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
