<?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"
	>
<channel>
	<title>Comments on: Optimizing with Const Member Functions</title>
	<atom:link href="http://mark.santaniello.com/archives/89/feed" rel="self" type="application/rss+xml" />
	<link>http://mark.santaniello.com/archives/89</link>
	<description>the body of a very slow loop</description>
	<pubDate>Thu, 04 Dec 2008 00:37:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Const Constructors at mark++</title>
		<link>http://mark.santaniello.com/archives/89#comment-36215</link>
		<dc:creator>Const Constructors at mark++</dc:creator>
		<pubDate>Fri, 13 Jul 2007 17:14:03 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=89#comment-36215</guid>
		<description>[...] sure you&#8217;re not &#8212; you may have seen my infamous &#8220;craptimizing&#8221; articles: one and two. Read them if you like, but the short story is that I thought I had discovered a neat way [...]</description>
		<content:encoded><![CDATA[<p>[...] sure you&#8217;re not &#8212; you may have seen my infamous &#8220;craptimizing&#8221; articles: one and two. Read them if you like, but the short story is that I thought I had discovered a neat way [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bheeshmar</title>
		<link>http://mark.santaniello.com/archives/89#comment-171</link>
		<dc:creator>Bheeshmar</dc:creator>
		<pubDate>Sat, 10 Sep 2005 04:15:11 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=89#comment-171</guid>
		<description>I forgot to mention the most common genuine use of this feature: returning iterators.

Calling begin() on a const vector returns a const_iterator and calling begin() on a non-const vector returns a plain-old iterator.

That's overload resolution I can get behind!</description>
		<content:encoded><![CDATA[<p>I forgot to mention the most common genuine use of this feature: returning iterators.</p>
<p>Calling begin() on a const vector returns a const_iterator and calling begin() on a non-const vector returns a plain-old iterator.</p>
<p>That&#8217;s overload resolution I can get behind!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://mark.santaniello.com/archives/89#comment-161</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Wed, 07 Sep 2005 15:45:42 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=89#comment-161</guid>
		<description>Bheesh,

The backstory here is that I spent a few weeks building a perf test harness around boost::function.  It's working great now, but the overhead (measuring the perf of a call to a "null" function) is too high.  I started looking into what was happening when you invoke operator()() on a const boost::function object, and it turns out that there is an "if(!initialized)" check very similar to this example.

So I think it's mature optimization :)   But I guess that's not really the point.

As you point out, I totally missed the fact that you can still create an uninitialized const Foo.  I think that's the major problem here.

What I want is a class which can be instantiated as both const and non-const, but where all const objects must use the 2nd constructor (the one which sets data_ and initialized_ to true).

Unfortunately, I don't know of a way to do this.  Maybe I need a FooFactory or something?  I'll have to think about this for a while.

-Mark</description>
		<content:encoded><![CDATA[<p>Bheesh,</p>
<p>The backstory here is that I spent a few weeks building a perf test harness around boost::function.  It&#8217;s working great now, but the overhead (measuring the perf of a call to a &#8220;null&#8221; function) is too high.  I started looking into what was happening when you invoke operator()() on a const boost::function object, and it turns out that there is an &#8220;if(!initialized)&#8221; check very similar to this example.</p>
<p>So I think it&#8217;s mature optimization :)   But I guess that&#8217;s not really the point.</p>
<p>As you point out, I totally missed the fact that you can still create an uninitialized const Foo.  I think that&#8217;s the major problem here.</p>
<p>What I want is a class which can be instantiated as both const and non-const, but where all const objects must use the 2nd constructor (the one which sets data_ and initialized_ to true).</p>
<p>Unfortunately, I don&#8217;t know of a way to do this.  Maybe I need a FooFactory or something?  I&#8217;ll have to think about this for a while.</p>
<p>-Mark</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bheeshmar</title>
		<link>http://mark.santaniello.com/archives/89#comment-160</link>
		<dc:creator>Bheeshmar</dc:creator>
		<pubDate>Wed, 07 Sep 2005 02:35:34 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=89#comment-160</guid>
		<description>Beware premature optimization.

You are sacrificing semantic clarity for a false optimization.  Just because you *can* do something doesn't mean you should.

BTW, your example is flawed; I can clearly write this (nothing prevents misuse):
const Foo f;
f.fubarize(); // "Called fast fubarize" but it wasn't initialized (and can never be)!

The bigger problem I have is that your example isn't using an accessor:  it returns void!  A better example may be a lazy initialization example (ala caching database queries), but all you save is the query and I'd still do that by making the cached value mutable.

A better optimization for your case is to remove the default constructor for Foo and force clients to provide a valid value!</description>
		<content:encoded><![CDATA[<p>Beware premature optimization.</p>
<p>You are sacrificing semantic clarity for a false optimization.  Just because you *can* do something doesn&#8217;t mean you should.</p>
<p>BTW, your example is flawed; I can clearly write this (nothing prevents misuse):<br />
const Foo f;<br />
f.fubarize(); // &#8220;Called fast fubarize&#8221; but it wasn&#8217;t initialized (and can never be)!</p>
<p>The bigger problem I have is that your example isn&#8217;t using an accessor:  it returns void!  A better example may be a lazy initialization example (ala caching database queries), but all you save is the query and I&#8217;d still do that by making the cached value mutable.</p>
<p>A better optimization for your case is to remove the default constructor for Foo and force clients to provide a valid value!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://mark.santaniello.com/archives/89#comment-152</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Fri, 02 Sep 2005 23:53:13 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=89#comment-152</guid>
		<description>Kev,

I just tried a global static const object with a mutable member, and it seems to work.  I also tried the old const_cast(this) trick, and that works too.

Either you guys are smart enough to detect the const_cast, or you never stick static const global objects into rdata.  

I'm guessing the latter.

I'll probably make a blog post out of this...one day :)

-Mark</description>
		<content:encoded><![CDATA[<p>Kev,</p>
<p>I just tried a global static const object with a mutable member, and it seems to work.  I also tried the old const_cast(this) trick, and that works too.</p>
<p>Either you guys are smart enough to detect the const_cast, or you never stick static const global objects into rdata.  </p>
<p>I&#8217;m guessing the latter.</p>
<p>I&#8217;ll probably make a blog post out of this&#8230;one day :)</p>
<p>-Mark</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Frei</title>
		<link>http://mark.santaniello.com/archives/89#comment-151</link>
		<dc:creator>Kevin Frei</dc:creator>
		<pubDate>Fri, 02 Sep 2005 22:39:14 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=89#comment-151</guid>
		<description>I'll bet that mutable will cause some serious problems with most compilers if there are global const objects - they'll get stuck in read only data, and then the mutable member will make the code puke :-)</description>
		<content:encoded><![CDATA[<p>I&#8217;ll bet that mutable will cause some serious problems with most compilers if there are global const objects - they&#8217;ll get stuck in read only data, and then the mutable member will make the code puke :-)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
