<?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/hsutter/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/hsutter/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2003-11-23T02:02:00Z</updated><entry><title>Movin' Out</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/10/04/237558.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</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/hsutter/archive/2004/09/06/226076.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>Downloading the Express beta compiler</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/09/04/225736.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>C++/CLI timeline and one-line description</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/09/03/225462.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>finally + destructors = best of both worlds</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/09/03/225457.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>An update to the C++/CLI draft</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/08/03/207298.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>A new article on C++/CLI</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/08/03/207293.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>C++ RAII compared with Java Dispose pattern</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/07/31/203137.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>More on % (managed reference)</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/07/31/203133.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>Using % and &amp;</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/07/31/203131.aspx" /><id>http://blogs.msdn.com/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;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/members/hsutter.aspx</uri></author></entry><entry><title>The object lifetime issue is the same in C++, Java, C#</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2004/07/31/203126.aspx" /><id>http://blogs.msdn.com/hsutter/archive/2004/07/31/203126.aspx</id><published>2004-07-31T19:51:00Z</published><updated>2004-07-31T19:51:00Z</updated><content type="html">&lt;DIV class=Section1&gt;
&lt;P&gt;&lt;FONT face="Times New Roman" size=3&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;I&amp;#8217;m going to echo various interesting pieces of newsgroup discussion here. Check out the newsgroups for the full threads.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Times New Roman" size=3&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;On comp.lang.c++.moderated, Dietmar Kuehl &amp;lt;dietmar_kuehl@yahoo.com&amp;gt; wrote:&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;FONT face="Times New Roman" size=3&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;I have thought about integrating GC into C++ for quite some time. My personal conclusion was, however, that deterministic destruction and garbage collection don't really mix. The major issue is that in a truly garbage collected system you could simple use whatever reference you get hold of. This promise does not hold if deterministic destructors are involved.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Times New Roman" size=3&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;I would dispute that characterization of a "truly garbage-collected system" -- it certainly doesn't describe either Java or .NET. In Java and C#, you routinely have objects to which you still have references but whose lifetimes have ended via the Dispose and using patterns, and you have to know (or the member functions have to check) that you're not using an already-disposed object.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Times New Roman" size=3&gt;&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.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Times New Roman" size=3&gt;&lt;SPAN style="FONT-SIZE: 12pt"&gt;So yes, in ISO C++ or Java or C# or C++ with C++/CLI, if you use a * or ^ or object reference to an object which has been Disposed/destroyed, you have exactly the same issue in all of those languages and environments: You need to be aware when pointers/references are invalidated. It's just the same as C++'s plain old pointer invalidation problem.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=203126" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/members/hsutter.aspx</uri></author></entry><entry><title>How much C++/CLI syntax do you need to use all of the .NET Frameworks?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2003/12/15/53526.aspx" /><id>http://blogs.msdn.com/hsutter/archive/2003/12/15/53526.aspx</id><published>2003-12-16T01:13:00Z</published><updated>2003-12-16T01:13:00Z</updated><content type="html">&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;p&gt;
        The short answer to the question in the title is: In Whidbey, &lt;strong&gt;^&lt;/strong&gt; and &lt;strong&gt;gcnew&lt;/strong&gt; are
        mostly all you're likely to use, if all you're doing with .NET is consuming (using)
        .NET&amp;#160;Frameworks types and services. Post-Whidbey, you could even dispense with
        those and use &lt;strong&gt;*&lt;/strong&gt; and &lt;strong&gt;new&lt;/strong&gt;, because we intend to&amp;#160;support
        allocating CLI objects on the native heap with &lt;strong&gt;new&lt;/strong&gt; and pointing at
        them with &lt;strong&gt;*&lt;/strong&gt;'s.
    &lt;/p&gt;
    &lt;p&gt;
        Alisdair Meredith recently asked me: 
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            &lt;font size="2"&gt; 
            &lt;p&gt;
                &lt;em&gt;"I am still trying to work out the target audience [...] I can see 2 ways of approaching
                the CLI binding:&lt;br /&gt;
                &lt;br /&gt;
                "i/ I want to write all my code in ISO conformant C++, and will use CLI&amp;#160;binding
                as a public interface layer to handle targetting a .NET&amp;#160;environment.&lt;br /&gt;
                &lt;br /&gt;
                "ii/ I write all my code in this new C++ .NET dialect, as I only want&amp;#160;to target
                .NET, and I get to use all the familiar goodies of C++ along&amp;#160;with all the new
                goodies in .NET and the binding.&lt;br /&gt;
                &lt;br /&gt;
                "Clearly, market (ii) will be much happier with the more sugary&amp;#160;extension such
                as for each, abstract etc."&lt;/em&gt; 
            &lt;/p&gt;
            &lt;/font&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;font size="2"&gt; 
    &lt;p&gt;
        This is a very good point, and I'll use it as a hook to talk about the two major categories
        of use I see for the C++/CLI extensions. 
    &lt;/p&gt;
    &lt;p&gt;
        I do think the current design serves both markets well. In particular, note that nearly
        all of the extensions will only be used by people &lt;em&gt;authoring&lt;/em&gt; CLI types, so: 
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            Group (ii) is going to go whole hog using .NET and &lt;em&gt;authoring&lt;/em&gt; new types, and
            is well served by more elegant/sugary syntax. 
        &lt;/li&gt;
        &lt;li&gt;
            Group (i) is simply going to be &lt;em&gt;consuming&lt;/em&gt; the CLI types, probably not author
            them, and will probably only ever need &lt;strong&gt;^&lt;/strong&gt; and &lt;strong&gt;gcnew&lt;/strong&gt;. 
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
        What's more, in the post-Whidbey timeframe when we complete the support for allocating
        any object (including CLI objects) on the native heap using &lt;strong&gt;new&lt;/strong&gt;,
        people could actually consume CLI / .NET Frameworks types seamlessly in their application
        without any new syntax. Of course, under the covers we'll be creating a proxy object
        each time, so there's a probably-slight performance impact to doing it this way, but
        it's worth noting that you'll be able to do this eventually. 
    &lt;/p&gt;
    &lt;p&gt;
        Finally, I should also add that we intend to emit warnings by default when any of
        the extensions are used with native (i.e., normal C++) types, so that people will
        know when they are using a nonstandard (well, non-ISO-C++-standard at any ware) extension
        in a class that would otherwise be portable. 
    &lt;/p&gt;
    &lt;/font&gt;
&lt;/body&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=53526" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/members/hsutter.aspx</uri></author></entry><entry><title>Why not limit GC to CLI types?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2003/12/05/53523.aspx" /><id>http://blogs.msdn.com/hsutter/archive/2003/12/05/53523.aspx</id><published>2003-12-05T17:44:00Z</published><updated>2003-12-05T17:44:00Z</updated><content type="html">&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;p&gt;
        Edward Diener made the observation (emphasis is mine):
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            &lt;em&gt;I would much rather the syntax remained as consistent with C++ as possible and
            the places where CLI diverged from C++ occur &lt;strong&gt;because GC types are different
            than C++ types in that they are automatically garbage collected&lt;/strong&gt;, be the primary
            focus of a programmer's understanding.&lt;/em&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
        In my previous blog entry, I listed two reasons not to conflate the type category
        with where objects of that type can be allocated, namely to let us in the future support
        allocating native types on the GC heap, and CLI types on the native heap. The decision
        to garbage-collect or not&amp;#160;should not be per-type (as it was in MC++), but per-object.
    &lt;/p&gt;
    &lt;p&gt;
        To that, I should also add that "CLI types" already does not mean "garbage-collected
        types." In particular, the CLI knows about two kinds of types: reference types, and
        value types. First, value types are never garbage-collected in their unboxed form.
        Second, nearly all programs have objects of reference and boxed value type that are
        intentionally never collected because they are used right through to the termination
        of the program; they are one reason&amp;#160;why the CLI GC is generational, and why the
        CLI has features for controlling whether or not such objects ever get the finalizers
        run, for example.
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            &lt;em&gt;Now C++ programmers must use a new syntax, '^' and '%', where they are already
            use to pointer and reference syntax. This only creates more headaches for C++ programmers
            using CLI.&lt;/em&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
        That's possible, but in our experience so far (having tried to do it that way for
        two releases now) we've found that users are far more apt to be confused by failing
        to surface an important distinction. This is the balancing act of language design:
        it is just as bad to fail to surface an important distinction (which creates a trap
        and a usability barrier)&amp;#160;as it is to needlessly surface a distinction that could
        have been hidden from the programmer (which creates clutter and a different kind of
        usability barrier).
    &lt;/p&gt;
&lt;/body&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=53523" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/members/hsutter.aspx</uri></author></entry><entry><title>Why "gcnew" instead of just plain "new"?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2003/12/05/53522.aspx" /><id>http://blogs.msdn.com/hsutter/archive/2003/12/05/53522.aspx</id><published>2003-12-05T08:47:00Z</published><updated>2003-12-05T08:47:00Z</updated><content type="html">&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;p&gt;
        Edward Diener asked:
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            &lt;em&gt;I don't see why you don't use just 'new' rather than 'gcnew' ? The type of T will
            determine whether the obect is allocated in GC collected memory or the C++ heap.&lt;/em&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
        That's one way to design it. Interestingly, the existing "Managed C++" does use just
        plain old &lt;strong&gt;new&lt;/strong&gt; for everything, with pretty much the semantics described
        above: In MC++, the expression&amp;#160;&lt;strong&gt;new T&lt;/strong&gt; allocates a &lt;strong&gt;T&lt;/strong&gt; object
        on the CLI&amp;#160;gc heap if &lt;strong&gt;T&lt;/strong&gt; is a CLI type,&amp;#160;and on the native
        heap if &lt;strong&gt;T&lt;/strong&gt; is a native type.
    &lt;/p&gt;
    &lt;p&gt;
        Briefly, this model was limiting, and it was confusing to users. Just to give a taste
        for what happens when you go down this path, the next question you hit is "what is&amp;#160;a &lt;strong&gt;T*&lt;/strong&gt;?"
        In particular, consider:
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;T* t = new T;&lt;/strong&gt;
    &lt;/p&gt;
    &lt;p&gt;
        Is &lt;strong&gt;t&lt;/strong&gt; a pointer to an object in gc'd memory, which means that the
        object can move? Or is it a pointer&amp;#160;to an object in&amp;#160;native memory that doesn't
        move? "That's easy!" one might say. "We could deduce that &lt;strong&gt;t&lt;/strong&gt; points
        into gc'd memory if and only if &lt;strong&gt;T&lt;/strong&gt; is a CLI type." In the above code
        statement, that's true, and Managed C++ used "defaulting rules" to make it mean exactly
        that. In particular, in MC++ the &lt;strong&gt;T*&lt;/strong&gt; above means the same as the following
        longer version if &lt;strong&gt;T&lt;/strong&gt; is&amp;#160;a CLI type:
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;T __gc * t = __gc new T;&lt;/strong&gt;
    &lt;/p&gt;
    &lt;p&gt;
        But it turns out that when going along this path&amp;#160;you do need that &lt;strong&gt;__gc&lt;/strong&gt; pointer
        decoration (or its moral equivalent) sometimes, even if much of the time you can make
        it optional by defaulting it based on the pointed-at type. In particular, you need
        it&amp;#160;for&amp;#160;combinations of pointers, including the simplest case:
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;int** ppi;&amp;#160; // what is this?&lt;/strong&gt;
    &lt;/p&gt;
    &lt;p&gt;
        Consider: Where is the &lt;strong&gt;int*&lt;/strong&gt; that the &lt;strong&gt;int**&lt;/strong&gt; is pointing
        to?&amp;#160;The answer cannot be deduced from the type, because an &lt;strong&gt;int*&lt;/strong&gt; can
        exist on the gc'd heap or on the native heap. Therefore both &lt;strong&gt;int* __gc *&lt;/strong&gt; (a
        pointer to an &lt;strong&gt;int*&lt;/strong&gt; on the gc heap) and &lt;strong&gt;int**&lt;/strong&gt; (a
        pointer to an &lt;strong&gt;int*&lt;/strong&gt; on the native heap) are valid pointer types, and&amp;#160;therefore
        you need to be able to distinguish between them.
    &lt;/p&gt;
    &lt;p&gt;
        In practice, this has been a great source of user confusion&amp;#160;because programmers
        are never really sure where to put the &lt;strong&gt;__gc&lt;/strong&gt;. So what we have observed
        many users do, time and again, is simply add &lt;strong&gt;__gc&lt;/strong&gt;'s until the code
        compiles -- whether the &lt;strong&gt;__gc&lt;/strong&gt; is needed (or correct) or not.
    &lt;/p&gt;
    &lt;p&gt;
        So you can use defaulting rules to hide the &lt;strong&gt;__gc&lt;/strong&gt; some of the time,
        but not all of the time. And the "some of the time" itself is at a cost, namely that
        it arbitrarily restricts native types from ever being allocated on the gc heap (i.e.,
        from being garbage collected), and restricts CLI types from ever being allocated on
        the native heap.
    &lt;/p&gt;
    &lt;p&gt;
        He continued:
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            &lt;em&gt;The only reason for disambiguating them is if, in the future, CLI will allow allocating
            a GC object on the C++ heap and/or allocating a non-GC object in the GC collected
            memory area. But it baffles me why one would ever change the CLI bindings to do that.&lt;/em&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
        Bingo. In addition to the clarity and usability issue above, you hit on&amp;#160;a primary
        reason for not just using unadorned &lt;strong&gt;new&lt;/strong&gt;: It conflates two ideas that
        ought to be independent, namely the idea of what kind of type &lt;strong&gt;T&lt;/strong&gt; is
        and the idea of where &lt;strong&gt;T&lt;/strong&gt; objects are allocated. In particular, we
        will definitely in the future allow allocating an object of any type on the gc heap
        or on the native heap.
    &lt;/p&gt;
    &lt;p&gt;
        Why do we feel the need to support allocating an object of native type on the GC heap?
        Because customers ask for it. "You've got a great garbage collector in there," they
        say, "so why can't I use it to garbage-collect the objects I already have today?"
        That's a reasonable question.
    &lt;/p&gt;
    &lt;p&gt;
        Why do we feel the need to support allocating an object of CLI type on the native
        heap? Because there are lots of native templated libraries out there today, many of
        which &amp;#160;that internally allocate objects on the native heap (because, after all,
        they know nothing about the GC heap).&amp;#160;We ought to be able to leverage all those
        existing libraries and use them unmodified&amp;#160;also with&amp;#160;the CLI types. True,&amp;#160;for
        some such libraries it may be enough simply to give a different meaning to &lt;strong&gt;new&lt;/strong&gt; depending
        on the type of &lt;strong&gt;T&lt;/strong&gt; as originally proposed above.&amp;#160;But note that
        such libraries might not only allocate objects using &lt;strong&gt;new&lt;/strong&gt;, but may
        rely on the resulting pointers to support things like pointer arithmetic that MC++'s &lt;strong&gt;__gc*&lt;/strong&gt;'s
        and C++/CLI's &lt;strong&gt;^&lt;/strong&gt;'s cannot support, and so they would still be broken
        for CLI types.
    &lt;/p&gt;
    &lt;p&gt;
        Of the two, I see gc'ing native objects as the more compelling and mainstream use
        case.
    &lt;/p&gt;
&lt;/body&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=53522" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/members/hsutter.aspx</uri></author></entry><entry><title>C++/CLI keywords: Under the hood</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/hsutter/archive/2003/11/23/53519.aspx" /><id>http://blogs.msdn.com/hsutter/archive/2003/11/23/53519.aspx</id><published>2003-11-23T10:02:00Z</published><updated>2003-11-23T10:02:00Z</updated><content type="html">&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;p&gt;
        C++/CLI specifies several keywords as extensions to ISO C++. The way they are handled
        falls into five major categories, where only the first impacts the meaning of existing
        ISO C++ programs. 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;1. Outright reserved words&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        As of this writing (November 22, 2003, the day after we released the candidate base
        document), C++/CLI is down to only three reserved words: 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;&amp;#160; gcnew&amp;#160;&amp;#160; generic&amp;#160;&amp;#160; nullptr&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        An existing program that uses these words as identifiers and wants to use C++/CLI
        would have to rename the identifiers. I'll return to these three again at the end. 
    &lt;/p&gt;
    &lt;p&gt;
        All the other keywords, below, are contextual keywords that do not conflict with identifiers.
        Any legal ISO C++ program that already uses the names below as identifiers will continue
        to work as before; these keywords are not reserved words. 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;2. Spaced keywords&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        One implementation technique we are using is to specify some keywords that include
        embedded whitespace. These are safe: They can't possibly conflict with any user identifiers
        because no C++ program can create an identifier that contains whitespace characters.
        [I'll omit the obligatory reference to Bjarne's classic April Fool's joke article
        on the whitespace operator. :-) But what I'm saying here is true, not a joke.] 
    &lt;/p&gt;
    &lt;p&gt;
        Currently these are: 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;&amp;#160; for each&lt;br /&gt;
        &amp;#160; enum class/struct&lt;br /&gt;
        &amp;#160; interface class/struct&lt;br /&gt;
        &amp;#160; ref class/struct&lt;br /&gt;
        &amp;#160; value class/struct&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        For example, "&lt;strong&gt;ref class&lt;/strong&gt;" is a single token in the lexer, and programs
        that have a type or variable or namespace named &lt;strong&gt;ref&lt;/strong&gt; are entirely
        unaffected. (Somewhat amazingly, even most &lt;em&gt;macros&lt;/em&gt; named &lt;strong&gt;ref&lt;/strong&gt; are
        unaffected and don't affect C++/CLI, unless coincidentally the next token in the macro's
        definition line happens to be &lt;strong&gt;class&lt;/strong&gt; or &lt;strong&gt;struct&lt;/strong&gt;; more
        on this near the end.) 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;3. Contextual keywords that can never appear where an identifier could appear&lt;/strong&gt;
    &lt;/p&gt;
    &lt;p&gt;
        Another technique we used was to define some keywords that can only appear in positions
        in the language grammar where today nothing may appear. These too are safe: They can't
        conflict with any user identifiers because no identifiers could appear where the keyword
        appears, and vice versa. Currently these are: 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;&amp;#160; abstract&amp;#160;&amp;#160;&amp;#160; finally&amp;#160;&amp;#160;&amp;#160; in&amp;#160;&amp;#160;&amp;#160;
        override&amp;#160;&amp;#160;&amp;#160; sealed&amp;#160;&amp;#160;&amp;#160; where&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        For example, &lt;strong&gt;abstract&lt;/strong&gt; as a C++/CLI keyword can only appear in a class
        definition after the class name and before the base class list, where nothing can
        appear today: 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;&amp;#160; ref class X abstract : B1, B2 { &lt;/strong&gt;// ok, can only be the keyword&lt;br /&gt;
        &lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; int abstract;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/strong&gt;//
        ok, just another identifier&lt;br /&gt;
        &lt;strong&gt;&amp;#160; };&lt;br /&gt;
        &lt;br /&gt;
        &lt;/strong&gt;&lt;strong&gt;&amp;#160; class abstract { };&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/strong&gt;//
        ok, just another identifier&lt;br /&gt;
        &lt;strong&gt;&amp;#160; namespace abstract { /*...*/ }&amp;#160; &lt;/strong&gt;// ok, just another identifier 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;4. Contextual keywords that can appear where an identifier could appear&lt;/strong&gt;
    &lt;/p&gt;
    &lt;p&gt;
        Some keywords can appear in a grammar position where an identifier could also appear,
        and this is the case that needs some extra attention. There are currently five keywords
        in this category: 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;&amp;#160; delegate&amp;#160;&amp;#160;&amp;#160; event&amp;#160;&amp;#160;&amp;#160; initonly&amp;#160;&amp;#160;&amp;#160;
        literal&amp;#160;&amp;#160;&amp;#160; property&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        In such grammar positions, when the compiler encounters a token that is spelled the
        same as one of these keywords, the compiler can't know whether the token means the
        keyword or whether it means an identifier until it first does some further lookahead
        to consider later tokens. For example, consider the following inside a class scope: 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;&amp;#160; property int x;&amp;#160; &lt;/strong&gt;// ok, here property is the contextual
        keyword&lt;br /&gt;
        &lt;strong&gt;&amp;#160; property x;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/strong&gt;// ok, if property
        is the name of a type 
    &lt;/p&gt;
    &lt;p&gt;
        Now imagine you're a compiler: What do you do when you hit the token &lt;strong&gt;property&lt;/strong&gt; as
        the first token of the next class member declaration? There's not enough information
        to decide for sure whether it's an identifier or a keyword without looking further
        ahead, and C++/CLI has to specify the decision procedure -- the rules for deciding
        whether it's a keyword or an identifier. As long as the user doesn't make a mistake
        (i.e., as long as it's a legal program with or without C++/CLI) the answer is clear,
        because there's no ambiguity. 
    &lt;/p&gt;
    &lt;p&gt;
        But now the "quality of diagnostics" issue&amp;#160;rears its head, in this category of
        contextual keywords&amp;#160;and this&amp;#160;category only: What if the user makes a mistake?
        For example: 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;&amp;#160; property x;&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // error, if no
        type "property" exists 
    &lt;/p&gt;
    &lt;p&gt;
        Let's say that we set up a disambiguation rule with the following general structure
        (I'll get specific in just a moment): 
    &lt;/p&gt;
    &lt;p&gt;
        &amp;#160; 1. Assume one case and try to parse what comes next that way.&lt;br /&gt;
        &amp;#160; 2. If that fails, then assume the other case and try again.&lt;br /&gt;
        &amp;#160; 3. If that fails, then issue a diagnostic. 
    &lt;/p&gt;
    &lt;p&gt;
        In the case of &lt;strong&gt;property x;&lt;/strong&gt; when there's no type in scope named &lt;strong&gt;property&lt;/strong&gt;,
        both #1 and #2 will fail and the question is: When we get to the diagnostic in case
        #3, what error message is the user likely to see? The answer almost certainly is,
        a message that applies to the second "other" case. Why? Because the compiler already
        tried the first case, failed, backed up and tried the second "other" case -- and it's
        still in that latter mode with all that context when it finally realizes that didn't
        work either and now it has to issue the diagnostic. So by default, absent some (often
        prodigious) amount of extra work inside the compiler, the diagnostic that you'll get
        is the one that's easiest to give, namely the one for the case the compiler was most
        recently pursuing, namely the "other" case mentioned in #2 -- because the compiler
        already gave up on the first case, and went down the other path instead. 
    &lt;/p&gt;
    &lt;p&gt;
        So let's get specific. Let's say that the rule we picked was: 
    &lt;/p&gt;
    &lt;p&gt;
        &amp;#160; 1. Assume that it's an identifier and try to parse it that way&lt;br /&gt;
        &amp;#160;&amp;#160;&amp;#160;&amp;#160; (i.e., by default assume no use of the keyword extension).&lt;br /&gt;
        &amp;#160; 2. If that fails, then assume that it's the keyword and try again.&lt;br /&gt;
        &amp;#160; 3. If that fails, then issue a diagnostic. 
    &lt;/p&gt;
    &lt;p&gt;
        Under that rule, what's the diagnostic the user gets on an illegal declaration of &lt;strong&gt;property
        x;&lt;/strong&gt;? One that's in the context of #2 (keyword), something like "illegal property
        declaration," perhaps with a "the type '&lt;strong&gt;x&lt;/strong&gt;' was not defined" or a
        "you forgot to specify the type for property '&lt;strong&gt;x&lt;/strong&gt;'" in there somewhere. 
    &lt;/p&gt;
    &lt;p&gt;
        On the other hand, let's say that the rule we picked was: 
    &lt;/p&gt;
    &lt;p&gt;
        &amp;#160; 1. Assume that it's the keyword and try to parse it that way.&lt;br /&gt;
        &amp;#160; 2. If that fails, then assume that it's an identifier and try again.&lt;br /&gt;
        &amp;#160; 3. If that fails, then issue a diagnostic. 
    &lt;/p&gt;
    &lt;p&gt;
        Under this rule, the diagnostic that's easy to give is something like "the type '&lt;strong&gt;property&lt;/strong&gt;'
        was not defined." 
    &lt;/p&gt;
    &lt;p&gt;
        Which is better? 
    &lt;/p&gt;
    &lt;p&gt;
        This illustrates why it's very important to consider common mistakes and whether the
        diagnostic the user will get really applies to what he was probably trying to do.
        In this case, it's probably better to emit something like "no type named '&lt;strong&gt;property&lt;/strong&gt;'
        exists" than "you forgot to specify a type for your property named '&lt;strong&gt;x&lt;/strong&gt;'"
        -- the former is more likely to address what the user was trying to do, and it also
        happens to preserve the diagnostics for ISO C++ programs. 
    &lt;/p&gt;
    &lt;p&gt;
        More broadly, of course, there are other rules you can use than the two "try one way
        then try the other" variants shown above. But I hope this helps to give the flavor
        for the 'quality of diagnostics' problem. 
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            Aside: There's usually no&amp;#160;ambiguity in the case of &lt;strong&gt;property&lt;/strong&gt; (or
            the other keywords in this category); the only case I know of where you could write
            legal C++/CLI code where one of these five keywords&amp;#160;could be legally interpreted
            both ways, both as the keyword and as an identifier,&amp;#160;is when the type has a global
            qualification. Here's an&amp;#160;example courtesy of Mark Hall:&lt;br /&gt;
            &lt;br /&gt;
            &amp;#160;&amp;#160;&amp;#160;&lt;strong&gt;initonly&amp;#160; :: &amp;#160;T &amp;#160;t;&lt;br /&gt;
            &lt;/strong&gt;
            &lt;br /&gt;
            Is this a declaration of an &lt;strong&gt;initonly&lt;/strong&gt; member &lt;strong&gt;t&lt;/strong&gt; of
            type &lt;strong&gt;::T&lt;/strong&gt; (i.e, &lt;strong&gt;initonly &amp;#160;::T&amp;#160; t;&lt;/strong&gt;),&amp;#160;or
            a declaration of a member &lt;strong&gt;t&lt;/strong&gt; of type &lt;strong&gt;initonly::T&lt;/strong&gt; (i.e, &lt;strong&gt;initonly::T&amp;#160;
            t;&lt;/strong&gt; where if initonly is the name of a namespace or class then this is legal
            ISO C++). Our current thinking is to adopt the rule "if it can be an identifier, it
            is," and so this case would mean the latter, either always (even if there's no such
            type) or perhaps only if there is such a type.&lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
        I feel compelled to add that the collaboration and input over the past year-plus from &lt;a href="http://www.research.att.com/~bs"&gt;Bjarne
        Stroustrup&lt;/a&gt; and the folks at &lt;a href="http://www.edg.com"&gt;EDG&lt;/a&gt; (Steve Adamczyk,
        John Spicer, and Daveed Vandevoorde) has been wonderful and invaluable in this regard
        specifically. It has really helped to have input from other experienced compiler writers,
        including in Bjarne's case the creator of the first C++ compiler and in EDG's case
        the folks who have one of the world's strongest current C++ compilers. On several
        occasions all of their input has helped get rid of inadvertent assumptions about "what's
        implementable" and "what's diagnosable" based on just VC++'s own compiler implementation
        and its source base. What's easy for one compiler implementation is not necessarily
        so for another, and it's been extremely useful to draw on the experience of comparing
        notes from two current popular ones to make sure that features can be implemented
        readily on various compiler architectures and source bases (not just VC++'s) and with
        quality user diagnostics. 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;5. Not keywords, but in a namespace scope&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        Finally, there are a few "namespaced" keywords. These make the most sense for pseudo-library
        features (ones that look and feel like library types/functions but really are special
        names known to the compiler because the compiler does special things when handling
        them). They appear in the &lt;strong&gt;stdcli&lt;/strong&gt; namespace and are: 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;strong&gt;&amp;#160; array&amp;#160;&amp;#160;&amp;#160; interior_ptr&amp;#160;&amp;#160;&amp;#160; pin_ptr&amp;#160;&amp;#160;&amp;#160;
        safe_cast&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        That's it. 
    &lt;/p&gt;
    &lt;p&gt;
        &lt;br /&gt;
        Now, for a moment let's go back to case #1, reserved words. Right now we're down to
        three reserved words. What would it take to get down to zero? Consider the cases: 
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            &lt;strong&gt;nullptr&lt;/strong&gt;: This has been proposed in WG21/J16 for C++0x, and at the
            last meeting three weeks ago the evolution working group (EWG) was favorable to it
            but wanted a few changes. The &lt;a href="http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1488.pdf"&gt;proposal
            paper&lt;/a&gt; was written by me and Bjarne, and&amp;#160;we will revise the paper for the
            next meeting to reflect the EWG direction. If C++0x does adopt the proposal and chooses
            to take the keyword &lt;strong&gt;nullptr&lt;/strong&gt; then the list of C++/CLI reserved words
            goes down to two and C++/CLI would just directly follow the C++0x design for nullptr,
            including any changes C++0x makes to it. 
        &lt;/li&gt;
        &lt;li&gt;
            &lt;strong&gt;gcnew&lt;/strong&gt;: One obvious way to avoid taking this as a reserved word would
            be to put it into bucket #1 as a spaced keyword, "&lt;strong&gt;gc new&lt;/strong&gt;". 
        &lt;/li&gt;
        &lt;li&gt;
            &lt;strong&gt;generic&lt;/strong&gt;: Similarly, a spaced keyword (possibly "&lt;strong&gt;generic template&lt;/strong&gt;")
            would avoid taking this reserved word. Unfortunately, spelling it "&amp;lt;anything&amp;gt; &lt;strong&gt;template&lt;/strong&gt;"
            is not only ugly, but seriously misleading because a generic really is not at all
            a template. 
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
        Is it worth it to push all the way down to zero reserved words in C++/CLI? There are
        pros and cons to doing so, but I've certainly always been sympathetic to the goal
        of zero reserved words; &lt;a href="http://blogs.gotdotnet.com/branbray"&gt;Brandon&lt;/a&gt; and
        others will surely tell you of my stubborn campaigning to kill off reserved words
        (I think I've killed off over a half dozen already since I took the reins of this
        effort in January, but I haven't kept an exact body count). 
    &lt;/p&gt;
    &lt;p&gt;
        I think the right time to decide whether to push for zero reserved words is probably
        near the end of the C++/CLI standards process (summer-ish 2004). At that point, when
        all other changes and refinements have been made and everything else is in its final
        form, we will have a complete (and I hope still very short) list of places where C++/CLI
        could change the meaning of an existing C++ program, and that will be the best time
        to consider them as a package and to make a decision whether to eliminate some or
        all of them in a drive-it-to-zero cleanup push. I am looking forward to seeing what
        the other participants in all C++ standards arenas, and the broader community, think
        is the right thing to do as we get there. 
    &lt;/p&gt;
    &lt;p&gt;
        Putting it all together, what's the impact on a legal ISO C++ program? Only: 
    &lt;/p&gt;
    &lt;ul&gt;
        &lt;li&gt;
            The (zero to three) reserved words, which we may get down to zero. 
        &lt;/li&gt;
        &lt;li&gt;
            Macros with the same name as a contextual keyword, which ought to be&amp;#160;rare because
            macros with all-lowercase names, never mind names that are common words, are already
            considered bad form and liable to break way more code than just C++/CLI. (For example,
            if a macro named &lt;strong&gt;event&lt;/strong&gt; existed it would already be breaking most
            attempts to use Standard C++ iostreams, because the iostreams library has an enum
            named &lt;strong&gt;event&lt;/strong&gt;.) 
        &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
        Let me illustrate the macro cases with two main examples that affect the spaced keywords: 
    &lt;/p&gt;
    &lt;p&gt;
        &amp;#160; // Example 1: this has a different meaning in ISO C++ and C++/CLI&lt;br /&gt;
        &lt;strong&gt;&amp;#160; #define interface struct&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        In ISO C++, this means change every instance of &lt;strong&gt;interface&lt;/strong&gt; to &lt;strong&gt;struct&lt;/strong&gt;.
        In C++/CLI, because "&lt;strong&gt;interface struct&lt;/strong&gt;" is a single token, the macro
        means instead to change every instance of "&lt;strong&gt;interface struct&lt;/strong&gt;" to nothing. 
    &lt;/p&gt;
    &lt;p&gt;
        Here's the simplest workaround: 
    &lt;/p&gt;
    &lt;p&gt;
        &amp;#160; // Workaround 1: this has the same meaning in both&lt;br /&gt;
        &lt;strong&gt;&amp;#160; #define interface interface__&lt;br /&gt;
        &amp;#160; #define interface__ struct&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        Here's another example of a macro that can change the meaning of a program in ISO
        C++ and C++/CLI: 
    &lt;/p&gt;
    &lt;p&gt;
        &amp;#160; // Example 2: this has a different meaning in ISO C++ and C++/CLI&lt;br /&gt;
        &lt;strong&gt;&amp;#160; #define ref const&lt;br /&gt;
        &amp;#160; ref class C { } c;&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        In ISO C++, &lt;strong&gt;ref&lt;/strong&gt; goes to &lt;strong&gt;const&lt;/strong&gt; and the last line
        defines a class &lt;strong&gt;C&lt;/strong&gt; and simultaneously declares a const object of that
        type named &lt;strong&gt;c&lt;/strong&gt;. This is legal code, albeit uncommon. In C++/CLI, the
        macro has no effect on the class declaration because "&lt;strong&gt;ref class&lt;/strong&gt;"
        is a single token (whereas the macro is looking for the token &lt;strong&gt;ref&lt;/strong&gt;&amp;#160;alone,
        not "&lt;strong&gt;ref class&lt;/strong&gt;") and so the last line defines a ref class &lt;strong&gt;C&lt;/strong&gt; and
        simultaneously declares a (non-const) object of that type named &lt;strong&gt;c&lt;/strong&gt;. 
    &lt;/p&gt;
    &lt;p&gt;
        Here's the simplest workaround: 
    &lt;/p&gt;
    &lt;p&gt;
        &amp;#160; // Workaround 2: this has the same meaning in both&lt;br /&gt;
        &lt;strong&gt;&amp;#160; #define REF const&lt;br /&gt;
        &amp;#160; REF class C { } c;&lt;/strong&gt; 
    &lt;/p&gt;
    &lt;p&gt;
        But hey, macro names are supposed to be uppercase anyway. :-) 
    &lt;/p&gt;
    &lt;p&gt;
        I hope these cases are somewhere between obscure and pathological. At any rate, macros
        with short and common names are generally unusual in the wild because they just break
        so much stuff. I would rate example 1 above as fairly obscure (although windows.h
        has exactly that line in it, alas) and example 2 as probably outright pathological
        (as I would rate all macros with short and common names). 
    &lt;/p&gt;
    &lt;p&gt;
        Whew. That's all for tonight. 
    &lt;/p&gt;
&lt;/body&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=53519" width="1" height="1"&gt;</content><author><name>hsutter</name><uri>http://blogs.msdn.com/members/hsutter.aspx</uri></author></entry></feed>