<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Herb Sutter's Blog</title><subtitle type="html" /><id>http://blogs.msdn.com/b/hsutter/atom.aspx</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/b/hsutter/atom.aspx" /><generator uri="http://telligent.com" version="5.6.50428.7875">Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><updated>2004-07-31T13:05:00Z</updated><entry><title>Movin' Out</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/10/04/237558.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/10/04/237558.aspx</id><published>2004-10-04T15:44:00Z</published><updated>2004-10-04T15:44:00Z</updated><content type="html">&lt;p&gt;(With apologies to Billy Joel.)&lt;/p&gt; &lt;p&gt;This is it: Hopefully the last time I move this blog for a while. I've moved over to a planned-to-be long-term blog home over at &lt;a href="http://www.pluralsight.com/"&gt;PluralSight&lt;/a&gt;. Thanks to &lt;a href="http://pluralsight.com/blogs/dbox/" xmlns="http://www.w3.org/1999/xhtml"&gt;Don&lt;/a&gt; for prodding, &lt;a href="http://pluralsight.com/blogs/fritz/default.aspx" xmlns="http://www.w3.org/1999/xhtml"&gt;Fritz&lt;/a&gt; for hosting, and retroactive thanks to all the folks at MSDN and GDN for hosting my initial blog home here.&lt;/p&gt; &lt;p&gt;I'll probably move my existing blog articles there. No ETA on when, though. Things are crazy, and I'm just about done the indexing on the &lt;em&gt;&lt;a href="http://www.gotw.ca/publications/c++cs.htm"&gt;C++ Coding Standards&lt;/a&gt;&lt;/em&gt; book. When that's done, and the C++ standards meeting a couple of weeks from now is over, and &lt;a href="http://www.gotw.ca/presentations.htm"&gt;some upcoming talks&lt;/a&gt;&amp;nbsp;including&amp;nbsp;&lt;a href="http://www.oopsla.org/2004/ShowEvent.do?id=804"&gt;this one at OOPSLA&lt;/a&gt; are behind me, there'll be more time. Probably. (I always think there'll be more time someday. I guess it's good to be optimistic.)&lt;/p&gt; &lt;p&gt;See you on &lt;a href="http://www.pluralsight.com"&gt;PluralSight&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=237558" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>Simulating try/finally in plain C++ (without the C++/CLI extensions)</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/09/06/226076.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/09/06/226076.aspx</id><published>2004-09-06T16:12:00Z</published><updated>2004-09-06T16:12:00Z</updated><content type="html">&lt;p&gt;Dan wrote:&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;I think this is close enough to try/finally in C++: &lt;/p&gt; &lt;p&gt;Resource* pRes = new Resource; &lt;br /&gt;try &lt;br /&gt;{ &lt;br /&gt;&amp;nbsp; // use the allocated resource somehow &lt;br /&gt;} &lt;br /&gt;catch (...) &lt;br /&gt;{ &lt;br /&gt;&amp;nbsp; delete pRes; &lt;br /&gt;&amp;nbsp; throw; &lt;br /&gt;} &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;First, note that this code as written doesn't do "finally," because the idea behind a finally block is that it gets executed whether an exception is thrown or not. But it's close: You can indeed get a similar effect if you add a throw statement (e.g., throw 1;) at the end of your try block, so that it always exits via an exception, and then catch(...).&lt;/p&gt; &lt;p&gt;Even after applying the quick fix to make this do what was intended,&amp;nbsp;this (ab)use of the try/catch mechanism&amp;nbsp;inferior to having finally (or better still, in most cases, a destructor) because it interacts poorly with other exceptions. For example, what if other parts of the body could throw specific exceptions&amp;nbsp;we want to catch? It would be repetitive and fragile to duplicate the delete statement in every catch block. Also, it's going to be more expensive in runtime cost because an exception will always be thrown, and actually throwing an exception has nonzero cost on every implementation I've heard of; having finally just has the compiler ensure the given code runs in all cases without having to throw an extra exception if none was otherwise being thrown.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=226076" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>Downloading the Express beta compiler</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/09/04/225736.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/09/04/225736.aspx</id><published>2004-09-05T01:19:00Z</published><updated>2004-09-05T01:19:00Z</updated><content type="html">&lt;p&gt;Mark wrote:&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;I have downloaded the Visual C++ Express and have been trying out simple programs. I have to say C++/CLI is amazingly simple and elegant compared to MC++. You guys rock!!!&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;That reminds me to announce (belatedly): Try out the &lt;a href="http://lab.msdn.microsoft.com/express/visualc/default.aspx"&gt;free Express compiler&lt;/a&gt; and play around with the new syntax. Make sure you also &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=afd04ff1-9d16-439a-9a5e-e13eb0341923&amp;amp;displaylang=en"&gt;get the tool refresh&lt;/a&gt; so that you get all the latest parts of the syntax now implemented (e.g., putting .NET Framework types on the stack and having them automatically Disposed for you).&lt;/p&gt; &lt;p&gt;Cool stuff. Check it out.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=225736" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>C++/CLI timeline and one-line description</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/09/03/225462.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/09/03/225462.aspx</id><published>2004-09-03T22:10:00Z</published><updated>2004-09-03T22:10:00Z</updated><content type="html">&lt;p&gt;Ioannis Vranos wrote:&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;There is an upcoming C++/CLI standard (currently draft, official&amp;nbsp; document expected in December 2004)&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Just a note, we now expect to complete work on the C++/CLI standard in March and have it approved by Ecma in June. We wanted the extra time to complete and polish the C++/CLI spec, but another reason for the slip was that the CLI standard (undergoing its second round of work in parallel with the first round of C++/CLI) also slipped.&lt;/p&gt; &lt;p&gt;In case you're wondering "why 6 months," the answer is that Ecma's schedule, you get two shots a year to submit a document for approval (June and December), so any slip is 6 months (or a multiple thereof).&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;C++/CLI is more powerful than C#/CLI (including the upcoming C#/CLI one).&lt;/p&gt; &lt;p&gt;The design ideals of C++/CLI are the following:&lt;/p&gt; &lt;p&gt;1) Bring C++ abilities to CLI.&lt;br /&gt;2) Bring CLI additional features to C++.&lt;br /&gt;3) Leave no room for a lower level language in CLI (*including IL*).&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;BTW, another way of saying this that people seem to find useful is that, as of our Whidbey (VC++ 2005) release, C++ is the systems programming language for .NET.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=225462" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>finally + destructors = best of both worlds</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/09/03/225457.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/09/03/225457.aspx</id><published>2004-09-03T22:03:00Z</published><updated>2004-09-03T22:03:00Z</updated><content type="html">&lt;p dir="ltr"&gt;On comp.lang.c++.moderated, "Howard" &amp;lt;&lt;a href="mailto:alicebt@hotmail.com"&gt;alicebt@hotmail.com&lt;/a&gt;&amp;gt; wrote:&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;You know how we use try { } catch() {} in C++?&amp;nbsp; Well, both Delphi and C# (which was developed with the aid of one of Delphi's main original developers) have another construct, which is "try {} finally {}".&amp;nbsp; The idea is that no matter what else takes place, no matter whether it's a return statement or an exception or simply dropping on out of the try block normally, the finally clause will ALWAYS be executed.&amp;nbsp; (Short of an abnormal program termination, I would suspect.)&amp;nbsp; It might be used like this, for example (if it were available in C++):&lt;/p&gt; &lt;p&gt;Resource* pRes = new Resource;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; // use the allocated resource somehow&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; delete pRes;&lt;br /&gt;}&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Actually, that's valid C++/CLI code -- C++/CLI also supports finally with exactly that syntax. This is described in the current draft spec available via: &lt;a href="http://www.msdn.microsoft.com/visualc/homepageheadlines/ecma/default.aspx"&gt;http://www.msdn.microsoft.com/visualc/homepageheadlines/ecma/default.aspx&lt;/a&gt; . See section 16.4, and search for "finally" throughout (there are other spots that specify how it interacts with other language features like goto).&lt;/p&gt; &lt;p&gt;This is in addition to destructors -- as another responder, Mike Wahler, noted, you can also get a similar effect with destructors:&lt;/p&gt; &lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt; &lt;p&gt;C++ already has this, via destructors.&amp;nbsp; When 'throw' causes a scope to be exited, all automatic objects are destroyed, and any destructors will run. ALWAYS.&amp;nbsp; So just wrap that 'new' and 'delete' in a class, create an automatic object of that type, and you have the same mechanism.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;One advantage of finally is visual locality of reference -- the code is right there in the function instead of off in a different place (a destructor definition). One advantage of destructors is that RAII objects encapsulate and automatically manage ownership. In your example above, I would prefer to use a destructor (by writing auto_ptr&amp;lt;Resource&amp;gt; instead of Resource*, you don't need a finally at all). You can use either facility, of course.&lt;/p&gt; &lt;p&gt;To me, this is yet another case where C++ gives you the best of both worlds. Having both is better than having either alone (in languages that have only finally or only destructors). Note this is very closely related to the Dispose pattern I mentioned in a recent post, because the Dispose pattern relies on finally for things that destructors do better, so it's good to have destructors. For other things, finally is better, so it's good to have finally.&lt;/p&gt; &lt;p&gt;Herb&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=225457" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>An update to the C++/CLI draft</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/08/03/207298.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/08/03/207298.aspx</id><published>2004-08-03T20:36:00Z</published><updated>2004-08-03T20:36:00Z</updated><content type="html">&lt;P&gt;An interim updated snapshot of the C++/CLI draft standard is now available:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://download.microsoft.com/download/9/9/c/99c65bcd-ac66-482e-8dc1-0e14cd1670cd/C++-CLI%20Standard.pdf"&gt;C++/CLI Language Specification (Working Draft 1.5, June 2004)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Related links:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/visualc/homepageheadlines/ecma/default.aspx"&gt;C++/CLI home page on MSDN&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/visualc/"&gt;Visual C++ home page on MSDN&lt;/A&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=207298" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>A new article on C++/CLI</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/08/03/207293.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/08/03/207293.aspx</id><published>2004-08-03T20:28:00Z</published><updated>2004-08-03T20:28:00Z</updated><content type="html">&lt;P&gt;&lt;A href="http://weblogs.asp.net/kennykerr/archive/2004/07/29/200596.aspx"&gt;Kenny Kerr announced&lt;/A&gt; a cool article he wrote about C++/CLI on MSDN:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/VS05Cplus.asp"&gt;C++/CLI: The Most Powerful Language for .NET Programming&lt;/A&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=207293" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>C++ RAII compared with Java Dispose pattern</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/07/31/203137.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/07/31/203137.aspx</id><published>2004-07-31T20:49:00Z</published><updated>2004-07-31T20:49:00Z</updated><content type="html">&lt;P&gt;Earlier today I wrote that: &amp;#8220;&lt;SPAN style="FONT-SIZE: 12pt"&gt;The C++ destructor model is exactly the same as the Dispose and using patterns, except that it is far easier to use and a direct language feature and correct by default, instead of a coding pattern that is off by default and causing correctness or performance problems when it is forgotten.&amp;#8220; &lt;/SPAN&gt;Niclas Lindgren agreed with the post but added:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;...although you use the word coding pattern to confuse something simple with something that sounds trickier. &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;I was referring to the Dispose coding pattern. For the benefit of those who aren't familiar with the Java Dispose pattern, let me give two examples: first a simple one with just one resource, then a more complex one with three resources. The examples use .NET Framework types.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;Consider this C++/CLI code:&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;String^ ReadFirstLineFromFile( String^ path ) {&lt;BR&gt;&#xB;&amp;nbsp; StreamReader r(path);&lt;BR&gt;&amp;nbsp; return r.ReadLine();&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;The minimal C# equivalent is the &amp;#8220;using&amp;#8220; patterns which semiautomates the Java Dispose pattern (I'm showing the {} around the block to be explicit that there's a block, although since there's only one statement you don't really need them):&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;String ReadFirstLineFromFile( String path ) {&lt;BR&gt;&amp;nbsp; using ( StreamReader r = new StreamReader(path) ) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r.ReadLine();&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;The minimal Java equivalent is the Dispose pattern (this is what C#'s &amp;#8220;using&amp;#8220; generates under the covers):&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;String ReadFirstLineFromFile( String path ) {&lt;BR&gt;&amp;nbsp; StreamReader r = null;&lt;BR&gt;&amp;nbsp; String s = null;&lt;BR&gt;&amp;nbsp; try {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = new StreamReader(path);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; s = r.ReadLine();&lt;BR&gt;&amp;nbsp; } finally {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ( r != null ) r.Dispose();&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;&amp;nbsp; return s;&lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;I called this Dispose pattern &amp;#8220;a coding pattern that is off by default and causing correctness or performance problems when it is forgotten.&amp;#8220; That is all true. You have to remember to write it, and you have to write it correctly. In constrast, in C++ you just put stuff in scopes or delete it when you're done, whether said stuff has a nontrivial destructor or not.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;But that example is still flattering to the Dispose pattern, because there's only one resource. Consider as a second example this C++ code that opens a message queue and echoes one message to&amp;nbsp;two target queues (a main queue and a backup queue) -- and automatically correctly closes the queues:&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;void Transfer() {&lt;BR&gt;&amp;nbsp; MessageQueue&amp;nbsp;source( "server\\sourceQueue" );&lt;BR&gt;&amp;nbsp; String^ qname = (String^)source.Receive().Body;&lt;BR&gt;&amp;nbsp; MessageQueue&amp;nbsp; dest1( "server\\" + qname ), dest2( "backup\\" + qname );&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;&amp;nbsp; Message^ message = source.Receive();&lt;BR&gt;&amp;nbsp; dest1.Send( message );&lt;BR&gt;&amp;nbsp;&amp;nbsp;dest2.Send( message );&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;The minimal correct Java equivalent is:&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;void Transfer() {&lt;BR&gt;&amp;nbsp; MessageQueue source = null, dest1 = null, dest2 = null;&lt;BR&gt;&amp;nbsp; try {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; source = new MessageQueue( "server\\sourceQueue" );&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String qname = (String)source.Receive().Body;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dest1 = new MessageQueue( "server\\" + qname );&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dest2 = new MessageQueue( "backup\\" + qname );&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Message message = source.Receive();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dest1.Send( message );&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dest2.Send( message );&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;&amp;nbsp; finally {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if( dest2 != null ) { dest2.Dispose(); }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if( dest1 != null ) { dest1.Dispose(); }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if( source != null ) { source.Dispose(); }&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;In this case, the queues will be freed up eventually when the garbage collector runs (which it probably will). In the meantime, we've tied up scarce resources. Worse, we've tied up resources &lt;EM&gt;on other machines&lt;/EM&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;So the C++ destructor model is exactly the same as the Dispose and using patterns, except that it is far easier to use and a direct language feature and correct by default, instead of a coding pattern that is off by default and causing correctness or performance problems when it is forgotten.&lt;/SPAN&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=203137" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>More on % (managed reference)</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/07/31/203133.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/07/31/203133.aspx</id><published>2004-07-31T20:28:00Z</published><updated>2004-07-31T20:28:00Z</updated><content type="html">&lt;P&gt;Keith Duggar asked:&lt;/P&gt;
&lt;P&gt;Ok, I'm missing a basic point here. So, out of curiousity:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;1) Is there any overhead associated with using 'T%' in place of 'T&amp;amp;'?&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No.&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;2) Are there things that can be done with 'T&amp;amp;' that cannot be done with 'T%'&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes: A % can only exist on the stack (a CLI limitation). Cases where a C++ program puts a &amp;amp; on the heap are relatively rare -- you'd have to a have a reference member of an object, which is problematic in general -- but % can't be a drop-in replacement for that use of &amp;amp;.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=203133" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry><entry><title>Using % and &amp;</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/b/hsutter/archive/2004/07/31/203131.aspx" /><id>http://blogs.msdn.com/b/hsutter/archive/2004/07/31/203131.aspx</id><published>2004-07-31T20:05:00Z</published><updated>2004-07-31T20:05:00Z</updated><content type="html">&lt;P&gt;This post brings together three related questions and answers posted recently on comp.lang.c++.moderated. They all have to do with the relationship between % (tracking reference) and &amp;amp; (unmanaged reference).&lt;/P&gt;
&lt;P&gt;First, Dietmar Kuehl asked about this C++/CLI code:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;template &amp;lt;typename T&amp;gt; void f(T&amp;amp;) {}&lt;/P&gt;
&lt;P&gt;int main() {&lt;BR&gt;&amp;nbsp; int^ ptr = gcnew int(0);&lt;BR&gt;&amp;nbsp; f(*ptr);&amp;nbsp; &lt;EM&gt;// error&lt;/EM&gt;&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The solution is to change f's parameter from T&amp;amp; to T% . When referring to ab object on the GC heap, just as instead of an unmanaged pointer you need a handle ^&amp;nbsp;(which tracks during GC) you also instead of an unmanaged reference need a tracking reference %&amp;nbsp;(which tracks during GC).&lt;/P&gt;
&lt;P&gt;Trying to pass a deref'd ^ (which tracks) to a &amp;amp; (which doesn't track) is the same kind of error as trying to pass a const object (which is const) to a non-const &amp;amp; (which isn't const):&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;template &amp;lt;typename T&amp;gt; void f(T&amp;amp;) {}&lt;/P&gt;
&lt;P&gt;int main() {&lt;BR&gt;&amp;nbsp; const int i;&lt;BR&gt;&amp;nbsp; f(i);&amp;nbsp;&amp;nbsp;&lt;EM&gt;// similar category of error&lt;/EM&gt;&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So in general, to be able to bind also to objects on the GC heap, a function taking a &amp;amp; parameter only needs to change the &amp;amp; to % instead (the body generally won't care).&lt;/P&gt;
&lt;P&gt;In our STL.NET project we're providing the standard algorithms with % instead of &amp;amp; parameter types, which doesn't affect the meaning but allows them to be used with all the types they work with already plus also managed types.&lt;/P&gt;
&lt;P&gt;If you can't change the function and must call a function with a plain &amp;amp; (including if you just want to call the standard algorithms directly for some reason), you can still do it by using the RAII pin_ptr so that tracking isn't needed for the duration:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;template &amp;lt;typename T&amp;gt; void f(T&amp;amp;) {}&lt;/P&gt;
&lt;P&gt;int main() {&lt;BR&gt;&amp;nbsp; int^ ptr = gcnew int(0);&lt;BR&gt;&amp;nbsp; f( *pin_ptr&amp;lt;int&amp;gt;(&amp;amp;*ptr) );&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that normally you don't need that extra &amp;amp;* -- here it's needed because we're dealing with a boxed value type and want to reach into the box.&lt;/P&gt;
&lt;P&gt;Separately, Sebastian Kaliszewski asked about this code:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;template &amp;lt;typename T&amp;gt; void f(T%) {}&lt;/P&gt;
&lt;P&gt;int main() {&lt;BR&gt;&amp;nbsp; int* ptr = new int(0);&lt;BR&gt;&amp;nbsp; f(^ptr);&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This code does work, except that you dereference a ^ with * (just like you dereference all pointerlike abstractions), so change the call to f(*ptr); and it works just fine.&lt;/P&gt;
&lt;P&gt;Finally, Thorsten Ottosen asked whether the following couldn't be possible:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;template&amp;lt; class T &amp;gt; void foo( T&amp;amp; );&lt;BR&gt;template&amp;lt; class T &amp;gt; void bar( T% );&lt;/P&gt;
&lt;P&gt;int main()&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; int^ gc&amp;nbsp; = gcnew int(0);&lt;BR&gt;&amp;nbsp;&amp;nbsp; int* ptr = new int(0);&lt;BR&gt;&amp;nbsp;&amp;nbsp; foo( *gc ); //&amp;nbsp;1&lt;BR&gt;&amp;nbsp;&amp;nbsp; foo( *ptr ); //&amp;nbsp;2&lt;BR&gt;&amp;nbsp;&amp;nbsp; bar( *gc ); //&amp;nbsp;3&lt;BR&gt;&amp;nbsp;&amp;nbsp; bar( *ptr ); // 4&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The answer is that all of those work as written, except for #1 which only needs a pin_ptr.&amp;nbsp;A pin_ptr is an RAII helper that pins an object on the GC heap&amp;nbsp;for the lifetime of the pin_ptr. Pinning ensures that the GC doesn't move the object around, during which time it's safe to take a plain old unmanaged pointer to it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; foo( *pin_ptr&amp;lt;int&amp;gt;(&amp;amp;*gc) ); // 1 (fixed)&lt;/P&gt;
&lt;P&gt;Or, a little more clearly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; pin_ptr&amp;lt;int&amp;gt; pp = &amp;amp;*gc;&amp;nbsp; // take a pin&lt;BR&gt;&amp;nbsp;&amp;nbsp; f( *pp );&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // call any old function taking * or &amp;amp;&lt;/P&gt;
&lt;P&gt;The % can bind to any object whether the object moves (i.e., is on the gc heap) or not -- thus % can bind to a strict superset of what a &amp;amp; can bind to. (The &amp;amp; can bind directly to any object not on the gc heap, but requires a pin to bind to one on the gc heap.)&lt;/P&gt;
&lt;P&gt;Thorsten also asked:&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;What has been the rationale for the current design?&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In short, to provide pointerlike and referencelike abstractions that work for the general case of garbage-collected memory, which includes compacting GCs that move objects around (so the new pointer/reference abstractions have to be able to track), as close to unmanaged pointers/references in function and syntax as possible.&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=203131" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/hsutter/ProfileUrlRedirect.ashx</uri></author></entry></feed>