<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5084302589052586355</id><updated>2011-07-30T21:23:54.566-07:00</updated><category term='garbage'/><category term='outlook'/><category term='programming?'/><category term='emacs'/><category term='java'/><category term='haskell'/><category term='programming'/><category term='boost'/><category term='WTF'/><category term='windows'/><category term='lisp'/><category term='c++'/><category term='bacon'/><title type='text'>placeholder</title><subtitle type='html'>I'll figure out a better title later</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-6494123617834197044</id><published>2010-04-27T22:18:00.000-07:00</published><updated>2010-04-27T22:19:04.986-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>Sometimes My Const is None of Your Business</title><content type='html'>&lt;p&gt;There seems to be a relative dearth of C++ related blogs out there, at least compared to the &lt;em&gt;webby&lt;/em&gt; world of Java and Ruby and the like. One that I have found to be consistently good is &lt;a href="http://www.informit.com/guides/guide.aspx?g=cplusplus"&gt;The C++ Reference Guide&lt;/a&gt;, though it is debatable if it can really be considered a blog. As the name implies it's a &lt;em&gt;reference guide&lt;/em&gt; for C++, but as it is in the process of being created it has a RSS feed that gets updated as new topics are added. There's also a bit more commentary in it than one would normally expect from a reference guide. Given these two qualities I'm considering it a blog, even if that's not strictly accurate.&lt;/p&gt;&lt;p&gt;Now while I did say that the &lt;em&gt;Guide&lt;/em&gt; is consistently good, I do find fault with it from time to time. The most recent case would be &lt;a href="http://www.informit.com/guides/content.aspx?g=cplusplus&amp;amp;seqNum=246"&gt;the following&lt;/a&gt;:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Declaring value arguments as const is a close relative of the first gaffe. Raise your hand if you haven't seen a hyper-corrective (yet utterly wrong) function declaration of this kind:&lt;/p&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;p&gt;void doStuff(const int action_code);&lt;/p&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;p&gt;Programmers who declare a value argument as const don't realize that by doing so, they expose themselves to mockery. Even if their aim is to document the fact that the function isn't allowed to modify its argument, declaring a value argument const is redundant and wrong. Remember: the const in this context applies to a local copy of the argument passed to the callee. It doesn't apply to the caller's argument. Hence, declaring value arguments as const is always a bad idea.&lt;/p&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;p&gt;Bottom line: never declare value arguments as const. Although it might seem harmless, it's plain wrong, and it might suggest that you don't understand the C++ argument passing mechanism at all.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Let me start by saying that I declare &lt;em&gt;const by value&lt;/em&gt; arguments all the time when the type being passed is a primitive like &lt;strong&gt;int&lt;/strong&gt;&lt;a href="#fn1" class="footnoteRef" id="fnref1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;. Now I'm pretty sure I have a decent grasp of what it means to pass a parameter by value; I understand that when a function takes a parameter by value it is making a copy of that value for its own internal use and thus the const doesn't affect the caller's variable in any way.&lt;/p&gt;&lt;p&gt;So why do it? That's none of your business, at least if you're the caller of my function. If my function takes a parameter by value then I have my own copy of the value that you passed in and I can do damn well anything I please with it including declaring it const. In this case the const is not for the caller's benefit, it's for the function author's. In my experience the more stuff you can declare const the better (stuff that can't change has less chance of being stuff that can break). The only way to make that parameter value const is to declare it as such in the function's declaration. This could probably be considered a leak of information about the function's internals by the interface, but it seems like a rather innocuous one to me. Maybe I should be demanding that the C++ standard be amended so that I can have my &lt;em&gt;const by value&lt;/em&gt; arguments without having to declare them as such in function declarations so as to avoid ridicule and mockery by the likes of the author above...&lt;/p&gt;&lt;div class="footnotes"&gt;&lt;hr /&gt;&lt;ol&gt;&lt;li id="fn1"&gt;&lt;p&gt;Note that I do not do this with anything that is not a primitive type, I'm not a moron (as far as I know, the &lt;a href="http://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect"&gt;Dunning-Kruger effect&lt;/a&gt; notwithstanding). Unfortunately the code base that I work on passed through some &lt;strong&gt;less than stellar&lt;/strong&gt; hands in the past and I see lots of functions that take arguments like &lt;strong&gt;const CString foo&lt;/strong&gt; (note the lack of ampersand). &lt;a href="#fnref1" class="footnoteBackLink" title="Jump back to footnote 1"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-6494123617834197044?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/6494123617834197044/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=6494123617834197044' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/6494123617834197044'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/6494123617834197044'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2010/04/sometimes-my-const-is-none-of-your.html' title='Sometimes My Const is None of Your Business'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-164321097878833114</id><published>2009-12-14T21:51:00.000-08:00</published><updated>2009-12-14T21:54:58.495-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='WTF'/><category scheme='http://www.blogger.com/atom/ns#' term='outlook'/><category scheme='http://www.blogger.com/atom/ns#' term='windows'/><title type='text'>This Message Has Not Been Sent</title><content type='html'>&lt;p&gt;&lt;em&gt;This message has not been sent&lt;/em&gt;. WTF Outlook? I know that the message has not been sent -- &lt;strong&gt;I'm still writing it!&lt;/strong&gt; Your endless little interruptions are growing tiresome. Like a four-year-old who's not getting enough attention you used to bug me about every little message that you got for me. &lt;em&gt;Look at me, I've got a message that, no matter what, must be more important than whatever it is that you're working on&lt;/em&gt;. I finally broke you of that habit but now you have to bug me with your little comments about the obvious &lt;strong&gt;at the top of the window&lt;/strong&gt; so that the rest of the window shifts and distracts me from the whole reason that I have your window open in the first place. Really, it just needs to stop. Plus, half the time your messages are premature; you declare messages as being replied to before I even finish writing the reply. And while we're talking, what have you done to piss off Windows? You seem to be the only program that Windows decides to &lt;strong&gt;aggressively swap&lt;/strong&gt;, to the point that every time I try to raise your window I'm greeted with ten seconds of hard-drive grinding before I can use you. What are you doing to antagonize Windows so? Thunderbird doesn't have this problem. Maybe the sysadmin would be willing to turn on the IMAP support in the exchange server...&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-164321097878833114?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/164321097878833114/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=164321097878833114' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/164321097878833114'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/164321097878833114'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2009/12/this-message-has-not-been-sent.html' title='This Message Has Not Been Sent'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-5003550111244964552</id><published>2009-02-13T22:23:00.000-08:00</published><updated>2009-02-13T22:24:22.005-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Hoops to Jump Through</title><content type='html'>Whose idea was it for C++ classes that have virtual functions to not automatically have a virtual destructor? There is just no reason for this not to be the case. Not having a virtual destructor is just not going to ever do that right thing when you're dealing with a polymorphic class. Most of the time it's probably going to be a silent problem that you're dealing with. My last encounter with this wonderful bit of the C++ obstacle course resulted in a not insignificant memory leak when I failed to jump through the hoop. I have yet to see a case where it would make sense for a polymorphic class to not a virtual destructor. Sometimes I really do think that C++ is just some colossal joke that Stroustrup is playing on us.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-5003550111244964552?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/5003550111244964552/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=5003550111244964552' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/5003550111244964552'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/5003550111244964552'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2009/02/hoops-to-jump-through.html' title='Hoops to Jump Through'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-3750680966487863453</id><published>2009-02-13T22:09:00.000-08:00</published><updated>2009-03-11T22:21:16.268-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='boost'/><title type='text'>disable_shared_from_this</title><content type='html'>&lt;p&gt;About once a year I end up thinking that using &lt;code&gt;boost::enable_shared_from_this&lt;/code&gt; is a good idea. And every time it ends up wasting almost an hour of my time. You see there's a restriction on &lt;code&gt;shared_from_this&lt;/code&gt; that disallows its use in the constructor of the object that is being &lt;em&gt;shared from this&lt;/em&gt;. Unfortunately this restriction is &lt;strong&gt;not mentioned in the documentation&lt;/strong&gt;. I always try to use &lt;code&gt;shared_from_this&lt;/code&gt; from the constructor and get &lt;em&gt;bad weak pointer&lt;/em&gt; errors at run time. I then spend twenty minutes flailing around in the source code trying to figure out what I'm doing wrong until finally a bit of googling brings the &lt;em&gt;not in the constructor&lt;/em&gt; restriction out in the open. Once I realize this and start trying to fix things so I don't need to use &lt;code&gt;shared_from_this&lt;/code&gt; in the constructor I find that I don't need &lt;code&gt;enable_shared_from_this&lt;/code&gt; at all. &lt;strong&gt;This happens every time&lt;/strong&gt;. I'm guessing that most of the boost folks have had the same experience as well since they don't go out of their way to call attention to &lt;code&gt;enable_shared_from_this&lt;/code&gt;. Maybe I should stay away from libraries whose documentation can only be reached from a link in an answer to a FAQ about another library. That is just not a good sign.&lt;/p&gt;

&lt;span style="font-weight:bold;"&gt;Update 3-11-09:&lt;/span&gt; Of course now that I've posted this I've had two occasions where &lt;code&gt;boost::enable_shared_from_this&lt;/code&gt; turned out to be useful.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-3750680966487863453?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/3750680966487863453/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=3750680966487863453' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/3750680966487863453'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/3750680966487863453'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2009/02/disablesharedfromthis.html' title='disable_shared_from_this'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-6192493365834310320</id><published>2008-09-08T22:12:00.000-07:00</published><updated>2008-09-08T22:13:47.106-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='emacs'/><title type='text'>Why Emacs?</title><content type='html'>&lt;p&gt;In the process of hacking some C# compatibility into my Emacs config I came across this &lt;a href="http://blogs.msdn.com/dotnetinterop/archive/2008/04/12/emacs-is-better-than-visual-studio-as-a-c-development-tool.aspx"&gt;page&lt;/a&gt;. The page itself is moderately interesting and another entry in the guys blog held the solution to a problem I was facing. But the thing that jumped out at me were some of the comments. The most egregious was&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;You are so lame man... you spent all that time just so you have MOST
  of the features you get for free on the C# express. lol you FAIL at
  seeing the big picture&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes, there are features in Visual Studio that Emacs lacks. I'll give him that. But then Visual Studio lacks the &lt;a href="http://steve-yegge.blogspot.com/2007/01/pinocchio-problem.html"&gt;quality&lt;/a&gt; that Emacs has. The problem is to get at this quality you can't have this poster's attitude:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The problem is that while emacs and vim will always be flexible, they &gt;require a lot of knowledge, time etc... to gather stuff.&lt;/p&gt;
  
  &lt;p&gt;And while you may know and enjoy writing elisp code, I simply refuse &gt;to learn something specifically for one program. I could live with &gt;python or ruby these days, or with a human-readable nice and easy &gt;special language, but learning lisp just so that i maximize using &gt;emacs sounds like sure overkill, especially cuz i would know i &gt;wouldnt really use lisp outside from that (sorry lispers)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The power of Emacs comes from Elisp&lt;sup class="footnote-ref" id="fnref-elisp"&gt;&lt;a href="#fn-elisp"&gt;1&lt;/a&gt;&lt;/sup&gt;. This is similar to the problem with this comment (and a few others like it)&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Hey you do know that their is an Emacs mode inside of VS 2005 and &gt;higher; just go to Tools -&gt;Options-&gt; Enviroment-&gt;Keyboard and select &gt;Emacs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's not the keyboard shortcuts that make people use Emacs. It's the control that Elisp gives you. And I don't just mean writing huge amounts of code that implement huge features like JDEE&lt;sup class="footnote-ref" id="fnref-code"&gt;&lt;a href="#fn-code"&gt;2&lt;/a&gt;&lt;/sup&gt;. It's little things. Like today I needed to insert a new function into a COM interface and then shift the id numbers of all the following functions up one. I could have just gone through and typed all the numbers but instead I just had to do a regex replace from "id(([0-9]+))" to "id(\,(+ 1 (string-to-number \1)))" and boom it was done in a split second. A lot less time, including that spent writing the regex, and way less brain numbing. And that's a pretty minor example, once you get the hang of it you start automating all sorts of little things that bug you that are too specific to be something the MS, Jetbrains or the Eclipse folks are going to bother dealing with. And, at least in my experience with Visual Studio, adding thee features to the IDE would be more pain then it is worth.&lt;/p&gt;

&lt;p&gt;Maybe the best way to put it is: do you control what your tool can do or does your tool control what you can do? &lt;sup class="footnote-ref" id="fnref-snicker"&gt;&lt;a href="#fn-snicker"&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;div class="footnotes"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn-elisp"&gt;
&lt;p&gt;Though it's funny, if the poster actually went out and learned a proper lisp like scheme or CL then the complaint would be coming form the opposite direction given the craptastic dialect that Elisp is.&amp;nbsp;&lt;a href="#fnref-elisp" class="footnoteBackLink" title="Jump back to footnote 1 in the text."&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn-code"&gt;
&lt;p&gt;Though I bet it's a lot less code than you'd write in VBScript or whatever it is that Visual Studio is using these days.&amp;nbsp;&lt;a href="#fnref-code" class="footnoteBackLink" title="Jump back to footnote 2 in the text."&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn-snicker"&gt;
&lt;p&gt;Insert Beavis and Butthead snickering here if you are so inclined.&amp;nbsp;&lt;a href="#fnref-snicker" class="footnoteBackLink" title="Jump back to footnote 3 in the text."&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-6192493365834310320?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/6192493365834310320/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=6192493365834310320' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/6192493365834310320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/6192493365834310320'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2008/09/why-emacs.html' title='Why Emacs?'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-4776771444537601498</id><published>2008-05-07T22:10:00.000-07:00</published><updated>2008-05-07T22:11:52.840-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='garbage'/><title type='text'>Blame the Management</title><content type='html'>&lt;p&gt;So I've come up with a new reason that C++'s lack of garbage collection is problematic.  The strange thing is that the reason is in one of C++'s usual sweet spots: performance.
&lt;/p&gt;
&lt;p&gt;Really I should qualify the previous statement, I'm talking about performance in multi-threaded programs where you actually want your software to work in some sane fashion as opposed to just flailing around and crashing.  In order to accomplish this you need to be making liberal use of &lt;code&gt;boost::shared_ptr&lt;/code&gt; or something similar in order to avoid having the &lt;a href="http://brett-hall.blogspot.com/2008/01/getting-rug-pulled-out-from-under-you.html"&gt;rug pulled out from under you&lt;/a&gt; on a regular basis&lt;sup id="fnr1-85313744"&gt;&lt;a href="#fn1-85313744"&gt;1&lt;/a&gt;&lt;/sup&gt;. 
&lt;/p&gt;
&lt;p&gt;Turns out that the &lt;code&gt;shared_ptr&lt;/code&gt; has a dirty little secret: copying one in a multi-threaded program is orders of magnitude slower than copying a bare pointer. The reason for this is that the &lt;code&gt;shared_ptr&lt;/code&gt; has a reference count in it.  When you copy the &lt;code&gt;shared_ptr&lt;/code&gt; that reference count needs to be incremented in a thread-safe manner.  On platforms that support it this is done using atomic integer operations, otherwise  OS synchronization primitives come into play.  Either way you're talking a lot more cycles than just a pointer copy since at minimum you need to wait for the atomic operations to maintain some semblance of processor cache sanity.
&lt;/p&gt;
&lt;p&gt;So does this mean that we abandon &lt;code&gt;shared_ptr&lt;/code&gt; in multi-threaded programs?  In C++ the alternative (bare pointers) is much, much worse so I would say no.
&lt;/p&gt;
&lt;p&gt;Now if we had a modern&lt;sup id="fnr2-85313744"&gt;&lt;a href="#fn2-85313744"&gt;2&lt;/a&gt;&lt;/sup&gt; garbage collected system we wouldn't need to sacrifice cycles to the cache sanity gods.  Instead we could copy pointers to our hearts content.  Occasionally we would need to give the garbage man some cycles so in a way the garbage collector is amortizing the cycles we need to spend on memory management.  But if well implemented this is a much lower tax to pay. In extreme circumstances you could even take control of the collector and only allow it to run when it's not going to get in the way of more important things.
&lt;/p&gt;
&lt;p&gt;So how do we mitigate things?  One option would be to hook up the &lt;a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/"&gt;Boehm garbage collector&lt;/a&gt; to our programs.  At some point I'm going to sit down and benchmark the collector versus &lt;code&gt;boost::shared_ptr&lt;/code&gt;.  But that might not be an option depending on how much legacy code we're talking about.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;If you're stuck with no garbage collection then the thing to do is minimize how often a &lt;code&gt;shared_ptr&lt;/code&gt; is being copied.  In the vast majority of cases you should take a const reference to a &lt;code&gt;shared_ptr&lt;/code&gt; as parameters.  Unless you're doing something wrong then somewhere either on the stack or &lt;em&gt;shudder&lt;/em&gt; in a global variable&lt;sup id="fnr3-85313744"&gt;&lt;a href="#fn3-85313744"&gt;3&lt;/a&gt;&lt;/sup&gt; there is an instance of the &lt;code&gt;shared_ptr&lt;/code&gt; so you shouldn't have to worry about losing the object pointed at while your function is running.  The exception to this is when passing the &lt;code&gt;shared_ptr&lt;/code&gt; to another thread.  In that case you will need to create a copy of the &lt;code&gt;shared_ptr&lt;/code&gt; for the other thread. Note that taking a const reference saves you from a double-whammy.  If you take the pointer by value then you pay for the new &lt;code&gt;shared_ptr&lt;/code&gt; instance when the function is called and again when the &lt;code&gt;shared_ptr&lt;/code&gt; instance is destroyed as the function exits.
&lt;/p&gt;
&lt;p&gt;You should also prefer plain old &lt;code&gt;dynamic_cast&lt;/code&gt; over &lt;code&gt;dynamic_pointer_cast&lt;/code&gt;.  The latter will increment the reference count on the pointer being casted.  If you have the original &lt;code&gt;shared_ptr&lt;/code&gt; then you know the object being pointed at isn't going away so just cast the underlying pointer.  Obviously if you are going to store the pointer away some where you'll have to pay for the copy.
&lt;/p&gt;
&lt;p&gt;Another thing to watch out for is locking &lt;code&gt;boost::weak_ptr&lt;/code&gt;.  You pay every time since the reference count will be locked.  If you are by chance using the pointer value without using the pointed to object all the time then consider storing the bare pointer along with the weak_pointer.  Then you can use the pointer value whenever without paying to lock but still have the object lifetime monitoring of &lt;code&gt;weak_ptr&lt;/code&gt; when you need it.
&lt;/p&gt;
&lt;p&gt; &lt;code&gt;boost::shared_ptr&lt;/code&gt; is great but be aware that it's use isn't free.  And while it isn't free the alternative is worse, as long as we're not referring to real garbage collection, which is better.
&lt;/p&gt;

&lt;div class="footnote"&gt;&lt;hr/&gt;&lt;ol&gt;
 &lt;li id="fn1-85313744"&gt;
     I suppose one could argue that by using a reference counted smart pointer one is using garbage collection.  Of course if you're driving a Model T you're driving car also.&lt;a href="#fnr1-85313744" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn2-85313744"&gt;
     read &lt;em&gt;non-reference counted&lt;/em&gt;.&lt;a href="#fnr2-85313744" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn3-85313744"&gt;
     I'm looking at you singleton.&lt;a href="#fnr3-85313744" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-4776771444537601498?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/4776771444537601498/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=4776771444537601498' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/4776771444537601498'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/4776771444537601498'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2008/05/blame-management.html' title='Blame the Management'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-3426979611594879626</id><published>2008-05-03T17:40:00.000-07:00</published><updated>2008-05-03T17:41:20.146-07:00</updated><title type='text'>It's not rocket science</title><content type='html'>&lt;p&gt;So i was listening to the &lt;a href="http://stackoverflow.com"&gt;stack overflow&lt;/a&gt; podcast and Jeff Atwood declared that he thought it was ridiculous that kids are graduating with a CS degree without being taught about source code control.  Now I agree that source code control is an important part of software engineering but it doesn't seem like a good subject for a college class.  Implementing a source code control system sounds like a good subject for a class but using one?  It's not like using a SCCS is all that hard.  Where I work we use Seapine Surround which is far from being well known but we usually have new people up to speed with it's basic use in a couple of minutes.  It's just not that hard.  Now I would look askance at someone who was supposedly &lt;em&gt;experienced&lt;/em&gt; yet had never used a source code control system, but a new grad I wouldn't really mind.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-3426979611594879626?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/3426979611594879626/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=3426979611594879626' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/3426979611594879626'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/3426979611594879626'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2008/05/its-not-rocket-science.html' title='It&apos;s not rocket science'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-6796834006027506936</id><published>2008-01-27T16:46:00.000-08:00</published><updated>2008-01-27T16:47:24.099-08:00</updated><title type='text'>Getting the rug pulled out from under you</title><content type='html'>&lt;p&gt;At some point when you start writing multi-threaded programs in C++ you come to the realization that the &lt;code&gt;this&lt;/code&gt; pointer is no longer the trustworthy soul that it once was.  Instead it has become a fair-weather friend that is just waiting for you to make that one little mistake and then it completely pulls the rug out from under you.  Consider this innocuous looking code:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void Foo::function()
{
    sleep(1000);
    ++m_variable;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;where &lt;code&gt;m_variable&lt;/code&gt; is a member variable of class &lt;code&gt;Foo&lt;/code&gt;.  Looks totally OK right?  Now consider calling &lt;code&gt;Foo::function&lt;/code&gt; in the following way:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void other_function()
{
    Foo f;
    run_in_other_thread(boost::bind(&amp;amp;Foo::function, ref(f)));
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;   &lt;br /&gt;
   If you were to trace through the call to &lt;code&gt;Foo::function&lt;/code&gt; that &lt;code&gt;run_in_other_thread&lt;/code&gt; runs in another thread you would see that it seg faults on &lt;code&gt;++m_variable&lt;/code&gt;.  How can this be? We're in a member function of a &lt;code&gt;Foo&lt;/code&gt; object, how can &lt;code&gt;m_variable&lt;/code&gt; not be valid?  The problem is that &lt;code&gt;this&lt;/code&gt; is no longer a valid pointer.  The object that &lt;code&gt;this&lt;/code&gt; points at in the call to &lt;code&gt;Foo::function&lt;/code&gt; goes out scope in the original thread invalidating &lt;code&gt;this&lt;/code&gt; as used in &lt;code&gt;Foo::function&lt;/code&gt;.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;In single-threaded code there is no danger in &lt;code&gt;Foo::function&lt;/code&gt;.  In order to call &lt;code&gt;Foo::function&lt;/code&gt; you need to have a valid reference to the object that you are calling the method on.  In the multi-threaded world all bets are off.  You no longer have any guarantee that the object that you called &lt;code&gt;Foo::function&lt;/code&gt; on still exists or if it does still exist that it will continue to exist all the way through the call to &lt;code&gt;Foo::function&lt;/code&gt;.  The object could exist on another thread's stack and when that stack frame goes away, &lt;strong&gt;BOOM&lt;/strong&gt;, out go the lights in the call to &lt;code&gt;Foo::function&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;About now you're probably cursing C++'s lack of garbage collection&lt;sup id="fnr1-539651438"&gt;&lt;a href="#fn1-539651438"&gt;1&lt;/a&gt;&lt;/sup&gt;.  In languages like Java or C# the garbage collector saves us.  In order to call &lt;code&gt;Foo::function&lt;/code&gt; you have to have a live reference to the &lt;code&gt;Foo&lt;/code&gt; object in the current thread thus the garbage collector can't get rid of it half way through the call to &lt;code&gt;Foo::function&lt;/code&gt;.  There still could be other resource allocation issues but as with most things the garbage collector takes care of about 90% of your problems.
&lt;/p&gt;
&lt;p&gt;But if we're stuck with C++ due to legacy code and adding &lt;a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/"&gt;garbage collection&lt;/a&gt; isn't an option we need to deal with this issue in some way.  The first thing that comes to mind is to only use methods in contexts where you can be sure that the object whose method is being called will outlast the method call.  When the object is on the stack and you're only using in the thread that owns that stack then you're fine.  That's a good argument for keeping as much of your state local to each thread as possible.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;There are probably a few other cases where you can reason about object lifetimes well enough to be able to say definitively that an object will outlive all of it's method calls.  But these cases are going to be rare and as with lock-based programming you are taking your life in your hands and really it would be nice to have &lt;a href="http://haskell.org/haskellwiki/Software_transactional_memory"&gt;other&lt;/a&gt; &lt;a href="http://en.wikipedia.org/wiki/Actor_model"&gt;options&lt;/a&gt; as we do for data sychronization.
&lt;/p&gt;
&lt;p&gt;When you need to start sharing state across threads you &lt;code&gt;boost::shared_ptr&lt;/code&gt; becomes a very good friend&lt;sup id="fnr2-539651438"&gt;&lt;a href="#fn2-539651438"&gt;2&lt;/a&gt;&lt;/sup&gt;.  At the very least this means that any object that is going to be shared across threads needs to be allocated on the heap and stored in a &lt;code&gt;shared_ptr&lt;/code&gt;.  Each thread should have its own &lt;code&gt;shared_ptr&lt;/code&gt; pointing at the object so you can be sure that the object will stick around as long as there are threads referencing it.  Just be sure that you create the new pointers in threads that already have a &lt;code&gt;shared_ptr&lt;/code&gt; instance of their own&lt;sup id="fnr3-539651438"&gt;&lt;a href="#fn3-539651438"&gt;3&lt;/a&gt;&lt;/sup&gt;.
&lt;/p&gt;
&lt;p&gt;So you need to be careful about separating object ownership and object access.  Being a method in a class gives a function the latter.  Object ownership is a thread-level concept, not an object or function level concept.  So you need to consider whether the thread that a function is executing in has some sort of ownership of the object that you are using.  If not then your method call could have the rug yanked out form under at any time&lt;sup id="fnr4-539651438"&gt;&lt;a href="#fn4-539651438"&gt;4&lt;/a&gt;&lt;/sup&gt;.
&lt;/p&gt;

&lt;div class="footnote"&gt;&lt;hr/&gt;&lt;ol&gt;
 &lt;li id="fn1-539651438"&gt;
     If you're not then you really should be.&lt;a href="#fnr1-539651438" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn2-539651438"&gt;
     Just be sure to respect shared_ptr's boundaries when it comes to threads.  Failure to do this will very quickly turn &lt;code&gt;boost::shared_ptr&lt;/code&gt; into a mortal enemy.&lt;a href="#fnr2-539651438" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn3-539651438"&gt;
     Just wanted to really stress that &lt;code&gt;shared_ptr&lt;/code&gt; has very specific &lt;a href="http://www.boost.org/libs/smart_ptr/shared_ptr.htm#ThreadSafety"&gt;threading issues&lt;/a&gt;  and you should really take the time to understand them before using &lt;code&gt;shared_ptr&lt;/code&gt; in a multi-threaded program.&lt;a href="#fnr3-539651438" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn4-539651438"&gt;
     Again, the lack of garbage collection should really concern you.&lt;a href="#fnr4-539651438" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-6796834006027506936?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/6796834006027506936/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=6796834006027506936' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/6796834006027506936'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/6796834006027506936'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2008/01/getting-rug-pulled-out-from-under-you.html' title='Getting the rug pulled out from under you'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-1219013799855830478</id><published>2008-01-14T22:20:00.000-08:00</published><updated>2008-01-14T22:22:23.469-08:00</updated><title type='text'>Haskell Shuffling</title><content type='html'>&lt;p&gt;After reading Jeff Atwood's &lt;a href="http://www.codinghorror.com/blog/archives/001015.html"&gt;post&lt;/a&gt; about shuffling I decided it would be interesting to implement both his naive shuffling algorithm and the &lt;a href="http://en.wikipedia.org/wiki/Knuth_shuffle"&gt;Fisher-Yates&lt;/a&gt; algorithm in Haskell. It seemed like a good little exercise and gave me a chance to check out the &lt;code&gt;ST&lt;/code&gt; monad and the various &lt;code&gt;Array&lt;/code&gt; data types in Haskell.
&lt;/p&gt;
&lt;p&gt;This post is a literate Haskell program, just copy it into a file named Shuffle.lhs and you can import the Shuffle module into any other Haskell program.  Here's the module definition and imports:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
&amp;gt; module Shuffle (naiveShuffle, fyShuffle) where
&amp;gt;
&amp;gt; import System.Random
&amp;gt; import Data.Array.ST
&amp;gt; import Data.Array.MArray 
&amp;gt; import Data.Array.IArray 
&amp;gt; import Data.Array.Unboxed 
&amp;gt; import Control.Monad.ST
&amp;gt; import Control.Monad.State
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;   So what were trying to do is take a sequence of numbers and generate a new sequence of numbers that is a random re-ordering of the initial sequence.  Since we're going to be dealing with randomly re-ordering elements of a sequence using a list would not be the greatest idea given list's O(n) performance for inserting and deleting elements. Instead we want to use an &lt;code&gt;Array&lt;/code&gt;.  To be more specific I'm going to use &lt;code&gt;STUArray&lt;/code&gt; since I want a mutable array while I'm doing the shuffling.  Also since I'm just shuffling &lt;code&gt;Int&lt;/code&gt;s I'm using an &lt;em&gt;unboxed&lt;/em&gt; array that directly stores the values in the array.  Were we shuffling a non-primitive type then we would need to use &lt;code&gt;STArray&lt;/code&gt; instead which would cost a bit in performance and memory usage since pointers to the elements are stored in the array instead of the elements themselves.  We will also be working in the &lt;code&gt;ST&lt;/code&gt; monad, as is required when using mutable arrays outside of the IO monad.
&lt;/p&gt;
&lt;p&gt;Instead of working in the &lt;code&gt;ST&lt;/code&gt; monad we could use &lt;code&gt;IOUArray&lt;/code&gt; but then our shuffling routines would only be usable in the &lt;code&gt;IO&lt;/code&gt; monad.  Using the &lt;code&gt;ST&lt;/code&gt; monad gives us a bit more flexibility.
&lt;/p&gt;
&lt;p&gt;It turns out that the only real difference between the naive and Fisher-Yates(FY) shuffling algorithms is how we choose the elements in the array to swap.  In both cases we start with the last element in the array and swap it with a random element from the array, then do the same with the second to last element and so until we get to the start of the array.  In the naive algorithm we swap with &lt;em&gt;any&lt;/em&gt; element in the array while in the FY algorithm we only consider elements before the current element for swapping.  We can encode these rules in the following two functions which pass a &lt;em&gt;bounds filter&lt;/em&gt; to the actual shuffle algorithm implementation.  The bounds filter function just takes the array bounds and the index of the element being swapped and returns the bounds for generating a random element to swap with:
&lt;/p&gt;
&lt;pre&gt; &lt;code&gt;
&amp;gt; naiveShuffle :: (Int, Int) -&amp;gt; StdGen -&amp;gt; (UArray Int Int, StdGen)
&amp;gt; naiveShuffle bs randGen = shuffleImpl bs randGen boundsFilter
&amp;gt;     where
&amp;gt;     boundsFilter bs _ = bs
&amp;gt;
&amp;gt; fyShuffle :: (Int, Int) -&amp;gt; StdGen -&amp;gt; (UArray Int Int, StdGen)
&amp;gt; fyShuffle bs randGen = shuffleImpl bs randGen boundsFilter
&amp;gt;     where
&amp;gt;     boundsFilter (lo, _) cur = (lo, cur)
&lt;/code&gt; &lt;/pre&gt; 
&lt;p&gt;Concentrate on &lt;code&gt;boundsFilter&lt;/code&gt; for the moment, we'll get to the rest in a minute.  &lt;code&gt;boundsFilter&lt;/code&gt; takes the array bounds and the index of the current element and generates bounds for the random element index to swap the current element with.  In the naive case the bounds for the random index are just the full array bounds while in the FY case the bounds are the &lt;code&gt;lo&lt;/code&gt; end of the array bounds and the current index. This is really the only difference between the two algorithms.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;The rest of the shuffling algorithm is defined in &lt;code&gt;shuffleImpl&lt;/code&gt; which takes the bounds for the array to shuffle, a random number generator and a filter for the bounds.  It is implemented as:
&lt;/p&gt;
&lt;pre&gt; &lt;code&gt;
&amp;gt; shuffleImpl :: (Int, Int) -&amp;gt; StdGen -&amp;gt; 
&amp;gt;                ((Int, Int) -&amp;gt; Int -&amp;gt; (Int, Int)) -&amp;gt; 
&amp;gt;                (UArray Int Int, StdGen)
&amp;gt; shuffleImpl bs@(lo, hi) randGen boundsFilter = runST runShuffle
&amp;gt;     where
&amp;gt;     runShuffle :: ST s (UArray Int Int, StdGen)
&amp;gt;     runShuffle = do a &amp;lt;- createArray bs
&amp;gt;                     r' &amp;lt;- doShuffle hi a randGen
&amp;gt;                     a' &lt;- unsafeFreeze a
&amp;gt;                     return (a', r')
&amp;gt;     doShuffle :: Int -&gt; (STUArray s Int Int) -&amp;gt; StdGen -&amp;gt; 
&amp;gt;                  ST s StdGen
&amp;gt;     doShuffle cur a r
&amp;gt;         | cur&amp;gt; lo = 
&amp;gt;             do
&amp;gt;             (n, r') &lt;- return $ randomR (boundsFilter bs cur) r
&amp;gt;             swapElems a cur n
&amp;gt;             doShuffle (cur - 1) a r'
&amp;gt;         | otherwise = return r
&amp;gt;     swapElems :: (STUArray s Int Int) -&gt; Int -&amp;gt; Int -&amp;gt; ST s ()
&amp;gt;     swapElems a n1 n2 = do
&amp;gt;                         v1 &amp;lt;- readArray a n1
&amp;gt;                         v2 &amp;lt;- readArray a n2
&amp;gt;                         writeArray a n1 v2
&amp;gt;                         writeArray a n2 v1
   &lt;/code&gt; &lt;/pre&gt;
&lt;p&gt;   &lt;code&gt;shuffleImpl&lt;/code&gt; returns the shuffled array and the updated random number generator.  The first thing to note is that we start with a call to &lt;code&gt;runST&lt;/code&gt;.  We have to use &lt;code&gt;runST&lt;/code&gt; instead of &lt;code&gt;runSTUArray&lt;/code&gt; because we want to get the updated random number generator out of the &lt;code&gt;ST&lt;/code&gt; computation and &lt;code&gt;runSTUArray&lt;/code&gt; only returns the computed array.  You've probably noticed that there are type annotations on all of the function definitions so far.  And so far none of them have been necessary, they're there for &lt;em&gt;pedagogical&lt;/em&gt; purposes&lt;sup id="fnr1-470284846"&gt;&lt;a href="#fn1-470284846"&gt;1&lt;/a&gt;&lt;/sup&gt;. Now for the definition of &lt;code&gt;createArray&lt;/code&gt;:
&lt;/p&gt;
&lt;pre&gt; &lt;code&gt;
&amp;gt; createArray :: (Int, Int) -&amp;gt; (ST s (STUArray s Int Int))
&amp;gt; createArray bs@(low, _) = newListArray bs [low..]
   &lt;/code&gt; &lt;/pre&gt;
&lt;p&gt;   When we define &lt;code&gt;createArray&lt;/code&gt; the type annotation is required so that the call to &lt;code&gt;MArray.newListArray&lt;/code&gt; knows which type of array to create.  All &lt;code&gt;newListArray&lt;/code&gt; knows is that we want something that is of type-class &lt;code&gt;MArray&lt;/code&gt;.  The explicit type annotation tells the compiler to use the &lt;code&gt;STUArray&lt;/code&gt; instance of &lt;code&gt;MArray&lt;/code&gt; when the call to  &lt;code&gt;newListArray&lt;/code&gt; is made.
&lt;/p&gt;
&lt;p&gt;So really all &lt;code&gt;shuffleImpl&lt;/code&gt; does is use &lt;code&gt;runST&lt;/code&gt; to run the &lt;code&gt;runShuffle&lt;/code&gt; computation.  In &lt;code&gt;runShuffle&lt;/code&gt; we use &lt;code&gt;createArray&lt;/code&gt; to create a new array initialized to the integers in our bounds in ascending order.  Then &lt;code&gt;doShuffle&lt;/code&gt; is run which iterates the elements of the array swapping them according to our random number generation scheme.  Note that the updates to the random number generator have to be threaded though the calls to &lt;code&gt;doShuffle&lt;/code&gt;.  When &lt;code&gt;doShuffle&lt;/code&gt; is done we have to &lt;em&gt;freeze&lt;/em&gt; the mutable array so that it can be sent out of the &lt;code&gt;ST&lt;/code&gt; monad and back to the caller of &lt;code&gt;shuffleImpl&lt;/code&gt;.  We use &lt;code&gt;unsafeFreeze&lt;/code&gt; here, which avoids an array copy when the immutable array is created.  Since we are not going to use the mutable array anymore beyond this point this is actually a &lt;em&gt;safe&lt;/em&gt; thing to do.  Finally the immutable array and the updated random number generator are returned.
&lt;/p&gt;
&lt;p&gt;One thing that gave me trouble when I first started trying to use the &lt;code&gt;ST&lt;/code&gt; monad was that I wanted to put &lt;code&gt;forall s .&lt;/code&gt; on all my type annotations.  The definition of &lt;code&gt;ST&lt;/code&gt; involves &lt;code&gt;forall&lt;/code&gt; so I thought that I needed &lt;code&gt;forall&lt;/code&gt; all over the place as well.  The problem is that in all of the &lt;code&gt;ST s&lt;/code&gt; types above the compiler fills in &lt;code&gt;s&lt;/code&gt; for you.  The type for &lt;code&gt;s&lt;/code&gt; is hidden in the call to &lt;code&gt;runST&lt;/code&gt; and the user of the &lt;code&gt;ST&lt;/code&gt; monad does not get to know what it is.  It's only purpose is to keep the state of one call to &lt;code&gt;runST&lt;/code&gt; separate from any other calls to &lt;code&gt;runST&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;Did you notice in &lt;code&gt;doShuffle&lt;/code&gt; how we're passing &lt;code&gt;StdGen&lt;/code&gt;s all over the place?  This is screaming out for the &lt;code&gt;State&lt;/code&gt; monad, or in our case its cousin the &lt;code&gt;StateT&lt;/code&gt; monad transformer.  So we're now going to wrap our &lt;code&gt;ST&lt;/code&gt; monad in &lt;code&gt;StateT&lt;/code&gt; so we don't have to pass random number generators all over the place.  We'll call the new version of &lt;code&gt;shuffleImpl&lt;/code&gt; that uses &lt;code&gt;StateT&lt;/code&gt; &lt;code&gt;shuffleImpl'&lt;/code&gt;.
&lt;/p&gt;
&lt;pre&gt; &lt;code&gt;
&amp;gt; type ShuffleState s a = StateT StdGen (ST s) a
&amp;gt;
&amp;gt; shuffleImpl' :: (Int, Int) -&amp;gt; StdGen -&amp;gt; 
&amp;gt;                 ((Int, Int) -&amp;gt; Int -&amp;gt; (Int, Int)) -&amp;gt; 
&amp;gt;                 (UArray Int Int, StdGen)
&amp;gt; shuffleImpl' bs@(lo, hi) randGen boundsFilter = 
&amp;gt;     runST (runStateT runShuffle randGen)
&amp;gt;     where
&amp;gt;     runShuffle :: ShuffleState s (UArray Int Int)
&amp;gt;     runShuffle  = do a &lt;- lift $ createArray bs
&amp;gt;                      doShuffle hi a
&amp;gt;                      lift $ unsafeFreeze a
&amp;gt;     doShuffle :: Int -&gt; (STUArray s Int Int) -&amp;gt; ShuffleState s ()
&amp;gt;     doShuffle cur a
&amp;gt;         | cur&amp;gt; lo = 
&amp;gt;             do n &lt;- getRandom $ boundsFilter bs cur
&amp;gt;                swapElems a cur n
&amp;gt;                doShuffle (cur - 1) a
&amp;gt;         | otherwise  = return ()
&amp;gt;     getRandom :: (Int, Int) -&gt; ShuffleState s Int
&amp;gt;     getRandom bs = do r &amp;lt;- get
&amp;gt;                       (n, r') &lt;- return $ randomR bs r
&amp;gt;                       put r'
&amp;gt;                       return n
&amp;gt;     swapElems :: (STUArray s Int Int) -&gt; Int -&amp;gt; Int -&amp;gt; 
&amp;gt;                  ShuffleState s ()
&amp;gt;     swapElems a n1 n2 = do
&amp;gt;                         v1 &amp;lt;- lift $ readArray a n1
&amp;gt;                         v2 &amp;lt;- lift $ readArray a n2
&amp;gt;                         lift $ writeArray a n1 v2
&amp;gt;                         lift $ writeArray a n2 v1
&amp;gt;
&amp;gt;
   &lt;/code&gt; &lt;/pre&gt;
 &lt;p&gt;  The first thing we do is define our state type &lt;code&gt;ShuffleState&lt;/code&gt;.  Note that it is parameterized on both the type of the monadic value &lt;code&gt;a&lt;/code&gt; and the &lt;code&gt;ST&lt;/code&gt; monad type &lt;code&gt;s&lt;/code&gt;.  This is important.  I originally tried only parameterizing on &lt;code&gt;a&lt;/code&gt; and introducing &lt;code&gt;s&lt;/code&gt; on the right side using &lt;code&gt;forall&lt;/code&gt;.  As with the &lt;em&gt;non-State&lt;/em&gt; implementation the use of &lt;code&gt;forall&lt;/code&gt; is the wrong thing to do.  The compiler is smart enough to figure out what &lt;code&gt;s&lt;/code&gt; should be in all the uses of &lt;code&gt;ShuffleState&lt;/code&gt;.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;The big changes in &lt;code&gt;shuffleImpl'&lt;/code&gt; is that we put a call to &lt;code&gt;runStateT&lt;/code&gt; inside the call to &lt;code&gt;runST&lt;/code&gt;.  This runs the computation in the combined &lt;code&gt;ST&lt;/code&gt; and &lt;code&gt;State&lt;/code&gt; monads.  Our state is the random number generator.  We no longer pass around the random number generator, instead we stick it in the state in the call to &lt;code&gt;runStateT&lt;/code&gt; and then in &lt;code&gt;getRandom&lt;/code&gt; we grab the generator from the state, get a random number and stick the updated generator back in the state.  Otherwise things work mostly the same as in &lt;code&gt;shuffleImpl&lt;/code&gt; modulo a few calls to &lt;code&gt;lift&lt;/code&gt; that are needed to lift values from the &lt;code&gt;ST&lt;/code&gt; monad into the combined monad.  In our case we need to &lt;em&gt;lift&lt;/em&gt; any value that is only in the &lt;code&gt;ST&lt;/code&gt; monad, like the results of &lt;code&gt;readArray&lt;/code&gt; and &lt;code&gt;writeArray&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;You might have noticed that &lt;code&gt;shuffleImpl'&lt;/code&gt; is actually bigger than &lt;code&gt;shuffleImpl&lt;/code&gt;.  This is due to &lt;code&gt;getRandom&lt;/code&gt;.  While it is bigger, the actual code is a bit cleaner so I think it's worth the trade-off.  If we were doing random number generation in more than just the one spot then we would probably see a net gain in code size.
&lt;/p&gt;
&lt;p&gt;So there you go, a quick tutorial on using mutable arrays in the &lt;code&gt;ST&lt;/code&gt; array on it's own and with &lt;code&gt;StateT&lt;/code&gt;.
&lt;/p&gt;

&lt;div class="footnote"&gt;&lt;hr/&gt;&lt;ol&gt;
 &lt;li id="fn1-470284846"&gt;
     I suppose pedagogical could mean &lt;em&gt;anal&lt;/em&gt; in this case.  Normally I wouldn't declare the types of functions defined in a &lt;code&gt;where&lt;/code&gt; clause but it seems instructive to do so in this case.&lt;a href="#fnr1-470284846" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-1219013799855830478?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/1219013799855830478/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=1219013799855830478' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/1219013799855830478'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/1219013799855830478'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2008/01/haskell-shuffling.html' title='Haskell Shuffling'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-7042254500701943481</id><published>2007-12-29T23:22:00.000-08:00</published><updated>2007-12-29T23:40:14.324-08:00</updated><title type='text'>Dunce Cap Removal</title><content type='html'>&lt;p&gt;OK, I have something to admit.  I realized the other day that I totally misunderstood how variable bindings in Haskell &lt;code&gt;do&lt;/code&gt; blocks propagated to later statements in the block.  Once I realized this and saw how wrong I had been it was sort of like taking off a dunce cap that I didn't know I was wearing.  I guess it was a good thing that I never went shooting my mouth off about this subject else my dunce cap would have become visible to everybody else as well.
&lt;/p&gt;
&lt;p&gt;To be a bit more explicit about what I was so wrong about consider this bit of Haskell code:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;   do
    x &amp;lt;- someAction
    y &amp;lt;- someOtherAction
    putStrLn $ show $ x + y
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;How does the last line access the &lt;code&gt;x&lt;/code&gt; binding? For some reason I had concocted this crazy complicated mechanism where the compiler was creating ever longer tuples containing all the variables bound in the &lt;code&gt;do&lt;/code&gt; block and then automatically extracting the proper element from the tuple in each statement that a variable was used in.  It was really &lt;em&gt;hand-wavey&lt;/em&gt; and looking back on it I guess I was being a bit lazy.  Had I really sat down and worked out how this &lt;em&gt;ever-growing tuple&lt;/em&gt; thing worked I would have seen how clunky it was and probably noticed the dunce cap a lot sooner.
&lt;/p&gt;
&lt;p&gt;Then the other day I decided to manually &lt;em&gt;de-sugar&lt;/em&gt; a &lt;code&gt;do&lt;/code&gt; block in some code that I was working on.  Doing the same to the above results in
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; someAction &amp;gt;&amp;gt;= 
        (\x -&amp;gt; someOtherAction &amp;gt;&amp;gt;= 
        (\y -&amp;gt; putStrLn $ show $ x + y))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It was at this point I realized how big of an idiot I had been.  The compiler doesn't need to start creating tuples of ever-growing length.  In the &lt;code&gt;putStrLn&lt;/code&gt; expression &lt;code&gt;y&lt;/code&gt; is passed in so it is obviously available.  But notice that the lambda expression containing &lt;code&gt;putStrLn&lt;/code&gt; is defined within another lambda expression that &lt;code&gt;x&lt;/code&gt; is passed into.  &lt;code&gt;x&lt;/code&gt; is part of the &lt;code&gt;putStrLn&lt;/code&gt; lambda's environment and thus is accessible.  So instead of building bigger and bigger tuples as we go further into the &lt;code&gt;do&lt;/code&gt; block the compiler is instead nesting more and more lambda expressions. Since the previous lines in a &lt;code&gt;do&lt;/code&gt; block become lambda expressions containing the current expression any parameters they take are available in the inner lambda expressions.
&lt;/p&gt;
&lt;p&gt;So now my dunce cap is off.  Maybe if I had exposed it to someone earlier they would have disabused me of my misunderstanding earlier. Maybe they would have just pointed and laughed or let me go on thinking the way that I was, sort of an intellectual &lt;em&gt;kick-me&lt;/em&gt; sign.  At least getting rid of this dunce cap means that I am learning.  Though now I'm left wondering how many more dunce caps I'm wearing that I don't know about.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-7042254500701943481?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/7042254500701943481'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/7042254500701943481'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2007/12/dunce-cap-removal.html' title='Dunce Cap Removal'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-4677655975916989416</id><published>2007-11-17T17:21:00.000-08:00</published><updated>2008-05-14T22:13:57.469-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Poor-man's Closure</title><content type='html'>&lt;p&gt;So C++ is lacking in some areas of modern programming language design, that's not a big secret.  But given the flexibility of C++ the question becomes: how hard is it to add missing features?  Here I'm going to take a look at &lt;a href="http://en.wikipedia.org/wiki/Closure_(computer_science)"&gt;closures&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Lambda_calculus"&gt;lambda expressions&lt;/a&gt;.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;First off we can talk about closures.  As far as I know there it isn't possible to add full closures to C++.  But we can get a poor man's closure using the &lt;a href="http://boost.org/libs/bind/bind.html"&gt;boost::bind&lt;/a&gt; library.  If you don't know about this library yet you should run (not walk) over to &lt;a href="http://boost.org"&gt;boost.org&lt;/a&gt; and check it out.  If you work at all with STL algorithms it will cut down on the amount of code you write immensely.  So for a demonstration of the usefulness of boost::bind as a sort of closure mechanism lets say that you want to add two to all the elements in some &lt;code&gt;std::vector&amp;lt;int&amp;gt;&lt;/code&gt;.  You could do this using &lt;code&gt;std::binder1st&lt;/code&gt; and &lt;code&gt;std::mem_fun&lt;/code&gt; but that's kind of a pain in the ass.  Instead you can do
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;std::transform(values.begin(), values.end(), 
               values.begin(),
               boost::bind(std::plus&amp;lt;int&amp;gt;(), _1, 2));
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Things start getting interesting when you want to use bind to modify a value from outside of your STL algorithm.  Take a look at this:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int lastVal = 0;
std::for_each(values.begin(), values.end(),
              boost::bind(sum, boost::ref(lastVal), _1));
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Assuming that &lt;code&gt;sum&lt;/code&gt; is a function that takes two &lt;code&gt;int&lt;/code&gt;s and puts the result into it's first argument then this would leave the sum of the vector in &lt;code&gt;lastVal&lt;/code&gt; &lt;sup id="fnr1-620245773"&gt;&lt;a href="#fn1-620245773"&gt;1&lt;/a&gt;&lt;/sup&gt;.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;This last example also can be used to illustrate why boost::bind doesn't create a &lt;em&gt;true closure&lt;/em&gt;.  Suppose that instead of using our bind expression immediately we return it using &lt;a href="http://boost.org/doc/html/function.html"&gt;boost::function&lt;/a&gt; &lt;sup id="fnr2-620245773"&gt;&lt;a href="#fn2-620245773"&gt;2&lt;/a&gt;&lt;/sup&gt;. When we try to use the bind object very bad things will happen, a crash if we're lucky.  This happens because the &lt;code&gt;boost::ref&lt;/code&gt; object in the bind object is referencing a variable that no longer exists&lt;sup id="fnr3-620245773"&gt;&lt;a href="#fn3-620245773"&gt;3&lt;/a&gt;&lt;/sup&gt;. If we had a &lt;em&gt;true closure&lt;/em&gt; then the variable would continue to exist as long as the closure did and we'd be safe.  Without garbage collection I don't see how to make this work&lt;sup id="fnr4-620245773"&gt;&lt;a href="#fn4-620245773"&gt;4&lt;/a&gt;&lt;/sup&gt;.
&lt;/p&gt;
&lt;p&gt;Once you have closures then lambda expressions (or local function definitions) become very useful.  Unfortunately C++ has neither.  There is the &lt;a href="http://boost.org/doc/html/lambda.html"&gt;boost::lambda&lt;/a&gt; library which can be useful in this area but it is somewhat limited and in my experience it doesn't work so hot with VC++, even the newest incarnation of the compiler&lt;sup id="fnr5-620245773"&gt;&lt;a href="#fn5-620245773"&gt;5&lt;/a&gt;&lt;/sup&gt;.  We can get local function definitions though by making a local structure definition&lt;sup id="fnr6-620245773"&gt;&lt;a href="#fn6-620245773"&gt;6&lt;/a&gt;&lt;/sup&gt;:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int sum(const std::vector&amp;lt;int&amp;gt;&amp;amp; values)
{   
    int total = 0;

    struct Calculate
{
    Calculate(int&amp;amp; total_) : m_total(total_) {}

    int&amp;amp; m_total;

    void operator()(int v2)
    {
    m_total += v2;
    }           
}

std::for_each(values.begin(), values.end(), Calculate(total));

return total;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;One thing to note about the above is that in order to pull &lt;code&gt;total&lt;/code&gt; in to the &lt;code&gt;Calculate&lt;/code&gt; closure we had to have &lt;code&gt;Calculate&lt;/code&gt; hold a reference to it&lt;sup id="fnr7-620245773"&gt;&lt;a href="#fn7-620245773"&gt;7&lt;/a&gt;&lt;/sup&gt;.  Local structures like &lt;code&gt;Calculate&lt;/code&gt; can refer to local variables in the enclosing function but only if they are static.  Now we probably don't want to go declaring &lt;code&gt;total&lt;/code&gt; static since that will lead to reentrancy problems.  This is unfortunate, if we didn't have this &lt;em&gt;static only&lt;/em&gt; restriction then &lt;code&gt;Calculate&lt;/code&gt; would be much more compact.  We can use &lt;code&gt;bind&lt;/code&gt; to build the closure though and make things a bit more compact&lt;sup id="fnr8-620245773"&gt;&lt;a href="#fn8-620245773"&gt;8&lt;/a&gt;&lt;/sup&gt;:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int sum(const std::vector&amp;lt;int&amp;gt;&amp;amp; values)
{   
    int total = 0;

    struct Calculate
{
    static void run(int&amp;amp; total_, int v)
    {
    total_ += v;
        }           
}

std::for_each(values.begin(), values.end(), 
              boost::bind(Calculate::run, boost::ref(total), _1));

return total;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;While this makes things a bit more compact the difference when you're binding more variables is much more dramatic. If we could make local function definitions the change in size would be slightly better still.  If we didn't have the &lt;em&gt;refer only to static local variables&lt;/em&gt; restriction it would be even better.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;So we can get pretty close to closures and local function definitions in C++ without having them built into the language.  Obviously you have to be careful to keep your local function definitions short in order to maintain readability.  While it's nice to have a short block of code that you're passing into a function defined right where you pass it in, once the code gets too long it will start obscuring the enclosing functions flow and really belongs off on its own.  But even if the block of code in question is defined outside of your function you can still close over it with &lt;code&gt;bind&lt;/code&gt;.
&lt;/p&gt;

&lt;div class="footnote"&gt;&lt;hr/&gt;&lt;ol&gt;
 &lt;li id="fn1-620245773"&gt;
     I realize this is a really contrived example and that there are much better ways to sum a sequence (std::accumulate comes to mind).  But this example is a simple illustration of a technique that is useful when combined with the &lt;em&gt;poor man's lambda&lt;/em&gt; that I talk about later.&lt;a href="#fnr1-620245773" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn2-620245773"&gt;
     Note that we can't return the bind expression directly because the type of &lt;code&gt;boost::bind(sum, boost::ref(lastVal), _1))&lt;/code&gt; is some hideous thing that the documentation calls various variations on &lt;em&gt;unspecified&lt;/em&gt;.  Instead we need to capture the bind expression in a &lt;code&gt;boost::function&amp;lt;void(int)&amp;gt;&lt;/code&gt; and return that.&lt;a href="#fnr2-620245773" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn3-620245773"&gt;
     You might be debating the usefulness of this construct and you'd be right.  In this case it's not so useful to create a bind the binds to a local variable and is returned from the function where that local variable exists.  But we could instead bind to a member variable in an object and register that bind object as a callback somewhere.  This is useful but puts you on dangerous ground where we need the object that contains the bound member variable to outlast the callback registration. &lt;a href="#fnr3-620245773" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn4-620245773"&gt;
     One could address this by cluttering up the code with &lt;code&gt;boost::shared_ptr&lt;/code&gt;'s but that can quickly get messy.  I've heard that the C++ standardization committee is talking about adding closures to the language but for the life of me I can't figure out how that's going to work without garbage collection.  Maybe they're only talking about adding closures to &lt;em&gt;Managed C++&lt;/em&gt;.&lt;a href="#fnr4-620245773" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn5-620245773"&gt;
     Visual C++ * with the service pack at the time of this writing.&lt;a href="#fnr5-620245773" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn6-620245773"&gt;
     Yep, you can make structure definitions within a function definition.  I didn't know about this until a few months ago and it has made my life a lot easier. One thing to note is that you cannot have static members in a local structure so their usefulness can be somewhat curtailed in some situations.  You also can't use templates at all in the local structure.&lt;a href="#fnr6-620245773" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn7-620245773"&gt;
     The underscore on the end of &lt;code&gt;total_&lt;/code&gt; is only there to make explicit that the constructor parameter is different from the &lt;code&gt;total&lt;/code&gt; local variable.  The constructor parameter could just as well have been called &lt;code&gt;total&lt;/code&gt; since nothing in &lt;code&gt;Calculate&lt;/code&gt; can refer to the local variable &lt;code&gt;total&lt;/code&gt;.&lt;a href="#fnr7-620245773" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn8-620245773"&gt;
     Note that if you're using VC++ 2005 without the service pack a compiler bug could prevent this code from compiling.&lt;a href="#fnr8-620245773" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-4677655975916989416?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/4677655975916989416/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=4677655975916989416' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/4677655975916989416'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/4677655975916989416'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2007/11/poor-mans-closure.html' title='Poor-man&apos;s Closure'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-2269516397185352974</id><published>2007-09-13T21:54:00.000-07:00</published><updated>2007-10-28T20:46:33.489-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='lisp'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Missing the point</title><content type='html'>&lt;p&gt;OK, I'll admit.  I've jumped on the &lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book.html"&gt;SICP&lt;/a&gt; bandwagon.  I've worked through about half the book&lt;sup id="fnr1-778212227"&gt;&lt;a href="#fn1-778212227"&gt;1&lt;/a&gt;&lt;/sup&gt; and am really liking it.  Of course I've come to it a bit late to the book not being a computer science graduate, though it seems these days one probably wouldn't get exposed to it anyway or so I &lt;a href="http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html"&gt;hear&lt;/a&gt;.  It seems like one of those books that's great but people dread taking a class that uses it, kind of like &lt;a href="http://www.amazon.com/Classical-Electrodynamics-Third-David-Jackson/dp/047130932X/ref=pd_bbs_sr_1/104-7880412-2407918?ie=UTF8&amp;s=books&amp;qid=1192340836&amp;sr=8-1"&gt;Jackson&lt;/a&gt; or &lt;a href="http://www.amazon.com/Classical-Mechanics-Addison-Wesley-physics-Goldstein/dp/0201029189/ref=ed_oe_h/104-7880412-2407918"&gt;Goldstein&lt;/a&gt; for physics majors.  Anyways, had the &lt;a href="http://steve.yegge.googlepages.com/ten-challenges"&gt;bandwagon&lt;/a&gt; not come by for me to jump on I probably wouldn't have ever found it so here's to bandwagons.&lt;/p&gt;

&lt;p&gt;Enough blowing smoke up Abelman and Sussman's asses about their book. It's great, we all get it.  Though it seems like some people have decided they liked the book but not the language and are going to &lt;a href="http://www.codepoetics.com/wiki/index.php?title=Topics:SICP_in_other_languages"&gt;do something about it&lt;/a&gt;.  And to me it seems like that's missing the point.  Sort of like how people go all ga-ga over peanut butter and chocolate together&lt;sup id="fnr2-778212227"&gt;&lt;a href="#fn2-778212227"&gt;2&lt;/a&gt;&lt;/sup&gt;, like somehow these two things are just meant to go together.  Well in this case they are.  A&amp;B chose lisp for a reason, and not just because they're lisp weenies.  &lt;br /&gt; 
&lt;/p&gt;
&lt;p&gt;The way I feel about it is that there are certain language categories that everyone programmer should learn at least one language from.  They are &lt;em&gt;machine level&lt;/em&gt; &lt;sup id="fnr3-778212227"&gt;&lt;a href="#fn3-778212227"&gt;3&lt;/a&gt;&lt;/sup&gt;, &lt;em&gt;static imperative&lt;/em&gt; &lt;sup id="fnr4-778212227"&gt;&lt;a href="#fn4-778212227"&gt;4&lt;/a&gt;&lt;/sup&gt;, &lt;em&gt;dynamic&lt;/em&gt; &lt;sup id="fnr5-778212227"&gt;&lt;a href="#fn5-778212227"&gt;5&lt;/a&gt;&lt;/sup&gt;, &lt;em&gt;functional&lt;/em&gt; &lt;sup id="fnr6-778212227"&gt;&lt;a href="#fn6-778212227"&gt;6&lt;/a&gt;&lt;/sup&gt; and &lt;em&gt;lisp&lt;/em&gt; &lt;sup id="fnr7-778212227"&gt;&lt;a href="#fn7-778212227"&gt;7&lt;/a&gt;&lt;/sup&gt;. 

&lt;/p&gt;
&lt;p&gt;Learning a machine level language will help you understand what the machine is doing with your code.  Learning lisp will teach you what the compiler is doing with your code.  Think of it as the &lt;em&gt;assembly language of computation&lt;/em&gt;, whereas assembly is the, well,  &lt;em&gt;assembly language of computers&lt;/em&gt;.  That's why A&amp;B chose it.  While there are other languages that look more like lambda calculus (ML anyone?), when you're writing lisp you're writing out the abstract syntax trees for your program.  And understanding how you're programs goes from source code to a computation is powerful stuff.
&lt;/p&gt;
&lt;p&gt;Now I don't want to bag on the folks at &lt;em&gt;SICP in other language&lt;/em&gt; but if you use another language when working through SICP then you're sort of missing the point.  Going back and using the exercises later when you're learning a new language is fine, but the first time through use lisp.  It won't bite but you might wear out your ( and ) keys.
&lt;/p&gt;

&lt;div class="footnote"&gt;&lt;hr/&gt;&lt;ol&gt;
 &lt;li id="fn1-778212227"&gt;

     Kind of slow going, but when you've got a two year old at home you don't get a many solid blocks of time to too deeply into things.&lt;a href="#fnr1-778212227" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn2-778212227"&gt;
     Never understood that one myself. Sure they taste OK together but I'm actually happier keeping them apart.  But what do I know, I don't Even like my ice cream with chunky bits in it and &lt;a href="http://brett-hall.blogspot.com/2007/04/bacon.html"&gt;have problems using my microwave&lt;/a&gt;.&lt;a href="#fnr2-778212227" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn3-778212227"&gt;

     C is probably good enough but some assembly wouldn't hurt.&lt;a href="#fnr3-778212227" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn4-778212227"&gt;
     Java, C#, C++, etc.  For better or worse it's where the jobs are so you probably can't avoid learning at least one of these language.&lt;a href="#fnr4-778212227" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn5-778212227"&gt;
     Python, Ruby, Javascript, etc.  &lt;br /&gt; &lt;a href="#fnr5-778212227" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;

 &lt;/li&gt;

 &lt;li id="fn6-778212227"&gt;
     Preferably &lt;a href="http://haskell.org"&gt;pure, lazy and strongly typed&lt;/a&gt;, but I guess we can compromise on a &lt;a href="http://caml.inria.fr/"&gt;point&lt;/a&gt; &lt;a href="http://research.microsoft.com/fsharp/"&gt;or&lt;/a&gt; &lt;a href="http://www.erlang.org/"&gt;two&lt;/a&gt;.&lt;a href="#fnr6-778212227" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

&lt;/ol&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-2269516397185352974?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/2269516397185352974/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=2269516397185352974' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/2269516397185352974'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/2269516397185352974'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2007/03/missing-point.html' title='Missing the point'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-871035497440188782</id><published>2007-06-25T22:33:00.000-07:00</published><updated>2007-07-20T12:57:10.125-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>pointer-less?</title><content type='html'>&lt;p&gt;One thing I remember about Java back when I was working on enterprisey sorts of things is that for a language that supposedly has no pointers it sure seemed like we got a lot of &lt;code&gt;NullPointerException&lt;/code&gt;s.  &lt;code&gt;NullPointerException&lt;/code&gt; was definitely the biggest cause of production bugs followed closely by ClassCastException.  So if a language is purported to not have pointers why do they seem to be causing so many problems?  Isn't Java supposed to be better than C++ due to this lack of pointers?
&lt;/p&gt;
&lt;p&gt;The problem is that Java does have pointers.  In fact everything that isn't a primitive type is handled through pointers.  There is no stack/heap dichotomy like there is in C/C++: you either have a  primitive type that lives on the stack (or is part of a non-primitive object) or you have a non-primitive type that lives &lt;strike&gt;on the heap&lt;/strike&gt; &lt;em&gt;in the object store&lt;/em&gt;. These non-pointer pointers do have some advantages over their C/C++ cousins, the biggest being that dereferencing a null pointer doesn't bring your whole program crashing down around you.  Ah the advantages of a managed run-time where an invalid pointer dereference doesn't have to treated as a possible attack and require the slaying of the perpetrator with extreme prejudice.
&lt;/p&gt;
&lt;p&gt;While it is nice that you can catch &lt;code&gt;NullPointerException&lt;/code&gt;s, it would be nicer if people actually did it more than half the time.  Like I said above: when I worked on enterprisey stuff &lt;code&gt;NullPointerException&lt;/code&gt; was the single biggest cause of production problems.  Now I don't think that me or any of the people I was working with in said shop were morons.  And it seemed like failure to account for the possibility of null pointers was one of the biggest problems we saw in programming tests we gave during interviews.  I'd say that this is a product of the &amp;quot;java has no pointers&amp;quot; mentality but C/C++ programmers make their fair share of the same sort of error.  Maybe &lt;a href="http://haskell.org"&gt;certain languages&lt;/a&gt; that have &lt;a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html"&gt;done away with pointers&lt;/a&gt; altogether are on to something.  Anyway, maybe keep this in mind the next time you're bagging on C/C++ pointers compared to Java/C# keep in mind that you're not really that far ahead of the game, a few steps  maybe but not leaps and bounds.

&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-871035497440188782?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/871035497440188782/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=871035497440188782' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/871035497440188782'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/871035497440188782'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2007/05/pointer-less.html' title='pointer-less?'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5084302589052586355.post-6852645208873740715</id><published>2007-04-18T10:33:00.000-07:00</published><updated>2007-04-18T10:33:53.102-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='bacon'/><category scheme='http://www.blogger.com/atom/ns#' term='programming?'/><title type='text'>Bacon</title><content type='html'>&lt;p&gt;My Microwave oven really wants me to eat a lot of bacon.  I'm vegan and live in California,  I'm already quite aware that my culinary habits are not exactly in sync with those of the average American.  Even so, every time I go to nuke a tofu dog my microwave dutifully reminds me of this fact. TV, magazines and just about everything else also remind me of this fact so I really don't need the microwave giving me a bunch of crap about it as well. When I hit the &amp;quot;Microwave&amp;quot; button to get its attention it responds with &amp;quot;Bacon&amp;quot; every time.  It doesn't think that I might just want it to zap something for 30 seconds or a minute, it immediately jumps to the conclusion that I must want bacon.  If I want something else it requires that I convince it of this by pressing buttons and turning a knob.
&lt;/p&gt;
&lt;p&gt;Now you might be wondering why the microwave would want me to hit a button labeled &amp;quot;Microwave&amp;quot; before it will microwave something.  It does seem a bit redundant if you don't know that my microwave oven is a microwave/convection combo oven.  Given this fact it shouldn't surprise you that it also has a &amp;quot;Convection&amp;quot; button.  In addition to this it also has &amp;quot;Speed Cook&amp;quot; button that puts the microwave in a mode that's similar to convection but it's faster.  I don't remember why I would want to use the slower convection mode. It's one of those things that's in the manual and if you don't use it all the time your brain dumps it in preference to something more useful. And considering how much I've ended up using the convection mode of the oven this difference has never spent much time in my brain.

&lt;/p&gt;
&lt;p&gt;So I guess it's not all that illogical that I have to hit the &amp;quot;Microwave&amp;quot; button if I want to nuke something.  Of course the bulk of the time I want to use the microwave and not the convection or mysterious speed-cook modes and I'm willing to bet that the majority of users of microwave/convection combo ovens are the same way.  Even if they aren't I still think that defaulting to the microwave mode is the right thing to do. It seems to me that the amount of effort that you put in to starting some process should correspond to the amount of time that the process is going to be running relative to the other possible processes that a device could run.  I'm not a usability or interaction design expert but the preceding seems reasonable to me.  Maybe it's called &amp;quot;the principle of proportional effort&amp;quot; in user interface expert circles or something.
&lt;/p&gt;
&lt;p&gt;In my case I often want to nuke a tofu-dog or heat up some leftovers for my kid, both of which have run times of say 30 seconds.  Running the convection oven for 30 seconds doesn't accomplish much of anything.  On the rare occasion I actually use the convection oven I'd say I always run it for at least 5 minutes. So I would want to optimize the interface so that the shorter nuking session takes less time to start than the convection oven.  Of course with my oven both operations take roughly the same amount of work to start.  To nuke a tofu-dog I have to hit the &amp;quot;Microwave&amp;quot; button, turn a knob to get from &amp;quot;Bacon&amp;quot; to &amp;quot;Timed&amp;quot; mode, press the knob to select &amp;quot;Timed&amp;quot; mode, turn the knob again to select a time, press the knob again to lock in that time and finally press the knob one last time to confirm that, yes, I actually do want to cook something.  If I want to convect&lt;sup id="fnr1-175657793"&gt;&lt;a href="#fn1-175657793"&gt;1&lt;/a&gt;&lt;/sup&gt; some food I have to hit the &amp;quot;Convection&amp;quot; button, turn the knob to select a time and then press it lock in the time, turn the knob to select the heat level and press it to lock in the level and then press the knob again to confirm that I'm happy with my choices.  Note that if the microwave wasn't convinced that I wanted bacon then there would be one less knob turn and push when using microwave mode and it would technically beat convection mode by a hair.  Both modes could also eliminate one knob press by not asking for confirmation.  Instead the oven could rely on the cancel button that I would hit the instant I realized that I had screwed something up.  And, as these things usually go, the confirmation is now useless since I've been conditioned to press the knob twice after I get the time dialed in and if I screwed something up the time I just hit cancel.

&lt;/p&gt;
&lt;p&gt;Instead of having the interface assume that I want to do the quick and common operation it feigns ignorance and requires me to jump through a bunch of hoops. By default it should be assumed that I want a timed microwave run and if I start turning the knob without hitting a mode button then it should start setting the cooking time.  When I press the knob it should just start, no confirmation.  I can just hit the cancel button if I entered the wrong amount of time, a few stray microwaves isn't going to ruin my food.  The oven does have a minute button that starts the microwave at one minute and then adds a minute to the run time for each successive press.  Often if I'm nuking something for less than thirty seconds I'll just hit the minute button and stand there until the amount of time I actually wanted has passed at which point I'll hit cancel&lt;sup id="fnr2-175657793"&gt;&lt;a href="#fn2-175657793"&gt;2&lt;/a&gt;&lt;/sup&gt;.
&lt;/p&gt;
&lt;p&gt;Having to jump through all the hoops to nuke a tofu dog is annoying, but making bacon the default microwave setting just mystifies me.  Am I really that out of touch with mainstream America that I don't realize that cooking bacon is the number one use of microwave's?  I don't see members of my extended family using their microwaves for this, but they also live in California and thus might also be outliers when it comes to microwave oven market research.  I know that obesity and heart disease are out of control in the US, but could  &lt;strike&gt;bacon pushers&lt;/strike&gt; microwave ovens be the reason?  Or do the people at Kenmore like bacon so much that they assumed that everyone would like to have the bacon as their first microwaving choice? Did they sell the top slot to the pork industry? Am I reading too much into this.  All I wanted to do is nuke a tofu dog, but every time I do I get all these questions in my head.  And it also gets me wondering if there are people out there getting ready to write their own &lt;em&gt;bacon screed&lt;/em&gt; after having used software that I've written.
&lt;/p&gt;

&lt;div class="footnote"&gt;&lt;hr/&gt;&lt;ol&gt;
 &lt;li id="fn1-175657793"&gt;

     I suppose that &lt;em&gt;convect&lt;/em&gt; is probably the wrong word to use here since really it's the air that is convecting in the oven, not the food. But &lt;em&gt;cook&lt;/em&gt; doesn't exactly nail down what it is that is being done to the food so &lt;em&gt;convect&lt;/em&gt; it is. &lt;a href="#fnr1-175657793" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;

 &lt;li id="fn2-175657793"&gt;

     It turns out that once the microwave is going, i.e. after I hit the minute button, turning the knob adjusts the run time. This moots my complaint about how long it takes to get the microwave going to some degree.  It isn't exactly the most discoverable of interfaces though since we've had the oven for about six months and my wife just discovered this feature a couple of days ago.  Either that or we're morons.  &lt;br /&gt; &lt;a href="#fnr2-175657793" class="footnoteBackLink" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;
 &lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5084302589052586355-6852645208873740715?l=brett-hall.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://brett-hall.blogspot.com/feeds/6852645208873740715/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5084302589052586355&amp;postID=6852645208873740715' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/6852645208873740715'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5084302589052586355/posts/default/6852645208873740715'/><link rel='alternate' type='text/html' href='http://brett-hall.blogspot.com/2007/04/bacon.html' title='Bacon'/><author><name>brett</name><uri>http://www.blogger.com/profile/01838925956817921960</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://bp3.blogger.com/_ACvcTVcq9OU/SB0H8d6Dp9I/AAAAAAAAAAM/c3nPcbwSIl0/S220/pic.png'/></author><thr:total>0</thr:total></entry></feed>
